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-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
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
)
|
|
|
|
|
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}
|
|
|
|
|
2021-09-21 15:02:48 +00:00
|
|
|
return ssz.BitwiseMerkleizeArrays(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
|
2021-03-18 23:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PendingAttEncKey returns the encoded key in bytes of input `pendingAttestation`,
|
|
|
|
// the returned key bytes can be used for caching purposes.
|
2021-07-29 21:45:17 +00:00
|
|
|
func PendingAttEncKey(att *ethpb.PendingAttestation) []byte {
|
2021-03-18 23:29:06 +00:00
|
|
|
enc := make([]byte, 2192)
|
|
|
|
|
|
|
|
if att != nil {
|
|
|
|
copy(enc[0:2048], att.AggregationBits)
|
|
|
|
|
|
|
|
inclusionBuf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(inclusionBuf, uint64(att.InclusionDelay))
|
|
|
|
copy(enc[2048:2056], inclusionBuf)
|
|
|
|
|
|
|
|
attDataBuf := marshalAttData(att.Data)
|
|
|
|
copy(enc[2056:2184], attDataBuf)
|
|
|
|
|
|
|
|
proposerBuf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(proposerBuf, uint64(att.ProposerIndex))
|
|
|
|
copy(enc[2184:2192], proposerBuf)
|
|
|
|
}
|
|
|
|
|
|
|
|
return enc
|
|
|
|
}
|
|
|
|
|
2021-09-21 15:02:48 +00:00
|
|
|
func attDataRootWithHasher(hasher ssz.HashFn, data *ethpb.AttestationData) ([32]byte, error) {
|
2021-03-18 23:29:06 +00:00
|
|
|
fieldRoots := make([][]byte, 5)
|
|
|
|
|
|
|
|
if data != nil {
|
|
|
|
// Slot.
|
|
|
|
slotBuf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(slotBuf, uint64(data.Slot))
|
|
|
|
slotRoot := bytesutil.ToBytes32(slotBuf)
|
|
|
|
fieldRoots[0] = slotRoot[:]
|
|
|
|
|
|
|
|
// CommitteeIndex.
|
|
|
|
indexBuf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(indexBuf, uint64(data.CommitteeIndex))
|
|
|
|
interRoot := bytesutil.ToBytes32(indexBuf)
|
|
|
|
fieldRoots[1] = interRoot[:]
|
|
|
|
|
|
|
|
// Beacon block root.
|
|
|
|
blockRoot := bytesutil.ToBytes32(data.BeaconBlockRoot)
|
|
|
|
fieldRoots[2] = blockRoot[:]
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
fieldRoots[3] = sourceRoot[:]
|
|
|
|
|
|
|
|
// Target
|
2021-09-21 15:02:48 +00:00
|
|
|
targetRoot, 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")
|
|
|
|
}
|
|
|
|
fieldRoots[4] = targetRoot[:]
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func marshalAttData(data *ethpb.AttestationData) []byte {
|
|
|
|
enc := make([]byte, 128)
|
|
|
|
|
|
|
|
if data != nil {
|
|
|
|
// Slot.
|
|
|
|
slotBuf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(slotBuf, uint64(data.Slot))
|
|
|
|
copy(enc[0:8], slotBuf)
|
|
|
|
|
|
|
|
// Committee index.
|
|
|
|
indexBuf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(indexBuf, uint64(data.CommitteeIndex))
|
|
|
|
copy(enc[8:16], indexBuf)
|
|
|
|
|
|
|
|
copy(enc[16:48], data.BeaconBlockRoot)
|
|
|
|
|
|
|
|
// Source epoch and root.
|
|
|
|
if data.Source != nil {
|
|
|
|
sourceEpochBuf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(sourceEpochBuf, uint64(data.Source.Epoch))
|
|
|
|
copy(enc[48:56], sourceEpochBuf)
|
|
|
|
copy(enc[56:88], data.Source.Root)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Target.
|
|
|
|
if data.Target != nil {
|
|
|
|
targetEpochBuf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(targetEpochBuf, uint64(data.Target.Epoch))
|
|
|
|
copy(enc[88:96], targetEpochBuf)
|
|
|
|
copy(enc[96:128], data.Target.Root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return enc
|
|
|
|
}
|