2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
2021-02-25 13:37:22 +00:00
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
2019-08-28 12:07:00 +00:00
|
|
|
# Licensed and distributed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * 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).
|
2019-08-28 12:07:00 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
# process_attestation (beaconstate.nim)
|
2021-12-03 17:40:23 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.6/specs/phase0/beacon-chain.md#attestations
|
2019-08-28 12:07:00 +00:00
|
|
|
# ---------------------------------------------------------------
|
|
|
|
|
2019-11-14 10:47:55 +00:00
|
|
|
{.used.}
|
|
|
|
|
2019-08-28 12:07:00 +00:00
|
|
|
import
|
2020-07-03 17:03:14 +00:00
|
|
|
stew/results,
|
2019-08-28 12:07:00 +00:00
|
|
|
# Specs
|
2021-08-10 20:46:35 +00:00
|
|
|
../../beacon_chain/spec/[beaconstate, forks, helpers],
|
2021-06-21 08:35:24 +00:00
|
|
|
../../beacon_chain/spec/datatypes/base,
|
2019-08-28 12:07:00 +00:00
|
|
|
# Mock helpers
|
2020-05-19 14:37:29 +00:00
|
|
|
../mocking/[mock_genesis, mock_attestations, mock_state],
|
2019-08-28 12:07:00 +00:00
|
|
|
../testutil
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
suite "[Unit - Spec - Block processing] Attestations " & preset():
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
const NumValidators = uint64(8) * SLOTS_PER_EPOCH
|
2021-09-13 22:56:37 +00:00
|
|
|
let genesisState = initGenesisState(NumValidators)
|
2021-06-11 17:51:46 +00:00
|
|
|
|
|
|
|
doAssert getStateField(genesisState[], validators).lenu64 == NumValidators
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
template valid_attestation(name: string, body: untyped): untyped {.dirty.}=
|
|
|
|
# Process a valid attestation
|
|
|
|
#
|
|
|
|
# The BeaconState is exposed as "state" in the calling context
|
|
|
|
# The attestation to process must be named "attestation" in the calling context
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test name:
|
2020-05-28 08:28:14 +00:00
|
|
|
var state {.inject.} = newClone(genesisState[])
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Attestation setup body
|
|
|
|
# ----------------------------------------
|
|
|
|
body
|
|
|
|
|
|
|
|
# Params for sanity checks
|
|
|
|
# ----------------------------------------
|
|
|
|
let
|
2021-06-11 17:51:46 +00:00
|
|
|
current_epoch_count =
|
2021-10-18 16:37:27 +00:00
|
|
|
state.phase0Data.data.current_epoch_attestations.len
|
2021-06-11 17:51:46 +00:00
|
|
|
previous_epoch_count =
|
2021-10-18 16:37:27 +00:00
|
|
|
state.phase0Data.data.previous_epoch_attestations.len
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# State transition
|
|
|
|
# ----------------------------------------
|
2020-07-15 10:44:18 +00:00
|
|
|
var cache = StateCache()
|
2019-08-28 12:07:00 +00:00
|
|
|
check process_attestation(
|
2021-10-18 16:37:27 +00:00
|
|
|
state.phase0Data.data, attestation, flags = {}, 0.Gwei, cache
|
2020-07-03 17:03:14 +00:00
|
|
|
).isOk
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Check that the attestation was processed
|
2021-06-11 17:51:46 +00:00
|
|
|
if attestation.data.target.epoch == get_current_epoch(state[]):
|
2021-10-18 16:37:27 +00:00
|
|
|
check(state.phase0Data.data.current_epoch_attestations.len ==
|
2021-06-11 17:51:46 +00:00
|
|
|
current_epoch_count + 1)
|
2019-08-28 12:07:00 +00:00
|
|
|
else:
|
2021-10-18 16:37:27 +00:00
|
|
|
check(state.phase0Data.data.previous_epoch_attestations.len ==
|
2021-06-11 17:51:46 +00:00
|
|
|
previous_epoch_count + 1)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
valid_attestation("Valid attestation"):
|
2021-10-18 16:37:27 +00:00
|
|
|
let attestation = mockAttestation(state.phase0Data.data)
|
2021-06-11 17:51:46 +00:00
|
|
|
getStateField(state[], slot) += MIN_ATTESTATION_INCLUSION_DELAY
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
valid_attestation("Valid attestation from previous epoch"):
|
2020-05-19 14:37:29 +00:00
|
|
|
nextSlot(state[])
|
2021-10-18 16:37:27 +00:00
|
|
|
let attestation = mockAttestation(state.phase0Data.data)
|
2021-06-11 17:51:46 +00:00
|
|
|
getStateField(state[], slot) = Slot(SLOTS_PER_EPOCH - 1)
|
2020-04-23 18:58:54 +00:00
|
|
|
nextEpoch(state[])
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-03-11 16:06:06 +00:00
|
|
|
# TODO: regression BLS V0.10.1
|
|
|
|
echo "[Skipping] \"Empty aggregation bit\""
|
|
|
|
|
|
|
|
# valid_attestation("Empty aggregation bit"):
|
|
|
|
# var attestation = mockAttestation(state)
|
2020-04-30 06:44:19 +00:00
|
|
|
# state.data.slot += MIN_ATTESTATION_INCLUSION_DELAY
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-03-11 16:06:06 +00:00
|
|
|
# # Overwrite committee
|
|
|
|
# attestation.aggregation_bits = init(CommitteeValidatorsBits, attestation.aggregation_bits.len)
|
|
|
|
# signMockAttestation(state, attestation)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# TODO - invalid attestations
|
|
|
|
# - Wrong end epoch
|
|
|
|
# - Invalid signature
|
|
|
|
# - Before inclusion delay
|
|
|
|
# - past last inclusion slot
|
|
|
|
# - before oldest known source epoch
|
|
|
|
# - wrong shard
|
|
|
|
# - invalid shard
|
|
|
|
# - target epoch too old
|
|
|
|
# - target epoch too far in the future
|
|
|
|
# - source epoch in the future
|
|
|
|
# - invalid current source root
|
|
|
|
# - bad source root
|
|
|
|
# - inconsistent custody bits length
|
|
|
|
# - non-empty custody bits in phase 0
|