mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
21bdbd548a
* v0 * getters/setters * init and copy * hasher * all the nice stuff * make bazel happy * remove tests for smaller PR * remove old states * move files * import fixes * custom MarshalSSZ * fixed deadlock * copy version when copying state * correct issues in state_trie * fix Copy() * better e2e comment * add code to minimal state * spectest test * Revert "Auxiliary commit to revert individual files from 84154423464e8372f7e0a03367403656ac5cd78e" This reverts commit 9602599d183081291dfa0ba4f1036430f63a7822. * native state assert * always error * always log * more native state usage * cleanup * remove empty line * Revert "spectests" This reverts commit 1c49bed5d1cf6224afaf21e18562bf72fae5d2b6. # Conflicts: # beacon-chain/powchain/service.go # beacon-chain/state/v1/state_trie.go # beacon-chain/state/v2/state_trie.go # beacon-chain/state/v3/state_trie.go # testing/spectest/shared/phase0/finality/BUILD.bazel # testing/spectest/shared/phase0/finality/runner.go * dedup field trie * fix test issues * cleanup * use correct field num in FinalizedRootProof * use existing version constant * halfway there * "working" version * some fixes * fix field nums in tests * rename v0types to nativetypes * Revert "Auxiliary commit to revert individual files from dc549b1cf8e724bd08cee1ecc760ff3771d5592d" This reverts commit 7254d3070d8693b283fc686a2e01a822ecbac1b3. * uncomment code * remove map size * Revert "Revert "spectests"" This reverts commit 39c271ae6b57d6a3737e2c202cd8407857475e56. * use reverse map * Revert "Revert "Revert "spectests""" This reverts commit 19ba8cf95cdca689357c8234a262e08cccbafef4. * finally found the bug (cherry picked from commit a5414c4be1bdb61a50b391ea5301895e772cc5e9) * simplify populateFieldIndexes * fix copy (cherry picked from commit 7da4fb8cf51557ef931bb781872ea52fc6731af5) * remove native state from e2e * remove index map * unsupported functions * Use ProtobufBeaconState() from native state * tests * typo * reduce complexity of `SaveStatesEfficient` * remove unused receiver name * update doc.go * fix test assertion * fix test assertion 2 * Phase0 justification bits * bring back state tests * rename fieldIndexRev * versioning of ToProto * remove version check from unexported function * hasher tests * don't return error from JustificationBits * extract fieldConvertersNative * helper error function * use fieldConvertersNative * Introduce RealPosition method on FieldIndex * use RealPosition in hasher * remove unused fields * remove TestAppendBeyondIndicesLimit (cherry picked from commit 3017e700282969c30006b64c95c21ffe6b166f8b) * simplify RealPosition * rename field interface * use helper in proofs.go * Update beacon-chain/core/altair/upgrade.go Co-authored-by: Nishant Das <nishdas93@gmail.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
241 lines
9.3 KiB
Go
241 lines
9.3 KiB
Go
package state_native
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
|
|
"github.com/pkg/errors"
|
|
nativetypes "github.com/prysmaticlabs/prysm/beacon-chain/state/state-native/types"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
|
"github.com/prysmaticlabs/prysm/crypto/hash"
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/encoding/ssz"
|
|
"github.com/prysmaticlabs/prysm/runtime/version"
|
|
"go.opencensus.io/trace"
|
|
)
|
|
|
|
// ComputeFieldRootsWithHasher hashes the provided state and returns its respective field roots.
|
|
func ComputeFieldRootsWithHasher(ctx context.Context, state *BeaconState) ([][]byte, error) {
|
|
_, span := trace.StartSpan(ctx, "ComputeFieldRootsWithHasher")
|
|
defer span.End()
|
|
|
|
if state == nil {
|
|
return nil, errors.New("nil state")
|
|
}
|
|
hasher := hash.CustomSHA256Hasher()
|
|
var fieldRoots [][]byte
|
|
switch state.version {
|
|
case version.Phase0:
|
|
fieldRoots = make([][]byte, params.BeaconConfig().BeaconStateFieldCount)
|
|
case version.Altair:
|
|
fieldRoots = make([][]byte, params.BeaconConfig().BeaconStateAltairFieldCount)
|
|
case version.Bellatrix:
|
|
fieldRoots = make([][]byte, params.BeaconConfig().BeaconStateBellatrixFieldCount)
|
|
}
|
|
|
|
// Genesis time root.
|
|
genesisRoot := ssz.Uint64Root(state.genesisTime)
|
|
fieldRoots[nativetypes.GenesisTime.RealPosition()] = genesisRoot[:]
|
|
|
|
// Genesis validators root.
|
|
r := [32]byte{}
|
|
copy(r[:], state.genesisValidatorsRoot[:])
|
|
fieldRoots[nativetypes.GenesisValidatorsRoot.RealPosition()] = r[:]
|
|
|
|
// Slot root.
|
|
slotRoot := ssz.Uint64Root(uint64(state.slot))
|
|
fieldRoots[nativetypes.Slot.RealPosition()] = slotRoot[:]
|
|
|
|
// Fork data structure root.
|
|
forkHashTreeRoot, err := ssz.ForkRoot(state.fork)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute fork merkleization")
|
|
}
|
|
fieldRoots[nativetypes.Fork.RealPosition()] = forkHashTreeRoot[:]
|
|
|
|
// BeaconBlockHeader data structure root.
|
|
headerHashTreeRoot, err := stateutil.BlockHeaderRoot(state.latestBlockHeader)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute block header merkleization")
|
|
}
|
|
fieldRoots[nativetypes.LatestBlockHeader.RealPosition()] = headerHashTreeRoot[:]
|
|
|
|
// BlockRoots array root.
|
|
bRoots := make([][]byte, len(state.blockRoots))
|
|
for i := range bRoots {
|
|
bRoots[i] = state.blockRoots[i][:]
|
|
}
|
|
blockRootsRoot, err := stateutil.ArraysRoot(bRoots, fieldparams.BlockRootsLength)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute block roots merkleization")
|
|
}
|
|
fieldRoots[nativetypes.BlockRoots.RealPosition()] = blockRootsRoot[:]
|
|
|
|
// StateRoots array root.
|
|
sRoots := make([][]byte, len(state.stateRoots))
|
|
for i := range sRoots {
|
|
sRoots[i] = state.stateRoots[i][:]
|
|
}
|
|
stateRootsRoot, err := stateutil.ArraysRoot(sRoots, fieldparams.StateRootsLength)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute state roots merkleization")
|
|
}
|
|
fieldRoots[nativetypes.StateRoots.RealPosition()] = stateRootsRoot[:]
|
|
|
|
// HistoricalRoots slice root.
|
|
hRoots := make([][]byte, len(state.historicalRoots))
|
|
for i := range hRoots {
|
|
hRoots[i] = state.historicalRoots[i][:]
|
|
}
|
|
historicalRootsRt, err := ssz.ByteArrayRootWithLimit(hRoots, fieldparams.HistoricalRootsLength)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute historical roots merkleization")
|
|
}
|
|
fieldRoots[nativetypes.HistoricalRoots.RealPosition()] = historicalRootsRt[:]
|
|
|
|
// Eth1Data data structure root.
|
|
eth1HashTreeRoot, err := stateutil.Eth1Root(hasher, state.eth1Data)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute eth1data merkleization")
|
|
}
|
|
fieldRoots[nativetypes.Eth1Data.RealPosition()] = eth1HashTreeRoot[:]
|
|
|
|
// Eth1DataVotes slice root.
|
|
eth1VotesRoot, err := stateutil.Eth1DataVotesRoot(state.eth1DataVotes)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute eth1data votes merkleization")
|
|
}
|
|
fieldRoots[nativetypes.Eth1DataVotes.RealPosition()] = eth1VotesRoot[:]
|
|
|
|
// Eth1DepositIndex root.
|
|
eth1DepositIndexBuf := make([]byte, 8)
|
|
binary.LittleEndian.PutUint64(eth1DepositIndexBuf, state.eth1DepositIndex)
|
|
eth1DepositBuf := bytesutil.ToBytes32(eth1DepositIndexBuf)
|
|
fieldRoots[nativetypes.Eth1DepositIndex.RealPosition()] = eth1DepositBuf[:]
|
|
|
|
// Validators slice root.
|
|
validatorsRoot, err := stateutil.ValidatorRegistryRoot(state.validators)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute validator registry merkleization")
|
|
}
|
|
fieldRoots[nativetypes.Validators.RealPosition()] = validatorsRoot[:]
|
|
|
|
// Balances slice root.
|
|
balancesRoot, err := stateutil.Uint64ListRootWithRegistryLimit(state.balances)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute validator balances merkleization")
|
|
}
|
|
fieldRoots[nativetypes.Balances.RealPosition()] = balancesRoot[:]
|
|
|
|
// RandaoMixes array root.
|
|
mixes := make([][]byte, len(state.randaoMixes))
|
|
for i := range mixes {
|
|
mixes[i] = state.randaoMixes[i][:]
|
|
}
|
|
randaoRootsRoot, err := stateutil.ArraysRoot(mixes, fieldparams.RandaoMixesLength)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute randao roots merkleization")
|
|
}
|
|
fieldRoots[nativetypes.RandaoMixes.RealPosition()] = randaoRootsRoot[:]
|
|
|
|
// Slashings array root.
|
|
slashingsRootsRoot, err := ssz.SlashingsRoot(state.slashings)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute slashings merkleization")
|
|
}
|
|
fieldRoots[nativetypes.Slashings.RealPosition()] = slashingsRootsRoot[:]
|
|
|
|
if state.version == version.Phase0 {
|
|
// PreviousEpochAttestations slice root.
|
|
prevAttsRoot, err := stateutil.EpochAttestationsRoot(state.previousEpochAttestations)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute previous epoch attestations merkleization")
|
|
}
|
|
fieldRoots[nativetypes.PreviousEpochAttestations.RealPosition()] = prevAttsRoot[:]
|
|
|
|
// CurrentEpochAttestations slice root.
|
|
currAttsRoot, err := stateutil.EpochAttestationsRoot(state.currentEpochAttestations)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute current epoch attestations merkleization")
|
|
}
|
|
fieldRoots[nativetypes.CurrentEpochAttestations.RealPosition()] = currAttsRoot[:]
|
|
}
|
|
|
|
if state.version == version.Altair || state.version == version.Bellatrix {
|
|
// PreviousEpochParticipation slice root.
|
|
prevParticipationRoot, err := stateutil.ParticipationBitsRoot(state.previousEpochParticipation)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute previous epoch participation merkleization")
|
|
}
|
|
fieldRoots[nativetypes.PreviousEpochParticipationBits.RealPosition()] = prevParticipationRoot[:]
|
|
|
|
// CurrentEpochParticipation slice root.
|
|
currParticipationRoot, err := stateutil.ParticipationBitsRoot(state.currentEpochParticipation)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute current epoch participation merkleization")
|
|
}
|
|
fieldRoots[nativetypes.CurrentEpochParticipationBits.RealPosition()] = currParticipationRoot[:]
|
|
}
|
|
|
|
// JustificationBits root.
|
|
justifiedBitsRoot := bytesutil.ToBytes32(state.justificationBits)
|
|
fieldRoots[nativetypes.JustificationBits.RealPosition()] = justifiedBitsRoot[:]
|
|
|
|
// PreviousJustifiedCheckpoint data structure root.
|
|
prevCheckRoot, err := ssz.CheckpointRoot(hasher, state.previousJustifiedCheckpoint)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute previous justified checkpoint merkleization")
|
|
}
|
|
fieldRoots[nativetypes.PreviousJustifiedCheckpoint.RealPosition()] = prevCheckRoot[:]
|
|
|
|
// CurrentJustifiedCheckpoint data structure root.
|
|
currJustRoot, err := ssz.CheckpointRoot(hasher, state.currentJustifiedCheckpoint)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute current justified checkpoint merkleization")
|
|
}
|
|
fieldRoots[nativetypes.CurrentJustifiedCheckpoint.RealPosition()] = currJustRoot[:]
|
|
|
|
// FinalizedCheckpoint data structure root.
|
|
finalRoot, err := ssz.CheckpointRoot(hasher, state.finalizedCheckpoint)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute finalized checkpoint merkleization")
|
|
}
|
|
fieldRoots[nativetypes.FinalizedCheckpoint.RealPosition()] = finalRoot[:]
|
|
|
|
if state.version == version.Altair || state.version == version.Bellatrix {
|
|
// Inactivity scores root.
|
|
inactivityScoresRoot, err := stateutil.Uint64ListRootWithRegistryLimit(state.inactivityScores)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute inactivityScoreRoot")
|
|
}
|
|
fieldRoots[nativetypes.InactivityScores.RealPosition()] = inactivityScoresRoot[:]
|
|
|
|
// Current sync committee root.
|
|
currentSyncCommitteeRoot, err := stateutil.SyncCommitteeRoot(state.currentSyncCommittee)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute sync committee merkleization")
|
|
}
|
|
fieldRoots[nativetypes.CurrentSyncCommittee.RealPosition()] = currentSyncCommitteeRoot[:]
|
|
|
|
// Next sync committee root.
|
|
nextSyncCommitteeRoot, err := stateutil.SyncCommitteeRoot(state.nextSyncCommittee)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not compute sync committee merkleization")
|
|
}
|
|
fieldRoots[nativetypes.NextSyncCommittee.RealPosition()] = nextSyncCommitteeRoot[:]
|
|
}
|
|
|
|
if state.version == version.Bellatrix {
|
|
// Execution payload root.
|
|
executionPayloadRoot, err := state.latestExecutionPayloadHeader.HashTreeRoot()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
fieldRoots[nativetypes.LatestExecutionPayloadHeader.RealPosition()] = executionPayloadRoot[:]
|
|
}
|
|
|
|
return fieldRoots, nil
|
|
}
|