From e696c11a1e1f1ef8f9cd9458fa3704a5a1bc766a Mon Sep 17 00:00:00 2001 From: Terence Tsao Date: Wed, 6 Feb 2019 21:57:22 +0100 Subject: [PATCH 1/3] fixed epoch_processing --- beacon-chain/core/blocks/block.go | 15 ++++---- beacon-chain/core/epoch/epoch_processing.go | 38 ++++++++++----------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/beacon-chain/core/blocks/block.go b/beacon-chain/core/blocks/block.go index 5436c6bd5bd9..3ab8eacff0d0 100644 --- a/beacon-chain/core/blocks/block.go +++ b/beacon-chain/core/blocks/block.go @@ -17,17 +17,16 @@ import ( "github.com/prysmaticlabs/prysm/shared/ssz" ) -var config = params.BeaconConfig() var clock utils.Clock = &utils.RealClock{} // NewGenesisBlock returns the canonical, genesis block for the beacon chain protocol. func NewGenesisBlock(stateRoot []byte) *pb.BeaconBlock { block := &pb.BeaconBlock{ - Slot: config.GenesisSlot, - ParentRootHash32: config.ZeroHash[:], + Slot: params.BeaconConfig().GenesisSlot, + ParentRootHash32: params.BeaconConfig().ZeroHash[:], StateRootHash32: stateRoot, - RandaoRevealHash32: config.ZeroHash[:], - Signature: config.EmptySignature, + RandaoRevealHash32: params.BeaconConfig().ZeroHash[:], + Signature: params.BeaconConfig().EmptySignature, Body: &pb.BeaconBlockBody{ ProposerSlashings: []*pb.ProposerSlashing{}, AttesterSlashings: []*pb.AttesterSlashing{}, @@ -47,7 +46,7 @@ func IsRandaoValid(blockRandao []byte, stateRandao []byte) bool { // IsSlotValid compares the slot to the system clock to determine if the block is valid. func IsSlotValid(slot uint64, genesisTime time.Time) bool { - slotDuration := time.Duration(slot*config.SlotDuration) * time.Second + slotDuration := time.Duration(slot*params.BeaconConfig().SlotDuration) * time.Second validTimeThreshold := genesisTime.Add(slotDuration) return clock.Now().After(validTimeThreshold) } @@ -90,8 +89,8 @@ func BlockRoot(state *pb.BeaconState, slot uint64) ([]byte, error) { // Set state.latest_block_roots[(state.slot - 1) % LATEST_BLOCK_ROOTS_LENGTH] = previous_block_root. // If state.slot % LATEST_BLOCK_ROOTS_LENGTH == 0 append merkle_root(state.latest_block_roots) to state.batched_block_roots. func ProcessBlockRoots(state *pb.BeaconState, prevBlockRoot [32]byte) *pb.BeaconState { - state.LatestBlockRootHash32S[(state.Slot-1)%config.LatestBlockRootsLength] = prevBlockRoot[:] - if state.Slot%config.LatestBlockRootsLength == 0 { + state.LatestBlockRootHash32S[(state.Slot-1)%params.BeaconConfig().LatestBlockRootsLength] = prevBlockRoot[:] + if state.Slot%params.BeaconConfig().LatestBlockRootsLength == 0 { merkleRoot := hashutil.MerkleRoot(state.LatestBlockRootHash32S) state.BatchedBlockRootHash32S = append(state.BatchedBlockRootHash32S, merkleRoot) } diff --git a/beacon-chain/core/epoch/epoch_processing.go b/beacon-chain/core/epoch/epoch_processing.go index c2683e7e55a4..0b0cee6bedd2 100644 --- a/beacon-chain/core/epoch/epoch_processing.go +++ b/beacon-chain/core/epoch/epoch_processing.go @@ -14,15 +14,13 @@ import ( "github.com/prysmaticlabs/prysm/shared/params" ) -var config = params.BeaconConfig() - // CanProcessEpoch checks the eligibility to process epoch. // The epoch can be processed every EPOCH_LENGTH. // // Spec pseudocode definition: // If state.slot % EPOCH_LENGTH == 0: func CanProcessEpoch(state *pb.BeaconState) bool { - return state.Slot%config.EpochLength == 0 + return state.Slot%params.BeaconConfig().EpochLength == 0 } // CanProcessEth1Data checks the eligibility to process the eth1 data. @@ -31,7 +29,7 @@ func CanProcessEpoch(state *pb.BeaconState) bool { // Spec pseudocode definition: // If state.slot % ETH1_DATA_VOTING_PERIOD == 0: func CanProcessEth1Data(state *pb.BeaconState) bool { - return state.Slot%config.Eth1DataVotingPeriod == 0 + return state.Slot%params.BeaconConfig().Eth1DataVotingPeriod == 0 } // CanProcessValidatorRegistry checks the eligibility to process validator registry. @@ -49,11 +47,11 @@ func CanProcessValidatorRegistry(state *pb.BeaconState) bool { if state.FinalizedEpoch <= state.ValidatorRegistryUpdateEpoch { return false } - shardsProcessed := helpers.CurrentEpochCommitteeCount(state) * config.EpochLength + shardsProcessed := helpers.CurrentEpochCommitteeCount(state) * params.BeaconConfig().EpochLength startShard := state.CurrentEpochStartShard for i := startShard; i < shardsProcessed; i++ { - if state.LatestCrosslinks[i%config.ShardCount].Epoch <= + if state.LatestCrosslinks[i%params.BeaconConfig().ShardCount].Epoch <= state.ValidatorRegistryUpdateEpoch { return false } @@ -73,9 +71,9 @@ func CanProcessValidatorRegistry(state *pb.BeaconState) bool { // Set state.eth1_data_votes = []. // func ProcessEth1Data(state *pb.BeaconState) *pb.BeaconState { - if state.Slot%config.Eth1DataVotingPeriod == 0 { + if state.Slot%params.BeaconConfig().Eth1DataVotingPeriod == 0 { for _, eth1DataVote := range state.Eth1DataVotes { - if eth1DataVote.VoteCount*2 > config.Eth1DataVotingPeriod { + if eth1DataVote.VoteCount*2 > params.BeaconConfig().Eth1DataVotingPeriod { state.LatestEth1Data.DepositRootHash32 = eth1DataVote.Eth1Data.DepositRootHash32 state.LatestEth1Data.BlockHash32 = eth1DataVote.Eth1Data.BlockHash32 } @@ -217,7 +215,7 @@ func ProcessEjections(state *pb.BeaconState) (*pb.BeaconState, error) { var err error activeValidatorIndices := helpers.ActiveValidatorIndices(state.ValidatorRegistry, state.Slot) for _, index := range activeValidatorIndices { - if state.ValidatorBalances[index] < config.EjectionBalance { + if state.ValidatorBalances[index] < params.BeaconConfig().EjectionBalance { state, err = validators.ExitValidator(state, index) if err != nil { return nil, fmt.Errorf("could not exit validator %d: %v", index, err) @@ -254,14 +252,14 @@ func ProcessValidatorRegistry( state.CurrentCalculationEpoch = state.Slot nextStartShard := (state.CurrentEpochStartShard + - helpers.CurrentEpochCommitteeCount(state)*config.EpochLength) % - config.EpochLength + helpers.CurrentEpochCommitteeCount(state)*params.BeaconConfig().EpochLength) % + params.BeaconConfig().EpochLength state.CurrentEpochStartShard = nextStartShard var randaoMixSlot uint64 - if state.CurrentCalculationEpoch > config.SeedLookahead { + if state.CurrentCalculationEpoch > params.BeaconConfig().SeedLookahead { randaoMixSlot = state.CurrentCalculationEpoch - - config.SeedLookahead + params.BeaconConfig().SeedLookahead } mix, err := helpers.RandaoMix(state, randaoMixSlot) if err != nil { @@ -288,17 +286,17 @@ func ProcessValidatorRegistry( // LATEST_RANDAO_MIXES_LENGTH]. func ProcessPartialValidatorRegistry(state *pb.BeaconState) *pb.BeaconState { epochsSinceLastRegistryChange := (state.Slot - state.ValidatorRegistryUpdateEpoch) / - config.EpochLength + params.BeaconConfig().EpochLength if mathutil.IsPowerOf2(epochsSinceLastRegistryChange) { state.CurrentCalculationEpoch = state.Slot var randaoIndex uint64 - if state.CurrentCalculationEpoch > config.SeedLookahead { - randaoIndex = state.CurrentCalculationEpoch - config.SeedLookahead + if state.CurrentCalculationEpoch > params.BeaconConfig().SeedLookahead { + randaoIndex = state.CurrentCalculationEpoch - params.BeaconConfig().SeedLookahead } - randaoMix := state.LatestRandaoMixesHash32S[randaoIndex%config.LatestRandaoMixesLength] + randaoMix := state.LatestRandaoMixesHash32S[randaoIndex%params.BeaconConfig().LatestRandaoMixesLength] state.CurrentEpochSeedHash32 = randaoMix } return state @@ -330,9 +328,9 @@ func CleanupAttestations(state *pb.BeaconState) *pb.BeaconState { // Set state.latest_penalized_exit_balances[(e+1) % LATEST_PENALIZED_EXIT_LENGTH] = // state.latest_penalized_exit_balances[e % LATEST_PENALIZED_EXIT_LENGTH] func UpdatePenalizedExitBalances(state *pb.BeaconState) *pb.BeaconState { - epoch := state.Slot / config.EpochLength - nextPenalizedEpoch := (epoch + 1) % config.LatestPenalizedExitLength - currPenalizedEpoch := (epoch) % config.LatestPenalizedExitLength + epoch := state.Slot / params.BeaconConfig().EpochLength + nextPenalizedEpoch := (epoch + 1) % params.BeaconConfig().LatestPenalizedExitLength + currPenalizedEpoch := (epoch) % params.BeaconConfig().LatestPenalizedExitLength state.LatestPenalizedBalances[nextPenalizedEpoch] = state.LatestPenalizedBalances[currPenalizedEpoch] return state From b03e09cc7297690baefe13afae347ed2b8285caa Mon Sep 17 00:00:00 2001 From: Terence Tsao Date: Fri, 8 Feb 2019 15:52:09 +0100 Subject: [PATCH 2/3] test p2p --- beacon-chain/core/state/transition.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beacon-chain/core/state/transition.go b/beacon-chain/core/state/transition.go index 789c6cc9f4d6..db6a030ce568 100644 --- a/beacon-chain/core/state/transition.go +++ b/beacon-chain/core/state/transition.go @@ -302,7 +302,7 @@ func ProcessEpoch(state *pb.BeaconState) (*pb.BeaconState, error) { if e.CanProcessValidatorRegistry(state) { state, err = e.ProcessValidatorRegistry(state) if err != nil { - return nil, fmt.Errorf("could not process validator registry: %v", err) + return nil, fmt.Errorf("can not process validator registry: %v", err) } } else { state = e.ProcessPartialValidatorRegistry(state) From 902586ca8917471d4a83b1ad99cc7b2aa9907bb0 Mon Sep 17 00:00:00 2001 From: Terence Tsao Date: Sat, 16 Feb 2019 06:41:30 -0800 Subject: [PATCH 3/3] fixed --- beacon-chain/core/blocks/block_operations.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/beacon-chain/core/blocks/block_operations.go b/beacon-chain/core/blocks/block_operations.go index 8ab97e045984..14a9686eb988 100644 --- a/beacon-chain/core/blocks/block_operations.go +++ b/beacon-chain/core/blocks/block_operations.go @@ -343,8 +343,8 @@ func isSurroundVote(data1 *pb.AttestationData, data2 *pb.AttestationData) bool { // For each attestation in block.body.attestations: // Verify that attestation.data.slot <= state.slot - MIN_ATTESTATION_INCLUSION_DELAY < // attestation.data.slot + EPOCH_LENGTH. -// Verify that attestation.data.justified_epoch is equal to state.justified_epoch -// if attestation.data.slot >= get_epoch_start_slot(get_current_epoch(state)) else state.previous_justified_epoch. +// Verify that `attestation.data.justified_epoch` is equal to `state.justified_epoch +// if slot_to_epoch(attestation.data.slot + 1) >= get_current_epoch(state) else state.previous_justified_epoch`. // Verify that attestation.data.justified_block_root is equal to // get_block_root(state, get_epoch_start_slot(attestation.data.justified_epoch)). // Verify that either attestation.data.latest_crosslink_root or @@ -400,10 +400,10 @@ func verifyAttestation(beaconState *pb.BeaconState, att *pb.Attestation, verifyS beaconState.Slot, ) } - // Verify that attestation.data.justified_epoch is equal to state.justified_epoch - // if attestation.data.slot >= get_epoch_start_slot(get_current_epoch(state)) - // else state.previous_justified_epoch. - if att.Data.Slot >= helpers.StartSlot(helpers.SlotToEpoch(beaconState.Slot)) { + // Verify that `attestation.data.justified_epoch` is equal to `state.justified_epoch + // if slot_to_epoch(attestation.data.slot + 1) >= get_current_epoch(state) + // else state.previous_justified_epoch`. + if helpers.SlotToEpoch(att.Data.Slot+1) >= helpers.CurrentEpoch(beaconState) { if att.Data.JustifiedEpoch != beaconState.JustifiedEpoch { return fmt.Errorf( "expected attestation.JustifiedEpoch == state.JustifiedEpoch, received %d == %d",