mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-23 03:51:29 +00:00
d077483577
* v3 import renamings * tidy * fmt * rev * Update beacon-chain/core/epoch/precompute/reward_penalty_test.go * Update beacon-chain/core/helpers/validators_test.go * Update beacon-chain/db/alias.go * Update beacon-chain/db/alias.go * Update beacon-chain/db/alias.go * Update beacon-chain/db/iface/BUILD.bazel * Update beacon-chain/db/kv/kv.go * Update beacon-chain/db/kv/state.go * Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go * Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go * Update beacon-chain/sync/initial-sync/service.go * fix deps * fix bad replacements * fix bad replacements * change back * gohashtree version * fix deps Co-authored-by: Nishant Das <nishdas93@gmail.com> Co-authored-by: Potuz <potuz@prysmaticlabs.com>
43 lines
972 B
Go
43 lines
972 B
Go
package stateutil
|
|
|
|
import (
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/v3/crypto/hash"
|
|
"github.com/prysmaticlabs/prysm/v3/encoding/ssz"
|
|
)
|
|
|
|
func ArraysRoot(input [][]byte, length uint64) ([32]byte, error) {
|
|
hashFunc := hash.CustomSHA256Hasher()
|
|
|
|
leaves := make([][32]byte, length)
|
|
for i, chunk := range input {
|
|
copy(leaves[i][:], chunk)
|
|
}
|
|
res, err := merkleize(leaves, length, hashFunc)
|
|
if err != nil {
|
|
return [32]byte{}, err
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func merkleize(leaves [][32]byte, length uint64,
|
|
hasher func([]byte) [32]byte) ([32]byte, error) {
|
|
if len(leaves) == 0 {
|
|
return [32]byte{}, errors.New("zero leaves provided")
|
|
}
|
|
if len(leaves) == 1 {
|
|
return leaves[0], nil
|
|
}
|
|
hashLayer := leaves
|
|
layers := make([][][32]byte, ssz.Depth(length)+1)
|
|
layers[0] = hashLayer
|
|
var err error
|
|
_, hashLayer, err = MerkleizeTrieLeaves(layers, hashLayer, hasher)
|
|
if err != nil {
|
|
return [32]byte{}, err
|
|
}
|
|
root := hashLayer[0]
|
|
return root, nil
|
|
}
|