Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Static Sizes for State Fields #10007

Merged
merged 11 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions beacon-chain/state/stateutil/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ go_library(
"//beacon-chain/core/transition/stateutils:go_default_library",
"//config/features:go_default_library",
"//config/params:go_default_library",
"//config/params/fieldparams:go_default_library",
"//container/trie:go_default_library",
"//crypto/hash:go_default_library",
"//encoding/bytesutil:go_default_library",
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/state/stateutil/eth1_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/binary"

"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/config/params"
fieldparams "github.com/prysmaticlabs/prysm/config/params/fieldparams"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/encoding/ssz"
Expand Down Expand Up @@ -98,7 +98,7 @@ func Eth1DatasRoot(eth1Datas []*ethpb.Eth1Data) ([32]byte, error) {
hasher,
eth1Chunks,
uint64(len(eth1Chunks)),
uint64(params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().EpochsPerEth1VotingPeriod))),
fieldparams.Eth1DataVotesLength,
)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute eth1data votes merkleization")
Expand Down
6 changes: 3 additions & 3 deletions beacon-chain/state/stateutil/field_root_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/config/features"
"github.com/prysmaticlabs/prysm/config/params"
fieldparams "github.com/prysmaticlabs/prysm/config/params/fieldparams"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/ssz"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
Expand All @@ -23,7 +23,7 @@ func RootsArrayHashTreeRoot(vals [][]byte, length uint64, fieldName string) ([32
}

func (h *stateRootHasher) epochAttestationsRoot(atts []*ethpb.PendingAttestation) ([32]byte, error) {
max := uint64(params.BeaconConfig().SlotsPerEpoch) * params.BeaconConfig().MaxAttestations
max := uint64(fieldparams.CurrentEpochAttestationsLength)
if uint64(len(atts)) > max {
return [32]byte{}, fmt.Errorf("epoch attestation exceeds max length %d", max)
}
Expand All @@ -42,7 +42,7 @@ func (h *stateRootHasher) epochAttestationsRoot(atts []*ethpb.PendingAttestation
hasher,
roots,
uint64(len(roots)),
uint64(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().MaxAttestations)),
fieldparams.CurrentEpochAttestationsLength,
)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute epoch attestations merkleization")
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/state/stateutil/field_root_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/config/features"
"github.com/prysmaticlabs/prysm/config/params"
fieldparams "github.com/prysmaticlabs/prysm/config/params/fieldparams"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/ssz"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
Expand Down Expand Up @@ -45,7 +45,7 @@ func (h *stateRootHasher) validatorRegistryRoot(validators []*ethpb.Validator) (
}
}

validatorsRootsRoot, err := ssz.BitwiseMerkleizeArrays(hasher, roots, uint64(len(roots)), params.BeaconConfig().ValidatorRegistryLimit)
validatorsRootsRoot, err := ssz.BitwiseMerkleizeArrays(hasher, roots, uint64(len(roots)), fieldparams.ValidatorRegistryLimit)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute validator registry merkleization")
}
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/state/stateutil/participation_bit_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/binary"

"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/config/params"
fieldparams "github.com/prysmaticlabs/prysm/config/params/fieldparams"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/ssz"
)
Expand All @@ -18,7 +18,7 @@ func ParticipationBitsRoot(bits []byte) ([32]byte, error) {
return [32]byte{}, err
}

