diff --git a/beacon_chain.nimble b/beacon_chain.nimble index 8c0f32a08..78ce5125d 100644 --- a/beacon_chain.nimble +++ b/beacon_chain.nimble @@ -57,11 +57,8 @@ task test, "Run all tests": #buildBinary "test_fixture_ssz_static", "tests/official/", "-r -d:release -d:chronicles_log_level=DEBUG -d:const_preset=minimal" #buildBinary "test_fixture_ssz_static", "tests/official/", "-r -d:release -d:chronicles_log_level=DEBUG -d:const_preset=mainnet" - buildBinary "transition_0_9_0", "tests/", "-r -d:release -d:chronicles_log_level=DEBUG -d:const_preset=minimal" - buildBinary "transition_0_9_0", "tests/", "-r -d:release -d:chronicles_log_level=DEBUG -d:const_preset=mainnet" - - #buildBinary "all_fixtures_require_ssz", "tests/official/", "-r -d:release -d:chronicles_log_level=DEBUG -d:const_preset=minimal" - #buildBinary "all_fixtures_require_ssz", "tests/official/", "-r -d:release -d:chronicles_log_level=DEBUG -d:const_preset=mainnet" + buildBinary "all_fixtures_require_ssz", "tests/official/", "-r -d:release -d:chronicles_log_level=DEBUG -d:const_preset=minimal" + buildBinary "all_fixtures_require_ssz", "tests/official/", "-r -d:release -d:chronicles_log_level=DEBUG -d:const_preset=mainnet" # State sim; getting into 3rd epoch useful buildBinary "state_sim", "research/", "-r -d:release", "--validators=128 --slots=24" diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index a75670f0f..aef73320c 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -513,7 +513,8 @@ proc handleAttestations(node: BeaconNode, head: BlockRef, slot: Slot) = var cache = get_empty_per_epoch_cache() let epoch = compute_epoch_at_slot(slot) - committees_per_slot = get_committee_count(state, epoch) div SLOTS_PER_EPOCH + committees_per_slot = + get_committee_count_at_slot(state, epoch.compute_start_slot_at_epoch) offset = committees_per_slot * (slot mod SLOTS_PER_EPOCH) slot_start_shard = (get_start_shard(state, epoch) + offset) mod SHARD_COUNT diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index 3053ad8ae..c6d546690 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -303,7 +303,8 @@ func get_attestation_data_slot*(state: BeaconState, func get_attestation_data_slot*(state: BeaconState, data: AttestationData): Slot = get_attestation_data_slot( - state, data, get_committee_count(state, data.target.epoch)) + state, data, get_committee_count_at_slot( + state, data.target.epoch.compute_start_slot_at_epoch) * SLOTS_PER_EPOCH) # https://github.com/ethereum/eth2.0-specs/blob/v0.9.0/specs/core/0_beacon-chain.md#get_block_root_at_slot func get_block_root_at_slot*(state: BeaconState, diff --git a/beacon_chain/spec/datatypes.nim b/beacon_chain/spec/datatypes.nim index 9777b5f48..06a40049f 100644 --- a/beacon_chain/spec/datatypes.nim +++ b/beacon_chain/spec/datatypes.nim @@ -665,11 +665,6 @@ chronicles.formatIt Epoch: it.shortLog chronicles.formatIt BeaconBlock: it.shortLog chronicles.formatIt AttestationData: it.shortLog -static: - # Ensure that get_crosslink_committee(...) can access all committees, which - # requires that SHARD_COUNT >= get_committee_count(...) - doAssert SHARD_COUNT >= SLOTS_PER_EPOCH, "Shard count should match or exceed number of slots per epoch" - import json_serialization export json_serialization export writeValue, readValue diff --git a/beacon_chain/spec/helpers.nim b/beacon_chain/spec/helpers.nim index 75c648e54..75831372f 100644 --- a/beacon_chain/spec/helpers.nim +++ b/beacon_chain/spec/helpers.nim @@ -54,7 +54,7 @@ func get_active_validator_indices*(state: BeaconState, epoch: Epoch): if is_active_validator(val, epoch): result.add idx.ValidatorIndex -# https://github.com/ethereum/eth2.0-specs/blob/v0.9.0/specs/core/0_beacon-chain.md#get_committee_count +# https://github.com/ethereum/eth2.0-specs/blob/v0.9.0/specs/core/0_beacon-chain.md#get_committee_count_at_slot func get_committee_count_at_slot*(state: BeaconState, slot: Slot): uint64 = # Return the number of committees at ``slot``. let epoch = compute_epoch_at_slot(slot) @@ -68,18 +68,6 @@ func get_committee_count_at_slot*(state: BeaconState, slot: Slot): uint64 = # committees. doAssert (SLOTS_PER_EPOCH * MAX_COMMITTEES_PER_SLOT).uint64 >= result -# https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/specs/core/0_beacon-chain.md#get_committee_count -func get_committee_count*(state: BeaconState, epoch: Epoch): uint64 = - # Return the number of committees at ``epoch``. - let active_validator_indices = get_active_validator_indices(state, epoch) - let committees_per_slot = clamp( - len(active_validator_indices) div SLOTS_PER_EPOCH div TARGET_COMMITTEE_SIZE, - 1, SHARD_COUNT div SLOTS_PER_EPOCH).uint64 - result = committees_per_slot * SLOTS_PER_EPOCH - - # Otherwise, get_crosslink_committee(...) cannot access some committees. - doAssert SHARD_COUNT >= result - # https://github.com/ethereum/eth2.0-specs/blob/v0.9.0/specs/core/0_beacon-chain.md#get_current_epoch func get_current_epoch*(state: BeaconState): Epoch = # Return the current epoch. diff --git a/beacon_chain/spec/presets/mainnet.nim b/beacon_chain/spec/presets/mainnet.nim index 141b07717..32fbba5eb 100644 --- a/beacon_chain/spec/presets/mainnet.nim +++ b/beacon_chain/spec/presets/mainnet.nim @@ -24,10 +24,9 @@ const MAX_COMMITTEES_PER_SLOT* = 64 - SHARD_COUNT* {.intdefine.} = 1024 ##\ - ## Number of shards supported by the network - validators will jump around - ## between these shards and provide attestations to their state. - ## Compile with -d:SHARD_COUNT=4 for fewer shard (= better with low validator counts) + # TODO remove + SHARD_COUNT* = 2048 ##\ + ## SLOTS_PER_EPOCH * MAX_COMMITTEES_PER_SLOT TARGET_COMMITTEE_SIZE* = 2^7 ##\ ## Number of validators in the committee attesting to one shard diff --git a/beacon_chain/spec/presets/minimal.nim b/beacon_chain/spec/presets/minimal.nim index da6f9bf1c..be0e2d993 100644 --- a/beacon_chain/spec/presets/minimal.nim +++ b/beacon_chain/spec/presets/minimal.nim @@ -22,13 +22,14 @@ const # --------------------------------------------------------------- # https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/configs/minimal.yaml#L4 - # TODO remove - SHARD_COUNT* {.intdefine.} = 8 - # Changed MAX_COMMITTEES_PER_SLOT* = 4 TARGET_COMMITTEE_SIZE* = 4 + # TODO remove + SHARD_COUNT* = 32 ##\ + ## SLOTS_PER_EPOCH * MAX_COMMITTEES_PER_SLOT + # Unchanged MAX_VALIDATORS_PER_COMMITTEE* = 2048 MIN_PER_EPOCH_CHURN_LIMIT* = 4 diff --git a/beacon_chain/spec/state_transition_helpers.nim b/beacon_chain/spec/state_transition_helpers.nim index bc7e8dd7e..6fdf8f9b5 100644 --- a/beacon_chain/spec/state_transition_helpers.nim +++ b/beacon_chain/spec/state_transition_helpers.nim @@ -24,7 +24,7 @@ func shortLog*(x: Checkpoint): string = # https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/specs/core/0_beacon-chain.md#get_attesting_indices # TODO there's another one of these, check for redundancy -func get_attesting_indices*( +func get_attesting_indices( state: BeaconState, attestations: openarray[PendingAttestation], stateCache: var StateCache): HashSet[ValidatorIndex] = result = initSet[ValidatorIndex]() diff --git a/beacon_chain/spec/validator.nim b/beacon_chain/spec/validator.nim index 14e645e3a..9911acc9d 100644 --- a/beacon_chain/spec/validator.nim +++ b/beacon_chain/spec/validator.nim @@ -91,7 +91,8 @@ func get_previous_epoch*(state: BeaconState): Epoch = func get_shard_delta*(state: BeaconState, epoch: Epoch): uint64 = ## Return the number of shards to increment ``state.start_shard`` ## during ``epoch``. - min(get_committee_count(state, epoch), + min(get_committee_count_at_slot(state, epoch.compute_start_slot_at_epoch) * + SLOTS_PER_EPOCH, (SHARD_COUNT - SHARD_COUNT div SLOTS_PER_EPOCH).uint64) # https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/specs/core/0_beacon-chain.md#get_start_shard @@ -159,11 +160,12 @@ func compute_committee(indices: seq[ValidatorIndex], seed: Eth2Digest, start.int .. (endIdx.int-1), indices[stateCache.crosslink_committee_cache[key][it]]) -# https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/specs/core/0_beacon-chain.md#get_beacon_committee +# https://github.com/ethereum/eth2.0-specs/blob/v0.9.0/specs/core/0_beacon-chain.md#get_beacon_committee func get_beacon_committee*(state: BeaconState, slot: Slot, index: uint64, cache: var StateCache): seq[ValidatorIndex] = # Return the beacon committee at ``slot`` for ``index``. let epoch = compute_epoch_at_slot(slot) + # TODO use state caching for this or not? committees_per_slot = get_committee_count_at_slot(state, slot) ## This is a somewhat more fragile, but high-ROI, caching setup -- @@ -173,19 +175,16 @@ func get_beacon_committee*(state: BeaconState, slot: Slot, index: uint64, cache: cache.active_validator_indices_cache[epoch] = get_active_validator_indices(state, epoch) - if epoch notin cache.committee_count_cache: - cache.committee_count_cache[epoch] = get_committee_count(state, epoch) + # TODO remove or replace this... + #if epoch notin cache.committee_count_cache: + # cache.committee_count_cache[epoch] = get_committee_count(state, epoch) # TODO profiling & make sure caches populated compute_committee( cache.active_validator_indices_cache[epoch], get_seed(state, epoch, DOMAIN_BEACON_ATTESTER), (slot mod SLOTS_PER_EPOCH) * committees_per_slot + index, - - # TODO switch to 0.9's - # committees_per_slot * SLOTS_PER_EPOCH, - cache.committee_count_cache[epoch], - + committees_per_slot * SLOTS_PER_EPOCH, cache ) diff --git a/research/state_sim.nim b/research/state_sim.nim index 7d5a1204f..eaf14b974 100644 --- a/research/state_sim.nim +++ b/research/state_sim.nim @@ -97,7 +97,8 @@ cli do(slots = 448'u, epoch = compute_epoch_at_slot(state.slot) scass = withTimerRet(timers[tShuffle]): mapIt( - 0'u64 .. (get_committee_count(state, epoch) - 1), + 0'u64 .. (get_committee_count_at_slot(state, state.slot) * + SLOTS_PER_EPOCH - 1), get_crosslink_committee(state, epoch, (it + get_start_shard(state, epoch)) mod SHARD_COUNT, cache)) diff --git a/tests/mocking/mock_attestations.nim b/tests/mocking/mock_attestations.nim index 6eac376a8..db2b57566 100644 --- a/tests/mocking/mock_attestations.nim +++ b/tests/mocking/mock_attestations.nim @@ -108,7 +108,8 @@ proc mockAttestationImpl( let epoch = compute_epoch_at_slot(slot) epoch_start_shard = get_start_shard(state, epoch) - committees_per_slot = get_committee_count(state, epoch) div SLOTS_PER_EPOCH + committees_per_slot = get_committee_count_at_slot( + state, epoch.compute_start_slot_at_epoch) shard = ( epoch_start_shard + committees_per_slot * (slot mod SLOTS_PER_EPOCH) diff --git a/tests/spec_epoch_processing/justification_finalization_helpers.nim b/tests/spec_epoch_processing/justification_finalization_helpers.nim index bb1c6508f..0d1866b9e 100644 --- a/tests/spec_epoch_processing/justification_finalization_helpers.nim +++ b/tests/spec_epoch_processing/justification_finalization_helpers.nim @@ -22,7 +22,7 @@ iterator getShardsForSlot(state: BeaconState, slot: Slot): Shard = let epoch = compute_epoch_at_slot(slot) epoch_start_shard = get_start_shard(state, epoch) - committees_per_slot = get_committee_count(state, epoch) div SLOTS_PER_EPOCH + committees_per_slot = get_committee_count_at_slot(state, slot) shard = epoch_start_shard + committees_per_slot * (slot mod SLOTS_PER_EPOCH) for i in 0 ..< committees_per_slot.int: diff --git a/tests/testutil.nim b/tests/testutil.nim index b875fb3d9..7c56b1125 100644 --- a/tests/testutil.nim +++ b/tests/testutil.nim @@ -161,7 +161,8 @@ proc find_shard_committee( state: BeaconState, validator_index: ValidatorIndex): auto = let epoch = compute_epoch_at_slot(state.slot) var cache = get_empty_per_epoch_cache() - for shard in 0'u64 ..< get_committee_count(state, epoch): + for shard in 0'u64 ..< get_committee_count_at_slot( + state, epoch.compute_start_slot_at_epoch) * SLOTS_PER_EPOCH: let committee = get_crosslink_committee(state, epoch, (shard + get_start_shard(state, epoch)) mod SHARD_COUNT, cache) if validator_index in committee: diff --git a/tests/transition_0_9_0.nim b/tests/transition_0_9_0.nim deleted file mode 100644 index 3ee3bf04b..000000000 --- a/tests/transition_0_9_0.nim +++ /dev/null @@ -1,15 +0,0 @@ -import - official/test_fixture_bls, - official/test_fixture_operations_attester_slashings, - official/test_fixture_operations_proposer_slashings, - official/test_fixture_shuffling, - official/test_fixture_operations_block_header, - official/test_fixture_sanity_blocks, - official/test_fixture_ssz_generic_types, - official/test_fixture_sanity_slots, - official/test_fixture_operations_voluntary_exit, - official/test_fixture_ssz_uint, - official/test_fixture_operations_deposits, - spec_block_processing/test_process_attestation, - spec_block_processing/test_process_deposits, - spec_epoch_processing/test_process_justification_and_finalization