2021-01-18 20:34:41 +00:00
|
|
|
# beacon_chain
|
2021-02-15 16:40:00 +00:00
|
|
|
# Copyright (c) 2020-2021 Status Research & Development GmbH
|
2021-01-18 20:34:41 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import
|
2021-03-11 05:39:04 +00:00
|
|
|
stew/assign2,
|
2021-08-12 13:08:20 +00:00
|
|
|
./spec/datatypes/phase0,
|
|
|
|
./spec/helpers
|
2021-01-18 20:34:41 +00:00
|
|
|
|
|
|
|
func diffModIncEpoch[T, U](hl: HashArray[U, T], startSlot: uint64):
|
|
|
|
array[SLOTS_PER_EPOCH, T] =
|
|
|
|
static: doAssert U.uint64 mod SLOTS_PER_EPOCH == 0
|
|
|
|
doAssert startSlot mod SLOTS_PER_EPOCH == 0
|
|
|
|
for i in startSlot ..< startSlot + SLOTS_PER_EPOCH:
|
|
|
|
result[i mod SLOTS_PER_EPOCH] = hl[i mod U.uint64]
|
|
|
|
|
|
|
|
func applyModIncrement[T, U](
|
|
|
|
ha: var HashArray[U, T], hl: array[SLOTS_PER_EPOCH, T], slot: uint64) =
|
|
|
|
var indexSlot = slot
|
|
|
|
|
|
|
|
for item in hl:
|
|
|
|
ha[indexSlot mod U.uint64] = item
|
|
|
|
indexSlot += 1
|
|
|
|
|
|
|
|
func applyValidatorIdentities(
|
|
|
|
validators: var HashList[Validator, Limit VALIDATOR_REGISTRY_LIMIT],
|
|
|
|
hl: auto) =
|
|
|
|
for item in hl:
|
2021-04-08 10:11:04 +00:00
|
|
|
if not validators.add Validator(
|
2021-06-10 07:37:02 +00:00
|
|
|
pubkey: item.pubkey.loadValid().toPubKey(),
|
2021-04-08 10:11:04 +00:00
|
|
|
withdrawal_credentials: item.withdrawal_credentials):
|
|
|
|
raiseAssert "cannot readd"
|
2021-01-18 20:34:41 +00:00
|
|
|
|
|
|
|
func setValidatorStatuses(
|
|
|
|
validators: var HashList[Validator, Limit VALIDATOR_REGISTRY_LIMIT],
|
|
|
|
hl: List[ValidatorStatus, Limit VALIDATOR_REGISTRY_LIMIT]) =
|
|
|
|
doAssert validators.len == hl.len
|
|
|
|
|
|
|
|
for i in 0 ..< hl.len:
|
|
|
|
validators[i].effective_balance = hl[i].effective_balance
|
|
|
|
validators[i].slashed = hl[i].slashed
|
|
|
|
|
|
|
|
validators[i].activation_eligibility_epoch =
|
|
|
|
hl[i].activation_eligibility_epoch
|
|
|
|
validators[i].activation_epoch = hl[i].activation_epoch
|
|
|
|
validators[i].exit_epoch = hl[i].exit_epoch
|
|
|
|
validators[i].withdrawable_epoch = hl[i].withdrawable_epoch
|
|
|
|
|
|
|
|
func replaceOrAddEncodeEth1Votes[T, U](votes0, votes1: HashList[T, U]):
|
|
|
|
(bool, List[T, U]) =
|
|
|
|
let
|
|
|
|
num_votes0 = votes0.len
|
|
|
|
lower_bound =
|
|
|
|
if votes1.len < num_votes0 or
|
|
|
|
(num_votes0 > 0 and votes0[num_votes0 - 1] != votes1[num_votes0 - 1]):
|
|
|
|
# EPOCHS_PER_ETH1_VOTING_PERIOD epochs have passed, and
|
|
|
|
# eth1_data_votes has been reset/cleared. Because their
|
|
|
|
# deposit_index counts increase monotonically, it works
|
|
|
|
# to use only the last element for comparison.
|
|
|
|
0
|
|
|
|
else:
|
|
|
|
num_votes0
|
|
|
|
|
|
|
|
result[0] = lower_bound == 0
|
|
|
|
for i in lower_bound ..< votes1.len:
|
2021-04-08 10:11:04 +00:00
|
|
|
if not result[1].add votes1[i]:
|
|
|
|
raiseAssert "same limit"
|
2021-01-18 20:34:41 +00:00
|
|
|
|
|
|
|
func replaceOrAddDecodeEth1Votes[T, U](
|
|
|
|
votes0: var HashList[T, U], eth1_data_votes_replaced: bool,
|
|
|
|
votes1: List[T, U]) =
|
|
|
|
if eth1_data_votes_replaced:
|
|
|
|
votes0 = HashList[T, U]()
|
|
|
|
|
|
|
|
for item in votes1:
|
2021-04-08 10:11:04 +00:00
|
|
|
if not votes0.add item:
|
|
|
|
raiseAssert "same limit"
|
2021-01-18 20:34:41 +00:00
|
|
|
|
2021-08-12 13:08:20 +00:00
|
|
|
func getMutableValidatorStatuses(state: phase0.BeaconState):
|
2021-02-15 16:40:00 +00:00
|
|
|
List[ValidatorStatus, Limit VALIDATOR_REGISTRY_LIMIT] =
|
2021-04-08 10:11:04 +00:00
|
|
|
if not result.setLen(state.validators.len):
|
|
|
|
raiseAssert "same limt as validators"
|
2021-03-15 14:11:51 +00:00
|
|
|
for i in 0 ..< state.validators.len:
|
|
|
|
let validator = unsafeAddr state.validators.data[i]
|
|
|
|
assign(result[i].effective_balance, validator.effective_balance)
|
|
|
|
assign(result[i].slashed, validator.slashed)
|
|
|
|
assign(
|
|
|
|
result[i].activation_eligibility_epoch,
|
|
|
|
validator.activation_eligibility_epoch)
|
|
|
|
assign(result[i].activation_epoch, validator.activation_epoch)
|
|
|
|
assign(result[i].exit_epoch, validator.exit_epoch)
|
|
|
|
assign(result[i].withdrawable_epoch, validator.withdrawable_epoch)
|
2021-02-15 16:40:00 +00:00
|
|
|
|
2021-08-12 13:08:20 +00:00
|
|
|
func diffStates*(state0, state1: phase0.BeaconState): BeaconStateDiff =
|
2021-01-18 20:34:41 +00:00
|
|
|
doAssert state1.slot > state0.slot
|
|
|
|
doAssert state0.slot.isEpoch
|
|
|
|
doAssert state1.slot == state0.slot + SLOTS_PER_EPOCH
|
2021-06-01 11:13:40 +00:00
|
|
|
# TODO not here, but in dag, an isancestorof check
|
2021-01-18 20:34:41 +00:00
|
|
|
|
|
|
|
doAssert state0.genesis_time == state1.genesis_time
|
|
|
|
doAssert state0.genesis_validators_root == state1.genesis_validators_root
|
|
|
|
doAssert state0.fork == state1.fork
|
|
|
|
doAssert state1.historical_roots.len - state0.historical_roots.len in [0, 1]
|
|
|
|
|
|
|
|
let
|
|
|
|
historical_root_added =
|
|
|
|
state0.historical_roots.len != state1.historical_roots.len
|
|
|
|
(eth1_data_votes_replaced, eth1_data_votes) =
|
|
|
|
replaceOrAddEncodeEth1Votes(state0.eth1_data_votes, state1.eth1_data_votes)
|
|
|
|
|
|
|
|
BeaconStateDiff(
|
|
|
|
slot: state1.slot,
|
|
|
|
latest_block_header: state1.latest_block_header,
|
|
|
|
|
|
|
|
block_roots: diffModIncEpoch(state1.block_roots, state0.slot.uint64),
|
|
|
|
state_roots: diffModIncEpoch(state1.state_roots, state0.slot.uint64),
|
|
|
|
historical_root_added: historical_root_added,
|
|
|
|
historical_root:
|
|
|
|
if historical_root_added:
|
|
|
|
state1.historical_roots[state0.historical_roots.len]
|
|
|
|
else:
|
|
|
|
default(Eth2Digest),
|
|
|
|
eth1_data: state1.eth1_data,
|
|
|
|
eth1_data_votes_replaced: eth1_data_votes_replaced,
|
|
|
|
eth1_data_votes: eth1_data_votes,
|
|
|
|
eth1_deposit_index: state1.eth1_deposit_index,
|
|
|
|
|
2021-02-15 16:40:00 +00:00
|
|
|
validatorStatuses: getMutableValidatorStatuses(state1),
|
2021-03-11 05:39:04 +00:00
|
|
|
balances: state1.balances.data,
|
2021-01-18 20:34:41 +00:00
|
|
|
|
|
|
|
# RANDAO mixes gets updated every block, in place
|
|
|
|
randao_mix: state1.randao_mixes[state0.slot.compute_epoch_at_slot.uint64 mod
|
|
|
|
EPOCHS_PER_HISTORICAL_VECTOR.uint64],
|
|
|
|
slashing: state1.slashings[state0.slot.compute_epoch_at_slot.uint64 mod
|
|
|
|
EPOCHS_PER_HISTORICAL_VECTOR.uint64],
|
|
|
|
|
2021-03-11 05:39:04 +00:00
|
|
|
previous_epoch_attestations: state1.previous_epoch_attestations.data,
|
|
|
|
current_epoch_attestations: state1.current_epoch_attestations.data,
|
2021-01-18 20:34:41 +00:00
|
|
|
|
|
|
|
justification_bits: state1.justification_bits,
|
|
|
|
previous_justified_checkpoint: state1.previous_justified_checkpoint,
|
|
|
|
current_justified_checkpoint: state1.current_justified_checkpoint,
|
|
|
|
finalized_checkpoint: state1.finalized_checkpoint
|
|
|
|
)
|
|
|
|
|
|
|
|
func applyDiff*(
|
2021-08-12 13:08:20 +00:00
|
|
|
state: var phase0.BeaconState,
|
2021-06-10 07:37:02 +00:00
|
|
|
immutableValidators: openArray[ImmutableValidatorData2],
|
2021-01-18 20:34:41 +00:00
|
|
|
stateDiff: BeaconStateDiff) =
|
2021-03-11 05:39:04 +00:00
|
|
|
template assign[T, U](tgt: var HashList[T, U], src: List[T, U]) =
|
|
|
|
assign(tgt.data, src)
|
2021-04-08 10:11:04 +00:00
|
|
|
tgt.resetCache()
|
2021-03-11 05:39:04 +00:00
|
|
|
|
2021-01-18 20:34:41 +00:00
|
|
|
# Carry over unchanged genesis_time, genesis_validators_root, and fork.
|
2021-03-11 05:39:04 +00:00
|
|
|
assign(state.latest_block_header, stateDiff.latest_block_header)
|
2021-01-18 20:34:41 +00:00
|
|
|
|
|
|
|
applyModIncrement(state.block_roots, stateDiff.block_roots, state.slot.uint64)
|
|
|
|
applyModIncrement(state.state_roots, stateDiff.state_roots, state.slot.uint64)
|
|
|
|
if stateDiff.historical_root_added:
|
2021-04-08 10:11:04 +00:00
|
|
|
if not state.historical_roots.add stateDiff.historical_root:
|
|
|
|
raiseAssert "cannot readd historical state root"
|
2021-01-18 20:34:41 +00:00
|
|
|
|
2021-03-11 05:39:04 +00:00
|
|
|
assign(state.eth1_data, stateDiff.eth1_data)
|
2021-01-18 20:34:41 +00:00
|
|
|
replaceOrAddDecodeEth1Votes(
|
|
|
|
state.eth1_data_votes, stateDiff.eth1_data_votes_replaced,
|
|
|
|
stateDiff.eth1_data_votes)
|
2021-03-11 05:39:04 +00:00
|
|
|
assign(state.eth1_deposit_index, stateDiff.eth1_deposit_index)
|
2021-01-18 20:34:41 +00:00
|
|
|
|
|
|
|
applyValidatorIdentities(state.validators, immutableValidators)
|
|
|
|
setValidatorStatuses(state.validators, stateDiff.validator_statuses)
|
2021-03-11 05:39:04 +00:00
|
|
|
assign(state.balances, stateDiff.balances)
|
2021-01-18 20:34:41 +00:00
|
|
|
|
|
|
|
# RANDAO mixes gets updated every block, in place, so ensure there's always
|
|
|
|
# >=1 value from it
|
|
|
|
let epochIndex =
|
|
|
|
state.slot.epoch.uint64 mod EPOCHS_PER_HISTORICAL_VECTOR.uint64
|
2021-03-11 05:39:04 +00:00
|
|
|
assign(state.randao_mixes[epochIndex], stateDiff.randao_mix)
|
|
|
|
assign(state.slashings[epochIndex], stateDiff.slashing)
|
2021-01-18 20:34:41 +00:00
|
|
|
|
2021-03-11 05:39:04 +00:00
|
|
|
assign(
|
|
|
|
state.previous_epoch_attestations, stateDiff.previous_epoch_attestations)
|
|
|
|
assign(
|
|
|
|
state.current_epoch_attestations, stateDiff.current_epoch_attestations)
|
2021-01-18 20:34:41 +00:00
|
|
|
|
|
|
|
state.justification_bits = stateDiff.justification_bits
|
2021-03-11 05:39:04 +00:00
|
|
|
assign(
|
|
|
|
state.previous_justified_checkpoint, stateDiff.previous_justified_checkpoint)
|
|
|
|
assign(
|
|
|
|
state.current_justified_checkpoint, stateDiff.current_justified_checkpoint)
|
|
|
|
assign(state.finalized_checkpoint, stateDiff.finalized_checkpoint)
|
2021-01-18 20:34:41 +00:00
|
|
|
|
|
|
|
# Don't update slot until the end, because various other updates depend on it
|
|
|
|
state.slot = stateDiff.slot
|