From a99de08562d2a8a7053322488c07e2d5246d56ec Mon Sep 17 00:00:00 2001 From: Raul Jordan Date: Sat, 1 Dec 2018 16:09:12 -0600 Subject: [PATCH] Merge Active/Cystallized State (#1019) --- beacon-chain/attestation/BUILD.bazel | 4 +- beacon-chain/attestation/service.go | 2 +- beacon-chain/attestation/service_test.go | 2 +- beacon-chain/blockchain/BUILD.bazel | 9 +- beacon-chain/blockchain/service.go | 209 ++++---- beacon-chain/blockchain/service_test.go | 121 ++--- .../core/incentives/incentives_test.go | 2 +- beacon-chain/core/state/BUILD.bazel | 7 + beacon-chain/core/state/state_transition.go | 151 +++--- .../core/state/state_transition_test.go | 72 ++- .../core/state/validity_conditions.go | 183 +++++++ .../core/state/validity_conditions_test.go | 161 +++++++ beacon-chain/core/types/BUILD.bazel | 22 +- beacon-chain/{ => core}/types/attestation.go | 51 +- .../{ => core}/types/attestation_test.go | 0 beacon-chain/core/types/block.go | 108 ++++- beacon-chain/core/types/block_test.go | 84 ++++ beacon-chain/core/types/state.go | 88 ++++ .../types/state_test.go} | 188 +++----- .../core/validators/committees_test.go | 2 +- beacon-chain/db/BUILD.bazel | 4 +- beacon-chain/db/attestation.go | 2 +- beacon-chain/db/attestation_test.go | 2 +- beacon-chain/db/block.go | 40 +- beacon-chain/db/block_test.go | 28 +- beacon-chain/db/schema.go | 3 +- beacon-chain/db/state.go | 156 ++---- beacon-chain/db/state_test.go | 80 +--- beacon-chain/dbcleanup/BUILD.bazel | 4 +- beacon-chain/dbcleanup/service.go | 10 +- beacon-chain/dbcleanup/service_test.go | 16 +- beacon-chain/node/node.go | 4 +- beacon-chain/node/p2p_config.go | 9 +- beacon-chain/rpc/BUILD.bazel | 4 +- beacon-chain/rpc/service.go | 66 ++- beacon-chain/rpc/service_test.go | 70 ++- beacon-chain/simulator/BUILD.bazel | 4 +- beacon-chain/simulator/service.go | 98 ++-- beacon-chain/simulator/service_test.go | 32 +- beacon-chain/sync/BUILD.bazel | 5 +- beacon-chain/sync/initial-sync/BUILD.bazel | 6 +- beacon-chain/sync/initial-sync/service.go | 157 +++--- .../sync/initial-sync/service_test.go | 131 ++--- beacon-chain/sync/service.go | 28 +- beacon-chain/sync/service_test.go | 2 +- beacon-chain/sync/sync-querier/BUILD.bazel | 2 +- beacon-chain/sync/sync-querier/service.go | 13 +- .../sync/sync-querier/service_test.go | 2 +- beacon-chain/types/BUILD.bazel | 49 -- beacon-chain/types/active_state.go | 241 ---------- beacon-chain/types/block.go | 344 ------------- beacon-chain/types/block_test.go | 253 ---------- beacon-chain/types/crystallized_state.go | 451 ------------------ beacon-chain/types/crystallized_state_test.go | 429 ----------------- beacon-chain/utils/genesis_json.go | 6 +- beacon-chain/utils/genesis_json_test.go | 4 +- proto/beacon/p2p/v1/messages.pb.go | 363 +++++--------- proto/beacon/p2p/v1/messages.proto | 29 +- proto/beacon/p2p/v1/types.pb.go | 444 +++++------------ proto/beacon/p2p/v1/types.proto | 32 +- shared/params/config.go | 2 +- 61 files changed, 1656 insertions(+), 3435 deletions(-) create mode 100644 beacon-chain/core/state/validity_conditions.go create mode 100644 beacon-chain/core/state/validity_conditions_test.go rename beacon-chain/{ => core}/types/attestation.go (90%) rename beacon-chain/{ => core}/types/attestation_test.go (100%) create mode 100644 beacon-chain/core/types/block_test.go rename beacon-chain/{types/active_state_test.go => core/types/state_test.go} (56%) delete mode 100644 beacon-chain/types/BUILD.bazel delete mode 100644 beacon-chain/types/active_state.go delete mode 100644 beacon-chain/types/block.go delete mode 100644 beacon-chain/types/block_test.go delete mode 100644 beacon-chain/types/crystallized_state.go delete mode 100644 beacon-chain/types/crystallized_state_test.go diff --git a/beacon-chain/attestation/BUILD.bazel b/beacon-chain/attestation/BUILD.bazel index 85a21c7c53..f023da3f28 100644 --- a/beacon-chain/attestation/BUILD.bazel +++ b/beacon-chain/attestation/BUILD.bazel @@ -6,8 +6,8 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/attestation", visibility = ["//beacon-chain:__subpackages__"], deps = [ + "//beacon-chain/core/types:go_default_library", "//beacon-chain/db:go_default_library", - "//beacon-chain/types:go_default_library", "//shared/event:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", ], @@ -18,8 +18,8 @@ go_test( srcs = ["service_test.go"], embed = [":go_default_library"], deps = [ + "//beacon-chain/core/types:go_default_library", "//beacon-chain/internal:go_default_library", - "//beacon-chain/types:go_default_library", "//shared/testutil:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", "@com_github_sirupsen_logrus//hooks/test:go_default_library", diff --git a/beacon-chain/attestation/service.go b/beacon-chain/attestation/service.go index a10e59ebf2..29dbce85c6 100644 --- a/beacon-chain/attestation/service.go +++ b/beacon-chain/attestation/service.go @@ -4,8 +4,8 @@ package attestation import ( "context" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" "github.com/prysmaticlabs/prysm/beacon-chain/db" - "github.com/prysmaticlabs/prysm/beacon-chain/types" "github.com/prysmaticlabs/prysm/shared/event" "github.com/sirupsen/logrus" ) diff --git a/beacon-chain/attestation/service_test.go b/beacon-chain/attestation/service_test.go index 90d27fe4a4..1a7d242ecb 100644 --- a/beacon-chain/attestation/service_test.go +++ b/beacon-chain/attestation/service_test.go @@ -4,8 +4,8 @@ import ( "context" "testing" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" "github.com/prysmaticlabs/prysm/beacon-chain/internal" - "github.com/prysmaticlabs/prysm/beacon-chain/types" "github.com/prysmaticlabs/prysm/shared/testutil" "github.com/sirupsen/logrus" logTest "github.com/sirupsen/logrus/hooks/test" diff --git a/beacon-chain/blockchain/BUILD.bazel b/beacon-chain/blockchain/BUILD.bazel index 7a66345ae6..98475e5893 100644 --- a/beacon-chain/blockchain/BUILD.bazel +++ b/beacon-chain/blockchain/BUILD.bazel @@ -6,12 +6,15 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/blockchain", visibility = ["//beacon-chain:__subpackages__"], deps = [ + "//beacon-chain/core/state:go_default_library", + "//beacon-chain/core/types:go_default_library", + "//beacon-chain/core/validators:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/powchain:go_default_library", - "//beacon-chain/types:go_default_library", "//beacon-chain/utils:go_default_library", "//shared/bitutil:go_default_library", "//shared/event:go_default_library", + "//shared/params:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", ], ) @@ -21,17 +24,17 @@ go_test( srcs = ["service_test.go"], embed = [":go_default_library"], deps = [ + "//beacon-chain/core/types:go_default_library", "//beacon-chain/core/validators:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/internal:go_default_library", "//beacon-chain/powchain:go_default_library", - "//beacon-chain/types:go_default_library", "//proto/beacon/p2p/v1:go_default_library", - "//shared/event:go_default_library", "//shared/testutil:go_default_library", "@com_github_ethereum_go_ethereum//:go_default_library", "@com_github_ethereum_go_ethereum//common:go_default_library", "@com_github_ethereum_go_ethereum//core/types:go_default_library", + "@com_github_ethereum_go_ethereum//event:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", "@com_github_sirupsen_logrus//hooks/test:go_default_library", ], diff --git a/beacon-chain/blockchain/service.go b/beacon-chain/blockchain/service.go index 52779de740..8596b5abb4 100644 --- a/beacon-chain/blockchain/service.go +++ b/beacon-chain/blockchain/service.go @@ -8,12 +8,15 @@ import ( "fmt" "time" + "github.com/prysmaticlabs/prysm/beacon-chain/core/state" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" + v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/powchain" - "github.com/prysmaticlabs/prysm/beacon-chain/types" "github.com/prysmaticlabs/prysm/beacon-chain/utils" "github.com/prysmaticlabs/prysm/shared/bitutil" "github.com/prysmaticlabs/prysm/shared/event" + "github.com/prysmaticlabs/prysm/shared/params" "github.com/sirupsen/logrus" ) @@ -22,18 +25,18 @@ var log = logrus.WithField("prefix", "blockchain") // ChainService represents a service that handles the internal // logic of managing the full PoS beacon chain. type ChainService struct { - ctx context.Context - cancel context.CancelFunc - beaconDB *db.BeaconDB - web3Service *powchain.Web3Service - incomingBlockFeed *event.Feed - incomingBlockChan chan *types.Block - processedBlockChan chan *types.Block - canonicalBlockFeed *event.Feed - canonicalCrystallizedStateFeed *event.Feed - genesisTime time.Time - unfinalizedBlocks map[[32]byte]*statePair - enablePOWChain bool + ctx context.Context + cancel context.CancelFunc + beaconDB *db.BeaconDB + web3Service *powchain.Web3Service + incomingBlockFeed *event.Feed + incomingBlockChan chan *types.Block + processedBlockChan chan *types.Block + canonicalBlockFeed *event.Feed + canonicalStateFeed *event.Feed + genesisTime time.Time + unfinalizedBlocks map[[32]byte]*types.BeaconState + enablePOWChain bool } // Config options for the service. @@ -46,30 +49,22 @@ type Config struct { EnablePOWChain bool } -// Struct used to represent an unfinalized block's state pair -// (active state, crystallized state) tuple. -type statePair struct { - crystallizedState *types.CrystallizedState - activeState *types.ActiveState - cycleTransition bool -} - // NewChainService instantiates a new service instance that will // be registered into a running beacon node. func NewChainService(ctx context.Context, cfg *Config) (*ChainService, error) { ctx, cancel := context.WithCancel(ctx) return &ChainService{ - ctx: ctx, - cancel: cancel, - beaconDB: cfg.BeaconDB, - web3Service: cfg.Web3Service, - incomingBlockChan: make(chan *types.Block, cfg.IncomingBlockBuf), - processedBlockChan: make(chan *types.Block), - incomingBlockFeed: new(event.Feed), - canonicalBlockFeed: new(event.Feed), - canonicalCrystallizedStateFeed: new(event.Feed), - unfinalizedBlocks: make(map[[32]byte]*statePair), - enablePOWChain: cfg.EnablePOWChain, + ctx: ctx, + cancel: cancel, + beaconDB: cfg.BeaconDB, + web3Service: cfg.Web3Service, + incomingBlockChan: make(chan *types.Block, cfg.IncomingBlockBuf), + processedBlockChan: make(chan *types.Block), + incomingBlockFeed: new(event.Feed), + canonicalBlockFeed: new(event.Feed), + canonicalStateFeed: new(event.Feed), + unfinalizedBlocks: make(map[[32]byte]*types.BeaconState), + enablePOWChain: cfg.EnablePOWChain, }, nil } @@ -110,10 +105,10 @@ func (c *ChainService) CanonicalBlockFeed() *event.Feed { return c.canonicalBlockFeed } -// CanonicalCrystallizedStateFeed returns a feed that is written to -// whenever a new crystallized state is determined to be canonical in the chain. -func (c *ChainService) CanonicalCrystallizedStateFeed() *event.Feed { - return c.canonicalCrystallizedStateFeed +// CanonicalStateFeed returns a feed that is written to +// whenever a new state is determined to be canonical in the chain. +func (c *ChainService) CanonicalStateFeed() *event.Feed { + return c.canonicalStateFeed } // doesPoWBlockExist checks if the referenced PoW block exists. @@ -154,33 +149,33 @@ func (c *ChainService) updateHead(processedBlock <-chan *types.Block) { log.Errorf("Could not get current chain head: %v", err) continue } - currentcState, err := c.beaconDB.GetCrystallizedState() + currentState, err := c.beaconDB.GetState() if err != nil { - log.Errorf("Could not get current crystallized state: %v", err) + log.Errorf("Could not get current beacon state: %v", err) continue } - blockcState := c.unfinalizedBlocks[h].crystallizedState + blockState := c.unfinalizedBlocks[h] var headUpdated bool newHead := currentHead // If both blocks have the same crystallized state root, we favor one which has // the higher slot. - if currentHead.CrystallizedStateRoot() == block.CrystallizedStateRoot() { + if currentHead.StateRoot() == block.StateRoot() { if block.SlotNumber() > currentHead.SlotNumber() { newHead = block headUpdated = true } // 2a. Pick the block with the higher last_finalized_slot. // 2b. If same, pick the block with the higher last_justified_slot. - } else if blockcState.LastFinalizedSlot() > currentcState.LastFinalizedSlot() { + } else if blockState.LastFinalizedSlot() > currentState.LastFinalizedSlot() { newHead = block headUpdated = true - } else if blockcState.LastFinalizedSlot() == currentcState.LastFinalizedSlot() { - if blockcState.LastJustifiedSlot() > currentcState.LastJustifiedSlot() { + } else if blockState.LastFinalizedSlot() == currentState.LastFinalizedSlot() { + if blockState.LastJustifiedSlot() > currentState.LastJustifiedSlot() { newHead = block headUpdated = true - } else if blockcState.LastJustifiedSlot() == currentcState.LastJustifiedSlot() { + } else if blockState.LastJustifiedSlot() == currentState.LastJustifiedSlot() { if block.SlotNumber() > currentHead.SlotNumber() { newHead = block headUpdated = true @@ -195,43 +190,25 @@ func (c *ChainService) updateHead(processedBlock <-chan *types.Block) { } // TODO(#674): Handle chain reorgs. - var newCState *types.CrystallizedState - if c.unfinalizedBlocks[h].cycleTransition { - newCState = blockcState - } - if err := c.beaconDB.UpdateChainHead(newHead, c.unfinalizedBlocks[h].activeState, newCState); err != nil { + newState := blockState + if err := c.beaconDB.UpdateChainHead(newHead, newState); err != nil { log.Errorf("Failed to update chain: %v", err) continue } log.WithField("blockHash", fmt.Sprintf("0x%x", h)).Info("Chain head block and state updated") - // We fire events that notify listeners of a new block (or crystallized state in - // the case of a state transition). This is useful for the beacon node's gRPC + // We fire events that notify listeners of a new block in + // the case of a state transition. This is useful for the beacon node's gRPC // server to stream these events to beacon clients. - if c.unfinalizedBlocks[h].cycleTransition { - c.canonicalCrystallizedStateFeed.Send(blockcState) + // When the transition is a cycle transition, we stream the state containing the new validator + // assignments to clients. + if block.SlotNumber()%params.BeaconConfig().CycleLength == 0 { + c.canonicalStateFeed.Send(newState) } c.canonicalBlockFeed.Send(newHead) } } } -func (c *ChainService) executeStateTransition( - cState *types.CrystallizedState, - aState *types.ActiveState, - block *types.Block) (*types.CrystallizedState, error) { - - var err error - log.Infof("Executing state transition for slot: %d", block.SlotNumber()) - for cState.IsCycleTransition(block.SlotNumber()) { - cState, err = cState.NewStateRecalculations(aState, block, c.beaconDB) - if err != nil { - return nil, err - } - } - - return cState, nil -} - func (c *ChainService) blockProcessing(processedBlock chan<- *types.Block) { subBlock := c.incomingBlockFeed.Subscribe(c.incomingBlockChan) defer subBlock.Unsubscribe() @@ -275,80 +252,88 @@ func (c *ChainService) processBlock(block *types.Block) error { return fmt.Errorf("block points to nil parent: %#x", block.ParentHash()) } - aState, err := c.beaconDB.GetActiveState() + beaconState, err := c.beaconDB.GetState() if err != nil { - return fmt.Errorf("failed to get active state: %v", err) - } - cState, err := c.beaconDB.GetCrystallizedState() - if err != nil { - return fmt.Errorf("failed to get crystallized state: %v", err) + return fmt.Errorf("failed to get beacon state: %v", err) } - if valid := block.IsValid( - c.beaconDB, - aState, - cState, + // Verifies the block against the validity conditions specifies as part of the + // Ethereum 2.0 specification. + if err := state.IsValidBlock( + block, + beaconState, parent.SlotNumber(), c.genesisTime, - ); !valid { - return errors.New("block failed validity conditions") + c.beaconDB.HasBlock, + ); err != nil { + return fmt.Errorf("block failed validity conditions: %v", err) } - if err = c.calculateNewBlockVotes(block, aState, cState); err != nil { - return fmt.Errorf("failed to update block vote cache: %v", err) + if err := c.calculateNewBlockVotes(block, beaconState); err != nil { + return fmt.Errorf("failed to calculate block vote cache: %v", err) } // First, include new attestations to the active state // so that they're accounted for during cycle transitions. - aState = aState.UpdateAttestations(block.Attestations()) + beaconState.SetPendingAttestations(block.Attestations()) // If the block is valid, we compute its associated state tuple (active, crystallized) - var didCycleTransition bool - if cState.IsCycleTransition(block.SlotNumber()) { - cState, err = c.executeStateTransition(cState, aState, block) - if err != nil { - return fmt.Errorf("initialize new cycle transition failed: %v", err) - } - didCycleTransition = true - } - - aState, err = aState.CalculateNewActiveState( - block, - cState, - parent.SlotNumber(), - ) + beaconState, err = c.executeStateTransition(beaconState, block, parent.SlotNumber()) if err != nil { - return fmt.Errorf("compute active state failed: %v", err) + return fmt.Errorf("initialize new cycle transition failed: %v", err) } if err := c.beaconDB.SaveBlock(block); err != nil { return fmt.Errorf("failed to save block: %v", err) } - if err := c.beaconDB.SaveUnfinalizedBlockState(aState, cState); err != nil { + if err := c.beaconDB.SaveUnfinalizedBlockState(beaconState); err != nil { return fmt.Errorf("error persisting unfinalized block's state: %v", err) } - log.Infof("Processed block: %#x", blockHash) + log.WithField("hash", fmt.Sprintf("%#x", blockHash)).Info("Processed beacon block") // We keep a map of unfinalized blocks in memory along with their state // pair to apply the fork choice rule. - c.unfinalizedBlocks[blockHash] = &statePair{ - crystallizedState: cState, - activeState: aState, - cycleTransition: didCycleTransition, - } + c.unfinalizedBlocks[blockHash] = beaconState return nil } -func (c *ChainService) calculateNewBlockVotes(block *types.Block, aState *types.ActiveState, cState *types.CrystallizedState) error { +func (c *ChainService) executeStateTransition( + beaconState *types.BeaconState, + block *types.Block, + parentSlot uint64, +) (*types.BeaconState, error) { + log.WithField("slotNumber", block.SlotNumber()).Info("Executing state transition") + blockVoteCache, err := c.beaconDB.ReadBlockVoteCache(beaconState.RecentBlockHashes()) + if err != nil { + return nil, err + } + newState, err := state.NewStateTransition(beaconState, block, parentSlot, blockVoteCache) + if err != nil { + return nil, err + } + if newState.IsValidatorSetChange(block.SlotNumber()) { + log.WithField("slotNumber", block.SlotNumber()).Info("Validator set rotation occurred") + } + return newState, nil +} + +func (c *ChainService) calculateNewBlockVotes(block *types.Block, beaconState *types.BeaconState) error { for _, attestation := range block.Attestations() { - parentHashes, err := aState.GetSignedParentHashes(block, attestation) + parentHashes, err := beaconState.SignedParentHashes(block, attestation) if err != nil { return err } - - attesterIndices, err := cState.AttesterIndices(attestation) + shardCommittees, err := v.GetShardAndCommitteesForSlot( + beaconState.ShardAndCommitteesForSlots(), + beaconState.LastStateRecalculationSlot(), + attestation.GetSlot(), + ) + if err != nil { + return fmt.Errorf("unable to fetch ShardAndCommittees for slot %d: %v", attestation.Slot, err) + } + attesterIndices, err := v.AttesterIndices(shardCommittees, attestation) if err != nil { return err } @@ -398,7 +383,7 @@ func (c *ChainService) calculateNewBlockVotes(block *types.Block, aState *types. } if !attesterExists { blockVoteCache[h].VoterIndices = append(blockVoteCache[h].VoterIndices, attesterIndex) - blockVoteCache[h].VoteTotalDeposit += cState.Validators()[attesterIndex].Balance + blockVoteCache[h].VoteTotalDeposit += beaconState.Validators()[attesterIndex].Balance } } } diff --git a/beacon-chain/blockchain/service_test.go b/beacon-chain/blockchain/service_test.go index 67531c4a87..db180e5da6 100644 --- a/beacon-chain/blockchain/service_test.go +++ b/beacon-chain/blockchain/service_test.go @@ -7,17 +7,18 @@ import ( "math/big" "testing" - "github.com/ethereum/go-ethereum" + ethereum "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" gethTypes "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/event" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/internal" "github.com/prysmaticlabs/prysm/beacon-chain/powchain" - "github.com/prysmaticlabs/prysm/beacon-chain/types" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" - "github.com/prysmaticlabs/prysm/shared/event" "github.com/prysmaticlabs/prysm/shared/testutil" + "github.com/sirupsen/logrus" logTest "github.com/sirupsen/logrus/hooks/test" ) @@ -94,7 +95,7 @@ func setupBeaconChain(t *testing.T, faultyPoWClient bool, beaconDB *db.BeaconDB) t.Fatalf("unable to set up web3 service: %v", err) } if err := beaconDB.InitializeState(nil); err != nil { - t.Fatalf("Failed to initialize state: %v", err) + t.Fatalf("failed to initialize state: %v", err) } cfg := &Config{ @@ -170,16 +171,14 @@ func TestRunningChainService(t *testing.T) { db := internal.SetupDB(t) defer internal.TeardownDB(t, db) chainService := setupBeaconChain(t, false, db) - active := types.NewGenesisActiveState() - crystallized, err := types.NewGenesisCrystallizedState(nil) + beaconState, err := types.NewGenesisBeaconState(nil) if err != nil { t.Fatalf("Can't generate genesis state: %v", err) } - activeStateRoot, _ := active.Hash() - crystallizedStateRoot, _ := crystallized.Hash() + stateRoot, _ := beaconState.Hash() - genesis := types.NewGenesisBlock([32]byte{}, [32]byte{}) + genesis := types.NewGenesisBlock([32]byte{}) chainService.beaconDB.SaveBlock(genesis) parentHash, err := genesis.Hash() if err != nil { @@ -188,14 +187,13 @@ func TestRunningChainService(t *testing.T) { currentSlot := uint64(1) attestationSlot := uint64(0) - shard := crystallized.ShardAndCommitteesForSlots()[attestationSlot].ArrayShardAndCommittee[0].Shard + shard := beaconState.ShardAndCommitteesForSlots()[attestationSlot].ArrayShardAndCommittee[0].Shard block := types.NewBlock(&pb.BeaconBlock{ - Slot: currentSlot, - ActiveStateRoot: activeStateRoot[:], - CrystallizedStateRoot: crystallizedStateRoot[:], - AncestorHashes: [][]byte{parentHash[:]}, - PowChainRef: []byte("a"), + Slot: currentSlot, + StateRoot: stateRoot[:], + AncestorHashes: [][]byte{parentHash[:]}, + PowChainRef: []byte("a"), Attestations: []*pb.AggregatedAttestation{{ Slot: attestationSlot, AttesterBitfield: []byte{128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -221,7 +219,7 @@ func TestRunningChainService(t *testing.T) { chainService.cancel() exitRoutine <- true testutil.AssertLogsContain(t, hook, "Chain service context closed, exiting goroutine") - testutil.AssertLogsContain(t, hook, "Processed block") + testutil.AssertLogsContain(t, hook, "Processed beacon block") } func TestDoesPOWBlockExist(t *testing.T) { @@ -242,11 +240,12 @@ func TestDoesPOWBlockExist(t *testing.T) { testutil.AssertLogsContain(t, hook, "fetching PoW block corresponding to mainchain reference failed") } -func getShardForSlot(t *testing.T, cState *types.CrystallizedState, slot uint64) uint64 { +func getShardForSlot(t *testing.T, beaconState *types.BeaconState, slot uint64) uint64 { shardAndCommittee, err := v.GetShardAndCommitteesForSlot( - cState.ShardAndCommitteesForSlots(), - cState.LastStateRecalculationSlot(), - slot) + beaconState.ShardAndCommitteesForSlots(), + beaconState.LastStateRecalculationSlot(), + slot, + ) if err != nil { t.Fatalf("Unable to get shard for slot: %d", slot) } @@ -257,14 +256,12 @@ func TestProcessBlocksWithCorrectAttestations(t *testing.T) { db := internal.SetupDB(t) defer internal.TeardownDB(t, db) chainService := setupBeaconChain(t, false, db) - active := types.NewGenesisActiveState() - crystallized, err := types.NewGenesisCrystallizedState(nil) + beaconState, err := types.NewGenesisBeaconState(nil) if err != nil { t.Fatalf("Can't generate genesis state: %v", err) } - activeStateRoot, _ := active.Hash() - crystallizedStateRoot, _ := crystallized.Hash() + stateRoot, _ := beaconState.Hash() block0 := types.NewBlock(&pb.BeaconBlock{ Slot: 0, @@ -281,15 +278,14 @@ func TestProcessBlocksWithCorrectAttestations(t *testing.T) { attestationSlot := currentSlot - 1 block1 := types.NewBlock(&pb.BeaconBlock{ - AncestorHashes: [][]byte{block0Hash[:]}, - Slot: currentSlot, - ActiveStateRoot: activeStateRoot[:], - CrystallizedStateRoot: crystallizedStateRoot[:], + AncestorHashes: [][]byte{block0Hash[:]}, + Slot: currentSlot, + StateRoot: stateRoot[:], Attestations: []*pb.AggregatedAttestation{{ Slot: attestationSlot, AttesterBitfield: []byte{128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - Shard: getShardForSlot(t, crystallized, attestationSlot), + Shard: getShardForSlot(t, beaconState, attestationSlot), JustifiedBlockHash: block0Hash[:], }}, }) @@ -323,13 +319,13 @@ func TestProcessBlocksWithCorrectAttestations(t *testing.T) { { Slot: currentSlot - 1, AttesterBitfield: []byte{64, 0}, - Shard: getShardForSlot(t, crystallized, currentSlot-1), + Shard: getShardForSlot(t, beaconState, currentSlot-1), JustifiedBlockHash: block0Hash[:], }, { Slot: currentSlot - 2, AttesterBitfield: []byte{128, 0}, - Shard: getShardForSlot(t, crystallized, currentSlot-2), + Shard: getShardForSlot(t, beaconState, currentSlot-2), JustifiedBlockHash: block0Hash[:], }, }}) @@ -348,19 +344,19 @@ func TestProcessBlocksWithCorrectAttestations(t *testing.T) { { Slot: currentSlot - 1, AttesterBitfield: []byte{32, 0}, - Shard: getShardForSlot(t, crystallized, currentSlot-1), + Shard: getShardForSlot(t, beaconState, currentSlot-1), JustifiedBlockHash: block0Hash[:], }, { Slot: currentSlot - 2, AttesterBitfield: []byte{64, 0}, - Shard: getShardForSlot(t, crystallized, currentSlot-2), + Shard: getShardForSlot(t, beaconState, currentSlot-2), JustifiedBlockHash: block0Hash[:], }, { Slot: currentSlot - 3, AttesterBitfield: []byte{128, 0}, - Shard: getShardForSlot(t, crystallized, currentSlot-3), + Shard: getShardForSlot(t, beaconState, currentSlot-3), JustifiedBlockHash: block0Hash[:], }, }}) @@ -377,15 +373,13 @@ func TestProcessBlocksWithCorrectAttestations(t *testing.T) { } func TestUpdateHead(t *testing.T) { - genesisActive := types.NewGenesisActiveState() - genesisCrystallized, err := types.NewGenesisCrystallizedState(nil) + beaconState, err := types.NewGenesisBeaconState(nil) if err != nil { t.Fatalf("Could not generate genesis state: %v", err) } - genesisActiveRoot, _ := genesisActive.Hash() - genesisCrystallizedRoot, _ := genesisCrystallized.Hash() + stateRoot, _ := beaconState.Hash() - genesis := types.NewGenesisBlock(genesisActiveRoot, genesisCrystallizedRoot) + genesis := types.NewGenesisBlock(stateRoot) genesisHash, err := genesis.Hash() if err != nil { t.Fatalf("Could not get genesis block hash: %v", err) @@ -393,30 +387,26 @@ func TestUpdateHead(t *testing.T) { // Table driven tests for various fork choice scenarios. tests := []struct { blockSlot uint64 - aState *types.ActiveState - cState *types.CrystallizedState + state *types.BeaconState logAssert string }{ // Higher slot but same crystallized state should trigger chain update. { blockSlot: 64, - aState: genesisActive, - cState: genesisCrystallized, + state: beaconState, logAssert: "Chain head block and state updated", }, // Higher slot, different crystallized state, but higher last finalized slot. { blockSlot: 64, - aState: genesisActive, - cState: types.NewCrystallizedState(&pb.CrystallizedState{LastFinalizedSlot: 10}), + state: types.NewBeaconState(&pb.BeaconState{LastFinalizedSlot: 10}), logAssert: "Chain head block and state updated", }, // Higher slot, different crystallized state, same last finalized slot, // but last justified slot. { blockSlot: 64, - aState: genesisActive, - cState: types.NewCrystallizedState(&pb.CrystallizedState{ + state: types.NewBeaconState(&pb.BeaconState{ LastFinalizedSlot: 0, LastJustifiedSlot: 10, }), @@ -425,8 +415,7 @@ func TestUpdateHead(t *testing.T) { // Same slot should not trigger a head update. { blockSlot: 0, - aState: genesisActive, - cState: genesisCrystallized, + state: beaconState, logAssert: "Chain head not updated", }, } @@ -436,14 +425,12 @@ func TestUpdateHead(t *testing.T) { defer internal.TeardownDB(t, db) chainService := setupBeaconChain(t, false, db) - aRoot, _ := tt.aState.Hash() - cRoot, _ := tt.cState.Hash() + stateRoot, _ := tt.state.Hash() block := types.NewBlock(&pb.BeaconBlock{ - Slot: tt.blockSlot, - ActiveStateRoot: aRoot[:], - CrystallizedStateRoot: cRoot[:], - AncestorHashes: [][]byte{genesisHash[:]}, - PowChainRef: []byte("a"), + Slot: tt.blockSlot, + StateRoot: stateRoot[:], + AncestorHashes: [][]byte{genesisHash[:]}, + PowChainRef: []byte("a"), }) h, err := block.Hash() if err != nil { @@ -460,11 +447,7 @@ func TestUpdateHead(t *testing.T) { if err := chainService.beaconDB.SaveBlock(block); err != nil { t.Fatal(err) } - chainService.unfinalizedBlocks[h] = &statePair{ - activeState: tt.aState, - crystallizedState: tt.cState, - cycleTransition: true, - } + chainService.unfinalizedBlocks[h] = tt.state // If blocks pending processing is empty, the updateHead routine does nothing. blockChan <- block @@ -480,10 +463,9 @@ func TestUpdateBlockVoteCache(t *testing.T) { defer internal.TeardownDB(t, db) chainService := setupBeaconChain(t, true, db) - aState := types.NewGenesisActiveState() - cState, err := types.NewGenesisCrystallizedState(nil) + beaconState, err := types.NewGenesisBeaconState(nil) if err != nil { - t.Fatalf("failed to initialize crystallized state: %v", err) + t.Fatalf("failed to initialize genesis state: %v", err) } block := types.NewBlock(&pb.BeaconBlock{ Slot: 1, @@ -497,9 +479,9 @@ func TestUpdateBlockVoteCache(t *testing.T) { }, }) - err = chainService.calculateNewBlockVotes(block, aState, cState) + err = chainService.calculateNewBlockVotes(block, beaconState) if err != nil { - t.Fatalf("failed to update the block vote cache: %v", err) + t.Errorf("failed to update the block vote cache: %v", err) } } @@ -508,14 +490,13 @@ func TestUpdateBlockVoteCacheNoAttestations(t *testing.T) { defer internal.TeardownDB(t, db) chainService := setupBeaconChain(t, true, db) - aState := types.NewGenesisActiveState() - cState, err := types.NewGenesisCrystallizedState(nil) + beaconState, err := types.NewGenesisBeaconState(nil) if err != nil { - t.Fatalf("failed to initialize crystallized state: %v", err) + t.Fatalf("failed to initialize genesis state: %v", err) } block := types.NewBlock(nil) - err = chainService.calculateNewBlockVotes(block, aState, cState) + err = chainService.calculateNewBlockVotes(block, beaconState) if err != nil { t.Errorf("failed to update the block vote cache: %v", err) } diff --git a/beacon-chain/core/incentives/incentives_test.go b/beacon-chain/core/incentives/incentives_test.go index 73a1fc35d1..df622af1bf 100644 --- a/beacon-chain/core/incentives/incentives_test.go +++ b/beacon-chain/core/incentives/incentives_test.go @@ -28,7 +28,7 @@ func TestComputeValidatorRewardsAndPenalties(t *testing.T) { penaltyQuotient := QuadraticPenaltyQuotient() timeSinceFinality := uint64(5) - data := &pb.CrystallizedState{ + data := &pb.BeaconState{ Validators: validators, ValidatorSetChangeSlot: 1, LastJustifiedSlot: 4, diff --git a/beacon-chain/core/state/BUILD.bazel b/beacon-chain/core/state/BUILD.bazel index 0d3508d7dd..60f16c866e 100644 --- a/beacon-chain/core/state/BUILD.bazel +++ b/beacon-chain/core/state/BUILD.bazel @@ -5,6 +5,7 @@ go_library( srcs = [ "processing.go", "state_transition.go", + "validity_conditions.go", ], importpath = "github.com/prysmaticlabs/prysm/beacon-chain/core/state", visibility = ["//beacon-chain:__subpackages__"], @@ -14,7 +15,10 @@ go_library( "//beacon-chain/core/validators:go_default_library", "//beacon-chain/utils:go_default_library", "//proto/beacon/p2p/v1:go_default_library", + "//shared/bitutil:go_default_library", + "//shared/hashutil:go_default_library", "//shared/params:go_default_library", + "@com_github_ethereum_go_ethereum//common:go_default_library", ], ) @@ -23,6 +27,7 @@ go_test( srcs = [ "processing_test.go", "state_transition_test.go", + "validity_conditions_test.go", ], embed = [":go_default_library"], deps = [ @@ -31,6 +36,8 @@ go_test( "//beacon-chain/utils:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/bytes:go_default_library", + "//shared/hashutil:go_default_library", "//shared/params:go_default_library", + "@com_github_sirupsen_logrus//:go_default_library", ], ) diff --git a/beacon-chain/core/state/state_transition.go b/beacon-chain/core/state/state_transition.go index e9ab5a22ec..4a33c09c19 100644 --- a/beacon-chain/core/state/state_transition.go +++ b/beacon-chain/core/state/state_transition.go @@ -1,6 +1,9 @@ package state import ( + "fmt" + + "github.com/ethereum/go-ethereum/common" "github.com/prysmaticlabs/prysm/beacon-chain/core/incentives" "github.com/prysmaticlabs/prysm/beacon-chain/core/types" v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" @@ -10,12 +13,13 @@ import ( ) // NewStateTransition computes the new beacon state, given the previous beacon state -// and a beacon block. This method is called during a cycle transition. +// a beacon block, and its parent slot. This method is called during a cycle transition. // We also check for validator set change transition and compute for new // committees if necessary during this transition. func NewStateTransition( st *types.BeaconState, block *types.Block, + parentSlot uint64, blockVoteCache utils.BlockVoteCache, ) (*types.BeaconState, error) { var lastStateRecalculationSlotCycleBack uint64 @@ -26,81 +30,96 @@ func NewStateTransition( justifiedSlot := st.LastJustifiedSlot() finalizedSlot := st.LastFinalizedSlot() timeSinceFinality := block.SlotNumber() - newState.LastFinalizedSlot() - recentBlockHashes := st.RecentBlockHashes() newState.SetValidators(v.CopyValidators(newState.Validators())) - if st.LastStateRecalculationSlot() < params.BeaconConfig().CycleLength { - lastStateRecalculationSlotCycleBack = 0 - } else { - lastStateRecalculationSlotCycleBack = st.LastStateRecalculationSlot() - params.BeaconConfig().CycleLength - } - - // walk through all the slots from LastStateRecalculationSlot - cycleLength to - // LastStateRecalculationSlot - 1. - for i := uint64(0); i < params.BeaconConfig().CycleLength; i++ { - var blockVoteBalance uint64 - - slot := lastStateRecalculationSlotCycleBack + i - blockHash := recentBlockHashes[i] - - blockVoteBalance, validators := incentives.TallyVoteBalances( - blockHash, - blockVoteCache, - newState.Validators(), - v.ActiveValidatorIndices(newState.Validators()), - v.TotalActiveValidatorDeposit(newState.Validators()), - timeSinceFinality, - ) - - newState.SetValidators(validators) - - justifiedSlot, finalizedSlot, justifiedStreak = FinalizeAndJustifySlots( - slot, - justifiedSlot, - finalizedSlot, - justifiedStreak, - blockVoteBalance, - v.TotalActiveValidatorDeposit(st.Validators()), - ) - } - - crossLinks, err := crossLinkCalculations( - newState, - st.PendingAttestations(), - block.SlotNumber(), - ) + newState.ClearAttestations(st.LastStateRecalculationSlot()) + // Derive the new set of recent block hashes. + recentBlockHashes, err := st.CalculateNewBlockHashes(block, parentSlot) if err != nil { return nil, err } + newState.SetRecentBlockHashes(recentBlockHashes) + if err != nil { + return nil, fmt.Errorf("failed to update recent block hashes: %v", err) + } - newState.SetCrossLinks(crossLinks) + newRandao := createRandaoMix(block.RandaoReveal(), st.RandaoMix()) + newState.SetRandaoMix(newRandao[:]) - newState.SetLastJustifiedSlot(justifiedSlot) - newState.SetLastFinalizedSlot(finalizedSlot) - newState.SetJustifiedStreak(justifiedStreak) - newState.SetLastStateRecalculationSlot(newState.LastStateRecalculationSlot() + params.BeaconConfig().CycleLength) + // The changes below are only applied if this is a cycle transition. + if block.SlotNumber()%params.BeaconConfig().CycleLength == 0 { + if st.LastStateRecalculationSlot() < params.BeaconConfig().CycleLength { + lastStateRecalculationSlotCycleBack = 0 + } else { + lastStateRecalculationSlotCycleBack = st.LastStateRecalculationSlot() - params.BeaconConfig().CycleLength + } - // Exit the validators when their balance fall below min online deposit size. - newState.SetValidators(v.CheckValidatorMinDeposit(newState.Validators(), block.SlotNumber())) + // walk through all the slots from LastStateRecalculationSlot - cycleLength to + // LastStateRecalculationSlot - 1. + for i := uint64(0); i < params.BeaconConfig().CycleLength; i++ { + var blockVoteBalance uint64 - // Entering new validator set change transition. - if newState.IsValidatorSetChange(block.SlotNumber()) { - newState.SetValidatorSetChangeSlot(newState.LastStateRecalculationSlot()) - shardAndCommitteesForSlots, err := validatorSetRecalculations( - newState.ShardAndCommitteesForSlots(), - newState.Validators(), - block.ParentHash(), + slot := lastStateRecalculationSlotCycleBack + i + blockHash := recentBlockHashes[i] + + blockVoteBalance, validators := incentives.TallyVoteBalances( + common.BytesToHash(blockHash), + blockVoteCache, + newState.Validators(), + v.ActiveValidatorIndices(newState.Validators()), + v.TotalActiveValidatorDeposit(newState.Validators()), + timeSinceFinality, + ) + + newState.SetValidators(validators) + + justifiedSlot, finalizedSlot, justifiedStreak = FinalizeAndJustifySlots( + slot, + justifiedSlot, + finalizedSlot, + justifiedStreak, + blockVoteBalance, + v.TotalActiveValidatorDeposit(st.Validators()), + ) + } + + crossLinks, err := crossLinkCalculations( + newState, + st.PendingAttestations(), + block.SlotNumber(), ) if err != nil { return nil, err } - newState.SetShardAndCommitteesForSlots(shardAndCommitteesForSlots) - period := block.SlotNumber() / params.BeaconConfig().MinWithdrawalPeriod - totalPenalties := newState.PenalizedETH(period) - newState.SetValidators(v.ChangeValidators(block.SlotNumber(), totalPenalties, newState.Validators())) + newState.SetCrossLinks(crossLinks) + + newState.SetLastJustifiedSlot(justifiedSlot) + newState.SetLastFinalizedSlot(finalizedSlot) + newState.SetJustifiedStreak(justifiedStreak) + + // Exit the validators when their balance fall below min online deposit size. + newState.SetValidators(v.CheckValidatorMinDeposit(newState.Validators(), block.SlotNumber())) + + // Entering new validator set change transition. + if newState.IsValidatorSetChange(block.SlotNumber()) { + newState.SetValidatorSetChangeSlot(newState.LastStateRecalculationSlot()) + shardAndCommitteesForSlots, err := validatorSetRecalculations( + newState.ShardAndCommitteesForSlots(), + newState.Validators(), + block.ParentHash(), + ) + if err != nil { + return nil, err + } + newState.SetShardAndCommitteesForSlots(shardAndCommitteesForSlots) + + period := block.SlotNumber() / params.BeaconConfig().MinWithdrawalPeriod + totalPenalties := newState.PenalizedETH(period) + newState.SetValidators(v.ChangeValidators(block.SlotNumber(), totalPenalties, newState.Validators())) + } } - + newState.SetLastStateRecalculationSlot(newState.LastStateRecalculationSlot() + 1) return newState, nil } @@ -175,3 +194,13 @@ func validatorSetRecalculations( return append(shardAndCommittesForSlots[params.BeaconConfig().CycleLength:], newShardCommitteeArray...), nil } + +// createRandaoMix sets the block randao seed into a beacon state randao. This function +// XOR's the current state randao with the block's randao value added by the +// proposer. +func createRandaoMix(blockRandao [32]byte, beaconStateRandao [32]byte) [32]byte { + for i, b := range blockRandao { + beaconStateRandao[i] ^= b + } + return beaconStateRandao +} diff --git a/beacon-chain/core/state/state_transition_test.go b/beacon-chain/core/state/state_transition_test.go index fbc13c86d7..7dd4ebb4cd 100644 --- a/beacon-chain/core/state/state_transition_test.go +++ b/beacon-chain/core/state/state_transition_test.go @@ -12,7 +12,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/params" ) -func TestInitialDeriveCrystallizedState(t *testing.T) { +func TestInitialDeriveState(t *testing.T) { beaconState, err := types.NewGenesisBeaconState(nil) if err != nil { t.Fatalf("Failed to initialize beacon state: %v", err) @@ -24,10 +24,9 @@ func TestInitialDeriveCrystallizedState(t *testing.T) { } block := types.NewBlock(&pb.BeaconBlock{ - AncestorHashes: [][]byte{}, - Slot: 0, - ActiveStateRoot: []byte{}, - CrystallizedStateRoot: []byte{}, + AncestorHashes: [][]byte{{'A'}}, + Slot: 0, + StateRoot: []byte{}, Attestations: []*pb.AggregatedAttestation{{ Slot: 0, AttesterBitfield: attesterBitfield, @@ -36,7 +35,7 @@ func TestInitialDeriveCrystallizedState(t *testing.T) { }) var blockVoteCache utils.BlockVoteCache - newState, err := NewStateTransition(beaconState, block, blockVoteCache) + newState, err := NewStateTransition(beaconState, block, 0, blockVoteCache) if err != nil { t.Fatalf("failed to derive new state: %v", err) } @@ -49,12 +48,12 @@ func TestInitialDeriveCrystallizedState(t *testing.T) { t.Fatalf("expected justified streak to equal %d: got %d", 0, newState.JustifiedStreak()) } - if newState.LastStateRecalculationSlot() != params.BeaconConfig().CycleLength { - t.Fatalf("expected last state recalc to equal %d: got %d", params.BeaconConfig().CycleLength, newState.LastStateRecalculationSlot()) + if newState.LastStateRecalculationSlot() != 1 { + t.Fatalf("expected last state recalc to equal %d: got %d", 1, newState.LastStateRecalculationSlot()) } if newState.LastFinalizedSlot() != 0 { - t.Fatalf("xpected finalized slot to equal %d, got %d", 0, newState.LastFinalizedSlot()) + t.Fatalf("expected finalized slot to equal %d, got %d", 0, newState.LastFinalizedSlot()) } } @@ -64,11 +63,13 @@ func TestNextDeriveSlot(t *testing.T) { t.Fatalf("Failed to initialized state: %v", err) } - block := types.NewBlock(nil) + block := types.NewBlock(&pb.BeaconBlock{ + AncestorHashes: [][]byte{{'A'}}, + Slot: 0, + }) blockVoteCache := utils.NewBlockVoteCache() - - beaconState, err = NewStateTransition(beaconState, block, blockVoteCache) + beaconState, err = NewStateTransition(beaconState, block, 0, blockVoteCache) if err != nil { t.Fatalf("failed to derive next crystallized state: %v", err) } @@ -89,14 +90,18 @@ func TestNextDeriveSlot(t *testing.T) { VoteTotalDeposit: totalDeposits * 3 / 4, } } - beaconState.SetRecentBlockHashes(recentShardBlockHashes) - beaconState, err = NewStateTransition(beaconState, block, blockVoteCache) + beaconState.SetLastStateRecalculationSlot(params.BeaconConfig().CycleLength - 1) + block = types.NewBlock(&pb.BeaconBlock{ + AncestorHashes: [][]byte{{'A'}}, + Slot: params.BeaconConfig().CycleLength, + }) + beaconState, err = NewStateTransition(beaconState, block, params.BeaconConfig().CycleLength, blockVoteCache) if err != nil { t.Fatalf("failed to derive state: %v", err) } - if beaconState.LastStateRecalculationSlot() != 2*params.BeaconConfig().CycleLength { - t.Fatalf("expected last state recalc to equal %d: got %d", 2*params.BeaconConfig().CycleLength, beaconState.LastStateRecalculationSlot()) + if beaconState.LastStateRecalculationSlot() != params.BeaconConfig().CycleLength { + t.Fatalf("expected last state recalc to equal %d: got %d", params.BeaconConfig().CycleLength, beaconState.LastStateRecalculationSlot()) } if beaconState.LastJustifiedSlot() != params.BeaconConfig().CycleLength-1 { t.Fatalf("expected justified slot to equal %d: got %d", params.BeaconConfig().CycleLength-1, beaconState.LastJustifiedSlot()) @@ -109,40 +114,27 @@ func TestNextDeriveSlot(t *testing.T) { } beaconState.SetRecentBlockHashes(recentShardBlockHashes) - beaconState, err = NewStateTransition(beaconState, block, blockVoteCache) + beaconState.SetLastStateRecalculationSlot(2*params.BeaconConfig().CycleLength - 1) + block = types.NewBlock(&pb.BeaconBlock{ + AncestorHashes: [][]byte{{'A'}}, + Slot: params.BeaconConfig().CycleLength * 2, + }) + beaconState, err = NewStateTransition(beaconState, block, params.BeaconConfig().CycleLength*2, blockVoteCache) if err != nil { t.Fatalf("failed to derive state: %v", err) } - if beaconState.LastStateRecalculationSlot() != 3*params.BeaconConfig().CycleLength { - t.Fatalf("expected last state recalc to equal %d: got %d", 3*params.BeaconConfig().CycleLength, beaconState.LastStateRecalculationSlot()) + if beaconState.LastStateRecalculationSlot() != 2*params.BeaconConfig().CycleLength { + t.Fatalf("expected last state recalc to equal %d: got %d", 3, beaconState.LastStateRecalculationSlot()) } - if beaconState.LastJustifiedSlot() != 2*params.BeaconConfig().CycleLength-1 { + if beaconState.LastJustifiedSlot() != 2*(params.BeaconConfig().CycleLength-1) { t.Fatalf("expected justified slot to equal %d: got %d", 2*params.BeaconConfig().CycleLength-1, beaconState.LastJustifiedSlot()) } if beaconState.JustifiedStreak() != 2*params.BeaconConfig().CycleLength { t.Fatalf("expected justified streak to equal %d: got %d", 2*params.BeaconConfig().CycleLength, beaconState.JustifiedStreak()) } - if beaconState.LastFinalizedSlot() != params.BeaconConfig().CycleLength-2 { + if beaconState.LastFinalizedSlot() != params.BeaconConfig().CycleLength-3 { t.Fatalf("expected finalized slot to equal %d: got %d", params.BeaconConfig().CycleLength-2, beaconState.LastFinalizedSlot()) } - - beaconState.SetRecentBlockHashes(recentShardBlockHashes) - beaconState, err = NewStateTransition(beaconState, block, blockVoteCache) - if err != nil { - t.Fatalf("failed to derive state: %v", err) - } - if beaconState.LastStateRecalculationSlot() != 4*params.BeaconConfig().CycleLength { - t.Fatalf("expected last state recalc to equal %d: got %d", 3*params.BeaconConfig().CycleLength, beaconState.LastStateRecalculationSlot()) - } - if beaconState.LastJustifiedSlot() != 3*params.BeaconConfig().CycleLength-1 { - t.Fatalf("expected justified slot to equal %d: got %d", 3*params.BeaconConfig().CycleLength-1, beaconState.LastJustifiedSlot()) - } - if beaconState.JustifiedStreak() != 3*params.BeaconConfig().CycleLength { - t.Fatalf("expected justified streak to equal %d: got %d", 3*params.BeaconConfig().CycleLength, beaconState.JustifiedStreak()) - } - if beaconState.LastFinalizedSlot() != 2*params.BeaconConfig().CycleLength-2 { - t.Fatalf("expected finalized slot to equal %d: got %d", 2*params.BeaconConfig().CycleLength-2, beaconState.LastFinalizedSlot()) - } } func TestProcessCrosslinks(t *testing.T) { @@ -205,7 +197,7 @@ func TestProcessCrosslinks(t *testing.T) { func TestIsNewValidatorSetTransition(t *testing.T) { beaconState, err := types.NewGenesisBeaconState(nil) if err != nil { - t.Fatalf("Failed to initialize crystallized state: %v", err) + t.Fatalf("Failed to initialize state: %v", err) } beaconState.SetValidatorSetChangeSlot(1) if beaconState.IsValidatorSetChange(0) { diff --git a/beacon-chain/core/state/validity_conditions.go b/beacon-chain/core/state/validity_conditions.go new file mode 100644 index 0000000000..e94e298b77 --- /dev/null +++ b/beacon-chain/core/state/validity_conditions.go @@ -0,0 +1,183 @@ +package state + +import ( + "bytes" + "errors" + "fmt" + "time" + + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" + v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" + pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/bitutil" + "github.com/prysmaticlabs/prysm/shared/hashutil" + "github.com/prysmaticlabs/prysm/shared/params" +) + +// IsValidBlock verifies a block is valid according to the ETH 2.0 specification for +// validity conditions taking into consideration attestation processing and more. +// TODO(#781): Refactor with the new spec validity conditions. +func IsValidBlock( + block *types.Block, + beaconState *types.BeaconState, + parentSlot uint64, + genesisTime time.Time, + isInChain func(blockHash [32]byte) bool, +) error { + _, err := block.Hash() + if err != nil { + return fmt.Errorf("could not hash incoming block: %v", err) + } + + if block.SlotNumber() == 0 { + return errors.New("cannot process a genesis block: received block with slot 0") + } + + if !block.IsSlotValid(genesisTime) { + return fmt.Errorf("slot of block is too high: %d", block.SlotNumber()) + } + + if err := doesParentProposerExist(block, beaconState, parentSlot); err != nil { + return fmt.Errorf("could not get proposer index: %v", err) + } + + for _, attestation := range block.Attestations() { + if err := isBlockAttestationValid(block, attestation, beaconState, parentSlot, isInChain); err != nil { + return fmt.Errorf("invalid block attestation: %v", err) + } + } + + _, proposerIndex, err := v.ProposerShardAndIndex( + beaconState.ShardAndCommitteesForSlots(), + beaconState.LastStateRecalculationSlot(), + block.SlotNumber(), + ) + if err != nil { + return fmt.Errorf("could not get proposer index: %v", err) + } + + stateProposerRandaoSeed := beaconState.Validators()[proposerIndex].RandaoCommitment + blockRandaoReveal := block.RandaoReveal() + + // If this is a block created by the simulator service (while in development + // mode), we skip the RANDAO validation condition. + isSimulatedBlock := bytes.Equal(blockRandaoReveal[:], params.BeaconConfig().SimulatedBlockRandao[:]) + if !isSimulatedBlock && !block.IsRandaoValid(stateProposerRandaoSeed) { + return fmt.Errorf( + "pre-image of %#x is %#x, Got: %#x", + blockRandaoReveal[:], + hashutil.Hash(blockRandaoReveal[:]), + stateProposerRandaoSeed, + ) + } + return nil +} + +// doesParentProposerExist checks that the proposer from the parent slot is included in the first +// aggregated attestation object +func doesParentProposerExist(block *types.Block, beaconState *types.BeaconState, parentSlot uint64) error { + _, parentProposerIndex, err := v.ProposerShardAndIndex( + beaconState.ShardAndCommitteesForSlots(), + beaconState.LastStateRecalculationSlot(), + parentSlot, + ) + if err != nil { + return err + } + + // Verifies the attester bitfield to check if the proposer index is in the first included one. + if isBitSet, err := bitutil.CheckBit(block.Attestations()[0].AttesterBitfield, int(parentProposerIndex)); !isBitSet { + return fmt.Errorf("could not locate proposer in the first attestation of AttestionRecord: %v", err) + } + return nil +} + +// isBlockAttestationValid verifies a block's attestations pass validity conditions. +// TODO(#781): Refactor with the new spec attestation checking conditions. +func isBlockAttestationValid( + block *types.Block, + attestation *pb.AggregatedAttestation, + beaconState *types.BeaconState, + parentSlot uint64, + isInChain func(blockHash [32]byte) bool, +) error { + // Validate attestation's slot number has is within range of incoming block number. + if err := isAttestationSlotNumberValid(attestation.Slot, parentSlot); err != nil { + return fmt.Errorf("invalid attestation slot %d: %v", attestation.Slot, err) + } + + if attestation.JustifiedSlot > beaconState.LastJustifiedSlot() { + return fmt.Errorf( + "attestation's justified slot has to be <= the state's last justified slot: found: %d. want <=: %d", + attestation.JustifiedSlot, + beaconState.LastJustifiedSlot(), + ) + } + + hash := [32]byte{} + copy(hash[:], attestation.JustifiedBlockHash) + if !isInChain(hash) { + return fmt.Errorf( + "the attestation's justifed block hash not found in current chain: justified block hash: 0x%x", + attestation.JustifiedBlockHash, + ) + } + + // Get all the block hashes up to cycle length. + parentHashes, err := beaconState.SignedParentHashes(block, attestation) + if err != nil { + return fmt.Errorf("unable to get signed parent hashes: %v", err) + } + + shardCommittees, err := v.GetShardAndCommitteesForSlot( + beaconState.ShardAndCommitteesForSlots(), + beaconState.LastStateRecalculationSlot(), + attestation.Slot, + ) + attesterIndices, err := v.AttesterIndices(shardCommittees, attestation) + if err != nil { + return fmt.Errorf("unable to get validator committee: %v", err) + } + + // Verify attester bitfields matches crystallized state's prev computed bitfield. + if !v.AreAttesterBitfieldsValid(attestation, attesterIndices) { + return fmt.Errorf("unable to match attester bitfield with shard and committee bitfield") + } + + forkVersion := beaconState.PostForkVersion() + if attestation.Slot < beaconState.ForkSlotNumber() { + forkVersion = beaconState.PreForkVersion() + } + + // TODO(#258): Generate validators aggregated pub key. + attestationMsg := types.AttestationMsg( + parentHashes, + attestation.ShardBlockHash, + attestation.Slot, + attestation.Shard, + attestation.JustifiedSlot, + forkVersion, + ) + _ = attestationMsg + + // TODO(#258): Verify msgHash against aggregated pub key and aggregated signature. + return nil +} + +func isAttestationSlotNumberValid(attestationSlot uint64, parentSlot uint64) error { + if parentSlot != 0 && attestationSlot > parentSlot { + return fmt.Errorf( + "attestation slot number higher than parent block's slot number: found: %d, needed < %d", + attestationSlot, + parentSlot, + ) + } + if parentSlot >= params.BeaconConfig().CycleLength-1 && attestationSlot < parentSlot-params.BeaconConfig().CycleLength+1 { + return fmt.Errorf( + "attestation slot number lower than parent block's slot number by one CycleLength: found: %d, needed > %d", + attestationSlot, + parentSlot-params.BeaconConfig().CycleLength+1, + ) + } + return nil +} diff --git a/beacon-chain/core/state/validity_conditions_test.go b/beacon-chain/core/state/validity_conditions_test.go new file mode 100644 index 0000000000..94ed350323 --- /dev/null +++ b/beacon-chain/core/state/validity_conditions_test.go @@ -0,0 +1,161 @@ +package state + +import ( + "testing" + + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" + "github.com/prysmaticlabs/prysm/beacon-chain/utils" + pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/hashutil" + "github.com/prysmaticlabs/prysm/shared/params" + "github.com/sirupsen/logrus" +) + +func init() { + logrus.SetLevel(logrus.DebugLevel) +} + +type mockDB struct { + blockVoteCache utils.BlockVoteCache +} + +func (f *mockDB) HasBlock(h [32]byte) bool { + return true +} + +func (f *mockDB) ReadBlockVoteCache(blockHashes [][32]byte) (utils.BlockVoteCache, error) { + return f.blockVoteCache, nil +} + +func (f *mockDB) loadMockBlockVoteCache(blockVoteCache utils.BlockVoteCache) { + f.blockVoteCache = blockVoteCache +} + +func TestBlockValidity(t *testing.T) { + beaconState, err := types.NewGenesisBeaconState(nil) + if err != nil { + t.Fatalf("failed to generate beacon state: %v", err) + } + + recentBlockHashes := make([][]byte, 2*params.BeaconConfig().CycleLength) + for i := 0; i < 2*int(params.BeaconConfig().CycleLength); i++ { + recentBlockHashes = append(recentBlockHashes, make([]byte, 32)) + } + randaoPreCommit := [32]byte{'A'} + hashedRandaoPreCommit := hashutil.Hash(randaoPreCommit[:]) + validators := beaconState.Validators() + validators[1].RandaoCommitment = hashedRandaoPreCommit[:] + beaconState.SetValidators(validators) + beaconState.SetRecentBlockHashes(recentBlockHashes) + + b := types.NewBlock(&pb.BeaconBlock{ + Slot: 1, + RandaoReveal: randaoPreCommit[:], + Attestations: []*pb.AggregatedAttestation{ + { + Slot: 0, + Shard: 1, + JustifiedSlot: 0, + AttesterBitfield: []byte{128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + }, + }, + }) + + parentSlot := uint64(0) + db := &mockDB{} + + genesisTime := params.BeaconConfig().GenesisTime + if err := IsValidBlock(b, beaconState, parentSlot, genesisTime, db.HasBlock); err != nil { + t.Fatalf("failed block validation: %v", err) + } +} + +func TestBlockValidityNoParentProposer(t *testing.T) { + beaconState, err := types.NewGenesisBeaconState(nil) + if err != nil { + t.Fatalf("failed to generate beacon state: %v", err) + } + + recentBlockHashes := make([][]byte, 2*params.BeaconConfig().CycleLength) + for i := 0; i < 2*int(params.BeaconConfig().CycleLength); i++ { + recentBlockHashes = append(recentBlockHashes, make([]byte, 32)) + } + + beaconState.SetRecentBlockHashes(recentBlockHashes) + + parentSlot := uint64(1) + db := &mockDB{} + + // Test case with invalid RANDAO reveal. + badRandaoBlock := types.NewBlock(&pb.BeaconBlock{ + Slot: 2, + RandaoReveal: []byte{'B'}, + Attestations: []*pb.AggregatedAttestation{ + { + Slot: 0, + Shard: 1, + JustifiedSlot: 0, + AttesterBitfield: []byte{64, 0}, + }, + }, + }) + genesisTime := params.BeaconConfig().GenesisTime + if err := IsValidBlock(badRandaoBlock, beaconState, parentSlot, genesisTime, db.HasBlock); err == nil { + t.Fatal("test should have failed without a parent proposer") + } +} + +func TestBlockValidityInvalidRandao(t *testing.T) { + beaconState, err := types.NewGenesisBeaconState(nil) + if err != nil { + t.Fatalf("failed to generate beacon state: %v", err) + } + + recentBlockHashes := make([][]byte, 2*params.BeaconConfig().CycleLength) + for i := 0; i < 2*int(params.BeaconConfig().CycleLength); i++ { + recentBlockHashes = append(recentBlockHashes, make([]byte, 32)) + } + + beaconState.SetRecentBlockHashes(recentBlockHashes) + + parentSlot := uint64(0) + db := &mockDB{} + + // Test case with invalid RANDAO reveal. + badRandaoBlock := types.NewBlock(&pb.BeaconBlock{ + Slot: 1, + RandaoReveal: []byte{'B'}, + Attestations: []*pb.AggregatedAttestation{ + { + Slot: 0, + Shard: 1, + JustifiedSlot: 0, + AttesterBitfield: []byte{64, 0}, + }, + }, + }) + + genesisTime := params.BeaconConfig().GenesisTime + if err := IsValidBlock(badRandaoBlock, beaconState, parentSlot, genesisTime, db.HasBlock); err == nil { + t.Fatal("should have failed with invalid RANDAO") + } +} + +func TestIsAttestationSlotNumberValid(t *testing.T) { + if err := isAttestationSlotNumberValid(2, 1); err == nil { + t.Error("attestation slot number can't be higher than parent block's slot number") + } + + if err := isAttestationSlotNumberValid(1, params.BeaconConfig().CycleLength+1); err == nil { + t.Error("attestation slot number can't be lower than parent block's slot number by one CycleLength and 1") + } + + if err := isAttestationSlotNumberValid(2, 2); err != nil { + t.Errorf("attestation slot number could be less than or equal to parent block's slot number: %v", err) + } + + if err := isAttestationSlotNumberValid(2, 10); err != nil { + t.Errorf("attestation slot number could be less than or equal to parent block's slot number: %v", err) + } +} diff --git a/beacon-chain/core/types/BUILD.bazel b/beacon-chain/core/types/BUILD.bazel index 61d43a3fec..dd1a2cac13 100644 --- a/beacon-chain/core/types/BUILD.bazel +++ b/beacon-chain/core/types/BUILD.bazel @@ -1,8 +1,9 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "go_default_library", srcs = [ + "attestation.go", "block.go", "state.go", ], @@ -10,10 +11,29 @@ go_library( visibility = ["//beacon-chain:__subpackages__"], deps = [ "//beacon-chain/core/validators:go_default_library", + "//beacon-chain/utils:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/hashutil:go_default_library", "//shared/params:go_default_library", "@com_github_ethereum_go_ethereum//common:go_default_library", "@com_github_gogo_protobuf//proto:go_default_library", + "@com_github_golang_protobuf//proto:go_default_library", + "@com_github_golang_protobuf//ptypes:go_default_library_gen", + ], +) + +go_test( + name = "go_default_test", + srcs = [ + "attestation_test.go", + "block_test.go", + "state_test.go", + ], + embed = [":go_default_library"], + deps = [ + "//proto/beacon/p2p/v1:go_default_library", + "//shared/params:go_default_library", + "@com_github_ethereum_go_ethereum//common:go_default_library", + "@com_github_golang_protobuf//ptypes:go_default_library_gen", ], ) diff --git a/beacon-chain/types/attestation.go b/beacon-chain/core/types/attestation.go similarity index 90% rename from beacon-chain/types/attestation.go rename to beacon-chain/core/types/attestation.go index e9ece752f1..2eb98f5904 100644 --- a/beacon-chain/types/attestation.go +++ b/beacon-chain/core/types/attestation.go @@ -1,4 +1,3 @@ -// Package types defines the essential types used throughout the beacon-chain. package types import ( @@ -37,6 +36,28 @@ func NewAttestation(data *pb.AggregatedAttestation) *Attestation { return &Attestation{data: data} } +// AttestationMsg hashes parentHashes + shardID + slotNumber + shardBlockHash + justifiedSlot +// into a message to use for verifying with aggregated public key and signature. +func AttestationMsg( + parentHashes [][32]byte, + blockHash []byte, + slot uint64, + shardID uint64, + justifiedSlot uint64, + forkVersion uint64, +) [32]byte { + msg := make([]byte, binary.MaxVarintLen64) + binary.BigEndian.PutUint64(msg, forkVersion) + binary.PutUvarint(msg, slot%params.BeaconConfig().CycleLength) + for _, parentHash := range parentHashes { + msg = append(msg, parentHash[:]...) + } + binary.PutUvarint(msg, shardID) + msg = append(msg, blockHash...) + binary.PutUvarint(msg, justifiedSlot) + return hashutil.Hash(msg) +} + // Proto returns the underlying protobuf data. func (a *Attestation) Proto() *pb.AggregatedAttestation { return a.data @@ -119,42 +140,22 @@ func (a *Attestation) AggregateSig() []uint64 { // VerifyProposerAttestation verifies the proposer's attestation of the block. // Proposers broadcast the attestation along with the block to its peers. func (a *Attestation) VerifyProposerAttestation(pubKey [32]byte, proposerShardID uint64) error { - // Verify the attestation attached with block response. // Get proposer index and shardID. - attestationMsg := AttestationMsg( a.ObliqueParentHashes(), a.ShardBlockHash(), a.SlotNumber(), proposerShardID, a.JustifiedSlotNumber(), - params.BeaconConfig().InitialForkVersion) - - log.Debugf("Constructing attestation message for incoming block %#x", attestationMsg) - + params.BeaconConfig().InitialForkVersion, + ) + _ = attestationMsg + _ = pubKey // TODO(#258): use attestationMsg to verify against signature and public key. Return error if incorrect. - log.Debugf("Verifying attestation with public key %#x", pubKey) - - log.Debugf("Successfully verified attestation with incoming block") return nil } -// AttestationMsg hashes parentHashes + shardID + slotNumber + shardBlockHash + justifiedSlot -// into a message to use for verifying with aggregated public key and signature. -func AttestationMsg(parentHashes [][32]byte, blockHash []byte, slot uint64, shardID uint64, justifiedSlot uint64, forkVersion uint32) [32]byte { - msg := make([]byte, binary.MaxVarintLen64) - binary.BigEndian.PutUint32(msg, forkVersion) - binary.PutUvarint(msg, slot%params.BeaconConfig().CycleLength) - for _, parentHash := range parentHashes { - msg = append(msg, parentHash[:]...) - } - binary.PutUvarint(msg, shardID) - msg = append(msg, blockHash...) - binary.PutUvarint(msg, justifiedSlot) - return hashutil.Hash(msg) -} - // ContainsValidator checks if the validator is included in the attestation. // TODO(#569): Modify method to accept a single index rather than a bitfield. func (a *Attestation) ContainsValidator(bitfield []byte) bool { diff --git a/beacon-chain/types/attestation_test.go b/beacon-chain/core/types/attestation_test.go similarity index 100% rename from beacon-chain/types/attestation_test.go rename to beacon-chain/core/types/attestation_test.go diff --git a/beacon-chain/core/types/block.go b/beacon-chain/core/types/block.go index 834022376a..8f412a889d 100644 --- a/beacon-chain/core/types/block.go +++ b/beacon-chain/core/types/block.go @@ -1,9 +1,20 @@ package types import ( + "fmt" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/gogo/protobuf/proto" + "github.com/golang/protobuf/ptypes" + "github.com/prysmaticlabs/prysm/beacon-chain/utils" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/shared/hashutil" + "github.com/prysmaticlabs/prysm/shared/params" ) +var clock utils.Clock = &utils.RealClock{} + // Block defines a beacon chain core primitive. type Block struct { data *pb.BeaconBlock @@ -14,16 +25,14 @@ type Block struct { func NewBlock(data *pb.BeaconBlock) *Block { if data == nil { var ancestorHashes = make([][]byte, 0, 32) - // It is assumed when data==nil, a genesis block will be returned. return &Block{ data: &pb.BeaconBlock{ - AncestorHashes: ancestorHashes, - RandaoReveal: []byte{0}, - PowChainRef: []byte{0}, - ActiveStateRoot: []byte{0}, - CrystallizedStateRoot: []byte{0}, - Specials: []*pb.SpecialRecord{}, + AncestorHashes: ancestorHashes, + RandaoReveal: []byte{0}, + PowChainRef: []byte{0}, + StateRoot: []byte{0}, + Specials: []*pb.SpecialRecord{}, }, } } @@ -31,6 +40,17 @@ func NewBlock(data *pb.BeaconBlock) *Block { return &Block{data: data} } +// NewGenesisBlock returns the canonical, genesis block for the beacon chain protocol. +func NewGenesisBlock(stateRoot [32]byte) *Block { + // Genesis time here is static so error can be safely ignored. + // #nosec G104 + protoGenesis, _ := ptypes.TimestampProto(params.BeaconConfig().GenesisTime) + gb := NewBlock(nil) + gb.data.Timestamp = protoGenesis + gb.data.StateRoot = stateRoot[:] + return gb +} + // SlotNumber of the beacon block. func (b *Block) SlotNumber() uint64 { return b.data.Slot @@ -42,3 +62,77 @@ func (b *Block) ParentHash() [32]byte { copy(h[:], b.data.AncestorHashes[0]) return h } + +// Hash generates the blake2b hash of the block +func (b *Block) Hash() ([32]byte, error) { + data, err := proto.Marshal(b.data) + if err != nil { + return [32]byte{}, fmt.Errorf("could not marshal block proto data: %v", err) + } + return hashutil.Hash(data), nil +} + +// Proto returns the underlying protobuf data within a block primitive. +func (b *Block) Proto() *pb.BeaconBlock { + return b.data +} + +// Marshal encodes block object into the wire format. +func (b *Block) Marshal() ([]byte, error) { + return proto.Marshal(b.data) +} + +// Timestamp returns the Go type time.Time from the protobuf type contained in the block. +func (b *Block) Timestamp() (time.Time, error) { + return ptypes.Timestamp(b.data.Timestamp) +} + +// AncestorHashes of the block. +func (b *Block) AncestorHashes() [][]byte { + return b.data.AncestorHashes +} + +// AttestationCount returns the number of attestations. +func (b *Block) AttestationCount() int { + return len(b.data.Attestations) +} + +// Attestations returns an array of attestations in the block. +func (b *Block) Attestations() []*pb.AggregatedAttestation { + return b.data.Attestations +} + +// PowChainRef returns a keccak256 hash corresponding to a PoW chain block. +func (b *Block) PowChainRef() common.Hash { + return common.BytesToHash(b.data.PowChainRef) +} + +// RandaoReveal returns the blake2b randao hash. +func (b *Block) RandaoReveal() [32]byte { + var h [32]byte + copy(h[:], b.data.RandaoReveal) + return h +} + +// StateRoot returns the state hash. +func (b *Block) StateRoot() [32]byte { + var h [32]byte + copy(h[:], b.data.StateRoot) + return h +} + +// IsRandaoValid verifies the validity of randao from block by comparing it with +// the proposer's randao from the beacon state. +func (b *Block) IsRandaoValid(stateRandao []byte) bool { + var h [32]byte + copy(h[:], stateRandao) + blockRandaoReveal := b.RandaoReveal() + return hashutil.Hash(blockRandaoReveal[:]) == h +} + +// IsSlotValid compares the slot to the system clock to determine if the block is valid. +func (b *Block) IsSlotValid(genesisTime time.Time) bool { + slotDuration := time.Duration(b.SlotNumber()*params.BeaconConfig().SlotDuration) * time.Second + validTimeThreshold := genesisTime.Add(slotDuration) + return clock.Now().After(validTimeThreshold) +} diff --git a/beacon-chain/core/types/block_test.go b/beacon-chain/core/types/block_test.go new file mode 100644 index 0000000000..78757eea5c --- /dev/null +++ b/beacon-chain/core/types/block_test.go @@ -0,0 +1,84 @@ +package types + +import ( + "bytes" + "reflect" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/golang/protobuf/ptypes" + "github.com/prysmaticlabs/prysm/shared/params" +) + +func TestGenesisBlock(t *testing.T) { + stateHash := [32]byte{0} + b1 := NewGenesisBlock(stateHash) + b2 := NewGenesisBlock(stateHash) + + // We ensure that initializing a proto timestamp from + // genesis time will lead to no error. + if _, err := ptypes.TimestampProto(params.BeaconConfig().GenesisTime); err != nil { + t.Errorf("could not create proto timestamp, expected no error: %v", err) + } + + h1, err1 := b1.Hash() + h2, err2 := b2.Hash() + if err1 != nil || err2 != nil { + t.Fatalf("failed to hash genesis block: %v %v", err1, err2) + } + + if h1 != h2 { + t.Errorf("genesis block hash should be identical: %#x %#x", h1, h2) + } + + if b1.data.AncestorHashes == nil { + t.Error("genesis block missing ParentHash field") + } + + if b1.AttestationCount() > 0 || b1.Attestations() != nil { + t.Errorf("genesis block should have 0 attestations") + } + + if b1.RandaoReveal() != [32]byte{} { + t.Error("genesis block missing RandaoReveal field") + } + + if b1.PowChainRef() != common.BytesToHash([]byte{}) { + t.Error("genesis block missing PowChainRef field") + } + + if b1.StateRoot() != stateHash { + t.Error("genesis block StateRoot isn't initialized correctly") + } + + rd := [32]byte{} + if b1.IsRandaoValid(rd[:]) { + t.Error("RANDAO should be empty") + } + + gt1, _ := b1.Timestamp() + gt2, _ := b2.Timestamp() + if gt1 != gt2 { + t.Error("different timestamp") + } + + enc1, _ := b1.Marshal() + enc2, _ := b2.Marshal() + if !bytes.Equal(enc1, enc2) { + t.Error("genesis block encoding does not match") + } + + if !reflect.DeepEqual(b1.Proto(), b2.Proto()) { + t.Error("genesis blocks proto should be equal") + } + + b3 := NewBlock(nil) + h3, err3 := b3.Hash() + if err3 != nil { + t.Fatalf("failed to hash genesis block: %v", err3) + } + + if h1 == h3 { + t.Errorf("genesis block and new block should not have identical hash: %#x", h1) + } +} diff --git a/beacon-chain/core/types/state.go b/beacon-chain/core/types/state.go index 4ef1785560..2e5bbd45f4 100644 --- a/beacon-chain/core/types/state.go +++ b/beacon-chain/core/types/state.go @@ -1,6 +1,8 @@ package types import ( + "fmt" + "github.com/ethereum/go-ethereum/common" "github.com/gogo/protobuf/proto" v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" @@ -176,6 +178,12 @@ func (b *BeaconState) IsValidatorSetChange(slotNumber uint64) bool { return true } +// IsCycleTransition checks if a new cycle has been reached. At that point, +// a new state transition will occur in the beacon chain. +func (b *BeaconState) IsCycleTransition(slotNumber uint64) bool { + return slotNumber >= b.LastStateRecalculationSlot()+params.BeaconConfig().CycleLength +} + // ShardAndCommitteesForSlots returns the shard committee object. func (b *BeaconState) ShardAndCommitteesForSlots() []*pb.ShardAndCommitteeArray { return b.data.ShardAndCommitteesForSlots @@ -269,6 +277,76 @@ func (b *BeaconState) PenalizedETH(period uint64) uint64 { return totalPenalty } +// SignedParentHashes returns all the parent hashes stored in active state up to last cycle length. +func (b *BeaconState) SignedParentHashes(block *Block, attestation *pb.AggregatedAttestation) ([][32]byte, error) { + recentBlockHashes := b.RecentBlockHashes() + obliqueParentHashes := attestation.ObliqueParentHashes + earliestSlot := int(block.SlotNumber()) - len(recentBlockHashes) + + startIdx := int(attestation.Slot) - earliestSlot - int(params.BeaconConfig().CycleLength) + 1 + endIdx := startIdx - len(attestation.ObliqueParentHashes) + int(params.BeaconConfig().CycleLength) + if startIdx < 0 || endIdx > len(recentBlockHashes) || endIdx <= startIdx { + return nil, fmt.Errorf("attempt to fetch recent blockhashes from %d to %d invalid", startIdx, endIdx) + } + + hashes := make([][32]byte, 0, params.BeaconConfig().CycleLength) + for i := startIdx; i < endIdx; i++ { + hashes = append(hashes, recentBlockHashes[i]) + } + + for i := 0; i < len(obliqueParentHashes); i++ { + hash := common.BytesToHash(obliqueParentHashes[i]) + hashes = append(hashes, hash) + } + return hashes, nil +} + +// ClearAttestations removes attestations older than last state recalc slot. +func (b *BeaconState) ClearAttestations(lastStateRecalc uint64) { + existing := b.data.PendingAttestations + update := make([]*pb.AggregatedAttestation, 0, len(existing)) + for _, a := range existing { + if a.GetSlot() >= lastStateRecalc { + update = append(update, a) + } + } + b.data.PendingAttestations = update +} + +// CalculateNewBlockHashes builds a new slice of recent block hashes with the +// provided block and the parent slot number. +// +// The algorithm is: +// 1) shift the array by block.SlotNumber - parentSlot (i.e. truncate the +// first by the number of slots that have occurred between the block and +// its parent). +// +// 2) fill the array with the parent block hash for all values between the parent +// slot and the block slot. +// +// Computation of the state hash depends on this feature that slots with +// missing blocks have the block hash of the next block hash in the chain. +// +// For example, if we have a segment of recent block hashes that look like this +// [0xF, 0x7, 0x0, 0x0, 0x5] +// +// Where 0x0 is an empty or missing hash where no block was produced in the +// alloted slot. When storing the list (or at least when computing the hash of +// the active state), the list should be backfilled as such: +// +// [0xF, 0x7, 0x5, 0x5, 0x5] +// +// This method does not mutate the state. +func (b *BeaconState) CalculateNewBlockHashes(block *Block, parentSlot uint64) ([][]byte, error) { + distance := block.SlotNumber() - parentSlot + existing := b.data.RecentBlockHashes + update := existing[distance:] + for len(update) < 2*int(params.BeaconConfig().CycleLength) { + update = append(update, block.AncestorHashes()[0]) + } + return update, nil +} + // SetCrossLinks updates the inner proto's cross link records. func (b *BeaconState) SetCrossLinks(crossLinks []*pb.CrosslinkRecord) { b.data.Crosslinks = crossLinks @@ -299,6 +377,16 @@ func (b *BeaconState) SetLastStateRecalculationSlot(slot uint64) { b.data.LastStateRecalculationSlot = slot } +// SetPendingAttestations updates the inner proto's pending attestations. +func (b *BeaconState) SetPendingAttestations(pendingAttestations []*pb.AggregatedAttestation) { + b.data.PendingAttestations = pendingAttestations +} + +// SetRandaoMix updates the inner proto's randao mix. +func (b *BeaconState) SetRandaoMix(randaoMix []byte) { + b.data.RandaoMix = randaoMix +} + // SetRecentBlockHashes updates the inner proto's recent block hashes. func (b *BeaconState) SetRecentBlockHashes(recentShardBlockHashes [][]byte) { b.data.RecentBlockHashes = recentShardBlockHashes diff --git a/beacon-chain/types/active_state_test.go b/beacon-chain/core/types/state_test.go similarity index 56% rename from beacon-chain/types/active_state_test.go rename to beacon-chain/core/types/state_test.go index 136395bce8..a69b45460a 100644 --- a/beacon-chain/types/active_state_test.go +++ b/beacon-chain/core/types/state_test.go @@ -9,45 +9,45 @@ import ( "github.com/prysmaticlabs/prysm/shared/params" ) -func TestGenesisActiveState_HashEquality(t *testing.T) { - aState1 := NewGenesisActiveState() - aState2 := NewGenesisActiveState() +func TestGenesisState_HashEquality(t *testing.T) { + state1, _ := NewGenesisBeaconState(nil) + state2, _ := NewGenesisBeaconState(nil) - h1, err1 := aState1.Hash() - h2, err2 := aState2.Hash() + h1, err1 := state1.Hash() + h2, err2 := state2.Hash() if err1 != nil || err2 != nil { - t.Fatalf("Failed to hash crystallized state: %v %v", err1, err2) + t.Fatalf("Failed to hash state: %v %v", err1, err2) } if h1 != h2 { - t.Fatalf("Hash of two genesis crystallized states should be equal: %#x", h1) + t.Fatalf("Hash of two genesis states should be equal: %#x", h1) } } -func TestGenesisActiveState_InitializesRecentBlockHashes(t *testing.T) { - as := NewGenesisActiveState() - want, got := len(as.data.RecentBlockHashes), 2*int(params.BeaconConfig().CycleLength) +func TestGenesisState_InitializesRecentBlockHashes(t *testing.T) { + s, _ := NewGenesisBeaconState(nil) + want, got := len(s.data.RecentBlockHashes), 2*int(params.BeaconConfig().CycleLength) if want != got { t.Errorf("Wrong number of recent block hashes. Got: %d Want: %d", got, want) } - want = cap(as.data.RecentBlockHashes) + want = cap(s.data.RecentBlockHashes) if want != got { t.Errorf("The slice underlying array capacity is wrong. Got: %d Want: %d", got, want) } zero := make([]byte, 0, 32) - for _, h := range as.data.RecentBlockHashes { + for _, h := range s.data.RecentBlockHashes { if !bytes.Equal(h, zero) { t.Errorf("Unexpected non-zero hash data: %v", h) } } } -func TestCopyActiveState(t *testing.T) { - aState1 := NewGenesisActiveState() - aState2 := aState1.CopyState() +func TestCopyState(t *testing.T) { + state1, _ := NewGenesisBeaconState(nil) + state2 := state1.CopyState() newAttestations := []*pb.AggregatedAttestation{ { @@ -55,34 +55,35 @@ func TestCopyActiveState(t *testing.T) { Shard: 1, }, } - aState1.data.PendingAttestations = append(aState1.data.PendingAttestations, newAttestations...) - if len(aState1.data.PendingAttestations) == len(aState2.data.PendingAttestations) { + + state1.data.PendingAttestations = append(state1.data.PendingAttestations, newAttestations...) + if len(state1.data.PendingAttestations) == len(state2.data.PendingAttestations) { t.Fatalf("The PendingAttestations should not equal each other %d, %d", - len(aState1.data.PendingAttestations), - len(aState2.data.PendingAttestations), + len(state1.data.PendingAttestations), + len(state2.data.PendingAttestations), ) } - aState1.data.RecentBlockHashes = [][]byte{{'A'}} - if len(aState1.RecentBlockHashes()) == len(aState2.RecentBlockHashes()) { + state1.data.RecentBlockHashes = [][]byte{{'A'}} + if len(state1.RecentBlockHashes()) == len(state2.RecentBlockHashes()) { t.Fatalf("The RecentBlockHashes should not equal each other %d, %d", - len(aState1.RecentBlockHashes()), - len(aState2.RecentBlockHashes()), + len(state1.RecentBlockHashes()), + len(state2.RecentBlockHashes()), ) } - aState1.data.RandaoMix = []byte{22, 21} - aState2.data.RandaoMix = []byte{40, 31} - if aState1.data.RandaoMix[0] == aState2.data.RandaoMix[0] { + state1.data.RandaoMix = []byte{22, 21} + state2.data.RandaoMix = []byte{40, 31} + if state1.data.RandaoMix[0] == state2.data.RandaoMix[0] { t.Fatalf("The RandaoMix should not equal each other %d, %d", - aState1.data.RandaoMix[0], - aState2.data.RandaoMix[0], + state1.data.RandaoMix[0], + state2.data.RandaoMix[0], ) } } func TestUpdateAttestations(t *testing.T) { - aState := NewGenesisActiveState() + state, _ := NewGenesisBeaconState(nil) newAttestations := []*pb.AggregatedAttestation{ { @@ -95,27 +96,15 @@ func TestUpdateAttestations(t *testing.T) { }, } - aState = aState.UpdateAttestations(newAttestations) - attestations := aState.data.PendingAttestations + state.SetPendingAttestations(newAttestations) + attestations := state.data.PendingAttestations if len(attestations) != 2 { t.Fatalf("Updated attestations should be length 2: %d", len(attestations)) } } func TestUpdateAttestationsAfterRecalc(t *testing.T) { - aState := NewActiveState(&pb.ActiveState{ - PendingAttestations: []*pb.AggregatedAttestation{ - { - Slot: 0, - Shard: 0, - }, - { - Slot: 0, - Shard: 1, - }, - }, - }) - + state, _ := NewGenesisBeaconState(nil) newAttestations := []*pb.AggregatedAttestation{ { Slot: 10, @@ -127,10 +116,10 @@ func TestUpdateAttestationsAfterRecalc(t *testing.T) { }, } - aState = aState.UpdateAttestations(newAttestations) - aState.clearAttestations(8) - if len(aState.PendingAttestations()) != 2 { - t.Fatalf("Updated attestations should be length 2: %d", len(aState.PendingAttestations())) + state.SetPendingAttestations(newAttestations) + state.ClearAttestations(8) + if len(state.PendingAttestations()) != 2 { + t.Fatalf("Updated attestations should be length 2: %d", len(state.PendingAttestations())) } } @@ -145,11 +134,11 @@ func TestUpdateRecentBlockHashes(t *testing.T) { recentBlockHashes = append(recentBlockHashes, []byte{0}) } - aState := NewActiveState(&pb.ActiveState{ + state := NewBeaconState(&pb.BeaconState{ RecentBlockHashes: recentBlockHashes, }) - updated, err := aState.calculateNewBlockHashes(block, 0) + updated, err := state.CalculateNewBlockHashes(block, 0) if err != nil { t.Fatalf("failed to update recent blockhashes: %v", err) } @@ -177,7 +166,7 @@ func TestCalculateNewBlockHashes_DoesNotMutateData(t *testing.T) { []byte("hash"), } - s := NewGenesisActiveState() + s, _ := NewGenesisBeaconState(nil) copy(s.data.RecentBlockHashes, interestingData) original := make([][]byte, 2*params.BeaconConfig().CycleLength) copy(original, s.data.RecentBlockHashes) @@ -193,7 +182,7 @@ func TestCalculateNewBlockHashes_DoesNotMutateData(t *testing.T) { }, } - result, _ := s.calculateNewBlockHashes(block, 0 /*parentSlot*/) + result, _ := s.CalculateNewBlockHashes(block, 0 /*parentSlot*/) if !reflect.DeepEqual(s.data.RecentBlockHashes, original) { t.Error("data has mutated from the original") @@ -216,72 +205,6 @@ func areBytesEqual(s1, s2 []byte) bool { return true } -func TestCalculateNewActiveState(t *testing.T) { - block := NewBlock(&pb.BeaconBlock{ - Slot: 10, - AncestorHashes: [][]byte{{}}, - }) - - cState, err := NewGenesisCrystallizedState(nil) - if err != nil { - t.Fatalf("failed to initialize genesis crystallized state: %v", err) - } - - recentBlockHashes := [][]byte{} - for i := 0; i < 2*int(params.BeaconConfig().CycleLength); i++ { - recentBlockHashes = append(recentBlockHashes, []byte{0}) - } - - aState := NewActiveState(&pb.ActiveState{ - PendingAttestations: []*pb.AggregatedAttestation{ - { - Slot: 0, - Shard: 0, - }, - { - Slot: 0, - Shard: 1, - }, - }, - RecentBlockHashes: recentBlockHashes, - }) - - aState, err = aState.CalculateNewActiveState(block, cState, 0) - if err != nil { - t.Fatalf("failed to calculate new active state: %v", err) - } - - if len(aState.PendingAttestations()) != 2 { - t.Fatalf("expected 2 pending attestations, got %d", len(aState.PendingAttestations())) - } - - if len(aState.RecentBlockHashes()) != 2*int(params.BeaconConfig().CycleLength) { - t.Fatalf("incorrect number of items in RecentBlockHashes: %d", len(aState.RecentBlockHashes())) - } - - aState, err = aState.CalculateNewActiveState(block, cState, 0) - if err != nil { - t.Fatalf("failed to calculate new active state: %v", err) - } - - if len(aState.PendingAttestations()) != 2 { - t.Fatalf("expected 2 pending attestations, got %d", len(aState.PendingAttestations())) - } - - if len(aState.RecentBlockHashes()) != 2*int(params.BeaconConfig().CycleLength) { - t.Fatalf("incorrect number of items in RecentBlockHashes: %d", len(aState.RecentBlockHashes())) - } -} - -func createHashFromByte(repeatedByte byte) []byte { - hash := make([]byte, 32) - for i := 0; i < 32; i++ { - hash[i] = repeatedByte - } - - return hash -} - func TestGetSignedParentHashes(t *testing.T) { // Test the scenario described in the spec: // https://github.com/ethereum/eth2.0-specs/blob/d7458bf201c8fcb93503272c8844381962488cb7/specs/beacon-chain.md#per-block-processing @@ -306,7 +229,7 @@ func TestGetSignedParentHashes(t *testing.T) { recentBlockHashes[9] = createHashFromByte('I') recentBlockHashes[10] = createHashFromByte('J') - aState := NewActiveState(&pb.ActiveState{RecentBlockHashes: recentBlockHashes}) + state := NewBeaconState(&pb.BeaconState{RecentBlockHashes: recentBlockHashes}) b := NewBlock(&pb.BeaconBlock{Slot: 11}) @@ -318,15 +241,15 @@ func TestGetSignedParentHashes(t *testing.T) { Slot: 5, } - hashes, err := aState.GetSignedParentHashes(b, a) + hashes, err := state.SignedParentHashes(b, a) if err != nil { - t.Fatalf("failed to GetSignedParentHashes: %v", err) + t.Fatalf("failed to SignedParentHashes: %v", err) } if hashes[0][0] != 'B' || hashes[1][0] != 'C' { - t.Fatalf("GetSignedParentHashes did not return expected value: %#x and %#x", hashes[0], hashes[1]) + t.Fatalf("SignedParentHashes did not return expected value: %#x and %#x", hashes[0], hashes[1]) } if hashes[2][0] != 0 || hashes[3][0] != 1 { - t.Fatalf("GetSignedParentHashes did not return expected value: %#x and %#x", hashes[0], hashes[1]) + t.Fatalf("SignedParentHashes did not return expected value: %#x and %#x", hashes[0], hashes[1]) } } @@ -349,7 +272,7 @@ func TestGetSignedParentHashesIndexFail(t *testing.T) { recentBlockHashes[6] = createHashFromByte('F') recentBlockHashes[7] = createHashFromByte('G') - aState := NewActiveState(&pb.ActiveState{RecentBlockHashes: recentBlockHashes}) + state := NewBeaconState(&pb.BeaconState{RecentBlockHashes: recentBlockHashes}) b := NewBlock(&pb.BeaconBlock{Slot: 8}) a := &pb.AggregatedAttestation{ @@ -357,17 +280,26 @@ func TestGetSignedParentHashesIndexFail(t *testing.T) { Slot: 2, } - _, err := aState.GetSignedParentHashes(b, a) + _, err := state.SignedParentHashes(b, a) if err == nil { - t.Error("expected GetSignedParentHashes to fail") + t.Error("expected SignedParentHashes to fail") } a2 := &pb.AggregatedAttestation{ ObliqueParentHashes: [][]byte{}, Slot: 9, } - _, err = aState.GetSignedParentHashes(b, a2) + _, err = state.SignedParentHashes(b, a2) if err == nil { - t.Error("expected GetSignedParentHashes to fail") + t.Error("expected SignedParentHashes to fail") } } + +func createHashFromByte(repeatedByte byte) []byte { + hash := make([]byte, 32) + for i := 0; i < 32; i++ { + hash[i] = repeatedByte + } + + return hash +} diff --git a/beacon-chain/core/validators/committees_test.go b/beacon-chain/core/validators/committees_test.go index b8543ea104..6332935ac3 100644 --- a/beacon-chain/core/validators/committees_test.go +++ b/beacon-chain/core/validators/committees_test.go @@ -9,7 +9,7 @@ import ( ) func TestGetShardAndCommitteesForSlots(t *testing.T) { - state := &pb.CrystallizedState{ + state := &pb.BeaconState{ LastStateRecalculationSlot: 64, ShardAndCommitteesForSlots: []*pb.ShardAndCommitteeArray{ {ArrayShardAndCommittee: []*pb.ShardAndCommittee{ diff --git a/beacon-chain/db/BUILD.bazel b/beacon-chain/db/BUILD.bazel index 0d1b232b50..45a170f9dd 100644 --- a/beacon-chain/db/BUILD.bazel +++ b/beacon-chain/db/BUILD.bazel @@ -15,7 +15,7 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/db", visibility = ["//beacon-chain:__subpackages__"], deps = [ - "//beacon-chain/types:go_default_library", + "//beacon-chain/core/types:go_default_library", "//beacon-chain/utils:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/bytes:go_default_library", @@ -37,7 +37,7 @@ go_test( ], embed = [":go_default_library"], deps = [ - "//beacon-chain/types:go_default_library", + "//beacon-chain/core/types:go_default_library", "//beacon-chain/utils:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/params:go_default_library", diff --git a/beacon-chain/db/attestation.go b/beacon-chain/db/attestation.go index 25448d5b80..3839f32d89 100644 --- a/beacon-chain/db/attestation.go +++ b/beacon-chain/db/attestation.go @@ -5,7 +5,7 @@ import ( "github.com/boltdb/bolt" "github.com/gogo/protobuf/proto" - "github.com/prysmaticlabs/prysm/beacon-chain/types" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ) diff --git a/beacon-chain/db/attestation_test.go b/beacon-chain/db/attestation_test.go index 0dbfb65002..dd9a70bb66 100644 --- a/beacon-chain/db/attestation_test.go +++ b/beacon-chain/db/attestation_test.go @@ -4,7 +4,7 @@ import ( "bytes" "testing" - "github.com/prysmaticlabs/prysm/beacon-chain/types" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ) diff --git a/beacon-chain/db/block.go b/beacon-chain/db/block.go index 9737f01d3c..97bab1f49d 100644 --- a/beacon-chain/db/block.go +++ b/beacon-chain/db/block.go @@ -5,10 +5,11 @@ import ( "fmt" "time" + pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/boltdb/bolt" "github.com/gogo/protobuf/proto" - "github.com/prysmaticlabs/prysm/beacon-chain/types" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" ) func createBlock(enc []byte) (*types.Block, error) { @@ -90,7 +91,7 @@ func (db *BeaconDB) GetChainHead() (*types.Block, error) { height := chainInfo.Get(mainChainHeightKey) if height == nil { - return errors.New("unable to determinechain height") + return errors.New("unable to determine chain height") } blockhash := mainChain.Get(height) @@ -114,23 +115,15 @@ func (db *BeaconDB) GetChainHead() (*types.Block, error) { // UpdateChainHead atomically updates the head of the chain as well as the corresponding state changes // Including a new crystallized state is optional. -func (db *BeaconDB) UpdateChainHead(block *types.Block, aState *types.ActiveState, cState *types.CrystallizedState) error { - blockhash, err := block.Hash() +func (db *BeaconDB) UpdateChainHead(block *types.Block, beaconState *types.BeaconState) error { + blockHash, err := block.Hash() if err != nil { return fmt.Errorf("unable to get the block hash: %v", err) } - aStateEnc, err := aState.Marshal() + beaconStateEnc, err := beaconState.Marshal() if err != nil { - return fmt.Errorf("unable to encode the active state: %v", err) - } - - var cStateEnc []byte - if cState != nil { - cStateEnc, err = cState.Marshal() - if err != nil { - return fmt.Errorf("unable to encode the crystallized state: %v", err) - } + return fmt.Errorf("unable to encode the beacon state: %v", err) } slotBinary := encodeSlotNumber(block.SlotNumber()) @@ -140,11 +133,11 @@ func (db *BeaconDB) UpdateChainHead(block *types.Block, aState *types.ActiveStat chainInfo := tx.Bucket(chainInfoBucket) mainChain := tx.Bucket(mainChainBucket) - if blockBucket.Get(blockhash[:]) == nil { - return fmt.Errorf("expected block %#x to have already been saved before updating head: %v", blockhash, err) + if blockBucket.Get(blockHash[:]) == nil { + return fmt.Errorf("expected block %#x to have already been saved before updating head: %v", blockHash, err) } - if err := mainChain.Put(slotBinary, blockhash[:]); err != nil { + if err := mainChain.Put(slotBinary, blockHash[:]); err != nil { return fmt.Errorf("failed to include the block in the main chain bucket: %v", err) } @@ -152,16 +145,9 @@ func (db *BeaconDB) UpdateChainHead(block *types.Block, aState *types.ActiveStat return fmt.Errorf("failed to record the block as the head of the main chain: %v", err) } - if err := chainInfo.Put(aStateLookupKey, aStateEnc); err != nil { - return fmt.Errorf("failed to save active state as canonical: %v", err) + if err := chainInfo.Put(stateLookupKey, beaconStateEnc); err != nil { + return fmt.Errorf("failed to save beacon state as canonical: %v", err) } - - if cStateEnc != nil { - if err := chainInfo.Put(cStateLookupKey, cStateEnc); err != nil { - return fmt.Errorf("failed to save crystallized state as canonical: %v", err) - } - } - return nil }) } diff --git a/beacon-chain/db/block_test.go b/beacon-chain/db/block_test.go index 83b4885300..a718650f4d 100644 --- a/beacon-chain/db/block_test.go +++ b/beacon-chain/db/block_test.go @@ -3,7 +3,7 @@ package db import ( "testing" - "github.com/prysmaticlabs/prysm/beacon-chain/types" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/params" ) @@ -92,13 +92,13 @@ func TestUpdateChainHeadNoBlock(t *testing.T) { if err != nil { t.Fatalf("failed to initialize state: %v", err) } - aState, err := db.GetActiveState() + beaconState, err := db.GetState() if err != nil { - t.Fatalf("failed to get active state: %v", err) + t.Fatalf("failed to get beacon state: %v", err) } b := types.NewBlock(&pb.BeaconBlock{Slot: 1}) - if err := db.UpdateChainHead(b, aState, nil); err == nil { + if err := db.UpdateChainHead(b, beaconState); err == nil { t.Fatalf("expected UpdateChainHead to fail if the block does not exist: %v", err) } } @@ -121,9 +121,9 @@ func TestUpdateChainHead(t *testing.T) { t.Fatalf("failed to get hash of b: %v", err) } - aState, err := db.GetActiveState() + beaconState, err := db.GetState() if err != nil { - t.Fatalf("failed to get active state: %v", err) + t.Fatalf("failed to get beacon state: %v", err) } b2 := types.NewBlock(&pb.BeaconBlock{ @@ -137,7 +137,7 @@ func TestUpdateChainHead(t *testing.T) { if err := db.SaveBlock(b2); err != nil { t.Fatalf("failed to save block: %v", err) } - if err := db.UpdateChainHead(b2, aState, nil); err != nil { + if err := db.UpdateChainHead(b2, beaconState); err != nil { t.Fatalf("failed to record the new head of the main chain: %v", err) } @@ -176,13 +176,9 @@ func TestChainProgress(t *testing.T) { t.Fatalf("failed to initialize state: %v", err) } - aState, err := db.GetActiveState() + beaconState, err := db.GetState() if err != nil { - t.Fatalf("Failed to get active state: %v", err) - } - cState, err := db.GetCrystallizedState() - if err != nil { - t.Fatalf("Failed to get crystallized state: %v", err) + t.Fatalf("Failed to get beacon state: %v", err) } cycleLength := params.BeaconConfig().CycleLength @@ -190,7 +186,7 @@ func TestChainProgress(t *testing.T) { if err := db.SaveBlock(b1); err != nil { t.Fatalf("failed to save block: %v", err) } - if err := db.UpdateChainHead(b1, aState, nil); err != nil { + if err := db.UpdateChainHead(b1, beaconState); err != nil { t.Fatalf("failed to record the new head: %v", err) } heighestBlock, err := db.GetChainHead() @@ -205,7 +201,7 @@ func TestChainProgress(t *testing.T) { if err := db.SaveBlock(b2); err != nil { t.Fatalf("failed to save block: %v", err) } - if err := db.UpdateChainHead(b2, aState, nil); err != nil { + if err := db.UpdateChainHead(b2, beaconState); err != nil { t.Fatalf("failed to record the new head: %v", err) } heighestBlock, err = db.GetChainHead() @@ -220,7 +216,7 @@ func TestChainProgress(t *testing.T) { if err := db.SaveBlock(b3); err != nil { t.Fatalf("failed to save block: %v", err) } - if err := db.UpdateChainHead(b3, aState, cState); err != nil { + if err := db.UpdateChainHead(b3, beaconState); err != nil { t.Fatalf("failed to update head: %v", err) } heighestBlock, err = db.GetChainHead() diff --git a/beacon-chain/db/schema.go b/beacon-chain/db/schema.go index 0fa850b57b..cc3e5018fd 100644 --- a/beacon-chain/db/schema.go +++ b/beacon-chain/db/schema.go @@ -23,8 +23,7 @@ var ( simulatorBucket = []byte("simulator-bucket") mainChainHeightKey = []byte("chain-height") - aStateLookupKey = []byte("active-state") - cStateLookupKey = []byte("crystallized-state") + stateLookupKey = []byte("state") simSlotLookupKey = []byte("simulator-slot") // DB internal use diff --git a/beacon-chain/db/state.go b/beacon-chain/db/state.go index c460029868..c3cbe0a560 100644 --- a/beacon-chain/db/state.go +++ b/beacon-chain/db/state.go @@ -5,34 +5,27 @@ import ( "github.com/boltdb/bolt" "github.com/gogo/protobuf/proto" - "github.com/prysmaticlabs/prysm/beacon-chain/types" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ) -// InitializeState ... +// InitializeState creates an initial genesis state for the beacon +// node using a set of genesis validators. func (db *BeaconDB) InitializeState(genesisValidators []*pb.ValidatorRecord) error { - aState := types.NewGenesisActiveState() - cState, err := types.NewGenesisCrystallizedState(genesisValidators) + beaconState, err := types.NewGenesisBeaconState(genesisValidators) if err != nil { return err } // #nosec G104 - activeStateHash, _ := aState.Hash() + stateHash, _ := beaconState.Hash() + genesisBlock := types.NewGenesisBlock(stateHash) // #nosec G104 - crystallizedStateHash, _ := cState.Hash() - - genesisBlock := types.NewGenesisBlock(activeStateHash, crystallizedStateHash) - // #nosec G104 - blockhash, _ := genesisBlock.Hash() - + blockHash, _ := genesisBlock.Hash() // #nosec G104 blockEnc, _ := genesisBlock.Marshal() // #nosec G104 - aStateEnc, _ := aState.Marshal() - // #nosec G104 - cStateEnc, _ := cState.Marshal() - + stateEnc, _ := beaconState.Marshal() zeroBinary := encodeSlotNumber(0) return db.update(func(tx *bolt.Tx) error { @@ -44,19 +37,15 @@ func (db *BeaconDB) InitializeState(genesisValidators []*pb.ValidatorRecord) err return fmt.Errorf("failed to record block height: %v", err) } - if err := mainChain.Put(zeroBinary, blockhash[:]); err != nil { + if err := mainChain.Put(zeroBinary, blockHash[:]); err != nil { return fmt.Errorf("failed to record block hash: %v", err) } - if err := blockBkt.Put(blockhash[:], blockEnc); err != nil { + if err := blockBkt.Put(blockHash[:], blockEnc); err != nil { return err } - if err := chainInfo.Put(aStateLookupKey, aStateEnc); err != nil { - return err - } - - if err := chainInfo.Put(cStateLookupKey, cStateEnc); err != nil { + if err := chainInfo.Put(stateLookupKey, stateEnc); err != nil { return err } @@ -64,149 +53,82 @@ func (db *BeaconDB) InitializeState(genesisValidators []*pb.ValidatorRecord) err }) } -// GetActiveState fetches the current active state. -func (db *BeaconDB) GetActiveState() (*types.ActiveState, error) { - var aState *types.ActiveState +// GetState fetches the canonical beacon chain's state from the DB. +func (db *BeaconDB) GetState() (*types.BeaconState, error) { + var beaconState *types.BeaconState err := db.view(func(tx *bolt.Tx) error { chainInfo := tx.Bucket(chainInfoBucket) - - enc := chainInfo.Get(aStateLookupKey) + enc := chainInfo.Get(stateLookupKey) if enc == nil { return nil } var err error - aState, err = createActiveState(enc) + beaconState, err = createState(enc) return err }) - return aState, err + return beaconState, err } -// GetCrystallizedState fetches the current active state. -func (db *BeaconDB) GetCrystallizedState() (*types.CrystallizedState, error) { - var cState *types.CrystallizedState - err := db.view(func(tx *bolt.Tx) error { - chainInfo := tx.Bucket(chainInfoBucket) - - enc := chainInfo.Get(cStateLookupKey) - if enc == nil { - return nil - } - - var err error - cState, err = createCrystallizedState(enc) - return err - }) - - return cState, err -} - -// SaveCrystallizedState updates the crystallized state for initial sync. -func (db *BeaconDB) SaveCrystallizedState(cState *types.CrystallizedState) error { - +// SaveState updates the beacon chain state. +func (db *BeaconDB) SaveState(beaconState *types.BeaconState) error { return db.update(func(tx *bolt.Tx) error { chainInfo := tx.Bucket(chainInfoBucket) - cStateEnc, err := cState.Marshal() + beaconStateEnc, err := beaconState.Marshal() if err != nil { return err } - return chainInfo.Put(cStateLookupKey, cStateEnc) + return chainInfo.Put(stateLookupKey, beaconStateEnc) }) } // GetUnfinalizedBlockState fetches an unfinalized block's // active and crystallized state pair. -func (db *BeaconDB) GetUnfinalizedBlockState( - aStateRoot [32]byte, - cStateRoot [32]byte, -) (*types.ActiveState, *types.CrystallizedState, error) { - var aState *types.ActiveState - var cState *types.CrystallizedState +func (db *BeaconDB) GetUnfinalizedBlockState(stateRoot [32]byte) (*types.BeaconState, error) { + var beaconState *types.BeaconState err := db.view(func(tx *bolt.Tx) error { chainInfo := tx.Bucket(chainInfoBucket) - - encActive := chainInfo.Get(aStateRoot[:]) - if encActive == nil { - return nil - } - encCrystallized := chainInfo.Get(cStateRoot[:]) - if encCrystallized == nil { + encState := chainInfo.Get(stateRoot[:]) + if encState == nil { return nil } var err error - aState, err = createActiveState(encActive) - if err != nil { - return err - } - cState, err = createCrystallizedState(encCrystallized) + beaconState, err = createState(encState) return err }) - - return aState, cState, err + return beaconState, err } -// SaveUnfinalizedBlockState persists the associated crystallized and -// active state pair for a given unfinalized block. -func (db *BeaconDB) SaveUnfinalizedBlockState(aState *types.ActiveState, cState *types.CrystallizedState) error { - aStateHash, err := aState.Hash() +// SaveUnfinalizedBlockState persists the associated state +// for a given unfinalized block. +func (db *BeaconDB) SaveUnfinalizedBlockState(beaconState *types.BeaconState) error { + stateHash, err := beaconState.Hash() if err != nil { - return fmt.Errorf("unable to hash the active state: %v", err) + return fmt.Errorf("unable to hash the beacon state: %v", err) } - aStateEnc, err := aState.Marshal() + beaconStateEnc, err := beaconState.Marshal() if err != nil { - return fmt.Errorf("unable to encode the active state: %v", err) - } - - var cStateEnc []byte - var cStateHash [32]byte - if cState != nil { - cStateHash, err = cState.Hash() - if err != nil { - return fmt.Errorf("unable to hash the crystallized state: %v", err) - } - cStateEnc, err = cState.Marshal() - if err != nil { - return fmt.Errorf("unable to encode the crystallized state: %v", err) - } + return fmt.Errorf("unable to encode the beacon state: %v", err) } return db.update(func(tx *bolt.Tx) error { chainInfo := tx.Bucket(chainInfoBucket) - if err := chainInfo.Put(aStateHash[:], aStateEnc); err != nil { - return fmt.Errorf("failed to save active state as canonical: %v", err) - } - - if cStateEnc != nil { - if err := chainInfo.Put(cStateHash[:], cStateEnc); err != nil { - return fmt.Errorf("failed to save crystallized state as canonical: %v", err) - } + if err := chainInfo.Put(stateHash[:], beaconStateEnc); err != nil { + return fmt.Errorf("failed to save beacon state: %v", err) } return nil }) } -func createActiveState(enc []byte) (*types.ActiveState, error) { - protoState := &pb.ActiveState{} +func createState(enc []byte) (*types.BeaconState, error) { + protoState := &pb.BeaconState{} err := proto.Unmarshal(enc, protoState) if err != nil { return nil, fmt.Errorf("failed to unmarshal encoding: %v", err) } - state := types.NewActiveState(protoState) - - return state, nil -} - -func createCrystallizedState(enc []byte) (*types.CrystallizedState, error) { - protoState := &pb.CrystallizedState{} - err := proto.Unmarshal(enc, protoState) - if err != nil { - return nil, fmt.Errorf("failed to unmarshal encoding: %v", err) - } - - state := types.NewCrystallizedState(protoState) - + state := types.NewBeaconState(protoState) return state, nil } diff --git a/beacon-chain/db/state_test.go b/beacon-chain/db/state_test.go index 9bb1239172..8d7eb00582 100644 --- a/beacon-chain/db/state_test.go +++ b/beacon-chain/db/state_test.go @@ -2,11 +2,7 @@ package db import ( "bytes" - "reflect" "testing" - - "github.com/prysmaticlabs/prysm/beacon-chain/types" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ) func TestInitializeState(t *testing.T) { @@ -24,78 +20,28 @@ func TestInitializeState(t *testing.T) { t.Fatalf("Expected block height to equal 1. Got %d", b.SlotNumber()) } - aState, err := db.GetActiveState() + beaconState, err := db.GetState() if err != nil { - t.Fatalf("Failed to get active state: %v", err) + t.Fatalf("Failed to get state: %v", err) } - cState, err := db.GetCrystallizedState() + if beaconState == nil { + t.Fatalf("Failed to retrieve state: %v", beaconState) + } + beaconStateEnc, err := beaconState.Marshal() if err != nil { - t.Fatalf("Failed to get crystallized state: %v", err) - } - if aState == nil || cState == nil { - t.Fatalf("Failed to retrieve state: %v, %v", aState, cState) - } - aStateEnc, err := aState.Marshal() - if err != nil { - t.Fatalf("Failed to encode active state: %v", err) - } - cStateEnc, err := cState.Marshal() - if err != nil { - t.Fatalf("Failed t oencode crystallized state: %v", err) + t.Fatalf("Failed to encode state: %v", err) } - aStatePrime, err := db.GetActiveState() + statePrime, err := db.GetState() if err != nil { - t.Fatalf("Failed to get active state: %v", err) + t.Fatalf("Failed to get state: %v", err) } - aStatePrimeEnc, err := aStatePrime.Marshal() + statePrimeEnc, err := statePrime.Marshal() if err != nil { - t.Fatalf("Failed to encode active state: %v", err) + t.Fatalf("Failed to encode state: %v", err) } - cStatePrime, err := db.GetCrystallizedState() - if err != nil { - t.Fatalf("Failed to get crystallized state: %v", err) - } - cStatePrimeEnc, err := cStatePrime.Marshal() - if err != nil { - t.Fatalf("Failed to encode crystallized state: %v", err) - } - - if !bytes.Equal(aStateEnc, aStatePrimeEnc) { - t.Fatalf("Expected %#x and %#x to be equal", aStateEnc, aStatePrimeEnc) - } - if !bytes.Equal(cStateEnc, cStatePrimeEnc) { - t.Fatalf("Expected %#x and %#x to be equal", cStateEnc, cStatePrimeEnc) - } -} - -func TestGetUnfinalizedBlockState(t *testing.T) { - db := setupDB(t) - defer teardownDB(t, db) - aState := types.NewActiveState(&pb.ActiveState{}) - cState := types.NewCrystallizedState(&pb.CrystallizedState{}) - if err := db.SaveUnfinalizedBlockState(aState, cState); err != nil { - t.Fatalf("Could not save unfinalized block state: %v", err) - } - - aStateHash, err := aState.Hash() - if err != nil { - t.Fatal(err) - } - cStateHash, err := cState.Hash() - if err != nil { - t.Fatal(err) - } - got1, got2, err := db.GetUnfinalizedBlockState(aStateHash, cStateHash) - if err != nil { - t.Errorf("Unexpected error: wanted nil, received %v", err) - return - } - if !reflect.DeepEqual(got1, aState) { - t.Errorf("ActiveState not equal: got = %v, want %v", got1, aState) - } - if !reflect.DeepEqual(got2, cState) { - t.Errorf("CrystallizedState not equal: got = %v, want %v", got2, cState) + if !bytes.Equal(beaconStateEnc, statePrimeEnc) { + t.Fatalf("Expected %#x and %#x to be equal", beaconStateEnc, statePrimeEnc) } } diff --git a/beacon-chain/dbcleanup/BUILD.bazel b/beacon-chain/dbcleanup/BUILD.bazel index 048bf272a4..3f62e10ed6 100644 --- a/beacon-chain/dbcleanup/BUILD.bazel +++ b/beacon-chain/dbcleanup/BUILD.bazel @@ -6,8 +6,8 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/dbcleanup", visibility = ["//beacon-chain:__subpackages__"], deps = [ + "//beacon-chain/core/types:go_default_library", "//beacon-chain/db:go_default_library", - "//beacon-chain/types:go_default_library", "//shared/event:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", ], @@ -18,9 +18,9 @@ go_test( srcs = ["service_test.go"], embed = [":go_default_library"], deps = [ + "//beacon-chain/core/types:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/internal:go_default_library", - "//beacon-chain/types:go_default_library", "//beacon-chain/utils:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/event:go_default_library", diff --git a/beacon-chain/dbcleanup/service.go b/beacon-chain/dbcleanup/service.go index 1a8e935b0b..8208c96143 100644 --- a/beacon-chain/dbcleanup/service.go +++ b/beacon-chain/dbcleanup/service.go @@ -5,7 +5,7 @@ import ( "context" "fmt" - "github.com/prysmaticlabs/prysm/beacon-chain/types" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" "github.com/prysmaticlabs/prysm/shared/event" "github.com/prysmaticlabs/prysm/beacon-chain/db" @@ -15,7 +15,7 @@ import ( var log = logrus.WithField("prefix", "dbcleaner") type chainService interface { - CanonicalCrystallizedStateFeed() *event.Feed + CanonicalStateFeed() *event.Feed } // CleanupService represents a service that handles routine task for cleaning up @@ -27,7 +27,7 @@ type CleanupService struct { cancel context.CancelFunc beaconDB *db.BeaconDB chainService chainService - canonicalStateChan chan *types.CrystallizedState + canonicalStateChan chan *types.BeaconState } // Config defines the needed fields for creating a new cleanup service. @@ -45,7 +45,7 @@ func NewCleanupService(ctx context.Context, cfg *Config) *CleanupService { cancel: cancel, beaconDB: cfg.BeaconDB, chainService: cfg.ChainService, - canonicalStateChan: make(chan *types.CrystallizedState, cfg.SubscriptionBuf), + canonicalStateChan: make(chan *types.BeaconState, cfg.SubscriptionBuf), } } @@ -64,7 +64,7 @@ func (d *CleanupService) Stop() error { } func (d *CleanupService) cleanDB() { - cStateSub := d.chainService.CanonicalCrystallizedStateFeed().Subscribe(d.canonicalStateChan) + cStateSub := d.chainService.CanonicalStateFeed().Subscribe(d.canonicalStateChan) defer cStateSub.Unsubscribe() for { diff --git a/beacon-chain/dbcleanup/service_test.go b/beacon-chain/dbcleanup/service_test.go index 76bed4a9bd..a7f320443a 100644 --- a/beacon-chain/dbcleanup/service_test.go +++ b/beacon-chain/dbcleanup/service_test.go @@ -4,7 +4,7 @@ import ( "context" "testing" - "github.com/prysmaticlabs/prysm/beacon-chain/types" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" "github.com/prysmaticlabs/prysm/beacon-chain/utils" "github.com/prysmaticlabs/prysm/beacon-chain/db" @@ -21,7 +21,7 @@ type mockChainService struct { stateFeed *event.Feed } -func (m *mockChainService) CanonicalCrystallizedStateFeed() *event.Feed { +func (m *mockChainService) CanonicalStateFeed() *event.Feed { return m.stateFeed } @@ -74,11 +74,13 @@ func TestCleanBlockVoteCache(t *testing.T) { if err = beaconDB.SaveBlock(oldBlock); err != nil { t.Fatalf("failed to write block int DB: %v", err) } - oldAState := types.NewActiveState(&pb.ActiveState{}) - oldCState := types.NewCrystallizedState(&pb.CrystallizedState{}) - if err = beaconDB.UpdateChainHead(oldBlock, oldAState, oldCState); err != nil { + oldState := types.NewBeaconState(&pb.BeaconState{}) + if err = beaconDB.SaveState(oldState); err != nil { t.Fatalf("failed to pre-fill DB: %v", err) } + if err := beaconDB.UpdateChainHead(oldBlock, oldState); err != nil { + t.Fatalf("failed to update chain head: %v", err) + } oldBlockVoteCache := utils.NewBlockVoteCache() oldBlockVoteCache[oldBlockHash] = utils.NewBlockVote() if err = beaconDB.WriteBlockVoteCache(oldBlockVoteCache); err != nil { @@ -97,8 +99,8 @@ func TestCleanBlockVoteCache(t *testing.T) { // Now let the cleanup service do its job cleanupService := createCleanupService(beaconDB) - cState := types.NewCrystallizedState(&pb.CrystallizedState{LastFinalizedSlot: 1}) - if err = cleanupService.cleanBlockVoteCache(cState.LastFinalizedSlot()); err != nil { + state := types.NewBeaconState(&pb.BeaconState{LastFinalizedSlot: 1}) + if err = cleanupService.cleanBlockVoteCache(state.LastFinalizedSlot()); err != nil { t.Fatalf("failed to clean block vote cache") } diff --git a/beacon-chain/node/node.go b/beacon-chain/node/node.go index 7668044fcd..f44c6a7c7e 100644 --- a/beacon-chain/node/node.go +++ b/beacon-chain/node/node.go @@ -178,12 +178,12 @@ func (b *BeaconNode) startDB(ctx *cli.Context) error { log.Info("checking db") - cState, err := db.GetCrystallizedState() + beaconState, err := db.GetState() if err != nil { return err } // Ensure that state has been initialized. - if cState == nil { + if beaconState == nil { var genesisValidators []*pb.ValidatorRecord if genesisJSON != "" { log.Infof("Initializing Crystallized State from %s", genesisJSON) diff --git a/beacon-chain/node/p2p_config.go b/beacon-chain/node/p2p_config.go index e806619184..cd8a6aabe2 100644 --- a/beacon-chain/node/p2p_config.go +++ b/beacon-chain/node/p2p_config.go @@ -17,12 +17,9 @@ var topicMappings = map[pb.Topic]proto.Message{ pb.Topic_BEACON_BLOCK_RESPONSE: &pb.BeaconBlockResponse{}, pb.Topic_CHAIN_HEAD_REQUEST: &pb.ChainHeadRequest{}, pb.Topic_CHAIN_HEAD_RESPONSE: &pb.ChainHeadResponse{}, - pb.Topic_CRYSTALLIZED_STATE_HASH_ANNOUNCE: &pb.CrystallizedStateHashAnnounce{}, - pb.Topic_CRYSTALLIZED_STATE_REQUEST: &pb.CrystallizedStateRequest{}, - pb.Topic_CRYSTALLIZED_STATE_RESPONSE: &pb.CrystallizedStateResponse{}, - pb.Topic_ACTIVE_STATE_HASH_ANNOUNCE: &pb.ActiveStateHashAnnounce{}, - pb.Topic_ACTIVE_STATE_REQUEST: &pb.ActiveStateRequest{}, - pb.Topic_ACTIVE_STATE_RESPONSE: &pb.ActiveStateResponse{}, + pb.Topic_BEACON_STATE_HASH_ANNOUNCE: &pb.BeaconStateHashAnnounce{}, + pb.Topic_BEACON_STATE_REQUEST: &pb.BeaconStateRequest{}, + pb.Topic_BEACON_STATE_RESPONSE: &pb.BeaconStateResponse{}, } func configureP2P(ctx *cli.Context) (*p2p.Server, error) { diff --git a/beacon-chain/rpc/BUILD.bazel b/beacon-chain/rpc/BUILD.bazel index 218af070dd..30948d7cd1 100644 --- a/beacon-chain/rpc/BUILD.bazel +++ b/beacon-chain/rpc/BUILD.bazel @@ -6,9 +6,9 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/rpc", visibility = ["//beacon-chain:__subpackages__"], deps = [ + "//beacon-chain/core/types:go_default_library", "//beacon-chain/core/validators:go_default_library", "//beacon-chain/db:go_default_library", - "//beacon-chain/types:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//proto/beacon/rpc/v1:go_default_library", "//shared/event:go_default_library", @@ -26,8 +26,8 @@ go_test( srcs = ["service_test.go"], embed = [":go_default_library"], deps = [ + "//beacon-chain/core/types:go_default_library", "//beacon-chain/internal:go_default_library", - "//beacon-chain/types:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//proto/beacon/rpc/v1:go_default_library", "//shared/event:go_default_library", diff --git a/beacon-chain/rpc/service.go b/beacon-chain/rpc/service.go index b333d17e13..eaabc2134a 100644 --- a/beacon-chain/rpc/service.go +++ b/beacon-chain/rpc/service.go @@ -11,9 +11,9 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/golang/protobuf/ptypes/empty" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" "github.com/prysmaticlabs/prysm/beacon-chain/db" - "github.com/prysmaticlabs/prysm/beacon-chain/types" pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1" "github.com/prysmaticlabs/prysm/shared/event" @@ -31,7 +31,7 @@ type chainService interface { // but instead streamed to connected validators every // time the canonical head changes in the chain service. CanonicalBlockFeed() *event.Feed - CanonicalCrystallizedStateFeed() *event.Feed + CanonicalStateFeed() *event.Feed } type attestationService interface { @@ -56,7 +56,7 @@ type Service struct { withKey string grpcServer *grpc.Server canonicalBlockChan chan *types.Block - canonicalStateChan chan *types.CrystallizedState + canonicalStateChan chan *types.BeaconState incomingAttestation chan *types.Attestation enablePOWChain bool slotAlignmentDuration time.Duration @@ -91,7 +91,7 @@ func NewRPCService(ctx context.Context, cfg *Config) *Service { withKey: cfg.KeyFlag, slotAlignmentDuration: time.Duration(params.BeaconConfig().SlotDuration) * time.Second, canonicalBlockChan: make(chan *types.Block, cfg.SubscriptionBuf), - canonicalStateChan: make(chan *types.CrystallizedState, cfg.SubscriptionBuf), + canonicalStateChan: make(chan *types.BeaconState, cfg.SubscriptionBuf), incomingAttestation: make(chan *types.Attestation, cfg.SubscriptionBuf), enablePOWChain: cfg.EnablePOWChain, } @@ -170,13 +170,13 @@ func (s *Service) CurrentAssignmentsAndGenesisTime( if err != nil { return nil, fmt.Errorf("could not get genesis block: %v", err) } - cState, err := s.beaconDB.GetCrystallizedState() + beaconState, err := s.beaconDB.GetState() if err != nil { - return nil, fmt.Errorf("could not get crystallized state: %v", err) + return nil, fmt.Errorf("could not get beacon state: %v", err) } var keys []*pb.PublicKey if req.AllValidators { - for _, val := range cState.Validators() { + for _, val := range beaconState.Validators() { keys = append(keys, &pb.PublicKey{PublicKey: val.GetPubkey()}) } } else { @@ -185,7 +185,7 @@ func (s *Service) CurrentAssignmentsAndGenesisTime( return nil, errors.New("no public keys specified in request") } } - assignments, err := assignmentsForPublicKeys(keys, cState) + assignments, err := assignmentsForPublicKeys(keys, beaconState) if err != nil { return nil, fmt.Errorf("could not get assignments for public keys: %v", err) } @@ -207,17 +207,16 @@ func (s *Service) ProposeBlock(ctx context.Context, req *pb.ProposeRequest) (*pb } //TODO(#589) The attestation should be aggregated in the validator client side not in the beacon node. - cState, err := s.beaconDB.GetCrystallizedState() + beaconState, err := s.beaconDB.GetState() if err != nil { - return nil, fmt.Errorf("could not get crystallized state: %v", err) + return nil, fmt.Errorf("could not get beacon state: %v", err) } _, prevProposerIndex, err := v.ProposerShardAndIndex( - cState.ShardAndCommitteesForSlots(), - cState.LastStateRecalculationSlot(), + beaconState.ShardAndCommitteesForSlots(), + beaconState.LastStateRecalculationSlot(), req.GetSlotNumber(), ) - if err != nil { return nil, fmt.Errorf("could not get index of previous proposer: %v", err) } @@ -285,15 +284,15 @@ func (s *Service) LatestAttestation(req *empty.Empty, stream pb.BeaconService_La // ValidatorShardID is called by a validator to get the shard ID of where it's suppose // to proposer or attest. func (s *Service) ValidatorShardID(ctx context.Context, req *pb.PublicKey) (*pb.ShardIDResponse, error) { - cState, err := s.beaconDB.GetCrystallizedState() + beaconState, err := s.beaconDB.GetState() if err != nil { - return nil, fmt.Errorf("could not get crystallized state: %v", err) + return nil, fmt.Errorf("could not get beacon state: %v", err) } shardID, err := v.ValidatorShardID( req.PublicKey, - cState.Validators(), - cState.ShardAndCommitteesForSlots(), + beaconState.Validators(), + beaconState.ShardAndCommitteesForSlots(), ) if err != nil { return nil, fmt.Errorf("could not get validator shard ID: %v", err) @@ -308,15 +307,15 @@ func (s *Service) ValidatorSlotAndResponsibility( ctx context.Context, req *pb.PublicKey, ) (*pb.SlotResponsibilityResponse, error) { - cState, err := s.beaconDB.GetCrystallizedState() + beaconState, err := s.beaconDB.GetState() if err != nil { - return nil, fmt.Errorf("could not get crystallized state: %v", err) + return nil, fmt.Errorf("could not get beacon state: %v", err) } slot, role, err := v.ValidatorSlotAndRole( req.PublicKey, - cState.Validators(), - cState.ShardAndCommitteesForSlots(), + beaconState.Validators(), + beaconState.ShardAndCommitteesForSlots(), ) if err != nil { return nil, fmt.Errorf("could not get assigned validator slot for attester/proposer: %v", err) @@ -328,14 +327,13 @@ func (s *Service) ValidatorSlotAndResponsibility( // ValidatorIndex is called by a validator to get its index location that corresponds // to the attestation bit fields. func (s *Service) ValidatorIndex(ctx context.Context, req *pb.PublicKey) (*pb.IndexResponse, error) { - cState, err := s.beaconDB.GetCrystallizedState() + beaconState, err := s.beaconDB.GetState() if err != nil { - return nil, fmt.Errorf("could not get crystallized state: %v", err) + return nil, fmt.Errorf("could not get beacon state: %v", err) } - index, err := v.ValidatorIndex( req.PublicKey, - cState.Validators(), + beaconState.Validators(), ) if err != nil { return nil, fmt.Errorf("could not get validator index: %v", err) @@ -351,16 +349,16 @@ func (s *Service) ValidatorAssignments( req *pb.ValidatorAssignmentRequest, stream pb.BeaconService_ValidatorAssignmentsServer) error { - sub := s.chainService.CanonicalCrystallizedStateFeed().Subscribe(s.canonicalStateChan) + sub := s.chainService.CanonicalStateFeed().Subscribe(s.canonicalStateChan) defer sub.Unsubscribe() for { select { - case cState := <-s.canonicalStateChan: + case beaconState := <-s.canonicalStateChan: log.Info("Sending new cycle assignments to validator clients") var keys []*pb.PublicKey if req.AllValidators { - for _, val := range cState.Validators() { + for _, val := range beaconState.Validators() { keys = append(keys, &pb.PublicKey{PublicKey: val.GetPubkey()}) } } else { @@ -370,7 +368,7 @@ func (s *Service) ValidatorAssignments( } } - assignments, err := assignmentsForPublicKeys(keys, cState) + assignments, err := assignmentsForPublicKeys(keys, beaconState) if err != nil { return fmt.Errorf("could not get assignments for public keys: %v", err) } @@ -396,7 +394,7 @@ func (s *Service) ValidatorAssignments( // assignmentsForPublicKeys fetches the validator assignments for a subset of public keys // given a crystallized state. -func assignmentsForPublicKeys(keys []*pb.PublicKey, cState *types.CrystallizedState) ([]*pb.Assignment, error) { +func assignmentsForPublicKeys(keys []*pb.PublicKey, beaconState *types.BeaconState) ([]*pb.Assignment, error) { // Next, for each public key in the request, we build // up an array of assignments. assignments := []*pb.Assignment{} @@ -406,8 +404,8 @@ func assignmentsForPublicKeys(keys []*pb.PublicKey, cState *types.CrystallizedSt // should act as a proposer or attester. assignedSlot, role, err := v.ValidatorSlotAndRole( val.GetPublicKey(), - cState.Validators(), - cState.ShardAndCommitteesForSlots(), + beaconState.Validators(), + beaconState.ShardAndCommitteesForSlots(), ) if err != nil { return nil, err @@ -417,8 +415,8 @@ func assignmentsForPublicKeys(keys []*pb.PublicKey, cState *types.CrystallizedSt // based on a public key and current crystallized state. shardID, err := v.ValidatorShardID( val.GetPublicKey(), - cState.Validators(), - cState.ShardAndCommitteesForSlots(), + beaconState.Validators(), + beaconState.ShardAndCommitteesForSlots(), ) if err != nil { return nil, err diff --git a/beacon-chain/rpc/service_test.go b/beacon-chain/rpc/service_test.go index 485954aaf9..aea193f681 100644 --- a/beacon-chain/rpc/service_test.go +++ b/beacon-chain/rpc/service_test.go @@ -11,8 +11,8 @@ import ( "github.com/golang/mock/gomock" "github.com/golang/protobuf/ptypes" "github.com/golang/protobuf/ptypes/empty" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" "github.com/prysmaticlabs/prysm/beacon-chain/internal" - "github.com/prysmaticlabs/prysm/beacon-chain/types" pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1" "github.com/prysmaticlabs/prysm/shared/event" @@ -52,7 +52,7 @@ func (m *mockChainService) CanonicalBlockFeed() *event.Feed { return m.blockFeed } -func (m *mockChainService) CanonicalCrystallizedStateFeed() *event.Feed { +func (m *mockChainService) CanonicalStateFeed() *event.Feed { return m.stateFeed } @@ -117,18 +117,17 @@ func TestCurrentAssignmentsAndGenesisTime(t *testing.T) { defer internal.TeardownDB(t, db) mockChain := &mockChainService{} - genesis := types.NewGenesisBlock([32]byte{}, [32]byte{}) + genesis := types.NewGenesisBlock([32]byte{}) if err := db.SaveBlock(genesis); err != nil { t.Fatalf("Could not save genesis block: %v", err) } - aState := types.NewGenesisActiveState() - cState, err := types.NewGenesisCrystallizedState(nil) + beaconState, err := types.NewGenesisBeaconState(nil) if err != nil { - t.Fatalf("Could not instantiate initial crystallized state: %v", err) + t.Fatalf("Could not instantiate initial state: %v", err) } - if err := db.UpdateChainHead(genesis, aState, cState); err != nil { + if err := db.UpdateChainHead(genesis, beaconState); err != nil { t.Fatalf("Could not save genesis state: %v", err) } @@ -149,7 +148,7 @@ func TestCurrentAssignmentsAndGenesisTime(t *testing.T) { if err != nil { t.Errorf("Could not call CurrentAssignments correctly: %v", err) } - genesis = types.NewGenesisBlock([32]byte{}, [32]byte{}) + genesis = types.NewGenesisBlock([32]byte{}) if res.GenesisTimestamp.String() != genesis.Proto().GetTimestamp().String() { t.Errorf( "Received different genesis timestamp, wanted: %v, received: %v", @@ -164,18 +163,17 @@ func TestProposeBlock(t *testing.T) { defer internal.TeardownDB(t, db) mockChain := &mockChainService{} - genesis := types.NewGenesisBlock([32]byte{}, [32]byte{}) + genesis := types.NewGenesisBlock([32]byte{}) if err := db.SaveBlock(genesis); err != nil { t.Fatalf("Could not save genesis block: %v", err) } - aState := types.NewGenesisActiveState() - cState, err := types.NewGenesisCrystallizedState(nil) + beaconState, err := types.NewGenesisBeaconState(nil) if err != nil { - t.Fatalf("Could not instantiate initial crystallized state: %v", err) + t.Fatalf("Could not instantiate initial state: %v", err) } - if err := db.UpdateChainHead(genesis, aState, cState); err != nil { + if err := db.UpdateChainHead(genesis, beaconState); err != nil { t.Fatalf("Could not save genesis state: %v", err) } @@ -300,18 +298,17 @@ func TestValidatorSlotAndResponsibility(t *testing.T) { defer internal.TeardownDB(t, db) mockChain := &mockChainService{} - genesis := types.NewGenesisBlock([32]byte{}, [32]byte{}) + genesis := types.NewGenesisBlock([32]byte{}) if err := db.SaveBlock(genesis); err != nil { t.Fatalf("Could not save genesis block: %v", err) } - aState := types.NewGenesisActiveState() - cState, err := types.NewGenesisCrystallizedState(nil) + beaconState, err := types.NewGenesisBeaconState(nil) if err != nil { - t.Fatalf("Could not instantiate initial crystallized state: %v", err) + t.Fatalf("Could not instantiate initial state: %v", err) } - if err := db.UpdateChainHead(genesis, aState, cState); err != nil { + if err := db.UpdateChainHead(genesis, beaconState); err != nil { t.Fatalf("Could not save genesis state: %v", err) } @@ -333,18 +330,17 @@ func TestValidatorIndex(t *testing.T) { defer internal.TeardownDB(t, db) mockChain := &mockChainService{} - genesis := types.NewGenesisBlock([32]byte{}, [32]byte{}) + genesis := types.NewGenesisBlock([32]byte{}) if err := db.SaveBlock(genesis); err != nil { t.Fatalf("Could not save genesis block: %v", err) } - aState := types.NewGenesisActiveState() - cState, err := types.NewGenesisCrystallizedState(nil) + beaconState, err := types.NewGenesisBeaconState(nil) if err != nil { - t.Fatalf("Could not instantiate initial crystallized state: %v", err) + t.Fatalf("Could not instantiate initial state: %v", err) } - if err := db.UpdateChainHead(genesis, aState, cState); err != nil { + if err := db.UpdateChainHead(genesis, beaconState); err != nil { t.Fatalf("Could not save genesis state: %v", err) } @@ -366,19 +362,18 @@ func TestValidatorShardID(t *testing.T) { defer internal.TeardownDB(t, db) mockChain := &mockChainService{} - genesis := types.NewGenesisBlock([32]byte{}, [32]byte{}) + genesis := types.NewGenesisBlock([32]byte{}) if err := db.SaveBlock(genesis); err != nil { t.Fatalf("Could not save genesis block: %v", err) } - astate := types.NewGenesisActiveState() - cstate, err := types.NewGenesisCrystallizedState(nil) + beaconState, err := types.NewGenesisBeaconState(nil) if err != nil { - t.Fatalf("could not instantiate initial crystallized state: %v", err) + t.Fatalf("Could not instantiate initial state: %v", err) } - if err := db.UpdateChainHead(genesis, astate, cstate); err != nil { - t.Fatalf("could not save genesis state: %v", err) + if err := db.UpdateChainHead(genesis, beaconState); err != nil { + t.Fatalf("Could not save genesis state: %v", err) } rpcService := NewRPCService(context.Background(), &Config{ @@ -400,19 +395,18 @@ func TestValidatorAssignments(t *testing.T) { defer internal.TeardownDB(t, db) mockChain := newMockChainService() - genesis := types.NewGenesisBlock([32]byte{}, [32]byte{}) + genesis := types.NewGenesisBlock([32]byte{}) if err := db.SaveBlock(genesis); err != nil { t.Fatalf("Could not save genesis block: %v", err) } - astate := types.NewGenesisActiveState() - cstate, err := types.NewGenesisCrystallizedState(nil) + beaconState, err := types.NewGenesisBeaconState(nil) if err != nil { - t.Fatalf("could not instantiate initial crystallized state: %v", err) + t.Fatalf("Could not instantiate initial state: %v", err) } - if err := db.UpdateChainHead(genesis, astate, cstate); err != nil { - t.Fatalf("could not save genesis state: %v", err) + if err := db.UpdateChainHead(genesis, beaconState); err != nil { + t.Fatalf("Could not save genesis state: %v", err) } rpcService := NewRPCService(context.Background(), &Config{ @@ -443,12 +437,12 @@ func TestValidatorAssignments(t *testing.T) { <-exitRoutine }(t) - genesisState, err := types.NewGenesisCrystallizedState(nil) + beaconState, err = types.NewGenesisBeaconState(nil) if err != nil { - t.Fatal(err) + t.Fatalf("Could not instantiate initial state: %v", err) } - rpcService.canonicalStateChan <- genesisState + rpcService.canonicalStateChan <- beaconState rpcService.cancel() exitRoutine <- true testutil.AssertLogsContain(t, hook, "Sending new cycle assignments to validator clients") diff --git a/beacon-chain/simulator/BUILD.bazel b/beacon-chain/simulator/BUILD.bazel index dc00f69c27..3ff4b2e98a 100644 --- a/beacon-chain/simulator/BUILD.bazel +++ b/beacon-chain/simulator/BUILD.bazel @@ -6,7 +6,9 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/simulator", visibility = ["//beacon-chain:__subpackages__"], deps = [ - "//beacon-chain/types:go_default_library", + "//beacon-chain/core/types:go_default_library", + "//beacon-chain/core/validators:go_default_library", + "//beacon-chain/db:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/bitutil:go_default_library", "//shared/event:go_default_library", diff --git a/beacon-chain/simulator/service.go b/beacon-chain/simulator/service.go index f8424e999a..aef8317d3d 100644 --- a/beacon-chain/simulator/service.go +++ b/beacon-chain/simulator/service.go @@ -5,12 +5,13 @@ import ( "bytes" "context" "fmt" - "time" "github.com/ethereum/go-ethereum/common" "github.com/golang/protobuf/proto" "github.com/golang/protobuf/ptypes" - "github.com/prysmaticlabs/prysm/beacon-chain/types" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" + v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" + "github.com/prysmaticlabs/prysm/beacon-chain/db" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bitutil" "github.com/prysmaticlabs/prysm/shared/event" @@ -38,11 +39,11 @@ type Simulator struct { cancel context.CancelFunc p2p p2pAPI web3Service powChainService - beaconDB beaconDB + beaconDB *db.BeaconDB enablePOWChain bool blockRequestChan chan p2p.Message blockBySlotChan chan p2p.Message - cStateReqChan chan p2p.Message + stateReqChan chan p2p.Message chainHeadRequestChan chan p2p.Message } @@ -51,29 +52,19 @@ type Config struct { BlockRequestBuf int BlockSlotBuf int ChainHeadRequestBuf int - CStateReqBuf int + StateReqBuf int P2P p2pAPI Web3Service powChainService - BeaconDB beaconDB + BeaconDB *db.BeaconDB EnablePOWChain bool } -type beaconDB interface { - GetChainHead() (*types.Block, error) - GetGenesisTime() (time.Time, error) - GetSimulatorSlot() (uint64, error) - SaveSimulatorSlot(uint64) error - GetActiveState() (*types.ActiveState, error) - GetCrystallizedState() (*types.CrystallizedState, error) - SaveCrystallizedState(*types.CrystallizedState) error -} - // DefaultConfig options for the simulator. func DefaultConfig() *Config { return &Config{ BlockRequestBuf: 100, BlockSlotBuf: 100, - CStateReqBuf: 100, + StateReqBuf: 100, ChainHeadRequestBuf: 100, } } @@ -90,7 +81,7 @@ func NewSimulator(ctx context.Context, cfg *Config) *Simulator { enablePOWChain: cfg.EnablePOWChain, blockRequestChan: make(chan p2p.Message, cfg.BlockRequestBuf), blockBySlotChan: make(chan p2p.Message, cfg.BlockSlotBuf), - cStateReqChan: make(chan p2p.Message, cfg.CStateReqBuf), + stateReqChan: make(chan p2p.Message, cfg.StateReqBuf), chainHeadRequestChan: make(chan p2p.Message, cfg.ChainHeadRequestBuf), } } @@ -130,10 +121,10 @@ func (sim *Simulator) run(slotInterval <-chan uint64) { chainHdReqSub := sim.p2p.Subscribe(&pb.ChainHeadRequest{}, sim.chainHeadRequestChan) blockReqSub := sim.p2p.Subscribe(&pb.BeaconBlockRequest{}, sim.blockRequestChan) blockBySlotSub := sim.p2p.Subscribe(&pb.BeaconBlockRequestBySlotNumber{}, sim.blockBySlotChan) - cStateReqSub := sim.p2p.Subscribe(&pb.CrystallizedStateRequest{}, sim.cStateReqChan) + stateReqSub := sim.p2p.Subscribe(&pb.BeaconStateRequest{}, sim.stateReqChan) defer blockReqSub.Unsubscribe() defer blockBySlotSub.Unsubscribe() - defer cStateReqSub.Unsubscribe() + defer stateReqSub.Unsubscribe() defer chainHdReqSub.Unsubscribe() lastBlock, err := sim.beaconDB.GetChainHead() @@ -156,13 +147,12 @@ func (sim *Simulator) run(slotInterval <-chan uint64) { return case msg := <-sim.chainHeadRequestChan: - log.Debug("Received Chain Head Request") + log.Debug("Received chain head request") if err := sim.SendChainHead(msg.Peer); err != nil { log.Errorf("Unable to send chain head response %v", err) } case slot := <-slotInterval: - block, err := sim.generateBlock(slot, lastHash) if err != nil { log.Error(err) @@ -233,35 +223,35 @@ func (sim *Simulator) run(slotInterval <-chan uint64) { AttesterBitfield: []byte{byte(255)}, }} sim.p2p.Send(res, msg.Peer) - case msg := <-sim.cStateReqChan: - data := msg.Data.(*pb.CrystallizedStateRequest) + case msg := <-sim.stateReqChan: + data := msg.Data.(*pb.BeaconStateRequest) - cState, err := sim.beaconDB.GetCrystallizedState() + beaconState, err := sim.beaconDB.GetState() if err != nil { - log.Errorf("Could not retrieve crystallized state: %v", err) + log.Errorf("Could not retrieve beacon state: %v", err) continue } - hash, err := cState.Hash() + hash, err := beaconState.Hash() if err != nil { - log.Errorf("Could not hash crystallized state: %v", err) + log.Errorf("Could not hash beacon state: %v", err) continue } if !bytes.Equal(data.GetHash(), hash[:]) { log.WithFields(logrus.Fields{ "hash": fmt.Sprintf("%#x", data.GetHash()), - }).Debug("Requested Crystallized state is of a different hash") + }).Debug("Requested beacon state is of a different hash") continue } log.WithFields(logrus.Fields{ "hash": fmt.Sprintf("%#x", hash), - }).Debug("Responding to full crystallized state request") + }).Debug("Responding to full beacon state request") // Sends the full crystallized state to the requester. - res := &pb.CrystallizedStateResponse{ - CrystallizedState: cState.Proto(), + res := &pb.BeaconStateResponse{ + BeaconState: beaconState.Proto(), } sim.p2p.Send(res, msg.Peer) } @@ -270,26 +260,14 @@ func (sim *Simulator) run(slotInterval <-chan uint64) { // generateBlock generates fake blocks for the simulator. func (sim *Simulator) generateBlock(slot uint64, lastHash [32]byte) (*types.Block, error) { - - aState, err := sim.beaconDB.GetActiveState() + beaconState, err := sim.beaconDB.GetState() if err != nil { - return nil, fmt.Errorf("failed to get active state: %v", err) + return nil, fmt.Errorf("could not retrieve beacon state: %v", err) } - cState, err := sim.beaconDB.GetCrystallizedState() + stateHash, err := beaconState.Hash() if err != nil { - return nil, fmt.Errorf("failed to get crystallized state: %v", err) - } - - aStateHash, err := aState.Hash() - if err != nil { - return nil, fmt.Errorf("failed to hash active state: %v", err) - } - - cStateHash, err := cState.Hash() - if err != nil { - return nil, fmt.Errorf("failed to hash crystallized state: %v", err) - + return nil, fmt.Errorf("could not hash beacon state: %v", err) } var powChainRef []byte @@ -300,10 +278,13 @@ func (sim *Simulator) generateBlock(slot uint64, lastHash [32]byte) (*types.Bloc } parentSlot := slot - 1 - committees, err := cState.GetShardsAndCommitteesForSlot(parentSlot) + committees, err := v.GetShardAndCommitteesForSlot( + beaconState.ShardAndCommitteesForSlots(), + beaconState.LastStateRecalculationSlot(), + parentSlot, + ) if err != nil { - log.Errorf("Failed to get shard committee: %v", err) - + return nil, fmt.Errorf("failed to get shard committee: %v", err) } parentHash := make([]byte, 32) @@ -326,14 +307,13 @@ func (sim *Simulator) generateBlock(slot uint64, lastHash [32]byte) (*types.Bloc } block := types.NewBlock(&pb.BeaconBlock{ - Slot: slot, - Timestamp: ptypes.TimestampNow(), - PowChainRef: powChainRef, - ActiveStateRoot: aStateHash[:], - CrystallizedStateRoot: cStateHash[:], - AncestorHashes: [][]byte{parentHash}, - RandaoReveal: params.BeaconConfig().SimulatedBlockRandao[:], - Attestations: attestations, + Slot: slot, + Timestamp: ptypes.TimestampNow(), + PowChainRef: powChainRef, + StateRoot: stateHash[:], + AncestorHashes: [][]byte{parentHash}, + RandaoReveal: params.BeaconConfig().SimulatedBlockRandao[:], + Attestations: attestations, }) return block, nil } diff --git a/beacon-chain/simulator/service_test.go b/beacon-chain/simulator/service_test.go index 6f23c5263e..f5e0182931 100644 --- a/beacon-chain/simulator/service_test.go +++ b/beacon-chain/simulator/service_test.go @@ -58,7 +58,7 @@ func setupSimulator(t *testing.T, beaconDB *db.BeaconDB) (*Simulator, *mockP2P) Web3Service: &mockPOWChainService{}, BeaconDB: beaconDB, EnablePOWChain: true, - CStateReqBuf: 10, + StateReqBuf: 10, } return NewSimulator(ctx, cfg), p2pService @@ -187,7 +187,7 @@ func TestBlockRequestBySlot(t *testing.T) { hook.Reset() } -func TestCrystallizedStateRequest(t *testing.T) { +func TestStateRequest(t *testing.T) { hook := logTest.NewGlobal() db := internal.SetupDB(t) @@ -202,43 +202,43 @@ func TestCrystallizedStateRequest(t *testing.T) { <-exitRoutine }() - cState, err := sim.beaconDB.GetCrystallizedState() + beaconState, err := sim.beaconDB.GetState() if err != nil { - t.Fatalf("could not retrieve crystallized state %v", err) + t.Fatalf("could not retrieve beacon state %v", err) } - hash, err := cState.Hash() + hash, err := beaconState.Hash() if err != nil { - t.Fatalf("could not hash crystallized state %v", err) + t.Fatalf("could not hash beacon state %v", err) } - cStateRequest := &pb.CrystallizedStateRequest{ + beaconStateRequest := &pb.BeaconStateRequest{ Hash: []byte{'t', 'e', 's', 't'}, } message := p2p.Message{ - Data: cStateRequest, + Data: beaconStateRequest, } - sim.cStateReqChan <- message + sim.stateReqChan <- message - testutil.WaitForLog(t, hook, "Requested Crystallized state is of a different hash") - testutil.AssertLogsDoNotContain(t, hook, "Responding to full crystallized state request") + testutil.WaitForLog(t, hook, "Requested beacon state is of a different hash") + testutil.AssertLogsDoNotContain(t, hook, "Responding to full beacon state request") hook.Reset() - newCStateReq := &pb.CrystallizedStateRequest{ + newStateReq := &pb.BeaconStateRequest{ Hash: hash[:], } newMessage := p2p.Message{ - Data: newCStateReq, + Data: newStateReq, } - sim.cStateReqChan <- newMessage + sim.stateReqChan <- newMessage - testutil.WaitForLog(t, hook, "Responding to full crystallized state request") - testutil.AssertLogsDoNotContain(t, hook, "Requested Crystallized state is of a different hash") + testutil.WaitForLog(t, hook, "Responding to full beacon state request") + testutil.AssertLogsDoNotContain(t, hook, "Requested beacon state is of a different hash") sim.cancel() exitRoutine <- true diff --git a/beacon-chain/sync/BUILD.bazel b/beacon-chain/sync/BUILD.bazel index bc15cbe012..0316f825db 100644 --- a/beacon-chain/sync/BUILD.bazel +++ b/beacon-chain/sync/BUILD.bazel @@ -6,8 +6,9 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/sync", visibility = ["//beacon-chain:__subpackages__"], deps = [ + "//beacon-chain/core/types:go_default_library", "//beacon-chain/core/validators:go_default_library", - "//beacon-chain/types:go_default_library", + "//beacon-chain/db:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/event:go_default_library", "//shared/p2p:go_default_library", @@ -22,9 +23,9 @@ go_test( srcs = ["service_test.go"], embed = [":go_default_library"], deps = [ + "//beacon-chain/core/types:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/internal:go_default_library", - "//beacon-chain/types:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/event:go_default_library", "//shared/hashutil:go_default_library", diff --git a/beacon-chain/sync/initial-sync/BUILD.bazel b/beacon-chain/sync/initial-sync/BUILD.bazel index 4437f4aea2..6792b27e34 100644 --- a/beacon-chain/sync/initial-sync/BUILD.bazel +++ b/beacon-chain/sync/initial-sync/BUILD.bazel @@ -6,7 +6,8 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync", visibility = ["//beacon-chain:__subpackages__"], deps = [ - "//beacon-chain/types:go_default_library", + "//beacon-chain/core/types:go_default_library", + "//beacon-chain/db:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/event:go_default_library", "//shared/p2p:go_default_library", @@ -21,7 +22,8 @@ go_test( srcs = ["service_test.go"], embed = [":go_default_library"], deps = [ - "//beacon-chain/types:go_default_library", + "//beacon-chain/core/types:go_default_library", + "//beacon-chain/internal:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/event:go_default_library", "//shared/p2p:go_default_library", diff --git a/beacon-chain/sync/initial-sync/service.go b/beacon-chain/sync/initial-sync/service.go index 29e3d8ca88..fd0cd80cdc 100644 --- a/beacon-chain/sync/initial-sync/service.go +++ b/beacon-chain/sync/initial-sync/service.go @@ -17,7 +17,8 @@ import ( "time" "github.com/golang/protobuf/proto" - "github.com/prysmaticlabs/prysm/beacon-chain/types" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" + "github.com/prysmaticlabs/prysm/beacon-chain/db" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/event" "github.com/prysmaticlabs/prysm/shared/p2p" @@ -30,14 +31,14 @@ var log = logrus.WithField("prefix", "initial-sync") // Config defines the configurable properties of InitialSync. // type Config struct { - SyncPollingInterval time.Duration - BlockBufferSize int - BlockAnnounceBufferSize int - CrystallizedStateBufferSize int - BeaconDB beaconDB - P2P p2pAPI - SyncService syncService - QueryService queryService + SyncPollingInterval time.Duration + BlockBufferSize int + BlockAnnounceBufferSize int + StateBufferSize int + BeaconDB *db.BeaconDB + P2P p2pAPI + SyncService syncService + QueryService queryService } // DefaultConfig provides the default configuration for a sync service. @@ -46,10 +47,10 @@ type Config struct { // CrystallizedStateBufferSize determines the buffer size of thhe `crystallizedStateBuf` channel. func DefaultConfig() Config { return Config{ - SyncPollingInterval: time.Duration(params.BeaconConfig().SyncPollingInterval) * time.Second, - BlockBufferSize: 100, - BlockAnnounceBufferSize: 100, - CrystallizedStateBufferSize: 100, + SyncPollingInterval: time.Duration(params.BeaconConfig().SyncPollingInterval) * time.Second, + BlockBufferSize: 100, + BlockAnnounceBufferSize: 100, + StateBufferSize: 100, } } @@ -59,11 +60,6 @@ type p2pAPI interface { Broadcast(msg proto.Message) } -type beaconDB interface { - SaveBlock(*types.Block) error - SaveCrystallizedState(*types.CrystallizedState) error -} - // SyncService is the interface for the Sync service. // InitialSync calls `Start` when initial sync completes. type syncService interface { @@ -79,20 +75,20 @@ type queryService interface { // InitialSync defines the main class in this package. // See the package comments for a general description of the service's functions. type InitialSync struct { - ctx context.Context - cancel context.CancelFunc - p2p p2pAPI - syncService syncService - queryService queryService - db beaconDB - blockAnnounceBuf chan p2p.Message - blockBuf chan p2p.Message - crystallizedStateBuf chan p2p.Message - currentSlot uint64 - highestObservedSlot uint64 - syncPollingInterval time.Duration - initialCrystallizedStateRoot [32]byte - inMemoryBlocks map[uint64]*pb.BeaconBlockResponse + ctx context.Context + cancel context.CancelFunc + p2p p2pAPI + syncService syncService + queryService queryService + db *db.BeaconDB + blockAnnounceBuf chan p2p.Message + blockBuf chan p2p.Message + stateBuf chan p2p.Message + currentSlot uint64 + highestObservedSlot uint64 + syncPollingInterval time.Duration + initialStateRoot [32]byte + inMemoryBlocks map[uint64]*pb.BeaconBlockResponse } // NewInitialSyncService constructs a new InitialSyncService. @@ -103,23 +99,23 @@ func NewInitialSyncService(ctx context.Context, ctx, cancel := context.WithCancel(ctx) blockBuf := make(chan p2p.Message, cfg.BlockBufferSize) - crystallizedStateBuf := make(chan p2p.Message, cfg.CrystallizedStateBufferSize) + stateBuf := make(chan p2p.Message, cfg.StateBufferSize) blockAnnounceBuf := make(chan p2p.Message, cfg.BlockAnnounceBufferSize) return &InitialSync{ - ctx: ctx, - cancel: cancel, - p2p: cfg.P2P, - syncService: cfg.SyncService, - db: cfg.BeaconDB, - currentSlot: 0, - highestObservedSlot: 0, - blockBuf: blockBuf, - crystallizedStateBuf: crystallizedStateBuf, - blockAnnounceBuf: blockAnnounceBuf, - syncPollingInterval: cfg.SyncPollingInterval, - inMemoryBlocks: map[uint64]*pb.BeaconBlockResponse{}, - queryService: cfg.QueryService, + ctx: ctx, + cancel: cancel, + p2p: cfg.P2P, + syncService: cfg.SyncService, + db: cfg.BeaconDB, + currentSlot: 0, + highestObservedSlot: 0, + blockBuf: blockBuf, + stateBuf: stateBuf, + blockAnnounceBuf: blockAnnounceBuf, + syncPollingInterval: cfg.SyncPollingInterval, + inMemoryBlocks: map[uint64]*pb.BeaconBlockResponse{}, + queryService: cfg.QueryService, } } @@ -153,17 +149,17 @@ func (s *InitialSync) Stop() error { // run is the main goroutine for the initial sync service. // delayChan is explicitly passed into this function to facilitate tests that don't require a timeout. // It is assumed that the goroutine `run` is only called once per instance. -func (s *InitialSync) run(delaychan <-chan time.Time) { +func (s *InitialSync) run(delayChan <-chan time.Time) { blockSub := s.p2p.Subscribe(&pb.BeaconBlockResponse{}, s.blockBuf) blockAnnounceSub := s.p2p.Subscribe(&pb.BeaconBlockAnnounce{}, s.blockAnnounceBuf) - crystallizedStateSub := s.p2p.Subscribe(&pb.CrystallizedStateResponse{}, s.crystallizedStateBuf) + beaconStateSub := s.p2p.Subscribe(&pb.BeaconStateResponse{}, s.stateBuf) defer func() { blockSub.Unsubscribe() blockAnnounceSub.Unsubscribe() - crystallizedStateSub.Unsubscribe() + beaconStateSub.Unsubscribe() close(s.blockBuf) - close(s.crystallizedStateBuf) + close(s.stateBuf) }() for { @@ -171,7 +167,7 @@ func (s *InitialSync) run(delaychan <-chan time.Time) { case <-s.ctx.Done(): log.Debug("Exiting goroutine") return - case <-delaychan: + case <-delayChan: if s.currentSlot == 0 { continue } @@ -201,7 +197,7 @@ func (s *InitialSync) run(delaychan <-chan time.Time) { } if s.currentSlot == 0 { - if s.initialCrystallizedStateRoot != [32]byte{} { + if s.initialStateRoot != [32]byte{} { continue } if data.GetBlock().GetSlot() != 1 { @@ -216,8 +212,8 @@ func (s *InitialSync) run(delaychan <-chan time.Time) { if err := s.setBlockForInitialSync(data); err != nil { log.Errorf("Could not set block for initial sync: %v", err) } - if err := s.requestCrystallizedStateFromPeer(data, msg.Peer); err != nil { - log.Errorf("Could not request crystallized state from peer: %v", err) + if err := s.requestStateFromPeer(data, msg.Peer); err != nil { + log.Errorf("Could not request beacon state from peer: %v", err) } continue @@ -234,51 +230,51 @@ func (s *InitialSync) run(delaychan <-chan time.Time) { log.Errorf("Unable to save block: %v", err) } s.requestNextBlockBySlot(s.currentSlot + 1) - case msg := <-s.crystallizedStateBuf: - data := msg.Data.(*pb.CrystallizedStateResponse) + case msg := <-s.stateBuf: + data := msg.Data.(*pb.BeaconStateResponse) - if s.initialCrystallizedStateRoot == [32]byte{} { + if s.initialStateRoot == [32]byte{} { continue } - cState := types.NewCrystallizedState(data.CrystallizedState) - hash, err := cState.Hash() + beaconState := types.NewBeaconState(data.BeaconState) + hash, err := beaconState.Hash() if err != nil { - log.Errorf("Unable to hash crytsallized state: %v", err) + log.Errorf("Unable to hash beacon state: %v", err) } - if hash != s.initialCrystallizedStateRoot { + if hash != s.initialStateRoot { continue } - if err := s.db.SaveCrystallizedState(cState); err != nil { - log.Errorf("Unable to set crystallized state for initial sync %v", err) + if err := s.db.SaveState(beaconState); err != nil { + log.Errorf("Unable to set beacon state for initial sync %v", err) } - log.Debug("Successfully saved crystallized state to the db") + log.Debug("Successfully saved beacon state to the db") - if s.currentSlot >= cState.LastFinalizedSlot() { + if s.currentSlot >= beaconState.LastFinalizedSlot() { continue } // sets the current slot to the last finalized slot of the // crystallized state to begin our sync from. - s.currentSlot = cState.LastFinalizedSlot() - log.Debugf("Successfully saved crystallized state with the last finalized slot: %d", cState.LastFinalizedSlot()) + s.currentSlot = beaconState.LastFinalizedSlot() + log.Debugf("Successfully saved crystallized state with the last finalized slot: %d", beaconState.LastFinalizedSlot()) s.requestNextBlockBySlot(s.currentSlot + 1) - crystallizedStateSub.Unsubscribe() + beaconStateSub.Unsubscribe() } } } -// requestCrystallizedStateFromPeer sends a request to a peer for the corresponding crystallized state +// requestStateFromPeer sends a request to a peer for the corresponding state // for a beacon block. -func (s *InitialSync) requestCrystallizedStateFromPeer(data *pb.BeaconBlockResponse, peer p2p.Peer) error { +func (s *InitialSync) requestStateFromPeer(data *pb.BeaconBlockResponse, peer p2p.Peer) error { block := types.NewBlock(data.Block) - h := block.CrystallizedStateRoot() - log.Debugf("Successfully processed incoming block with crystallized state hash: %#x", h) - s.p2p.Send(&pb.CrystallizedStateRequest{Hash: h[:]}, peer) + h := block.StateRoot() + log.Debugf("Successfully processed incoming block with state hash: %#x", h) + s.p2p.Send(&pb.BeaconStateRequest{Hash: h[:]}, peer) return nil } @@ -291,13 +287,13 @@ func (s *InitialSync) setBlockForInitialSync(data *pb.BeaconBlockResponse) error if err != nil { return err } - log.WithField("blockhash", fmt.Sprintf("%#x", h)).Debug("Crystallized state hash exists locally") + log.WithField("blockhash", fmt.Sprintf("%#x", h)).Debug("State state hash exists locally") if err := s.writeBlockToDB(block); err != nil { return err } - s.initialCrystallizedStateRoot = block.CrystallizedStateRoot() + s.initialStateRoot = block.StateRoot() log.Infof("Saved block with hash %#x for initial sync", h) s.currentSlot = block.SlotNumber() @@ -306,15 +302,15 @@ func (s *InitialSync) setBlockForInitialSync(data *pb.BeaconBlockResponse) error } // requestNextBlock broadcasts a request for a block with the entered slotnumber. -func (s *InitialSync) requestNextBlockBySlot(slotnumber uint64) { - log.Debugf("Requesting block %d ", slotnumber) - if _, ok := s.inMemoryBlocks[slotnumber]; ok { +func (s *InitialSync) requestNextBlockBySlot(slotNumber uint64) { + log.Debugf("Requesting block %d ", slotNumber) + if _, ok := s.inMemoryBlocks[slotNumber]; ok { s.blockBuf <- p2p.Message{ - Data: s.inMemoryBlocks[slotnumber], + Data: s.inMemoryBlocks[slotNumber], } return } - s.p2p.Broadcast(&pb.BeaconBlockRequestBySlotNumber{SlotNumber: slotnumber}) + s.p2p.Broadcast(&pb.BeaconBlockRequestBySlotNumber{SlotNumber: slotNumber}) } // requestBatchedBlocks sends out multiple requests for blocks till a @@ -340,7 +336,6 @@ func (s *InitialSync) validateAndSaveNextBlock(data *pb.BeaconBlockResponse) err } if (s.currentSlot + 1) == block.SlotNumber() { - if err := s.writeBlockToDB(block); err != nil { return err } diff --git a/beacon-chain/sync/initial-sync/service_test.go b/beacon-chain/sync/initial-sync/service_test.go index d6d89b12c6..3b053ba2f4 100644 --- a/beacon-chain/sync/initial-sync/service_test.go +++ b/beacon-chain/sync/initial-sync/service_test.go @@ -7,7 +7,8 @@ import ( "time" "github.com/golang/protobuf/proto" - "github.com/prysmaticlabs/prysm/beacon-chain/types" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" + "github.com/prysmaticlabs/prysm/beacon-chain/internal" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/event" "github.com/prysmaticlabs/prysm/shared/p2p" @@ -52,23 +53,15 @@ func (ms *mockQueryService) IsSynced() (bool, error) { return ms.isSynced, nil } -type mockDB struct{} - -func (m *mockDB) SaveBlock(*types.Block) error { - return nil -} - -func (m *mockDB) SaveCrystallizedState(*types.CrystallizedState) error { - return nil -} - func TestSetBlockForInitialSync(t *testing.T) { hook := logTest.NewGlobal() + db := internal.SetupDB(t) + defer internal.TeardownDB(t, db) cfg := Config{ P2P: &mockP2P{}, SyncService: &mockSyncService{}, - BeaconDB: &mockDB{}, + BeaconDB: db, } ss := NewInitialSyncService(context.Background(), cfg) @@ -89,10 +82,10 @@ func TestSetBlockForInitialSync(t *testing.T) { genericHash[0] = 'a' block := &pb.BeaconBlock{ - PowChainRef: []byte{1, 2, 3}, - AncestorHashes: [][]byte{genericHash}, - Slot: uint64(1), - CrystallizedStateRoot: genericHash, + PowChainRef: []byte{1, 2, 3}, + AncestorHashes: [][]byte{genericHash}, + Slot: uint64(1), + StateRoot: genericHash, } blockResponse := &pb.BeaconBlockResponse{Block: block} @@ -107,10 +100,10 @@ func TestSetBlockForInitialSync(t *testing.T) { <-exitRoutine var hash [32]byte - copy(hash[:], blockResponse.Block.CrystallizedStateRoot) + copy(hash[:], blockResponse.Block.StateRoot) - if hash != ss.initialCrystallizedStateRoot { - t.Fatalf("Crystallized state hash not updated: %#x", blockResponse.Block.CrystallizedStateRoot) + if hash != ss.initialStateRoot { + t.Fatalf("Crystallized state hash not updated: %#x", blockResponse.Block.StateRoot) } hook.Reset() @@ -118,11 +111,13 @@ func TestSetBlockForInitialSync(t *testing.T) { func TestSavingBlocksInSync(t *testing.T) { hook := logTest.NewGlobal() + db := internal.SetupDB(t) + defer internal.TeardownDB(t, db) cfg := Config{ P2P: &mockP2P{}, SyncService: &mockSyncService{}, - BeaconDB: &mockDB{}, + BeaconDB: db, } ss := NewInitialSyncService(context.Background(), cfg) @@ -142,34 +137,34 @@ func TestSavingBlocksInSync(t *testing.T) { genericHash := make([]byte, 32) genericHash[0] = 'a' - crystallizedState := &pb.CrystallizedState{ + beaconState := &pb.BeaconState{ LastFinalizedSlot: 99, } - stateResponse := &pb.CrystallizedStateResponse{ - CrystallizedState: crystallizedState, + stateResponse := &pb.BeaconStateResponse{ + BeaconState: beaconState, } - incorrectState := &pb.CrystallizedState{ + incorrectState := &pb.BeaconState{ LastFinalizedSlot: 9, LastJustifiedSlot: 20, } - incorrectStateResponse := &pb.CrystallizedStateResponse{ - CrystallizedState: incorrectState, + incorrectStateResponse := &pb.BeaconStateResponse{ + BeaconState: incorrectState, } - crystallizedStateRoot, err := types.NewCrystallizedState(crystallizedState).Hash() + beaconStateRoot, err := types.NewBeaconState(beaconState).Hash() if err != nil { - t.Fatalf("unable to get hash of crystallized state: %v", err) + t.Fatalf("unable to get hash of state: %v", err) } getBlockResponseMsg := func(Slot uint64) p2p.Message { block := &pb.BeaconBlock{ - PowChainRef: []byte{1, 2, 3}, - AncestorHashes: [][]byte{genericHash}, - Slot: Slot, - CrystallizedStateRoot: crystallizedStateRoot[:], + PowChainRef: []byte{1, 2, 3}, + AncestorHashes: [][]byte{genericHash}, + Slot: Slot, + StateRoot: beaconStateRoot[:], } blockResponse := &pb.BeaconBlockResponse{ @@ -190,27 +185,27 @@ func TestSavingBlocksInSync(t *testing.T) { } ss.blockBuf <- msg1 - ss.crystallizedStateBuf <- msg2 + ss.stateBuf <- msg2 - if ss.currentSlot == incorrectStateResponse.CrystallizedState.LastFinalizedSlot { + if ss.currentSlot == incorrectStateResponse.BeaconState.LastFinalizedSlot { t.Fatalf("Crystallized state updated incorrectly: %d", ss.currentSlot) } msg2.Data = stateResponse - ss.crystallizedStateBuf <- msg2 + ss.stateBuf <- msg2 - if crystallizedStateRoot != ss.initialCrystallizedStateRoot { + if beaconStateRoot != ss.initialStateRoot { br := msg1.Data.(*pb.BeaconBlockResponse) - t.Fatalf("Crystallized state hash not updated to: %#x instead it is %#x", br.Block.CrystallizedStateRoot, - ss.initialCrystallizedStateRoot) + t.Fatalf("state hash not updated to: %#x instead it is %#x", br.Block.StateRoot, + ss.initialStateRoot) } msg1 = getBlockResponseMsg(30) ss.blockBuf <- msg1 - if stateResponse.CrystallizedState.GetLastFinalizedSlot() != ss.currentSlot { - t.Fatalf("Slot saved when it was not supposed too: %v", stateResponse.CrystallizedState.GetLastFinalizedSlot()) + if stateResponse.BeaconState.GetLastFinalizedSlot() != ss.currentSlot { + t.Fatalf("Slot saved when it was not supposed too: %v", stateResponse.BeaconState.GetLastFinalizedSlot()) } msg1 = getBlockResponseMsg(100) @@ -229,10 +224,12 @@ func TestSavingBlocksInSync(t *testing.T) { func TestDelayChan(t *testing.T) { hook := logTest.NewGlobal() + db := internal.SetupDB(t) + defer internal.TeardownDB(t, db) cfg := Config{ P2P: &mockP2P{}, SyncService: &mockSyncService{}, - BeaconDB: &mockDB{}, + BeaconDB: db, } ss := NewInitialSyncService(context.Background(), cfg) @@ -252,24 +249,24 @@ func TestDelayChan(t *testing.T) { genericHash := make([]byte, 32) genericHash[0] = 'a' - crystallizedstate := &pb.CrystallizedState{ + beaconState := &pb.BeaconState{ LastFinalizedSlot: 99, } - stateResponse := &pb.CrystallizedStateResponse{ - CrystallizedState: crystallizedstate, + stateResponse := &pb.BeaconStateResponse{ + BeaconState: beaconState, } - crystallizedStateRoot, err := types.NewCrystallizedState(stateResponse.CrystallizedState).Hash() + beaconStateRoot, err := types.NewBeaconState(stateResponse.BeaconState).Hash() if err != nil { - t.Fatalf("unable to get hash of crystallized state: %v", err) + t.Fatalf("unable to get hash of state: %v", err) } block := &pb.BeaconBlock{ - PowChainRef: []byte{1, 2, 3}, - AncestorHashes: [][]byte{genericHash}, - Slot: uint64(1), - CrystallizedStateRoot: crystallizedStateRoot[:], + PowChainRef: []byte{1, 2, 3}, + AncestorHashes: [][]byte{genericHash}, + Slot: uint64(1), + StateRoot: beaconStateRoot[:], } blockResponse := &pb.BeaconBlockResponse{ @@ -288,7 +285,7 @@ func TestDelayChan(t *testing.T) { ss.blockBuf <- msg1 - ss.crystallizedStateBuf <- msg2 + ss.stateBuf <- msg2 blockResponse.Block.Slot = 100 msg1.Data = blockResponse @@ -308,10 +305,12 @@ func TestDelayChan(t *testing.T) { func TestIsSyncedWithNetwork(t *testing.T) { hook := logTest.NewGlobal() mockSync := &mockSyncService{} + db := internal.SetupDB(t) + defer internal.TeardownDB(t, db) cfg := Config{ P2P: &mockP2P{}, SyncService: mockSync, - BeaconDB: &mockDB{}, + BeaconDB: db, QueryService: &mockQueryService{ isSynced: true, }, @@ -331,10 +330,12 @@ func TestIsSyncedWithNetwork(t *testing.T) { func TestIsNotSyncedWithNetwork(t *testing.T) { hook := logTest.NewGlobal() mockSync := &mockSyncService{} + db := internal.SetupDB(t) + defer internal.TeardownDB(t, db) cfg := Config{ P2P: &mockP2P{}, SyncService: mockSync, - BeaconDB: &mockDB{}, + BeaconDB: db, QueryService: &mockQueryService{ isSynced: false, }, @@ -353,10 +354,12 @@ func TestIsNotSyncedWithNetwork(t *testing.T) { func TestRequestBlocksBySlot(t *testing.T) { hook := logTest.NewGlobal() + db := internal.SetupDB(t) + defer internal.TeardownDB(t, db) cfg := Config{ P2P: &mockP2P{}, SyncService: &mockSyncService{}, - BeaconDB: &mockDB{}, + BeaconDB: db, BlockBufferSize: 100, } ss := NewInitialSyncService(context.Background(), cfg) @@ -380,10 +383,10 @@ func TestRequestBlocksBySlot(t *testing.T) { getBlockResponseMsg := func(Slot uint64) (p2p.Message, [32]byte) { block := &pb.BeaconBlock{ - PowChainRef: []byte{1, 2, 3}, - AncestorHashes: [][]byte{genericHash}, - Slot: Slot, - CrystallizedStateRoot: nil, + PowChainRef: []byte{1, 2, 3}, + AncestorHashes: [][]byte{genericHash}, + Slot: Slot, + StateRoot: nil, } blockResponse := &pb.BeaconBlockResponse{ @@ -432,10 +435,12 @@ func TestRequestBlocksBySlot(t *testing.T) { func TestRequestBatchedBlocks(t *testing.T) { hook := logTest.NewGlobal() + db := internal.SetupDB(t) + defer internal.TeardownDB(t, db) cfg := Config{ P2P: &mockP2P{}, SyncService: &mockSyncService{}, - BeaconDB: &mockDB{}, + BeaconDB: db, BlockBufferSize: 100, } ss := NewInitialSyncService(context.Background(), cfg) @@ -459,10 +464,10 @@ func TestRequestBatchedBlocks(t *testing.T) { getBlockResponse := func(Slot uint64) (*pb.BeaconBlockResponse, [32]byte) { block := &pb.BeaconBlock{ - PowChainRef: []byte{1, 2, 3}, - AncestorHashes: [][]byte{genericHash}, - Slot: Slot, - CrystallizedStateRoot: nil, + PowChainRef: []byte{1, 2, 3}, + AncestorHashes: [][]byte{genericHash}, + Slot: Slot, + StateRoot: nil, } blockResponse := &pb.BeaconBlockResponse{ diff --git a/beacon-chain/sync/service.go b/beacon-chain/sync/service.go index 804d202acb..4cfeec7339 100644 --- a/beacon-chain/sync/service.go +++ b/beacon-chain/sync/service.go @@ -6,8 +6,9 @@ import ( "fmt" "github.com/golang/protobuf/proto" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" - "github.com/prysmaticlabs/prysm/beacon-chain/types" + "github.com/prysmaticlabs/prysm/beacon-chain/db" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/event" "github.com/prysmaticlabs/prysm/shared/p2p" @@ -25,15 +26,6 @@ type attestationService interface { IncomingAttestationFeed() *event.Feed } -type beaconDB interface { - GetCrystallizedState() (*types.CrystallizedState, error) - GetBlock([32]byte) (*types.Block, error) - HasBlock([32]byte) bool - GetChainHead() (*types.Block, error) - GetAttestation([32]byte) (*types.Attestation, error) - GetBlockBySlot(uint64) (*types.Block, error) -} - type queryService interface { IsSynced() (bool, error) } @@ -63,7 +55,7 @@ type Service struct { chainService chainService attestationService attestationService queryService queryService - db beaconDB + db *db.BeaconDB blockAnnouncementFeed *event.Feed announceBlockBuf chan p2p.Message blockBuf chan p2p.Message @@ -82,7 +74,7 @@ type Config struct { ChainService chainService AttestService attestationService QueryService queryService - BeaconDB beaconDB + BeaconDB *db.BeaconDB P2P p2pAPI } @@ -233,13 +225,13 @@ func (ss *Service) receiveBlock(msg p2p.Message) { return } - cState, err := ss.db.GetCrystallizedState() + beaconState, err := ss.db.GetState() if err != nil { - log.Errorf("Failed to get crystallized state: %v", err) + log.Errorf("Failed to get beacon state: %v", err) return } - if block.SlotNumber() < cState.LastFinalizedSlot() { + if block.SlotNumber() < beaconState.LastFinalizedSlot() { log.Debug("Discarding received block with a slot number smaller than the last finalized slot") return } @@ -247,7 +239,11 @@ func (ss *Service) receiveBlock(msg p2p.Message) { // Verify attestation coming from proposer then forward block to the subscribers. attestation := types.NewAttestation(response.Attestation) - proposerShardID, _, err := v.ProposerShardAndIndex(cState.ShardAndCommitteesForSlots(), cState.LastStateRecalculationSlot(), block.SlotNumber()) + proposerShardID, _, err := v.ProposerShardAndIndex( + beaconState.ShardAndCommitteesForSlots(), + beaconState.LastStateRecalculationSlot(), + block.SlotNumber(), + ) if err != nil { log.Errorf("Failed to get proposer shard ID: %v", err) return diff --git a/beacon-chain/sync/service_test.go b/beacon-chain/sync/service_test.go index 57769db9f2..ad77edfebe 100644 --- a/beacon-chain/sync/service_test.go +++ b/beacon-chain/sync/service_test.go @@ -6,9 +6,9 @@ import ( "testing" "github.com/golang/protobuf/proto" + "github.com/prysmaticlabs/prysm/beacon-chain/core/types" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/internal" - "github.com/prysmaticlabs/prysm/beacon-chain/types" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/event" "github.com/prysmaticlabs/prysm/shared/hashutil" diff --git a/beacon-chain/sync/sync-querier/BUILD.bazel b/beacon-chain/sync/sync-querier/BUILD.bazel index c91258b5f3..e8a942ac07 100644 --- a/beacon-chain/sync/sync-querier/BUILD.bazel +++ b/beacon-chain/sync/sync-querier/BUILD.bazel @@ -6,7 +6,7 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/sync/sync-querier", visibility = ["//beacon-chain:__subpackages__"], deps = [ - "//beacon-chain/types:go_default_library", + "//beacon-chain/db:go_default_library", "//proto/beacon/p2p/v1:go_default_library", "//shared/event:go_default_library", "//shared/p2p:go_default_library", diff --git a/beacon-chain/sync/sync-querier/service.go b/beacon-chain/sync/sync-querier/service.go index 7d5911ba68..dd9dd0f2a2 100644 --- a/beacon-chain/sync/sync-querier/service.go +++ b/beacon-chain/sync/sync-querier/service.go @@ -5,7 +5,7 @@ import ( "time" "github.com/golang/protobuf/proto" - "github.com/prysmaticlabs/prysm/beacon-chain/types" + "github.com/prysmaticlabs/prysm/beacon-chain/db" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/event" "github.com/prysmaticlabs/prysm/shared/p2p" @@ -19,7 +19,7 @@ var log = logrus.WithField("prefix", "syncQuerier") type Config struct { ResponseBufferSize int P2P p2pAPI - BeaconDB beaconDB + BeaconDB *db.BeaconDB } // DefaultConfig provides the default configuration for a sync service. @@ -36,18 +36,13 @@ type p2pAPI interface { Broadcast(msg proto.Message) } -type beaconDB interface { - SaveBlock(*types.Block) error - GetChainHead() (*types.Block, error) -} - // SyncQuerier defines the main class in this package. // See the package comments for a general description of the service's functions. type SyncQuerier struct { ctx context.Context cancel context.CancelFunc p2p p2pAPI - db beaconDB + db *db.BeaconDB curentHeadSlot uint64 currentHeadHash []byte responseBuf chan p2p.Message @@ -107,7 +102,7 @@ func (s *SyncQuerier) run() { s.RequestLatestHead() case msg := <-s.responseBuf: response := msg.Data.(*pb.ChainHeadResponse) - log.Infof("Latest Chain head is at slot: %d and hash %#x", response.Slot, response.Hash) + log.Infof("Latest chain head is at slot: %d and hash %#x", response.Slot, response.Hash) s.curentHeadSlot = response.Slot s.currentHeadHash = response.Hash diff --git a/beacon-chain/sync/sync-querier/service_test.go b/beacon-chain/sync/sync-querier/service_test.go index 439ee48d19..07c5a4ae05 100644 --- a/beacon-chain/sync/sync-querier/service_test.go +++ b/beacon-chain/sync/sync-querier/service_test.go @@ -82,7 +82,7 @@ func TestChainReqResponse(t *testing.T) { sq.responseBuf <- msg - expMsg := fmt.Sprintf("Latest Chain head is at slot: %d and hash %#x", response.Slot, response.Hash) + expMsg := fmt.Sprintf("Latest chain head is at slot: %d and hash %#x", response.Slot, response.Hash) testutil.WaitForLog(t, hook, expMsg) diff --git a/beacon-chain/types/BUILD.bazel b/beacon-chain/types/BUILD.bazel deleted file mode 100644 index 99277fc8e3..0000000000 --- a/beacon-chain/types/BUILD.bazel +++ /dev/null @@ -1,49 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "go_default_library", - srcs = [ - "active_state.go", - "attestation.go", - "block.go", - "crystallized_state.go", - ], - importpath = "github.com/prysmaticlabs/prysm/beacon-chain/types", - visibility = ["//beacon-chain:__subpackages__"], - deps = [ - "//beacon-chain/core/incentives:go_default_library", - "//beacon-chain/core/state:go_default_library", - "//beacon-chain/core/validators:go_default_library", - "//beacon-chain/utils:go_default_library", - "//proto/beacon/p2p/v1:go_default_library", - "//shared/bitutil:go_default_library", - "//shared/hashutil:go_default_library", - "//shared/params:go_default_library", - "@com_github_ethereum_go_ethereum//common:go_default_library", - "@com_github_gogo_protobuf//proto:go_default_library", - "@com_github_golang_protobuf//proto:go_default_library", - "@com_github_golang_protobuf//ptypes:go_default_library_gen", - "@com_github_sirupsen_logrus//:go_default_library", - ], -) - -go_test( - name = "go_default_test", - srcs = [ - "active_state_test.go", - "attestation_test.go", - "block_test.go", - "crystallized_state_test.go", - ], - embed = [":go_default_library"], - race = "on", - deps = [ - "//beacon-chain/core/validators:go_default_library", - "//beacon-chain/utils:go_default_library", - "//proto/beacon/p2p/v1:go_default_library", - "//shared/hashutil:go_default_library", - "//shared/params:go_default_library", - "@com_github_golang_protobuf//ptypes:go_default_library_gen", - "@com_github_sirupsen_logrus//:go_default_library", - ], -) diff --git a/beacon-chain/types/active_state.go b/beacon-chain/types/active_state.go deleted file mode 100644 index 0c32d28d8a..0000000000 --- a/beacon-chain/types/active_state.go +++ /dev/null @@ -1,241 +0,0 @@ -package types - -import ( - "fmt" - - "github.com/ethereum/go-ethereum/common" - "github.com/gogo/protobuf/proto" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" - "github.com/prysmaticlabs/prysm/shared/hashutil" - "github.com/prysmaticlabs/prysm/shared/params" -) - -// ActiveState contains fields of current state of beacon chain, -// it changes every block. -type ActiveState struct { - data *pb.ActiveState -} - -// NewGenesisActiveState initializes the active state for slot 0. -func NewGenesisActiveState() *ActiveState { - // Bootstrap recent block hashes to all 0s for first 2 cycles. - var recentBlockHashes [][]byte - for i := 0; i < 2*int(params.BeaconConfig().CycleLength); i++ { - recentBlockHashes = append(recentBlockHashes, make([]byte, 0, 32)) - } - - return &ActiveState{ - data: &pb.ActiveState{ - PendingAttestations: []*pb.AggregatedAttestation{}, - PendingSpecials: []*pb.SpecialRecord{}, - RecentBlockHashes: recentBlockHashes, - RandaoMix: make([]byte, 0, 32), - }, - } -} - -// NewActiveState creates a new active state with a explicitly set data field. -func NewActiveState(data *pb.ActiveState) *ActiveState { - return &ActiveState{data: data} -} - -// Proto returns the underlying protobuf data within a state primitive. -func (a *ActiveState) Proto() *pb.ActiveState { - return a.data -} - -// Marshal encodes active state object into the wire format. -func (a *ActiveState) Marshal() ([]byte, error) { - return proto.Marshal(a.data) -} - -// Hash serializes the active state object then uses -// blake2b to hash the serialized object. -func (a *ActiveState) Hash() ([32]byte, error) { - data, err := proto.Marshal(a.data) - if err != nil { - return [32]byte{}, err - } - return hashutil.Hash(data), nil -} - -// CopyState returns a deep copy of the current active state. -func (a *ActiveState) CopyState() *ActiveState { - pendingAttestations := make([]*pb.AggregatedAttestation, len(a.data.PendingAttestations)) - for index, pendingAttestation := range a.data.PendingAttestations { - pendingAttestations[index] = &pb.AggregatedAttestation{ - Slot: pendingAttestation.GetSlot(), - Shard: pendingAttestation.GetShard(), - JustifiedSlot: pendingAttestation.GetJustifiedSlot(), - JustifiedBlockHash: pendingAttestation.GetJustifiedBlockHash(), - ShardBlockHash: pendingAttestation.GetShardBlockHash(), - AttesterBitfield: pendingAttestation.GetAttesterBitfield(), - ObliqueParentHashes: pendingAttestation.GetObliqueParentHashes(), - AggregateSig: pendingAttestation.GetAggregateSig(), - } - } - - recentBlockHashes := make([][]byte, len(a.data.RecentBlockHashes)) - copy(recentBlockHashes, a.data.RecentBlockHashes) - - pendingSpecials := make([]*pb.SpecialRecord, len(a.data.PendingSpecials)) - for index, pendingSpecial := range a.data.PendingSpecials { - pendingSpecials[index] = &pb.SpecialRecord{ - Kind: pendingSpecial.GetKind(), - Data: pendingSpecial.GetData(), - } - } - randaoMix := a.RandaoMix() - - newA := ActiveState{ - data: &pb.ActiveState{ - PendingAttestations: pendingAttestations, - RecentBlockHashes: recentBlockHashes, - PendingSpecials: pendingSpecials, - RandaoMix: randaoMix[:], - }, - } - - return &newA -} - -// PendingAttestations returns attestations that have not yet been processed. -func (a *ActiveState) PendingAttestations() []*pb.AggregatedAttestation { - return a.data.PendingAttestations -} - -// PendingSpecials returns special records that have not yet been processed. -func (a *ActiveState) PendingSpecials() []*pb.SpecialRecord { - return a.data.PendingSpecials -} - -// RecentBlockHashes returns the most recent 2*EPOCH_LENGTH block hashes. -func (a *ActiveState) RecentBlockHashes() [][32]byte { - var blockhashes [][32]byte - for _, hash := range a.data.RecentBlockHashes { - blockhashes = append(blockhashes, common.BytesToHash(hash)) - } - return blockhashes -} - -// RandaoMix tracks the current RANDAO state. -func (a *ActiveState) RandaoMix() [32]byte { - var h [32]byte - copy(h[:], a.data.RandaoMix) - return h -} - -// UpdateAttestations returns a new state with the provided attestations. -func (a *ActiveState) UpdateAttestations(attestations []*pb.AggregatedAttestation) *ActiveState { - newState := a.CopyState() - - newState.data.PendingAttestations = append(newState.data.PendingAttestations, attestations...) - return newState -} - -// clearAttestations removes attestations older than last state recalc slot. -func (a *ActiveState) clearAttestations(lastStateRecalc uint64) { - existing := a.data.PendingAttestations - update := make([]*pb.AggregatedAttestation, 0, len(existing)) - for _, a := range existing { - if a.GetSlot() >= lastStateRecalc { - update = append(update, a) - } - } - - a.data.PendingAttestations = update -} - -// calculateNewBlockHashes builds a new slice of recent block hashes with the -// provided block and the parent slot number. -// -// The algorithm is: -// 1) shift the array by block.SlotNumber - parentSlot (i.e. truncate the -// first by the number of slots that have occurred between the block and -// its parent). -// -// 2) fill the array with the parent block hash for all values between the parent -// slot and the block slot. -// -// Computation of the active state hash depends on this feature that slots with -// missing blocks have the block hash of the next block hash in the chain. -// -// For example, if we have a segment of recent block hashes that look like this -// [0xF, 0x7, 0x0, 0x0, 0x5] -// -// Where 0x0 is an empty or missing hash where no block was produced in the -// alloted slot. When storing the list (or at least when computing the hash of -// the active state), the list should be backfilled as such: -// -// [0xF, 0x7, 0x5, 0x5, 0x5] -// -// This method does not mutate the active state. -func (a *ActiveState) calculateNewBlockHashes(block *Block, parentSlot uint64) ([][]byte, error) { - distance := block.SlotNumber() - parentSlot - existing := a.data.RecentBlockHashes - update := existing[distance:] - for len(update) < 2*int(params.BeaconConfig().CycleLength) { - update = append(update, block.data.AncestorHashes[0]) - } - - return update, nil -} - -// CalculateNewActiveState returns the active state for `block` based on its own state. -// This method should not modify its own state. -func (a *ActiveState) CalculateNewActiveState( - block *Block, - cState *CrystallizedState, - parentSlot uint64) (*ActiveState, error) { - var err error - - newState := a.CopyState() - - newState.clearAttestations(cState.LastStateRecalculationSlot()) - - // Derive the new set of recent block hashes. - newState.data.RecentBlockHashes, err = newState.calculateNewBlockHashes(block, parentSlot) - if err != nil { - return nil, fmt.Errorf("failed to update recent block hashes: %v", err) - } - - log.Debugf("Calculating new active state. Crystallized state lastStateRecalc is %d", cState.LastStateRecalculationSlot()) - - newRandao := setRandaoMix(block.RandaoReveal(), a.RandaoMix()) - newState.data.RandaoMix = newRandao[:] - - return newState, nil -} - -// GetSignedParentHashes returns all the parent hashes stored in active state up to last cycle length. -func (a *ActiveState) GetSignedParentHashes(block *Block, attestation *pb.AggregatedAttestation) ([][32]byte, error) { - recentBlockHashes := a.RecentBlockHashes() - obliqueParentHashes := attestation.ObliqueParentHashes - earliestSlot := int(block.SlotNumber()) - len(recentBlockHashes) - - startIdx := int(attestation.Slot) - earliestSlot - int(params.BeaconConfig().CycleLength) + 1 - endIdx := startIdx - len(attestation.ObliqueParentHashes) + int(params.BeaconConfig().CycleLength) - if startIdx < 0 || endIdx > len(recentBlockHashes) || endIdx <= startIdx { - return nil, fmt.Errorf("attempt to fetch recent blockhashes from %d to %d invalid", startIdx, endIdx) - } - - hashes := make([][32]byte, 0, params.BeaconConfig().CycleLength) - for i := startIdx; i < endIdx; i++ { - hashes = append(hashes, recentBlockHashes[i]) - } - - for i := 0; i < len(obliqueParentHashes); i++ { - hash := common.BytesToHash(obliqueParentHashes[i]) - hashes = append(hashes, hash) - } - - return hashes, nil -} - -// setRandaoMix sets the current randao seed into active state. -func setRandaoMix(blockRandao [32]byte, aStateRandao [32]byte) [32]byte { - for i, b := range blockRandao { - aStateRandao[i] ^= b - } - return aStateRandao -} diff --git a/beacon-chain/types/block.go b/beacon-chain/types/block.go deleted file mode 100644 index 9e60aaafef..0000000000 --- a/beacon-chain/types/block.go +++ /dev/null @@ -1,344 +0,0 @@ -// Package types defines the essential types used throughout the beacon-chain. -package types - -import ( - "bytes" - "fmt" - "time" - - "github.com/ethereum/go-ethereum/common" - "github.com/golang/protobuf/proto" - "github.com/golang/protobuf/ptypes" - v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" - "github.com/prysmaticlabs/prysm/beacon-chain/utils" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" - "github.com/prysmaticlabs/prysm/shared/bitutil" - "github.com/prysmaticlabs/prysm/shared/hashutil" - "github.com/prysmaticlabs/prysm/shared/params" - "github.com/sirupsen/logrus" -) - -var log = logrus.WithField("prefix", "types") - -var clock utils.Clock = &utils.RealClock{} - -// Block defines a beacon chain core primitive. -type Block struct { - data *pb.BeaconBlock -} - -type beaconDB interface { - HasBlock(h [32]byte) bool - ReadBlockVoteCache(blockHashes [][32]byte) (utils.BlockVoteCache, error) -} - -// NewBlock explicitly sets the data field of a block. -// Return block with default fields if data is nil. -func NewBlock(data *pb.BeaconBlock) *Block { - if data == nil { - var ancestorHashes = make([][]byte, 0, 32) - - //It is assumed when data==nil, you're asking for a Genesis Block - return &Block{ - data: &pb.BeaconBlock{ - AncestorHashes: ancestorHashes, - RandaoReveal: []byte{0}, - PowChainRef: []byte{0}, - ActiveStateRoot: []byte{0}, - CrystallizedStateRoot: []byte{0}, - Specials: []*pb.SpecialRecord{}, - }, - } - } - - return &Block{data: data} -} - -// NewGenesisBlock returns the canonical, genesis block for the beacon chain protocol. -func NewGenesisBlock(activeStateRoot [32]byte, crystallizedStateRoot [32]byte) *Block { - // Genesis time here is static so error can be safely ignored. - // #nosec G104 - protoGenesis, _ := ptypes.TimestampProto(params.BeaconConfig().GenesisTime) - gb := NewBlock(nil) - gb.data.Timestamp = protoGenesis - - gb.data.ActiveStateRoot = activeStateRoot[:] - gb.data.CrystallizedStateRoot = crystallizedStateRoot[:] - return gb -} - -// Proto returns the underlying protobuf data within a block primitive. -func (b *Block) Proto() *pb.BeaconBlock { - return b.data -} - -// Marshal encodes block object into the wire format. -func (b *Block) Marshal() ([]byte, error) { - return proto.Marshal(b.data) -} - -// Hash generates the blake2b hash of the block -func (b *Block) Hash() ([32]byte, error) { - data, err := proto.Marshal(b.data) - if err != nil { - return [32]byte{}, fmt.Errorf("could not marshal block proto data: %v", err) - } - return hashutil.Hash(data), nil -} - -// ParentHash corresponding to parent beacon block. -func (b *Block) ParentHash() [32]byte { - var h [32]byte - copy(h[:], b.data.AncestorHashes[0]) - return h -} - -// SlotNumber of the beacon block. -func (b *Block) SlotNumber() uint64 { - return b.data.Slot -} - -// PowChainRef returns a keccak256 hash corresponding to a PoW chain block. -func (b *Block) PowChainRef() common.Hash { - return common.BytesToHash(b.data.PowChainRef) -} - -// RandaoReveal returns the blake2b randao hash. -func (b *Block) RandaoReveal() [32]byte { - var h [32]byte - copy(h[:], b.data.RandaoReveal) - return h -} - -// ActiveStateRoot returns the active state hash. -func (b *Block) ActiveStateRoot() [32]byte { - var h [32]byte - copy(h[:], b.data.ActiveStateRoot) - return h -} - -// CrystallizedStateRoot returns the crystallized state hash. -func (b *Block) CrystallizedStateRoot() [32]byte { - var h [32]byte - copy(h[:], b.data.CrystallizedStateRoot) - return h -} - -// AttestationCount returns the number of attestations. -func (b *Block) AttestationCount() int { - return len(b.data.Attestations) -} - -// Attestations returns an array of attestations in the block. -func (b *Block) Attestations() []*pb.AggregatedAttestation { - return b.data.Attestations -} - -// Specials returns an array of special objects in the block. -func (b *Block) Specials() []*pb.SpecialRecord { - return b.data.Specials -} - -// Timestamp returns the Go type time.Time from the protobuf type contained in the block. -func (b *Block) Timestamp() (time.Time, error) { - return ptypes.Timestamp(b.data.Timestamp) -} - -// isSlotValid compares the slot to the system clock to determine if the block is valid. -func (b *Block) isSlotValid(genesisTime time.Time) bool { - slotDuration := time.Duration(b.SlotNumber()*params.BeaconConfig().SlotDuration) * time.Second - validTimeThreshold := genesisTime.Add(slotDuration) - return clock.Now().After(validTimeThreshold) -} - -// IsValid is called to decide if an incoming p2p block can be processed. It checks for following conditions: -// 1.) Ensure local time is large enough to process this block's slot. -// 2.) Verify that the parent block's proposer's attestation is included. -func (b *Block) IsValid( - db beaconDB, - aState *ActiveState, - cState *CrystallizedState, - parentSlot uint64, - genesisTime time.Time) bool { - _, err := b.Hash() - if err != nil { - log.Errorf("Could not hash incoming block: %v", err) - return false - } - - if b.SlotNumber() == 0 { - log.Error("Cannot process a genesis block: received block with slot 0") - return false - } - - if !b.isSlotValid(genesisTime) { - log.Errorf("Slot of block is too high: %d", b.SlotNumber()) - return false - } - - if !b.doesParentProposerExist(cState, parentSlot) || !b.areAttestationsValid(db, aState, cState, parentSlot) { - log.Error("Invalid attestation") - return false - } - - _, proposerIndex, err := v.ProposerShardAndIndex( - cState.ShardAndCommitteesForSlots(), - cState.LastStateRecalculationSlot(), - b.SlotNumber()) - if err != nil { - log.Errorf("Could not get proposer index: %v", err) - return false - } - - cStateProposerRandaoSeed := cState.Validators()[proposerIndex].RandaoCommitment - blockRandaoReveal := b.RandaoReveal() - - // If this is a block created by the simulator service (while in development - // mode), we skip the RANDAO validation condition. - isSimulatedBlock := bytes.Equal(blockRandaoReveal[:], params.BeaconConfig().SimulatedBlockRandao[:]) - if !isSimulatedBlock && !b.isRandaoValid(cStateProposerRandaoSeed) { - log.Errorf("Pre-image of %#x is %#x, Got: %#x", blockRandaoReveal[:], hashutil.Hash(blockRandaoReveal[:]), cStateProposerRandaoSeed) - return false - } - - return true -} - -func (b *Block) areAttestationsValid(db beaconDB, aState *ActiveState, cState *CrystallizedState, parentSlot uint64) bool { - for index, attestation := range b.Attestations() { - if !b.isAttestationValid(index, db, aState, cState, parentSlot) { - log.Errorf("Attestation invalid: %v", attestation) - return false - } - } - - return true -} - -func (b *Block) doesParentProposerExist(cState *CrystallizedState, parentSlot uint64) bool { - _, parentProposerIndex, err := v.ProposerShardAndIndex( - cState.ShardAndCommitteesForSlots(), - cState.LastStateRecalculationSlot(), - parentSlot) - if err != nil { - log.Errorf("Could not get proposer index: %v", err) - return false - } - - // verify proposer from last slot is in the first attestation object in AggregatedAttestation. - if isBitSet, err := bitutil.CheckBit(b.Attestations()[0].AttesterBitfield, int(parentProposerIndex)); !isBitSet { - log.Errorf("Could not locate proposer in the first attestation of AttestionRecord: %v", err) - return false - } - - return true -} - -// UpdateAncestorHashes updates the skip list of ancestor block hashes. -// i'th item is 2**i'th ancestor for i = 0, ..., 31. -// TODO(#712): Use this for per-block processing check for incoming block's ancestor hashes. -func UpdateAncestorHashes(parentAncestorHashes [][32]byte, parentSlotNum uint64, parentHash [32]byte) [][32]byte { - newAncestorHashes := parentAncestorHashes - for i := range parentAncestorHashes { - if (parentSlotNum % (1 << uint64(i))) == 0 { - newAncestorHashes[i] = parentHash - } - } - return newAncestorHashes -} - -// isAttestationValid validates an attestation in a block. -// Attestations are cross-checked against validators in CrystallizedState.ShardAndCommitteesForSlots. -// In addition, the signature is verified by constructing the list of parent hashes using ActiveState.RecentBlockHashes. -func (b *Block) isAttestationValid(attestationIndex int, db beaconDB, aState *ActiveState, cState *CrystallizedState, parentSlot uint64) bool { - // Validate attestation's slot number has is within range of incoming block number. - attestation := b.Attestations()[attestationIndex] - if !isAttestationSlotNumberValid(attestation.Slot, parentSlot) { - log.Errorf("invalid attestation slot %d", attestation.Slot) - return false - } - - if attestation.JustifiedSlot > cState.LastJustifiedSlot() { - log.Errorf("attestation's justified slot has to be earlier or equal to crystallized state's last justified slot. Found: %d. Want <=: %d", - attestation.JustifiedSlot, - cState.LastJustifiedSlot()) - return false - } - - hash := [32]byte{} - copy(hash[:], attestation.JustifiedBlockHash) - blockInChain := db.HasBlock(hash) - - if !blockInChain { - log.Errorf("the attestion's justifed block hash has to be in the current chain, but was not found. Justified block hash: %v", - attestation.JustifiedBlockHash) - return false - } - - // Get all the block hashes up to cycle length. - parentHashes, err := aState.GetSignedParentHashes(b, attestation) - if err != nil { - log.Errorf("Unable to get signed parent hashes: %v", err) - return false - } - - attesterIndices, err := cState.AttesterIndices(attestation) - if err != nil { - log.Errorf("Unable to get validator committee: %v", err) - return false - } - - // Verify attester bitfields matches crystallized state's prev computed bitfield. - if !v.AreAttesterBitfieldsValid(attestation, attesterIndices) { - log.Error("Unable to match attester bitfield with shard and committee bitfield") - return false - } - - forkVersion := cState.PostForkVersion() - if attestation.Slot < cState.ForkSlotNumber() { - forkVersion = cState.PreForkVersion() - } - - // TODO(#258): Generate validators aggregated pub key. - - attestationMsg := AttestationMsg( - parentHashes, - attestation.ShardBlockHash, - attestation.Slot, - attestation.Shard, - attestation.JustifiedSlot, - forkVersion) - - log.Debugf("Attestation message for shard: %v, slot %v is %#x", - attestation.Shard, attestation.Slot, attestationMsg) - - // TODO(#258): Verify msgHash against aggregated pub key and aggregated signature. - return true -} - -// isRandaoValid verifies the validity of randao from block by comparing it with proposer's randao -// from crystallized state. -func (b *Block) isRandaoValid(cStateRandao []byte) bool { - var h [32]byte - copy(h[:], cStateRandao) - blockRandaoReveal := b.RandaoReveal() - return hashutil.Hash(blockRandaoReveal[:]) == h -} - -func isAttestationSlotNumberValid(attestationSlot uint64, parentSlot uint64) bool { - if parentSlot != 0 && attestationSlot > parentSlot { - log.Debugf("attestation slot number can't be higher than parent block's slot number. Found: %d, Needed lower than: %d", - attestationSlot, - parentSlot) - return false - } - - if parentSlot >= params.BeaconConfig().CycleLength-1 && attestationSlot < parentSlot-params.BeaconConfig().CycleLength+1 { - log.Debugf("attestation slot number can't be lower than parent block's slot number by one CycleLength. Found: %d, Needed greater than: %d", - attestationSlot, - parentSlot-params.BeaconConfig().CycleLength+1) - return false - } - - return true -} diff --git a/beacon-chain/types/block_test.go b/beacon-chain/types/block_test.go deleted file mode 100644 index 169a417cf2..0000000000 --- a/beacon-chain/types/block_test.go +++ /dev/null @@ -1,253 +0,0 @@ -package types - -import ( - "bytes" - "testing" - - "github.com/golang/protobuf/ptypes" - "github.com/prysmaticlabs/prysm/beacon-chain/utils" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" - "github.com/prysmaticlabs/prysm/shared/hashutil" - "github.com/prysmaticlabs/prysm/shared/params" - "github.com/sirupsen/logrus" -) - -func init() { - logrus.SetLevel(logrus.DebugLevel) -} - -type mockDB struct { - blockVoteCache utils.BlockVoteCache -} - -func (f *mockDB) HasBlock(h [32]byte) bool { - return true -} - -func (f *mockDB) ReadBlockVoteCache(blockHashes [][32]byte) (utils.BlockVoteCache, error) { - return f.blockVoteCache, nil -} - -func (f *mockDB) loadMockBlockVoteCache(blockVoteCache utils.BlockVoteCache) { - f.blockVoteCache = blockVoteCache -} - -func TestGenesisBlock(t *testing.T) { - aStateHash := [32]byte{0} - cStateHash := [32]byte{1} - b1 := NewGenesisBlock(aStateHash, cStateHash) - b2 := NewGenesisBlock(aStateHash, cStateHash) - - // We ensure that initializing a proto timestamp from - // genesis time will lead to no error. - if _, err := ptypes.TimestampProto(params.BeaconConfig().GenesisTime); err != nil { - t.Errorf("could not create proto timestamp, expected no error: %v", err) - } - - h1, err1 := b1.Hash() - h2, err2 := b2.Hash() - if err1 != nil || err2 != nil { - t.Fatalf("failed to hash genesis block: %v %v", err1, err2) - } - - if h1 != h2 { - t.Fatalf("genesis block hash should be identical: %#x %#x", h1, h2) - } - - if b1.data.AncestorHashes == nil { - t.Fatal("genesis block missing ParentHash field") - } - - if b1.Specials() == nil { - t.Fatal("genesis block missing Special field") - } - - if b1.data.RandaoReveal == nil { - t.Fatal("genesis block missing RandaoReveal field") - } - - if b1.data.PowChainRef == nil { - t.Fatal("genesis block missing PowChainRef field") - } - - if !bytes.Equal(b1.data.ActiveStateRoot, aStateHash[:]) { - t.Fatal("genesis block ActiveStateHash isn't initialized correctly") - } - - if !bytes.Equal(b1.data.CrystallizedStateRoot, cStateHash[:]) { - t.Fatal("genesis block CrystallizedStateHash isn't initialized correctly") - } - - b3 := NewBlock(nil) - h3, err3 := b3.Hash() - if err3 != nil { - t.Fatalf("failed to hash genesis block: %v", err3) - } - - if h1 == h3 { - t.Fatalf("genesis block and new block should not have identical hash: %#x", h1) - } -} - -func TestBlockValidity(t *testing.T) { - cState, err := NewGenesisCrystallizedState(nil) - if err != nil { - t.Fatalf("failed to generate crystallized state: %v", err) - } - - recentBlockHashes := make([][]byte, 2*params.BeaconConfig().CycleLength) - for i := 0; i < 2*int(params.BeaconConfig().CycleLength); i++ { - recentBlockHashes = append(recentBlockHashes, make([]byte, 32)) - } - aState := NewActiveState(&pb.ActiveState{ - RecentBlockHashes: recentBlockHashes, - }) - - randaoPreCommit := [32]byte{'A'} - hashedRandaoPreCommit := hashutil.Hash(randaoPreCommit[:]) - cState.data.Validators[1].RandaoCommitment = hashedRandaoPreCommit[:] - - b := NewBlock(&pb.BeaconBlock{ - Slot: 1, - RandaoReveal: randaoPreCommit[:], - Attestations: []*pb.AggregatedAttestation{ - { - Slot: 0, - Shard: 1, - JustifiedSlot: 0, - AttesterBitfield: []byte{128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - }, - }, - }) - - parentSlot := uint64(0) - db := &mockDB{} - - if !b.isAttestationValid(0, db, aState, cState, parentSlot) { - t.Fatalf("failed attestation validation") - } - - genesisTime := params.BeaconConfig().GenesisTime - if !b.IsValid(db, aState, cState, parentSlot, genesisTime) { - t.Fatalf("failed block validation") - } -} - -func TestBlockValidityNoParentProposer(t *testing.T) { - cState, err := NewGenesisCrystallizedState(nil) - if err != nil { - t.Fatalf("failed to generate crystallized state: %v", err) - } - - recentBlockHashes := make([][]byte, 2*params.BeaconConfig().CycleLength) - for i := 0; i < 2*int(params.BeaconConfig().CycleLength); i++ { - recentBlockHashes = append(recentBlockHashes, make([]byte, 32)) - } - - aState := NewActiveState(&pb.ActiveState{ - RecentBlockHashes: recentBlockHashes, - }) - parentSlot := uint64(1) - db := &mockDB{} - - // Test case with invalid RANDAO reveal. - badRandaoBlock := NewBlock(&pb.BeaconBlock{ - Slot: 2, - RandaoReveal: []byte{'B'}, - Attestations: []*pb.AggregatedAttestation{ - { - Slot: 0, - Shard: 1, - JustifiedSlot: 0, - AttesterBitfield: []byte{64, 0}, - }, - }, - }) - genesisTime := params.BeaconConfig().GenesisTime - if badRandaoBlock.IsValid(db, aState, cState, parentSlot, genesisTime) { - t.Fatalf("should have failed doesParentProposerExist") - } -} - -func TestBlockValidityInvalidRandao(t *testing.T) { - cState, err := NewGenesisCrystallizedState(nil) - if err != nil { - t.Fatalf("failed to generate crystallized state: %v", err) - } - - recentBlockHashes := make([][]byte, 2*params.BeaconConfig().CycleLength) - for i := 0; i < 2*int(params.BeaconConfig().CycleLength); i++ { - recentBlockHashes = append(recentBlockHashes, make([]byte, 32)) - } - - aState := NewActiveState(&pb.ActiveState{ - RecentBlockHashes: recentBlockHashes, - }) - parentSlot := uint64(0) - db := &mockDB{} - - // Test case with invalid RANDAO reveal. - badRandaoBlock := NewBlock(&pb.BeaconBlock{ - Slot: 1, - RandaoReveal: []byte{'B'}, - Attestations: []*pb.AggregatedAttestation{ - { - Slot: 0, - Shard: 1, - JustifiedSlot: 0, - AttesterBitfield: []byte{64, 0}, - }, - }, - }) - - genesisTime := params.BeaconConfig().GenesisTime - if badRandaoBlock.IsValid(db, aState, cState, parentSlot, genesisTime) { - t.Fatalf("should have failed with invalid RANDAO") - } -} - -func TestIsAttestationSlotNumberValid(t *testing.T) { - if isAttestationSlotNumberValid(2, 1) { - t.Errorf("attestation slot number can't be higher than parent block's slot number") - } - - if isAttestationSlotNumberValid(1, params.BeaconConfig().CycleLength+1) { - t.Errorf("attestation slot number can't be lower than parent block's slot number by one CycleLength and 1") - } - - if !isAttestationSlotNumberValid(2, 2) { - t.Errorf("attestation slot number could be less than or equal to parent block's slot number") - } - - if !isAttestationSlotNumberValid(2, 10) { - t.Errorf("attestation slot number could be less than or equal to parent block's slot number") - } -} - -func TestUpdateAncestorHashes(t *testing.T) { - parentHashes := make([][32]byte, 32) - for i := 0; i < 32; i++ { - parentHashes[i] = hashutil.Hash([]byte{byte(i)}) - } - - tests := []struct { - a uint64 - b [32]byte - c int - }{ - {a: 1, b: [32]byte{'a'}, c: 0}, - {a: 2, b: [32]byte{'b'}, c: 1}, - {a: 4, b: [32]byte{'c'}, c: 2}, - {a: 8, b: [32]byte{'d'}, c: 3}, - {a: 16, b: [32]byte{'e'}, c: 4}, - {a: 1 << 29, b: [32]byte{'f'}, c: 29}, - {a: 1 << 30, b: [32]byte{'g'}, c: 30}, - {a: 1 << 31, b: [32]byte{'h'}, c: 31}, - } - for _, tt := range tests { - if UpdateAncestorHashes(parentHashes, tt.a, tt.b)[tt.c] != tt.b { - t.Errorf("Failed to update ancestor hash at index %d. Wanted: %v, got: %v", tt.c, tt.b, UpdateAncestorHashes(parentHashes, tt.a, tt.b)[tt.c]) - } - } -} diff --git a/beacon-chain/types/crystallized_state.go b/beacon-chain/types/crystallized_state.go deleted file mode 100644 index 7d01871f1c..0000000000 --- a/beacon-chain/types/crystallized_state.go +++ /dev/null @@ -1,451 +0,0 @@ -package types - -import ( - "fmt" - - "github.com/gogo/protobuf/proto" - "github.com/prysmaticlabs/prysm/beacon-chain/core/incentives" - "github.com/prysmaticlabs/prysm/beacon-chain/core/state" - v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" - "github.com/prysmaticlabs/prysm/shared/hashutil" - "github.com/prysmaticlabs/prysm/shared/params" -) - -var shardCount = params.BeaconConfig().ShardCount - -// CrystallizedState contains fields of every Slot state, -// it changes every Slot. -type CrystallizedState struct { - data *pb.CrystallizedState -} - -// NewCrystallizedState creates a new crystallized state with a explicitly set data field. -func NewCrystallizedState(data *pb.CrystallizedState) *CrystallizedState { - return &CrystallizedState{data: data} -} - -// NewGenesisCrystallizedState initializes the crystallized state for slot 0. -func NewGenesisCrystallizedState(genesisValidators []*pb.ValidatorRecord) (*CrystallizedState, error) { - // We seed the genesis crystallized state with a bunch of validators to - // bootstrap the system. - var err error - if genesisValidators == nil { - genesisValidators = v.InitialValidators() - - } - // Bootstrap attester indices for slots, each slot contains an array of attester indices. - shardAndCommitteesForSlots, err := v.InitialShardAndCommitteesForSlots(genesisValidators) - if err != nil { - return nil, err - } - - // Bootstrap cross link records. - var crosslinks []*pb.CrosslinkRecord - for i := uint64(0); i < shardCount; i++ { - crosslinks = append(crosslinks, &pb.CrosslinkRecord{ - ShardBlockHash: make([]byte, 0, 32), - Slot: 0, - }) - } - - return &CrystallizedState{ - data: &pb.CrystallizedState{ - LastStateRecalculationSlot: 0, - JustifiedStreak: 0, - LastJustifiedSlot: 0, - LastFinalizedSlot: 0, - ValidatorSetChangeSlot: 0, - ForkSlotNumber: 0, - Crosslinks: crosslinks, - Validators: genesisValidators, - ShardAndCommitteesForSlots: shardAndCommitteesForSlots, - ValidatorSetDeltaHashChain: make([]byte, 0, 32), - PreForkVersion: params.BeaconConfig().InitialForkVersion, - PostForkVersion: params.BeaconConfig().InitialForkVersion, - }, - }, nil -} - -// Proto returns the underlying protobuf data within a state primitive. -func (c *CrystallizedState) Proto() *pb.CrystallizedState { - return c.data -} - -// Marshal encodes crystallized state object into the wire format. -func (c *CrystallizedState) Marshal() ([]byte, error) { - return proto.Marshal(c.data) -} - -// Hash serializes the crystallized state object then uses -// blake2b to hash the serialized object. -func (c *CrystallizedState) Hash() ([32]byte, error) { - data, err := proto.Marshal(c.data) - if err != nil { - return [32]byte{}, err - } - return hashutil.Hash(data), nil -} - -// CopyState returns a deep copy of the current state. -func (c *CrystallizedState) CopyState() *CrystallizedState { - crosslinks := make([]*pb.CrosslinkRecord, len(c.Crosslinks())) - for index, crossLink := range c.Crosslinks() { - crosslinks[index] = &pb.CrosslinkRecord{ - ShardBlockHash: crossLink.GetShardBlockHash(), - Slot: crossLink.GetSlot(), - } - } - - validators := make([]*pb.ValidatorRecord, len(c.Validators())) - for index, validator := range c.Validators() { - validators[index] = &pb.ValidatorRecord{ - Pubkey: validator.GetPubkey(), - WithdrawalShard: validator.GetWithdrawalShard(), - WithdrawalAddress: validator.GetWithdrawalAddress(), - RandaoCommitment: validator.GetRandaoCommitment(), - Balance: validator.GetBalance(), - Status: validator.GetStatus(), - ExitSlot: validator.GetExitSlot(), - } - } - - shardAndCommitteesForSlots := make([]*pb.ShardAndCommitteeArray, len(c.ShardAndCommitteesForSlots())) - for index, shardAndCommitteesForSlot := range c.ShardAndCommitteesForSlots() { - shardAndCommittees := make([]*pb.ShardAndCommittee, len(shardAndCommitteesForSlot.GetArrayShardAndCommittee())) - for index, shardAndCommittee := range shardAndCommitteesForSlot.GetArrayShardAndCommittee() { - shardAndCommittees[index] = &pb.ShardAndCommittee{ - Shard: shardAndCommittee.GetShard(), - Committee: shardAndCommittee.GetCommittee(), - } - } - shardAndCommitteesForSlots[index] = &pb.ShardAndCommitteeArray{ - ArrayShardAndCommittee: shardAndCommittees, - } - } - - newState := CrystallizedState{&pb.CrystallizedState{ - LastStateRecalculationSlot: c.LastStateRecalculationSlot(), - JustifiedStreak: c.JustifiedStreak(), - LastJustifiedSlot: c.LastJustifiedSlot(), - LastFinalizedSlot: c.LastFinalizedSlot(), - ValidatorSetChangeSlot: c.ValidatorSetChangeSlot(), - Crosslinks: crosslinks, - Validators: validators, - ShardAndCommitteesForSlots: shardAndCommitteesForSlots, - DepositsPenalizedInPeriod: c.DepositsPenalizedInPeriod(), - ValidatorSetDeltaHashChain: c.data.ValidatorSetDeltaHashChain, - PreForkVersion: c.data.PreForkVersion, - PostForkVersion: c.data.PostForkVersion, - ForkSlotNumber: c.data.ForkSlotNumber, - }} - - return &newState -} - -// LastStateRecalculationSlot returns when the last time crystallized state recalculated. -func (c *CrystallizedState) LastStateRecalculationSlot() uint64 { - return c.data.LastStateRecalculationSlot -} - -// JustifiedStreak returns number of consecutive justified slots ending at head. -func (c *CrystallizedState) JustifiedStreak() uint64 { - return c.data.JustifiedStreak -} - -// LastJustifiedSlot return the last justified slot of the beacon chain. -func (c *CrystallizedState) LastJustifiedSlot() uint64 { - return c.data.LastJustifiedSlot -} - -// LastFinalizedSlot returns the last finalized Slot of the beacon chain. -func (c *CrystallizedState) LastFinalizedSlot() uint64 { - return c.data.LastFinalizedSlot -} - -// TotalDeposits returns total balance of the deposits of the active validators. -func (c *CrystallizedState) TotalDeposits() uint64 { - validators := c.data.Validators - totalDeposit := v.TotalActiveValidatorDeposit(validators) - return totalDeposit -} - -// ValidatorSetChangeSlot returns the slot of last time validator set changes. -func (c *CrystallizedState) ValidatorSetChangeSlot() uint64 { - return c.data.ValidatorSetChangeSlot -} - -// ShardAndCommitteesForSlots returns the shard committee object. -func (c *CrystallizedState) ShardAndCommitteesForSlots() []*pb.ShardAndCommitteeArray { - return c.data.ShardAndCommitteesForSlots -} - -// Crosslinks returns the cross link records of the all the shards. -func (c *CrystallizedState) Crosslinks() []*pb.CrosslinkRecord { - return c.data.Crosslinks -} - -// Validators returns list of validators. -func (c *CrystallizedState) Validators() []*pb.ValidatorRecord { - return c.data.Validators -} - -// DepositsPenalizedInPeriod returns total deposits penalized in the given withdrawal period. -func (c *CrystallizedState) DepositsPenalizedInPeriod() []uint32 { - return c.data.DepositsPenalizedInPeriod -} - -// ForkSlotNumber returns the slot of last fork. -func (c *CrystallizedState) ForkSlotNumber() uint64 { - return c.data.ForkSlotNumber -} - -// PreForkVersion returns the last pre fork version. -func (c *CrystallizedState) PreForkVersion() uint32 { - return c.data.PreForkVersion -} - -// PostForkVersion returns the last post fork version. -func (c *CrystallizedState) PostForkVersion() uint32 { - return c.data.PostForkVersion -} - -// IsCycleTransition checks if a new cycle has been reached. At that point, -// a new crystallized state and active state transition will occur. -func (c *CrystallizedState) IsCycleTransition(slotNumber uint64) bool { - return slotNumber >= c.LastStateRecalculationSlot()+params.BeaconConfig().CycleLength -} - -// GetShardsAndCommitteesForSlot returns the shard committees of a given slot. -func (c *CrystallizedState) GetShardsAndCommitteesForSlot(slotNumber uint64) (*pb.ShardAndCommitteeArray, error) { - return v.GetShardAndCommitteesForSlot(c.ShardAndCommitteesForSlots(), c.LastStateRecalculationSlot(), slotNumber) -} - -// isValidatorSetChange checks if a validator set change transition can be processed. At that point, -// validator shuffle will occur. -func (c *CrystallizedState) isValidatorSetChange(slotNumber uint64) bool { - if c.LastFinalizedSlot() <= c.ValidatorSetChangeSlot() { - return false - } - if slotNumber-c.ValidatorSetChangeSlot() < params.BeaconConfig().MinValidatorSetChangeInterval { - return false - } - - shardProcessed := map[uint64]bool{} - - for _, shardAndCommittee := range c.ShardAndCommitteesForSlots() { - for _, committee := range shardAndCommittee.ArrayShardAndCommittee { - shardProcessed[committee.Shard] = true - } - } - - crosslinks := c.Crosslinks() - for shard := range shardProcessed { - if c.ValidatorSetChangeSlot() >= crosslinks[shard].Slot { - return false - } - } - return true -} - -// AttesterIndices fetches the attesters for a given attestation record. -func (c *CrystallizedState) AttesterIndices(attestation *pb.AggregatedAttestation) ([]uint32, error) { - shardCommittees, err := v.GetShardAndCommitteesForSlot( - c.ShardAndCommitteesForSlots(), - c.LastStateRecalculationSlot(), - attestation.GetSlot()) - if err != nil { - return nil, fmt.Errorf("unable to fetch ShardAndCommittees for slot %d: %v", attestation.Slot, err) - } - - shardCommitteesArray := shardCommittees.ArrayShardAndCommittee - for _, shardCommittee := range shardCommitteesArray { - if attestation.Shard == shardCommittee.Shard { - return shardCommittee.Committee, nil - } - } - - return nil, fmt.Errorf("unable to find committee for shard %d", attestation.Shard) -} - -// NewStateRecalculations computes the new crystallized state, given the previous crystallized state -// and the current active state. This method is called during a cycle transition. -// We also check for validator set change transition and compute for new committees if necessary during this transition. -func (c *CrystallizedState) NewStateRecalculations(aState *ActiveState, block *Block, db beaconDB) (*CrystallizedState, error) { - var lastStateRecalculationSlotCycleBack uint64 - var err error - - newState := c.CopyState() - justifiedStreak := c.JustifiedStreak() - justifiedSlot := c.LastJustifiedSlot() - finalizedSlot := c.LastFinalizedSlot() - timeSinceFinality := block.SlotNumber() - newState.LastFinalizedSlot() - recentBlockHashes := aState.RecentBlockHashes() - newState.data.Validators = v.CopyValidators(newState.Validators()) - - if c.LastStateRecalculationSlot() < params.BeaconConfig().CycleLength { - lastStateRecalculationSlotCycleBack = 0 - } else { - lastStateRecalculationSlotCycleBack = c.LastStateRecalculationSlot() - params.BeaconConfig().CycleLength - } - - // TODO(711): Need to implement clean-up mechanism for block vote cache. - blockVoteCache, err := db.ReadBlockVoteCache(recentBlockHashes[0:params.BeaconConfig().CycleLength]) - if err != nil { - return nil, err - } - - // walk through all the slots from LastStateRecalculationSlot - cycleLength to LastStateRecalculationSlot - 1. - for i := uint64(0); i < params.BeaconConfig().CycleLength; i++ { - var blockVoteBalance uint64 - - slot := lastStateRecalculationSlotCycleBack + i - blockHash := recentBlockHashes[i] - - blockVoteBalance, newState.data.Validators = incentives.TallyVoteBalances( - blockHash, - blockVoteCache, - newState.data.Validators, - v.ActiveValidatorIndices(newState.data.Validators), - v.TotalActiveValidatorDeposit(newState.data.Validators), - timeSinceFinality, - ) - - justifiedSlot, finalizedSlot, justifiedStreak = state.FinalizeAndJustifySlots(slot, justifiedSlot, finalizedSlot, - justifiedStreak, blockVoteBalance, c.TotalDeposits()) - } - - newState.data.Crosslinks, err = newState.processCrosslinks(aState.PendingAttestations(), newState.Validators(), block.SlotNumber()) - if err != nil { - return nil, err - } - - newState.data.LastJustifiedSlot = justifiedSlot - newState.data.LastFinalizedSlot = finalizedSlot - newState.data.JustifiedStreak = justifiedStreak - newState.data.LastStateRecalculationSlot = newState.LastStateRecalculationSlot() + params.BeaconConfig().CycleLength - - // Process the pending special records gathered from last cycle. - newState.data.Validators, err = state.ProcessSpecialRecords(block.SlotNumber(), newState.Validators(), aState.PendingSpecials()) - if err != nil { - return nil, err - } - - // Exit the validators when their balance fall below min online deposit size. - newState.data.Validators = v.CheckValidatorMinDeposit(newState.Validators(), block.SlotNumber()) - - newState.data.LastFinalizedSlot = finalizedSlot - // Entering new validator set change transition. - if newState.isValidatorSetChange(block.SlotNumber()) { - log.Info("Entering validator set change transition") - newState.data.ValidatorSetChangeSlot = newState.LastStateRecalculationSlot() - newState.data.ShardAndCommitteesForSlots, err = newState.newValidatorSetRecalculations(block.ParentHash()) - if err != nil { - return nil, err - } - - period := uint32(block.SlotNumber() / params.BeaconConfig().MinWithdrawalPeriod) - totalPenalties := newState.penalizedETH(period) - newState.data.Validators = v.ChangeValidators(block.SlotNumber(), totalPenalties, newState.Validators()) - } - - printCommittee(newState.data.ShardAndCommitteesForSlots) - - return newState, nil -} - -func printCommittee(shardAndCommittees []*pb.ShardAndCommitteeArray) { - log.Debug("Dumping shard committees") - for slot, shardCommittees := range shardAndCommittees { - for _, shardCommittee := range shardCommittees.ArrayShardAndCommittee { - log.Debugf("Committee slot: %d, committee shard: %d committee validator indices: %v", - slot, shardCommittee.Shard, shardCommittee.Committee) - } - } -} - -// newValidatorSetRecalculations recomputes the validator set. -func (c *CrystallizedState) newValidatorSetRecalculations(seed [32]byte) ([]*pb.ShardAndCommitteeArray, error) { - lastSlot := len(c.data.ShardAndCommitteesForSlots) - 1 - lastCommitteeFromLastSlot := len(c.ShardAndCommitteesForSlots()[lastSlot].ArrayShardAndCommittee) - 1 - crosslinkLastShard := c.ShardAndCommitteesForSlots()[lastSlot].ArrayShardAndCommittee[lastCommitteeFromLastSlot].Shard - crosslinkNextShard := (crosslinkLastShard + 1) % uint64(shardCount) - - newShardCommitteeArray, err := v.ShuffleValidatorsToCommittees( - seed, - c.data.Validators, - crosslinkNextShard, - ) - if err != nil { - return nil, err - } - - return append(c.data.ShardAndCommitteesForSlots[params.BeaconConfig().CycleLength:], newShardCommitteeArray...), nil -} - -// processCrosslinks checks if the proposed shard block has recevied -// 2/3 of the votes. If yes, we update crosslink record to point to -// the proposed shard block with latest beacon chain slot numbers. -func (c *CrystallizedState) processCrosslinks(pendingAttestations []*pb.AggregatedAttestation, - validators []*pb.ValidatorRecord, currentSlot uint64) ([]*pb.CrosslinkRecord, error) { - crosslinkRecords := c.data.Crosslinks - slot := c.LastStateRecalculationSlot() + params.BeaconConfig().CycleLength - - for _, attestation := range pendingAttestations { - indices, err := c.AttesterIndices(attestation) - if err != nil { - return nil, err - } - - totalBalance, voteBalance, err := v.VotedBalanceInAttestation(validators, indices, attestation) - if err != nil { - return nil, err - } - - _, err = incentives.ApplyCrosslinkRewardsAndPenalties( - crosslinkRecords, - currentSlot, - indices, - attestation, - validators, - v.TotalActiveValidatorDeposit(validators), - totalBalance, - voteBalance, - ) - if err != nil { - return nil, err - } - - crosslinkRecords = state.UpdateCrosslinks(slot, voteBalance, totalBalance, attestation, crosslinkRecords) - - } - return crosslinkRecords, nil -} - -func getPenaltyForPeriod(penalties []uint32, period uint32) uint64 { - numPeriods := uint32(len(penalties)) - if numPeriods < period+1 { - return 0 - } - - return uint64(penalties[period]) -} - -// penalizedETH calculates penalized total ETH during the last 3 withdrawal periods. -func (c *CrystallizedState) penalizedETH(period uint32) uint64 { - var totalPenalty uint64 - - penalties := c.DepositsPenalizedInPeriod() - - totalPenalty += getPenaltyForPeriod(penalties, period) - - if period >= 1 { - totalPenalty += getPenaltyForPeriod(penalties, period-1) - } - - if period >= 2 { - totalPenalty += getPenaltyForPeriod(penalties, period-2) - } - - return totalPenalty -} diff --git a/beacon-chain/types/crystallized_state_test.go b/beacon-chain/types/crystallized_state_test.go deleted file mode 100644 index 58c3459699..0000000000 --- a/beacon-chain/types/crystallized_state_test.go +++ /dev/null @@ -1,429 +0,0 @@ -package types - -import ( - "bytes" - "strconv" - "testing" - - v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators" - "github.com/prysmaticlabs/prysm/beacon-chain/utils" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" - "github.com/prysmaticlabs/prysm/shared/params" -) - -func TestGenesisCrystallizedState(t *testing.T) { - cState1, err1 := NewGenesisCrystallizedState(nil) - cState2, err2 := NewGenesisCrystallizedState(nil) - - if err1 != nil || err2 != nil { - t.Fatalf("Failed to initialize crystallized state: %v %v", err1, err2) - } - - h1, err1 := cState1.Hash() - h2, err2 := cState2.Hash() - - if err1 != nil || err2 != nil { - t.Fatalf("Failed to hash crystallized state: %v %v", err1, err2) - } - - if h1 != h2 { - t.Fatalf("Hash of two genesis crystallized states should be equal: %#x", h1) - } -} - -func TestCopyCrystallizedState(t *testing.T) { - cState1, err1 := NewGenesisCrystallizedState(nil) - cState2 := cState1.CopyState() - - if err1 != nil { - t.Fatalf("Failed to initialize crystallized state: %v", err1) - } - - cState1.data.LastStateRecalculationSlot = 40 - if cState1.LastStateRecalculationSlot() == cState2.LastStateRecalculationSlot() { - t.Fatalf("The Last State Recalculation Slot should not be equal: %d %d", - cState1.LastStateRecalculationSlot(), - cState2.LastStateRecalculationSlot(), - ) - } - - cState1.data.JustifiedStreak = 40 - if cState1.JustifiedStreak() == cState2.JustifiedStreak() { - t.Fatalf("The Justified Streak should not be equal: %d %d", - cState1.JustifiedStreak(), - cState2.JustifiedStreak(), - ) - } - - cState1.data.LastJustifiedSlot = 40 - if cState1.LastJustifiedSlot() == cState2.LastJustifiedSlot() { - t.Fatalf("The Last Justified Slot should not be equal: %d %d", - cState1.LastJustifiedSlot(), - cState2.LastJustifiedSlot(), - ) - } - - cState1.data.LastFinalizedSlot = 40 - if cState1.LastFinalizedSlot() == cState2.LastFinalizedSlot() { - t.Fatalf("The Last Finalized Slot should not be equal: %d %d", - cState1.LastFinalizedSlot(), - cState2.LastFinalizedSlot(), - ) - } - - cState1.data.ValidatorSetChangeSlot = 40 - if cState1.ValidatorSetChangeSlot() == cState2.ValidatorSetChangeSlot() { - t.Fatalf("The Last Validator Set Change Slot should not be equal: %d %d", - cState1.ValidatorSetChangeSlot(), - cState2.ValidatorSetChangeSlot(), - ) - } - - var crosslinks []*pb.CrosslinkRecord - for i := uint64(0); i < shardCount; i++ { - crosslinks = append(crosslinks, &pb.CrosslinkRecord{ - ShardBlockHash: make([]byte, 2, 34), - Slot: 2, - }) - } - cState1.data.Crosslinks = crosslinks - if cState1.Crosslinks()[0].Slot == cState2.Crosslinks()[0].Slot { - t.Fatalf("The Crosslinks should not be equal: %d %d", - cState1.Crosslinks()[0].Slot, - cState2.Crosslinks()[0].Slot, - ) - } - - cState1.data.Validators = append(cState1.Validators(), &pb.ValidatorRecord{Balance: 32 * 1e9, Status: uint64(params.Active)}) - if len(cState1.Validators()) == len(cState2.Validators()) { - t.Fatalf("The Validators should be equal: %d %d", - len(cState1.Validators()), - len(cState2.Validators()), - ) - } - - newArray := &pb.ShardAndCommitteeArray{ - ArrayShardAndCommittee: []*pb.ShardAndCommittee{ - {Shard: 1, Committee: []uint32{0, 1, 2, 3, 4}}, - {Shard: 2, Committee: []uint32{5, 6, 7, 8, 9}}, - }, - } - cState1.data.ShardAndCommitteesForSlots = append(cState1.ShardAndCommitteesForSlots(), newArray) - if len(cState1.ShardAndCommitteesForSlots()) == len(cState2.ShardAndCommitteesForSlots()) { - t.Fatalf("The ShardAndCommitteesForSlots shouldnt be equal: %d %d", - cState1.ShardAndCommitteesForSlots(), - cState2.ShardAndCommitteesForSlots(), - ) - } -} - -func TestInitialDeriveCrystallizedState(t *testing.T) { - cState, err := NewGenesisCrystallizedState(nil) - if err != nil { - t.Fatalf("Failed to initialize crystallized state: %v", err) - } - - var attesterBitfield []byte - for uint64(len(attesterBitfield))*8 < params.BeaconConfig().BootstrappedValidatorsCount { - attesterBitfield = append(attesterBitfield, byte(0)) - } - - aState := NewGenesisActiveState() - block := NewBlock(&pb.BeaconBlock{ - AncestorHashes: [][]byte{}, - Slot: 0, - ActiveStateRoot: []byte{}, - CrystallizedStateRoot: []byte{}, - Attestations: []*pb.AggregatedAttestation{{ - Slot: 0, - AttesterBitfield: attesterBitfield, - Shard: 0, - }}, - }) - - db := &mockDB{} - newCState, err := cState.NewStateRecalculations(aState, block, db) - if err != nil { - t.Fatalf("failed to derive new crystallized state: %v", err) - } - - if newCState.LastJustifiedSlot() != 0 { - t.Fatalf("expected justified slot to equal %d: got %d", 0, newCState.LastJustifiedSlot()) - } - - if newCState.JustifiedStreak() != 0 { - t.Fatalf("expected justified streak to equal %d: got %d", 0, newCState.JustifiedStreak()) - } - - if newCState.LastStateRecalculationSlot() != params.BeaconConfig().CycleLength { - t.Fatalf("expected last state recalc to equal %d: got %d", params.BeaconConfig().CycleLength, newCState.LastStateRecalculationSlot()) - } - - if newCState.LastFinalizedSlot() != 0 { - t.Fatalf("xpected finalized slot to equal %d, got %d", 0, newCState.LastFinalizedSlot()) - } -} - -func TestNextDeriveCrystallizedSlot(t *testing.T) { - cState, err := NewGenesisCrystallizedState(nil) - if err != nil { - t.Fatalf("Failed to initialized crystallized state: %v", err) - } - - aState := NewGenesisActiveState() - block := NewBlock(nil) - - db := &mockDB{} - cState, err = cState.NewStateRecalculations(aState, block, db) - if err != nil { - t.Fatalf("failed to derive next crystallized state: %v", err) - } - - cState.data.Validators = []*pb.ValidatorRecord{ - {Balance: uint64(params.BeaconConfig().DepositSize * params.BeaconConfig().Gwei), - Status: uint64(params.Active)}, - } - - totalDeposits := cState.TotalDeposits() - recentShardBlockHashes := make([][]byte, 3*params.BeaconConfig().CycleLength) - blockVoteCache := utils.NewBlockVoteCache() - for i := 0; i < 3*int(params.BeaconConfig().CycleLength); i++ { - shardBlockHash := [32]byte{} - counter := []byte(strconv.Itoa(i)) - copy(shardBlockHash[:], counter) - recentShardBlockHashes[i] = shardBlockHash[:] - blockVoteCache[shardBlockHash] = &utils.BlockVote{ - VoteTotalDeposit: totalDeposits * 3 / 4, - } - } - db.loadMockBlockVoteCache(blockVoteCache) - - aState = NewActiveState(&pb.ActiveState{ - RecentBlockHashes: recentShardBlockHashes, - }) - - cState, err = cState.NewStateRecalculations(aState, block, db) - if err != nil { - t.Fatalf("failed to derive crystallized state: %v", err) - } - if cState.LastStateRecalculationSlot() != 2*params.BeaconConfig().CycleLength { - t.Fatalf("expected last state recalc to equal %d: got %d", 2*params.BeaconConfig().CycleLength, cState.LastStateRecalculationSlot()) - } - if cState.LastJustifiedSlot() != params.BeaconConfig().CycleLength-1 { - t.Fatalf("expected justified slot to equal %d: got %d", params.BeaconConfig().CycleLength-1, cState.LastJustifiedSlot()) - } - if cState.JustifiedStreak() != params.BeaconConfig().CycleLength { - t.Fatalf("expected justified streak to equal %d: got %d", params.BeaconConfig().CycleLength, cState.JustifiedStreak()) - } - if cState.LastFinalizedSlot() != 0 { - t.Fatalf("expected finalized slot to equal %d: got %d", 0, cState.LastFinalizedSlot()) - } - - cState, err = cState.NewStateRecalculations(aState, block, db) - if err != nil { - t.Fatalf("failed to derive crystallized state: %v", err) - } - if cState.LastStateRecalculationSlot() != 3*params.BeaconConfig().CycleLength { - t.Fatalf("expected last state recalc to equal %d: got %d", 3*params.BeaconConfig().CycleLength, cState.LastStateRecalculationSlot()) - } - if cState.LastJustifiedSlot() != 2*params.BeaconConfig().CycleLength-1 { - t.Fatalf("expected justified slot to equal %d: got %d", 2*params.BeaconConfig().CycleLength-1, cState.LastJustifiedSlot()) - } - if cState.JustifiedStreak() != 2*params.BeaconConfig().CycleLength { - t.Fatalf("expected justified streak to equal %d: got %d", 2*params.BeaconConfig().CycleLength, cState.JustifiedStreak()) - } - if cState.LastFinalizedSlot() != params.BeaconConfig().CycleLength-2 { - t.Fatalf("expected finalized slot to equal %d: got %d", params.BeaconConfig().CycleLength-2, cState.LastFinalizedSlot()) - } - - cState, err = cState.NewStateRecalculations(aState, block, db) - if err != nil { - t.Fatalf("failed to derive crystallized state: %v", err) - } - if cState.LastStateRecalculationSlot() != 4*params.BeaconConfig().CycleLength { - t.Fatalf("expected last state recalc to equal %d: got %d", 3*params.BeaconConfig().CycleLength, cState.LastStateRecalculationSlot()) - } - if cState.LastJustifiedSlot() != 3*params.BeaconConfig().CycleLength-1 { - t.Fatalf("expected justified slot to equal %d: got %d", 3*params.BeaconConfig().CycleLength-1, cState.LastJustifiedSlot()) - } - if cState.JustifiedStreak() != 3*params.BeaconConfig().CycleLength { - t.Fatalf("expected justified streak to equal %d: got %d", 3*params.BeaconConfig().CycleLength, cState.JustifiedStreak()) - } - if cState.LastFinalizedSlot() != 2*params.BeaconConfig().CycleLength-2 { - t.Fatalf("expected finalized slot to equal %d: got %d", 2*params.BeaconConfig().CycleLength-2, cState.LastFinalizedSlot()) - } -} - -func TestProcessCrosslinks(t *testing.T) { - // Set up crosslink record for every shard. - var clRecords []*pb.CrosslinkRecord - for i := uint64(0); i < params.BeaconConfig().ShardCount; i++ { - clRecord := &pb.CrosslinkRecord{ShardBlockHash: []byte{'A'}, Slot: 1} - clRecords = append(clRecords, clRecord) - } - - // Set up validators. - var validators []*pb.ValidatorRecord - - for i := 0; i < 20; i++ { - validators = append(validators, &pb.ValidatorRecord{ - Balance: 1e18, - Status: uint64(params.Active), - }) - } - - // Set up pending attestations. - pAttestations := []*pb.AggregatedAttestation{ - { - Slot: 0, - Shard: 1, - ShardBlockHash: []byte{'a'}, - AttesterBitfield: []byte{224}, - }, - } - - // Process crosslinks happened at slot 50. - shardAndCommitteesForSlots, err := v.InitialShardAndCommitteesForSlots(validators) - if err != nil { - t.Fatalf("failed to initialize indices for slots: %v", err) - } - - committee := []uint32{0, 4, 6} - - shardAndCommitteesForSlots[0].ArrayShardAndCommittee[0].Committee = committee - - cState := NewCrystallizedState(&pb.CrystallizedState{ - Crosslinks: clRecords, - Validators: validators, - ShardAndCommitteesForSlots: shardAndCommitteesForSlots, - }) - newCrosslinks, err := cState.processCrosslinks(pAttestations, cState.Validators(), 100) - if err != nil { - t.Fatalf("process crosslink failed %v", err) - } - - if newCrosslinks[1].Slot != params.BeaconConfig().CycleLength { - t.Errorf("Slot did not change for new cross link. Wanted: %d. Got: %d", params.BeaconConfig().CycleLength, newCrosslinks[0].Slot) - } - if !bytes.Equal(newCrosslinks[1].ShardBlockHash, []byte{'a'}) { - t.Errorf("ShardBlockHash did not change for new cross link. Wanted a. Got: %s", newCrosslinks[0].ShardBlockHash) - } - //TODO(#538) Implement tests on balances of the validators in committee once big.Int is introduced. -} - -func TestIsNewValidatorSetTransition(t *testing.T) { - cState, err := NewGenesisCrystallizedState(nil) - if err != nil { - t.Fatalf("Failed to initialize crystallized state: %v", err) - } - cState.data.ValidatorSetChangeSlot = 1 - if cState.isValidatorSetChange(0) { - t.Errorf("Is new validator set change should be false, last changed slot greater than finalized slot") - } - cState.data.LastFinalizedSlot = 2 - if cState.isValidatorSetChange(1) { - t.Errorf("Is new validator set change should be false, MinValidatorSetChangeInterval has not reached") - } - shardCommitteeForSlots := []*pb.ShardAndCommitteeArray{{ - ArrayShardAndCommittee: []*pb.ShardAndCommittee{ - {Shard: 0}, - {Shard: 1}, - {Shard: 2}, - }, - }, - } - cState.data.ShardAndCommitteesForSlots = shardCommitteeForSlots - - crosslinks := []*pb.CrosslinkRecord{ - {Slot: 1}, - {Slot: 1}, - {Slot: 1}, - } - cState.data.Crosslinks = crosslinks - - if cState.isValidatorSetChange(params.BeaconConfig().MinValidatorSetChangeInterval + 1) { - t.Errorf("Is new validator set change should be false, crosslink slot record is higher than current slot") - } - - crosslinks = []*pb.CrosslinkRecord{ - {Slot: 2}, - {Slot: 2}, - {Slot: 2}, - } - cState.data.Crosslinks = crosslinks - - if !cState.isValidatorSetChange(params.BeaconConfig().MinValidatorSetChangeInterval + 1) { - t.Errorf("New validator set changen failed should have been true") - } -} - -func TestNewValidatorSetRecalculationsInvalid(t *testing.T) { - cState, err := NewGenesisCrystallizedState(nil) - if err != nil { - t.Fatalf("Failed to initialize crystallized state: %v", err) - } - - // Negative test case, shuffle validators with more than MaxValidators. - size := params.BeaconConfig().ModuloBias + 1 - validators := make([]*pb.ValidatorRecord, size) - validator := &pb.ValidatorRecord{Status: uint64(params.Active)} - for i := uint64(0); i < size; i++ { - validators[i] = validator - } - cState.data.Validators = validators - if _, err := cState.newValidatorSetRecalculations([32]byte{'A'}); err == nil { - t.Errorf("new validator set change calculation should have failed with invalid validator count") - } -} - -func TestNewValidatorSetRecalculations(t *testing.T) { - cState, err := NewGenesisCrystallizedState(nil) - if err != nil { - t.Fatalf("Failed to initialize crystallized state: %v", err) - } - - // Create shard committee for every slot. - var shardCommitteesForSlot []*pb.ShardAndCommitteeArray - for i := 0; i < int(params.BeaconConfig().CycleLength); i++ { - // Only 10 shards gets crosslinked by validators this period. - var shardCommittees []*pb.ShardAndCommittee - for i := 0; i < 10; i++ { - shardCommittees = append(shardCommittees, &pb.ShardAndCommittee{Shard: uint64(i)}) - } - shardCommitteesForSlot = append(shardCommitteesForSlot, &pb.ShardAndCommitteeArray{ArrayShardAndCommittee: shardCommittees}) - } - - cState.data.ShardAndCommitteesForSlots = shardCommitteesForSlot - cState.data.LastStateRecalculationSlot = 65 - - shardCount = 10 - _, err = cState.newValidatorSetRecalculations([32]byte{'A'}) - if err != nil { - t.Fatalf("New validator set change failed %v", err) - } -} - -func TestPenalizedETH(t *testing.T) { - cState, err := NewGenesisCrystallizedState(nil) - if err != nil { - t.Fatalf("Failed to initialize crystallized state: %v", err) - } - cState.data.DepositsPenalizedInPeriod = []uint32{100, 200, 300, 400, 500} - cState.penalizedETH(2) - - tests := []struct { - a uint64 - b uint64 - }{ - {a: 0, b: 100}, - {a: 1, b: 300}, - {a: 2, b: 600}, - {a: 3, b: 900}, - {a: 4, b: 1200}, - } - for _, tt := range tests { - if cState.penalizedETH(uint32(tt.a)) != tt.b { - t.Errorf("PenalizedETH(%d) = %v, want = %d", tt.a, cState.penalizedETH(uint32(tt.a)), tt.b) - } - } -} diff --git a/beacon-chain/utils/genesis_json.go b/beacon-chain/utils/genesis_json.go index c7b8b1318f..f7729f9e5d 100644 --- a/beacon-chain/utils/genesis_json.go +++ b/beacon-chain/utils/genesis_json.go @@ -19,10 +19,10 @@ func InitialValidatorsFromJSON(genesisJSONPath string) ([]*pb.ValidatorRecord, e return nil, err } - cState := &pb.CrystallizedState{} - if err := jsonpb.Unmarshal(f, cState); err != nil { + beaconState := &pb.BeaconState{} + if err := jsonpb.Unmarshal(f, beaconState); err != nil { return nil, fmt.Errorf("error converting JSON to proto: %v", err) } - return cState.Validators, nil + return beaconState.Validators, nil } diff --git a/beacon-chain/utils/genesis_json_test.go b/beacon-chain/utils/genesis_json_test.go index 4187280ea4..f72d61e24e 100644 --- a/beacon-chain/utils/genesis_json_test.go +++ b/beacon-chain/utils/genesis_json_test.go @@ -27,7 +27,7 @@ func TestInitGenesisJson(t *testing.T) { os.Remove(fnamePath) params.UseDemoBeaconConfig() - cStateJSON := &pb.CrystallizedState{ + stateJSON := &pb.BeaconState{ LastStateRecalculationSlot: 0, JustifiedStreak: 1, LastFinalizedSlot: 99, @@ -42,7 +42,7 @@ func TestInitGenesisJson(t *testing.T) { } ma := jsonpb.Marshaler{} - err = ma.Marshal(f, cStateJSON) + err = ma.Marshal(f, stateJSON) if err != nil { t.Fatalf("can't marshal file %v", err) } diff --git a/proto/beacon/p2p/v1/messages.pb.go b/proto/beacon/p2p/v1/messages.pb.go index 887d68eb9a..b4e59c30c6 100755 --- a/proto/beacon/p2p/v1/messages.pb.go +++ b/proto/beacon/p2p/v1/messages.pb.go @@ -28,28 +28,22 @@ const ( Topic_BEACON_BLOCK_RESPONSE Topic = 4 Topic_CHAIN_HEAD_REQUEST Topic = 5 Topic_CHAIN_HEAD_RESPONSE Topic = 6 - Topic_CRYSTALLIZED_STATE_HASH_ANNOUNCE Topic = 7 - Topic_CRYSTALLIZED_STATE_REQUEST Topic = 8 - Topic_CRYSTALLIZED_STATE_RESPONSE Topic = 9 - Topic_ACTIVE_STATE_HASH_ANNOUNCE Topic = 10 - Topic_ACTIVE_STATE_REQUEST Topic = 11 - Topic_ACTIVE_STATE_RESPONSE Topic = 12 + Topic_BEACON_STATE_HASH_ANNOUNCE Topic = 7 + Topic_BEACON_STATE_REQUEST Topic = 8 + Topic_BEACON_STATE_RESPONSE Topic = 9 ) var Topic_name = map[int32]string{ - 0: "UNKNOWN", - 1: "BEACON_BLOCK_ANNOUNCE", - 2: "BEACON_BLOCK_REQUEST", - 3: "BEACON_BLOCK_REQUEST_BY_SLOT_NUMBER", - 4: "BEACON_BLOCK_RESPONSE", - 5: "CHAIN_HEAD_REQUEST", - 6: "CHAIN_HEAD_RESPONSE", - 7: "CRYSTALLIZED_STATE_HASH_ANNOUNCE", - 8: "CRYSTALLIZED_STATE_REQUEST", - 9: "CRYSTALLIZED_STATE_RESPONSE", - 10: "ACTIVE_STATE_HASH_ANNOUNCE", - 11: "ACTIVE_STATE_REQUEST", - 12: "ACTIVE_STATE_RESPONSE", + 0: "UNKNOWN", + 1: "BEACON_BLOCK_ANNOUNCE", + 2: "BEACON_BLOCK_REQUEST", + 3: "BEACON_BLOCK_REQUEST_BY_SLOT_NUMBER", + 4: "BEACON_BLOCK_RESPONSE", + 5: "CHAIN_HEAD_REQUEST", + 6: "CHAIN_HEAD_RESPONSE", + 7: "BEACON_STATE_HASH_ANNOUNCE", + 8: "BEACON_STATE_REQUEST", + 9: "BEACON_STATE_RESPONSE", } var Topic_value = map[string]int32{ "UNKNOWN": 0, @@ -59,19 +53,16 @@ var Topic_value = map[string]int32{ "BEACON_BLOCK_RESPONSE": 4, "CHAIN_HEAD_REQUEST": 5, "CHAIN_HEAD_RESPONSE": 6, - "CRYSTALLIZED_STATE_HASH_ANNOUNCE": 7, - "CRYSTALLIZED_STATE_REQUEST": 8, - "CRYSTALLIZED_STATE_RESPONSE": 9, - "ACTIVE_STATE_HASH_ANNOUNCE": 10, - "ACTIVE_STATE_REQUEST": 11, - "ACTIVE_STATE_RESPONSE": 12, + "BEACON_STATE_HASH_ANNOUNCE": 7, + "BEACON_STATE_REQUEST": 8, + "BEACON_STATE_RESPONSE": 9, } func (x Topic) String() string { return proto.EnumName(Topic_name, int32(x)) } func (Topic) EnumDescriptor() ([]byte, []int) { - return fileDescriptor_messages_595dfd11dfebcb97, []int{0} + return fileDescriptor_messages_948b3cc3a74cee71, []int{0} } type BeaconBlockAnnounce struct { @@ -86,7 +77,7 @@ func (m *BeaconBlockAnnounce) Reset() { *m = BeaconBlockAnnounce{} } func (m *BeaconBlockAnnounce) String() string { return proto.CompactTextString(m) } func (*BeaconBlockAnnounce) ProtoMessage() {} func (*BeaconBlockAnnounce) Descriptor() ([]byte, []int) { - return fileDescriptor_messages_595dfd11dfebcb97, []int{0} + return fileDescriptor_messages_948b3cc3a74cee71, []int{0} } func (m *BeaconBlockAnnounce) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_BeaconBlockAnnounce.Unmarshal(m, b) @@ -131,7 +122,7 @@ func (m *BeaconBlockRequest) Reset() { *m = BeaconBlockRequest{} } func (m *BeaconBlockRequest) String() string { return proto.CompactTextString(m) } func (*BeaconBlockRequest) ProtoMessage() {} func (*BeaconBlockRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_messages_595dfd11dfebcb97, []int{1} + return fileDescriptor_messages_948b3cc3a74cee71, []int{1} } func (m *BeaconBlockRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_BeaconBlockRequest.Unmarshal(m, b) @@ -169,7 +160,7 @@ func (m *BeaconBlockRequestBySlotNumber) Reset() { *m = BeaconBlockReque func (m *BeaconBlockRequestBySlotNumber) String() string { return proto.CompactTextString(m) } func (*BeaconBlockRequestBySlotNumber) ProtoMessage() {} func (*BeaconBlockRequestBySlotNumber) Descriptor() ([]byte, []int) { - return fileDescriptor_messages_595dfd11dfebcb97, []int{2} + return fileDescriptor_messages_948b3cc3a74cee71, []int{2} } func (m *BeaconBlockRequestBySlotNumber) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_BeaconBlockRequestBySlotNumber.Unmarshal(m, b) @@ -208,7 +199,7 @@ func (m *BeaconBlockResponse) Reset() { *m = BeaconBlockResponse{} } func (m *BeaconBlockResponse) String() string { return proto.CompactTextString(m) } func (*BeaconBlockResponse) ProtoMessage() {} func (*BeaconBlockResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_messages_595dfd11dfebcb97, []int{3} + return fileDescriptor_messages_948b3cc3a74cee71, []int{3} } func (m *BeaconBlockResponse) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_BeaconBlockResponse.Unmarshal(m, b) @@ -252,7 +243,7 @@ func (m *ChainHeadRequest) Reset() { *m = ChainHeadRequest{} } func (m *ChainHeadRequest) String() string { return proto.CompactTextString(m) } func (*ChainHeadRequest) ProtoMessage() {} func (*ChainHeadRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_messages_595dfd11dfebcb97, []int{4} + return fileDescriptor_messages_948b3cc3a74cee71, []int{4} } func (m *ChainHeadRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_ChainHeadRequest.Unmarshal(m, b) @@ -285,7 +276,7 @@ func (m *ChainHeadResponse) Reset() { *m = ChainHeadResponse{} } func (m *ChainHeadResponse) String() string { return proto.CompactTextString(m) } func (*ChainHeadResponse) ProtoMessage() {} func (*ChainHeadResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_messages_595dfd11dfebcb97, []int{5} + return fileDescriptor_messages_948b3cc3a74cee71, []int{5} } func (m *ChainHeadResponse) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_ChainHeadResponse.Unmarshal(m, b) @@ -326,230 +317,116 @@ func (m *ChainHeadResponse) GetBlock() *BeaconBlock { return nil } -type CrystallizedStateHashAnnounce struct { +type BeaconStateHashAnnounce struct { Hash []byte `protobuf:"bytes,1,opt,name=hash,proto3" json:"hash,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` } -func (m *CrystallizedStateHashAnnounce) Reset() { *m = CrystallizedStateHashAnnounce{} } -func (m *CrystallizedStateHashAnnounce) String() string { return proto.CompactTextString(m) } -func (*CrystallizedStateHashAnnounce) ProtoMessage() {} -func (*CrystallizedStateHashAnnounce) Descriptor() ([]byte, []int) { - return fileDescriptor_messages_595dfd11dfebcb97, []int{6} +func (m *BeaconStateHashAnnounce) Reset() { *m = BeaconStateHashAnnounce{} } +func (m *BeaconStateHashAnnounce) String() string { return proto.CompactTextString(m) } +func (*BeaconStateHashAnnounce) ProtoMessage() {} +func (*BeaconStateHashAnnounce) Descriptor() ([]byte, []int) { + return fileDescriptor_messages_948b3cc3a74cee71, []int{6} } -func (m *CrystallizedStateHashAnnounce) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_CrystallizedStateHashAnnounce.Unmarshal(m, b) +func (m *BeaconStateHashAnnounce) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_BeaconStateHashAnnounce.Unmarshal(m, b) } -func (m *CrystallizedStateHashAnnounce) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_CrystallizedStateHashAnnounce.Marshal(b, m, deterministic) +func (m *BeaconStateHashAnnounce) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_BeaconStateHashAnnounce.Marshal(b, m, deterministic) } -func (dst *CrystallizedStateHashAnnounce) XXX_Merge(src proto.Message) { - xxx_messageInfo_CrystallizedStateHashAnnounce.Merge(dst, src) +func (dst *BeaconStateHashAnnounce) XXX_Merge(src proto.Message) { + xxx_messageInfo_BeaconStateHashAnnounce.Merge(dst, src) } -func (m *CrystallizedStateHashAnnounce) XXX_Size() int { - return xxx_messageInfo_CrystallizedStateHashAnnounce.Size(m) +func (m *BeaconStateHashAnnounce) XXX_Size() int { + return xxx_messageInfo_BeaconStateHashAnnounce.Size(m) } -func (m *CrystallizedStateHashAnnounce) XXX_DiscardUnknown() { - xxx_messageInfo_CrystallizedStateHashAnnounce.DiscardUnknown(m) +func (m *BeaconStateHashAnnounce) XXX_DiscardUnknown() { + xxx_messageInfo_BeaconStateHashAnnounce.DiscardUnknown(m) } -var xxx_messageInfo_CrystallizedStateHashAnnounce proto.InternalMessageInfo +var xxx_messageInfo_BeaconStateHashAnnounce proto.InternalMessageInfo -func (m *CrystallizedStateHashAnnounce) GetHash() []byte { +func (m *BeaconStateHashAnnounce) GetHash() []byte { if m != nil { return m.Hash } return nil } -type CrystallizedStateRequest struct { +type BeaconStateRequest struct { Hash []byte `protobuf:"bytes,1,opt,name=hash,proto3" json:"hash,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` } -func (m *CrystallizedStateRequest) Reset() { *m = CrystallizedStateRequest{} } -func (m *CrystallizedStateRequest) String() string { return proto.CompactTextString(m) } -func (*CrystallizedStateRequest) ProtoMessage() {} -func (*CrystallizedStateRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_messages_595dfd11dfebcb97, []int{7} +func (m *BeaconStateRequest) Reset() { *m = BeaconStateRequest{} } +func (m *BeaconStateRequest) String() string { return proto.CompactTextString(m) } +func (*BeaconStateRequest) ProtoMessage() {} +func (*BeaconStateRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_messages_948b3cc3a74cee71, []int{7} } -func (m *CrystallizedStateRequest) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_CrystallizedStateRequest.Unmarshal(m, b) +func (m *BeaconStateRequest) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_BeaconStateRequest.Unmarshal(m, b) } -func (m *CrystallizedStateRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_CrystallizedStateRequest.Marshal(b, m, deterministic) +func (m *BeaconStateRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_BeaconStateRequest.Marshal(b, m, deterministic) } -func (dst *CrystallizedStateRequest) XXX_Merge(src proto.Message) { - xxx_messageInfo_CrystallizedStateRequest.Merge(dst, src) +func (dst *BeaconStateRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_BeaconStateRequest.Merge(dst, src) } -func (m *CrystallizedStateRequest) XXX_Size() int { - return xxx_messageInfo_CrystallizedStateRequest.Size(m) +func (m *BeaconStateRequest) XXX_Size() int { + return xxx_messageInfo_BeaconStateRequest.Size(m) } -func (m *CrystallizedStateRequest) XXX_DiscardUnknown() { - xxx_messageInfo_CrystallizedStateRequest.DiscardUnknown(m) +func (m *BeaconStateRequest) XXX_DiscardUnknown() { + xxx_messageInfo_BeaconStateRequest.DiscardUnknown(m) } -var xxx_messageInfo_CrystallizedStateRequest proto.InternalMessageInfo +var xxx_messageInfo_BeaconStateRequest proto.InternalMessageInfo -func (m *CrystallizedStateRequest) GetHash() []byte { +func (m *BeaconStateRequest) GetHash() []byte { if m != nil { return m.Hash } return nil } -type CrystallizedStateResponse struct { - CrystallizedState *CrystallizedState `protobuf:"bytes,1,opt,name=crystallized_state,json=crystallizedState,proto3" json:"crystallized_state,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` -} - -func (m *CrystallizedStateResponse) Reset() { *m = CrystallizedStateResponse{} } -func (m *CrystallizedStateResponse) String() string { return proto.CompactTextString(m) } -func (*CrystallizedStateResponse) ProtoMessage() {} -func (*CrystallizedStateResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_messages_595dfd11dfebcb97, []int{8} -} -func (m *CrystallizedStateResponse) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_CrystallizedStateResponse.Unmarshal(m, b) -} -func (m *CrystallizedStateResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_CrystallizedStateResponse.Marshal(b, m, deterministic) -} -func (dst *CrystallizedStateResponse) XXX_Merge(src proto.Message) { - xxx_messageInfo_CrystallizedStateResponse.Merge(dst, src) -} -func (m *CrystallizedStateResponse) XXX_Size() int { - return xxx_messageInfo_CrystallizedStateResponse.Size(m) -} -func (m *CrystallizedStateResponse) XXX_DiscardUnknown() { - xxx_messageInfo_CrystallizedStateResponse.DiscardUnknown(m) -} - -var xxx_messageInfo_CrystallizedStateResponse proto.InternalMessageInfo - -func (m *CrystallizedStateResponse) GetCrystallizedState() *CrystallizedState { - if m != nil { - return m.CrystallizedState - } - return nil -} - -type ActiveStateHashAnnounce struct { - Hash []byte `protobuf:"bytes,1,opt,name=hash,proto3" json:"hash,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` -} - -func (m *ActiveStateHashAnnounce) Reset() { *m = ActiveStateHashAnnounce{} } -func (m *ActiveStateHashAnnounce) String() string { return proto.CompactTextString(m) } -func (*ActiveStateHashAnnounce) ProtoMessage() {} -func (*ActiveStateHashAnnounce) Descriptor() ([]byte, []int) { - return fileDescriptor_messages_595dfd11dfebcb97, []int{9} -} -func (m *ActiveStateHashAnnounce) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_ActiveStateHashAnnounce.Unmarshal(m, b) -} -func (m *ActiveStateHashAnnounce) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_ActiveStateHashAnnounce.Marshal(b, m, deterministic) -} -func (dst *ActiveStateHashAnnounce) XXX_Merge(src proto.Message) { - xxx_messageInfo_ActiveStateHashAnnounce.Merge(dst, src) -} -func (m *ActiveStateHashAnnounce) XXX_Size() int { - return xxx_messageInfo_ActiveStateHashAnnounce.Size(m) -} -func (m *ActiveStateHashAnnounce) XXX_DiscardUnknown() { - xxx_messageInfo_ActiveStateHashAnnounce.DiscardUnknown(m) -} - -var xxx_messageInfo_ActiveStateHashAnnounce proto.InternalMessageInfo - -func (m *ActiveStateHashAnnounce) GetHash() []byte { - if m != nil { - return m.Hash - } - return nil -} - -type ActiveStateRequest struct { - Hash []byte `protobuf:"bytes,1,opt,name=hash,proto3" json:"hash,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` -} - -func (m *ActiveStateRequest) Reset() { *m = ActiveStateRequest{} } -func (m *ActiveStateRequest) String() string { return proto.CompactTextString(m) } -func (*ActiveStateRequest) ProtoMessage() {} -func (*ActiveStateRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_messages_595dfd11dfebcb97, []int{10} -} -func (m *ActiveStateRequest) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_ActiveStateRequest.Unmarshal(m, b) -} -func (m *ActiveStateRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_ActiveStateRequest.Marshal(b, m, deterministic) -} -func (dst *ActiveStateRequest) XXX_Merge(src proto.Message) { - xxx_messageInfo_ActiveStateRequest.Merge(dst, src) -} -func (m *ActiveStateRequest) XXX_Size() int { - return xxx_messageInfo_ActiveStateRequest.Size(m) -} -func (m *ActiveStateRequest) XXX_DiscardUnknown() { - xxx_messageInfo_ActiveStateRequest.DiscardUnknown(m) -} - -var xxx_messageInfo_ActiveStateRequest proto.InternalMessageInfo - -func (m *ActiveStateRequest) GetHash() []byte { - if m != nil { - return m.Hash - } - return nil -} - -type ActiveStateResponse struct { - ActiveState *ActiveState `protobuf:"bytes,1,opt,name=active_state,json=activeState,proto3" json:"active_state,omitempty"` +type BeaconStateResponse struct { + BeaconState *BeaconState `protobuf:"bytes,1,opt,name=beacon_state,json=beaconState,proto3" json:"beacon_state,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` } -func (m *ActiveStateResponse) Reset() { *m = ActiveStateResponse{} } -func (m *ActiveStateResponse) String() string { return proto.CompactTextString(m) } -func (*ActiveStateResponse) ProtoMessage() {} -func (*ActiveStateResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_messages_595dfd11dfebcb97, []int{11} +func (m *BeaconStateResponse) Reset() { *m = BeaconStateResponse{} } +func (m *BeaconStateResponse) String() string { return proto.CompactTextString(m) } +func (*BeaconStateResponse) ProtoMessage() {} +func (*BeaconStateResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_messages_948b3cc3a74cee71, []int{8} } -func (m *ActiveStateResponse) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_ActiveStateResponse.Unmarshal(m, b) +func (m *BeaconStateResponse) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_BeaconStateResponse.Unmarshal(m, b) } -func (m *ActiveStateResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_ActiveStateResponse.Marshal(b, m, deterministic) +func (m *BeaconStateResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_BeaconStateResponse.Marshal(b, m, deterministic) } -func (dst *ActiveStateResponse) XXX_Merge(src proto.Message) { - xxx_messageInfo_ActiveStateResponse.Merge(dst, src) +func (dst *BeaconStateResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_BeaconStateResponse.Merge(dst, src) } -func (m *ActiveStateResponse) XXX_Size() int { - return xxx_messageInfo_ActiveStateResponse.Size(m) +func (m *BeaconStateResponse) XXX_Size() int { + return xxx_messageInfo_BeaconStateResponse.Size(m) } -func (m *ActiveStateResponse) XXX_DiscardUnknown() { - xxx_messageInfo_ActiveStateResponse.DiscardUnknown(m) +func (m *BeaconStateResponse) XXX_DiscardUnknown() { + xxx_messageInfo_BeaconStateResponse.DiscardUnknown(m) } -var xxx_messageInfo_ActiveStateResponse proto.InternalMessageInfo +var xxx_messageInfo_BeaconStateResponse proto.InternalMessageInfo -func (m *ActiveStateResponse) GetActiveState() *ActiveState { +func (m *BeaconStateResponse) GetBeaconState() *BeaconState { if m != nil { - return m.ActiveState + return m.BeaconState } return nil } @@ -561,55 +438,47 @@ func init() { proto.RegisterType((*BeaconBlockResponse)(nil), "ethereum.beacon.p2p.v1.BeaconBlockResponse") proto.RegisterType((*ChainHeadRequest)(nil), "ethereum.beacon.p2p.v1.ChainHeadRequest") proto.RegisterType((*ChainHeadResponse)(nil), "ethereum.beacon.p2p.v1.ChainHeadResponse") - proto.RegisterType((*CrystallizedStateHashAnnounce)(nil), "ethereum.beacon.p2p.v1.CrystallizedStateHashAnnounce") - proto.RegisterType((*CrystallizedStateRequest)(nil), "ethereum.beacon.p2p.v1.CrystallizedStateRequest") - proto.RegisterType((*CrystallizedStateResponse)(nil), "ethereum.beacon.p2p.v1.CrystallizedStateResponse") - proto.RegisterType((*ActiveStateHashAnnounce)(nil), "ethereum.beacon.p2p.v1.ActiveStateHashAnnounce") - proto.RegisterType((*ActiveStateRequest)(nil), "ethereum.beacon.p2p.v1.ActiveStateRequest") - proto.RegisterType((*ActiveStateResponse)(nil), "ethereum.beacon.p2p.v1.ActiveStateResponse") + proto.RegisterType((*BeaconStateHashAnnounce)(nil), "ethereum.beacon.p2p.v1.BeaconStateHashAnnounce") + proto.RegisterType((*BeaconStateRequest)(nil), "ethereum.beacon.p2p.v1.BeaconStateRequest") + proto.RegisterType((*BeaconStateResponse)(nil), "ethereum.beacon.p2p.v1.BeaconStateResponse") proto.RegisterEnum("ethereum.beacon.p2p.v1.Topic", Topic_name, Topic_value) } func init() { - proto.RegisterFile("proto/beacon/p2p/v1/messages.proto", fileDescriptor_messages_595dfd11dfebcb97) + proto.RegisterFile("proto/beacon/p2p/v1/messages.proto", fileDescriptor_messages_948b3cc3a74cee71) } -var fileDescriptor_messages_595dfd11dfebcb97 = []byte{ - // 567 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x54, 0x51, 0x6f, 0xd2, 0x50, - 0x14, 0xb6, 0x1b, 0xdb, 0xf4, 0x74, 0x0f, 0xdd, 0x45, 0x37, 0x36, 0xe3, 0x20, 0x9d, 0x89, 0x68, - 0xb2, 0x92, 0xb1, 0x27, 0x1f, 0x6f, 0xbb, 0x9a, 0xe2, 0xb0, 0xd5, 0xb6, 0xa8, 0x33, 0x31, 0xcd, - 0xa5, 0xdc, 0x00, 0x11, 0xda, 0xca, 0xbd, 0x90, 0xe0, 0xbf, 0xf1, 0x6f, 0xfa, 0x64, 0xda, 0x95, - 0xd1, 0x42, 0x21, 0xfa, 0x76, 0xfb, 0x7d, 0xe7, 0xfb, 0xce, 0xf9, 0xce, 0x49, 0x0a, 0x72, 0x34, - 0x09, 0x79, 0xd8, 0xe8, 0x52, 0xe2, 0x87, 0x41, 0x23, 0x6a, 0x46, 0x8d, 0xd9, 0x55, 0x63, 0x4c, - 0x19, 0x23, 0x7d, 0xca, 0x94, 0x84, 0x44, 0xc7, 0x94, 0x0f, 0xe8, 0x84, 0x4e, 0xc7, 0xca, 0x7d, - 0x99, 0x12, 0x35, 0x23, 0x65, 0x76, 0x75, 0x56, 0x2d, 0xd2, 0xf2, 0x79, 0x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a1, 0xa6, 0xd9, 0x77, 0x8e, 0x8b, 0xdb, 0xed, 0xd6, - 0x37, 0xfd, 0xc6, 0x73, 0x5c, 0xec, 0xea, 0x9e, 0x81, 0x1d, 0x63, 0x39, 0xf4, 0x01, 0x3a, 0x87, - 0xb3, 0x82, 0xaa, 0x85, 0xfd, 0x63, 0x54, 0x85, 0xe7, 0x85, 0x7c, 0xda, 0xe6, 0x49, 0x6c, 0x80, - 0x35, 0xb7, 0xf5, 0x59, 0x2f, 0x6c, 0x00, 0xf1, 0x56, 0x72, 0xfc, 0xc2, 0x5a, 0x8c, 0xc3, 0xae, - 0x30, 0xa9, 0xe9, 0x61, 0x77, 0x3f, 0xf9, 0x01, 0x5e, 0xff, 0x0d, 0x00, 0x00, 0xff, 0xff, 0xf6, - 0xde, 0x24, 0xa0, 0x5f, 0x05, 0x00, 0x00, +var fileDescriptor_messages_948b3cc3a74cee71 = []byte{ + // 482 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x53, 0xc1, 0x6e, 0xd3, 0x40, + 0x14, 0xc4, 0x69, 0xd2, 0xc2, 0x73, 0x0f, 0x66, 0x0b, 0x6d, 0xe8, 0xa1, 0xad, 0xdc, 0x03, 0x11, + 0x52, 0x1d, 0x35, 0x9c, 0x38, 0xae, 0x83, 0x91, 0xa1, 0x65, 0x0d, 0xb6, 0x23, 0xc4, 0x01, 0x59, + 0xeb, 0xf4, 0x29, 0x8e, 0x68, 0x6c, 0x93, 0xdd, 0x44, 0xea, 0x77, 0xf0, 0x05, 0xfc, 0x29, 0xca, + 0xda, 0xa9, 0x1d, 0x62, 0x2a, 0xb8, 0x39, 0x33, 0x6f, 0x66, 0x67, 0xde, 0x53, 0xc0, 0xcc, 0xe7, + 0x99, 0xcc, 0xfa, 0x31, 0xf2, 0x71, 0x96, 0xf6, 0xf3, 0x41, 0xde, 0x5f, 0x5e, 0xf6, 0x67, 0x28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 0x33, 0xa5, 0xe9, 0x93, 0x78, + 0x57, 0xfd, 0x7b, 0x5f, 0xff, 0x0e, 0x00, 0x00, 0xff, 0xff, 0x03, 0x6d, 0x1d, 0x09, 0x1c, 0x04, + 0x00, 0x00, } diff --git a/proto/beacon/p2p/v1/messages.proto b/proto/beacon/p2p/v1/messages.proto index dfdda644f3..e8914f6f0f 100644 --- a/proto/beacon/p2p/v1/messages.proto +++ b/proto/beacon/p2p/v1/messages.proto @@ -12,12 +12,9 @@ enum Topic { BEACON_BLOCK_RESPONSE = 4; CHAIN_HEAD_REQUEST = 5; CHAIN_HEAD_RESPONSE = 6; - CRYSTALLIZED_STATE_HASH_ANNOUNCE = 7; - CRYSTALLIZED_STATE_REQUEST = 8; - CRYSTALLIZED_STATE_RESPONSE = 9; - ACTIVE_STATE_HASH_ANNOUNCE = 10; - ACTIVE_STATE_REQUEST = 11; - ACTIVE_STATE_RESPONSE = 12; + BEACON_STATE_HASH_ANNOUNCE = 7; + BEACON_STATE_REQUEST = 8; + BEACON_STATE_RESPONSE = 9; } message BeaconBlockAnnounce { @@ -46,26 +43,14 @@ message ChainHeadResponse { BeaconBlock block = 3; } -message CrystallizedStateHashAnnounce { +message BeaconStateHashAnnounce { bytes hash = 1; } -message CrystallizedStateRequest { +message BeaconStateRequest { bytes hash = 1; } -message CrystallizedStateResponse { - CrystallizedState crystallized_state = 1; -} - -message ActiveStateHashAnnounce { - bytes hash = 1; -} - -message ActiveStateRequest { - bytes hash = 1; -} - -message ActiveStateResponse { - ActiveState active_state = 1; +message BeaconStateResponse { + BeaconState beacon_state = 1; } diff --git a/proto/beacon/p2p/v1/types.pb.go b/proto/beacon/p2p/v1/types.pb.go index f480d6a33c..96cfe344bf 100644 --- a/proto/beacon/p2p/v1/types.pb.go +++ b/proto/beacon/p2p/v1/types.pb.go @@ -53,7 +53,7 @@ func (m *BeaconState) Reset() { *m = BeaconState{} } func (m *BeaconState) String() string { return proto.CompactTextString(m) } func (*BeaconState) ProtoMessage() {} func (*BeaconState) Descriptor() ([]byte, []int) { - return fileDescriptor_types_5befaac8cf59dd97, []int{0} + return fileDescriptor_types_019467b1bb39515a, []int{0} } func (m *BeaconState) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_BeaconState.Unmarshal(m, b) @@ -259,7 +259,7 @@ func (m *ValidatorRecord) Reset() { *m = ValidatorRecord{} } func (m *ValidatorRecord) String() string { return proto.CompactTextString(m) } func (*ValidatorRecord) ProtoMessage() {} func (*ValidatorRecord) Descriptor() ([]byte, []int) { - return fileDescriptor_types_5befaac8cf59dd97, []int{1} + return fileDescriptor_types_019467b1bb39515a, []int{1} } func (m *ValidatorRecord) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_ValidatorRecord.Unmarshal(m, b) @@ -348,7 +348,7 @@ func (m *ShardReassignmentRecord) Reset() { *m = ShardReassignmentRecord func (m *ShardReassignmentRecord) String() string { return proto.CompactTextString(m) } func (*ShardReassignmentRecord) ProtoMessage() {} func (*ShardReassignmentRecord) Descriptor() ([]byte, []int) { - return fileDescriptor_types_5befaac8cf59dd97, []int{2} + return fileDescriptor_types_019467b1bb39515a, []int{2} } func (m *ShardReassignmentRecord) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_ShardReassignmentRecord.Unmarshal(m, b) @@ -407,7 +407,7 @@ func (m *AggregatedAttestation) Reset() { *m = AggregatedAttestation{} } func (m *AggregatedAttestation) String() string { return proto.CompactTextString(m) } func (*AggregatedAttestation) ProtoMessage() {} func (*AggregatedAttestation) Descriptor() ([]byte, []int) { - return fileDescriptor_types_5befaac8cf59dd97, []int{3} + return fileDescriptor_types_019467b1bb39515a, []int{3} } func (m *AggregatedAttestation) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_AggregatedAttestation.Unmarshal(m, b) @@ -499,7 +499,7 @@ func (m *AttestationSignedData) Reset() { *m = AttestationSignedData{} } func (m *AttestationSignedData) String() string { return proto.CompactTextString(m) } func (*AttestationSignedData) ProtoMessage() {} func (*AttestationSignedData) Descriptor() ([]byte, []int) { - return fileDescriptor_types_5befaac8cf59dd97, []int{4} + return fileDescriptor_types_019467b1bb39515a, []int{4} } func (m *AttestationSignedData) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_AttestationSignedData.Unmarshal(m, b) @@ -572,7 +572,7 @@ func (m *AttestationHashes) Reset() { *m = AttestationHashes{} } func (m *AttestationHashes) String() string { return proto.CompactTextString(m) } func (*AttestationHashes) ProtoMessage() {} func (*AttestationHashes) Descriptor() ([]byte, []int) { - return fileDescriptor_types_5befaac8cf59dd97, []int{5} + return fileDescriptor_types_019467b1bb39515a, []int{5} } func (m *AttestationHashes) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_AttestationHashes.Unmarshal(m, b) @@ -611,7 +611,7 @@ func (m *SpecialRecord) Reset() { *m = SpecialRecord{} } func (m *SpecialRecord) String() string { return proto.CompactTextString(m) } func (*SpecialRecord) ProtoMessage() {} func (*SpecialRecord) Descriptor() ([]byte, []int) { - return fileDescriptor_types_5befaac8cf59dd97, []int{6} + return fileDescriptor_types_019467b1bb39515a, []int{6} } func (m *SpecialRecord) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_SpecialRecord.Unmarshal(m, b) @@ -657,7 +657,7 @@ func (m *CrosslinkRecord) Reset() { *m = CrosslinkRecord{} } func (m *CrosslinkRecord) String() string { return proto.CompactTextString(m) } func (*CrosslinkRecord) ProtoMessage() {} func (*CrosslinkRecord) Descriptor() ([]byte, []int) { - return fileDescriptor_types_5befaac8cf59dd97, []int{7} + return fileDescriptor_types_019467b1bb39515a, []int{7} } func (m *CrosslinkRecord) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_CrosslinkRecord.Unmarshal(m, b) @@ -702,7 +702,7 @@ func (m *ShardAndCommitteeArray) Reset() { *m = ShardAndCommitteeArray{} func (m *ShardAndCommitteeArray) String() string { return proto.CompactTextString(m) } func (*ShardAndCommitteeArray) ProtoMessage() {} func (*ShardAndCommitteeArray) Descriptor() ([]byte, []int) { - return fileDescriptor_types_5befaac8cf59dd97, []int{8} + return fileDescriptor_types_019467b1bb39515a, []int{8} } func (m *ShardAndCommitteeArray) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_ShardAndCommitteeArray.Unmarshal(m, b) @@ -741,7 +741,7 @@ func (m *ShardAndCommittee) Reset() { *m = ShardAndCommittee{} } func (m *ShardAndCommittee) String() string { return proto.CompactTextString(m) } func (*ShardAndCommittee) ProtoMessage() {} func (*ShardAndCommittee) Descriptor() ([]byte, []int) { - return fileDescriptor_types_5befaac8cf59dd97, []int{9} + return fileDescriptor_types_019467b1bb39515a, []int{9} } func (m *ShardAndCommittee) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_ShardAndCommittee.Unmarshal(m, b) @@ -776,25 +776,24 @@ func (m *ShardAndCommittee) GetCommittee() []uint32 { } type BeaconBlock struct { - AncestorHashes [][]byte `protobuf:"bytes,1,rep,name=ancestor_hashes,json=ancestorHashes,proto3" json:"ancestor_hashes,omitempty"` - Slot uint64 `protobuf:"varint,2,opt,name=slot,proto3" json:"slot,omitempty"` - RandaoReveal []byte `protobuf:"bytes,3,opt,name=randao_reveal,json=randaoReveal,proto3" json:"randao_reveal,omitempty"` - PowChainRef []byte `protobuf:"bytes,4,opt,name=pow_chain_ref,json=powChainRef,proto3" json:"pow_chain_ref,omitempty"` - ActiveStateRoot []byte `protobuf:"bytes,5,opt,name=active_state_root,json=activeStateRoot,proto3" json:"active_state_root,omitempty"` - CrystallizedStateRoot []byte `protobuf:"bytes,6,opt,name=crystallized_state_root,json=crystallizedStateRoot,proto3" json:"crystallized_state_root,omitempty"` - Timestamp *timestamp.Timestamp `protobuf:"bytes,7,opt,name=timestamp,proto3" json:"timestamp,omitempty"` - Attestations []*AggregatedAttestation `protobuf:"bytes,8,rep,name=attestations,proto3" json:"attestations,omitempty"` - Specials []*SpecialRecord `protobuf:"bytes,9,rep,name=specials,proto3" json:"specials,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + AncestorHashes [][]byte `protobuf:"bytes,1,rep,name=ancestor_hashes,json=ancestorHashes,proto3" json:"ancestor_hashes,omitempty"` + Slot uint64 `protobuf:"varint,2,opt,name=slot,proto3" json:"slot,omitempty"` + RandaoReveal []byte `protobuf:"bytes,3,opt,name=randao_reveal,json=randaoReveal,proto3" json:"randao_reveal,omitempty"` + PowChainRef []byte `protobuf:"bytes,4,opt,name=pow_chain_ref,json=powChainRef,proto3" json:"pow_chain_ref,omitempty"` + StateRoot []byte `protobuf:"bytes,5,opt,name=state_root,json=stateRoot,proto3" json:"state_root,omitempty"` + Timestamp *timestamp.Timestamp `protobuf:"bytes,6,opt,name=timestamp,proto3" json:"timestamp,omitempty"` + Attestations []*AggregatedAttestation `protobuf:"bytes,7,rep,name=attestations,proto3" json:"attestations,omitempty"` + Specials []*SpecialRecord `protobuf:"bytes,8,rep,name=specials,proto3" json:"specials,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *BeaconBlock) Reset() { *m = BeaconBlock{} } func (m *BeaconBlock) String() string { return proto.CompactTextString(m) } func (*BeaconBlock) ProtoMessage() {} func (*BeaconBlock) Descriptor() ([]byte, []int) { - return fileDescriptor_types_5befaac8cf59dd97, []int{10} + return fileDescriptor_types_019467b1bb39515a, []int{10} } func (m *BeaconBlock) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_BeaconBlock.Unmarshal(m, b) @@ -842,16 +841,9 @@ func (m *BeaconBlock) GetPowChainRef() []byte { return nil } -func (m *BeaconBlock) GetActiveStateRoot() []byte { +func (m *BeaconBlock) GetStateRoot() []byte { if m != nil { - return m.ActiveStateRoot - } - return nil -} - -func (m *BeaconBlock) GetCrystallizedStateRoot() []byte { - if m != nil { - return m.CrystallizedStateRoot + return m.StateRoot } return nil } @@ -877,202 +869,6 @@ func (m *BeaconBlock) GetSpecials() []*SpecialRecord { return nil } -type CrystallizedState struct { - LastStateRecalculationSlot uint64 `protobuf:"varint,1,opt,name=last_state_recalculation_slot,json=lastStateRecalculationSlot,proto3" json:"last_state_recalculation_slot,omitempty"` - JustifiedStreak uint64 `protobuf:"varint,2,opt,name=justified_streak,json=justifiedStreak,proto3" json:"justified_streak,omitempty"` - LastJustifiedSlot uint64 `protobuf:"varint,3,opt,name=last_justified_slot,json=lastJustifiedSlot,proto3" json:"last_justified_slot,omitempty"` - LastFinalizedSlot uint64 `protobuf:"varint,4,opt,name=last_finalized_slot,json=lastFinalizedSlot,proto3" json:"last_finalized_slot,omitempty"` - ValidatorSetChangeSlot uint64 `protobuf:"varint,5,opt,name=validator_set_change_slot,json=validatorSetChangeSlot,proto3" json:"validator_set_change_slot,omitempty"` - Crosslinks []*CrosslinkRecord `protobuf:"bytes,6,rep,name=crosslinks,proto3" json:"crosslinks,omitempty"` - Validators []*ValidatorRecord `protobuf:"bytes,7,rep,name=validators,proto3" json:"validators,omitempty"` - ShardAndCommitteesForSlots []*ShardAndCommitteeArray `protobuf:"bytes,8,rep,name=shard_and_committees_for_slots,json=shardAndCommitteesForSlots,proto3" json:"shard_and_committees_for_slots,omitempty"` - DepositsPenalizedInPeriod []uint32 `protobuf:"varint,9,rep,packed,name=deposits_penalized_in_period,json=depositsPenalizedInPeriod,proto3" json:"deposits_penalized_in_period,omitempty"` - ValidatorSetDeltaHashChain []byte `protobuf:"bytes,10,opt,name=validator_set_delta_hash_chain,json=validatorSetDeltaHashChain,proto3" json:"validator_set_delta_hash_chain,omitempty"` - PreForkVersion uint32 `protobuf:"varint,11,opt,name=pre_fork_version,json=preForkVersion,proto3" json:"pre_fork_version,omitempty"` - PostForkVersion uint32 `protobuf:"varint,12,opt,name=post_fork_version,json=postForkVersion,proto3" json:"post_fork_version,omitempty"` - ForkSlotNumber uint64 `protobuf:"varint,13,opt,name=fork_slot_number,json=forkSlotNumber,proto3" json:"fork_slot_number,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` -} - -func (m *CrystallizedState) Reset() { *m = CrystallizedState{} } -func (m *CrystallizedState) String() string { return proto.CompactTextString(m) } -func (*CrystallizedState) ProtoMessage() {} -func (*CrystallizedState) Descriptor() ([]byte, []int) { - return fileDescriptor_types_5befaac8cf59dd97, []int{11} -} -func (m *CrystallizedState) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_CrystallizedState.Unmarshal(m, b) -} -func (m *CrystallizedState) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_CrystallizedState.Marshal(b, m, deterministic) -} -func (dst *CrystallizedState) XXX_Merge(src proto.Message) { - xxx_messageInfo_CrystallizedState.Merge(dst, src) -} -func (m *CrystallizedState) XXX_Size() int { - return xxx_messageInfo_CrystallizedState.Size(m) -} -func (m *CrystallizedState) XXX_DiscardUnknown() { - xxx_messageInfo_CrystallizedState.DiscardUnknown(m) -} - -var xxx_messageInfo_CrystallizedState proto.InternalMessageInfo - -func (m *CrystallizedState) GetLastStateRecalculationSlot() uint64 { - if m != nil { - return m.LastStateRecalculationSlot - } - return 0 -} - -func (m *CrystallizedState) GetJustifiedStreak() uint64 { - if m != nil { - return m.JustifiedStreak - } - return 0 -} - -func (m *CrystallizedState) GetLastJustifiedSlot() uint64 { - if m != nil { - return m.LastJustifiedSlot - } - return 0 -} - -func (m *CrystallizedState) GetLastFinalizedSlot() uint64 { - if m != nil { - return m.LastFinalizedSlot - } - return 0 -} - -func (m *CrystallizedState) GetValidatorSetChangeSlot() uint64 { - if m != nil { - return m.ValidatorSetChangeSlot - } - return 0 -} - -func (m *CrystallizedState) GetCrosslinks() []*CrosslinkRecord { - if m != nil { - return m.Crosslinks - } - return nil -} - -func (m *CrystallizedState) GetValidators() []*ValidatorRecord { - if m != nil { - return m.Validators - } - return nil -} - -func (m *CrystallizedState) GetShardAndCommitteesForSlots() []*ShardAndCommitteeArray { - if m != nil { - return m.ShardAndCommitteesForSlots - } - return nil -} - -func (m *CrystallizedState) GetDepositsPenalizedInPeriod() []uint32 { - if m != nil { - return m.DepositsPenalizedInPeriod - } - return nil -} - -func (m *CrystallizedState) GetValidatorSetDeltaHashChain() []byte { - if m != nil { - return m.ValidatorSetDeltaHashChain - } - return nil -} - -func (m *CrystallizedState) GetPreForkVersion() uint32 { - if m != nil { - return m.PreForkVersion - } - return 0 -} - -func (m *CrystallizedState) GetPostForkVersion() uint32 { - if m != nil { - return m.PostForkVersion - } - return 0 -} - -func (m *CrystallizedState) GetForkSlotNumber() uint64 { - if m != nil { - return m.ForkSlotNumber - } - return 0 -} - -type ActiveState struct { - PendingAttestations []*AggregatedAttestation `protobuf:"bytes,1,rep,name=pending_attestations,json=pendingAttestations,proto3" json:"pending_attestations,omitempty"` - RecentBlockHashes [][]byte `protobuf:"bytes,2,rep,name=recent_block_hashes,json=recentBlockHashes,proto3" json:"recent_block_hashes,omitempty"` - PendingSpecials []*SpecialRecord `protobuf:"bytes,3,rep,name=pending_specials,json=pendingSpecials,proto3" json:"pending_specials,omitempty"` - RandaoMix []byte `protobuf:"bytes,4,opt,name=randao_mix,json=randaoMix,proto3" json:"randao_mix,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` -} - -func (m *ActiveState) Reset() { *m = ActiveState{} } -func (m *ActiveState) String() string { return proto.CompactTextString(m) } -func (*ActiveState) ProtoMessage() {} -func (*ActiveState) Descriptor() ([]byte, []int) { - return fileDescriptor_types_5befaac8cf59dd97, []int{12} -} -func (m *ActiveState) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_ActiveState.Unmarshal(m, b) -} -func (m *ActiveState) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_ActiveState.Marshal(b, m, deterministic) -} -func (dst *ActiveState) XXX_Merge(src proto.Message) { - xxx_messageInfo_ActiveState.Merge(dst, src) -} -func (m *ActiveState) XXX_Size() int { - return xxx_messageInfo_ActiveState.Size(m) -} -func (m *ActiveState) XXX_DiscardUnknown() { - xxx_messageInfo_ActiveState.DiscardUnknown(m) -} - -var xxx_messageInfo_ActiveState proto.InternalMessageInfo - -func (m *ActiveState) GetPendingAttestations() []*AggregatedAttestation { - if m != nil { - return m.PendingAttestations - } - return nil -} - -func (m *ActiveState) GetRecentBlockHashes() [][]byte { - if m != nil { - return m.RecentBlockHashes - } - return nil -} - -func (m *ActiveState) GetPendingSpecials() []*SpecialRecord { - if m != nil { - return m.PendingSpecials - } - return nil -} - -func (m *ActiveState) GetRandaoMix() []byte { - if m != nil { - return m.RandaoMix - } - return nil -} - func init() { proto.RegisterType((*BeaconState)(nil), "ethereum.beacon.p2p.v1.BeaconState") proto.RegisterType((*ValidatorRecord)(nil), "ethereum.beacon.p2p.v1.ValidatorRecord") @@ -1085,112 +881,98 @@ func init() { proto.RegisterType((*ShardAndCommitteeArray)(nil), "ethereum.beacon.p2p.v1.ShardAndCommitteeArray") proto.RegisterType((*ShardAndCommittee)(nil), "ethereum.beacon.p2p.v1.ShardAndCommittee") proto.RegisterType((*BeaconBlock)(nil), "ethereum.beacon.p2p.v1.BeaconBlock") - proto.RegisterType((*CrystallizedState)(nil), "ethereum.beacon.p2p.v1.CrystallizedState") - proto.RegisterType((*ActiveState)(nil), "ethereum.beacon.p2p.v1.ActiveState") } func init() { - proto.RegisterFile("proto/beacon/p2p/v1/types.proto", fileDescriptor_types_5befaac8cf59dd97) + proto.RegisterFile("proto/beacon/p2p/v1/types.proto", fileDescriptor_types_019467b1bb39515a) } -var fileDescriptor_types_5befaac8cf59dd97 = []byte{ - // 1557 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x57, 0xeb, 0x6e, 0x1b, 0xb7, - 0x12, 0x86, 0x2e, 0xbe, 0x8d, 0x24, 0x5b, 0xa2, 0x6f, 0x1b, 0x9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06, 0x58, 0x22, 0x4c, 0x39, 0xdb, 0xbb, 0x05, 0x53, 0x96, 0x56, 0x75, 0x68, 0x34, - 0xaf, 0x50, 0x41, 0xee, 0x00, 0x48, 0x1a, 0xfa, 0x54, 0x78, 0x43, 0x7e, 0xee, 0x38, 0x98, 0x86, - 0x15, 0x2b, 0xf9, 0x8a, 0x9f, 0xd7, 0x7f, 0xc9, 0xc3, 0xda, 0xd4, 0x08, 0x25, 0x5b, 0xb0, 0x18, - 0x8d, 0xda, 0x03, 0x76, 0x81, 0x73, 0xba, 0xec, 0xc6, 0xff, 0xcc, 0x88, 0x39, 0xe3, 0xba, 0xef, - 0x4b, 0x7a, 0x46, 0x03, 0x0f, 0x9b, 0xdd, 0xc9, 0xdb, 0x88, 0xa5, 0x72, 0xec, 0x37, 0xf2, 0x14, - 0x48, 0x06, 0x4a, 0x7d, 0x5f, 0x32, 0x65, 0x26, 0x30, 0xf6, 0x4e, 0xaa, 0x69, 0x5a, 0x05, 0x79, - 0x02, 0xb5, 0xd8, 0x49, 0xdb, 0xe4, 0xd8, 0xd8, 0x45, 0x44, 0x57, 0xad, 0xe2, 0x68, 0x2c, 0x27, - 0x0e, 0x2c, 0xb5, 0x69, 0x40, 0xc3, 0x0e, 0x8b, 0xa7, 0x67, 0xf2, 0xd7, 0x38, 0x6e, 0xe2, 0x34, - 0x52, 0xf1, 0x98, 0x8c, 0xff, 0x91, 0x7f, 0xc1, 0x8a, 0x6d, 0x0a, 0x33, 0x41, 0xed, 0x50, 0x5c, - 0x36, 0x02, 0x1c, 0x9c, 0xff, 0x01, 0x12, 0x7f, 0x1b, 0xe7, 0xad, 0x7d, 0xa6, 0x9c, 0x65, 0x44, - 0xc5, 0x1f, 0xff, 0x92, 0xaa, 0xf8, 0x7d, 0xaa, 0x07, 0xb0, 0x3d, 0x67, 0xa2, 0x90, 0x47, 0xb0, - 0x96, 0xf6, 0x2f, 0x0f, 0x7d, 0x76, 0x8e, 0xf1, 0xab, 0xb8, 0xab, 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0x96, 0x2c, 0x09, 0xb9, 0x09, - 0xff, 0x2d, 0x23, 0xf4, 0x46, 0xfc, 0xb7, 0x32, 0x8b, 0xff, 0xd6, 0x7f, 0xca, 0x43, 0xa9, 0x99, - 0x4e, 0x91, 0xb9, 0x7c, 0x38, 0xf7, 0x77, 0xf3, 0xe1, 0xfc, 0x3c, 0x3e, 0x7c, 0x02, 0xd5, 0xc4, - 0xa3, 0xf1, 0x58, 0x28, 0xfc, 0x95, 0xb1, 0xb0, 0x16, 0x9b, 0xc7, 0xd2, 0x69, 0x86, 0x5d, 0x9c, - 0x62, 0xd8, 0xed, 0x45, 0x9c, 0x78, 0xcf, 0xff, 0x0c, 0x00, 0x00, 0xff, 0xff, 0x32, 0x82, 0xea, - 0x37, 0x7b, 0x11, 0x00, 0x00, +var fileDescriptor_types_019467b1bb39515a = []byte{ + // 1375 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x56, 0xdd, 0x72, 0x13, 0xc7, + 0x12, 0x2e, 0x59, 0xc2, 0x3f, 0x2d, 0xc9, 0xb6, 0xc6, 0x3f, 0x2c, 0x3e, 0x70, 0x30, 0xa2, 0x28, + 0xcc, 0xe1, 0x20, 0x1f, 0xcc, 0xc5, 0x49, 0x2a, 0x55, 0x49, 0x19, 0x13, 0x03, 0xa9, 0xfc, 0x38, + 0x2b, 0x8a, 0xdb, 0xc9, 0x48, 0xdb, 0x92, 0x06, 0xad, 0x76, 0x96, 0x99, 0x91, 0x6d, 0x92, 0xaa, + 0x5c, 0xe4, 0x7d, 0x72, 0x97, 0xa7, 0xc8, 0x23, 0xe4, 0x69, 0x52, 0xd3, 0xb3, 0xab, 0x5d, 0xdb, + 0x32, 0x45, 0xe5, 0x6e, 0xf7, 0xeb, 0xaf, 0x67, 0x7a, 0xfa, 0x1f, 0xee, 0xa6, 0x5a, 0x59, 0xb5, + 0xdf, 0x43, 0xd1, 0x57, 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0x48, 0x6b, 0x78, 0x8a, 0x79, 0x90, 0x65, 0xc2, 0x53, 0xd4, 0x52, 0x45, 0x41, + 0x63, 0xb7, 0xba, 0x57, 0x0b, 0x6f, 0xe5, 0x9c, 0x93, 0x9c, 0xf2, 0x3a, 0x39, 0x21, 0x02, 0xfb, + 0x02, 0x76, 0x8a, 0x8a, 0x89, 0x30, 0xb6, 0x82, 0x8f, 0x84, 0x19, 0xb9, 0xc2, 0x91, 0x49, 0xd0, + 0xa4, 0x7b, 0x6f, 0xce, 0x18, 0x2f, 0x1c, 0xe1, 0x95, 0x30, 0xa3, 0x23, 0x27, 0x66, 0x7b, 0xb0, + 0xde, 0x9f, 0x6a, 0xed, 0xfc, 0x84, 0xe7, 0xd2, 0x72, 0x83, 0xef, 0x83, 0x55, 0x0a, 0xff, 0x6a, + 0x86, 0x7f, 0x7d, 0x2e, 0x6d, 0x17, 0xdf, 0xb3, 0x7b, 0xd0, 0x18, 0x62, 0x82, 0x46, 0x1a, 0xee, + 0x6a, 0x38, 0x58, 0x23, 0x56, 0x3d, 0xc3, 0xde, 0xc8, 0x09, 0xb2, 0x67, 0xb0, 0x3d, 0x4e, 0xd4, + 0x59, 0xc2, 0x53, 0x75, 0xe6, 0xb2, 0x1d, 0x65, 0x6a, 0xb9, 0x56, 0xca, 0x06, 0xeb, 0x64, 0xc5, + 0x06, 0x49, 0x4f, 0xd4, 0x59, 0xe8, 0x65, 0xa1, 0x52, 0xd6, 0x99, 0xdf, 0x17, 0x49, 0xe4, 0xac, + 0xc3, 0xab, 0x8a, 0x2d, 0x6f, 0xfe, 0x8c, 0x71, 0x49, 0xf9, 0x18, 0x76, 0xaf, 0x57, 0xe6, 0xa7, + 0xca, 0xa2, 0x09, 0x18, 0x19, 0x7a, 0xfb, 0x9a, 0x23, 0xde, 0x3a, 0x8e, 0x73, 0x43, 0xaa, 0xd1, + 0x65, 0xf2, 0x98, 0x9f, 0xba, 0x00, 0xab, 0x24, 0xd8, 0xf0, 0x6e, 0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xfd, 0xe0, 0xd1, + 0x27, 0xaf, 0x2c, 0xe1, 0x36, 0x9d, 0x75, 0x05, 0x6f, 0xbf, 0x84, 0xd6, 0x15, 0xb0, 0x08, 0x75, + 0xa5, 0x1c, 0xea, 0xdb, 0xb0, 0x52, 0x18, 0xb0, 0x40, 0x8b, 0x4c, 0x01, 0xb4, 0x7f, 0xab, 0xe6, + 0x1b, 0x31, 0x3d, 0xd8, 0xf5, 0x07, 0xd7, 0xa5, 0x8c, 0x6b, 0x0f, 0x59, 0x6a, 0xf8, 0x60, 0xac, + 0xe6, 0x70, 0x16, 0xb6, 0x79, 0xb9, 0x76, 0x1f, 0x9a, 0x59, 0x97, 0xd2, 0x78, 0x8a, 0x22, 0xce, + 0x7a, 0x69, 0xc3, 0x83, 0x21, 0x61, 0xac, 0x0d, 0x4d, 0x37, 0x3b, 0x69, 0x61, 0xe0, 0x1a, 0x07, + 0x59, 0xf1, 0xd6, 0x53, 0x75, 0x46, 0x5b, 0x42, 0x88, 0x03, 0x37, 0x0f, 0xb2, 0x2d, 0x56, 0x65, + 0xeb, 0x67, 0x23, 0x5c, 0x21, 0x84, 0x06, 0xf1, 0x67, 0xb0, 0x32, 0xdb, 0xec, 0x29, 0xd3, 0xea, + 0x07, 0x3b, 0x1d, 0xbf, 0xfb, 0x77, 0xf2, 0xdd, 0xbf, 0xf3, 0x26, 0x67, 0x84, 0x05, 0x99, 0xfd, + 0x08, 0x8d, 0x0b, 0x23, 0x6f, 0xe9, 0x9f, 0x8c, 0xbc, 0x0b, 0x47, 0xb0, 0x43, 0x58, 0x36, 0x3e, + 0x29, 0xf3, 0x95, 0xf4, 0xc1, 0xb5, 0xf1, 0x2d, 0x27, 0x6f, 0x38, 0x53, 0xeb, 0x2d, 0x92, 0xd1, + 0xcf, 0xfe, 0x0e, 0x00, 0x00, 0xff, 0xff, 0xa3, 0xdd, 0xa9, 0x15, 0xf9, 0x0c, 0x00, 0x00, } diff --git a/proto/beacon/p2p/v1/types.proto b/proto/beacon/p2p/v1/types.proto index 947b8398d2..bb0776de9a 100644 --- a/proto/beacon/p2p/v1/types.proto +++ b/proto/beacon/p2p/v1/types.proto @@ -97,32 +97,8 @@ message BeaconBlock { uint64 slot = 2; bytes randao_reveal = 3; bytes pow_chain_ref = 4; - bytes active_state_root = 5; - bytes crystallized_state_root = 6; - google.protobuf.Timestamp timestamp = 7; - repeated AggregatedAttestation attestations = 8; - repeated SpecialRecord specials = 9; -} - -message CrystallizedState { - uint64 last_state_recalculation_slot = 1; - uint64 justified_streak = 2; - uint64 last_justified_slot = 3; - uint64 last_finalized_slot = 4; - uint64 validator_set_change_slot = 5; - repeated CrosslinkRecord crosslinks = 6; - repeated ValidatorRecord validators = 7; - repeated ShardAndCommitteeArray shard_and_committees_for_slots = 8; - repeated uint32 deposits_penalized_in_period = 9; - bytes validator_set_delta_hash_chain = 10; - uint32 pre_fork_version = 11; - uint32 post_fork_version = 12; - uint64 fork_slot_number =13; -} - -message ActiveState { - repeated AggregatedAttestation pending_attestations = 1; - repeated bytes recent_block_hashes = 2; - repeated SpecialRecord pending_specials = 3; - bytes randao_mix = 4; + bytes state_root = 5; + google.protobuf.Timestamp timestamp = 6; + repeated AggregatedAttestation attestations = 7; + repeated SpecialRecord specials = 8; } diff --git a/shared/params/config.go b/shared/params/config.go index 368ff64879..984c34e42f 100644 --- a/shared/params/config.go +++ b/shared/params/config.go @@ -43,7 +43,7 @@ type BeaconChainConfig struct { IncluderRewardShareQuotient uint64 // IncluderRewardShareQuotient defines the reward quotient for proposer. MaxValidatorChurnQuotient uint64 // MaxValidatorChurnQuotient defines the quotient how many validators can change each time. POWContractMerkleTreeDepth uint64 // POWContractMerkleTreeDepth defines the depth of PoW contract merkle tree. - InitialForkVersion uint32 // InitialForkVersion is used to track fork version between state transitions. + InitialForkVersion uint64 // InitialForkVersion is used to track fork version between state transitions. SimulatedBlockRandao [32]byte // SimulatedBlockRandao is a RANDAO seed stubbed in side simulated block to advance local beacon chain. ModuloBias uint64 // ModuloBias is the upper bound of validator shuffle function. Can shuffle validator lists up to that size. BootstrappedValidatorsCount uint64 // BootstrappedValidatorsCount is the number of validators we seed to start beacon chain.