Reduce ChainRef usage (#3093)

* Reduce ChainRef usage

It's now only used in the hive node, from which it probably also should
be removed

* lint
This commit is contained in:
Jacek Sieka 2025-02-19 04:04:22 +01:00 committed by GitHub
parent a86df054b2
commit 03010dc558
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 49 deletions

View File

@ -13,11 +13,9 @@
import
stew/assign2,
results,
../../evm/state,
../../evm/types,
../executor,
../validate,
./chain_desc,
../../evm/[state, types],
../../common,
../[executor, validate],
chronicles,
stint
@ -42,7 +40,7 @@ type
PersistBlockFlags* = set[PersistBlockFlag]
Persister* = object
c: ChainRef
com: CommonRef
flags: PersistBlockFlags
vmState: BaseVMState
@ -64,11 +62,11 @@ proc getVmState(
if p.vmState == nil:
let
vmState = BaseVMState()
txFrame = p.c.db.baseTxFrame.txFrameBegin()
txFrame = p.com.db.baseTxFrame.txFrameBegin()
parent = ?txFrame.getBlockHeader(header.parentHash)
doAssert txFrame.getSavedStateBlockNumber() == parent.number
vmState.init(parent, header, p.c.com, txFrame, storeSlotHash = storeSlotHash)
vmState.init(parent, header, p.com, txFrame, storeSlotHash = storeSlotHash)
p.vmState = vmState
else:
if header.number != p.parent.number + 1:
@ -83,13 +81,13 @@ proc dispose*(p: var Persister) =
p.vmState.ledger.txFrame.dispose()
p.vmState = nil
proc init*(T: type Persister, c: ChainRef, flags: PersistBlockFlags): T =
T(c: c, flags: flags)
proc init*(T: type Persister, com: CommonRef, flags: PersistBlockFlags): T =
T(com: com, flags: flags)
proc checkpoint*(p: var Persister): Result[void, string] =
if NoValidation notin p.flags:
let stateRoot = p.vmState.ledger.txFrame.getStateRoot().valueOr:
return err($$error)
return err($$error)
if p.parent.stateRoot != stateRoot:
# TODO replace logging with better error
@ -104,9 +102,9 @@ proc checkpoint*(p: var Persister): Result[void, string] =
)
# Move in-memory state to disk
p.c.db.persist(p.vmState.ledger.txFrame)
p.com.db.persist(p.vmState.ledger.txFrame)
# Get a new frame since the DB assumes ownership
p.vmState.ledger.txFrame = p.c.db.baseTxFrame().txFrameBegin()
p.vmState.ledger.txFrame = p.com.db.baseTxFrame().txFrameBegin()
ok()
@ -114,7 +112,7 @@ proc persistBlock*(p: var Persister, blk: Block): Result[void, string] =
template header(): Header =
blk.header
let c = p.c
let com = p.com
# Full validation means validating the state root at every block and
# performing the more expensive hash computations on the block itself, ie
@ -143,7 +141,7 @@ proc persistBlock*(p: var Persister, blk: Block): Result[void, string] =
# sanity checks should be performed early in the processing pipeline no
# matter their provenance.
if not skipValidation:
?c.com.validateHeaderAndKinship(blk, vmState.parent, txFrame)
?com.validateHeaderAndKinship(blk, vmState.parent, txFrame)
# Generate receipts for storage or validation but skip them otherwise
?vmState.processBlock(
@ -151,12 +149,12 @@ proc persistBlock*(p: var Persister, blk: Block): Result[void, string] =
skipValidation,
skipReceipts = skipValidation and NoPersistReceipts in p.flags,
skipUncles = NoPersistUncles in p.flags,
taskpool = c.com.taskpool,
taskpool = com.taskpool,
)
if NoPersistHeader notin p.flags:
let blockHash = header.blockHash()
?txFrame.persistHeaderAndSetHead(blockHash, header, c.com.startOfHistory)
?txFrame.persistHeaderAndSetHead(blockHash, header, com.startOfHistory)
if NoPersistTransactions notin p.flags:
txFrame.persistTransactions(header.number, header.txRoot, blk.transactions)
@ -181,14 +179,14 @@ proc persistBlock*(p: var Persister, blk: Block): Result[void, string] =
ok()
proc persistBlocks*(
c: ChainRef, blocks: openArray[Block], flags: PersistBlockFlags = {}
com: CommonRef, blocks: openArray[Block], flags: PersistBlockFlags = {}
): Result[PersistStats, string] =
# Run the VM here
if blocks.len == 0:
debug "Nothing to do"
return ok(default(PersistStats)) # TODO not nice to return nil
var p = Persister.init(c, flags)
var p = Persister.init(com, flags)
for blk in blocks:
p.persistBlock(blk).isOkOr:
@ -198,7 +196,7 @@ proc persistBlocks*(
# update currentBlock *after* we persist it
# so the rpc return consistent result
# between eth_blockNumber and eth_syncing
c.com.syncCurrent = p.parent.number
com.syncCurrent = p.parent.number
let res = p.checkpoint()
p.dispose()

View File

@ -98,7 +98,6 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
let
start = com.db.baseTxFrame().getSavedStateBlockNumber() + 1
chain = com.newChain()
(cfg, genesis_validators_root, lastEra1Block, firstSlotAfterMerge) =
getMetadata(conf.networkId)
time0 = Moment.now()
@ -120,7 +119,7 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
boolFlag({PersistBlockFlag.NoPersistReceipts}, not conf.storeReceipts) +
boolFlag({PersistBlockFlag.NoPersistSlotHashes}, not conf.storeSlotHashes)
blk: Block
persister = Persister.init(chain, flags)
persister = Persister.init(com, flags)
cstats: PersistStats # stats at start of chunk
defer:

View File

@ -1,5 +1,5 @@
# Nimbus
# Copyright (c) 2018-2024 Status Research & Development GmbH
# Copyright (c) 2018-2025 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)
@ -31,26 +31,6 @@ proc addEthHandlerCapability*(
protocol.eth,
EthWireRef.new(chain, txPool, peerPool))
# ------------------------------------------------------------------------------
# Public functions: convenience mappings for `snap`
# ------------------------------------------------------------------------------
when false: # needs to be updated
import
./snap as handlers_snap
proc addSnapHandlerCapability*(
node: EthereumNode;
peerPool: PeerPool;
chain = ChainRef(nil);
) =
## Install `snap` handlers,Passing `chein` as `nil` installs the handler
## in minimal/outbound mode.
if chain.isNil:
node.addCapability protocol.snap
else:
node.addCapability(protocol.snap, SnapWireRef.init(chain, peerPool))
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------

View File

@ -114,7 +114,6 @@ proc test_chainSync*(
## Store persistent blocks from dump into chain DB
let
sayBlocks = 900'u64
chain = com.newChain()
blockOnDb = com.db.baseTxFrame().getSavedStateBlockNumber()
lastBlock = max(1, numBlocks).BlockNumber
@ -181,12 +180,12 @@ proc test_chainSync*(
noisy.startLogging(w[0].header.number)
noisy.stopLoggingAfter():
let runPersistBlocksRc = chain.persistBlocks(w)
let runPersistBlocksRc = com.persistBlocks(w)
xCheck runPersistBlocksRc.isOk():
if noisy:
noisy.whisper "***", "Re-run with logging enabled...\n"
setTraceLevel()
discard chain.persistBlocks(w)
discard com.persistBlocks(w)
blocks += w.len
continue
@ -211,7 +210,7 @@ proc test_chainSync*(
sayPerf
noisy.whisper "***",
&"processing {dotsOrSpace}[#{fromBlock:>8},#{(lastBlock-1):>8}]"
let runPersistBlocks1Rc = chain.persistBlocks(blocks1)
let runPersistBlocks1Rc = com.persistBlocks(blocks1)
xCheck runPersistBlocks1Rc.isOk()
dotsOrSpace = " "
@ -227,7 +226,7 @@ proc test_chainSync*(
noisy.whisper "***",
&"processing {dotsOrSpace}[#{lastBlock:>8},#{lastBlock:>8}]"
noisy.stopLoggingAfter():
let runPersistBlocks0Rc = chain.persistBlocks(blocks0)
let runPersistBlocks0Rc = com.persistBlocks(blocks0)
xCheck runPersistBlocks0Rc.isOk()
else:
if oldLogAlign:
@ -238,7 +237,7 @@ proc test_chainSync*(
noisy.whisper "***",
&"processing {dotsOrSpace}[#{lastBlock:>8},#{toBlock:>8}]"
noisy.stopLoggingAfter():
let runPersistBlocks9Rc = chain.persistBlocks(blocks9)
let runPersistBlocks9Rc = com.persistBlocks(blocks9)
xCheck runPersistBlocks9Rc.isOk()
break
if not oldLogAlign: