Use Static Sizes for State Fields (#10007)

This commit is contained in:
Nishant Das
2021-12-13 17:16:23 +08:00
committed by GitHub
parent d6338f6042
commit 5ab88da183
25 changed files with 201 additions and 59 deletions

View File

@@ -649,6 +649,8 @@ func TestServer_StreamBlocks_ContextCanceled(t *testing.T) {
}
func TestServer_StreamBlocks_OnHeadUpdated(t *testing.T) {
params.SetupTestConfigCleanup(t)
params.UseMainnetConfig()
ctx := context.Background()
beaconState, privs := util.DeterministicGenesisState(t, 32)
b, err := util.GenerateFullBlock(beaconState, privs, util.DefaultBlockGenConfig(), 1)
@@ -683,6 +685,8 @@ func TestServer_StreamBlocks_OnHeadUpdated(t *testing.T) {
}
func TestServer_StreamBlocksVerified_OnHeadUpdated(t *testing.T) {
params.SetupTestConfigCleanup(t)
params.UseMainnetConfig()
db := dbTest.SetupDB(t)
ctx := context.Background()
beaconState, privs := util.DeterministicGenesisState(t, 32)

View File

@@ -34,6 +34,7 @@ go_library(
deps = [
"//beacon-chain/core/transition/stateutils:go_default_library",
"//config/features:go_default_library",
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
"//container/trie:go_default_library",
"//crypto/hash:go_default_library",

View File

@@ -5,7 +5,7 @@ import (
"encoding/binary"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/config/params"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/encoding/ssz"
@@ -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")

View File

@@ -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/fieldparams"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/ssz"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
@@ -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)
}
@@ -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")

View File

@@ -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/fieldparams"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/ssz"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
@@ -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")
}

View File

@@ -4,7 +4,7 @@ import (
"encoding/binary"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/config/params"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/ssz"
)
@@ -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

View File

@@ -7,6 +7,7 @@ import (
"github.com/dgraph-io/ristretto"
"github.com/pkg/errors"
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"
@@ -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")
}
@@ -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")
}
@@ -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")
}
@@ -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")
}
@@ -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")
}
@@ -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")
}

View File

@@ -4,7 +4,7 @@ import (
"encoding/binary"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/config/params"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/encoding/ssz"
@@ -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 {

View File

@@ -49,6 +49,7 @@ go_library(
"//beacon-chain/state/stateutil:go_default_library",
"//beacon-chain/state/types:go_default_library",
"//config/features:go_default_library",
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
"//container/slice:go_default_library",
"//crypto/hash:go_default_library",

View File

@@ -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/fieldparams"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
)
@@ -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)
}
@@ -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)
}

View File

@@ -13,6 +13,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
"github.com/prysmaticlabs/prysm/beacon-chain/state/types"
"github.com/prysmaticlabs/prysm/config/features"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/config/params"
"github.com/prysmaticlabs/prysm/container/slice"
"github.com/prysmaticlabs/prysm/crypto/hash"
@@ -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
}
@@ -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
}
@@ -292,7 +293,7 @@ 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:
@@ -300,7 +301,7 @@ func (b *BeaconState) rootSelector(ctx context.Context, field types.FieldIndex)
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
@@ -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
}
@@ -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)
@@ -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
}
@@ -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
@@ -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

View File

@@ -41,6 +41,7 @@ go_library(
"//beacon-chain/state/types:go_default_library",
"//beacon-chain/state/v1:go_default_library",
"//config/features:go_default_library",
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
"//container/slice:go_default_library",
"//crypto/hash:go_default_library",

View File

@@ -15,6 +15,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
"github.com/prysmaticlabs/prysm/beacon-chain/state/types"
"github.com/prysmaticlabs/prysm/config/features"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/config/params"
"github.com/prysmaticlabs/prysm/container/slice"
"github.com/prysmaticlabs/prysm/crypto/hash"
@@ -301,7 +302,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
}
@@ -311,7 +312,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
}
@@ -320,7 +321,7 @@ 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:
@@ -328,7 +329,7 @@ func (b *BeaconState) rootSelector(ctx context.Context, field types.FieldIndex)
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
@@ -339,7 +340,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
}
@@ -350,7 +351,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)
@@ -365,7 +366,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
}

