State storage predicate moved to beacon_node, split persistBlock to persistBlock and persistState

This commit is contained in:
Yuriy Glukhov 2019-01-31 18:06:48 +02:00 committed by zah
parent 1606bc6f92
commit bbd94185a4
2 changed files with 17 additions and 12 deletions

View File

@ -4,7 +4,6 @@ import
spec/[datatypes, digest, crypto],
eth_trie/db, ssz
const STATE_STORAGE_PERIOD = 1000 # Save states once per this number of slots. TODO: Find a good number.
type
BeaconChainDB* = ref object
@ -66,17 +65,12 @@ proc persistState*(db: BeaconChainDB, s: BeaconState) =
discard
let serializedState = ssz.serialize(s)
# TODO: Consider mapping slots and last pointer to state hashes to avoid
# duplicating in the db
db.backend.put(lastFinalizedStateKey(), serializedState)
db.backend.put(slotToStateKey(s.slot), serializedState)
if s.slot mod STATE_STORAGE_PERIOD == 0:
# Save slot to state mapping
db.backend.put(slotToStateKey(s.slot), serializedState)
proc persistBlock*(db: BeaconChainDB, s: BeaconState, b: BeaconBlock) =
var prevState = db.lastFinalizedState()
db.persistState(s)
proc persistBlock*(db: BeaconChainDB, b: BeaconBlock) =
let blockHash = b.hash_tree_root_final
db.backend.put(hashToBlockKey(blockHash), ssz.serialize(b))
db.backend.put(slotToBlockHashKey(b.slot), blockHash.data)

View File

@ -28,6 +28,8 @@ const
topicBeaconBlocks = "ethereum/2.1/beacon_chain/blocks"
topicAttestations = "ethereum/2.1/beacon_chain/attestations"
stateStoragePeriod = EPOCH_LENGTH * 10 # Save states once per this number of slots. TODO: Find a good number.
func shortHash(x: auto): string = ($x)[0..7]
func shortValidatorKey(node: BeaconNode, validatorIdx: int): string =
($node.beaconState.validator_registry[validatorIdx].pubkey)[0..7]
@ -106,7 +108,8 @@ proc sync*(node: BeaconNode): Future[bool] {.async.} =
return false
if applyValidatorChangeLog(changeLog, node.beaconState):
node.db.persistBlock(node.beaconState, changeLog.signedBlock)
node.db.persistState(node.beaconState)
node.db.persistBlock(changeLog.signedBlock)
else:
warn "Ignoring invalid validator change log", sentFrom = peer
@ -325,6 +328,10 @@ proc scheduleEpochActions(node: BeaconNode, epoch: uint64) =
if node.lastScheduledEpoch != nextEpoch:
node.scheduleEpochActions(nextEpoch)
proc stateNeedsSaving(s: BeaconState): bool =
# TODO: Come up with a better predicate logic
s.slot mod stateStoragePeriod == 0
proc processBlocks*(node: BeaconNode) =
node.network.subscribe(topicBeaconBlocks) do (newBlock: BeaconBlock):
let stateSlot = node.beaconState.slot
@ -356,7 +363,11 @@ proc processBlocks*(node: BeaconNode) =
node.headBlock = newBlock
node.headBlockRoot = newBlockRoot
node.beaconState = state
node.db.persistBlock(node.beaconState, newBlock)
if stateNeedsSaving(node.beaconState):
node.db.persistState(node.beaconState)
node.db.persistBlock(newBlock)
# TODO:
#