Skip to content

Instantly share code, notes, and snippets.

@mratsim
Created April 29, 2019 08:35
Show Gist options
  • Select an option

  • Save mratsim/b2d76d52e058e6725f3107f8dcd3b9c6 to your computer and use it in GitHub Desktop.

Select an option

Save mratsim/b2d76d52e058e6725f3107f8dcd3b9c6 to your computer and use it in GitHub Desktop.
# 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
unittest,
./testutil,
../beacon_chain/[extras, beacon_node_types, fork_choice, attestation_pool, block_pool, state_transition],
../beacon_chain/spec/[beaconstate, datatypes, digest, validator]
# We create the following chains with a varying amount of votes
# 0, 1, 2, 3, 10, 21, ... are blockIds
#
# slot
# 0 G
# -----------------------------
# | | | |
# | | | |
# 1 0 1 2 3
# | | ----------
# | | | |
# 2 10 21 32 33
# |
# |
# 3 320
const NumValidators = 10 * SLOTS_PER_EPOCH
# committees_per_slot = committees_per_epoch div SLOTS_PER_EPOCH
# We want 3 committees per slot for testing
let genState = get_genesis_beacon_state(
makeInitialDeposits(NumValidators, {}),
0,
Eth1Data(), {}
)
# Genesis block - slot 0
let G = get_initial_beacon_block(genState)
var # Not sure why I can't use the blockPool name here - Nim bug on "previous declaration"
blocksDB = BlockPool.init(makeTestDB(genState, G))
state = blocksDB.loadTailState().data
# G0 block - slot 1
let G0 = state.mockBlock(
previous_block_root = G.state_root,
body = BeaconBlockBody()
)
# G1 block - slot 1
let G1 = state.mockBlock(
previous_block_root = G.state_root,
body = BeaconBlockBody()
)
# G2 block - slot 1
let G2 = state.mockBlock(
previous_block_root = G.state_root,
body = BeaconBlockBody()
)
# G3 block - slot 1
let G3 = state.mockBlock(
previous_block_root = G.state_root,
body = BeaconBlockBody()
)
# G10 block - slot 2
let G10 = state.mockBlock(
previous_block_root = G1.state_root,
body = BeaconBlockBody()
)
# G21 block - slot 2
let G21 = state.mockBlock(
previous_block_root = G2.state_root,
body = BeaconBlockBody()
)
# G32 block - slot 2
let G32 = state.mockBlock(
previous_block_root = G3.state_root,
body = BeaconBlockBody()
)
# G32 block - slot 2
let G33 = state.mockBlock(
previous_block_root = G3.state_root,
body = BeaconBlockBody()
)
# G320 block - slot 3
let G320 = state.mockBlock(
previous_block_root = G32.state_root,
body = BeaconBlockBody()
)
# Crosslink Committees
let
xcom1 = get_crosslink_committees_at_slot(state, GENESIS_SLOT + 1'u64)
xcom2 = get_crosslink_committees_at_slot(state, GENESIS_SLOT + 2'u64)
xcom3 = get_crosslink_committees_at_slot(state, GENESIS_SLOT + 3'u64)
suite "Fork choice test":
test "One vote for each fork":
var attPool = AttestationPool.init(blocksDB)
# Slot 0, no attestation
# Slot 1
advanceState(state)
attPool.add(
state,
mockAttestation(
state,
G0.state_root,
xcom1[0].committee[0]
)
)
echo "NumValidators: ", NumValidators
echo xcom1[0]
echo xcom1[1]
echo xcom1[2]
echo xcom2[0]
echo xcom2[1]
echo xcom2[2]
attPool.add(
state,
mockAttestation(
state,
G1.state_root,
xcom1[0].committee[1]
)
)
attPool.add(
state,
mockAttestation(
state,
G2.state_root,
xcom1[0].committee[2]
)
)
attPool.add(
state,
mockAttestation(
state,
G3.state_root,
xcom1[0].committee[3]
)
)
# Slot 2 - TODO: we assume that after shuffling we don't have collision with
# the validators from slot 1 ... (i.e. someone voting for 2 different forks)
advanceState(state)
attPool.add(
state,
mockAttestation(
state,
G10.state_root,
xcom1[0].committee[0]
)
)
attPool.add(
state,
mockAttestation(
state,
G21.state_root,
xcom1[0].committee[1]
)
)
attPool.add(
state,
mockAttestation(
state,
G32.state_root,
xcom1[0].committee[2]
)
)
attPool.add(
state,
mockAttestation(
state,
G33.state_root,
xcom1[0].committee[3]
)
)
# Slot 3 - TODO: we assume that after shuffling we don't have collision with
# the validators from slot 1&2 ... (i.e. someone voting for 2 different forks)
advanceState(state)
attPool.add(
state,
mockAttestation(
state,
G320.state_root,
xcom2[0].committee[0]
)
)
let forkChoice = lmdGhost(
attPool,
state,
blocksDB.getOrResolve(G.state_root)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment