From cdaf85cdde402d668d744e7af9bd7af59e66b4e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mamy=20Andr=C3=A9-Ratsimbazafy?= Date: Thu, 12 Dec 2019 16:07:02 +0100 Subject: [PATCH] Add Attestations processing (workaround confutils bug: - https://github.com/status-im/nim-confutils/issues/10 - https://github.com/status-im/nim-confutils/issues/11 - https://github.com/status-im/nim-confutils/issues/12 --- beacon_chain/spec/beaconstate.nim | 13 ++++++------ nbench/foo.nim | 5 +++++ nbench/nbench.nim | 34 ++++++++++++++++++++++++++++++- nbench/scenarios.nim | 7 +++++-- 4 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 nbench/foo.nim diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index f73ea29eda..de9e3b2b6f 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -9,10 +9,11 @@ import tables, algorithm, math, sequtils, options, json_serialization/std/sets, chronicles, stew/bitseqs, ../extras, ../ssz, - ./crypto, ./datatypes, ./digest, ./helpers, ./validator + ./crypto, ./datatypes, ./digest, ./helpers, ./validator, + ../../nbench/bench_lab # https://github.com/ethereum/eth2.0-specs/blob/v0.9.3/specs/core/0_beacon-chain.md#is_valid_merkle_branch -func is_valid_merkle_branch*(leaf: Eth2Digest, branch: openarray[Eth2Digest], depth: uint64, index: uint64, root: Eth2Digest): bool = +func is_valid_merkle_branch*(leaf: Eth2Digest, branch: openarray[Eth2Digest], depth: uint64, index: uint64, root: Eth2Digest): bool {.nbench.}= ## Check if ``leaf`` at ``index`` verifies against the Merkle ``root`` and ## ``branch``. var @@ -48,7 +49,7 @@ func decrease_balance*( # https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/specs/core/0_beacon-chain.md#deposits func process_deposit*( - state: var BeaconState, deposit: Deposit, flags: UpdateFlags = {}): bool = + state: var BeaconState, deposit: Deposit, flags: UpdateFlags = {}): bool {.nbench.}= # Process an Eth1 deposit, registering a validator or increasing its balance. # Verify the Merkle branch @@ -194,7 +195,7 @@ func initialize_beacon_state_from_eth1*( eth1_block_hash: Eth2Digest, eth1_timestamp: uint64, deposits: openArray[Deposit], - flags: UpdateFlags = {}): BeaconState = + flags: UpdateFlags = {}): BeaconState {.nbench.}= ## Get the genesis ``BeaconState``. ## ## Before the beacon chain starts, validators will register in the Eth1 chain @@ -315,7 +316,7 @@ func is_eligible_for_activation(state: BeaconState, validator: Validator): validator.activation_epoch == FAR_FUTURE_EPOCH # https://github.com/ethereum/eth2.0-specs/blob/v0.9.3/specs/core/0_beacon-chain.md#registry-updates -proc process_registry_updates*(state: var BeaconState) = +proc process_registry_updates*(state: var BeaconState) {.nbench.}= ## Process activation eligibility and ejections ## Try to avoid caching here, since this could easily become undefined @@ -500,7 +501,7 @@ proc check_attestation*( proc process_attestation*( state: var BeaconState, attestation: Attestation, flags: UpdateFlags, - stateCache: var StateCache): bool = + stateCache: var StateCache): bool {.nbench.}= # In the spec, attestation validation is mixed with state mutation, so here # we've split it into two functions so that the validation logic can be # reused when looking for suitable blocks to include in attestations. diff --git a/nbench/foo.nim b/nbench/foo.nim new file mode 100644 index 0000000000..748905344a --- /dev/null +++ b/nbench/foo.nim @@ -0,0 +1,5 @@ +import scenarios, confutils + +let scenario = ScenarioConf.load() + +echo scenario.attestation diff --git a/nbench/nbench.nim b/nbench/nbench.nim index a6470566b6..389a01c3e9 100644 --- a/nbench/nbench.nim +++ b/nbench/nbench.nim @@ -11,7 +11,7 @@ import # Status libraries confutils, serialization, # Beacon-chain - ../beacon_chain/spec/[datatypes, crypto], + ../beacon_chain/spec/[datatypes, crypto, beaconstate, validator], ../beacon_chain/[ssz, state_transition, extras], # Bench specific scenarios, bench_lab, reports @@ -55,6 +55,26 @@ proc runProcessSlots(dir, preState: string, numSlots: uint64) = process_slots(state[], state.slot + numSlots) +proc runProcessAttestation(dir, preState, attestation: string, skipBLS: bool) = + let prePath = dir / preState & ".ssz" + + var state: ref BeaconState + new state + echo "Running: ", prePath + state[] = SSZ.loadFile(prePath, BeaconState) + + var att: ref Attestation + new att + var cache = get_empty_per_epoch_cache() + + let attPath = dir / attestation & ".ssz" + echo "Processing: ", attPath + att[] = SSZ.loadFile(attPath, Attestation) + let flags = if skipBLS: {skipValidation} # TODO: this also skips state root verification + else: {} + let success = process_attestation(state[], att[], flags, cache) + echo "process_attestation status: ", if success: "SUCCESS ✓" else: "FAILURE ⚠️" + proc main() = # TODO versioning echo "Nimbus bench, preset \"", const_preset, '\"' @@ -77,6 +97,18 @@ proc main() = scenario.preState, scenario.numSlots ) + of cmdBlockProcessing: + case scenario.blockProcessingCat + of catAttestations: + runProcessAttestation( + scenario.scenarioDir.string, + scenario.preState, + "attestation", # Pending https://github.com/status-im/nim-confutils/issues/11 + # scenario.attestation, + scenario.skipBLS + ) + else: + quit "Unsupported" else: quit "Unsupported" diff --git a/nbench/scenarios.nim b/nbench/scenarios.nim index 6be824dc7b..dff9ef1a64 100644 --- a/nbench/scenarios.nim +++ b/nbench/scenarios.nim @@ -70,7 +70,7 @@ type of cmdBlockProcessing: case blockProcessingCat* {. desc: "block transitions" - name: "process-blocks" + # name: "process-blocks" # Pending https://github.com/status-im/nim-confutils/issues/10 implicitlySelectable required .}: BlockProcessingCat of catBlockHeader: @@ -84,7 +84,10 @@ type of catAttesterSlashings: discard of catAttestations: - discard + attestation*{. + desc: "Attestation filename (without .ssz)" + name: "attestation" + defaultValue: "attestation".}: string of catDeposits: discard of catVoluntaryExits: