prysm-pulse/beacon-chain/state/stateutil/sync_committee.root.go
terence tsao 86efa87101
Refactor sync and participation field roots (#9703)
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2021-09-29 17:46:51 +00:00

50 lines
1.5 KiB
Go

package stateutil
import (
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/ssz"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
)
// SyncCommitteeRoot computes the HashTreeRoot Merkleization of a commitee root.
// a SyncCommitteeRoot struct according to the eth2
// Simple Serialize specification.
func SyncCommitteeRoot(committee *ethpb.SyncCommittee) ([32]byte, error) {
hasher := hash.CustomSHA256Hasher()
var fieldRoots [][32]byte
if committee == nil {
return [32]byte{}, nil
}
// Field 1: Vector[BLSPubkey, SYNC_COMMITTEE_SIZE]
pubKeyRoots := make([][32]byte, 0)
for _, pubkey := range committee.Pubkeys {
r, err := merkleizePubkey(hasher, pubkey)
if err != nil {
return [32]byte{}, err
}
pubKeyRoots = append(pubKeyRoots, r)
}
pubkeyRoot, err := ssz.BitwiseMerkleizeArrays(hasher, pubKeyRoots, uint64(len(pubKeyRoots)), uint64(len(pubKeyRoots)))
if err != nil {
return [32]byte{}, err
}
// Field 2: BLSPubkey
aggregateKeyRoot, err := merkleizePubkey(hasher, committee.AggregatePubkey)
if err != nil {
return [32]byte{}, err
}
fieldRoots = [][32]byte{pubkeyRoot, aggregateKeyRoot}
return ssz.BitwiseMerkleizeArrays(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
}
func merkleizePubkey(hasher ssz.HashFn, pubkey []byte) ([32]byte, error) {
chunks, err := ssz.Pack([][]byte{pubkey})
if err != nil {
return [32]byte{}, err
}
return ssz.BitwiseMerkleize(hasher, chunks, uint64(len(chunks)), uint64(len(chunks)))
}