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],
|
2019-03-12 15:03:14 +00:00
|
|
|
../beacon_chain/[beacon_node_types, attestation_pool, block_pool, extras, state_transition, ssz]
|
2019-02-19 23:35:02 +00:00
|
|
|
|
2019-06-03 08:26:38 +00:00
|
|
|
template withPool(body: untyped) =
|
|
|
|
mixin genState, genBlock
|
|
|
|
|
|
|
|
var
|
|
|
|
blockPool {.inject.} = BlockPool.init(makeTestDB(genState, genBlock))
|
|
|
|
pool {.inject.} = AttestationPool.init(blockPool)
|
|
|
|
state {.inject.} = loadTailState(blockPool)
|
|
|
|
# Slot 0 is a finalized slot - won't be making attestations for it..
|
|
|
|
advanceState(state.data)
|
|
|
|
|
|
|
|
body
|
|
|
|
|
2019-05-27 12:48:13 +00:00
|
|
|
suite "Attestation pool processing" & preset():
|
2019-02-19 23:35:02 +00:00
|
|
|
## For now just test that we can compile and execute block processing with
|
|
|
|
## mock data.
|
|
|
|
|
2019-06-03 08:26:38 +00:00
|
|
|
# Genesis state that results in 2 members per committee
|
2019-03-13 22:59:20 +00:00
|
|
|
let
|
2019-02-28 21:21:29 +00:00
|
|
|
genState = get_genesis_beacon_state(
|
2019-06-03 08:26:38 +00:00
|
|
|
makeInitialDeposits(SLOTS_PER_EPOCH * 2, {skipValidation}), 0, Eth1Data(),
|
2019-02-28 21:21:29 +00:00
|
|
|
{skipValidation})
|
|
|
|
genBlock = get_initial_beacon_block(genState)
|
|
|
|
|
2019-05-27 12:48:13 +00:00
|
|
|
test "Can add and retrieve simple attestation" & preset():
|
2019-06-24 09:21:56 +00:00
|
|
|
var cache = get_empty_per_epoch_cache()
|
2019-06-03 08:26:38 +00:00
|
|
|
withPool:
|
|
|
|
let
|
|
|
|
# Create an attestation for slot 1!
|
2019-06-24 09:21:56 +00:00
|
|
|
crosslink_committee = get_crosslink_committee(state.data.data,
|
|
|
|
slot_to_epoch(state.data.data.slot), 0, cache)
|
2019-06-03 08:26:38 +00:00
|
|
|
attestation = makeAttestation(
|
2019-06-24 09:21:56 +00:00
|
|
|
state.data.data, state.blck.root, crosslink_committee[0])
|
2019-02-19 23:35:02 +00:00
|
|
|
|
2019-06-03 08:26:38 +00:00
|
|
|
pool.add(state.data.data, attestation)
|
2019-02-19 23:35:02 +00:00
|
|
|
|
2019-06-03 08:26:38 +00:00
|
|
|
for i in 0..<MIN_ATTESTATION_INCLUSION_DELAY.int - 1:
|
|
|
|
advanceState(state.data)
|
2019-02-19 23:35:02 +00:00
|
|
|
|
2019-06-03 08:26:38 +00:00
|
|
|
let attestations = pool.getAttestationsForBlock(
|
|
|
|
state.data.data, state.data.data.slot + 1)
|
2019-02-19 23:35:02 +00:00
|
|
|
|
2019-06-03 08:26:38 +00:00
|
|
|
check:
|
|
|
|
attestations.len == 1
|
2019-02-21 04:42:17 +00:00
|
|
|
|
2019-05-27 12:48:13 +00:00
|
|
|
test "Attestations may arrive in any order" & preset():
|
2019-06-24 09:21:56 +00:00
|
|
|
var cache = get_empty_per_epoch_cache()
|
2019-06-03 08:26:38 +00:00
|
|
|
withPool:
|
|
|
|
let
|
|
|
|
# Create an attestation for slot 1!
|
2019-06-24 09:21:56 +00:00
|
|
|
cc0 = get_crosslink_committee(state.data.data,
|
|
|
|
slot_to_epoch(state.data.data.slot), 0, cache)
|
2019-06-03 08:26:38 +00:00
|
|
|
attestation0 = makeAttestation(
|
2019-06-24 09:21:56 +00:00
|
|
|
state.data.data, state.blck.root, cc0[0])
|
2019-06-03 08:26:38 +00:00
|
|
|
|
|
|
|
advanceState(state.data)
|
|
|
|
|
|
|
|
let
|
2019-06-24 09:21:56 +00:00
|
|
|
cc1 = get_crosslink_committee(state.data.data,
|
|
|
|
slot_to_epoch(state.data.data.slot), 0, cache)
|
2019-06-03 08:26:38 +00:00
|
|
|
attestation1 = makeAttestation(
|
2019-06-24 09:21:56 +00:00
|
|
|
state.data.data, state.blck.root, cc1[0])
|
2019-06-03 08:26:38 +00:00
|
|
|
|
|
|
|
# test reverse order
|
|
|
|
pool.add(state.data.data, attestation1)
|
|
|
|
pool.add(state.data.data, attestation0)
|
|
|
|
|
|
|
|
for i in 0..<MIN_ATTESTATION_INCLUSION_DELAY.int - 1: advanceState(state.data)
|
|
|
|
|
|
|
|
let attestations = pool.getAttestationsForBlock(
|
|
|
|
state.data.data, state.data.data.slot + 1)
|
|
|
|
|
|
|
|
check:
|
|
|
|
attestations.len == 1
|
|
|
|
|
|
|
|
test "Attestations should be combined" & preset():
|
2019-06-24 09:21:56 +00:00
|
|
|
var cache = get_empty_per_epoch_cache()
|
2019-06-03 08:26:38 +00:00
|
|
|
withPool:
|
|
|
|
let
|
|
|
|
# Create an attestation for slot 1!
|
2019-06-24 09:21:56 +00:00
|
|
|
cc0 = get_crosslink_committee(state.data.data,
|
|
|
|
slot_to_epoch(state.data.data.slot), 0, cache)
|
2019-06-03 08:26:38 +00:00
|
|
|
attestation0 = makeAttestation(
|
2019-06-24 09:21:56 +00:00
|
|
|
state.data.data, state.blck.root, cc0[0])
|
2019-06-03 08:26:38 +00:00
|
|
|
attestation1 = makeAttestation(
|
2019-06-24 09:21:56 +00:00
|
|
|
state.data.data, state.blck.root, cc0[1])
|
2019-06-03 08:26:38 +00:00
|
|
|
|
|
|
|
pool.add(state.data.data, attestation0)
|
|
|
|
pool.add(state.data.data, attestation1)
|
|
|
|
|
|
|
|
for i in 0..<MIN_ATTESTATION_INCLUSION_DELAY.int - 1: advanceState(state.data)
|
|
|
|
|
|
|
|
let attestations = pool.getAttestationsForBlock(
|
|
|
|
state.data.data, state.data.data.slot + 1)
|
|
|
|
|
|
|
|
check:
|
|
|
|
attestations.len == 1
|
|
|
|
|
|
|
|
test "Attestations may overlap, bigger first" & preset():
|
2019-06-24 09:21:56 +00:00
|
|
|
var cache = get_empty_per_epoch_cache()
|
2019-06-03 08:26:38 +00:00
|
|
|
withPool:
|
|
|
|
|
|
|
|
var
|
|
|
|
# Create an attestation for slot 1!
|
2019-06-24 09:21:56 +00:00
|
|
|
cc0 = get_crosslink_committee(state.data.data,
|
|
|
|
slot_to_epoch(state.data.data.slot), 0, cache)
|
2019-06-03 08:26:38 +00:00
|
|
|
attestation0 = makeAttestation(
|
2019-06-24 09:21:56 +00:00
|
|
|
state.data.data, state.blck.root, cc0[0])
|
2019-06-03 08:26:38 +00:00
|
|
|
attestation1 = makeAttestation(
|
2019-06-24 09:21:56 +00:00
|
|
|
state.data.data, state.blck.root, cc0[1])
|
2019-06-03 08:26:38 +00:00
|
|
|
|
|
|
|
attestation0.combine(attestation1, {skipValidation})
|
|
|
|
|
|
|
|
pool.add(state.data.data, attestation0)
|
|
|
|
pool.add(state.data.data, attestation1)
|
|
|
|
|
|
|
|
for i in 0..<MIN_ATTESTATION_INCLUSION_DELAY.int - 1: advanceState(state.data)
|
|
|
|
|
|
|
|
let attestations = pool.getAttestationsForBlock(
|
|
|
|
state.data.data, state.data.data.slot + 1)
|
|
|
|
|
|
|
|
check:
|
|
|
|
attestations.len == 1
|
|
|
|
|
|
|
|
test "Attestations may overlap, smaller first" & preset():
|
2019-06-24 09:21:56 +00:00
|
|
|
var cache = get_empty_per_epoch_cache()
|
2019-06-03 08:26:38 +00:00
|
|
|
withPool:
|
|
|
|
var
|
|
|
|
# Create an attestation for slot 1!
|
2019-06-24 09:21:56 +00:00
|
|
|
cc0 = get_crosslink_committee(state.data.data,
|
|
|
|
slot_to_epoch(state.data.data.slot), 0, cache)
|
2019-06-03 08:26:38 +00:00
|
|
|
attestation0 = makeAttestation(
|
2019-06-24 09:21:56 +00:00
|
|
|
state.data.data, state.blck.root, cc0[0])
|
2019-06-03 08:26:38 +00:00
|
|
|
attestation1 = makeAttestation(
|
2019-06-24 09:21:56 +00:00
|
|
|
state.data.data, state.blck.root, cc0[1])
|
2019-06-03 08:26:38 +00:00
|
|
|
|
|
|
|
attestation0.combine(attestation1, {skipValidation})
|
|
|
|
|
|
|
|
pool.add(state.data.data, attestation1)
|
|
|
|
pool.add(state.data.data, attestation0)
|
|
|
|
|
|
|
|
for i in 0..<MIN_ATTESTATION_INCLUSION_DELAY.int - 1: advanceState(state.data)
|
|
|
|
|
|
|
|
let attestations = pool.getAttestationsForBlock(
|
|
|
|
state.data.data, state.data.data.slot + 1)
|
|
|
|
|
|
|
|
check:
|
|
|
|
attestations.len == 1
|