View File

@@ -41,6 +41,7 @@ go_library(
"//beacon-chain/state/types:go_default_library",
"//beacon-chain/state/v1:go_default_library",
"//config/features:go_default_library",
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
"//container/slice:go_default_library",
"//crypto/hash:go_default_library",

View File

@@ -12,6 +12,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/state/fieldtrie"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
"github.com/prysmaticlabs/prysm/beacon-chain/state/types"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/config/params"
"github.com/prysmaticlabs/prysm/container/slice"
"github.com/prysmaticlabs/prysm/crypto/hash"
@@ -273,7 +274,7 @@ func (b *BeaconState) rootSelector(field types.FieldIndex) ([32]byte, error) {
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
}
@@ -284,7 +285,7 @@ func (b *BeaconState) rootSelector(field types.FieldIndex) ([32]byte, error) {
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
}
@@ -294,12 +295,16 @@ func (b *BeaconState) rootSelector(field types.FieldIndex) ([32]byte, error) {
}
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))))
err := b.resetFieldTrie(
field,
b.state.Eth1DataVotes,
fieldparams.Eth1DataVotesLength,
)
if err != nil {
return [32]byte{}, err
}
@@ -310,7 +315,7 @@ func (b *BeaconState) rootSelector(field types.FieldIndex) ([32]byte, error) {
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
}
@@ -323,7 +328,7 @@ func (b *BeaconState) rootSelector(field types.FieldIndex) ([32]byte, error) {
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
}

13
config/BUILD.bazel Normal file
View File

@@ -0,0 +1,13 @@
config_setting(
name = "mainnet",
flag_values = {
"//proto:network": "mainnet",
},
)
config_setting(
name = "minimal",
flag_values = {
"//proto:network": "minimal",
},
)

View File

@@ -0,0 +1,32 @@
load("@prysm//tools/go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = select({
"//config:mainnet": ["mainnet.go"],
"//config:minimal": ["minimal.go"],
}),
importpath = "github.com/prysmaticlabs/prysm/config/fieldparams",
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["mainnet_test.go"],
deps = [
":go_default_library",
"//config/params:go_default_library",
"//testing/assert:go_default_library",
],
)
go_test(
name = "go_minimal_test",
srcs = ["minimal_test.go"],
eth_network = "minimal",
deps = [
":go_default_library",
"//config/params:go_default_library",
"//testing/assert:go_default_library",
],
)

View File

@@ -0,0 +1,16 @@
// +build !minimal
package field_params
const (
BlockRootsLength = 8192 // SLOTS_PER_HISTORICAL_ROOT
StateRootsLength = 8192 // SLOTS_PER_HISTORICAL_ROOT
RandaoMixesLength = 65536 // EPOCHS_PER_HISTORICAL_VECTOR
HistoricalRootsLength = 16777216 // HISTORICAL_ROOTS_LIMIT
ValidatorRegistryLimit = 1099511627776 // VALIDATOR_REGISTRY_LIMIT
Eth1DataVotesLength = 2048 // SLOTS_PER_ETH1_VOTING_PERIOD
PreviousEpochAttestationsLength = 4096 // MAX_ATTESTATIONS * SLOTS_PER_EPOCH
CurrentEpochAttestationsLength = 4096 // MAX_ATTESTATIONS * SLOTS_PER_EPOCH
SlashingsLength = 8192 // EPOCHS_PER_SLASHINGS_VECTOR
SyncCommitteeLength = 512 // SYNC_COMMITTEE_SIZE
)

View File

