mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-01-27 06:47:13 +00:00
rename BeaconState.validator_registry to BeaconState.validators
This commit is contained in:
parent
2ba14bc9af
commit
fb601bd5b8
@ -81,15 +81,15 @@ proc validate(
|
||||
custody_bit_0_participants = participants
|
||||
|
||||
group_public_key = bls_aggregate_pubkeys(
|
||||
participants.mapIt(state.validator_registry[it].pubkey))
|
||||
participants.mapIt(state.validators[it].pubkey))
|
||||
|
||||
# Verify that aggregate_signature verifies using the group pubkey.
|
||||
if not bls_verify_multiple(
|
||||
@[
|
||||
bls_aggregate_pubkeys(mapIt(custody_bit_0_participants,
|
||||
state.validator_registry[it].pubkey)),
|
||||
state.validators[it].pubkey)),
|
||||
bls_aggregate_pubkeys(mapIt(custody_bit_1_participants,
|
||||
state.validator_registry[it].pubkey)),
|
||||
state.validators[it].pubkey)),
|
||||
],
|
||||
@[
|
||||
hash_tree_root(AttestationDataAndCustodyBit(
|
||||
@ -159,7 +159,7 @@ proc updateLatestVotes(
|
||||
participants: seq[ValidatorIndex], blck: BlockRef) =
|
||||
for validator in participants:
|
||||
let
|
||||
pubKey = state.validator_registry[validator].pubkey
|
||||
pubKey = state.validators[validator].pubkey
|
||||
current = pool.latestAttestations.getOrDefault(pubKey)
|
||||
if current.isNil or current.slot < attestationSlot:
|
||||
pool.latestAttestations[pubKey] = blck
|
||||
|
@ -238,7 +238,7 @@ proc addLocalValidator(
|
||||
node: BeaconNode, state: BeaconState, privKey: ValidatorPrivKey) =
|
||||
let pubKey = privKey.pubKey()
|
||||
|
||||
let idx = state.validator_registry.findIt(it.pubKey == pubKey)
|
||||
let idx = state.validators.findIt(it.pubKey == pubKey)
|
||||
if idx == -1:
|
||||
warn "Validator not in registry", pubKey
|
||||
else:
|
||||
@ -256,7 +256,7 @@ proc addLocalValidators(node: BeaconNode, state: BeaconState) =
|
||||
|
||||
proc getAttachedValidator(
|
||||
node: BeaconNode, state: BeaconState, idx: int): AttachedValidator =
|
||||
let validatorKey = state.validator_registry[idx].pubkey
|
||||
let validatorKey = state.validators[idx].pubkey
|
||||
node.attachedValidators.getValidator(validatorKey)
|
||||
|
||||
proc updateHead(node: BeaconNode, slot: Slot): BlockRef =
|
||||
@ -507,7 +507,7 @@ proc handleProposal(node: BeaconNode, head: BlockRef, slot: Slot):
|
||||
debug "Expecting proposal",
|
||||
headRoot = shortLog(head.root),
|
||||
slot = humaneSlotNum(slot),
|
||||
proposer = shortLog(state.validator_registry[proposerIdx].pubKey)
|
||||
proposer = shortLog(state.validators[proposerIdx].pubKey)
|
||||
|
||||
return head
|
||||
|
||||
|
@ -607,7 +607,7 @@ proc preInit*(
|
||||
blockRoot = shortLog(blockRoot),
|
||||
stateRoot = shortLog(blck.state_root),
|
||||
fork = state.fork,
|
||||
validators = state.validator_registry.len()
|
||||
validators = state.validators.len()
|
||||
|
||||
db.putState(state)
|
||||
db.putBlock(blck)
|
||||
|
@ -29,7 +29,7 @@ proc lmdGhost*(
|
||||
|
||||
var attestation_targets: seq[tuple[validator: ValidatorIndex, blck: BlockRef]]
|
||||
for i in active_validator_indices:
|
||||
let pubKey = start_state.validator_registry[i].pubkey
|
||||
let pubKey = start_state.validators[i].pubkey
|
||||
if (let vote = pool.latestAttestation(pubKey); not vote.isNil):
|
||||
attestation_targets.add((i, vote))
|
||||
|
||||
@ -40,7 +40,7 @@ proc lmdGhost*(
|
||||
# The div on the balance is to chop off the insignification bits that
|
||||
# fluctuate a lot epoch to epoch to have a more stable fork choice
|
||||
res +=
|
||||
start_state.validator_registry[validator_index].effective_balance div
|
||||
start_state.validators[validator_index].effective_balance div
|
||||
FORK_CHOICE_BALANCE_INCREMENT
|
||||
res
|
||||
|
||||
|
@ -74,7 +74,7 @@ func process_deposit*(
|
||||
let
|
||||
pubkey = deposit.data.pubkey
|
||||
amount = deposit.data.amount
|
||||
validator_pubkeys = mapIt(state.validator_registry, it.pubkey)
|
||||
validator_pubkeys = mapIt(state.validators, it.pubkey)
|
||||
index = validator_pubkeys.find(pubkey)
|
||||
|
||||
if index == -1:
|
||||
@ -86,7 +86,7 @@ func process_deposit*(
|
||||
return false
|
||||
|
||||
# Add validator and balance entries
|
||||
state.validator_registry.add(Validator(
|
||||
state.validators.add(Validator(
|
||||
pubkey: pubkey,
|
||||
withdrawal_credentials: deposit.data.withdrawal_credentials,
|
||||
activation_eligibility_epoch: FAR_FUTURE_EPOCH,
|
||||
@ -123,20 +123,20 @@ func initiate_validator_exit*(state: var BeaconState,
|
||||
# Initiate the exit of the validator with index ``index``.
|
||||
|
||||
# Return if validator already initiated exit
|
||||
let validator = addr state.validator_registry[index]
|
||||
let validator = addr state.validators[index]
|
||||
if validator.exit_epoch != FAR_FUTURE_EPOCH:
|
||||
return
|
||||
|
||||
# Compute exit queue epoch
|
||||
# TODO try zero-functional here
|
||||
let exit_epochs = mapIt(
|
||||
filterIt(state.validator_registry, it.exit_epoch != FAR_FUTURE_EPOCH),
|
||||
filterIt(state.validators, it.exit_epoch != FAR_FUTURE_EPOCH),
|
||||
it.exit_epoch)
|
||||
var exit_queue_epoch =
|
||||
max(max(exit_epochs),
|
||||
get_delayed_activation_exit_epoch(get_current_epoch(state)))
|
||||
let exit_queue_churn = foldl(
|
||||
state.validator_registry,
|
||||
state.validators,
|
||||
a + (if b.exit_epoch == exit_queue_epoch: 1'u64 else: 0'u64),
|
||||
0'u64)
|
||||
|
||||
@ -154,11 +154,11 @@ func slash_validator*(state: var BeaconState, slashed_index: ValidatorIndex,
|
||||
# Slash the validator with index ``index``.
|
||||
let current_epoch = get_current_epoch(state)
|
||||
initiate_validator_exit(state, slashed_index)
|
||||
state.validator_registry[slashed_index].slashed = true
|
||||
state.validator_registry[slashed_index].withdrawable_epoch =
|
||||
state.validators[slashed_index].slashed = true
|
||||
state.validators[slashed_index].withdrawable_epoch =
|
||||
current_epoch + LATEST_SLASHED_EXIT_LENGTH
|
||||
let slashed_balance =
|
||||
state.validator_registry[slashed_index].effective_balance
|
||||
state.validators[slashed_index].effective_balance
|
||||
state.latest_slashed_balances[current_epoch mod LATEST_SLASHED_EXIT_LENGTH] +=
|
||||
slashed_balance
|
||||
|
||||
@ -252,8 +252,8 @@ func get_genesis_beacon_state*(
|
||||
discard process_deposit(state, deposit, flags)
|
||||
|
||||
# Process genesis activations
|
||||
for validator_index in 0 ..< state.validator_registry.len:
|
||||
let validator = addr state.validator_registry[validator_index]
|
||||
for validator_index in 0 ..< state.validators.len:
|
||||
let validator = addr state.validators[validator_index]
|
||||
if validator.effective_balance >= MAX_EFFECTIVE_BALANCE:
|
||||
validator.activation_eligibility_epoch = GENESIS_EPOCH
|
||||
validator.activation_epoch = GENESIS_EPOCH
|
||||
@ -308,7 +308,7 @@ func get_total_balance*(state: BeaconState, validators: auto): Gwei =
|
||||
## Return the combined effective balance of the ``indices``. (1 Gwei minimum
|
||||
## to avoid divisions by zero.)
|
||||
max(1'u64,
|
||||
foldl(validators, a + state.validator_registry[b].effective_balance, 0'u64)
|
||||
foldl(validators, a + state.validators[b].effective_balance, 0'u64)
|
||||
)
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#registry-updates
|
||||
@ -316,10 +316,10 @@ func process_registry_updates*(state: var BeaconState) =
|
||||
## Process activation eligibility and ejections
|
||||
## Try to avoid caching here, since this could easily become undefined
|
||||
|
||||
for index, validator in state.validator_registry:
|
||||
for index, validator in state.validators:
|
||||
if validator.activation_eligibility_epoch == FAR_FUTURE_EPOCH and
|
||||
validator.effective_balance >= MAX_EFFECTIVE_BALANCE:
|
||||
state.validator_registry[index].activation_eligibility_epoch =
|
||||
state.validators[index].activation_eligibility_epoch =
|
||||
get_current_epoch(state)
|
||||
|
||||
if is_active_validator(validator, get_current_epoch(state)) and
|
||||
@ -329,12 +329,12 @@ func process_registry_updates*(state: var BeaconState) =
|
||||
## Queue validators eligible for activation and not dequeued for activation
|
||||
## prior to finalized epoch
|
||||
var activation_queue : seq[tuple[a: Epoch, b: int]] = @[]
|
||||
for index, validator in state.validator_registry:
|
||||
for index, validator in state.validators:
|
||||
if validator.activation_eligibility_epoch != FAR_FUTURE_EPOCH and
|
||||
validator.activation_epoch >=
|
||||
get_delayed_activation_exit_epoch(state.finalized_epoch):
|
||||
activation_queue.add (
|
||||
state.validator_registry[index].activation_eligibility_epoch, index)
|
||||
state.validators[index].activation_eligibility_epoch, index)
|
||||
|
||||
activation_queue.sort(system.cmp)
|
||||
|
||||
@ -346,7 +346,7 @@ func process_registry_updates*(state: var BeaconState) =
|
||||
break
|
||||
let
|
||||
(epoch, index) = epoch_and_index
|
||||
validator = addr state.validator_registry[index]
|
||||
validator = addr state.validators[index]
|
||||
if validator.activation_epoch == FAR_FUTURE_EPOCH:
|
||||
validator.activation_epoch =
|
||||
get_delayed_activation_exit_epoch(get_current_epoch(state))
|
||||
@ -384,9 +384,9 @@ func validate_indexed_attestation*(
|
||||
bls_verify_multiple(
|
||||
@[
|
||||
bls_aggregate_pubkeys(
|
||||
mapIt(bit_0_indices, state.validator_registry[it.int].pubkey)),
|
||||
mapIt(bit_0_indices, state.validators[it.int].pubkey)),
|
||||
bls_aggregate_pubkeys(
|
||||
mapIt(bit_1_indices, state.validator_registry[it.int].pubkey)),
|
||||
mapIt(bit_1_indices, state.validators[it.int].pubkey)),
|
||||
],
|
||||
@[
|
||||
hash_tree_root(AttestationDataAndCustodyBit(
|
||||
|
@ -241,7 +241,7 @@ type
|
||||
## For versioning hard forks
|
||||
|
||||
# Validator registry
|
||||
validator_registry*: seq[Validator]
|
||||
validators*: seq[Validator]
|
||||
balances*: seq[uint64] ##\
|
||||
## Validator balances in Gwei!
|
||||
|
||||
@ -360,7 +360,7 @@ type
|
||||
root*: Eth2Digest # hash_tree_root (not signing_root!)
|
||||
|
||||
func shortValidatorKey*(state: BeaconState, validatorIdx: int): string =
|
||||
($state.validator_registry[validatorIdx].pubkey)[0..7]
|
||||
($state.validators[validatorIdx].pubkey)[0..7]
|
||||
|
||||
template ethTimeUnit(typ: type) {.dirty.} =
|
||||
proc `+`*(x: typ, y: uint64): typ {.borrow.}
|
||||
|
@ -76,7 +76,7 @@ func is_active_validator*(validator: Validator, epoch: Epoch): bool =
|
||||
func get_active_validator_indices*(state: BeaconState, epoch: Epoch):
|
||||
seq[ValidatorIndex] =
|
||||
# Get active validator indices at ``epoch``.
|
||||
for idx, val in state.validator_registry:
|
||||
for idx, val in state.validators:
|
||||
if is_active_validator(val, epoch):
|
||||
result.add idx.ValidatorIndex
|
||||
|
||||
|
@ -66,7 +66,7 @@ proc processBlockHeader(
|
||||
|
||||
# Verify proposer is not slashed
|
||||
let proposer =
|
||||
state.validator_registry[get_beacon_proposer_index(state, stateCache)]
|
||||
state.validators[get_beacon_proposer_index(state, stateCache)]
|
||||
if proposer.slashed:
|
||||
notice "Block header: proposer slashed"
|
||||
return false
|
||||
@ -91,7 +91,7 @@ proc processRandao(
|
||||
stateCache: var StateCache): bool =
|
||||
let
|
||||
proposer_index = get_beacon_proposer_index(state, stateCache)
|
||||
proposer = addr state.validator_registry[proposer_index]
|
||||
proposer = addr state.validators[proposer_index]
|
||||
|
||||
# Verify that the provided randao value is valid
|
||||
if skipValidation notin flags:
|
||||
@ -141,7 +141,7 @@ proc processProposerSlashings(
|
||||
return false
|
||||
|
||||
for proposer_slashing in blck.body.proposer_slashings:
|
||||
let proposer = state.validator_registry[proposer_slashing.proposer_index.int]
|
||||
let proposer = state.validators[proposer_slashing.proposer_index.int]
|
||||
|
||||
# Verify that the epoch is the same
|
||||
if not (compute_epoch_of_slot(proposer_slashing.header_1.slot) ==
|
||||
@ -227,7 +227,7 @@ proc processAttesterSlashings(state: var BeaconState, blck: BeaconBlock,
|
||||
attestation_2.custody_bit_0_indices & attestation_2.custody_bit_1_indices
|
||||
for index in sorted(toSeq(intersection(toSet(attesting_indices_1),
|
||||
toSet(attesting_indices_2)).items), system.cmp):
|
||||
if is_slashable_validator(state.validator_registry[index.int],
|
||||
if is_slashable_validator(state.validators[index.int],
|
||||
get_current_epoch(state)):
|
||||
slash_validator(state, index.ValidatorIndex, stateCache)
|
||||
slashed_any = true
|
||||
@ -303,7 +303,7 @@ proc processVoluntaryExits(
|
||||
return false
|
||||
|
||||
for exit in blck.body.voluntary_exits:
|
||||
let validator = state.validator_registry[exit.validator_index.int]
|
||||
let validator = state.validators[exit.validator_index.int]
|
||||
|
||||
# Verify the validator is active
|
||||
if not is_active_validator(validator, get_current_epoch(state)):
|
||||
@ -365,10 +365,10 @@ proc processTransfers(state: var BeaconState, blck: BeaconBlock,
|
||||
## Sender must be not yet eligible for activation, withdrawn, or transfer
|
||||
## balance over MAX_EFFECTIVE_BALANCE
|
||||
if not (
|
||||
state.validator_registry[transfer.sender.int].activation_epoch ==
|
||||
state.validators[transfer.sender.int].activation_epoch ==
|
||||
FAR_FUTURE_EPOCH or
|
||||
get_current_epoch(state) >=
|
||||
state.validator_registry[
|
||||
state.validators[
|
||||
transfer.sender.int].withdrawable_epoch or
|
||||
transfer.amount + transfer.fee + MAX_EFFECTIVE_BALANCE <=
|
||||
state.balances[transfer.sender.int]):
|
||||
@ -376,7 +376,7 @@ proc processTransfers(state: var BeaconState, blck: BeaconBlock,
|
||||
return false
|
||||
|
||||
# Verify that the pubkey is valid
|
||||
let wc = state.validator_registry[transfer.sender.int].
|
||||
let wc = state.validators[transfer.sender.int].
|
||||
withdrawal_credentials
|
||||
if not (wc.data[0] == BLS_WITHDRAWAL_PREFIX and
|
||||
wc.data[1..^1] == eth2hash(transfer.pubkey.getBytes).data[1..^1]):
|
||||
|
@ -75,7 +75,7 @@ func get_unslashed_attesting_indices(
|
||||
state, a.data, a.aggregation_bits, stateCache))
|
||||
|
||||
for index in result:
|
||||
if state.validator_registry[index].slashed:
|
||||
if state.validators[index].slashed:
|
||||
result.excl index
|
||||
|
||||
func get_attesting_balance(
|
||||
@ -239,7 +239,7 @@ func process_crosslinks(state: var BeaconState, stateCache: var StateCache) =
|
||||
func get_base_reward(state: BeaconState, index: ValidatorIndex): Gwei =
|
||||
let
|
||||
total_balance = get_total_active_balance(state)
|
||||
effective_balance = state.validator_registry[index].effective_balance
|
||||
effective_balance = state.validators[index].effective_balance
|
||||
effective_balance * BASE_REWARD_FACTOR div
|
||||
integer_squareroot(total_balance) div BASE_REWARDS_PER_EPOCH
|
||||
|
||||
@ -250,11 +250,11 @@ func get_attestation_deltas(state: BeaconState, stateCache: var StateCache):
|
||||
previous_epoch = get_previous_epoch(state)
|
||||
total_balance = get_total_active_balance(state)
|
||||
var
|
||||
rewards = repeat(0'u64, len(state.validator_registry))
|
||||
penalties = repeat(0'u64, len(state.validator_registry))
|
||||
rewards = repeat(0'u64, len(state.validators))
|
||||
penalties = repeat(0'u64, len(state.validators))
|
||||
eligible_validator_indices : seq[ValidatorIndex] = @[]
|
||||
|
||||
for index, v in state.validator_registry:
|
||||
for index, v in state.validators:
|
||||
if is_active_validator(v, previous_epoch) or
|
||||
(v.slashed and previous_epoch + 1 < v.withdrawable_epoch):
|
||||
eligible_validator_indices.add index.ValidatorIndex
|
||||
@ -312,7 +312,7 @@ func get_attestation_deltas(state: BeaconState, stateCache: var StateCache):
|
||||
BASE_REWARDS_PER_EPOCH.uint64 * get_base_reward(state, index)
|
||||
if index notin matching_target_attesting_indices:
|
||||
penalties[index] +=
|
||||
state.validator_registry[index].effective_balance *
|
||||
state.validators[index].effective_balance *
|
||||
finality_delay div INACTIVITY_PENALTY_QUOTIENT
|
||||
|
||||
(rewards, penalties)
|
||||
@ -322,8 +322,8 @@ func get_crosslink_deltas(state: BeaconState, cache: var StateCache):
|
||||
tuple[a: seq[Gwei], b: seq[Gwei]] =
|
||||
|
||||
var
|
||||
rewards = repeat(0'u64, len(state.validator_registry))
|
||||
penalties = repeat(0'u64, len(state.validator_registry))
|
||||
rewards = repeat(0'u64, len(state.validators))
|
||||
penalties = repeat(0'u64, len(state.validators))
|
||||
let epoch = get_previous_epoch(state)
|
||||
for offset in 0'u64 ..< get_epoch_committee_count(state, epoch):
|
||||
let
|
||||
@ -355,7 +355,7 @@ func process_rewards_and_penalties(
|
||||
let
|
||||
(rewards1, penalties1) = get_attestation_deltas(state, cache)
|
||||
(rewards2, penalties2) = get_crosslink_deltas(state, cache)
|
||||
for i in 0 ..< len(state.validator_registry):
|
||||
for i in 0 ..< len(state.validators):
|
||||
increase_balance(state, i.ValidatorIndex, rewards1[i] + rewards2[i])
|
||||
decrease_balance(state, i.ValidatorIndex, penalties1[i] + penalties2[i])
|
||||
|
||||
@ -373,7 +373,7 @@ func process_slashings(state: var BeaconState) =
|
||||
LATEST_SLASHED_EXIT_LENGTH]
|
||||
total_penalties = total_at_end - total_at_start
|
||||
|
||||
for index, validator in state.validator_registry:
|
||||
for index, validator in state.validators:
|
||||
if validator.slashed and current_epoch == validator.withdrawable_epoch -
|
||||
LATEST_SLASHED_EXIT_LENGTH div 2:
|
||||
let
|
||||
@ -394,12 +394,12 @@ func process_final_updates(state: var BeaconState) =
|
||||
state.eth1_data_votes = @[]
|
||||
|
||||
# Update effective balances with hysteresis
|
||||
for index, validator in state.validator_registry:
|
||||
for index, validator in state.validators:
|
||||
let balance = state.balances[index]
|
||||
const HALF_INCREMENT = EFFECTIVE_BALANCE_INCREMENT div 2
|
||||
if balance < validator.effective_balance or
|
||||
validator.effective_balance + 3'u64 * HALF_INCREMENT < balance:
|
||||
state.validator_registry[index].effective_balance =
|
||||
state.validators[index].effective_balance =
|
||||
min(
|
||||
balance - balance mod EFFECTIVE_BALANCE_INCREMENT,
|
||||
MAX_EFFECTIVE_BALANCE)
|
||||
|
@ -194,7 +194,7 @@ func get_beacon_proposer_index*(state: BeaconState, stateCache: var StateCache):
|
||||
len(first_committee).uint64).int]
|
||||
random_byte = (eth2hash(buffer).data)[i mod 32]
|
||||
effective_balance =
|
||||
state.validator_registry[candidate_index].effective_balance
|
||||
state.validators[candidate_index].effective_balance
|
||||
if effective_balance * MAX_RANDOM_BYTE >=
|
||||
MAX_EFFECTIVE_BALANCE * random_byte:
|
||||
return candidate_index
|
||||
|
@ -15,4 +15,4 @@ suite "Beacon state" & preset():
|
||||
test "Smoke test get_genesis_beacon_state" & preset():
|
||||
let state = get_genesis_beacon_state(
|
||||
makeInitialDeposits(SLOTS_PER_EPOCH, {}), 0, Eth1Data(), {})
|
||||
check: state.validator_registry.len == SLOTS_PER_EPOCH
|
||||
check: state.validators.len == SLOTS_PER_EPOCH
|
||||
|
@ -85,7 +85,7 @@ proc addBlock*(
|
||||
|
||||
let
|
||||
# Index from the new state, but registry from the old state.. hmm...
|
||||
proposer = state.validator_registry[proposer_index]
|
||||
proposer = state.validators[proposer_index]
|
||||
privKey = hackPrivKey(proposer)
|
||||
|
||||
# TODO ugly hack; API needs rethinking
|
||||
@ -157,7 +157,7 @@ proc makeAttestation*(
|
||||
validator_index: ValidatorIndex, flags: UpdateFlags = {}): Attestation =
|
||||
let
|
||||
(committee, shard) = find_shard_committee(state, validator_index)
|
||||
validator = state.validator_registry[validator_index]
|
||||
validator = state.validators[validator_index]
|
||||
sac_index = committee.find(validator_index)
|
||||
data = makeAttestationData(state, shard, beacon_block_root)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user