2021-11-29 16:30:17 +00:00
|
|
|
package stateutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-03-17 18:52:56 +00:00
|
|
|
params "github.com/prysmaticlabs/prysm/v4/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/ssz"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
2021-11-29 16:30:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RootsArrayHashTreeRoot computes the Merkle root of arrays of 32-byte hashes, such as [64][32]byte
|
|
|
|
// according to the Simple Serialize specification of Ethereum.
|
2022-02-22 09:27:51 +00:00
|
|
|
func RootsArrayHashTreeRoot(vals [][]byte, length uint64) ([32]byte, error) {
|
2022-05-09 13:02:34 +00:00
|
|
|
return ArraysRoot(vals, length)
|
2021-11-29 16:30:17 +00:00
|
|
|
}
|
|
|
|
|
2022-05-09 13:02:34 +00:00
|
|
|
func EpochAttestationsRoot(atts []*ethpb.PendingAttestation) ([32]byte, error) {
|
2023-01-25 10:42:03 +00:00
|
|
|
max := uint64(params.BeaconConfig().CurrentEpochAttestationsLength())
|
2021-11-29 16:30:17 +00:00
|
|
|
if uint64(len(atts)) > max {
|
|
|
|
return [32]byte{}, fmt.Errorf("epoch attestation exceeds max length %d", max)
|
|
|
|
}
|
|
|
|
|
2022-03-04 15:19:07 +00:00
|
|
|
roots := make([][32]byte, len(atts))
|
2021-11-29 16:30:17 +00:00
|
|
|
for i := 0; i < len(atts); i++ {
|
2023-03-24 14:05:31 +00:00
|
|
|
pendingRoot, err := pendingAttestationRoot(atts[i])
|
2021-11-29 16:30:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, errors.Wrap(err, "could not attestation merkleization")
|
|
|
|
}
|
2022-03-04 15:19:07 +00:00
|
|
|
roots[i] = pendingRoot
|
2021-11-29 16:30:17 +00:00
|
|
|
}
|
|
|
|
|
2023-03-24 14:05:31 +00:00
|
|
|
attsRootsRoot, err := ssz.BitwiseMerkleize(roots, uint64(len(roots)), params.BeaconConfig().CurrentEpochAttestationsLength())
|
2021-11-29 16:30:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, errors.Wrap(err, "could not compute epoch attestations merkleization")
|
|
|
|
}
|
|
|
|
attsLenBuf := new(bytes.Buffer)
|
|
|
|
if err := binary.Write(attsLenBuf, binary.LittleEndian, uint64(len(atts))); err != nil {
|
|
|
|
return [32]byte{}, errors.Wrap(err, "could not marshal epoch attestations length")
|
|
|
|
}
|
|
|
|
// We need to mix in the length of the slice.
|
|
|
|
attsLenRoot := make([]byte, 32)
|
|
|
|
copy(attsLenRoot, attsLenBuf.Bytes())
|
|
|
|
res := ssz.MixInLength(attsRootsRoot, attsLenRoot)
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-03-24 14:05:31 +00:00
|
|
|
func pendingAttestationRoot(att *ethpb.PendingAttestation) ([32]byte, error) {
|
2021-11-29 16:30:17 +00:00
|
|
|
if att == nil {
|
|
|
|
return [32]byte{}, errors.New("nil pending attestation")
|
|
|
|
}
|
2023-03-17 11:41:02 +00:00
|
|
|
return PendingAttRootWithHasher(att)
|
2021-11-29 16:30:17 +00:00
|
|
|
}
|