2019-02-19 23:35:02 +00:00
|
|
|
# beacon_chain
|
2020-03-11 09:26:18 +00:00
|
|
|
# Copyright (c) 2018-2020 Status Research & Development GmbH
|
2019-02-19 23:35:02 +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-02-19 23:35:02 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2019-11-14 10:47:55 +00:00
|
|
|
{.used.}
|
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
import
|
|
|
|
../beacon_chain/spec/datatypes,
|
|
|
|
../beacon_chain/ssz
|
|
|
|
|
2019-02-19 23:35:02 +00:00
|
|
|
import
|
2020-04-29 11:44:07 +00:00
|
|
|
unittest,
|
|
|
|
chronicles,
|
|
|
|
stew/byteutils,
|
|
|
|
./testutil, ./testblockutil,
|
2020-06-10 06:58:12 +00:00
|
|
|
../beacon_chain/spec/[digest, validator],
|
|
|
|
../beacon_chain/[beacon_node_types, attestation_pool, block_pool, state_transition]
|
2020-04-29 11:44:07 +00:00
|
|
|
|
|
|
|
suiteReport "Attestation pool processing" & preset():
|
|
|
|
## For now just test that we can compile and execute block processing with
|
|
|
|
## mock data.
|
2019-12-19 14:13:35 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
setup:
|
|
|
|
# Genesis state that results in 3 members per committee
|
|
|
|
var
|
2020-06-10 06:58:12 +00:00
|
|
|
blockPool = newClone(BlockPool.init(makeTestDB(SLOTS_PER_EPOCH * 3)))
|
|
|
|
pool = newClone(AttestationPool.init(blockPool[]))
|
|
|
|
state = newClone(loadTailState(blockPool[]))
|
2020-04-29 11:44:07 +00:00
|
|
|
# Slot 0 is a finalized slot - won't be making attestations for it..
|
2020-05-19 15:46:29 +00:00
|
|
|
check:
|
|
|
|
process_slots(state.data, state.data.data.slot + 1)
|
2020-04-29 11:44:07 +00:00
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
# pool[].add(blockPool[].tail) # Make the tail known to fork choice
|
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
timedTest "Can add and retrieve simple attestation" & preset():
|
|
|
|
var cache = get_empty_per_epoch_cache()
|
|
|
|
let
|
|
|
|
# Create an attestation for slot 1!
|
|
|
|
beacon_committee = get_beacon_committee(
|
|
|
|
state.data.data, state.data.data.slot, 0.CommitteeIndex, cache)
|
|
|
|
attestation = makeAttestation(
|
|
|
|
state.data.data, state.blck.root, beacon_committee[0], cache)
|
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
pool[].add(attestation)
|
2019-12-19 14:13:35 +00:00
|
|
|
|
2020-05-19 15:46:29 +00:00
|
|
|
check:
|
|
|
|
process_slots(state.data, MIN_ATTESTATION_INCLUSION_DELAY.Slot + 1)
|
2020-04-29 11:44:07 +00:00
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
let attestations = pool[].getAttestationsForBlock(state.data.data)
|
2019-12-19 14:13:35 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
check:
|
|
|
|
attestations.len == 1
|
|
|
|
|
|
|
|
timedTest "Attestations may arrive in any order" & preset():
|
|
|
|
var cache = get_empty_per_epoch_cache()
|
|
|
|
let
|
|
|
|
# Create an attestation for slot 1!
|
|
|
|
bc0 = get_beacon_committee(
|
|
|
|
state.data.data, state.data.data.slot, 0.CommitteeIndex, cache)
|
|
|
|
attestation0 = makeAttestation(
|
|
|
|
state.data.data, state.blck.root, bc0[0], cache)
|
|
|
|
|
2020-05-19 15:46:29 +00:00
|
|
|
check:
|
|
|
|
process_slots(state.data, state.data.data.slot + 1)
|
2020-04-29 11:44:07 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
bc1 = get_beacon_committee(state.data.data,
|
|
|
|
state.data.data.slot, 0.CommitteeIndex, cache)
|
|
|
|
attestation1 = makeAttestation(
|
|
|
|
state.data.data, state.blck.root, bc1[0], cache)
|
|
|
|
|
|
|
|
# test reverse order
|
2020-06-10 06:58:12 +00:00
|
|
|
pool[].add(attestation1)
|
|
|
|
pool[].add(attestation0)
|
2020-04-29 11:44:07 +00:00
|
|
|
|
2020-05-19 14:37:29 +00:00
|
|
|
discard process_slots(state.data, MIN_ATTESTATION_INCLUSION_DELAY.Slot + 1)
|
2020-04-29 11:44:07 +00:00
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
let attestations = pool[].getAttestationsForBlock(state.data.data)
|
2020-04-29 11:44:07 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
attestations.len == 1
|
|
|
|
|
|
|
|
timedTest "Attestations should be combined" & preset():
|
|
|
|
var cache = get_empty_per_epoch_cache()
|
|
|
|
let
|
|
|
|
# Create an attestation for slot 1!
|
|
|
|
bc0 = get_beacon_committee(
|
|
|
|
state.data.data, state.data.data.slot, 0.CommitteeIndex, cache)
|
|
|
|
attestation0 = makeAttestation(
|
|
|
|
state.data.data, state.blck.root, bc0[0], cache)
|
|
|
|
attestation1 = makeAttestation(
|
|
|
|
state.data.data, state.blck.root, bc0[1], cache)
|
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
pool[].add(attestation0)
|
|
|
|
pool[].add(attestation1)
|
2020-04-29 11:44:07 +00:00
|
|
|
|
2020-05-19 15:46:29 +00:00
|
|
|
check:
|
|
|
|
process_slots(state.data, MIN_ATTESTATION_INCLUSION_DELAY.Slot + 1)
|
2020-04-29 11:44:07 +00:00
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
let attestations = pool[].getAttestationsForBlock(state.data.data)
|
2020-04-29 11:44:07 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
attestations.len == 1
|
|
|
|
|
|
|
|
timedTest "Attestations may overlap, bigger first" & preset():
|
|
|
|
var cache = get_empty_per_epoch_cache()
|
|
|
|
|
|
|
|
var
|
|
|
|
# Create an attestation for slot 1!
|
|
|
|
bc0 = get_beacon_committee(
|
|
|
|
state.data.data, state.data.data.slot, 0.CommitteeIndex, cache)
|
|
|
|
attestation0 = makeAttestation(
|
|
|
|
state.data.data, state.blck.root, bc0[0], cache)
|
|
|
|
attestation1 = makeAttestation(
|
|
|
|
state.data.data, state.blck.root, bc0[1], cache)
|
2019-12-19 14:13:35 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
attestation0.combine(attestation1, {})
|
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
pool[].add(attestation0)
|
|
|
|
pool[].add(attestation1)
|
2019-12-19 13:02:28 +00:00
|
|
|
|
2020-05-19 15:46:29 +00:00
|
|
|
check:
|
|
|
|
process_slots(state.data, MIN_ATTESTATION_INCLUSION_DELAY.Slot + 1)
|
2019-12-19 13:02:28 +00:00
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
let attestations = pool[].getAttestationsForBlock(state.data.data)
|
2019-12-19 13:02:28 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
check:
|
|
|
|
attestations.len == 1
|
2019-12-19 13:02:28 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
timedTest "Attestations may overlap, smaller first" & preset():
|
|
|
|
var cache = get_empty_per_epoch_cache()
|
|
|
|
var
|
|
|
|
# Create an attestation for slot 1!
|
|
|
|
bc0 = get_beacon_committee(state.data.data,
|
|
|
|
state.data.data.slot, 0.CommitteeIndex, cache)
|
|
|
|
attestation0 = makeAttestation(
|
|
|
|
state.data.data, state.blck.root, bc0[0], cache)
|
|
|
|
attestation1 = makeAttestation(
|
|
|
|
state.data.data, state.blck.root, bc0[1], cache)
|
2019-12-19 13:02:28 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
attestation0.combine(attestation1, {})
|
2019-12-19 14:13:35 +00:00
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
pool[].add(attestation1)
|
|
|
|
pool[].add(attestation0)
|
2019-12-19 13:02:28 +00:00
|
|
|
|
2020-05-19 15:46:29 +00:00
|
|
|
check:
|
|
|
|
process_slots(state.data, MIN_ATTESTATION_INCLUSION_DELAY.Slot + 1)
|
2019-12-19 13:02:28 +00:00
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
let attestations = pool[].getAttestationsForBlock(state.data.data)
|
2019-12-19 13:02:28 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
check:
|
|
|
|
attestations.len == 1
|
2019-12-19 13:02:28 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
timedTest "Fork choice returns latest block with no attestations":
|
2020-06-04 12:03:16 +00:00
|
|
|
var cache = get_empty_per_epoch_cache()
|
2020-04-29 11:44:07 +00:00
|
|
|
let
|
2020-06-10 06:58:12 +00:00
|
|
|
b1 = addTestBlock(state.data, blockPool[].tail.root, cache)
|
2020-04-29 11:44:07 +00:00
|
|
|
b1Root = hash_tree_root(b1.message)
|
2020-06-10 06:58:12 +00:00
|
|
|
b1Add = blockpool[].add(b1Root, b1)[]
|
|
|
|
|
|
|
|
# pool[].add(b1Add) - make a block known to the future fork choice
|
|
|
|
let head = pool[].selectHead()
|
2019-12-19 14:13:35 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
check:
|
|
|
|
head == b1Add
|
2019-12-19 14:13:35 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
let
|
2020-06-04 12:03:16 +00:00
|
|
|
b2 = addTestBlock(state.data, b1Root, cache)
|
2020-04-29 11:44:07 +00:00
|
|
|
b2Root = hash_tree_root(b2.message)
|
2020-06-10 06:58:12 +00:00
|
|
|
b2Add = blockpool[].add(b2Root, b2)[]
|
|
|
|
|
|
|
|
# pool[].add(b2Add) - make a block known to the future fork choice
|
|
|
|
let head2 = pool[].selectHead()
|
2019-12-19 13:02:28 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
check:
|
|
|
|
head2 == b2Add
|
2019-12-19 13:02:28 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
timedTest "Fork choice returns block with attestation":
|
|
|
|
var cache = get_empty_per_epoch_cache()
|
|
|
|
let
|
2020-06-10 06:58:12 +00:00
|
|
|
b10 = makeTestBlock(state.data, blockPool[].tail.root, cache)
|
2020-04-29 11:44:07 +00:00
|
|
|
b10Root = hash_tree_root(b10.message)
|
2020-06-10 06:58:12 +00:00
|
|
|
b10Add = blockpool[].add(b10Root, b10)[]
|
|
|
|
|
|
|
|
# pool[].add(b10Add) - make a block known to the future fork choice
|
|
|
|
let head = pool[].selectHead()
|
2019-12-19 13:02:28 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
check:
|
|
|
|
head == b10Add
|
2019-12-19 13:02:28 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
let
|
2020-06-10 06:58:12 +00:00
|
|
|
b11 = makeTestBlock(state.data, blockPool[].tail.root, cache,
|
2020-04-29 11:44:07 +00:00
|
|
|
graffiti = Eth2Digest(data: [1'u8, 0, 0, 0 ,0 ,0 ,0 ,0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
|
|
|
|
)
|
|
|
|
b11Root = hash_tree_root(b11.message)
|
2020-06-10 06:58:12 +00:00
|
|
|
b11Add = blockpool[].add(b11Root, b11)[]
|
2019-12-19 13:02:28 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
bc1 = get_beacon_committee(
|
|
|
|
state.data.data, state.data.data.slot, 1.CommitteeIndex, cache)
|
|
|
|
attestation0 = makeAttestation(state.data.data, b10Root, bc1[0], cache)
|
2019-06-03 08:26:38 +00:00
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
# pool[].add(b11Add) - make a block known to the future fork choice
|
|
|
|
pool[].add(attestation0)
|
2019-06-03 08:26:38 +00:00
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
let head2 = pool[].selectHead()
|
2019-06-03 08:26:38 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
check:
|
|
|
|
# Single vote for b10 and no votes for b11
|
|
|
|
head2 == b10Add
|
2019-06-03 08:26:38 +00:00
|
|
|
|
2020-04-29 11:44:07 +00:00
|
|
|
let
|
|
|
|
attestation1 = makeAttestation(state.data.data, b11Root, bc1[1], cache)
|
|
|
|
attestation2 = makeAttestation(state.data.data, b11Root, bc1[2], cache)
|
2020-06-10 06:58:12 +00:00
|
|
|
pool[].add(attestation1)
|
2019-06-03 08:26:38 +00:00
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
let head3 = pool[].selectHead()
|
|
|
|
# Warning - the tiebreak are incorrect and guaranteed consensus fork, it should be bigger
|
2020-04-29 11:44:07 +00:00
|
|
|
let smaller = if b10Root.data < b11Root.data: b10Add else: b11Add
|
|
|
|
|
|
|
|
check:
|
2020-06-10 06:58:12 +00:00
|
|
|
# Ties broken lexicographically in spec -> ?
|
|
|
|
# all implementations favor the biggest root
|
|
|
|
# TODO
|
|
|
|
# currently using smaller as we have used for over a year
|
2020-04-29 11:44:07 +00:00
|
|
|
head3 == smaller
|
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
pool[].add(attestation2)
|
2020-04-29 11:44:07 +00:00
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
let head4 = pool[].selectHead()
|
2020-04-29 11:44:07 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
# Two votes for b11
|
|
|
|
head4 == b11Add
|