@@ -0,0 +1,25 @@
// +build !minimal
package field_params_test
import (
"testing"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/config/params"
"github.com/prysmaticlabs/prysm/testing/assert"
)
func TestFieldParametersValues(t *testing.T) {
params.UseMainnetConfig()
assert.Equal(t, uint64(params.BeaconConfig().SlotsPerHistoricalRoot), uint64(fieldparams.BlockRootsLength))
assert.Equal(t, uint64(params.BeaconConfig().SlotsPerHistoricalRoot), uint64(fieldparams.StateRootsLength))
assert.Equal(t, params.BeaconConfig().HistoricalRootsLimit, uint64(fieldparams.HistoricalRootsLength))
assert.Equal(t, uint64(params.BeaconConfig().EpochsPerHistoricalVector), uint64(fieldparams.RandaoMixesLength))
assert.Equal(t, params.BeaconConfig().ValidatorRegistryLimit, uint64(fieldparams.ValidatorRegistryLimit))
assert.Equal(t, uint64(params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().EpochsPerEth1VotingPeriod))), uint64(fieldparams.Eth1DataVotesLength))
assert.Equal(t, uint64(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().MaxAttestations)), uint64(fieldparams.PreviousEpochAttestationsLength))
assert.Equal(t, uint64(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().MaxAttestations)), uint64(fieldparams.CurrentEpochAttestationsLength))
assert.Equal(t, uint64(params.BeaconConfig().EpochsPerSlashingsVector), uint64(fieldparams.SlashingsLength))
assert.Equal(t, params.BeaconConfig().SyncCommitteeSize, uint64(fieldparams.SyncCommitteeLength))
}

View File

@@ -0,0 +1,16 @@
// +build minimal
package field_params
const (
BlockRootsLength = 64 // SLOTS_PER_HISTORICAL_ROOT
StateRootsLength = 64 // SLOTS_PER_HISTORICAL_ROOT
RandaoMixesLength = 64 // EPOCHS_PER_HISTORICAL_VECTOR
HistoricalRootsLength = 16777216 // HISTORICAL_ROOTS_LIMIT
ValidatorRegistryLimit = 1099511627776 // VALIDATOR_REGISTRY_LIMIT
Eth1DataVotesLength = 32 // SLOTS_PER_ETH1_VOTING_PERIOD
PreviousEpochAttestationsLength = 1024 // MAX_ATTESTATIONS * SLOTS_PER_EPOCH
CurrentEpochAttestationsLength = 1024 // MAX_ATTESTATIONS * SLOTS_PER_EPOCH
SlashingsLength = 64 // EPOCHS_PER_SLASHINGS_VECTOR
SyncCommitteeLength = 32 // SYNC_COMMITTEE_SIZE
)

View File

@@ -0,0 +1,25 @@
// +build minimal
package field_params_test
import (
"testing"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/config/params"
"github.com/prysmaticlabs/prysm/testing/assert"
)
func TestFieldParametersValues(t *testing.T) {
params.UseMinimalConfig()
assert.Equal(t, uint64(params.BeaconConfig().SlotsPerHistoricalRoot), uint64(fieldparams.BlockRootsLength))
assert.Equal(t, uint64(params.BeaconConfig().SlotsPerHistoricalRoot), uint64(fieldparams.StateRootsLength))
assert.Equal(t, params.BeaconConfig().HistoricalRootsLimit, uint64(fieldparams.HistoricalRootsLength))
assert.Equal(t, uint64(params.BeaconConfig().EpochsPerHistoricalVector), uint64(fieldparams.RandaoMixesLength))
assert.Equal(t, params.BeaconConfig().ValidatorRegistryLimit, uint64(fieldparams.ValidatorRegistryLimit))
assert.Equal(t, uint64(params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().EpochsPerEth1VotingPeriod))), uint64(fieldparams.Eth1DataVotesLength))
assert.Equal(t, uint64(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().MaxAttestations)), uint64(fieldparams.PreviousEpochAttestationsLength))
assert.Equal(t, uint64(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().MaxAttestations)), uint64(fieldparams.CurrentEpochAttestationsLength))
assert.Equal(t, uint64(params.BeaconConfig().EpochsPerSlashingsVector), uint64(fieldparams.SlashingsLength))
assert.Equal(t, params.BeaconConfig().SyncCommitteeSize, uint64(fieldparams.SyncCommitteeLength))
}

View File

