mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-17 15:28:45 +00:00
9e5cc81340
* new ssz hash tree root * Merge branch 'master' into new-ssz-state * better comments on func * add errors instead of panic in state * utilize errors wrap everywhere * include bench * added bench info * equality test * dup * gaz * use new hash tree root in state transition * fix build * separate test package * three targets failign * single target fails * please test targets...pass for me * revert * Merge branch 'master' into new-ssz-state * rev * Merge branch 'new-ssz-state' of github.com:prysmaticlabs/prysm into new-ssz-state * broken build * Merge branch 'master' into new-ssz-state * gaz * Merge branch 'new-ssz-state' of github.com:prysmaticlabs/prysm into new-ssz-state * ssz workspace * master ssz * Merge branch 'master' into new-ssz-state * resolve conf * resolve some conflicts and fix up broken file * fix up build file issues and sync * eth1 data votes included * further abstractions, simplifications * Merge branch 'master' into new-ssz-state * gaz * Merge branch 'new-ssz-state' of github.com:prysmaticlabs/prysm into new-ssz-state * feature flag gating * add field count test * Merge branch 'master' into new-ssz-state * resolving ivan feedback * Merge branch 'new-ssz-state' of github.com:prysmaticlabs/prysm into new-ssz-state * gaz * Merge branch 'master' into new-ssz-state * addressed * Merge branch 'new-ssz-state' of github.com:prysmaticlabs/prysm into new-ssz-state
103 lines
3.1 KiB
Go
103 lines
3.1 KiB
Go
package stateutil
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
|
|
"github.com/pkg/errors"
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
)
|
|
|
|
func attestationDataRoot(data *ethpb.AttestationData) ([32]byte, error) {
|
|
fieldRoots := make([][]byte, 5)
|
|
|
|
// Slot.
|
|
slotBuf := make([]byte, 8)
|
|
binary.LittleEndian.PutUint64(slotBuf, data.Slot)
|
|
slotRoot := bytesutil.ToBytes32(slotBuf)
|
|
fieldRoots[0] = slotRoot[:]
|
|
|
|
// CommitteeIndex.
|
|
indexBuf := make([]byte, 8)
|
|
binary.LittleEndian.PutUint64(indexBuf, data.CommitteeIndex)
|
|
interRoot := bytesutil.ToBytes32(indexBuf)
|
|
fieldRoots[1] = interRoot[:]
|
|
|
|
// Beacon block root.
|
|
fieldRoots[2] = data.BeaconBlockRoot
|
|
|
|
// Source
|
|
sourceRoot, err := checkpointRoot(data.Source)
|
|
if err != nil {
|
|
return [32]byte{}, errors.Wrap(err, "could not compute source checkpoint merkleization")
|
|
}
|
|
fieldRoots[3] = sourceRoot[:]
|
|
|
|
// Target
|
|
targetRoot, err := checkpointRoot(data.Target)
|
|
if err != nil {
|
|
return [32]byte{}, errors.Wrap(err, "could not compute target checkpoint merkleization")
|
|
}
|
|
fieldRoots[4] = targetRoot[:]
|
|
|
|
return bitwiseMerkleize(fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
|
|
}
|
|
|
|
func pendingAttestationRoot(att *pb.PendingAttestation) ([32]byte, error) {
|
|
fieldRoots := make([][]byte, 4)
|
|
|
|
// Bitfield.
|
|
aggregationRoot, err := bitlistRoot(att.AggregationBits, 2048)
|
|
if err != nil {
|
|
return [32]byte{}, err
|
|
}
|
|
fieldRoots[0] = aggregationRoot[:]
|
|
|
|
// Attestation data.
|
|
attDataRoot, err := attestationDataRoot(att.Data)
|
|
if err != nil {
|
|
return [32]byte{}, err
|
|
}
|
|
fieldRoots[1] = attDataRoot[:]
|
|
|
|
// Inclusion delay.
|
|
inclusionBuf := make([]byte, 8)
|
|
binary.LittleEndian.PutUint64(inclusionBuf, att.InclusionDelay)
|
|
inclusionRoot := bytesutil.ToBytes32(inclusionBuf)
|
|
fieldRoots[2] = inclusionRoot[:]
|
|
|
|
// Proposer index.
|
|
proposerBuf := make([]byte, 8)
|
|
binary.LittleEndian.PutUint64(proposerBuf, att.ProposerIndex)
|
|
proposerRoot := bytesutil.ToBytes32(proposerBuf)
|
|
fieldRoots[3] = proposerRoot[:]
|
|
|
|
return bitwiseMerkleize(fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
|
|
}
|
|
|
|
func epochAttestationsRoot(atts []*pb.PendingAttestation) ([32]byte, error) {
|
|
attsRoots := make([][]byte, 0)
|
|
for i := 0; i < len(atts); i++ {
|
|
pendingRoot, err := pendingAttestationRoot(atts[i])
|
|
if err != nil {
|
|
return [32]byte{}, errors.Wrap(err, "could not attestation merkleization")
|
|
}
|
|
attsRoots = append(attsRoots, pendingRoot[:])
|
|
}
|
|
attsRootsRoot, err := bitwiseMerkleize(attsRoots, uint64(len(attsRoots)), params.BeaconConfig().MaxAttestations*params.BeaconConfig().SlotsPerEpoch)
|
|
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())
|
|
return mixInLength(attsRootsRoot, attsLenRoot), nil
|
|
}
|