6262 blockExecutionTimer = metrics .NewRegisteredTimer ("chain/execution" , nil )
6363 blockWriteTimer = metrics .NewRegisteredTimer ("chain/write" , nil )
6464
65+ blockPrefetchExecuteTimer = metrics .NewRegisteredTimer ("chain/prefetch/executes" , nil )
66+ blockPrefetchInterruptMeter = metrics .NewRegisteredMeter ("chain/prefetch/interrupts" , nil )
67+
6568 ErrNoGenesis = errors .New ("Genesis not found in chain" )
6669)
6770
@@ -126,7 +129,6 @@ type BlockChain struct {
126129 genesisBlock * types.Block
127130
128131 chainmu sync.RWMutex // blockchain insertion lock
129- procmu sync.RWMutex // block processor lock
130132
131133 checkpoint int // checkpoint counts towards the new checkpoint
132134 currentBlock atomic.Value // Current head of the block chain
@@ -145,10 +147,11 @@ type BlockChain struct {
145147 procInterrupt int32 // interrupt signaler for block processing
146148 wg sync.WaitGroup // chain processing wait group for shutting down
147149
148- engine consensus.Engine
149- processor Processor // block processor interface
150- validator Validator // block and state validator interface
151- vmConfig vm.Config
150+ engine consensus.Engine
151+ validator Validator // Block and state validator interface
152+ prefetcher Prefetcher // Block state prefetcher interface
153+ processor Processor // Block transaction processor interface
154+ vmConfig vm.Config
152155
153156 badBlocks * lru.Cache // Bad block cache
154157 shouldPreserve func (* types.Block ) bool // Function used to determine whether should preserve the given block.
@@ -189,8 +192,9 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
189192 vmConfig : vmConfig ,
190193 badBlocks : badBlocks ,
191194 }
192- bc .SetValidator (NewBlockValidator (chainConfig , bc , engine ))
193- bc .SetProcessor (NewStateProcessor (chainConfig , bc , engine ))
195+ bc .validator = NewBlockValidator (chainConfig , bc , engine )
196+ bc .prefetcher = newStatePrefetcher (chainConfig , bc , engine )
197+ bc .processor = NewStateProcessor (chainConfig , bc , engine )
194198
195199 var err error
196200 bc .hc , err = NewHeaderChain (db , chainConfig , engine , bc .getProcInterrupt )
@@ -381,31 +385,13 @@ func (bc *BlockChain) CurrentFastBlock() *types.Block {
381385 return bc .currentFastBlock .Load ().(* types.Block )
382386}
383387
384- // SetProcessor sets the processor required for making state modifications.
385- func (bc * BlockChain ) SetProcessor (processor Processor ) {
386- bc .procmu .Lock ()
387- defer bc .procmu .Unlock ()
388- bc .processor = processor
389- }
390-
391- // SetValidator sets the validator which is used to validate incoming blocks.
392- func (bc * BlockChain ) SetValidator (validator Validator ) {
393- bc .procmu .Lock ()
394- defer bc .procmu .Unlock ()
395- bc .validator = validator
396- }
397-
398388// Validator returns the current validator.
399389func (bc * BlockChain ) Validator () Validator {
400- bc .procmu .RLock ()
401- defer bc .procmu .RUnlock ()
402390 return bc .validator
403391}
404392
405393// Processor returns the current processor.
406394func (bc * BlockChain ) Processor () Processor {
407- bc .procmu .RLock ()
408- defer bc .procmu .RUnlock ()
409395 return bc .processor
410396}
411397
@@ -1147,7 +1133,7 @@ func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
11471133// is imported, but then new canon-head is added before the actual sidechain
11481134// completes, then the historic state could be pruned again
11491135func (bc * BlockChain ) insertChain (chain types.Blocks , verifySeals bool ) (int , []interface {}, []* types.Log , error ) {
1150- // If the chain is terminating, don't even bother starting u
1136+ // If the chain is terminating, don't even bother starting up
11511137 if atomic .LoadInt32 (& bc .procInterrupt ) == 1 {
11521138 return 0 , nil , nil , nil
11531139 }
@@ -1175,7 +1161,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
11751161 defer close (abort )
11761162
11771163 // Peek the error for the first block to decide the directing import logic
1178- it := newInsertIterator (chain , results , bc .Validator () )
1164+ it := newInsertIterator (chain , results , bc .validator )
11791165
11801166 block , err := it .next ()
11811167
@@ -1238,54 +1224,74 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
12381224 if parent == nil {
12391225 parent = bc .GetHeader (block .ParentHash (), block .NumberU64 ()- 1 )
12401226 }
1241- state , err := state .New (parent .Root , bc .stateCache )
1227+ statedb , err := state .New (parent .Root , bc .stateCache )
12421228 if err != nil {
12431229 return it .index , events , coalescedLogs , err
12441230 }
1245- // Process block using the parent state as reference point.
1231+ // If we have a followup block, run that against the current state to pre-cache
1232+ // transactions and probabilistically some of the account/storage trie nodes.
1233+ var followupInterrupt uint32
1234+
1235+ if followup , err := it .peek (); followup != nil && err == nil {
1236+ go func (start time.Time ) {
1237+ throwaway , _ := state .New (parent .Root , bc .stateCache )
1238+ bc .prefetcher .Prefetch (followup , throwaway , bc .vmConfig , & followupInterrupt )
1239+
1240+ blockPrefetchExecuteTimer .Update (time .Since (start ))
1241+ if atomic .LoadUint32 (& followupInterrupt ) == 1 {
1242+ blockPrefetchInterruptMeter .Mark (1 )
1243+ }
1244+ }(time .Now ())
1245+ }
1246+ // Process block using the parent state as reference point
12461247 substart := time .Now ()
1247- receipts , logs , usedGas , err := bc .processor .Process (block , state , bc .vmConfig )
1248+ receipts , logs , usedGas , err := bc .processor .Process (block , statedb , bc .vmConfig )
12481249 if err != nil {
12491250 bc .reportBlock (block , receipts , err )
1251+ atomic .StoreUint32 (& followupInterrupt , 1 )
12501252 return it .index , events , coalescedLogs , err
12511253 }
12521254 // Update the metrics touched during block processing
1253- accountReadTimer .Update (state .AccountReads ) // Account reads are complete, we can mark them
1254- storageReadTimer .Update (state .StorageReads ) // Storage reads are complete, we can mark them
1255- accountUpdateTimer .Update (state .AccountUpdates ) // Account updates are complete, we can mark them
1256- storageUpdateTimer .Update (state .StorageUpdates ) // Storage updates are complete, we can mark them
1255+ accountReadTimer .Update (statedb .AccountReads ) // Account reads are complete, we can mark them
1256+ storageReadTimer .Update (statedb .StorageReads ) // Storage reads are complete, we can mark them
1257+ accountUpdateTimer .Update (statedb .AccountUpdates ) // Account updates are complete, we can mark them
1258+ storageUpdateTimer .Update (statedb .StorageUpdates ) // Storage updates are complete, we can mark them
12571259
1258- triehash := state .AccountHashes + state .StorageHashes // Save to not double count in validation
1259- trieproc := state .AccountReads + state .AccountUpdates
1260- trieproc += state .StorageReads + state .StorageUpdates
1260+ triehash := statedb .AccountHashes + statedb .StorageHashes // Save to not double count in validation
1261+ trieproc := statedb .AccountReads + statedb .AccountUpdates
1262+ trieproc += statedb .StorageReads + statedb .StorageUpdates
12611263
12621264 blockExecutionTimer .Update (time .Since (substart ) - trieproc - triehash )
12631265
12641266 // Validate the state using the default validator
12651267 substart = time .Now ()
1266- if err := bc .Validator () .ValidateState (block , state , receipts , usedGas ); err != nil {
1268+ if err := bc .validator .ValidateState (block , statedb , receipts , usedGas ); err != nil {
12671269 bc .reportBlock (block , receipts , err )
1270+ atomic .StoreUint32 (& followupInterrupt , 1 )
12681271 return it .index , events , coalescedLogs , err
12691272 }
12701273 proctime := time .Since (start )
12711274
12721275 // Update the metrics touched during block validation
1273- accountHashTimer .Update (state .AccountHashes ) // Account hashes are complete, we can mark them
1274- storageHashTimer .Update (state .StorageHashes ) // Storage hashes are complete, we can mark them
1276+ accountHashTimer .Update (statedb .AccountHashes ) // Account hashes are complete, we can mark them
1277+ storageHashTimer .Update (statedb .StorageHashes ) // Storage hashes are complete, we can mark them
12751278
1276- blockValidationTimer .Update (time .Since (substart ) - (state .AccountHashes + state .StorageHashes - triehash ))
1279+ blockValidationTimer .Update (time .Since (substart ) - (statedb .AccountHashes + statedb .StorageHashes - triehash ))
12771280
12781281 // Write the block to the chain and get the status.
12791282 substart = time .Now ()
1280- status , err := bc .writeBlockWithState (block , receipts , state )
1283+ status , err := bc .writeBlockWithState (block , receipts , statedb )
12811284 if err != nil {
1285+ atomic .StoreUint32 (& followupInterrupt , 1 )
12821286 return it .index , events , coalescedLogs , err
12831287 }
1288+ atomic .StoreUint32 (& followupInterrupt , 1 )
1289+
12841290 // Update the metrics touched during block commit
1285- accountCommitTimer .Update (state .AccountCommits ) // Account commits are complete, we can mark them
1286- storageCommitTimer .Update (state .StorageCommits ) // Storage commits are complete, we can mark them
1291+ accountCommitTimer .Update (statedb .AccountCommits ) // Account commits are complete, we can mark them
1292+ storageCommitTimer .Update (statedb .StorageCommits ) // Storage commits are complete, we can mark them
12871293
1288- blockWriteTimer .Update (time .Since (substart ) - state .AccountCommits - state .StorageCommits )
1294+ blockWriteTimer .Update (time .Since (substart ) - statedb .AccountCommits - statedb .StorageCommits )
12891295 blockInsertTimer .UpdateSince (start )
12901296
12911297 switch status {
0 commit comments