2020-05-01 15:51:24 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2019-2020 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * 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).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
# `block_sim` is a block and attestation simulator similar to `state_sim` whose
|
|
|
|
# task is to run the beacon chain without considering the network or the
|
|
|
|
# wall clock. Functionally, it achieves the same as the distributed beacon chain
|
|
|
|
# by producing blocks and attestations as if they were created by separate
|
|
|
|
# nodes, just like a set of `beacon_node` instances would.
|
|
|
|
#
|
|
|
|
# Similar to `state_sim`, but uses the block and attestation pools along with
|
|
|
|
# a database, as if a real node was running.
|
|
|
|
|
|
|
|
import
|
|
|
|
confutils, chronicles, stats, times,
|
|
|
|
strformat,
|
|
|
|
options, random, tables,
|
|
|
|
../tests/[testblockutil],
|
2020-07-07 23:02:14 +00:00
|
|
|
../beacon_chain/spec/[beaconstate, crypto, datatypes, digest, presets,
|
|
|
|
helpers, validator, signatures, state_transition],
|
2020-05-01 15:51:24 +00:00
|
|
|
../beacon_chain/[
|
2020-07-30 19:18:17 +00:00
|
|
|
attestation_pool, beacon_node_types, beacon_chain_db,
|
2020-06-23 13:54:24 +00:00
|
|
|
interop, validator_pool],
|
2020-07-30 19:18:17 +00:00
|
|
|
../beacon_chain/block_pools/[
|
2020-07-31 14:49:06 +00:00
|
|
|
spec_cache, chain_dag, quarantine, clearance],
|
2020-05-01 15:51:24 +00:00
|
|
|
eth/db/[kvstore, kvstore_sqlite3],
|
2020-06-03 13:52:02 +00:00
|
|
|
../beacon_chain/ssz/[merkleization, ssz_serialization],
|
2020-05-01 15:51:24 +00:00
|
|
|
./simutils
|
|
|
|
|
|
|
|
type Timers = enum
|
|
|
|
tBlock = "Process non-epoch slot with block"
|
|
|
|
tEpoch = "Process epoch slot with block"
|
|
|
|
tHashBlock = "Tree-hash block"
|
|
|
|
tSignBlock = "Sign block"
|
2020-05-03 17:44:04 +00:00
|
|
|
tAttest = "Have committee attest to block"
|
|
|
|
tReplay = "Replay all produced blocks"
|
2020-05-01 15:51:24 +00:00
|
|
|
|
|
|
|
# TODO confutils is an impenetrable black box. how can a help text be added here?
|
|
|
|
cli do(slots = SLOTS_PER_EPOCH * 6,
|
2020-08-13 23:37:58 +00:00
|
|
|
validators = SLOTS_PER_EPOCH * 200, # One per shard is minimum
|
2020-05-03 17:44:04 +00:00
|
|
|
attesterRatio {.desc: "ratio of validators that attest in each round"} = 0.73,
|
|
|
|
blockRatio {.desc: "ratio of slots with blocks"} = 1.0,
|
|
|
|
replay = true):
|
2020-05-01 15:51:24 +00:00
|
|
|
let
|
|
|
|
state = loadGenesis(validators, true)
|
2020-05-22 14:21:22 +00:00
|
|
|
genesisBlock = get_initial_beacon_block(state[].data)
|
2020-05-01 15:51:24 +00:00
|
|
|
|
|
|
|
echo "Starting simulation..."
|
|
|
|
|
|
|
|
let
|
|
|
|
db = BeaconChainDB.init(kvStore SqStoreRef.init(".", "block_sim").tryGet())
|
2020-09-16 07:16:23 +00:00
|
|
|
defer: db.close()
|
2020-05-01 15:51:24 +00:00
|
|
|
|
2020-07-31 14:49:06 +00:00
|
|
|
ChainDAGRef.preInit(db, state[].data, genesisBlock)
|
2020-05-01 15:51:24 +00:00
|
|
|
|
|
|
|
var
|
2020-07-31 14:49:06 +00:00
|
|
|
chainDag = init(ChainDAGRef, defaultRuntimePreset, db)
|
|
|
|
quarantine = QuarantineRef()
|
2020-07-30 19:18:17 +00:00
|
|
|
attPool = AttestationPool.init(chainDag, quarantine)
|
2020-05-01 15:51:24 +00:00
|
|
|
timers: array[Timers, RunningStat]
|
|
|
|
attesters: RunningStat
|
2020-05-03 17:44:04 +00:00
|
|
|
r = initRand(1)
|
2020-05-01 15:51:24 +00:00
|
|
|
|
2020-07-30 19:18:17 +00:00
|
|
|
let replayState = assignClone(chainDag.headState)
|
2020-05-03 17:44:04 +00:00
|
|
|
|
|
|
|
proc handleAttestations(slot: Slot) =
|
2020-05-01 15:51:24 +00:00
|
|
|
let
|
2020-07-30 19:18:17 +00:00
|
|
|
attestationHead = chainDag.head.atSlot(slot)
|
2020-05-01 15:51:24 +00:00
|
|
|
|
2020-07-30 19:18:17 +00:00
|
|
|
chainDag.withState(chainDag.tmpState, attestationHead):
|
2020-07-27 10:59:57 +00:00
|
|
|
let committees_per_slot =
|
|
|
|
get_committee_count_per_slot(state, slot.epoch, cache)
|
2020-05-01 15:51:24 +00:00
|
|
|
|
|
|
|
for committee_index in 0'u64..<committees_per_slot:
|
|
|
|
let committee = get_beacon_committee(
|
|
|
|
state, slot, committee_index.CommitteeIndex, cache)
|
|
|
|
|
|
|
|
for index_in_committee, validatorIdx in committee:
|
2020-05-03 17:44:04 +00:00
|
|
|
if rand(r, 1.0) <= attesterRatio:
|
2020-05-01 15:51:24 +00:00
|
|
|
let
|
|
|
|
data = makeAttestationData(state, slot, committee_index, blck.root)
|
|
|
|
sig =
|
|
|
|
get_attestation_signature(state.fork,
|
|
|
|
state.genesis_validators_root,
|
|
|
|
data, hackPrivKey(state.validators[validatorIdx]))
|
|
|
|
var aggregation_bits = CommitteeValidatorsBits.init(committee.len)
|
|
|
|
aggregation_bits.setBit index_in_committee
|
|
|
|
|
2020-07-09 09:29:32 +00:00
|
|
|
attPool.addAttestation(
|
2020-05-01 15:51:24 +00:00
|
|
|
Attestation(
|
|
|
|
data: data,
|
|
|
|
aggregation_bits: aggregation_bits,
|
|
|
|
signature: sig
|
2020-08-27 07:34:12 +00:00
|
|
|
), [validatorIdx].toHashSet(), data.slot)
|
2020-05-01 15:51:24 +00:00
|
|
|
|
2020-05-03 17:44:04 +00:00
|
|
|
proc proposeBlock(slot: Slot) =
|
|
|
|
if rand(r, 1.0) > blockRatio:
|
|
|
|
return
|
|
|
|
|
2020-05-01 15:51:24 +00:00
|
|
|
let
|
2020-07-30 19:18:17 +00:00
|
|
|
head = chainDag.head
|
2020-05-01 15:51:24 +00:00
|
|
|
|
2020-07-30 19:18:17 +00:00
|
|
|
chainDag.withState(chainDag.tmpState, head.atSlot(slot)):
|
2020-05-01 15:51:24 +00:00
|
|
|
let
|
|
|
|
proposerIdx = get_beacon_proposer_index(state, cache).get()
|
|
|
|
privKey = hackPrivKey(state.validators[proposerIdx])
|
|
|
|
eth1data = get_eth1data_stub(
|
|
|
|
state.eth1_deposit_index, slot.compute_epoch_at_slot())
|
|
|
|
message = makeBeaconBlock(
|
2020-07-07 23:02:14 +00:00
|
|
|
defaultRuntimePreset,
|
2020-05-22 14:21:22 +00:00
|
|
|
hashedState,
|
|
|
|
proposerIdx,
|
2020-05-01 15:51:24 +00:00
|
|
|
head.root,
|
|
|
|
privKey.genRandaoReveal(state.fork, state.genesis_validators_root, slot),
|
|
|
|
eth1data,
|
2020-06-29 17:30:19 +00:00
|
|
|
default(GraffitiBytes),
|
2020-09-14 14:50:03 +00:00
|
|
|
attPool.getAttestationsForBlock(state, cache),
|
2020-05-22 14:21:22 +00:00
|
|
|
@[],
|
2020-06-04 12:03:16 +00:00
|
|
|
noRollback,
|
|
|
|
cache)
|
2020-05-01 15:51:24 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
newBlock = SignedBeaconBlock(
|
|
|
|
message: message.get()
|
|
|
|
)
|
|
|
|
|
|
|
|
let blockRoot = withTimerRet(timers[tHashBlock]):
|
|
|
|
hash_tree_root(newBlock.message)
|
2020-07-16 13:16:51 +00:00
|
|
|
newBlock.root = blockRoot
|
2020-05-01 15:51:24 +00:00
|
|
|
# Careful, state no longer valid after here because of the await..
|
|
|
|
newBlock.signature = withTimerRet(timers[tSignBlock]):
|
|
|
|
get_block_signature(
|
|
|
|
state.fork, state.genesis_validators_root, newBlock.message.slot,
|
|
|
|
blockRoot, privKey)
|
|
|
|
|
2020-07-30 19:18:17 +00:00
|
|
|
let added = chainDag.addRawBlock(quarantine, newBlock) do (
|
2020-07-22 09:42:55 +00:00
|
|
|
blckRef: BlockRef, signedBlock: SignedBeaconBlock,
|
2020-08-18 20:29:33 +00:00
|
|
|
epochRef: EpochRef, state: HashedBeaconState):
|
2020-07-22 09:42:55 +00:00
|
|
|
# Callback add to fork choice if valid
|
2020-08-03 18:39:43 +00:00
|
|
|
attPool.addForkChoice(epochRef, blckRef, signedBlock.message, blckRef.slot)
|
2020-07-22 09:42:55 +00:00
|
|
|
|
2020-07-09 09:29:32 +00:00
|
|
|
blck() = added[]
|
2020-08-31 09:00:38 +00:00
|
|
|
chainDag.updateHead(added[], quarantine)
|
2020-05-01 15:51:24 +00:00
|
|
|
|
|
|
|
for i in 0..<slots:
|
|
|
|
let
|
2020-05-03 17:44:04 +00:00
|
|
|
slot = Slot(i + 1)
|
2020-05-01 15:51:24 +00:00
|
|
|
t =
|
2020-05-03 17:44:04 +00:00
|
|
|
if slot.isEpoch: tEpoch
|
2020-05-01 15:51:24 +00:00
|
|
|
else: tBlock
|
|
|
|
|
2020-05-03 17:44:04 +00:00
|
|
|
if blockRatio > 0.0:
|
|
|
|
withTimer(timers[t]):
|
|
|
|
proposeBlock(slot)
|
2020-05-01 15:51:24 +00:00
|
|
|
if attesterRatio > 0.0:
|
|
|
|
withTimer(timers[tAttest]):
|
2020-05-03 17:44:04 +00:00
|
|
|
handleAttestations(slot)
|
2020-05-01 15:51:24 +00:00
|
|
|
|
2020-05-03 17:44:04 +00:00
|
|
|
# TODO if attestation pool was smarter, it would include older attestations
|
|
|
|
# too!
|
2020-07-30 19:18:17 +00:00
|
|
|
verifyConsensus(chainDag.headState.data.data, attesterRatio * blockRatio)
|
2020-05-01 15:51:24 +00:00
|
|
|
|
|
|
|
if t == tEpoch:
|
|
|
|
echo &". slot: {shortLog(slot)} ",
|
|
|
|
&"epoch: {shortLog(slot.compute_epoch_at_slot)}"
|
|
|
|
else:
|
|
|
|
write(stdout, ".")
|
|
|
|
flushFile(stdout)
|
|
|
|
|
2020-05-03 17:44:04 +00:00
|
|
|
if replay:
|
|
|
|
withTimer(timers[tReplay]):
|
2020-08-18 20:29:33 +00:00
|
|
|
var cache = StateCache()
|
2020-07-30 19:18:17 +00:00
|
|
|
chainDag.updateStateData(
|
2020-08-18 20:29:33 +00:00
|
|
|
replayState[], chainDag.head.atSlot(Slot(slots)), cache)
|
2020-05-03 17:44:04 +00:00
|
|
|
|
2020-05-01 15:51:24 +00:00
|
|
|
echo "Done!"
|
|
|
|
|
2020-07-30 19:18:17 +00:00
|
|
|
printTimers(chainDag.headState.data.data, attesters, true, timers)
|