2019-12-23 12:39:27 +00:00
|
|
|
# beacon_chain
|
2020-01-28 21:24:45 +00:00
|
|
|
# Copyright (c) 2019-2020 Status Research & Development GmbH
|
2019-12-23 12:39:27 +00:00
|
|
|
# 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.
|
|
|
|
|
2020-05-01 15:51:24 +00:00
|
|
|
# `state_sim` runs the state transition function in isolation, creating blocks
|
|
|
|
# and attesting to them as if the network was running as a whole.
|
|
|
|
|
2018-12-21 22:37:46 +00:00
|
|
|
import
|
2020-05-01 15:51:24 +00:00
|
|
|
confutils, stats, times,
|
2019-07-03 07:35:05 +00:00
|
|
|
strformat,
|
2019-06-29 09:17:24 +00:00
|
|
|
options, sequtils, random, tables,
|
2020-01-22 15:36:16 +00:00
|
|
|
../tests/[testblockutil],
|
2019-01-26 19:32:10 +00:00
|
|
|
../beacon_chain/spec/[beaconstate, crypto, datatypes, digest, helpers, validator],
|
2020-05-01 15:51:24 +00:00
|
|
|
../beacon_chain/[attestation_pool, extras, ssz],
|
|
|
|
./simutils
|
2018-12-21 22:37:46 +00:00
|
|
|
|
2019-03-04 13:04:26 +00:00
|
|
|
type Timers = enum
|
|
|
|
tBlock = "Process non-epoch slot with block"
|
2019-03-23 04:55:41 +00:00
|
|
|
tEpoch = "Process epoch slot with block"
|
2019-03-04 13:04:26 +00:00
|
|
|
tHashBlock = "Tree-hash block"
|
2019-11-25 06:31:37 +00:00
|
|
|
tShuffle = "Retrieve committee once using get_beacon_committee"
|
2019-03-04 13:04:26 +00:00
|
|
|
tAttest = "Combine committee attestations"
|
|
|
|
|
2020-01-22 15:36:16 +00:00
|
|
|
proc jsonName(prefix, slot: auto): string =
|
|
|
|
fmt"{prefix:04}-{shortLog(slot):08}.json"
|
|
|
|
|
|
|
|
proc writeJson*(fn, v: auto) =
|
2018-12-21 22:37:46 +00:00
|
|
|
var f: File
|
|
|
|
defer: close(f)
|
2020-01-22 15:36:16 +00:00
|
|
|
Json.saveFile(fn, v, pretty = true)
|
2018-12-21 22:37:46 +00:00
|
|
|
|
2019-11-15 22:37:39 +00:00
|
|
|
cli do(slots = SLOTS_PER_EPOCH * 6,
|
2020-04-29 11:44:07 +00:00
|
|
|
validators = SLOTS_PER_EPOCH * 100, # One per shard is minimum
|
2019-02-20 01:33:58 +00:00
|
|
|
json_interval = SLOTS_PER_EPOCH,
|
2020-01-22 15:36:16 +00:00
|
|
|
write_last_json = false,
|
2019-01-23 18:45:15 +00:00
|
|
|
prefix = 0,
|
2019-11-27 22:48:12 +00:00
|
|
|
attesterRatio {.desc: "ratio of validators that attest in each round"} = 0.73,
|
2019-04-05 14:18:13 +00:00
|
|
|
validate = true):
|
2018-12-21 22:37:46 +00:00
|
|
|
let
|
2020-03-05 12:52:10 +00:00
|
|
|
flags = if validate: {} else: {skipBlsValidation}
|
2020-05-01 15:51:24 +00:00
|
|
|
state = loadGenesis(validators, validate)
|
2020-05-22 14:21:22 +00:00
|
|
|
genesisBlock = get_initial_beacon_block(state.data)
|
2018-12-21 22:37:46 +00:00
|
|
|
|
2020-01-22 15:36:16 +00:00
|
|
|
echo "Starting simulation..."
|
|
|
|
|
2018-12-21 22:37:46 +00:00
|
|
|
var
|
2019-06-29 09:17:24 +00:00
|
|
|
attestations = initTable[Slot, seq[Attestation]]()
|
2019-12-16 18:08:50 +00:00
|
|
|
latest_block_root = hash_tree_root(genesisBlock.message)
|
2019-03-04 13:04:26 +00:00
|
|
|
timers: array[Timers, RunningStat]
|
|
|
|
attesters: RunningStat
|
2020-05-03 17:44:04 +00:00
|
|
|
r = initRand(1)
|
2020-02-29 15:15:44 +00:00
|
|
|
signedBlock: SignedBeaconBlock
|
2019-06-24 09:21:56 +00:00
|
|
|
cache = get_empty_per_epoch_cache()
|
2018-12-21 22:37:46 +00:00
|
|
|
|
2020-01-22 15:36:16 +00:00
|
|
|
proc maybeWrite(last: bool) =
|
|
|
|
if write_last_json:
|
2020-05-22 14:21:22 +00:00
|
|
|
if state[].data.slot mod json_interval.uint64 == 0:
|
2020-01-22 15:36:16 +00:00
|
|
|
write(stdout, ":")
|
|
|
|
else:
|
|
|
|
write(stdout, ".")
|
|
|
|
|
|
|
|
if last:
|
2020-04-26 19:13:33 +00:00
|
|
|
writeJson("state.json", state[])
|
2018-12-21 22:37:46 +00:00
|
|
|
else:
|
2020-05-22 14:21:22 +00:00
|
|
|
if state[].data.slot mod json_interval.uint64 == 0:
|
|
|
|
writeJson(jsonName(prefix, state[].data.slot), state[].data)
|
2020-01-22 15:36:16 +00:00
|
|
|
write(stdout, ":")
|
|
|
|
else:
|
|
|
|
write(stdout, ".")
|
2018-12-21 22:37:46 +00:00
|
|
|
|
2019-11-27 22:48:12 +00:00
|
|
|
# TODO doAssert against this up-front
|
|
|
|
# indexed attestation: validator index beyond max validators per committee
|
|
|
|
# len(indices) <= MAX_VALIDATORS_PER_COMMITTEE
|
|
|
|
|
2019-03-04 13:04:26 +00:00
|
|
|
for i in 0..<slots:
|
2020-01-22 15:36:16 +00:00
|
|
|
maybeWrite(false)
|
2020-05-22 14:21:22 +00:00
|
|
|
verifyConsensus(state[].data, attesterRatio)
|
2019-03-04 13:04:26 +00:00
|
|
|
|
2018-12-27 23:40:22 +00:00
|
|
|
let
|
2020-05-22 14:21:22 +00:00
|
|
|
attestations_idx = state[].data.slot
|
2020-03-19 23:48:03 +00:00
|
|
|
blockAttestations = attestations.getOrDefault(attestations_idx)
|
2018-12-27 23:40:22 +00:00
|
|
|
|
2019-06-29 09:17:24 +00:00
|
|
|
attestations.del attestations_idx
|
|
|
|
doAssert len(attestations) <=
|
|
|
|
(SLOTS_PER_EPOCH.int + MIN_ATTESTATION_INCLUSION_DELAY.int)
|
2018-12-27 23:40:22 +00:00
|
|
|
|
2019-03-04 13:04:26 +00:00
|
|
|
let t =
|
2020-05-22 14:21:22 +00:00
|
|
|
if (state[].data.slot > GENESIS_SLOT and
|
|
|
|
(state[].data.slot + 1).isEpoch): tEpoch
|
2019-03-04 13:04:26 +00:00
|
|
|
else: tBlock
|
|
|
|
|
|
|
|
withTimer(timers[t]):
|
2020-03-19 23:48:03 +00:00
|
|
|
signedBlock = addTestBlock(
|
2020-04-23 18:58:54 +00:00
|
|
|
state[], latest_block_root, attestations = blockAttestations, flags = flags)
|
2019-03-04 13:04:26 +00:00
|
|
|
latest_block_root = withTimerRet(timers[tHashBlock]):
|
2020-02-29 15:15:44 +00:00
|
|
|
hash_tree_root(signedBlock.message)
|
2018-12-27 23:40:22 +00:00
|
|
|
|
|
|
|
if attesterRatio > 0.0:
|
|
|
|
# attesterRatio is the fraction of attesters that actually do their
|
2019-03-11 15:33:24 +00:00
|
|
|
# work for every slot - we'll randomize it deterministically to give
|
2018-12-27 23:40:22 +00:00
|
|
|
# some variation
|
2019-06-24 09:21:56 +00:00
|
|
|
let
|
2020-05-22 14:21:22 +00:00
|
|
|
target_slot = state[].data.slot + MIN_ATTESTATION_INCLUSION_DELAY - 1
|
2019-06-24 09:21:56 +00:00
|
|
|
scass = withTimerRet(timers[tShuffle]):
|
|
|
|
mapIt(
|
2020-05-22 14:21:22 +00:00
|
|
|
0'u64 ..< get_committee_count_at_slot(state[].data, target_slot),
|
|
|
|
get_beacon_committee(state[].data, target_slot, it.CommitteeIndex, cache))
|
2018-12-27 23:40:22 +00:00
|
|
|
|
2019-11-27 22:48:12 +00:00
|
|
|
for i, scas in scass:
|
2018-12-27 23:40:22 +00:00
|
|
|
var
|
|
|
|
attestation: Attestation
|
|
|
|
first = true
|
|
|
|
|
2019-06-24 09:21:56 +00:00
|
|
|
attesters.push scas.len()
|
2019-03-04 13:04:26 +00:00
|
|
|
|
|
|
|
withTimer(timers[tAttest]):
|
2019-06-24 09:21:56 +00:00
|
|
|
for v in scas:
|
2019-03-04 13:04:26 +00:00
|
|
|
if (rand(r, high(int)).float * attesterRatio).int <= high(int):
|
|
|
|
if first:
|
2019-11-15 22:37:39 +00:00
|
|
|
attestation =
|
2020-05-22 14:21:22 +00:00
|
|
|
makeAttestation(state[].data, latest_block_root, scas, target_slot,
|
2019-11-27 22:48:12 +00:00
|
|
|
i.uint64, v, cache, flags)
|
2019-03-04 13:04:26 +00:00
|
|
|
first = false
|
|
|
|
else:
|
|
|
|
attestation.combine(
|
2020-05-22 14:21:22 +00:00
|
|
|
makeAttestation(state[].data, latest_block_root, scas, target_slot,
|
2019-11-27 22:48:12 +00:00
|
|
|
i.uint64, v, cache, flags),
|
2019-11-15 22:37:39 +00:00
|
|
|
flags)
|
2018-12-27 23:40:22 +00:00
|
|
|
|
|
|
|
if not first:
|
|
|
|
# add the attestation if any of the validators attested, as given
|
|
|
|
# by the randomness. We have to delay when the attestation is
|
|
|
|
# actually added to the block per the attestation delay rule!
|
2019-06-29 09:17:24 +00:00
|
|
|
let target_slot =
|
2019-11-12 05:35:52 +00:00
|
|
|
attestation.data.slot + MIN_ATTESTATION_INCLUSION_DELAY - 1
|
2019-06-29 09:17:24 +00:00
|
|
|
|
2019-11-27 22:48:12 +00:00
|
|
|
doAssert target_slot > attestations_idx
|
|
|
|
var target_slot_attestations =
|
|
|
|
getOrDefault(attestations, target_slot)
|
|
|
|
target_slot_attestations.add attestation
|
|
|
|
attestations[target_slot] = target_slot_attestations
|
2018-12-21 22:37:46 +00:00
|
|
|
|
|
|
|
flushFile(stdout)
|
|
|
|
|
2020-05-22 14:21:22 +00:00
|
|
|
if (state[].data.slot) mod SLOTS_PER_EPOCH == 0:
|
|
|
|
echo &" slot: {shortLog(state[].data.slot)} ",
|
|
|
|
&"epoch: {shortLog(state[].data.slot.compute_epoch_at_slot)}"
|
2019-03-04 13:04:26 +00:00
|
|
|
|
|
|
|
|
2020-01-22 15:36:16 +00:00
|
|
|
maybeWrite(true) # catch that last state as well..
|
|
|
|
|
|
|
|
echo "Done!"
|
2018-12-21 22:37:46 +00:00
|
|
|
|
2020-05-22 14:21:22 +00:00
|
|
|
printTimers(state[].data, attesters, validate, timers)
|