combine ncli_hash_tree_root and ncli_pretty into ncli_ssz (#1393)

* combine ncli_hash_tree_root and ncli_pretty into ncli_ssz

* disable ncli_query building by default

* fold ncli_transition into ncli_ssz and rename to ncli
This commit is contained in:
tersec 2020-07-29 21:19:36 +00:00 committed by GitHub
parent 023f7f4518
commit af012ed4dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 125 additions and 100 deletions

View File

@ -23,6 +23,7 @@ GOERLI_WEB3_URL := "wss://goerli.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871
VALIDATORS := 1
# unconditionally built by the default Make target
# TODO re-enable ncli_query if/when it works again
TOOLS := \
beacon_node \
block_sim \
@ -31,11 +32,8 @@ TOOLS := \
logtrace \
nbench \
nbench_spec_scenarios \
ncli \
ncli_db \
ncli_hash_tree_root \
ncli_pretty \
ncli_query \
ncli_transition \
process_dashboard \
stack_sizes \
state_sim \

View File

@ -12,7 +12,7 @@ import
confutils/defs, serialization, chronicles,
# Beacon-chain
../beacon_chain/spec/[
datatypes, crypto, helpers, beaconstate, validator, helpers,
datatypes, crypto, helpers, beaconstate, helpers,
state_transition_block, state_transition_epoch, state_transition],
../beacon_chain/extras,
../beacon_chain/ssz/[merkleization, ssz_serialization]

122
ncli/ncli.nim Normal file
View File

@ -0,0 +1,122 @@
import
confutils, chronicles, os, strutils, json_serialization,
stew/byteutils,
../beacon_chain/spec/[crypto, datatypes, digest, state_transition],
../beacon_chain/extras,
../beacon_chain/ssz/[merkleization, ssz_serialization]
type
Cmd* = enum
hashTreeRoot = "Compute hash tree root of SSZ object"
pretty = "Pretty-print SSZ object"
transition = "Run state transition function"
NcliConf* = object
# TODO confutils argument pragma doesn't seem to do much; also, the cases
# are largely equivalent, but this helps create command line usage text
case cmd* {.command}: Cmd
of hashTreeRoot:
htrKind* {.
argument
desc: "kind of SSZ object: attester_slashing, attestation, signed_block, block, block_body, block_header, deposit, deposit_data, eth1_data, state, proposer_slashing, or voluntary_exit"}: string
htrFile* {.
argument
desc: "filename of SSZ or JSON-encoded object of which to compute hash tree root"}: string
of pretty:
prettyKind* {.
argument
desc: "kind of SSZ object: attester_slashing, attestation, signed_block, block, block_body, block_header, deposit, deposit_data, eth1_data, state, proposer_slashing, or voluntary_exit"}: string
prettyFile* {.
argument
desc: "filename of SSZ or JSON-encoded object to pretty-print"}: string
of transition:
preState* {.
argument
desc: "State to which to apply specified block"}: string
blck* {.
argument
desc: "Block to apply to preState"}: string
postState* {.
argument
desc: "Filename of state resulting from applying blck to preState"}: string
verifyStateRoot* {.
argument
desc: "Verify state root (default true)"
defaultValue: true}: bool
proc doTransition(conf: NcliConf) =
let
stateY = (ref HashedBeaconState)(
data: SSZ.loadFile(conf.preState, BeaconState),
)
blckX = SSZ.loadFile(conf.blck, SignedBeaconBlock)
flags = if not conf.verifyStateRoot: {skipStateRootValidation} else: {}
stateY.root = hash_tree_root(stateY.data)
if not state_transition(defaultRuntimePreset, stateY[], blckX, flags, noRollback):
error "State transition failed"
quit 1
else:
SSZ.saveFile(conf.postState, stateY.data)
proc doSSZ(conf: NcliConf) =
let (kind, file) =
case conf.cmd:
of hashTreeRoot: (conf.htrKind, conf.htrFile)
of pretty: (conf.prettyKind, conf.prettyFile)
of transition:
raiseAssert "doSSZ() only implements hashTreeRoot and pretty commands"
template printit(t: untyped) {.dirty.} =
let v = newClone(
if cmpIgnoreCase(ext, ".ssz") == 0:
SSZ.loadFile(file, t)
elif cmpIgnoreCase(ext, ".json") == 0:
JSON.loadFile(file, t)
else:
echo "Unknown file type: ", ext
quit 1
)
case conf.cmd:
of hashTreeRoot:
when t is SignedBeaconBlock:
echo hash_tree_root(v.message).data.toHex()
else:
echo hash_tree_root(v[]).data.toHex()
of pretty:
echo JSON.encode(v[], pretty = true)
of transition:
raiseAssert "doSSZ() only implements hashTreeRoot and pretty commands"
let ext = splitFile(file).ext
case kind
of "attester_slashing": printit(AttesterSlashing)
of "attestation": printit(Attestation)
of "signed_block": printit(SignedBeaconBlock)
of "block": printit(BeaconBlock)
of "block_body": printit(BeaconBlockBody)
of "block_header": printit(BeaconBlockHeader)
of "deposit": printit(Deposit)
of "deposit_data": printit(DepositData)
of "eth1_data": printit(Eth1Data)
of "state": printit(BeaconState)
of "proposer_slashing": printit(ProposerSlashing)
of "voluntary_exit": printit(VoluntaryExit)
when isMainModule:
let conf = NcliConf.load()
case conf.cmd:
of hashTreeRoot: doSSZ(conf)
of pretty: doSSZ(conf)
of transition: doTransition(conf)

View File

@ -1,39 +0,0 @@
import
confutils, os, strutils, json_serialization,
stew/byteutils,
../beacon_chain/spec/[crypto, datatypes, digest],
../beacon_chain/ssz/[merkleization, ssz_serialization]
# TODO turn into arguments
cli do(kind: string, file: string):
template printit(t: untyped) {.dirty.} =
let v = newClone(
if cmpIgnoreCase(ext, ".ssz") == 0:
SSZ.loadFile(file, t)
elif cmpIgnoreCase(ext, ".json") == 0:
JSON.loadFile(file, t)
else:
echo "Unknown file type: ", ext
quit 1
)
when t is SignedBeaconBlock:
echo hash_tree_root(v.message).data.toHex()
else:
echo hash_tree_root(v[]).data.toHex()
let ext = splitFile(file).ext
case kind
of "attester_slashing": printit(AttesterSlashing)
of "attestation": printit(Attestation)
of "signed_block": printit(SignedBeaconBlock)
of "block": printit(BeaconBlock)
of "block_body": printit(BeaconBlockBody)
of "block_header": printit(BeaconBlockHeader)
of "deposit": printit(Deposit)
of "deposit_data": printit(DepositData)
of "eth1_data": printit(Eth1Data)
of "state": printit(BeaconState)
of "proposer_slashing": printit(ProposerSlashing)
of "voluntary_exit": printit(VoluntaryExit)

View File

@ -1,35 +0,0 @@
import
confutils, os, strutils, chronicles, json_serialization,
../beacon_chain/spec/[crypto, datatypes, digest],
../beacon_chain/ssz/ssz_serialization
# TODO turn into arguments
cli do(kind: string, file: string):
template printit(t: untyped) {.dirty.} =
let v = newClone(
if cmpIgnoreCase(ext, ".ssz") == 0:
SSZ.loadFile(file, t)
elif cmpIgnoreCase(ext, ".json") == 0:
JSON.loadFile(file, t)
else:
echo "Unknown file type: ", ext
quit 1
)
echo JSON.encode(v[], pretty = true)
let ext = splitFile(file).ext
case kind
of "attester_slashing": printit(AttesterSlashing)
of "attestation": printit(Attestation)
of "signed_block": printit(SignedBeaconBlock)
of "block": printit(BeaconBlock)
of "block_body": printit(BeaconBlockBody)
of "block_header": printit(BeaconBlockHeader)
of "deposit": printit(Deposit)
of "deposit_data": printit(DepositData)
of "eth1_data": printit(Eth1Data)
of "state": printit(BeaconState)
of "proposer_slashing": printit(ProposerSlashing)
of "voluntary_exit": printit(VoluntaryExit)
else: echo "Unknown kind"

View File

@ -1,21 +0,0 @@
import
confutils, chronicles,
../beacon_chain/spec/[crypto, datatypes, state_transition, presets],
../beacon_chain/extras,
../beacon_chain/ssz/[merkleization, ssz_serialization]
cli do(pre: string, blck: string, post: string, verifyStateRoot = true):
let
stateY = (ref HashedBeaconState)(
data: SSZ.loadFile(pre, BeaconState),
)
blckX = SSZ.loadFile(blck, SignedBeaconBlock)
flags = if not verifyStateRoot: {skipStateRootValidation} else: {}
stateY.root = hash_tree_root(stateY.data)
if not state_transition(defaultRuntimePreset, stateY[], blckX, flags, noRollback):
error "State transition failed"
quit 1
else:
SSZ.saveFile(post, stateY.data)