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

Update some of client to v0.3 spec #1733

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions beacon-chain/chaintest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test_cases:
- config:
epoch_length: 64
deposits_for_chain_start: 1000
num_slots: 32 # Testing advancing state to slot < EpochLength
num_slots: 32 # Testing advancing state to slot < SlotsPerEpoch
results:
slot: 32
num_validators: 1000
Expand Down Expand Up @@ -81,7 +81,7 @@ test_cases:
skip_slots: [10, 20]
epoch_length: 64
deposits_for_chain_start: 1000
num_slots: 128 # Testing advancing state's slot == 2*EpochLength
num_slots: 128 # Testing advancing state's slot == 2*SlotsPerEpoch
deposits:
- slot: 10
amount: 32
Expand Down
10 changes: 5 additions & 5 deletions beacon-chain/chaintest/tests/state-tests/block-processing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ test_cases:
- config:
slots_per_epoch: 64
deposits_for_chain_start: 64
num_slots: 32 # Testing advancing state to slot < EpochLength
num_slots: 32 # Testing advancing state to slot < SlotsPerEpoch
results:
slot: 9223372036854775840
num_validators: 64
- config:
slots_per_epoch: 64
deposits_for_chain_start: 64
num_slots: 64 # Testing advancing state to exactly slot == EpochLength
num_slots: 64 # Testing advancing state to exactly slot == SlotsPerEpoch
deposits:
- slot: 9223372036854775809
amount: 32
Expand Down Expand Up @@ -49,9 +49,9 @@ test_cases:
slashable_attestation_1_justified_epoch: 0
slashable_attestation_2_justified_epoch: 1
slashable_attestation_1_custody_bitfield: !binary "F"
slashable_attestation_1_validator_indices: [1,2,3,4,5,6,7,51]
slashable_attestation_1_validator_indices: [1, 2, 3, 4, 5, 6, 7, 51]
slashable_attestation_2_custody_bitfield: !binary "F"
slashable_attestation_2_validator_indices: [1,2,3,4,5,6,7,51]
slashable_attestation_2_validator_indices: [1, 2, 3, 4, 5, 6, 7, 51]
validator_exits:
- epoch: 144115188075855872
validator_index: 45 # At slot 9223372036854775868, validator at index 45 triggers a voluntary exit
Expand All @@ -65,7 +65,7 @@ test_cases:
# skip_slots: [10, 20]
# slots_per_epoch: 64
# deposits_for_chain_start: 1000
# num_slots: 128 # Testing advancing state's slot == 2*EpochLength
# num_slots: 128 # Testing advancing state's slot == 2*SlotsPerEpoch
# deposits:
# - slot: 10
# amount: 32
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/core/blocks/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestGenesisBlock_InitializedCorrectly(t *testing.T) {

func TestBlockRootAtSlot_AccurateBlockRoot(t *testing.T) {
if params.BeaconConfig().SlotsPerEpoch != 64 {
t.Errorf("epochLength should be 64 for these tests to pass")
t.Errorf("slotsPerEpoch should be 64 for these tests to pass")
}
var blockRoots [][]byte

Expand Down Expand Up @@ -101,7 +101,7 @@ func TestBlockRootAtSlot_AccurateBlockRoot(t *testing.T) {

func TestBlockRootAtSlot_OutOfBounds(t *testing.T) {
if params.BeaconConfig().SlotsPerEpoch != 64 {
t.Errorf("epochLength should be 64 for these tests to pass")
t.Errorf("slotsPerEpoch should be 64 for these tests to pass")
}

var blockRoots [][]byte
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/core/blocks/validity_conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func IsValidBlock(

// 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-params.BeaconConfig().GenesisSlot)*params.BeaconConfig().SecondsPerSlot) * time.Second
validTimeThreshold := genesisTime.Add(slotDuration)
secondsPerSlot := time.Duration((slot-params.BeaconConfig().GenesisSlot)*params.BeaconConfig().SecondsPerSlot) * time.Second
validTimeThreshold := genesisTime.Add(secondsPerSlot)
now := clock.Now()
if !now.After(validTimeThreshold) {
log.Infof("Waiting for slot to be valid. local clock: %v, genesis+slot: %v",
Expand Down
23 changes: 11 additions & 12 deletions beacon-chain/core/epoch/epoch_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ func CurrentAttestations(state *pb.BeaconState) []*pb.PendingAttestation {
return currentEpochAttestations
}

// CurrentBoundaryAttestations returns the pending attestations from
// CurrentEpochBoundaryAttestations returns the pending attestations from
// the epoch's boundary block.
//
// Spec pseudocode definition:
// return [a for a in current_epoch_attestations if a.data.epoch_boundary_root ==
// get_block_root(state, get_epoch_start_slot(current_epoch)) and
// a.data.justified_epoch == state.justified_epoch].
func CurrentBoundaryAttestations(
// get_block_root(state, get_epoch_start_slot(current_epoch))
func CurrentEpochBoundaryAttestations(
state *pb.BeaconState,
currentEpochAttestations []*pb.PendingAttestation,
) ([]*pb.PendingAttestation, error) {
Expand Down Expand Up @@ -109,31 +108,31 @@ func PrevJustifiedAttestations(
return prevJustifiedAttestations
}

// PrevBoundaryAttestations returns the boundary attestations
// PrevEpochBoundaryAttestations returns the boundary attestations
// at the start of the previous epoch.
//
// Spec pseudocode definition:
// return [a for a in previous_epoch_justified_attestations
// return [a for a in previous_epoch_attestations
// if a.epoch_boundary_root == get_block_root(state, get_epoch_start_slot(previous_epoch)]
func PrevBoundaryAttestations(
func PrevEpochBoundaryAttestations(
state *pb.BeaconState,
prevEpochJustifiedAttestations []*pb.PendingAttestation,
prevEpochAttestations []*pb.PendingAttestation,
) ([]*pb.PendingAttestation, error) {

var prevBoundaryAttestations []*pb.PendingAttestation
var prevEpochBoundaryAttestations []*pb.PendingAttestation

prevBoundaryBlockRoot, err := block.BlockRoot(state,
helpers.StartSlot(helpers.PrevEpoch(state)))
if err != nil {
return nil, err
}

for _, attestation := range prevEpochJustifiedAttestations {
for _, attestation := range prevEpochAttestations {
if bytes.Equal(attestation.Data.EpochBoundaryRootHash32, prevBoundaryBlockRoot) {
prevBoundaryAttestations = append(prevBoundaryAttestations, attestation)
prevEpochBoundaryAttestations = append(prevEpochBoundaryAttestations, attestation)
}
}
return prevBoundaryAttestations, nil
return prevEpochBoundaryAttestations, nil
}

// PrevHeadAttestations returns the pending attestations from
Expand Down
10 changes: 5 additions & 5 deletions beacon-chain/core/epoch/epoch_operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ func TestEpochBoundaryAttestations_AccurateAttestationData(t *testing.T) {
JustifiedEpoch: params.BeaconConfig().GenesisEpoch,
}

if _, err := CurrentBoundaryAttestations(state, epochAttestations); err == nil {
t.Fatal("CurrentBoundaryAttestations should have failed with empty block root hash")
if _, err := CurrentEpochBoundaryAttestations(state, epochAttestations); err == nil {
t.Fatal("CurrentEpochBoundaryAttestations should have failed with empty block root hash")
}

state.Slot = params.BeaconConfig().SlotsPerEpoch + params.BeaconConfig().GenesisSlot + 1
epochBoundaryAttestation, err := CurrentBoundaryAttestations(state, epochAttestations)
epochBoundaryAttestation, err := CurrentEpochBoundaryAttestations(state, epochAttestations)
if err != nil {
t.Fatalf("CurrentBoundaryAttestations failed: %v", err)
t.Fatalf("CurrentEpochBoundaryAttestations failed: %v", err)
}

if epochBoundaryAttestation[0].Data.JustifiedEpoch != params.BeaconConfig().GenesisEpoch {
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestPrevEpochBoundaryAttestations_AccurateAttestationData(t *testing.T) {
JustifiedEpoch: params.BeaconConfig().GenesisEpoch,
}

prevEpochBoundaryAttestation, err := PrevBoundaryAttestations(state, epochAttestations)
prevEpochBoundaryAttestation, err := PrevEpochBoundaryAttestations(state, epochAttestations)
if err != nil {
t.Fatalf("EpochBoundaryAttestations failed: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/core/epoch/epoch_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func ProcessEjections(state *pb.BeaconState) (*pb.BeaconState, error) {
// Spec pseudocode definition:
// Set state.previous_epoch_randao_mix = state.current_epoch_randao_mix
// Set state.previous_calculation_epoch = state.current_calculation_epoch
// Set state.previous_epoch_seed = state.current_epoch_seed.
// Set state.previous_shuffling_seed = state.current_shuffling_seed.
func ProcessPrevSlotShardSeed(state *pb.BeaconState) *pb.BeaconState {
state.PreviousShufflingEpoch = state.CurrentShufflingEpoch
state.PreviousShufflingStartShard = state.CurrentShufflingStartShard
Expand Down Expand Up @@ -286,7 +286,7 @@ func ProcessCurrSlotShardSeed(state *pb.BeaconState) (*pb.BeaconState, error) {
// If epochs_since_last_registry_update > 1 and
// epochs_since_last_registry_change is an exact power of 2:
// set state.current_calculation_epoch = next_epoch
// set state.current_epoch_seed = generate_seed(
// set state.current_shuffling_seed = generate_seed(
// state, state.current_calculation_epoch)
func ProcessPartialValidatorRegistry(state *pb.BeaconState) (*pb.BeaconState, error) {
epochsSinceLastRegistryChange := helpers.CurrentEpoch(state) -
Expand Down
24 changes: 12 additions & 12 deletions beacon-chain/core/epoch/epoch_processing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,27 +418,27 @@ func TestCleanupAttestations_RemovesFromLastEpoch(t *testing.T) {
if params.BeaconConfig().SlotsPerEpoch != 64 {
t.Errorf("SlotsPerEpoch should be 64 for these tests to pass")
}
epochLength := params.BeaconConfig().SlotsPerEpoch
slotsPerEpoch := params.BeaconConfig().SlotsPerEpoch
state := &pb.BeaconState{
Slot: epochLength,
Slot: slotsPerEpoch,
LatestAttestations: []*pb.PendingAttestation{
{Data: &pb.AttestationData{Slot: 1}},
{Data: &pb.AttestationData{Slot: epochLength - 10}},
{Data: &pb.AttestationData{Slot: epochLength}},
{Data: &pb.AttestationData{Slot: epochLength + 1}},
{Data: &pb.AttestationData{Slot: epochLength + 20}},
{Data: &pb.AttestationData{Slot: slotsPerEpoch - 10}},
{Data: &pb.AttestationData{Slot: slotsPerEpoch}},
{Data: &pb.AttestationData{Slot: slotsPerEpoch + 1}},
{Data: &pb.AttestationData{Slot: slotsPerEpoch + 20}},
{Data: &pb.AttestationData{Slot: 32}},
{Data: &pb.AttestationData{Slot: 33}},
{Data: &pb.AttestationData{Slot: 2 * epochLength}},
{Data: &pb.AttestationData{Slot: 2 * slotsPerEpoch}},
},
}
wanted := &pb.BeaconState{
Slot: epochLength,
Slot: slotsPerEpoch,
LatestAttestations: []*pb.PendingAttestation{
{Data: &pb.AttestationData{Slot: epochLength}},
{Data: &pb.AttestationData{Slot: epochLength + 1}},
{Data: &pb.AttestationData{Slot: epochLength + 20}},
{Data: &pb.AttestationData{Slot: 2 * epochLength}},
{Data: &pb.AttestationData{Slot: slotsPerEpoch}},
{Data: &pb.AttestationData{Slot: slotsPerEpoch + 1}},
{Data: &pb.AttestationData{Slot: slotsPerEpoch + 20}},
{Data: &pb.AttestationData{Slot: 2 * slotsPerEpoch}},
},
}
newState := CleanupAttestations(state)
Expand Down
10 changes: 5 additions & 5 deletions beacon-chain/core/helpers/committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func NextEpochCommitteeCount(state *pb.BeaconState) uint64 {
// in that committee.
// def get_crosslink_committees_at_slot(state: BeaconState,
// slot: SlotNumber,
// registry_change=False: bool) -> List[Tuple[List[ValidatorIndex], ShardNumber]]:
// registry_change=False: bool) -> List[Tuple[List[ValidatorIndex], Shard]]:
// """
// Return the list of ``(committee, shard)`` tuples for the ``slot``.
//
Expand All @@ -135,7 +135,7 @@ func NextEpochCommitteeCount(state *pb.BeaconState) uint64 {
//
// if epoch == current_epoch:
// committees_per_epoch = get_current_epoch_committee_count(state)
// seed = state.current_epoch_seed
// seed = state.current_shuffling_seed
// shuffling_epoch = state.current_calculation_epoch
// shuffling_start_shard = state.current_epoch_start_shard
// elif epoch == previous_epoch:
Expand Down Expand Up @@ -272,7 +272,7 @@ func CrosslinkCommitteesAtSlot(
// Spec pseudocode definition:
// def get_shuffling(seed: Bytes32,
// validators: List[Validator],
// epoch: EpochNumber) -> List[List[ValidatorIndex]]
// epoch: Epoch) -> List[List[ValidatorIndex]]
// """
// Shuffle ``validators`` into crosslink committees seeded by ``seed`` and ``epoch``.
// Return a list of ``committees_per_epoch`` committees where each
Expand Down Expand Up @@ -433,7 +433,7 @@ func VerifyBitfield(bitfield []byte, committeeSize int) (bool, error) {
// def get_next_epoch_committee_assignment(
// state: BeaconState,
// validator_index: ValidatorIndex,
// registry_change: bool) -> Tuple[List[ValidatorIndex], ShardNumber, SlotNumber, bool]:
// registry_change: bool) -> Tuple[List[ValidatorIndex], Shard, SlotNumber, bool]:
// """
// Return the committee assignment in the next epoch for ``validator_index`` and ``registry_change``.
// ``assignment`` returned is a tuple of the following form:
Expand All @@ -453,7 +453,7 @@ func VerifyBitfield(bitfield []byte, committeeSize int) (bool, error) {
// registry_change=registry_change,
// )
// selected_committees = [
// committee # Tuple[List[ValidatorIndex], ShardNumber]
// committee # Tuple[List[ValidatorIndex], Shard]
// for committee in crosslink_committees
// if validator_index in committee[0]
// ]
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/core/helpers/randao.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
//
// Spec pseudocode definition:
// def generate_seed(state: BeaconState,
// epoch: EpochNumber) -> Bytes32:
// epoch: Epoch) -> Bytes32:
// """
// Generate a seed for the given ``epoch``.
// """
Expand All @@ -39,11 +39,11 @@ func GenerateSeed(state *pb.BeaconState, wantedEpoch uint64) ([32]byte, error) {
//
// Spec pseudocode definition:
// def get_active_index_root(state: BeaconState,
// epoch: EpochNumber) -> Bytes32:
// epoch: Epoch) -> Bytes32:
// """
// Return the index root at a recent ``epoch``.
// """
// assert get_current_epoch(state) - LATEST_INDEX_ROOTS_LENGTH + ACTIVATION_EXIT_DELAY < epoch <= get_current_epoch(state) + ENTRY_EXIT_DELAY
// assert get_current_epoch(state) - LATEST_INDEX_ROOTS_LENGTH + ACTIVATION_EXIT_DELAY < epoch <= get_current_epoch(state) + ACTIVATION_EXIT_DELAY
// return state.latest_index_roots[epoch % LATEST_INDEX_ROOTS_LENGTH]
func ActiveIndexRoot(state *pb.BeaconState, wantedEpoch uint64) ([]byte, error) {
var earliestEpoch uint64
Expand All @@ -63,7 +63,7 @@ func ActiveIndexRoot(state *pb.BeaconState, wantedEpoch uint64) ([]byte, error)
//
// Spec pseudocode definition:
// def get_randao_mix(state: BeaconState,
// epoch: EpochNumber) -> Bytes32:
// epoch: Epoch) -> Bytes32:
// """
// Return the randao mix at a recent ``epoch``.
// """
Expand Down
6 changes: 3 additions & 3 deletions beacon-chain/core/helpers/slot_epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// SlotToEpoch returns the epoch number of the input slot.
//
// Spec pseudocode definition:
// def slot_to_epoch(slot: SlotNumber) -> EpochNumber:
// def slot_to_epoch(slot: SlotNumber) -> Epoch:
// return slot // SLOTS_PER_EPOCH
func SlotToEpoch(slot uint64) uint64 {
return slot / params.BeaconConfig().SlotsPerEpoch
Expand All @@ -18,7 +18,7 @@ func SlotToEpoch(slot uint64) uint64 {
// the slot number stored in beacon state.
//
// Spec pseudocode definition:
// def get_current_epoch(state: BeaconState) -> EpochNumber:
// def get_current_epoch(state: BeaconState) -> Epoch:
// return slot_to_epoch(state.slot)
func CurrentEpoch(state *pb.BeaconState) uint64 {
return SlotToEpoch(state.Slot)
Expand Down Expand Up @@ -50,7 +50,7 @@ func NextEpoch(state *pb.BeaconState) uint64 {
// current epoch.
//
// Spec pseudocode definition:
// def get_epoch_start_slot(epoch: EpochNumber) -> SlotNumber:
// def get_epoch_start_slot(epoch: Epoch) -> SlotNumber:
// return epoch * SLOTS_PER_EPOCH
func StartSlot(epoch uint64) uint64 {
return epoch * params.BeaconConfig().SlotsPerEpoch
Expand Down
6 changes: 3 additions & 3 deletions beacon-chain/core/helpers/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// is active or not.
//
// Spec pseudocode definition:
// def is_active_validator(validator: Validator, epoch: EpochNumber) -> bool:
// def is_active_validator(validator: Validator, epoch: Epoch) -> bool:
// """
// Check if ``validator`` is active.
// """
Expand All @@ -25,7 +25,7 @@ func IsActiveValidator(validator *pb.Validator, epoch uint64) bool {
// and returns their indices in a list.
//
// Spec pseudocode definition:
// def get_active_validator_indices(validators: List[Validator], epoch: EpochNumber) -> List[ValidatorIndex]:
// def get_active_validator_indices(validators: List[Validator], epoch: Epoch) -> List[ValidatorIndex]:
// """
// Get indices of active validators from ``validators``.
// """
Expand All @@ -45,7 +45,7 @@ func ActiveValidatorIndices(validators []*pb.Validator, epoch uint64) []uint64 {
// the validator is eligible for activation and exit.
//
// Spec pseudocode definition:
// def get_entry_exit_effect_epoch(epoch: EpochNumber) -> EpochNumber:
// def get_entry_exit_effect_epoch(epoch: Epoch) -> Epoch:
// """
// An entry or exit triggered in the ``epoch`` given by the input takes effect at
// the epoch given by the output.
Expand Down
6 changes: 3 additions & 3 deletions beacon-chain/core/state/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ func ProcessEpoch(state *pb.BeaconState) (*pb.BeaconState, error) {
currentAttestations := e.CurrentAttestations(state)
log.Infof("Number of current epoch attestations: %d", len(currentAttestations))

currentBoundaryAttestations, err := e.CurrentBoundaryAttestations(state, currentAttestations)
currentEpochBoundaryAttestations, err := e.CurrentEpochBoundaryAttestations(state, currentAttestations)
if err != nil {
return nil, fmt.Errorf("could not get current boundary attestations: %v", err)
}

currentBoundaryAttesterIndices, err := v.ValidatorIndices(state, currentBoundaryAttestations)
currentBoundaryAttesterIndices, err := v.ValidatorIndices(state, currentEpochBoundaryAttestations)
if err != nil {
return nil, fmt.Errorf("could not get current boundary attester indices: %v", err)
}
Expand All @@ -202,7 +202,7 @@ func ProcessEpoch(state *pb.BeaconState) (*pb.BeaconState, error) {

// Calculate the attesting balances of validator justifying epoch boundary block
// at the start of previous epoch.
prevEpochBoundaryAttestations, err := e.PrevBoundaryAttestations(state, prevEpochAttestations)
prevEpochBoundaryAttestations, err := e.PrevEpochBoundaryAttestations(state, prevEpochAttestations)
if err != nil {
return nil, fmt.Errorf("could not get prev boundary attestations: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/validators/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func prepareValidatorForWithdrawal(state *pb.BeaconState, idx uint64) *pb.Beacon
// the validator is eligible for activation and exit.
//
// Spec pseudocode definition:
// def get_entry_exit_effect_epoch(epoch: EpochNumber) -> EpochNumber:
// def get_entry_exit_effect_epoch(epoch: Epoch) -> Epoch:
// """
// An entry or exit triggered in the ``epoch`` given by the input takes effect at
// the epoch given by the output.
Expand Down
Loading