2018-12-21 22:37:46 +00:00
|
|
|
import
|
2019-03-04 13:04:26 +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,
|
2018-12-21 22:37:46 +00:00
|
|
|
../tests/[testutil],
|
2019-01-26 19:32:10 +00:00
|
|
|
../beacon_chain/spec/[beaconstate, crypto, datatypes, digest, helpers, validator],
|
2019-02-19 23:35:02 +00:00
|
|
|
../beacon_chain/[attestation_pool, extras, ssz, state_transition, fork_choice]
|
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-06-24 09:21:56 +00:00
|
|
|
tShuffle = "Retrieve committee once using get_crosslink_committee"
|
2019-03-04 13:04:26 +00:00
|
|
|
tAttest = "Combine committee attestations"
|
|
|
|
|
|
|
|
template withTimer(stats: var RunningStat, body: untyped) =
|
|
|
|
let start = cpuTime()
|
|
|
|
|
|
|
|
block:
|
|
|
|
body
|
|
|
|
|
|
|
|
let stop = cpuTime()
|
|
|
|
stats.push stop - start
|
|
|
|
|
|
|
|
template withTimerRet(stats: var RunningStat, body: untyped): untyped =
|
|
|
|
let start = cpuTime()
|
|
|
|
let tmp = block:
|
|
|
|
body
|
|
|
|
let stop = cpuTime()
|
|
|
|
stats.push stop - start
|
|
|
|
|
|
|
|
tmp
|
|
|
|
|
2018-12-21 22:37:46 +00:00
|
|
|
proc writeJson*(prefix, slot, v: auto) =
|
|
|
|
var f: File
|
|
|
|
defer: close(f)
|
2019-08-15 16:01:55 +00:00
|
|
|
let fileName = fmt"{prefix:04}-{shortLog(slot):08}.json"
|
2019-07-03 07:35:05 +00:00
|
|
|
Json.saveFile(fileName, v, pretty = true)
|
2018-12-21 22:37:46 +00:00
|
|
|
|
2019-06-29 09:17:24 +00:00
|
|
|
cli do(slots = 448,
|
|
|
|
validators = SLOTS_PER_EPOCH * 9, # One per shard is minimum
|
2019-02-20 01:33:58 +00:00
|
|
|
json_interval = SLOTS_PER_EPOCH,
|
2019-01-23 18:45:15 +00:00
|
|
|
prefix = 0,
|
2019-03-08 16:40:17 +00:00
|
|
|
attesterRatio {.desc: "ratio of validators that attest in each round"} = 0.9,
|
2019-04-05 14:18:13 +00:00
|
|
|
validate = true):
|
2018-12-21 22:37:46 +00:00
|
|
|
let
|
2018-12-27 23:40:22 +00:00
|
|
|
flags = if validate: {} else: {skipValidation}
|
2019-07-10 12:23:02 +00:00
|
|
|
genesisState = initialize_beacon_state_from_eth1(
|
2019-09-02 10:31:14 +00:00
|
|
|
Eth2Digest(), 0,
|
|
|
|
makeInitialDeposits(validators, flags), flags)
|
2019-02-21 04:42:17 +00:00
|
|
|
genesisBlock = get_initial_beacon_block(genesisState)
|
2018-12-21 22:37:46 +00:00
|
|
|
|
|
|
|
var
|
2019-06-29 09:17:24 +00:00
|
|
|
attestations = initTable[Slot, seq[Attestation]]()
|
2018-12-21 22:37:46 +00:00
|
|
|
state = genesisState
|
2019-05-09 12:27:37 +00:00
|
|
|
latest_block_root = signing_root(genesisBlock)
|
2019-03-04 13:04:26 +00:00
|
|
|
timers: array[Timers, RunningStat]
|
|
|
|
attesters: RunningStat
|
|
|
|
r: Rand
|
|
|
|
blck: BeaconBlock
|
2019-06-24 09:21:56 +00:00
|
|
|
cache = get_empty_per_epoch_cache()
|
2018-12-21 22:37:46 +00:00
|
|
|
|
2019-03-04 13:04:26 +00:00
|
|
|
proc maybeWrite() =
|
2018-12-21 22:37:46 +00:00
|
|
|
if state.slot mod json_interval.uint64 == 0:
|
|
|
|
writeJson(prefix, state.slot, state)
|
|
|
|
write(stdout, ":")
|
|
|
|
else:
|
|
|
|
write(stdout, ".")
|
|
|
|
|
2019-03-04 13:04:26 +00:00
|
|
|
for i in 0..<slots:
|
|
|
|
maybeWrite()
|
|
|
|
|
2018-12-27 23:40:22 +00:00
|
|
|
let
|
2019-06-29 09:17:24 +00:00
|
|
|
attestations_idx = state.slot
|
|
|
|
body = BeaconBlockBody(
|
|
|
|
attestations: 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 =
|
2019-03-23 04:52:45 +00:00
|
|
|
if (state.slot > GENESIS_SLOT and
|
|
|
|
(state.slot + 1) mod SLOTS_PER_EPOCH == 0): tEpoch
|
2019-03-04 13:04:26 +00:00
|
|
|
else: tBlock
|
|
|
|
|
|
|
|
withTimer(timers[t]):
|
|
|
|
blck = addBlock(state, latest_block_root, body, flags)
|
|
|
|
latest_block_root = withTimerRet(timers[tHashBlock]):
|
2019-05-09 12:27:37 +00:00
|
|
|
signing_root(blck)
|
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
|
2019-07-01 07:53:42 +00:00
|
|
|
epoch = compute_epoch_of_slot(state.slot)
|
2019-06-24 09:21:56 +00:00
|
|
|
scass = withTimerRet(timers[tShuffle]):
|
|
|
|
mapIt(
|
2019-07-02 14:31:48 +00:00
|
|
|
0'u64 .. (get_committee_count(state, epoch) - 1),
|
2019-06-24 09:21:56 +00:00
|
|
|
get_crosslink_committee(state, epoch,
|
2019-07-01 08:05:46 +00:00
|
|
|
(it + get_start_shard(state, epoch)) mod SHARD_COUNT,
|
2019-06-24 09:21:56 +00:00
|
|
|
cache))
|
2018-12-27 23:40:22 +00:00
|
|
|
|
|
|
|
for scas in scass:
|
|
|
|
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-03-07 13:59:28 +00:00
|
|
|
attestation = makeAttestation(state, latest_block_root, v, flags)
|
2019-03-04 13:04:26 +00:00
|
|
|
first = false
|
|
|
|
else:
|
|
|
|
attestation.combine(
|
2019-03-07 13:59:28 +00:00
|
|
|
makeAttestation(state, latest_block_root, v, flags), 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 =
|
|
|
|
get_attestation_data_slot(state, attestation.data) +
|
|
|
|
MIN_ATTESTATION_INCLUSION_DELAY - 1
|
|
|
|
|
|
|
|
## In principle, should enumerate possible shard/slot combinations by
|
|
|
|
## inverting get_attestation_data_slot(...), but this works. Could be
|
|
|
|
## filtering earlier if we know that this attestation's being created
|
|
|
|
## too late to be useful, as well.
|
|
|
|
if 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)
|
|
|
|
|
2019-03-04 13:04:26 +00:00
|
|
|
if (state.slot) mod SLOTS_PER_EPOCH == 0:
|
2019-08-15 16:01:55 +00:00
|
|
|
echo &" slot: {shortLog(state.slot)} ",
|
|
|
|
&"epoch: {shortLog(state.slot.compute_epoch_of_slot)}"
|
2019-03-04 13:04:26 +00:00
|
|
|
|
|
|
|
maybeWrite() # catch that last state as well..
|
|
|
|
|
2018-12-21 22:37:46 +00:00
|
|
|
echo "done!"
|
|
|
|
|
2019-03-04 13:04:26 +00:00
|
|
|
echo "Validators: ", validators, ", epoch length: ", SLOTS_PER_EPOCH
|
|
|
|
echo "Validators per attestation (mean): ", attesters.mean
|
|
|
|
|
|
|
|
proc fmtTime(t: float): string = &"{t * 1000 :>12.3f}, "
|
|
|
|
|
|
|
|
echo "All time are ms"
|
|
|
|
echo &"{\"Average\" :>12}, {\"StdDev\" :>12}, {\"Min\" :>12}, " &
|
|
|
|
&"{\"Max\" :>12}, {\"Samples\" :>12}, {\"Test\" :>12}"
|
2019-03-07 13:59:28 +00:00
|
|
|
|
|
|
|
if not validate:
|
|
|
|
echo "Validation is turned off meaning that no BLS operations are performed"
|
|
|
|
|
2019-03-04 13:04:26 +00:00
|
|
|
for t in Timers:
|
|
|
|
echo fmtTime(timers[t].mean), fmtTime(timers[t].standardDeviationS),
|
|
|
|
fmtTime(timers[t].min), fmtTime(timers[t].max), &"{timers[t].n :>12}, ",
|
|
|
|
$t
|