github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/state/stateutil/block_header_root.go (about)

     1  package stateutil
     2  
     3  import (
     4  	"encoding/binary"
     5  
     6  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     7  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
     8  	"github.com/prysmaticlabs/prysm/shared/hashutil"
     9  	"github.com/prysmaticlabs/prysm/shared/htrutils"
    10  )
    11  
    12  // BlockHeaderRoot computes the HashTreeRoot Merkleization of
    13  // a BeaconBlockHeader struct according to the Ethereum
    14  // Simple Serialize specification.
    15  func BlockHeaderRoot(header *ethpb.BeaconBlockHeader) ([32]byte, error) {
    16  	fieldRoots := make([][]byte, 5)
    17  	if header != nil {
    18  		headerSlotBuf := make([]byte, 8)
    19  		binary.LittleEndian.PutUint64(headerSlotBuf, uint64(header.Slot))
    20  		headerSlotRoot := bytesutil.ToBytes32(headerSlotBuf)
    21  		fieldRoots[0] = headerSlotRoot[:]
    22  		proposerIdxBuf := make([]byte, 8)
    23  		binary.LittleEndian.PutUint64(proposerIdxBuf, uint64(header.ProposerIndex))
    24  		proposerIndexRoot := bytesutil.ToBytes32(proposerIdxBuf)
    25  		fieldRoots[1] = proposerIndexRoot[:]
    26  		parentRoot := bytesutil.ToBytes32(header.ParentRoot)
    27  		fieldRoots[2] = parentRoot[:]
    28  		stateRoot := bytesutil.ToBytes32(header.StateRoot)
    29  		fieldRoots[3] = stateRoot[:]
    30  		bodyRoot := bytesutil.ToBytes32(header.BodyRoot)
    31  		fieldRoots[4] = bodyRoot[:]
    32  	}
    33  	return htrutils.BitwiseMerkleize(hashutil.CustomSHA256Hasher(), fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
    34  }