2019-02-19 23:35:02 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
options, sequtils, unittest,
|
|
|
|
./testutil,
|
|
|
|
../beacon_chain/spec/[beaconstate, crypto, datatypes, digest, helpers, validator],
|
|
|
|
../beacon_chain/[attestation_pool, extras, state_transition, ssz]
|
|
|
|
|
|
|
|
suite "Attestation pool processing":
|
|
|
|
## For now just test that we can compile and execute block processing with
|
|
|
|
## mock data.
|
|
|
|
|
|
|
|
let
|
|
|
|
# Genesis state with minimal number of deposits
|
|
|
|
# TODO bls verification is a bit of a bottleneck here
|
2019-02-22 09:56:45 +00:00
|
|
|
genesisState = get_genesis_beacon_state(
|
2019-02-19 23:35:02 +00:00
|
|
|
makeInitialDeposits(), 0, Eth1Data(), {skipValidation})
|
2019-02-21 04:42:17 +00:00
|
|
|
genesisBlock = get_initial_beacon_block(genesisState)
|
2019-02-19 23:35:02 +00:00
|
|
|
genesisRoot = hash_tree_root_final(genesisBlock)
|
|
|
|
|
|
|
|
test "Can add and retrieve simple attestation":
|
|
|
|
var
|
|
|
|
pool = init(AttestationPool, 42)
|
|
|
|
state = genesisState
|
|
|
|
# Slot 0 is a finalized slot - won't be making attestations for it..
|
|
|
|
discard updateState(
|
|
|
|
state, genesisRoot, none(BeaconBlock), {skipValidation})
|
|
|
|
|
|
|
|
let
|
|
|
|
# Create an attestation for slot 1 signed by the only attester we have!
|
|
|
|
crosslink_committees = get_crosslink_committees_at_slot(state, state.slot)
|
|
|
|
attestation = makeAttestation(
|
|
|
|
state, genesisRoot, crosslink_committees[0].committee[0])
|
|
|
|
|
|
|
|
pool.add(attestation, state)
|
|
|
|
|
|
|
|
let attestations = pool.getAttestationsForBlock(
|
|
|
|
state, state.slot + MIN_ATTESTATION_INCLUSION_DELAY)
|
|
|
|
|
|
|
|
check:
|
|
|
|
attestations.len == 1
|
2019-02-21 04:42:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
test "Attestations may arrive in any order":
|
|
|
|
var
|
|
|
|
pool = init(AttestationPool, 42)
|
|
|
|
state = genesisState
|
|
|
|
# Slot 0 is a finalized slot - won't be making attestations for it..
|
|
|
|
discard updateState(
|
|
|
|
state, genesisRoot, none(BeaconBlock), {skipValidation})
|
|
|
|
|
|
|
|
let
|
|
|
|
# Create an attestation for slot 1 signed by the only attester we have!
|
|
|
|
crosslink_committees1 = get_crosslink_committees_at_slot(state, state.slot)
|
|
|
|
attestation1 = makeAttestation(
|
|
|
|
state, genesisRoot, crosslink_committees1[0].committee[0])
|
|
|
|
|
|
|
|
discard updateState(
|
|
|
|
state, genesisRoot, none(BeaconBlock), {skipValidation})
|
|
|
|
|
|
|
|
let
|
|
|
|
crosslink_committees2 = get_crosslink_committees_at_slot(state, state.slot)
|
|
|
|
attestation2 = makeAttestation(
|
|
|
|
state, genesisRoot, crosslink_committees2[0].committee[0])
|
|
|
|
|
|
|
|
# test reverse order
|
|
|
|
pool.add(attestation2, state)
|
|
|
|
pool.add(attestation1, state)
|
|
|
|
|
|
|
|
let attestations = pool.getAttestationsForBlock(
|
|
|
|
state, state.slot + MIN_ATTESTATION_INCLUSION_DELAY)
|
|
|
|
|
|
|
|
check:
|
|
|
|
attestations.len == 1
|