2020-06-01 14:48:24 +00:00
|
|
|
import
|
2020-05-28 14:19:25 +00:00
|
|
|
confutils, stats, chronicles, strformat, tables,
|
2020-06-06 11:26:19 +00:00
|
|
|
stew/byteutils,
|
2020-07-30 19:18:17 +00:00
|
|
|
../beacon_chain/[beacon_chain_db, extras],
|
2020-07-31 14:49:06 +00:00
|
|
|
../beacon_chain/block_pools/[chain_dag],
|
2020-07-07 23:02:14 +00:00
|
|
|
../beacon_chain/spec/[crypto, datatypes, digest, helpers,
|
2020-07-28 13:54:32 +00:00
|
|
|
state_transition, presets],
|
2020-07-22 08:04:21 +00:00
|
|
|
../beacon_chain/sszdump, ../research/simutils,
|
2020-05-28 14:19:25 +00:00
|
|
|
eth/db/[kvstore, kvstore_sqlite3]
|
|
|
|
|
|
|
|
type Timers = enum
|
|
|
|
tInit = "Initialize DB"
|
|
|
|
tLoadBlock = "Load block from database"
|
|
|
|
tLoadState = "Load state from database"
|
|
|
|
tApplyBlock = "Apply block"
|
2020-06-01 14:48:24 +00:00
|
|
|
tApplyEpochBlock = "Apply epoch block"
|
2020-05-28 14:19:25 +00:00
|
|
|
|
2020-06-01 14:48:24 +00:00
|
|
|
type
|
|
|
|
DbCmd* = enum
|
|
|
|
bench
|
2020-06-06 11:26:19 +00:00
|
|
|
dumpState
|
2020-06-25 10:23:10 +00:00
|
|
|
dumpBlock
|
2020-06-16 08:49:32 +00:00
|
|
|
rewindState
|
2020-06-01 14:48:24 +00:00
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
# TODO:
|
|
|
|
# This should probably allow specifying a run-time preset
|
2020-06-01 14:48:24 +00:00
|
|
|
DbConf = object
|
|
|
|
databaseDir* {.
|
|
|
|
defaultValue: ""
|
|
|
|
desc: "Directory where `nbc.sqlite` is stored"
|
|
|
|
name: "db" }: InputDir
|
|
|
|
|
|
|
|
case cmd* {.
|
|
|
|
command
|
2020-06-06 11:26:19 +00:00
|
|
|
desc: ""
|
2020-06-01 14:48:24 +00:00
|
|
|
.}: DbCmd
|
|
|
|
|
|
|
|
of bench:
|
2020-06-25 10:23:10 +00:00
|
|
|
slots* {.
|
|
|
|
defaultValue: 50000
|
|
|
|
desc: "Number of slots to run benchmark for".}: uint64
|
2020-06-01 14:48:24 +00:00
|
|
|
|
2020-06-06 11:26:19 +00:00
|
|
|
of dumpState:
|
|
|
|
stateRoot* {.
|
|
|
|
argument
|
|
|
|
desc: "State roots to save".}: seq[string]
|
|
|
|
|
2020-06-25 10:23:10 +00:00
|
|
|
of dumpBlock:
|
|
|
|
blockRootx* {.
|
|
|
|
argument
|
|
|
|
desc: "Block roots to save".}: seq[string]
|
|
|
|
|
2020-06-16 08:49:32 +00:00
|
|
|
of rewindState:
|
|
|
|
blockRoot* {.
|
|
|
|
argument
|
|
|
|
desc: "Block root".}: string
|
|
|
|
|
|
|
|
slot* {.
|
|
|
|
argument
|
|
|
|
desc: "Slot".}: uint64
|
|
|
|
|
2020-06-01 14:48:24 +00:00
|
|
|
proc cmdBench(conf: DbConf) =
|
2020-05-28 14:19:25 +00:00
|
|
|
var timers: array[Timers, RunningStat]
|
|
|
|
|
|
|
|
echo "Opening database..."
|
|
|
|
let
|
2020-06-01 14:48:24 +00:00
|
|
|
db = BeaconChainDB.init(
|
|
|
|
kvStore SqStoreRef.init(conf.databaseDir.string, "nbc").tryGet())
|
2020-05-28 14:19:25 +00:00
|
|
|
|
2020-07-31 14:49:06 +00:00
|
|
|
if not ChainDAGRef.isInitialized(db):
|
2020-05-28 14:19:25 +00:00
|
|
|
echo "Database not initialized"
|
|
|
|
quit 1
|
|
|
|
|
|
|
|
echo "Initializing block pool..."
|
|
|
|
let pool = withTimerRet(timers[tInit]):
|
2020-07-31 14:49:06 +00:00
|
|
|
ChainDAGRef.init(defaultRuntimePreset, db, {})
|
2020-05-28 14:19:25 +00:00
|
|
|
|
2020-07-28 13:54:32 +00:00
|
|
|
echo &"Loaded {pool.blocks.len} blocks, head slot {pool.head.slot}"
|
2020-05-28 14:19:25 +00:00
|
|
|
|
2020-06-01 14:48:24 +00:00
|
|
|
var
|
|
|
|
blockRefs: seq[BlockRef]
|
2020-06-25 10:23:10 +00:00
|
|
|
blocks: seq[TrustedSignedBeaconBlock]
|
2020-07-28 13:54:32 +00:00
|
|
|
cur = pool.head
|
2020-05-28 14:19:25 +00:00
|
|
|
|
2020-06-01 14:48:24 +00:00
|
|
|
while cur != nil:
|
|
|
|
blockRefs.add cur
|
|
|
|
cur = cur.parent
|
2020-05-28 14:19:25 +00:00
|
|
|
|
2020-06-01 14:48:24 +00:00
|
|
|
for b in 1..<blockRefs.len: # Skip genesis block
|
2020-06-25 10:23:10 +00:00
|
|
|
if blockRefs[blockRefs.len - b - 1].slot > conf.slots:
|
|
|
|
break
|
|
|
|
|
2020-06-01 14:48:24 +00:00
|
|
|
withTimer(timers[tLoadBlock]):
|
|
|
|
blocks.add db.getBlock(blockRefs[blockRefs.len - b - 1].root).get()
|
2020-05-28 14:19:25 +00:00
|
|
|
|
2020-06-01 14:48:24 +00:00
|
|
|
let state = (ref HashedBeaconState)(
|
|
|
|
root: db.getBlock(blockRefs[^1].root).get().message.state_root
|
|
|
|
)
|
2020-05-28 14:19:25 +00:00
|
|
|
|
2020-06-01 14:48:24 +00:00
|
|
|
withTimer(timers[tLoadState]):
|
|
|
|
discard db.getState(state[].root, state[].data, noRollback)
|
2020-05-28 14:19:25 +00:00
|
|
|
|
2020-06-01 14:48:24 +00:00
|
|
|
for b in blocks:
|
|
|
|
let
|
2020-07-22 21:01:44 +00:00
|
|
|
isEpoch = state[].data.get_current_epoch() !=
|
2020-06-01 14:48:24 +00:00
|
|
|
b.message.slot.compute_epoch_at_slot
|
|
|
|
withTimer(timers[if isEpoch: tApplyEpochBlock else: tApplyBlock]):
|
2020-07-07 23:02:14 +00:00
|
|
|
if not state_transition(defaultRuntimePreset, state[], b, {}, noRollback):
|
2020-07-16 13:16:51 +00:00
|
|
|
dump("./", b)
|
2020-06-25 10:23:10 +00:00
|
|
|
echo "State transition failed (!)"
|
|
|
|
quit 1
|
2020-06-01 14:48:24 +00:00
|
|
|
|
2020-06-25 10:23:10 +00:00
|
|
|
printTimers(false, timers)
|
2020-06-01 14:48:24 +00:00
|
|
|
|
2020-06-06 11:26:19 +00:00
|
|
|
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
|
|
|
|
|
2020-06-25 10:23:10 +00:00
|
|
|
proc cmdDumpBlock(conf: DbConf) =
|
|
|
|
let
|
|
|
|
db = BeaconChainDB.init(
|
|
|
|
kvStore SqStoreRef.init(conf.databaseDir.string, "nbc").tryGet())
|
|
|
|
|
|
|
|
for blockRoot in conf.blockRootx:
|
|
|
|
try:
|
|
|
|
let root = Eth2Digest(data: hexToByteArray[32](blockRoot))
|
|
|
|
if (let blck = db.getBlock(root); blck.isSome):
|
2020-07-16 13:16:51 +00:00
|
|
|
dump("./", blck.get())
|
2020-06-25 10:23:10 +00:00
|
|
|
else:
|
|
|
|
echo "Couldn't load ", root
|
|
|
|
except CatchableError as e:
|
|
|
|
echo "Couldn't load ", blockRoot, ": ", e.msg
|
|
|
|
|
2020-06-16 08:49:32 +00:00
|
|
|
proc cmdRewindState(conf: DbConf) =
|
|
|
|
echo "Opening database..."
|
|
|
|
let
|
|
|
|
db = BeaconChainDB.init(
|
|
|
|
kvStore SqStoreRef.init(conf.databaseDir.string, "nbc").tryGet())
|
|
|
|
|
2020-07-31 14:49:06 +00:00
|
|
|
if not ChainDAGRef.isInitialized(db):
|
2020-06-16 08:49:32 +00:00
|
|
|
echo "Database not initialized"
|
|
|
|
quit 1
|
|
|
|
|
|
|
|
echo "Initializing block pool..."
|
2020-07-31 14:49:06 +00:00
|
|
|
let dag = init(ChainDAGRef, defaultRuntimePreset, db)
|
2020-06-16 08:49:32 +00:00
|
|
|
|
2020-07-30 19:18:17 +00:00
|
|
|
let blckRef = dag.getRef(fromHex(Eth2Digest, conf.blockRoot))
|
2020-06-16 08:49:32 +00:00
|
|
|
if blckRef == nil:
|
|
|
|
echo "Block not found in database"
|
|
|
|
return
|
|
|
|
|
2020-07-30 19:18:17 +00:00
|
|
|
dag.withState(dag.tmpState, blckRef.atSlot(Slot(conf.slot))):
|
2020-06-16 08:49:32 +00:00
|
|
|
echo "Writing state..."
|
|
|
|
dump("./", hashedState, blck)
|
|
|
|
|
2020-06-01 14:48:24 +00:00
|
|
|
when isMainModule:
|
|
|
|
let
|
2020-06-06 11:26:19 +00:00
|
|
|
conf = DbConf.load()
|
2020-05-28 14:19:25 +00:00
|
|
|
|
2020-06-06 11:26:19 +00:00
|
|
|
case conf.cmd
|
2020-06-01 14:48:24 +00:00
|
|
|
of bench:
|
2020-06-06 11:26:19 +00:00
|
|
|
cmdBench(conf)
|
|
|
|
of dumpState:
|
|
|
|
cmdDumpState(conf)
|
2020-06-25 10:23:10 +00:00
|
|
|
of dumpBlock:
|
|
|
|
cmdDumpBlock(conf)
|
2020-06-16 08:49:32 +00:00
|
|
|
of rewindState:
|
|
|
|
cmdRewindState(conf)
|