From ad3de5fde9fa88c45e924fde397f5f7171e4e950 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Mon, 28 Jan 2019 20:15:00 -0800 Subject: [PATCH] Uint24 -> ValidatorIndex; Fork.pre_fork_version -> Fork.previous_version; Fork.post_fork_version -> Fork.current_version --- beacon_chain/spec/beaconstate.nim | 44 +++++++++++++++---------------- beacon_chain/spec/datatypes.nim | 24 ++++++++--------- beacon_chain/spec/helpers.nim | 15 +++++------ beacon_chain/spec/validator.nim | 11 ++++---- beacon_chain/ssz.nim | 20 +++++++------- beacon_chain/state_transition.nim | 34 ++++++++++++------------ tests/test_ssz.nim | 10 +++---- tests/test_validator.nim | 2 +- tests/testutil.nim | 6 ++--- 9 files changed, 81 insertions(+), 85 deletions(-) diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index 51cae3fff..4b013b810 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -10,7 +10,7 @@ import ../extras, ../ssz, ./crypto, ./datatypes, ./digest, ./helpers, ./validator -func get_effective_balance*(state: BeaconState, index: Uint24): uint64 = +func get_effective_balance*(state: BeaconState, index: ValidatorIndex): uint64 = # Validators collect rewards which increases their balance but not their # influence. Validators may also lose balance if they fail to do their duty # in which case their influence decreases. Once they drop below a certain @@ -18,7 +18,7 @@ func get_effective_balance*(state: BeaconState, index: Uint24): uint64 = min(state.validator_balances[index], MAX_DEPOSIT_AMOUNT) func sum_effective_balances*( - state: BeaconState, validator_indices: openArray[Uint24]): uint64 = + state: BeaconState, validator_indices: openArray[ValidatorIndex]): uint64 = # TODO spec - add as helper? Used pretty often for index in validator_indices: result += get_effective_balance(state, index) @@ -51,7 +51,7 @@ func process_deposit(state: var BeaconState, proof_of_possession: ValidatorSig, withdrawal_credentials: Eth2Digest, randao_commitment: Eth2Digest, - custody_commitment: Eth2Digest) : Uint24 = + custody_commitment: Eth2Digest) : ValidatorIndex = ## Process a deposit from Ethereum 1.0. if false: @@ -86,11 +86,11 @@ func process_deposit(state: var BeaconState, if index.isNone(): state.validator_registry.add(validator) state.validator_balances.add(deposit) - (len(state.validator_registry) - 1).Uint24 + (len(state.validator_registry) - 1).ValidatorIndex else: state.validator_registry[index.get()] = validator state.validator_balances[index.get()] = deposit - index.get().Uint24 + index.get().ValidatorIndex else: # Increase balance by deposit amount let index = validator_pubkeys.find(pubkey) @@ -99,16 +99,15 @@ func process_deposit(state: var BeaconState, withdrawal_credentials state.validator_balances[index] += deposit - index.Uint24 + index.ValidatorIndex func get_entry_exit_effect_epoch*(epoch: EpochNumber): EpochNumber = ## An entry or exit triggered in the ``epoch`` given by the input takes effect at ## the epoch given by the output. epoch + 1 + ENTRY_EXIT_DELAY -# TODO: Uint24 -> ValidatorIndex func activate_validator(state: var BeaconState, - index: Uint24, + index: ValidatorIndex, genesis: bool) = ## Activate the validator with the given ``index``. let validator = addr state.validator_registry[index] @@ -116,14 +115,14 @@ func activate_validator(state: var BeaconState, validator.activation_epoch = if genesis: GENESIS_EPOCH else: get_entry_exit_effect_epoch(get_current_epoch(state)) func initiate_validator_exit(state: var BeaconState, - index: Uint24) = + index: ValidatorIndex) = ## Initiate exit for the validator with the given ``index``. var validator = state.validator_registry[index] validator.status_flags = validator.status_flags or INITIATED_EXIT state.validator_registry[index] = validator func exit_validator*(state: var BeaconState, - index: Uint24) = + index: ValidatorIndex) = ## Exit the validator with the given ``index``. let validator = addr state.validator_registry[index] @@ -155,16 +154,16 @@ func process_penalties_and_exits(state: var BeaconState) = total_at_start = state.latest_penalized_exit_balances[(e + 1) mod LATEST_PENALIZED_EXIT_LENGTH] total_at_end = state.latest_penalized_exit_balances[e] total_penalties = total_at_end - total_at_start - penalty = get_effective_balance(state, index.Uint24) * min(total_penalties * 3, total_balance) div total_balance + penalty = get_effective_balance(state, index.ValidatorIndex) * min(total_penalties * 3, total_balance) div total_balance state.validator_balances[index] -= penalty ## 'state' is of type which cannot be captured as it ## would violate memory safety, when using nested function approach in ## spec directly. That said, the spec approach evidently is not meant, ## based on its abundant and pointless memory copies, for production. - var eligible_indices : seq[Uint24] = @[] + var eligible_indices : seq[ValidatorIndex] = @[] for i in 0 ..< len(state.validator_registry): - eligible_indices.add i.Uint24 + eligible_indices.add i.ValidatorIndex ## TODO figure out that memory safety issue, which would come up again when ## sorting, and then actually do withdrawals @@ -194,11 +193,10 @@ func get_initial_beacon_state*( # Misc slot: GENESIS_SLOT, genesis_time: genesis_time, - # TODO pre_fork_version -> previous_version, post_fork_version -> current_version, # rm fork_slot init in favor of epoch fork: Fork( - pre_fork_version: GENESIS_FORK_VERSION, - post_fork_version: GENESIS_FORK_VERSION, + previous_version: GENESIS_FORK_VERSION, + current_version: GENESIS_FORK_VERSION, fork_slot: GENESIS_SLOT, ), @@ -241,7 +239,7 @@ func get_initial_beacon_state*( # Process initial activations for validator_index in 0 ..< state.validator_registry.len: - let vi = validator_index.Uint24 + let vi = validator_index.ValidatorIndex if get_effective_balance(state, vi) > MAX_DEPOSIT_AMOUNT: activate_validator(state, vi, true) @@ -255,7 +253,7 @@ func get_block_root*(state: BeaconState, func get_attestation_participants*(state: BeaconState, attestation_data: AttestationData, - aggregation_bitfield: seq[byte]): seq[Uint24] = + aggregation_bitfield: seq[byte]): seq[ValidatorIndex] = ## Attestation participants in the attestation data are called out in a ## bit field that corresponds to the committee of the shard at the time - this ## function converts it to list of indices in to BeaconState.validators @@ -270,7 +268,7 @@ func get_attestation_participants*(state: BeaconState, # TODO investigate functional library / approach to help avoid loop bugs assert any( crosslink_committees, - func (x: tuple[a: seq[Uint24], b: uint64]): bool = x[1] == attestation_data.shard) + func (x: tuple[a: seq[ValidatorIndex], b: uint64]): bool = x[1] == attestation_data.shard) let crosslink_committee = mapIt( filterIt(crosslink_committees, it.b == attestation_data.shard), it.a)[0] @@ -312,12 +310,12 @@ func update_validator_registry*(state: var BeaconState) = if validator.activation_epoch > get_entry_exit_effect_epoch(current_epoch) and state.validator_balances[index] >= MAX_DEPOSIT_AMOUNT: # Check the balance churn would be within the allowance - balance_churn += get_effective_balance(state, index.Uint24) + balance_churn += get_effective_balance(state, index.ValidatorIndex) if balance_churn > max_balance_churn: break # Activate validator - activate_validator(state, index.Uint24, false) + activate_validator(state, index.ValidatorIndex, false) # Exit validators within the allowable balance churn balance_churn = 0 @@ -325,12 +323,12 @@ func update_validator_registry*(state: var BeaconState) = if validator.exit_epoch > get_entry_exit_effect_epoch(current_epoch) and ((validator.status_flags and INITIATED_EXIT) == INITIATED_EXIT): # Check the balance churn would be within the allowance - balance_churn += get_effective_balance(state, index.Uint24) + balance_churn += get_effective_balance(state, index.ValidatorIndex) if balance_churn > max_balance_churn: break # Exit validator - exit_validator(state, index.Uint24) + exit_validator(state, index.ValidatorIndex) state.validator_registry_update_epoch = current_epoch diff --git a/beacon_chain/spec/datatypes.nim b/beacon_chain/spec/datatypes.nim index 2050fb6ed..f46576989 100644 --- a/beacon_chain/spec/datatypes.nim +++ b/beacon_chain/spec/datatypes.nim @@ -165,13 +165,13 @@ const MAX_EXITS* = 2^4 type - Uint24* = range[0'u32 .. 0xFFFFFF'u32] # TODO: wrap-around + ValidatorIndex* = range[0'u32 .. 0xFFFFFF'u32] # TODO: wrap-around SlotNumber* = uint64 EpochNumber* = uint64 # https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#data-structures ProposerSlashing* = object - proposer_index*: Uint24 + proposer_index*: ValidatorIndex proposal_data_1*: ProposalSignedData proposal_signature_1*: ValidatorSig proposal_data_2*: ProposalSignedData @@ -182,10 +182,10 @@ type slashable_vote_data_2*: SlashableVoteData SlashableVoteData* = object - aggregate_signature_poc_0_indices*: seq[Uint24] ##\ + aggregate_signature_poc_0_indices*: seq[ValidatorIndex] ##\ ## Proof-of-custody indices (0 bits) - aggregate_signature_poc_1_indices*: seq[Uint24] ##\ + aggregate_signature_poc_1_indices*: seq[ValidatorIndex] ##\ ## Proof-of-custody indices (1 bits) data*: AttestationData @@ -254,7 +254,7 @@ type # Minimum slot for processing exit slot*: uint64 # Index of the exiting validator - validator_index*: Uint24 + validator_index*: ValidatorIndex # Validator signature signature*: ValidatorSig @@ -395,7 +395,7 @@ type ShardCommittee* = object shard*: uint64 - committee*: seq[Uint24] ##\ + committee*: seq[ValidatorIndex] ##\ ## Committe participants that get to attest to blocks on this shard - ## indices into BeaconState.validator_registry @@ -420,13 +420,13 @@ type slot_included*: uint64 # Slot in which it was included Fork* = object - pre_fork_version*: uint64 # Previous fork version - post_fork_version*: uint64 # Post fork version - fork_slot*: uint64 # Fork slot number + previous_version*: uint64 # Previous fork version + current_version*: uint64 # Current fork version + fork_slot*: uint64 # Fork slot number TODO should be epoch ValidatorRegistryDeltaBlock* = object latest_registry_delta_root*: Eth2Digest - validator_index*: Uint24 + validator_index*: ValidatorIndex pubkey*: ValidatorPubKey slot*: uint64 flag*: ValidatorSetDeltaFlags @@ -455,10 +455,10 @@ when true: proc read*(rlp: var Rlp, T: type ValidatorPubKey): T {.inline.} = discard - proc append*(rlpWriter: var RlpWriter, value: Uint24) = + proc append*(rlpWriter: var RlpWriter, value: ValidatorIndex) = discard - proc read*(rlp: var Rlp, T: type Uint24): T {.inline.} = + proc read*(rlp: var Rlp, T: type ValidatorIndex): T {.inline.} = discard proc append*(rlpWriter: var RlpWriter, value: ValidatorSig) = diff --git a/beacon_chain/spec/helpers.nim b/beacon_chain/spec/helpers.nim index 32e7b7175..101146a41 100644 --- a/beacon_chain/spec/helpers.nim +++ b/beacon_chain/spec/helpers.nim @@ -53,9 +53,9 @@ func shuffle*[T](values: seq[T], seed: Eth2Digest): seq[T] = # Read 3-bytes of `source` as a 24-bit big-endian integer. let sample_from_source = - source.data[pos].Uint24 shl 16 or - source.data[pos+1].Uint24 shl 8 or - source.data[pos+2].Uint24 + source.data[pos].ValidatorIndex shl 16 or + source.data[pos+1].ValidatorIndex shl 8 or + source.data[pos+2].ValidatorIndex # Sample values greater than or equal to `sample_max` will cause # modulo bias when mapped into the `remaining` range. @@ -120,8 +120,8 @@ func integer_squareroot*(n: SomeInteger): SomeInteger = x func get_fork_version*(fork: Fork, slot: uint64): uint64 = - if slot < fork.fork_slot: fork.pre_fork_version - else: fork.post_fork_version + if slot < fork.fork_slot: fork.previous_version + else: fork.current_version func get_domain*( fork: Fork, slot: uint64, domain_type: SignatureDomain): uint64 = @@ -173,12 +173,11 @@ func is_active_validator*(validator: Validator, epoch: EpochNumber): bool = ### Checks if validator is active validator.activation_epoch <= epoch and epoch < validator.exit_epoch -# TODO Uint24 -> ValidatorIndex -func get_active_validator_indices*(validators: openArray[Validator], epoch: EpochNumber): seq[Uint24] = +func get_active_validator_indices*(validators: openArray[Validator], epoch: EpochNumber): seq[ValidatorIndex] = ## Gets indices of active validators from validators for idx, val in validators: if is_active_validator(val, epoch): - result.add idx.Uint24 + result.add idx.ValidatorIndex func get_epoch_committee_count*(active_validator_count: int): uint64 = clamp( diff --git a/beacon_chain/spec/validator.nim b/beacon_chain/spec/validator.nim index 5c04910c9..3b11a45cf 100644 --- a/beacon_chain/spec/validator.nim +++ b/beacon_chain/spec/validator.nim @@ -30,11 +30,10 @@ func xorSeed(seed: Eth2Digest, x: uint64): Eth2Digest = for i in 0 ..< 8: result.data[31 - i] = result.data[31 - i] xor byte((x shr i*8) and 0xff) -# TODO Uint24 -> ValidatorIndex func get_shuffling*(seed: Eth2Digest, validators: openArray[Validator], epoch: EpochNumber - ): seq[seq[Uint24]] = + ): seq[seq[ValidatorIndex]] = ## Shuffles ``validators`` into crosslink committees seeded by ``seed`` and ``slot``. ## Returns a list of ``EPOCH_LENGTH * committees_per_slot`` committees where each ## committee is itself a list of validator indices. @@ -56,7 +55,7 @@ func get_shuffling*(seed: Eth2Digest, func get_new_validator_registry_delta_chain_tip*( current_validator_registry_delta_chain_tip: Eth2Digest, - index: Uint24, + index: ValidatorIndex, pubkey: ValidatorPubKey, slot: uint64, flag: ValidatorSetDeltaFlags): Eth2Digest = @@ -84,7 +83,7 @@ func get_current_epoch_committee_count_per_slot(state: BeaconState): uint64 = ) get_epoch_committee_count(len(current_active_validators)) -func get_crosslink_committees_at_slot*(state: BeaconState, slot: uint64) : seq[tuple[a: seq[Uint24], b: uint64]] = +func get_crosslink_committees_at_slot*(state: BeaconState, slot: uint64) : seq[tuple[a: seq[ValidatorIndex], b: uint64]] = ## Returns the list of ``(committee, shard)`` tuples for the ``slot``. let @@ -133,7 +132,7 @@ func get_crosslink_committees_at_slot*(state: BeaconState, slot: uint64) : seq[t func get_shard_committees_at_slot*( state: BeaconState, slot: uint64): seq[ShardCommittee] = # TODO temporary adapter; remove when all users gone - # where ShardCommittee is: shard*: uint64 / committee*: seq[Uint24] + # where ShardCommittee is: shard*: uint64 / committee*: seq[ValidatorIndex] let index = state.get_shard_committees_index(slot) #state.shard_committees_at_slots[index] for crosslink_committee in get_crosslink_committees_at_slot(state, slot): @@ -142,7 +141,7 @@ func get_shard_committees_at_slot*( sac.committee = crosslink_committee.a result.add sac -func get_beacon_proposer_index*(state: BeaconState, slot: uint64): Uint24 = +func get_beacon_proposer_index*(state: BeaconState, slot: uint64): ValidatorIndex = ## From Casper RPJ mini-spec: ## When slot i begins, validator Vidx is expected ## to create ("propose") a block, which contains a pointer to some parent block diff --git a/beacon_chain/ssz.nim b/beacon_chain/ssz.nim index 99b20575a..3c5ab5fe8 100644 --- a/beacon_chain/ssz.nim +++ b/beacon_chain/ssz.nim @@ -28,7 +28,7 @@ func toBytesSSZ(x: SomeInteger): array[sizeof(x), byte] = elif x.sizeof == 1: copyMem(result.addr, x.unsafeAddr, sizeof(result)) else: {.fatal: "Unsupported type serialization: " & $(type(x)).name.} -func toBytesSSZ(x: Uint24): array[3, byte] = +func toBytesSSZ(x: ValidatorIndex): array[3, byte] = ## Integers are all encoded as little endian and not padded let v = x.uint32 result[0] = byte(v and 0xff) @@ -52,13 +52,13 @@ type # provides the actual nim-type-to-bytes conversion. # TODO think about this for a bit - depends where the serialization of # validator keys ends up going.. - # TODO can't put ranges like Uint24 in here: + # TODO can't put ranges like ValidatorIndex in here: # https://github.com/nim-lang/Nim/issues/10027 SomeInteger | EthAddress | Eth2Digest | ValidatorPubKey | ValidatorSig | bool func sszLen(v: TrivialTypes): int = toBytesSSZ(v).len -func sszLen(v: Uint24): int = toBytesSSZ(v).len +func sszLen(v: ValidatorIndex): int = toBytesSSZ(v).len func sszLen(v: object | tuple): int = result = 4 # Length @@ -94,14 +94,14 @@ func fromBytesSSZUnsafe(T: typedesc[bool], data: pointer): T = # definition for now, but maybe this should be a parse error instead? fromBytesSSZUnsafe(uint8, data) != 0 -func fromBytesSSZUnsafe(T: typedesc[Uint24], data: pointer): T = +func fromBytesSSZUnsafe(T: typedesc[ValidatorIndex], data: pointer): T = ## Integers are all encoded as littleendian and not padded var tmp: uint32 let p = cast[ptr UncheckedArray[byte]](data) tmp = tmp or uint32(p[0]) tmp = tmp or uint32(p[1]) shl 8 tmp = tmp or uint32(p[2]) shl 16 - result = tmp.Uint24 + result = tmp.ValidatorIndex func fromBytesSSZUnsafe(T: typedesc[EthAddress], data: pointer): T = copyMem(result.addr, data, sizeof(result)) @@ -127,11 +127,11 @@ proc deserialize[T: TrivialTypes]( true func deserialize( - dest: var Uint24, offset: var int, data: openArray[byte]): bool = + dest: var ValidatorIndex, offset: var int, data: openArray[byte]): bool = if offset + sszLen(dest) > data.len(): false else: - dest = fromBytesSSZUnsafe(Uint24, data[offset].unsafeAddr) + dest = fromBytesSSZUnsafe(ValidatorIndex, data[offset].unsafeAddr) offset += sszLen(dest) true @@ -146,7 +146,7 @@ func deserialize[T: enum](dest: var T, offset: var int, data: openArray[byte]): dest = cast[T](tmp) true -proc deserialize[T: not (enum|TrivialTypes|Uint24)]( +proc deserialize[T: not (enum|TrivialTypes|ValidatorIndex)]( dest: var T, offset: var int, data: openArray[byte]): bool = # Length in bytes, followed by each item var totalLen: uint32 @@ -176,7 +176,7 @@ proc deserialize[T: not (enum|TrivialTypes|Uint24)]( func serialize(dest: var seq[byte], src: TrivialTypes) = dest.add src.toBytesSSZ() -func serialize(dest: var seq[byte], src: Uint24) = +func serialize(dest: var seq[byte], src: ValidatorIndex) = dest.add src.toBytesSSZ() func serialize(dest: var seq[byte], x: enum) = @@ -265,7 +265,7 @@ func hash_tree_root*(x: SomeInteger | bool): array[sizeof(x), byte] = ## All integers are serialized as **little endian**. toBytesSSZ(x) -func hash_tree_root*(x: Uint24): array[3, byte] = +func hash_tree_root*(x: ValidatorIndex): array[3, byte] = ## Convert directly to bytes the size of the int. (e.g. ``uint16 = 2 bytes``) ## All integers are serialized as **little endian**. toBytesSSZ(x) diff --git a/beacon_chain/state_transition.nim b/beacon_chain/state_transition.nim index f5ddb2563..0d6382a10 100644 --- a/beacon_chain/state_transition.nim +++ b/beacon_chain/state_transition.nim @@ -20,7 +20,7 @@ # * We mix procedural and functional styles for no good reason, except that the # spec does so also. # * There are no tests, and likely lots of bugs. -# * For indices, we get a mix of uint64, Uint24 and int - this is currently +# * For indices, we get a mix of uint64, ValidatorIndex and int - this is currently # swept under the rug with casts # * The spec uses uint64 for data types, but functions in the spec often assume # signed bigint semantics - under- and overflows ensue @@ -117,10 +117,10 @@ func processDepositRoot(state: var BeaconState, blck: BeaconBlock) = vote_count: 1 ) -func penalizeValidator(state: var BeaconState, index: Uint24) = +func penalizeValidator(state: var BeaconState, index: ValidatorIndex) = exit_validator(state, index) var validator = state.validator_registry[index] - #state.latest_penalized_exit_balances[(state.slot div EPOCH_LENGTH) mod LATEST_PENALIZED_EXIT_LENGTH] += get_effective_balance(state, index.Uint24) + #state.latest_penalized_exit_balances[(state.slot div EPOCH_LENGTH) mod LATEST_PENALIZED_EXIT_LENGTH] += get_effective_balance(state, index.ValidatorIndex) let whistleblower_index = get_beacon_proposer_index(state, state.slot) @@ -199,7 +199,7 @@ func verify_slashable_vote_data(state: BeaconState, vote_data: SlashableVoteData return true -proc indices(vote: SlashableVoteData): seq[Uint24] = +proc indices(vote: SlashableVoteData): seq[ValidatorIndex] = vote.aggregate_signature_poc_0_indices & vote.aggregate_signature_poc_1_indices @@ -321,7 +321,7 @@ proc process_ejections(state: var BeaconState) = for index, validator in state.validator_registry: if is_active_validator(validator, state.slot) and state.validator_balances[index] < EJECTION_BALANCE: - exit_validator(state, index.Uint24) + exit_validator(state, index.ValidatorIndex) func processSlot(state: var BeaconState, previous_block_root: Eth2Digest) = ## Time on the beacon chain moves in slots. Every time we make it to a new @@ -403,7 +403,7 @@ proc processBlock( func get_attester_indices( state: BeaconState, - attestations: openArray[PendingAttestationRecord]): seq[Uint24] = + attestations: openArray[PendingAttestationRecord]): seq[ValidatorIndex] = # Union of attesters that participated in some attestations # TODO spec - add as helper? attestations. @@ -429,13 +429,13 @@ func lowerThan(candidate, current: Eth2Digest): bool = if v > candidate.data[i]: return true return false -func inclusion_slot(state: BeaconState, v: Uint24): uint64 = +func inclusion_slot(state: BeaconState, v: ValidatorIndex): uint64 = for a in state.latest_attestations: if v in get_attestation_participants(state, a.data, a.participation_bitfield): return a.slot_included doAssert false # shouldn't happen.. -func inclusion_distance(state: BeaconState, v: Uint24): uint64 = +func inclusion_distance(state: BeaconState, v: ValidatorIndex): uint64 = for a in state.latest_attestations: if v in get_attestation_participants(state, a.data, a.participation_bitfield): return a.slot_included - a.data.slot @@ -462,11 +462,11 @@ func processEpoch(state: var BeaconState) = previous_epoch = if current_epoch > GENESIS_EPOCH: current_epoch - 1 else: current_epoch next_epoch = (current_epoch + 1).EpochNumber - func base_reward(state: BeaconState, index: Uint24): uint64 = + func base_reward(state: BeaconState, index: ValidatorIndex): uint64 = get_effective_balance(state, index) div base_reward_quotient.uint64 div 4 func inactivity_penalty( - state: BeaconState, index: Uint24, epochs_since_finality: uint64): uint64 = + state: BeaconState, index: ValidatorIndex, epochs_since_finality: uint64): uint64 = base_reward(state, index) + get_effective_balance(state, index) * epochs_since_finality div INACTIVITY_PENALTY_QUOTIENT div 2 @@ -542,14 +542,14 @@ func processEpoch(state: var BeaconState) = # these closures outside this scope, but still.. let statePtr = state.addr func attesting_validator_indices( - crosslink_committee: tuple[a: seq[Uint24], b: uint64], shard_block_root: Eth2Digest): seq[Uint24] = + crosslink_committee: tuple[a: seq[ValidatorIndex], b: uint64], shard_block_root: Eth2Digest): seq[ValidatorIndex] = let shard_block_attestations = concat(current_epoch_attestations, previous_epoch_attestations). filterIt(it.data.shard == crosslink_committee.b and it.data.shard_block_root == shard_block_root) get_attester_indices(statePtr[], shard_block_attestations) - func winning_root(crosslink_committee: tuple[a: seq[Uint24], b: uint64]): Eth2Digest = + func winning_root(crosslink_committee: tuple[a: seq[ValidatorIndex], b: uint64]): Eth2Digest = # * Let `winning_root(crosslink_committee)` be equal to the value of # `shard_block_root` such that # `sum([get_effective_balance(state, i) for i in attesting_validator_indices(crosslink_committee, shard_block_root)])` @@ -575,17 +575,17 @@ func processEpoch(state: var BeaconState) = max_val = val max_hash - func attesting_validators(crosslink_committee: tuple[a: seq[Uint24], b: uint64]): seq[Uint24] = + func attesting_validators(crosslink_committee: tuple[a: seq[ValidatorIndex], b: uint64]): seq[ValidatorIndex] = attesting_validator_indices(crosslink_committee, winning_root(crosslink_committee)) - func attesting_validator_indices(crosslink_committee: tuple[a: seq[Uint24], b: uint64]): seq[Uint24] = + func attesting_validator_indices(crosslink_committee: tuple[a: seq[ValidatorIndex], b: uint64]): seq[ValidatorIndex] = attesting_validator_indices(crosslink_committee, winning_root(crosslink_committee)) - func total_attesting_balance(crosslink_committee: tuple[a: seq[Uint24], b: uint64]): uint64 = + func total_attesting_balance(crosslink_committee: tuple[a: seq[ValidatorIndex], b: uint64]): uint64 = sum_effective_balances( statePtr[], attesting_validator_indices(crosslink_committee)) - func total_balance_sac(crosslink_committee: tuple[a: seq[Uint24], b: uint64]): uint64 = + func total_balance_sac(crosslink_committee: tuple[a: seq[ValidatorIndex], b: uint64]): uint64 = sum_effective_balances(statePtr[], crosslink_committee.a) block: # Eth1 data @@ -646,7 +646,7 @@ func processEpoch(state: var BeaconState) = block: # Justification and finalization let epochs_since_finality = next_epoch - state.finalized_epoch - proc update_balance(attesters: openArray[Uint24], attesting_balance: uint64) = + proc update_balance(attesters: openArray[ValidatorIndex], attesting_balance: uint64) = # TODO Spec - add helper? for v in attesters: statePtr.validator_balances[v] += diff --git a/tests/test_ssz.nim b/tests/test_ssz.nim index 1257eef33..60a1bd447 100644 --- a/tests/test_ssz.nim +++ b/tests/test_ssz.nim @@ -27,7 +27,7 @@ suite "Simple serialization": f2: EthAddress f3: MDigest[256] f4: seq[byte] - f5: Uint24 + f5: ValidatorIndex let expected_deser = Foo( f0: 5, @@ -35,7 +35,7 @@ suite "Simple serialization": f2: EthAddress.filled(byte 35), f3: MDigest[256].filled(byte 35), f4: @[byte 'c'.ord, 'o'.ord, 'w'.ord], - f5: Uint24(79) + f5: ValidatorIndex(79) ) var expected_ser = @[ @@ -61,9 +61,9 @@ suite "Simple serialization": expected_ser[0..^2].deserialize(Foo).isNone() expected_ser[1..^1].deserialize(Foo).isNone() - test "Uint24 roundtrip": + test "ValidatorIndex roundtrip": # https://github.com/nim-lang/Nim/issues/10027 - let v = 79.Uint24 + let v = 79.ValidatorIndex let ser = v.serialize() check: ser.len() == 3 @@ -120,4 +120,4 @@ suite "Tree hashing": test "Hash integer": check: hash_tree_root(0x01'u32) == [1'u8, 0, 0, 0] # little endian! - check: hash_tree_root(Uint24(0x01)) == [1'u8, 0, 0] # little endian! + check: hash_tree_root(ValidatorIndex(0x01)) == [1'u8, 0, 0] # little endian! diff --git a/tests/test_validator.nim b/tests/test_validator.nim index 536edff39..017950972 100644 --- a/tests/test_validator.nim +++ b/tests/test_validator.nim @@ -8,7 +8,7 @@ import math,unittest, sequtils, ../beacon_chain/spec/[helpers, datatypes, digest, validator] -func sumCommittees(v: openArray[seq[Uint24]], reqCommitteeLen: int): int = +func sumCommittees(v: openArray[seq[ValidatorIndex]], reqCommitteeLen: int): int = for x in v: ## This only holds when num_validators is divisible by ## EPOCH_LENGTH * get_committee_count_per_slot(len(validators)) diff --git a/tests/testutil.nim b/tests/testutil.nim index 6af58afd6..492b178d1 100644 --- a/tests/testutil.nim +++ b/tests/testutil.nim @@ -82,7 +82,7 @@ func makeGenesisBlock*(state: BeaconState): BeaconBlock = state_root: Eth2Digest(data: hash_tree_root(state)) ) -func getNextBeaconProposerIndex*(state: BeaconState): Uint24 = +func getNextBeaconProposerIndex*(state: BeaconState): ValidatorIndex = # TODO: This is a special version of get_beacon_proposer_index that takes into # account the partial update done at the start of slot processing - # see get_shard_committees_index @@ -168,14 +168,14 @@ proc makeBlock*( addBlock(next_state, previous_block_root, body) proc find_shard_committee( - sacs: openArray[ShardCommittee], validator_index: Uint24): ShardCommittee = + sacs: openArray[ShardCommittee], validator_index: ValidatorIndex): ShardCommittee = for sac in sacs: if validator_index in sac.committee: return sac doAssert false proc makeAttestation*( state: BeaconState, beacon_block_root: Eth2Digest, - validator_index: Uint24, flags: UpdateFlags = {}): Attestation = + validator_index: ValidatorIndex, flags: UpdateFlags = {}): Attestation = let sac = find_shard_committee( get_shard_committees_at_slot(state, state.slot), validator_index)