Skip to content

Commit

Permalink
Add state field count to config (#8068)
Browse files Browse the repository at this point in the history
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
  • Loading branch information
terencechain and rauljordan committed Dec 8, 2020
1 parent 9d70527 commit ade3b2f
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 12 deletions.
7 changes: 3 additions & 4 deletions beacon-chain/state/state_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ import (
"go.opencensus.io/trace"
)

// There are 21 fields in the beacon state.
const fieldCount = 21

// InitializeFromProto the beacon state from a protobuf representation.
func InitializeFromProto(st *pbp2p.BeaconState) (*BeaconState, error) {
return InitializeFromProtoUnsafe(proto.Clone(st).(*pbp2p.BeaconState))
Expand All @@ -33,6 +30,7 @@ func InitializeFromProtoUnsafe(st *pbp2p.BeaconState) (*BeaconState, error) {
return nil, errors.New("received nil state")
}

fieldCount := params.BeaconConfig().BeaconStateFieldCount
b := &BeaconState{
state: st,
dirtyFields: make(map[fieldIndex]interface{}, fieldCount),
Expand Down Expand Up @@ -77,6 +75,7 @@ func (b *BeaconState) Copy() *BeaconState {

b.lock.RLock()
defer b.lock.RUnlock()
fieldCount := params.BeaconConfig().BeaconStateFieldCount
dst := &BeaconState{
state: &pbp2p.BeaconState{
// Primitive types, safe to copy.
Expand Down Expand Up @@ -189,7 +188,7 @@ func (b *BeaconState) HashTreeRoot(ctx context.Context) ([32]byte, error) {
}
layers := merkleize(fieldRoots)
b.merkleLayers = layers
b.dirtyFields = make(map[fieldIndex]interface{}, fieldCount)
b.dirtyFields = make(map[fieldIndex]interface{}, params.BeaconConfig().BeaconStateFieldCount)
}

for field := range b.dirtyFields {
Expand Down
5 changes: 3 additions & 2 deletions beacon-chain/state/stateutil/arrays.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/htrutils"
"github.com/prysmaticlabs/prysm/shared/params"
)

var (
leavesCache = make(map[string][][32]byte, fieldCount)
layersCache = make(map[string][][][32]byte, fieldCount)
leavesCache = make(map[string][][32]byte, params.BeaconConfig().BeaconStateFieldCount)
layersCache = make(map[string][][][32]byte, params.BeaconConfig().BeaconStateFieldCount)
lock sync.RWMutex
)

Expand Down
5 changes: 1 addition & 4 deletions beacon-chain/state/stateutil/state_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ const cacheSize = 100000
var nocachedHasher *stateRootHasher
var cachedHasher *stateRootHasher

// There are 21 fields in the beacon state.
const fieldCount = 21

func init() {
rootsCache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: cacheSize, // number of keys to track frequency of (1M).
Expand Down Expand Up @@ -56,7 +53,7 @@ func (h *stateRootHasher) computeFieldRoots(state *pb.BeaconState) ([][]byte, er
return nil, errors.New("nil state")
}
hasher := hashutil.CustomSHA256Hasher()
fieldRoots := make([][]byte, fieldCount)
fieldRoots := make([][]byte, params.BeaconConfig().BeaconStateFieldCount)

// Genesis time root.
genesisRoot := htrutils.Uint64Root(state.GenesisTime)
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/state/stateutil/state_root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestState_FieldCount(t *testing.T) {
count := 21
count := params.BeaconConfig().BeaconStateFieldCount
typ := reflect.TypeOf(pb.BeaconState{})
numFields := 0
for i := 0; i < typ.NumField(); i++ {
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/state/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
coreutils "github.com/prysmaticlabs/prysm/beacon-chain/core/state/stateutils"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
)

func init() {
fieldMap = make(map[fieldIndex]dataType, fieldCount)
fieldMap = make(map[fieldIndex]dataType, params.BeaconConfig().BeaconStateFieldCount)

// Initialize the fixed sized arrays.
fieldMap[blockRoots] = basicArray
Expand Down
1 change: 1 addition & 0 deletions shared/params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type BeaconChainConfig struct {
SlotsPerArchivedPoint uint64 // SlotsPerArchivedPoint defines the number of slots per one archived point.
GenesisCountdownInterval time.Duration // How often to log the countdown until the genesis time is reached.
NetworkName string // NetworkName for allowing an easy human-readable way of knowing what chain is being used.
BeaconStateFieldCount int // BeaconStateFieldCount defines how many fields are in beacon state.

// Slasher constants.
WeakSubjectivityPeriod uint64 // WeakSubjectivityPeriod defines the time period expressed in number of epochs were proof of stake network should validate block headers and attestations for slashable events.
Expand Down
1 change: 1 addition & 0 deletions shared/params/mainnet_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ var mainnetBeaconConfig = &BeaconChainConfig{
SlotsPerArchivedPoint: 2048,
GenesisCountdownInterval: time.Minute,
NetworkName: "Mainnet",
BeaconStateFieldCount: 21,

// Slasher related values.
WeakSubjectivityPeriod: 54000,
Expand Down

0 comments on commit ade3b2f

Please sign in to comment.