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)
|
|
|
|
genesisBlock = get_initial_beacon_block(state[])
|
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:
|
|
|
|
if state.slot mod json_interval.uint64 == 0:
|
|
|
|
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-04-26 19:13:33 +00:00
|
|
|
if state[].slot mod json_interval.uint64 == 0:
|
|
|
|
writeJson(jsonName(prefix, state.slot), state[])
|
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-04-23 18:58:54 +00:00
|
|
|
verifyConsensus(state[], attesterRatio)
|
2019-03-04 13:04:26 +00:00
|
|
|
|
2018-12-27 23:40:22 +00:00
|
|
|
let
|
2019-06-29 09:17:24 +00:00
|
|
|
attestations_idx = state.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 =
|
2019-03-23 04:52:45 +00:00
|
|
|
if (state.slot > GENESIS_SLOT and
|
2020-05-03 17:44:04 +00:00
|
|
|
(state.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
|
2019-11-27 22:48:12 +00:00
|
|
|
target_slot = state.slot + MIN_ATTESTATION_INCLUSION_DELAY - 1
|
2019-06-24 09:21:56 +00:00
|
|
|
scass = withTimerRet(timers[tShuffle]):
|
|
|
|
mapIt(
|
2020-04-23 18:58:54 +00:00
|
|
|
0'u64 ..< get_committee_count_at_slot(state[], target_slot),
|
|
|
|
get_beacon_committee(state[], 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-04-23 18:58:54 +00:00
|
|
|
makeAttestation(state[], 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-04-23 18:58:54 +00:00
|
|
|
makeAttestation(state[], 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)
|
|
|
|
|
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)} ",
|
initial 0.9.0 spec sync (#509)
* rename compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* remove some unnecessary imports; remove some crosslink-related code and tests; complete renaming of compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* rm more transfer-related code and tests; rm more unnecessary strutils imports
* rm remaining unused imports
* remove useless get_empty_per_epoch_cache(...)/compute_start_slot_of_epoch(...) calls
* rename compute_start_slot_of_epoch(...) to compute_start_slot_at_epoch(...)
* rename ACTIVATION_EXIT_DELAY to MAX_SEED_LOOKAHEAD
* update domain types to 0.9.0
* mark AttesterSlashing, IndexedAttestation, AttestationDataAndCustodyBit, DepositData, BeaconBlockHeader, Fork, integer_squareroot(...), and process_voluntary_exit(...) as 0.9.0
* mark increase_balance(...), decrease_balance(...), get_block_root(...), CheckPoint, Deposit, PendingAttestation, HistoricalBatch, is_active_validator(...), and is_slashable_attestation_data(...) as 0.9.0
* mark compute_activation_exit_epoch(...), bls_verify(...), Validator, get_active_validator_indices(...), get_current_epoch(...), get_total_active_balance(...), and get_previous_epoch(...) as 0.9.0
* mark get_block_root_at_slot(...), ProposerSlashing, get_domain(...), VoluntaryExit, mainnet preset Gwei values, minimal preset max operations, process_block_header(...), and is_slashable_validator(...) as 0.9.0
* mark makeWithdrawalCredentials(...), get_validator_churn_limit(...), get_total_balance(...), is_valid_indexed_attestation(...), bls_aggregate_pubkeys(...), initial genesis value/constants, Attestation, get_randao_mix(...), mainnet preset max operations per block constants, minimal preset Gwei values and time parameters, process_eth1_data(...), get_shuffled_seq(...), compute_committee(...), and process_slots(...) as 0.9.0; partially update get_indexed_attestation(...) to 0.9.0 by removing crosslink refs and associated tests
* mark initiate_validator_exit(...), process_registry_updates(...), BeaconBlock, Eth1Data, compute_domain(...), process_randao(...), process_attester_slashing(...), get_base_reward(...), and process_slot(...) as 0.9.0
2019-10-30 19:41:19 +00:00
|
|
|
&"epoch: {shortLog(state.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-01 15:51:24 +00:00
|
|
|
printTimers(state[], attesters, validate, timers)
|