Skip to content

Commit

Permalink
Add Attestations processing (workaround confutils bug:
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Dec 18, 2019
1 parent 271fb08 commit cdaf85c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
13 changes: 7 additions & 6 deletions beacon_chain/spec/beaconstate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions nbench/foo.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import scenarios, confutils

let scenario = ScenarioConf.load()

echo scenario.attestation
34 changes: 33 additions & 1 deletion nbench/nbench.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, '\"'
Expand All @@ -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"

Expand Down
7 changes: 5 additions & 2 deletions nbench/scenarios.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit cdaf85c

Please sign in to comment.