limit := (params.BeaconConfig().ValidatorRegistryLimit + 31) / 32
limit := (uint64(fieldparams.ValidatorRegistryLimit + 31)) / 32
if limit == 0 {
if len(bits) == 0 {
limit = 1
Expand Down
25 changes: 13 additions & 12 deletions beacon-chain/state/stateutil/state_hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/dgraph-io/ristretto"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/config/params"
fieldparams "github.com/prysmaticlabs/prysm/config/params/fieldparams"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/encoding/ssz"
Expand Down Expand Up @@ -90,21 +91,21 @@ func (h *stateRootHasher) ComputeFieldRootsWithHasherPhase0(ctx context.Context,
fieldRoots[4] = headerHashTreeRoot[:]

// BlockRoots array root.
blockRootsRoot, err := h.arraysRoot(state.BlockRoots, uint64(params.BeaconConfig().SlotsPerHistoricalRoot), "BlockRoots")
blockRootsRoot, err := h.arraysRoot(state.BlockRoots, fieldparams.BlockRootsLength, "BlockRoots")
if err != nil {
return nil, errors.Wrap(err, "could not compute block roots merkleization")
}
fieldRoots[5] = blockRootsRoot[:]

// StateRoots array root.
stateRootsRoot, err := h.arraysRoot(state.StateRoots, uint64(params.BeaconConfig().SlotsPerHistoricalRoot), "StateRoots")
stateRootsRoot, err := h.arraysRoot(state.StateRoots, fieldparams.StateRootsLength, "StateRoots")
if err != nil {
return nil, errors.Wrap(err, "could not compute state roots merkleization")
}
fieldRoots[6] = stateRootsRoot[:]

// HistoricalRoots slice root.
historicalRootsRt, err := ssz.ByteArrayRootWithLimit(state.HistoricalRoots, params.BeaconConfig().HistoricalRootsLimit)
historicalRootsRt, err := ssz.ByteArrayRootWithLimit(state.HistoricalRoots, fieldparams.HistoricalRootsLength)
if err != nil {
return nil, errors.Wrap(err, "could not compute historical roots merkleization")
}
Expand Down Expand Up @@ -145,7 +146,7 @@ func (h *stateRootHasher) ComputeFieldRootsWithHasherPhase0(ctx context.Context,
fieldRoots[12] = balancesRoot[:]

// RandaoMixes array root.
randaoRootsRoot, err := h.arraysRoot(state.RandaoMixes, uint64(params.BeaconConfig().EpochsPerHistoricalVector), "RandaoMixes")
randaoRootsRoot, err := h.arraysRoot(state.RandaoMixes, fieldparams.RandaoMixesLength, "RandaoMixes")
if err != nil {
return nil, errors.Wrap(err, "could not compute randao roots merkleization")
}
Expand Down Expand Up @@ -238,21 +239,21 @@ func (h *stateRootHasher) ComputeFieldRootsWithHasherAltair(ctx context.Context,
fieldRoots[4] = headerHashTreeRoot[:]

// BlockRoots array root.
blockRootsRoot, err := h.arraysRoot(state.BlockRoots, uint64(params.BeaconConfig().SlotsPerHistoricalRoot), "BlockRoots")
blockRootsRoot, err := h.arraysRoot(state.BlockRoots, fieldparams.BlockRootsLength, "BlockRoots")
if err != nil {
return nil, errors.Wrap(err, "could not compute block roots merkleization")
}
fieldRoots[5] = blockRootsRoot[:]

// StateRoots array root.
stateRootsRoot, err := h.arraysRoot(state.StateRoots, uint64(params.BeaconConfig().SlotsPerHistoricalRoot), "StateRoots")
stateRootsRoot, err := h.arraysRoot(state.StateRoots, fieldparams.StateRootsLength, "StateRoots")
if err != nil {
return nil, errors.Wrap(err, "could not compute state roots merkleization")
}
fieldRoots[6] = stateRootsRoot[:]

// HistoricalRoots slice root.
historicalRootsRt, err := ssz.ByteArrayRootWithLimit(state.HistoricalRoots, params.BeaconConfig().HistoricalRootsLimit)
historicalRootsRt, err := ssz.ByteArrayRootWithLimit(state.HistoricalRoots, fieldparams.HistoricalRootsLength)
if err != nil {
return nil, errors.Wrap(err, "could not compute historical roots merkleization")
}
Expand Down Expand Up @@ -293,7 +294,7 @@ func (h *stateRootHasher) ComputeFieldRootsWithHasherAltair(ctx context.Context,
fieldRoots[12] = balancesRoot[:]

// RandaoMixes array root.
randaoRootsRoot, err := h.arraysRoot(state.RandaoMixes, uint64(params.BeaconConfig().EpochsPerHistoricalVector), "RandaoMixes")
randaoRootsRoot, err := h.arraysRoot(state.RandaoMixes, fieldparams.RandaoMixesLength, "RandaoMixes")
if err != nil {
return nil, errors.Wrap(err, "could not compute randao roots merkleization")
}
Expand Down Expand Up @@ -408,21 +409,21 @@ func (h *stateRootHasher) ComputeFieldRootsWithHasherMerge(ctx context.Context,
fieldRoots[4] = headerHashTreeRoot[:]

// BlockRoots array root.
blockRootsRoot, err := h.arraysRoot(state.BlockRoots, uint64(params.BeaconConfig().SlotsPerHistoricalRoot), "BlockRoots")
blockRootsRoot, err := h.arraysRoot(state.BlockRoots, fieldparams.BlockRootsLength, "BlockRoots")
if err != nil {
return nil, errors.Wrap(err, "could not compute block roots merkleization")
}
fieldRoots[5] = blockRootsRoot[:]

// StateRoots array root.
stateRootsRoot, err := h.arraysRoot(state.StateRoots, uint64(params.BeaconConfig().SlotsPerHistoricalRoot), "StateRoots")
stateRootsRoot, err := h.arraysRoot(state.StateRoots, fieldparams.StateRootsLength, "StateRoots")
if err != nil {
return nil, errors.Wrap(err, "could not compute state roots merkleization")
}
fieldRoots[6] = stateRootsRoot[:]

// HistoricalRoots slice root.
historicalRootsRt, err := ssz.ByteArrayRootWithLimit(state.HistoricalRoots, params.BeaconConfig().HistoricalRootsLimit)
historicalRootsRt, err := ssz.ByteArrayRootWithLimit(state.HistoricalRoots, fieldparams.HistoricalRootsLength)
if err != nil {
return nil, errors.Wrap(err, "could not compute historical roots merkleization")
}
Expand Down Expand Up @@ -463,7 +464,7 @@ func (h *stateRootHasher) ComputeFieldRootsWithHasherMerge(ctx context.Context,
fieldRoots[12] = balancesRoot[:]

// RandaoMixes array root.
randaoRootsRoot, err := h.arraysRoot(state.RandaoMixes, uint64(params.BeaconConfig().EpochsPerHistoricalVector), "RandaoMixes")
randaoRootsRoot, err := h.arraysRoot(state.RandaoMixes, fieldparams.RandaoMixesLength, "RandaoMixes")
if err != nil {
return nil, errors.Wrap(err, "could not compute randao roots merkleization")
}
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/state/stateutil/validator_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/binary"

"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/config/params"
fieldparams "github.com/prysmaticlabs/prysm/config/params/fieldparams"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/encoding/ssz"
Expand Down Expand Up @@ -68,7 +68,7 @@ func Uint64ListRootWithRegistryLimit(balances []uint64) ([32]byte, error) {
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not pack balances into chunks")
}
maxBalCap := params.BeaconConfig().ValidatorRegistryLimit
maxBalCap := uint64(fieldparams.ValidatorRegistryLimit)
elemSize := uint64(8)
balLimit := (maxBalCap*elemSize + 31) / 32
if balLimit == 0 {
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/state/v1/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ go_library(
"//beacon-chain/state/types:go_default_library",
"//config/features:go_default_library",
"//config/params:go_default_library",
"//config/params/fieldparams:go_default_library",
"//container/slice:go_default_library",
"//crypto/hash:go_default_library",
"//encoding/bytesutil:go_default_library",
Expand Down
6 changes: 3 additions & 3 deletions beacon-chain/state/v1/setters_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
"github.com/prysmaticlabs/prysm/config/params"
fieldparams "github.com/prysmaticlabs/prysm/config/params/fieldparams"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
)

Expand Down Expand Up @@ -50,7 +50,7 @@ func (b *BeaconState) AppendCurrentEpochAttestations(val *ethpb.PendingAttestati
defer b.lock.Unlock()

atts := b.state.CurrentEpochAttestations
max := uint64(params.BeaconConfig().SlotsPerEpoch) * params.BeaconConfig().MaxAttestations
max := uint64(fieldparams.CurrentEpochAttestationsLength)
if uint64(len(atts)) >= max {
return fmt.Errorf("current pending attestation exceeds max length %d", max)
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func (b *BeaconState) AppendPreviousEpochAttestations(val *ethpb.PendingAttestat
defer b.lock.Unlock()

atts := b.state.PreviousEpochAttestations
max := uint64(params.BeaconConfig().SlotsPerEpoch) * params.BeaconConfig().MaxAttestations
max := uint64(fieldparams.PreviousEpochAttestationsLength)
if uint64(len(atts)) >= max {
return fmt.Errorf("previous pending attestation exceeds max length %d", max)
}
Expand Down
19 changes: 10 additions & 9 deletions beacon-chain/state/v1/state_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/state/types"
"github.com/prysmaticlabs/prysm/config/features"
"github.com/prysmaticlabs/prysm/config/params"
fieldparams "github.com/prysmaticlabs/prysm/config/params/fieldparams"
"github.com/prysmaticlabs/prysm/container/slice"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
Expand Down Expand Up @@ -273,7 +274,7 @@ func (b *BeaconState) rootSelector(ctx context.Context, field types.FieldIndex)
return stateutil.BlockHeaderRoot(b.state.LatestBlockHeader)
case blockRoots:
if b.rebuildTrie[field] {
err := b.resetFieldTrie(field, b.state.BlockRoots, uint64(params.BeaconConfig().SlotsPerHistoricalRoot))
err := b.resetFieldTrie(field, b.state.BlockRoots, fieldparams.BlockRootsLength)
if err != nil {
return [32]byte{}, err
}
Expand All @@ -283,7 +284,7 @@ func (b *BeaconState) rootSelector(ctx context.Context, field types.FieldIndex)
return b.recomputeFieldTrie(blockRoots, b.state.BlockRoots)
case stateRoots:
if b.rebuildTrie[field] {
err := b.resetFieldTrie(field, b.state.StateRoots, uint64(params.BeaconConfig().SlotsPerHistoricalRoot))
err := b.resetFieldTrie(field, b.state.StateRoots, fieldparams.StateRootsLength)
if err != nil {
return [32]byte{}, err
}
Expand All @@ -292,15 +293,15 @@ func (b *BeaconState) rootSelector(ctx context.Context, field types.FieldIndex)
}
return b.recomputeFieldTrie(stateRoots, b.state.StateRoots)
case historicalRoots:
return ssz.ByteArrayRootWithLimit(b.state.HistoricalRoots, params.BeaconConfig().HistoricalRootsLimit)
return ssz.ByteArrayRootWithLimit(b.state.HistoricalRoots, fieldparams.HistoricalRootsLength)
case eth1Data:
return stateutil.Eth1Root(hasher, b.state.Eth1Data)
case eth1DataVotes:
if b.rebuildTrie[field] {
err := b.resetFieldTrie(
field,
b.state.Eth1DataVotes,
uint64(params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().EpochsPerEth1VotingPeriod))),
fieldparams.Eth1DataVotesLength,
)
if err != nil {
return [32]byte{}, err
Expand All @@ -311,7 +312,7 @@ func (b *BeaconState) rootSelector(ctx context.Context, field types.FieldIndex)
return b.recomputeFieldTrie(field, b.state.Eth1DataVotes)
case validators:
if b.rebuildTrie[field] {
err := b.resetFieldTrie(field, b.state.Validators, params.BeaconConfig().ValidatorRegistryLimit)
err := b.resetFieldTrie(field, b.state.Validators, fieldparams.ValidatorRegistryLimit)
if err != nil {
return [32]byte{}, err
}
Expand All @@ -322,7 +323,7 @@ func (b *BeaconState) rootSelector(ctx context.Context, field types.FieldIndex)
case balances:
if features.Get().EnableBalanceTrieComputation {
if b.rebuildTrie[field] {
maxBalCap := params.BeaconConfig().ValidatorRegistryLimit
maxBalCap := uint64(fieldparams.ValidatorRegistryLimit)
elemSize := uint64(8)
balLimit := (maxBalCap*elemSize + 31) / 32
err := b.resetFieldTrie(field, b.state.Balances, balLimit)
Expand All @@ -337,7 +338,7 @@ func (b *BeaconState) rootSelector(ctx context.Context, field types.FieldIndex)
return stateutil.Uint64ListRootWithRegistryLimit(b.state.Balances)
case randaoMixes:
if b.rebuildTrie[field] {
err := b.resetFieldTrie(field, b.state.RandaoMixes, uint64(params.BeaconConfig().EpochsPerHistoricalVector))
err := b.resetFieldTrie(field, b.state.RandaoMixes, fieldparams.RandaoMixesLength)
if err != nil {
return [32]byte{}, err
}
Expand All @@ -352,7 +353,7 @@ func (b *BeaconState) rootSelector(ctx context.Context, field types.FieldIndex)
err := b.resetFieldTrie(
field,
b.state.PreviousEpochAttestations,
uint64(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().MaxAttestations)),
fieldparams.PreviousEpochAttestationsLength,
)
if err != nil {
return [32]byte{}, err
Expand All @@ -366,7 +367,7 @@ func (b *BeaconState) rootSelector(ctx context.Context, field types.FieldIndex)
err := b.resetFieldTrie(
field,
b.state.CurrentEpochAttestations,
uint64(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().MaxAttestations)),
fieldparams.CurrentEpochAttestationsLength,
)
if err != nil {
return [32]byte{}, err
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/state/v2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ go_library(
"//beacon-chain/state/v1:go_default_library",
"//config/features:go_default_library",
"//config/params:go_default_library",
"//config/params/fieldparams:go_default_library",
"//container/slice:go_default_library",
"//crypto/hash:go_default_library",
"//encoding/bytesutil:go_default_library",
Expand Down
Loading