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-06-23 13:54:24 +00:00
|
|
|
../beacon_chain/[beacon_chain_db, block_pool, extras],
|
|
|
|
../beacon_chain/spec/[crypto, datatypes, digest, helpers, state_transition],
|
2020-06-06 11:26:19 +00:00
|
|
|
../beacon_chain/sszdump,
|
2020-05-28 14:19:25 +00:00
|
|
|
../research/simutils,
|
|
|
|
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-16 08:49:32 +00:00
|
|
|
rewindState
|
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:
|
|
|
|
validate* {.
|
|
|
|
defaultValue: true
|
|
|
|
desc: "Enable BLS validation" }: bool
|
|
|
|
|
2020-06-06 11:26:19 +00:00
|
|
|
of dumpState:
|
|
|
|
stateRoot* {.
|
|
|
|
argument
|
|
|
|
desc: "State 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
|
|
|
|
|
|
|
if not BlockPool.isInitialized(db):
|
|
|
|
echo "Database not initialized"
|
|
|
|
quit 1
|
|
|
|
|
|
|
|
echo "Initializing block pool..."
|
|
|
|
let pool = withTimerRet(timers[tInit]):
|
|
|
|
CandidateChains.init(db, {})
|
|
|
|
|
|
|
|
echo &"Loaded {pool.blocks.len} blocks, head slot {pool.head.blck.slot}"
|
|
|
|
|
2020-06-01 14:48:24 +00:00
|
|
|
var
|
|
|
|
blockRefs: seq[BlockRef]
|
|
|
|
blocks: seq[SignedBeaconBlock]
|
|
|
|
cur = pool.head.blck
|
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
|
|
|
|
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
|
|
|
let flags = if conf.validate: {} else: {skipBlsValidation}
|
|
|
|
for b in blocks:
|
|
|
|
let
|
|
|
|
isEpoch = state[].data.slot.compute_epoch_at_slot !=
|
|
|
|
b.message.slot.compute_epoch_at_slot
|
|
|
|
withTimer(timers[if isEpoch: tApplyEpochBlock else: tApplyBlock]):
|
|
|
|
discard state_transition(state[], b, flags, noRollback)
|
|
|
|
|
|
|
|
printTimers(conf.validate, timers)
|
|
|
|
|
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-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())
|
|
|
|
|
|
|
|
if not BlockPool.isInitialized(db):
|
|
|
|
echo "Database not initialized"
|
|
|
|
quit 1
|
|
|
|
|
|
|
|
echo "Initializing block pool..."
|
|
|
|
let pool = BlockPool.init(db, {})
|
|
|
|
|
|
|
|
let blckRef = pool.getRef(fromHex(Eth2Digest, conf.blockRoot))
|
|
|
|
if blckRef == nil:
|
|
|
|
echo "Block not found in database"
|
|
|
|
return
|
|
|
|
|
|
|
|
pool.withState(pool.tmpState, blckRef.atSlot(Slot(conf.slot))):
|
|
|
|
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-16 08:49:32 +00:00
|
|
|
of rewindState:
|
|
|
|
cmdRewindState(conf)
|