nimbus-eth2/beacon_chain/beacon_chain_db.nim

33 lines
955 B
Nim
Raw Normal View History

import
2019-01-08 17:28:21 +00:00
os, json, tables,
chronicles, json_serialization, eth_common/eth_types_json_serialization,
spec/[datatypes, digest, crypto]
type
BeaconChainDB* = ref object
dataRoot: string
2019-01-08 17:28:21 +00:00
blocks*: Table[Eth2Digest, BeaconBlock]
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:
let stateFile = db.dataRoot / "BeaconState.json"
if fileExists stateFile:
new result
# TODO serialization error: Json.loadFile(stateFile, result[])
except:
error "Failed to load the latest finalized state",
err = getCurrentExceptionMsg()
return nil
proc persistBlock*(db: BeaconChainDB, s: BeaconState, b: BeaconBlock) =
Json.saveFile(db.dataRoot / "BeaconState.json", s, pretty = true)
debug "State persisted"