@@ -12,7 +12,7 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/encoding/ssz",
visibility = ["//visibility:public"],
deps = [
"//config/params:go_default_library",
"//config/fieldparams:go_default_library",
"//container/trie:go_default_library",
"//crypto/hash:go_default_library",
"//encoding/bytesutil:go_default_library",

View File

@@ -5,7 +5,7 @@ import (
"encoding/binary"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/config/params"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/crypto/hash"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
@@ -78,7 +78,7 @@ func ByteArrayRootWithLimit(roots [][]byte, limit uint64) ([32]byte, error) {
// a list of uint64 slashing values according to the Ethereum
// Simple Serialize specification.
func SlashingsRoot(slashings []uint64) ([32]byte, error) {
slashingMarshaling := make([][]byte, params.BeaconConfig().EpochsPerSlashingsVector)
slashingMarshaling := make([][]byte, fieldparams.SlashingsLength)
for i := 0; i < len(slashings) && i < len(slashingMarshaling); i++ {
slashBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(slashBuf, slashings[i])

View File

@@ -313,7 +313,7 @@ func generateAttesterSlashings(
attesterSlashings := make([]*ethpb.AttesterSlashing, numSlashings)
randGen := rand.NewDeterministicGenerator()
for i := uint64(0); i < numSlashings; i++ {
committeeIndex := randGen.Uint64() % params.BeaconConfig().MaxCommitteesPerSlot
committeeIndex := randGen.Uint64() % helpers.SlotCommitteeCount(uint64(bState.NumValidators()))
committee, err := helpers.BeaconCommitteeFromState(context.Background(), bState, bState.Slot(), types.CommitteeIndex(committeeIndex))
if err != nil {
return nil, err

View File

@@ -30,7 +30,7 @@ func TestGenerateFullBlock_PassesStateTransition(t *testing.T) {
func TestGenerateFullBlock_ThousandValidators(t *testing.T) {
params.SetupTestConfigCleanup(t)
params.OverrideBeaconConfig(params.MinimalSpecConfig())
params.UseMainnetConfig()
beaconState, privs := DeterministicGenesisState(t, 1024)
conf := &BlockGenConfig{
NumAttestations: 4,
@@ -42,9 +42,8 @@ func TestGenerateFullBlock_ThousandValidators(t *testing.T) {
}
func TestGenerateFullBlock_Passes4Epochs(t *testing.T) {
// Changing to minimal config as this will process 4 epochs of blocks.
params.SetupTestConfigCleanup(t)
params.OverrideBeaconConfig(params.MinimalSpecConfig())
params.UseMainnetConfig()
beaconState, privs := DeterministicGenesisState(t, 64)
conf := &BlockGenConfig{
@@ -73,7 +72,7 @@ func TestGenerateFullBlock_Passes4Epochs(t *testing.T) {
func TestGenerateFullBlock_ValidProposerSlashings(t *testing.T) {
params.SetupTestConfigCleanup(t)
params.OverrideBeaconConfig(params.MinimalSpecConfig())
params.UseMainnetConfig()
beaconState, privs := DeterministicGenesisState(t, 32)
conf := &BlockGenConfig{
NumProposerSlashings: 1,
@@ -92,8 +91,8 @@ func TestGenerateFullBlock_ValidProposerSlashings(t *testing.T) {
func TestGenerateFullBlock_ValidAttesterSlashings(t *testing.T) {
params.SetupTestConfigCleanup(t)
params.OverrideBeaconConfig(params.MinimalSpecConfig())
beaconState, privs := DeterministicGenesisState(t, 32)
params.UseMainnetConfig()
beaconState, privs := DeterministicGenesisState(t, 256)
conf := &BlockGenConfig{
NumAttesterSlashings: 1,
}
@@ -111,7 +110,7 @@ func TestGenerateFullBlock_ValidAttesterSlashings(t *testing.T) {
func TestGenerateFullBlock_ValidAttestations(t *testing.T) {
params.SetupTestConfigCleanup(t)
params.OverrideBeaconConfig(params.MinimalSpecConfig())
params.UseMainnetConfig()
beaconState, privs := DeterministicGenesisState(t, 256)
conf := &BlockGenConfig{