2021-03-18 23:29:06 +00:00
|
|
|
package stateutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
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-18 23:29:06 +00:00
|
|
|
)
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// PendingAttRootWithHasher describes a method from which the hash tree root
|
2021-03-18 23:29:06 +00:00
|
|
|
// of a pending attestation is returned.
|
2021-09-21 15:02:48 +00:00
|
|
|
func PendingAttRootWithHasher(hasher ssz.HashFn, att *ethpb.PendingAttestation) ([32]byte, error) {
|
2021-03-18 23:29:06 +00:00
|
|
|
var fieldRoots [][32]byte
|
|
|
|
|
|
|
|
// Bitfield.
|
2021-09-21 15:02:48 +00:00
|
|
|
aggregationRoot, err := ssz.BitlistRoot(hasher, att.AggregationBits, params.BeaconConfig().MaxValidatorsPerCommittee)
|
2021-03-18 23:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
|
|
|
// Attestation data.
|
|
|
|
attDataRoot, err := attDataRootWithHasher(hasher, att.Data)
|
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
|
|
|
inclusionBuf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(inclusionBuf, uint64(att.InclusionDelay))
|
|
|
|
// Inclusion delay.
|
|
|
|
inclusionRoot := bytesutil.ToBytes32(inclusionBuf)
|
|
|
|
|
|
|
|
proposerBuf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(proposerBuf, uint64(att.ProposerIndex))
|
|
|
|
// Proposer index.
|
|
|
|
proposerRoot := bytesutil.ToBytes32(proposerBuf)
|
|
|
|
|
|
|
|
fieldRoots = [][32]byte{aggregationRoot, attDataRoot, inclusionRoot, proposerRoot}
|
|
|
|
|
2022-03-04 15:19:07 +00:00
|
|
|
return ssz.BitwiseMerkleize(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
|
2021-03-18 23:29:06 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 15:02:48 +00:00
|
|
|
func attDataRootWithHasher(hasher ssz.HashFn, data *ethpb.AttestationData) ([32]byte, error) {
|
2022-03-04 15:19:07 +00:00
|
|
|
fieldRoots := make([][32]byte, 5)
|
2021-03-18 23:29:06 +00:00
|
|
|
|
|
|
|
if data != nil {
|
|
|
|
// Slot.
|
|
|
|
slotBuf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(slotBuf, uint64(data.Slot))
|
2022-03-04 15:19:07 +00:00
|
|
|
fieldRoots[0] = bytesutil.ToBytes32(slotBuf)
|
2021-03-18 23:29:06 +00:00
|
|
|
|
|
|
|
// CommitteeIndex.
|
|
|
|
indexBuf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(indexBuf, uint64(data.CommitteeIndex))
|
2022-03-04 15:19:07 +00:00
|
|
|
fieldRoots[1] = bytesutil.ToBytes32(indexBuf)
|
2021-03-18 23:29:06 +00:00
|
|
|
|
|
|
|
// Beacon block root.
|
2022-03-04 15:19:07 +00:00
|
|
|
fieldRoots[2] = bytesutil.ToBytes32(data.BeaconBlockRoot)
|
2021-03-18 23:29:06 +00:00
|
|
|
|
|
|
|
// Source
|
2021-09-21 15:02:48 +00:00
|
|
|
sourceRoot, err := ssz.CheckpointRoot(hasher, data.Source)
|
2021-03-18 23:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, errors.Wrap(err, "could not compute source checkpoint merkleization")
|
|
|
|
}
|
2022-03-04 15:19:07 +00:00
|
|
|
fieldRoots[3] = sourceRoot
|
2021-03-18 23:29:06 +00:00
|
|
|
|
|
|
|
// Target
|
2022-03-04 15:19:07 +00:00
|
|
|
fieldRoots[4], err = ssz.CheckpointRoot(hasher, data.Target)
|
2021-03-18 23:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, errors.Wrap(err, "could not compute target checkpoint merkleization")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 15:02:48 +00:00
|
|
|
return ssz.BitwiseMerkleize(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
|
2021-03-18 23:29:06 +00:00
|
|
|
}
|