2018-11-23 23:58:49 +00:00
|
|
|
import
|
2019-01-08 17:28:21 +00:00
|
|
|
os, json, tables,
|
2018-11-23 23:58:49 +00:00
|
|
|
chronicles, json_serialization, eth_common/eth_types_json_serialization,
|
2018-12-28 16:51:40 +00:00
|
|
|
spec/[datatypes, digest, crypto]
|
2018-11-23 23:58:49 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
BeaconChainDB* = ref object
|
|
|
|
dataRoot: string
|
2019-01-08 17:28:21 +00:00
|
|
|
blocks*: Table[Eth2Digest, BeaconBlock]
|
2018-11-23 23:58:49 +00:00
|
|
|
|
|
|
|
BeaconStateRef* = ref BeaconState
|
|
|
|
|
|
|
|
proc init*(T: type BeaconChainDB, dataDir: string): BeaconChainDB =
|
|
|
|
new result
|
|
|
|
result.dataRoot = dataDir / "beacon_db"
|
|
|
|
createDir(result.dataRoot)
|
|
|
|
|
|
|
|
proc lastFinalizedState*(db: BeaconChainDB): BeaconStateRef =
|
|
|
|
try:
|
2018-12-19 12:58:53 +00:00
|
|
|
let stateFile = db.dataRoot / "BeaconState.json"
|
|
|
|
if fileExists stateFile:
|
|
|
|
new result
|
2018-12-27 20:14:37 +00:00
|
|
|
# TODO serialization error: Json.loadFile(stateFile, result[])
|
2018-11-23 23:58:49 +00:00
|
|
|
except:
|
2018-12-19 12:58:53 +00:00
|
|
|
error "Failed to load the latest finalized state",
|
|
|
|
err = getCurrentExceptionMsg()
|
2018-11-23 23:58:49 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
proc persistBlock*(db: BeaconChainDB, s: BeaconState, b: BeaconBlock) =
|
2018-12-19 12:58:53 +00:00
|
|
|
Json.saveFile(db.dataRoot / "BeaconState.json", s, pretty = true)
|
2018-11-23 23:58:49 +00:00
|
|
|
debug "State persisted"
|
|
|
|
|