From 7e944824091651ef6446e852745185d7b8f90124 Mon Sep 17 00:00:00 2001 From: tersec Date: Thu, 23 Apr 2020 09:59:29 +0000 Subject: [PATCH] avoid rewindStateData/state_transition in fast-path (#919) * avoid rewindStateData/state_transition totally in fast-pathed updateStateData calls * document motivation behind BlockPool.getStateDataCached(...) and clarify naming --- beacon_chain/block_pool.nim | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/beacon_chain/block_pool.nim b/beacon_chain/block_pool.nim index c5a2f75e9..6e397bfa0 100644 --- a/beacon_chain/block_pool.nim +++ b/beacon_chain/block_pool.nim @@ -715,6 +715,28 @@ proc rewindState(pool: BlockPool, state: var StateData, bs: BlockSlot): ancestors +proc getStateDataCached(pool: BlockPool, state: var StateData, bs: BlockSlot): bool = + # This pointedly does not run rewindState or state_transition, but otherwise + # mostly matches updateStateData(...), because it's too expensive to run the + # rewindState(...)/skipAndUpdateState(...)/state_transition(...) procs, when + # each hash_tree_root(...) consumes a nontrivial fraction of a second. + for db in [pool.db, pool.cachedStates[0], pool.cachedStates[1]]: + if (let tmp = db.getStateRoot(bs.blck.root, bs.slot); tmp.isSome()): + if not db.containsState(tmp.get): + continue + + let + root = tmp.get() + ancestorState = db.getState(root) + + doAssert ancestorState.isSome() + state.data.data = ancestorState.get() + state.data.root = root + state.blck = pool.get(bs.blck).refs + return true + + false + proc updateStateData*(pool: BlockPool, state: var StateData, bs: BlockSlot) = ## Rewind or advance state such that it matches the given block and slot - ## this may include replaying from an earlier snapshot if blck is on a @@ -732,6 +754,9 @@ proc updateStateData*(pool: BlockPool, state: var StateData, bs: BlockSlot) = return # State already at the right spot + if pool.getStateDataCached(state, bs): + return + let ancestors = rewindState(pool, state, bs) # If we come this far, we found the state root. The last block on the stack