ncli_db: allow saving states by root (#1136)

also dump state+block when validation fails
This commit is contained in:
Jacek Sieka 2020-06-06 13:26:19 +02:00 committed by GitHub
parent 99b2d6758a
commit ea8f96d284
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 8 deletions

View File

@ -107,7 +107,17 @@ proc storeBlock*(
dump(node.config.dumpDir / "incoming", signedBlock, blockRoot) dump(node.config.dumpDir / "incoming", signedBlock, blockRoot)
beacon_blocks_received.inc() beacon_blocks_received.inc()
discard ? node.blockPool.add(blockRoot, signedBlock) let blck = node.blockPool.add(blockRoot, signedBlock)
if blck.isErr:
if blck.error == Invalid and node.config.dumpEnabled:
let parent = node.blockPool.getRef(signedBlock.message.parent_root)
if parent != nil:
node.blockPool.withState(
node.blockPool.tmpState, parent.atSlot(signedBlock.message.slot - 1)):
dump(node.config.dumpDir / "invalid", hashedState, parent)
dump(node.config.dumpDir / "invalid", signedBlock, blockRoot)
return err(blck.error)
# The block we received contains attestations, and we might not yet know about # The block we received contains attestations, and we might not yet know about
# all of them. Let's add them to the attestation pool - in case they block # all of them. Let's add them to the attestation pool - in case they block

View File

@ -18,3 +18,7 @@ proc dump*(dir: string, v: HashedBeaconState, blck: BlockRef) =
dir / &"state-{v.data.slot}-{shortLog(blck.root)}-{shortLog(v.root)}.ssz", dir / &"state-{v.data.slot}-{shortLog(blck.root)}-{shortLog(v.root)}.ssz",
v.data) v.data)
proc dump*(dir: string, v: HashedBeaconState) =
SSZ.saveFile(
dir / &"state-{v.data.slot}-{shortLog(v.root)}.ssz",
v.data)

View File

@ -1,8 +1,9 @@
import import
confutils, stats, chronicles, strformat, tables, confutils, stats, chronicles, strformat, tables,
../beacon_chain/block_pool, stew/byteutils,
../beacon_chain/spec/[crypto, datatypes, helpers], ../beacon_chain/[beacon_chain_db, block_pool, extras, state_transition],
../beacon_chain/[beacon_chain_db, extras, state_transition], ../beacon_chain/spec/[crypto, datatypes, digest, helpers],
../beacon_chain/sszdump,
../research/simutils, ../research/simutils,
eth/db/[kvstore, kvstore_sqlite3] eth/db/[kvstore, kvstore_sqlite3]
@ -16,6 +17,7 @@ type Timers = enum
type type
DbCmd* = enum DbCmd* = enum
bench bench
dumpState
DbConf = object DbConf = object
databaseDir* {. databaseDir* {.
@ -25,7 +27,7 @@ type
case cmd* {. case cmd* {.
command command
desc: "Run benchmark by applying all blocks from database" desc: ""
.}: DbCmd .}: DbCmd
of bench: of bench:
@ -33,6 +35,11 @@ type
defaultValue: true defaultValue: true
desc: "Enable BLS validation" }: bool desc: "Enable BLS validation" }: bool
of dumpState:
stateRoot* {.
argument
desc: "State roots to save".}: seq[string]
proc cmdBench(conf: DbConf) = proc cmdBench(conf: DbConf) =
var timers: array[Timers, RunningStat] var timers: array[Timers, RunningStat]
@ -81,10 +88,28 @@ proc cmdBench(conf: DbConf) =
printTimers(conf.validate, timers) printTimers(conf.validate, timers)
proc cmdDumpState(conf: DbConf) =
let
db = BeaconChainDB.init(
kvStore SqStoreRef.init(conf.databaseDir.string, "nbc").tryGet())
for stateRoot in conf.stateRoot:
try:
let root = Eth2Digest(data: hexToByteArray[32](stateRoot))
var state = (ref HashedBeaconState)(root: root)
if not db.getState(root, state.data, noRollback):
echo "Couldn't load ", root
else:
dump("./", state[])
except CatchableError as e:
echo "Couldn't load ", stateRoot, ": ", e.msg
when isMainModule: when isMainModule:
let let
config = DbConf.load() conf = DbConf.load()
case config.cmd case conf.cmd
of bench: of bench:
cmdBench(config) cmdBench(conf)
of dumpState:
cmdDumpState(conf)