Uint24 -> ValidatorIndex; Fork.pre_fork_version -> Fork.previous_version; Fork.post_fork_version -> Fork.current_version

This commit is contained in:
Dustin Brody 2019-01-28 20:15:00 -08:00
parent 4574178836
commit ad3de5fde9
9 changed files with 81 additions and 85 deletions

View File

@ -10,7 +10,7 @@ import
../extras, ../ssz, ../extras, ../ssz,
./crypto, ./datatypes, ./digest, ./helpers, ./validator ./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 # Validators collect rewards which increases their balance but not their
# influence. Validators may also lose balance if they fail to do their duty # 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 # 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) min(state.validator_balances[index], MAX_DEPOSIT_AMOUNT)
func sum_effective_balances*( 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 # TODO spec - add as helper? Used pretty often
for index in validator_indices: for index in validator_indices:
result += get_effective_balance(state, index) result += get_effective_balance(state, index)
@ -51,7 +51,7 @@ func process_deposit(state: var BeaconState,
proof_of_possession: ValidatorSig, proof_of_possession: ValidatorSig,
withdrawal_credentials: Eth2Digest, withdrawal_credentials: Eth2Digest,
randao_commitment: Eth2Digest, randao_commitment: Eth2Digest,
custody_commitment: Eth2Digest) : Uint24 = custody_commitment: Eth2Digest) : ValidatorIndex =
## Process a deposit from Ethereum 1.0. ## Process a deposit from Ethereum 1.0.
if false: if false:
@ -86,11 +86,11 @@ func process_deposit(state: var BeaconState,
if index.isNone(): if index.isNone():
state.validator_registry.add(validator) state.validator_registry.add(validator)
state.validator_balances.add(deposit) state.validator_balances.add(deposit)
(len(state.validator_registry) - 1).Uint24 (len(state.validator_registry) - 1).ValidatorIndex
else: else:
state.validator_registry[index.get()] = validator state.validator_registry[index.get()] = validator
state.validator_balances[index.get()] = deposit state.validator_balances[index.get()] = deposit
index.get().Uint24 index.get().ValidatorIndex
else: else:
# Increase balance by deposit amount # Increase balance by deposit amount
let index = validator_pubkeys.find(pubkey) let index = validator_pubkeys.find(pubkey)
@ -99,16 +99,15 @@ func process_deposit(state: var BeaconState,
withdrawal_credentials withdrawal_credentials
state.validator_balances[index] += deposit state.validator_balances[index] += deposit
index.Uint24 index.ValidatorIndex
func get_entry_exit_effect_epoch*(epoch: EpochNumber): EpochNumber = func get_entry_exit_effect_epoch*(epoch: EpochNumber): EpochNumber =
## An entry or exit triggered in the ``epoch`` given by the input takes effect at ## An entry or exit triggered in the ``epoch`` given by the input takes effect at
## the epoch given by the output. ## the epoch given by the output.
epoch + 1 + ENTRY_EXIT_DELAY epoch + 1 + ENTRY_EXIT_DELAY
# TODO: Uint24 -> ValidatorIndex
func activate_validator(state: var BeaconState, func activate_validator(state: var BeaconState,
index: Uint24, index: ValidatorIndex,
genesis: bool) = genesis: bool) =
## Activate the validator with the given ``index``. ## Activate the validator with the given ``index``.
let validator = addr state.validator_registry[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)) validator.activation_epoch = if genesis: GENESIS_EPOCH else: get_entry_exit_effect_epoch(get_current_epoch(state))
func initiate_validator_exit(state: var BeaconState, func initiate_validator_exit(state: var BeaconState,
index: Uint24) = index: ValidatorIndex) =
## Initiate exit for the validator with the given ``index``. ## Initiate exit for the validator with the given ``index``.
var validator = state.validator_registry[index] var validator = state.validator_registry[index]
validator.status_flags = validator.status_flags or INITIATED_EXIT validator.status_flags = validator.status_flags or INITIATED_EXIT
state.validator_registry[index] = validator state.validator_registry[index] = validator
func exit_validator*(state: var BeaconState, func exit_validator*(state: var BeaconState,
index: Uint24) = index: ValidatorIndex) =
## Exit the validator with the given ``index``. ## Exit the validator with the given ``index``.
let validator = addr state.validator_registry[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_start = state.latest_penalized_exit_balances[(e + 1) mod LATEST_PENALIZED_EXIT_LENGTH]
total_at_end = state.latest_penalized_exit_balances[e] total_at_end = state.latest_penalized_exit_balances[e]
total_penalties = total_at_end - total_at_start 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.validator_balances[index] -= penalty
## 'state' is of type <var BeaconState> which cannot be captured as it ## 'state' is of type <var BeaconState> which cannot be captured as it
## would violate memory safety, when using nested function approach in ## would violate memory safety, when using nested function approach in
## spec directly. That said, the spec approach evidently is not meant, ## spec directly. That said, the spec approach evidently is not meant,
## based on its abundant and pointless memory copies, for production. ## 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): 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 ## TODO figure out that memory safety issue, which would come up again when
## sorting, and then actually do withdrawals ## sorting, and then actually do withdrawals
@ -194,11 +193,10 @@ func get_initial_beacon_state*(
# Misc # Misc
slot: GENESIS_SLOT, slot: GENESIS_SLOT,
genesis_time: genesis_time, genesis_time: genesis_time,
# TODO pre_fork_version -> previous_version, post_fork_version -> current_version,
# rm fork_slot init in favor of epoch # rm fork_slot init in favor of epoch
fork: Fork( fork: Fork(
pre_fork_version: GENESIS_FORK_VERSION, previous_version: GENESIS_FORK_VERSION,
post_fork_version: GENESIS_FORK_VERSION, current_version: GENESIS_FORK_VERSION,
fork_slot: GENESIS_SLOT, fork_slot: GENESIS_SLOT,
), ),
@ -241,7 +239,7 @@ func get_initial_beacon_state*(
# Process initial activations # Process initial activations
for validator_index in 0 ..< state.validator_registry.len: 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: if get_effective_balance(state, vi) > MAX_DEPOSIT_AMOUNT:
activate_validator(state, vi, true) activate_validator(state, vi, true)
@ -255,7 +253,7 @@ func get_block_root*(state: BeaconState,
func get_attestation_participants*(state: BeaconState, func get_attestation_participants*(state: BeaconState,
attestation_data: AttestationData, 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 ## 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 ## 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 ## 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 # TODO investigate functional library / approach to help avoid loop bugs
assert any( assert any(
crosslink_committees, 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( let crosslink_committee = mapIt(
filterIt(crosslink_committees, it.b == attestation_data.shard), filterIt(crosslink_committees, it.b == attestation_data.shard),
it.a)[0] 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 if validator.activation_epoch > get_entry_exit_effect_epoch(current_epoch) and
state.validator_balances[index] >= MAX_DEPOSIT_AMOUNT: state.validator_balances[index] >= MAX_DEPOSIT_AMOUNT:
# Check the balance churn would be within the allowance # 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: if balance_churn > max_balance_churn:
break break
# Activate validator # Activate validator
activate_validator(state, index.Uint24, false) activate_validator(state, index.ValidatorIndex, false)
# Exit validators within the allowable balance churn # Exit validators within the allowable balance churn
balance_churn = 0 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 if validator.exit_epoch > get_entry_exit_effect_epoch(current_epoch) and
((validator.status_flags and INITIATED_EXIT) == INITIATED_EXIT): ((validator.status_flags and INITIATED_EXIT) == INITIATED_EXIT):
# Check the balance churn would be within the allowance # 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: if balance_churn > max_balance_churn:
break break
# Exit validator # Exit validator
exit_validator(state, index.Uint24) exit_validator(state, index.ValidatorIndex)
state.validator_registry_update_epoch = current_epoch state.validator_registry_update_epoch = current_epoch

View File

@ -165,13 +165,13 @@ const
MAX_EXITS* = 2^4 MAX_EXITS* = 2^4
type type
Uint24* = range[0'u32 .. 0xFFFFFF'u32] # TODO: wrap-around ValidatorIndex* = range[0'u32 .. 0xFFFFFF'u32] # TODO: wrap-around
SlotNumber* = uint64 SlotNumber* = uint64
EpochNumber* = uint64 EpochNumber* = uint64
# https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#data-structures # https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#data-structures
ProposerSlashing* = object ProposerSlashing* = object
proposer_index*: Uint24 proposer_index*: ValidatorIndex
proposal_data_1*: ProposalSignedData proposal_data_1*: ProposalSignedData
proposal_signature_1*: ValidatorSig proposal_signature_1*: ValidatorSig
proposal_data_2*: ProposalSignedData proposal_data_2*: ProposalSignedData
@ -182,10 +182,10 @@ type
slashable_vote_data_2*: SlashableVoteData slashable_vote_data_2*: SlashableVoteData
SlashableVoteData* = object SlashableVoteData* = object
aggregate_signature_poc_0_indices*: seq[Uint24] ##\ aggregate_signature_poc_0_indices*: seq[ValidatorIndex] ##\
## Proof-of-custody indices (0 bits) ## 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) ## Proof-of-custody indices (1 bits)
data*: AttestationData data*: AttestationData
@ -254,7 +254,7 @@ type
# Minimum slot for processing exit # Minimum slot for processing exit
slot*: uint64 slot*: uint64
# Index of the exiting validator # Index of the exiting validator
validator_index*: Uint24 validator_index*: ValidatorIndex
# Validator signature # Validator signature
signature*: ValidatorSig signature*: ValidatorSig
@ -395,7 +395,7 @@ type
ShardCommittee* = object ShardCommittee* = object
shard*: uint64 shard*: uint64
committee*: seq[Uint24] ##\ committee*: seq[ValidatorIndex] ##\
## Committe participants that get to attest to blocks on this shard - ## Committe participants that get to attest to blocks on this shard -
## indices into BeaconState.validator_registry ## indices into BeaconState.validator_registry
@ -420,13 +420,13 @@ type
slot_included*: uint64 # Slot in which it was included slot_included*: uint64 # Slot in which it was included
Fork* = object Fork* = object
pre_fork_version*: uint64 # Previous fork version previous_version*: uint64 # Previous fork version
post_fork_version*: uint64 # Post fork version current_version*: uint64 # Current fork version
fork_slot*: uint64 # Fork slot number fork_slot*: uint64 # Fork slot number TODO should be epoch
ValidatorRegistryDeltaBlock* = object ValidatorRegistryDeltaBlock* = object
latest_registry_delta_root*: Eth2Digest latest_registry_delta_root*: Eth2Digest
validator_index*: Uint24 validator_index*: ValidatorIndex
pubkey*: ValidatorPubKey pubkey*: ValidatorPubKey
slot*: uint64 slot*: uint64
flag*: ValidatorSetDeltaFlags flag*: ValidatorSetDeltaFlags
@ -455,10 +455,10 @@ when true:
proc read*(rlp: var Rlp, T: type ValidatorPubKey): T {.inline.} = proc read*(rlp: var Rlp, T: type ValidatorPubKey): T {.inline.} =
discard discard
proc append*(rlpWriter: var RlpWriter, value: Uint24) = proc append*(rlpWriter: var RlpWriter, value: ValidatorIndex) =
discard discard
proc read*(rlp: var Rlp, T: type Uint24): T {.inline.} = proc read*(rlp: var Rlp, T: type ValidatorIndex): T {.inline.} =
discard discard
proc append*(rlpWriter: var RlpWriter, value: ValidatorSig) = proc append*(rlpWriter: var RlpWriter, value: ValidatorSig) =

View File

@ -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. # Read 3-bytes of `source` as a 24-bit big-endian integer.
let sample_from_source = let sample_from_source =
source.data[pos].Uint24 shl 16 or source.data[pos].ValidatorIndex shl 16 or
source.data[pos+1].Uint24 shl 8 or source.data[pos+1].ValidatorIndex shl 8 or
source.data[pos+2].Uint24 source.data[pos+2].ValidatorIndex
# Sample values greater than or equal to `sample_max` will cause # Sample values greater than or equal to `sample_max` will cause
# modulo bias when mapped into the `remaining` range. # modulo bias when mapped into the `remaining` range.
@ -120,8 +120,8 @@ func integer_squareroot*(n: SomeInteger): SomeInteger =
x x
func get_fork_version*(fork: Fork, slot: uint64): uint64 = func get_fork_version*(fork: Fork, slot: uint64): uint64 =
if slot < fork.fork_slot: fork.pre_fork_version if slot < fork.fork_slot: fork.previous_version
else: fork.post_fork_version else: fork.current_version
func get_domain*( func get_domain*(
fork: Fork, slot: uint64, domain_type: SignatureDomain): uint64 = 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 ### Checks if validator is active
validator.activation_epoch <= epoch and epoch < validator.exit_epoch validator.activation_epoch <= epoch and epoch < validator.exit_epoch
# TODO Uint24 -> ValidatorIndex func get_active_validator_indices*(validators: openArray[Validator], epoch: EpochNumber): seq[ValidatorIndex] =
func get_active_validator_indices*(validators: openArray[Validator], epoch: EpochNumber): seq[Uint24] =
## Gets indices of active validators from validators ## Gets indices of active validators from validators
for idx, val in validators: for idx, val in validators:
if is_active_validator(val, epoch): if is_active_validator(val, epoch):
result.add idx.Uint24 result.add idx.ValidatorIndex
func get_epoch_committee_count*(active_validator_count: int): uint64 = func get_epoch_committee_count*(active_validator_count: int): uint64 =
clamp( clamp(

View File

@ -30,11 +30,10 @@ func xorSeed(seed: Eth2Digest, x: uint64): Eth2Digest =
for i in 0 ..< 8: for i in 0 ..< 8:
result.data[31 - i] = result.data[31 - i] xor byte((x shr i*8) and 0xff) result.data[31 - i] = result.data[31 - i] xor byte((x shr i*8) and 0xff)
# TODO Uint24 -> ValidatorIndex
func get_shuffling*(seed: Eth2Digest, func get_shuffling*(seed: Eth2Digest,
validators: openArray[Validator], validators: openArray[Validator],
epoch: EpochNumber epoch: EpochNumber
): seq[seq[Uint24]] = ): seq[seq[ValidatorIndex]] =
## Shuffles ``validators`` into crosslink committees seeded by ``seed`` and ``slot``. ## Shuffles ``validators`` into crosslink committees seeded by ``seed`` and ``slot``.
## Returns a list of ``EPOCH_LENGTH * committees_per_slot`` committees where each ## Returns a list of ``EPOCH_LENGTH * committees_per_slot`` committees where each
## committee is itself a list of validator indices. ## 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*( func get_new_validator_registry_delta_chain_tip*(
current_validator_registry_delta_chain_tip: Eth2Digest, current_validator_registry_delta_chain_tip: Eth2Digest,
index: Uint24, index: ValidatorIndex,
pubkey: ValidatorPubKey, pubkey: ValidatorPubKey,
slot: uint64, slot: uint64,
flag: ValidatorSetDeltaFlags): Eth2Digest = 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)) 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``. ## Returns the list of ``(committee, shard)`` tuples for the ``slot``.
let let
@ -133,7 +132,7 @@ func get_crosslink_committees_at_slot*(state: BeaconState, slot: uint64) : seq[t
func get_shard_committees_at_slot*( func get_shard_committees_at_slot*(
state: BeaconState, slot: uint64): seq[ShardCommittee] = state: BeaconState, slot: uint64): seq[ShardCommittee] =
# TODO temporary adapter; remove when all users gone # 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) let index = state.get_shard_committees_index(slot)
#state.shard_committees_at_slots[index] #state.shard_committees_at_slots[index]
for crosslink_committee in get_crosslink_committees_at_slot(state, slot): 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 sac.committee = crosslink_committee.a
result.add sac 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: ## From Casper RPJ mini-spec:
## When slot i begins, validator Vidx is expected ## When slot i begins, validator Vidx is expected
## to create ("propose") a block, which contains a pointer to some parent block ## to create ("propose") a block, which contains a pointer to some parent block

View File

@ -28,7 +28,7 @@ func toBytesSSZ(x: SomeInteger): array[sizeof(x), byte] =
elif x.sizeof == 1: copyMem(result.addr, x.unsafeAddr, sizeof(result)) elif x.sizeof == 1: copyMem(result.addr, x.unsafeAddr, sizeof(result))
else: {.fatal: "Unsupported type serialization: " & $(type(x)).name.} 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 ## Integers are all encoded as little endian and not padded
let v = x.uint32 let v = x.uint32
result[0] = byte(v and 0xff) result[0] = byte(v and 0xff)
@ -52,13 +52,13 @@ type
# provides the actual nim-type-to-bytes conversion. # provides the actual nim-type-to-bytes conversion.
# TODO think about this for a bit - depends where the serialization of # TODO think about this for a bit - depends where the serialization of
# validator keys ends up going.. # 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 # https://github.com/nim-lang/Nim/issues/10027
SomeInteger | EthAddress | Eth2Digest | ValidatorPubKey | ValidatorSig | SomeInteger | EthAddress | Eth2Digest | ValidatorPubKey | ValidatorSig |
bool bool
func sszLen(v: TrivialTypes): int = toBytesSSZ(v).len 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 = func sszLen(v: object | tuple): int =
result = 4 # Length 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? # definition for now, but maybe this should be a parse error instead?
fromBytesSSZUnsafe(uint8, data) != 0 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 ## Integers are all encoded as littleendian and not padded
var tmp: uint32 var tmp: uint32
let p = cast[ptr UncheckedArray[byte]](data) let p = cast[ptr UncheckedArray[byte]](data)
tmp = tmp or uint32(p[0]) tmp = tmp or uint32(p[0])
tmp = tmp or uint32(p[1]) shl 8 tmp = tmp or uint32(p[1]) shl 8
tmp = tmp or uint32(p[2]) shl 16 tmp = tmp or uint32(p[2]) shl 16
result = tmp.Uint24 result = tmp.ValidatorIndex
func fromBytesSSZUnsafe(T: typedesc[EthAddress], data: pointer): T = func fromBytesSSZUnsafe(T: typedesc[EthAddress], data: pointer): T =
copyMem(result.addr, data, sizeof(result)) copyMem(result.addr, data, sizeof(result))
@ -127,11 +127,11 @@ proc deserialize[T: TrivialTypes](
true true
func deserialize( 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(): if offset + sszLen(dest) > data.len():
false false
else: else:
dest = fromBytesSSZUnsafe(Uint24, data[offset].unsafeAddr) dest = fromBytesSSZUnsafe(ValidatorIndex, data[offset].unsafeAddr)
offset += sszLen(dest) offset += sszLen(dest)
true true
@ -146,7 +146,7 @@ func deserialize[T: enum](dest: var T, offset: var int, data: openArray[byte]):
dest = cast[T](tmp) dest = cast[T](tmp)
true 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 = dest: var T, offset: var int, data: openArray[byte]): bool =
# Length in bytes, followed by each item # Length in bytes, followed by each item
var totalLen: uint32 var totalLen: uint32
@ -176,7 +176,7 @@ proc deserialize[T: not (enum|TrivialTypes|Uint24)](
func serialize(dest: var seq[byte], src: TrivialTypes) = func serialize(dest: var seq[byte], src: TrivialTypes) =
dest.add src.toBytesSSZ() dest.add src.toBytesSSZ()
func serialize(dest: var seq[byte], src: Uint24) = func serialize(dest: var seq[byte], src: ValidatorIndex) =
dest.add src.toBytesSSZ() dest.add src.toBytesSSZ()
func serialize(dest: var seq[byte], x: enum) = 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**. ## All integers are serialized as **little endian**.
toBytesSSZ(x) 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``) ## Convert directly to bytes the size of the int. (e.g. ``uint16 = 2 bytes``)
## All integers are serialized as **little endian**. ## All integers are serialized as **little endian**.
toBytesSSZ(x) toBytesSSZ(x)

View File

@ -20,7 +20,7 @@
# * We mix procedural and functional styles for no good reason, except that the # * We mix procedural and functional styles for no good reason, except that the
# spec does so also. # spec does so also.
# * There are no tests, and likely lots of bugs. # * 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 # swept under the rug with casts
# * The spec uses uint64 for data types, but functions in the spec often assume # * The spec uses uint64 for data types, but functions in the spec often assume
# signed bigint semantics - under- and overflows ensue # signed bigint semantics - under- and overflows ensue
@ -117,10 +117,10 @@ func processDepositRoot(state: var BeaconState, blck: BeaconBlock) =
vote_count: 1 vote_count: 1
) )
func penalizeValidator(state: var BeaconState, index: Uint24) = func penalizeValidator(state: var BeaconState, index: ValidatorIndex) =
exit_validator(state, index) exit_validator(state, index)
var validator = state.validator_registry[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 let
whistleblower_index = get_beacon_proposer_index(state, state.slot) 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 return true
proc indices(vote: SlashableVoteData): seq[Uint24] = proc indices(vote: SlashableVoteData): seq[ValidatorIndex] =
vote.aggregate_signature_poc_0_indices & vote.aggregate_signature_poc_0_indices &
vote.aggregate_signature_poc_1_indices vote.aggregate_signature_poc_1_indices
@ -321,7 +321,7 @@ proc process_ejections(state: var BeaconState) =
for index, validator in state.validator_registry: for index, validator in state.validator_registry:
if is_active_validator(validator, state.slot) and if is_active_validator(validator, state.slot) and
state.validator_balances[index] < EJECTION_BALANCE: 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) = 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 ## 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( func get_attester_indices(
state: BeaconState, state: BeaconState,
attestations: openArray[PendingAttestationRecord]): seq[Uint24] = attestations: openArray[PendingAttestationRecord]): seq[ValidatorIndex] =
# Union of attesters that participated in some attestations # Union of attesters that participated in some attestations
# TODO spec - add as helper? # TODO spec - add as helper?
attestations. attestations.
@ -429,13 +429,13 @@ func lowerThan(candidate, current: Eth2Digest): bool =
if v > candidate.data[i]: return true if v > candidate.data[i]: return true
return false return false
func inclusion_slot(state: BeaconState, v: Uint24): uint64 = func inclusion_slot(state: BeaconState, v: ValidatorIndex): uint64 =
for a in state.latest_attestations: for a in state.latest_attestations:
if v in get_attestation_participants(state, a.data, a.participation_bitfield): if v in get_attestation_participants(state, a.data, a.participation_bitfield):
return a.slot_included return a.slot_included
doAssert false # shouldn't happen.. 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: for a in state.latest_attestations:
if v in get_attestation_participants(state, a.data, a.participation_bitfield): if v in get_attestation_participants(state, a.data, a.participation_bitfield):
return a.slot_included - a.data.slot 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 previous_epoch = if current_epoch > GENESIS_EPOCH: current_epoch - 1 else: current_epoch
next_epoch = (current_epoch + 1).EpochNumber 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 get_effective_balance(state, index) div base_reward_quotient.uint64 div 4
func inactivity_penalty( 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) + base_reward(state, index) +
get_effective_balance(state, index) * get_effective_balance(state, index) *
epochs_since_finality div INACTIVITY_PENALTY_QUOTIENT div 2 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.. # these closures outside this scope, but still..
let statePtr = state.addr let statePtr = state.addr
func attesting_validator_indices( 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 = let shard_block_attestations =
concat(current_epoch_attestations, previous_epoch_attestations). concat(current_epoch_attestations, previous_epoch_attestations).
filterIt(it.data.shard == crosslink_committee.b and filterIt(it.data.shard == crosslink_committee.b and
it.data.shard_block_root == shard_block_root) it.data.shard_block_root == shard_block_root)
get_attester_indices(statePtr[], shard_block_attestations) 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 # * Let `winning_root(crosslink_committee)` be equal to the value of
# `shard_block_root` such that # `shard_block_root` such that
# `sum([get_effective_balance(state, i) for i in attesting_validator_indices(crosslink_committee, shard_block_root)])` # `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_val = val
max_hash 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)) 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)) 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( sum_effective_balances(
statePtr[], attesting_validator_indices(crosslink_committee)) 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) sum_effective_balances(statePtr[], crosslink_committee.a)
block: # Eth1 data block: # Eth1 data
@ -646,7 +646,7 @@ func processEpoch(state: var BeaconState) =
block: # Justification and finalization block: # Justification and finalization
let epochs_since_finality = next_epoch - state.finalized_epoch 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? # TODO Spec - add helper?
for v in attesters: for v in attesters:
statePtr.validator_balances[v] += statePtr.validator_balances[v] +=

View File

@ -27,7 +27,7 @@ suite "Simple serialization":
f2: EthAddress f2: EthAddress
f3: MDigest[256] f3: MDigest[256]
f4: seq[byte] f4: seq[byte]
f5: Uint24 f5: ValidatorIndex
let expected_deser = Foo( let expected_deser = Foo(
f0: 5, f0: 5,
@ -35,7 +35,7 @@ suite "Simple serialization":
f2: EthAddress.filled(byte 35), f2: EthAddress.filled(byte 35),
f3: MDigest[256].filled(byte 35), f3: MDigest[256].filled(byte 35),
f4: @[byte 'c'.ord, 'o'.ord, 'w'.ord], f4: @[byte 'c'.ord, 'o'.ord, 'w'.ord],
f5: Uint24(79) f5: ValidatorIndex(79)
) )
var expected_ser = @[ var expected_ser = @[
@ -61,9 +61,9 @@ suite "Simple serialization":
expected_ser[0..^2].deserialize(Foo).isNone() expected_ser[0..^2].deserialize(Foo).isNone()
expected_ser[1..^1].deserialize(Foo).isNone() expected_ser[1..^1].deserialize(Foo).isNone()
test "Uint24 roundtrip": test "ValidatorIndex roundtrip":
# https://github.com/nim-lang/Nim/issues/10027 # https://github.com/nim-lang/Nim/issues/10027
let v = 79.Uint24 let v = 79.ValidatorIndex
let ser = v.serialize() let ser = v.serialize()
check: check:
ser.len() == 3 ser.len() == 3
@ -120,4 +120,4 @@ suite "Tree hashing":
test "Hash integer": test "Hash integer":
check: hash_tree_root(0x01'u32) == [1'u8, 0, 0, 0] # little endian! 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!

View File

@ -8,7 +8,7 @@ import
math,unittest, sequtils, math,unittest, sequtils,
../beacon_chain/spec/[helpers, datatypes, digest, validator] ../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: for x in v:
## This only holds when num_validators is divisible by ## This only holds when num_validators is divisible by
## EPOCH_LENGTH * get_committee_count_per_slot(len(validators)) ## EPOCH_LENGTH * get_committee_count_per_slot(len(validators))

View File

@ -82,7 +82,7 @@ func makeGenesisBlock*(state: BeaconState): BeaconBlock =
state_root: Eth2Digest(data: hash_tree_root(state)) 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 # 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 - # account the partial update done at the start of slot processing -
# see get_shard_committees_index # see get_shard_committees_index
@ -168,14 +168,14 @@ proc makeBlock*(
addBlock(next_state, previous_block_root, body) addBlock(next_state, previous_block_root, body)
proc find_shard_committee( proc find_shard_committee(
sacs: openArray[ShardCommittee], validator_index: Uint24): ShardCommittee = sacs: openArray[ShardCommittee], validator_index: ValidatorIndex): ShardCommittee =
for sac in sacs: for sac in sacs:
if validator_index in sac.committee: return sac if validator_index in sac.committee: return sac
doAssert false doAssert false
proc makeAttestation*( proc makeAttestation*(
state: BeaconState, beacon_block_root: Eth2Digest, state: BeaconState, beacon_block_root: Eth2Digest,
validator_index: Uint24, flags: UpdateFlags = {}): Attestation = validator_index: ValidatorIndex, flags: UpdateFlags = {}): Attestation =
let let
sac = find_shard_committee( sac = find_shard_committee(
get_shard_committees_at_slot(state, state.slot), validator_index) get_shard_committees_at_slot(state, state.slot), validator_index)