Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Explicit use of GENESIS_SLOT in fork choice #4343

Merged
merged 5 commits into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions beacon-chain/blockchain/forkchoice/process_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var ErrTargetRootNotInDB = errors.New("target root does not exist in db")
// assert target.root in store.blocks
// # Attestations cannot be from future epochs. If they are, delay consideration until the epoch arrives
// base_state = store.block_states[target.root].copy()
// assert store.time >= base_state.genesis_time + compute_start_slot_at_epoch(target.epoch) * SECONDS_PER_SLOT
// assert get_current_slot(store) >= compute_start_slot_at_epoch(target.epoch)
//
// # Attestations must be for a known block. If block is unknown, delay consideration until the block is found
// assert attestation.data.beacon_block_root in store.blocks
Expand All @@ -65,7 +65,7 @@ var ErrTargetRootNotInDB = errors.New("target root does not exist in db")
//
// # Attestations can only affect the fork choice of subsequent slots.
// # Delay consideration in the fork choice until their slot is in the past.
// assert store.time >= (attestation.data.slot + 1) * SECONDS_PER_SLOT
// assert get_current_slot(store) >= attestation.data.slot + 1
//
// # Get state at the `target` to validate attestation and calculate the committees
// indexed_attestation = get_indexed_attestation(target_state, attestation)
Expand All @@ -80,7 +80,6 @@ func (s *Store) OnAttestation(ctx context.Context, a *ethpb.Attestation) error {
defer span.End()

tgt := proto.Clone(a.Data.Target).(*ethpb.Checkpoint)
tgtSlot := helpers.StartSlot(tgt.Epoch)

// Verify beacon node has seen the target block before.
if !s.db.HasBlock(ctx, bytesutil.ToBytes32(tgt.Root)) {
Expand All @@ -99,8 +98,8 @@ func (s *Store) OnAttestation(ctx context.Context, a *ethpb.Attestation) error {
}

// Verify Attestations cannot be from future epochs.
if err := helpers.VerifySlotTime(baseState.GenesisTime, tgtSlot); err != nil {
return errors.Wrap(err, "could not verify attestation target slot")
if helpers.StartSlot(tgt.Epoch) > s.currentSlot() {
return fmt.Errorf("could not process future attestation %d, current slot %d", helpers.StartSlot(tgt.Epoch), s.currentSlot())
}

// Verify attestation beacon block is known and not from the future.
Expand All @@ -115,8 +114,8 @@ func (s *Store) OnAttestation(ctx context.Context, a *ethpb.Attestation) error {
}

// Verify attestations can only affect the fork choice of subsequent slots.
if err := helpers.VerifySlotTime(baseState.GenesisTime, a.Data.Slot+1); err != nil {
return err
if a.Data.Slot+1 > s.currentSlot() {
return fmt.Errorf("could not process attesatin until subsequent slots %d, current slot %d", a.Data.Slot+1, s.currentSlot())
}

// Use the target state to to validate attestation and calculate the committees.
Expand Down
13 changes: 9 additions & 4 deletions beacon-chain/blockchain/forkchoice/process_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
// assert block.parent_root in store.block_states
// pre_state = store.block_states[block.parent_root].copy()
// # Blocks cannot be in the future. If they are, their consideration must be delayed until the are in the past.
// assert store.time >= pre_state.genesis_time + block.slot * SECONDS_PER_SLOT
// assert get_current_slot(store) >= block.slot
// # Add new block to the store
// store.blocks[signing_root(block)] = block
// # Check block is a descendant of the finalized block
Expand Down Expand Up @@ -254,8 +254,8 @@ func (s *Store) getBlockPreState(ctx context.Context, b *ethpb.BeaconBlock) (*pb
}

// Verify block slot time is not from the feature.
if err := helpers.VerifySlotTime(preState.GenesisTime, b.Slot); err != nil {
return nil, err
if b.Slot > s.currentSlot() {
return nil, fmt.Errorf("could not process future block %d, current slot %d", b.Slot, s.currentSlot())
}

// Verify block is a descendent of a finalized block.
Expand Down Expand Up @@ -498,8 +498,13 @@ func (s *Store) shouldUpdateJustified(ctx context.Context, newJustifiedCheckpt *
return true, nil
}

// currentSlot returns the current slot based on time.
// currentSlot returns the current slot number.
func (s *Store) currentSlot() uint64 {
return params.BeaconConfig().GenesisSlot + s.slotsSinceGenesis()
}

// slotsSinceGenesis returns how many slots has passed since genesis time.
func (s *Store) slotsSinceGenesis() uint64 {
return (uint64(time.Now().Unix()) - s.genesisTime) / params.BeaconConfig().SecondsPerSlot
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be using roughtime.Now()

}

Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/blockchain/forkchoice/process_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestStore_OnBlock(t *testing.T) {
name: "block is from the feature",
blk: &ethpb.BeaconBlock{ParentRoot: randomParentRoot[:], Slot: params.BeaconConfig().FarFutureEpoch},
s: &pb.BeaconState{},
wantErrString: "could not process slot from the future",
wantErrString: "could not process future block",
},
{
name: "could not get finalized block",
Expand Down
2 changes: 2 additions & 0 deletions shared/params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type BeaconChainConfig struct {
MinGenesisActiveValidatorCount uint64 `yaml:"MIN_GENESIS_ACTIVE_VALIDATOR_COUNT"` // MinGenesisActiveValidatorCount defines how many validator deposits needed to kick off beacon chain.
MinGenesisTime uint64 `yaml:"MIN_GENESIS_TIME"` // MinGenesisTime is the time that needed to pass before kicking off beacon chain. Currently set to Jan/3/2020.
TargetAggregatorsPerCommittee uint64 // TargetAggregatorsPerCommittee defines the number of aggregators inside one committee.
GenesisSlot uint64 // GenesisSlot is the slot number when beacon chain starts.

// Gwei value constants.
MinDepositAmount uint64 `yaml:"MIN_DEPOSIT_AMOUNT"` // MinDepositAmount is the maximal amount of Gwei a validator can send to the deposit contract at once.
Expand Down Expand Up @@ -124,6 +125,7 @@ var defaultBeaconConfig = &BeaconChainConfig{
MinGenesisActiveValidatorCount: 16384,
MinGenesisTime: 1578009600,
TargetAggregatorsPerCommittee: 16,
GenesisSlot: 0,

// Gwei value constants.
MinDepositAmount: 1 * 1e9,
Expand Down