mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 13:28:01 -05:00
34 lines
1.2 KiB
Go
34 lines
1.2 KiB
Go
package stateutil
|
|
|
|
import (
|
|
"encoding/binary"
|
|
|
|
"github.com/OffchainLabs/prysm/v7/encoding/bytesutil"
|
|
"github.com/OffchainLabs/prysm/v7/encoding/ssz"
|
|
ethpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1"
|
|
)
|
|
|
|
// BlockHeaderRoot computes the HashTreeRoot Merkleization of
|
|
// a BeaconBlockHeader struct according to the Ethereum
|
|
// Simple Serialize specification.
|
|
func BlockHeaderRoot(header *ethpb.BeaconBlockHeader) ([32]byte, error) {
|
|
fieldRoots := make([][32]byte, 5)
|
|
if header != nil {
|
|
headerSlotBuf := make([]byte, 8)
|
|
binary.LittleEndian.PutUint64(headerSlotBuf, uint64(header.Slot))
|
|
headerSlotRoot := bytesutil.ToBytes32(headerSlotBuf)
|
|
fieldRoots[0] = headerSlotRoot
|
|
proposerIdxBuf := make([]byte, 8)
|
|
binary.LittleEndian.PutUint64(proposerIdxBuf, uint64(header.ProposerIndex))
|
|
proposerIndexRoot := bytesutil.ToBytes32(proposerIdxBuf)
|
|
fieldRoots[1] = proposerIndexRoot
|
|
parentRoot := bytesutil.ToBytes32(header.ParentRoot)
|
|
fieldRoots[2] = parentRoot
|
|
stateRoot := bytesutil.ToBytes32(header.StateRoot)
|
|
fieldRoots[3] = stateRoot
|
|
bodyRoot := bytesutil.ToBytes32(header.BodyRoot)
|
|
fieldRoots[4] = bodyRoot
|
|
}
|
|
return ssz.BitwiseMerkleize(fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
|
|
}
|