2021-03-18 23:29:06 +00:00
|
|
|
package stateutil
|
2021-03-16 18:14:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
|
2021-09-15 22:55:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/crypto/hash"
|
2021-09-23 15:23:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2021-09-21 15:02:48 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/ssz"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-03-16 18:14:26 +00:00
|
|
|
)
|
|
|
|
|
2021-03-18 23:29:06 +00:00
|
|
|
// BlockHeaderRoot computes the HashTreeRoot Merkleization of
|
2021-06-26 19:00:33 +00:00
|
|
|
// a BeaconBlockHeader struct according to the Ethereum
|
2021-03-16 18:14:26 +00:00
|
|
|
// Simple Serialize specification.
|
2021-03-18 23:29:06 +00:00
|
|
|
func BlockHeaderRoot(header *ethpb.BeaconBlockHeader) ([32]byte, error) {
|
2022-03-04 15:19:07 +00:00
|
|
|
fieldRoots := make([][32]byte, 5)
|
2021-03-16 18:14:26 +00:00
|
|
|
if header != nil {
|
|
|
|
headerSlotBuf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(headerSlotBuf, uint64(header.Slot))
|
|
|
|
headerSlotRoot := bytesutil.ToBytes32(headerSlotBuf)
|
2022-03-04 15:19:07 +00:00
|
|
|
fieldRoots[0] = headerSlotRoot
|
2021-03-16 18:14:26 +00:00
|
|
|
proposerIdxBuf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(proposerIdxBuf, uint64(header.ProposerIndex))
|
|
|
|
proposerIndexRoot := bytesutil.ToBytes32(proposerIdxBuf)
|
2022-03-04 15:19:07 +00:00
|
|
|
fieldRoots[1] = proposerIndexRoot
|
2021-03-16 18:14:26 +00:00
|
|
|
parentRoot := bytesutil.ToBytes32(header.ParentRoot)
|
2022-03-04 15:19:07 +00:00
|
|
|
fieldRoots[2] = parentRoot
|
2021-03-16 18:14:26 +00:00
|
|
|
stateRoot := bytesutil.ToBytes32(header.StateRoot)
|
2022-03-04 15:19:07 +00:00
|
|
|
fieldRoots[3] = stateRoot
|
2021-03-16 18:14:26 +00:00
|
|
|
bodyRoot := bytesutil.ToBytes32(header.BodyRoot)
|
2022-03-04 15:19:07 +00:00
|
|
|
fieldRoots[4] = bodyRoot
|
2021-03-16 18:14:26 +00:00
|
|
|
}
|
2021-09-21 15:02:48 +00:00
|
|
|
return ssz.BitwiseMerkleize(hash.CustomSHA256Hasher(), fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
|
2021-03-16 18:14:26 +00:00
|
|
|
}
|