2022-11-16 23:51:06 +00:00
|
|
|
# nimbus-eth1
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed
|
|
|
|
# except according to those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
eth/[common, rlp],
|
|
|
|
stew/results,
|
|
|
|
../../range_desc,
|
|
|
|
"."/[hexary_error, snapdb_desc, snapdb_persistent]
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
type
|
|
|
|
SnapDbPivotRegistry* = object
|
2022-11-25 14:56:42 +00:00
|
|
|
predecessor*: NodeKey ## Predecessor key in chain, auto filled
|
2022-11-16 23:51:06 +00:00
|
|
|
header*: BlockHeader ## Pivot state, containg state root
|
|
|
|
nAccounts*: uint64 ## Imported # of accounts
|
|
|
|
nSlotLists*: uint64 ## Imported # of account storage tries
|
|
|
|
dangling*: seq[Blob] ## Dangling nodes in accounts trie
|
2022-11-25 14:56:42 +00:00
|
|
|
processed*: seq[
|
|
|
|
(NodeTag,NodeTag)] ## Processed acoount ranges
|
2022-11-16 23:51:06 +00:00
|
|
|
slotAccounts*: seq[NodeKey] ## List of accounts with storage slots
|
|
|
|
|
|
|
|
const
|
|
|
|
extraTraceMessages = false or true
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template handleRlpException(info: static[string]; code: untyped) =
|
|
|
|
try:
|
|
|
|
code
|
|
|
|
except RlpError:
|
|
|
|
return err(RlpEncoding)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc savePivot*(
|
2022-12-02 04:39:12 +00:00
|
|
|
pv: SnapDbRef; ## Base descriptor on `ChainDBRef`
|
2022-11-16 23:51:06 +00:00
|
|
|
data: SnapDbPivotRegistry; ## Registered data record
|
2022-11-28 09:03:23 +00:00
|
|
|
): Result[int,HexaryError] =
|
2022-11-16 23:51:06 +00:00
|
|
|
## Register pivot environment
|
|
|
|
handleRlpException("savePivot()"):
|
|
|
|
let rlpData = rlp.encode(data)
|
|
|
|
pv.kvDb.persistentStateRootPut(data.header.stateRoot.to(NodeKey), rlpData)
|
|
|
|
return ok(rlpData.len)
|
|
|
|
# notreached
|
|
|
|
|
|
|
|
proc recoverPivot*(
|
2022-12-02 04:39:12 +00:00
|
|
|
pv: SnapDbRef; ## Base descriptor on `ChainDBRef`
|
2022-11-16 23:51:06 +00:00
|
|
|
stateRoot: NodeKey; ## Check for a particular state root
|
2022-11-28 09:03:23 +00:00
|
|
|
): Result[SnapDbPivotRegistry,HexaryError] =
|
2022-11-16 23:51:06 +00:00
|
|
|
## Restore pivot environment for a particular state root.
|
|
|
|
let rc = pv.kvDb.persistentStateRootGet(stateRoot)
|
|
|
|
if rc.isOk:
|
|
|
|
handleRlpException("recoverPivot()"):
|
|
|
|
var r = rlp.decode(rc.value.data, SnapDbPivotRegistry)
|
|
|
|
r.predecessor = rc.value.key
|
|
|
|
return ok(r)
|
|
|
|
err(StateRootNotFound)
|
|
|
|
|
|
|
|
proc recoverPivot*(
|
2022-12-02 04:39:12 +00:00
|
|
|
pv: SnapDbRef; ## Base descriptor on `ChainDBRef`
|
2022-11-28 09:03:23 +00:00
|
|
|
): Result[SnapDbPivotRegistry,HexaryError] =
|
2022-11-16 23:51:06 +00:00
|
|
|
## Restore pivot environment that was saved latest.
|
|
|
|
let rc = pv.kvDb.persistentStateRootGet(NodeKey.default)
|
|
|
|
if rc.isOk:
|
|
|
|
return pv.recoverPivot(rc.value.key)
|
|
|
|
err(StateRootNotFound)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|