github.com/prysmaticlabs/prysm@v1.4.4/shared/blockutil/block_utils.go (about)

     1  package blockutil
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     6  )
     7  
     8  // SignedBeaconBlockHeaderFromBlock function to retrieve signed block header from block.
     9  func SignedBeaconBlockHeaderFromBlock(block *ethpb.SignedBeaconBlock) (*ethpb.SignedBeaconBlockHeader, error) {
    10  	if block.Block == nil || block.Block.Body == nil {
    11  		return nil, errors.New("nil block")
    12  	}
    13  
    14  	bodyRoot, err := block.Block.Body.HashTreeRoot()
    15  	if err != nil {
    16  		return nil, errors.Wrap(err, "failed to get body root of block")
    17  	}
    18  	return &ethpb.SignedBeaconBlockHeader{
    19  		Header: &ethpb.BeaconBlockHeader{
    20  			Slot:          block.Block.Slot,
    21  			ProposerIndex: block.Block.ProposerIndex,
    22  			ParentRoot:    block.Block.ParentRoot,
    23  			StateRoot:     block.Block.StateRoot,
    24  			BodyRoot:      bodyRoot[:],
    25  		},
    26  		Signature: block.Signature,
    27  	}, nil
    28  }
    29  
    30  // BeaconBlockHeaderFromBlock function to retrieve block header from block.
    31  func BeaconBlockHeaderFromBlock(block *ethpb.BeaconBlock) (*ethpb.BeaconBlockHeader, error) {
    32  	if block.Body == nil {
    33  		return nil, errors.New("nil block body")
    34  	}
    35  
    36  	bodyRoot, err := block.Body.HashTreeRoot()
    37  	if err != nil {
    38  		return nil, errors.Wrap(err, "failed to get body root of block")
    39  	}
    40  	return &ethpb.BeaconBlockHeader{
    41  		Slot:          block.Slot,
    42  		ProposerIndex: block.ProposerIndex,
    43  		ParentRoot:    block.ParentRoot,
    44  		StateRoot:     block.StateRoot,
    45  		BodyRoot:      bodyRoot[:],
    46  	}, nil
    47  }