2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
2020-05-18 23:05:27 +00:00
|
|
|
# Copyright (c) 2018-2020 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)
|
2020-07-27 16:49:46 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/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
|
|
|
|
# Standard library
|
2019-11-14 10:47:55 +00:00
|
|
|
unittest,
|
2020-07-03 17:03:14 +00:00
|
|
|
stew/results,
|
2019-08-28 12:07:00 +00:00
|
|
|
# Specs
|
2020-07-15 10:44:18 +00:00
|
|
|
../../beacon_chain/spec/[beaconstate, datatypes, helpers],
|
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
|
|
|
|
|
2020-03-10 04:00:19 +00:00
|
|
|
suiteReport "[Unit - Spec - Block processing] Attestations " & preset():
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
const NumValidators = uint64(8) * SLOTS_PER_EPOCH
|
2020-05-28 08:28:14 +00:00
|
|
|
let genesisState = newClone(initGenesisState(NumValidators))
|
2020-07-26 18:55:48 +00:00
|
|
|
doAssert genesisState.data.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
|
|
|
|
|
2019-12-05 10:27:00 +00:00
|
|
|
timedTest 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
|
2020-04-30 06:44:19 +00:00
|
|
|
current_epoch_count = state.data.current_epoch_attestations.len
|
|
|
|
previous_epoch_count = state.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(
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data, attestation, flags = {}, cache
|
2020-07-03 17:03:14 +00:00
|
|
|
).isOk
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Check that the attestation was processed
|
2020-04-30 06:44:19 +00:00
|
|
|
if attestation.data.target.epoch == get_current_epoch(state.data):
|
|
|
|
check(state.data.current_epoch_attestations.len == current_epoch_count + 1)
|
2019-08-28 12:07:00 +00:00
|
|
|
else:
|
2020-04-30 06:44:19 +00:00
|
|
|
check(state.data.previous_epoch_attestations.len == previous_epoch_count + 1)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
valid_attestation("Valid attestation"):
|
2020-04-30 06:44:19 +00:00
|
|
|
let attestation = mockAttestation(state.data)
|
|
|
|
state.data.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[])
|
2020-04-30 06:44:19 +00:00
|
|
|
let attestation = mockAttestation(state.data)
|
|
|
|
state.data.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
|