@@ -178,17 +178,21 @@ trait Blockchain {
178
178
179
179
def genesisBlock : Block = getBlockByNumber(0 ).get
180
180
181
- def getWorldStateProxy (blockNumber : BigInt ,
182
- accountStartNonce : UInt256 ,
183
- stateRootHash : Option [ByteString ],
184
- noEmptyAccounts : Boolean ,
185
- ethCompatibleStorage : Boolean ): WS
186
-
187
- def getReadOnlyWorldStateProxy (blockNumber : Option [BigInt ],
188
- accountStartNonce : UInt256 ,
189
- stateRootHash : Option [ByteString ],
190
- noEmptyAccounts : Boolean ,
191
- ethCompatibleStorage : Boolean ): WS
181
+ def getWorldStateProxy (
182
+ blockNumber : BigInt ,
183
+ accountStartNonce : UInt256 ,
184
+ stateRootHash : Option [ByteString ],
185
+ noEmptyAccounts : Boolean ,
186
+ ethCompatibleStorage : Boolean
187
+ ): WS
188
+
189
+ def getReadOnlyWorldStateProxy (
190
+ blockNumber : Option [BigInt ],
191
+ accountStartNonce : UInt256 ,
192
+ stateRootHash : Option [ByteString ],
193
+ noEmptyAccounts : Boolean ,
194
+ ethCompatibleStorage : Boolean
195
+ ): WS
192
196
193
197
def getStateStorage : StateStorage
194
198
}
@@ -259,7 +263,11 @@ class BlockchainImpl(
259
263
mpt.get(address)
260
264
}
261
265
262
- override def getAccountStorageAt (rootHash : ByteString , position : BigInt , ethCompatibleStorage : Boolean ): ByteString = {
266
+ override def getAccountStorageAt (
267
+ rootHash : ByteString ,
268
+ position : BigInt ,
269
+ ethCompatibleStorage : Boolean
270
+ ): ByteString = {
263
271
val storage = stateStorage.getBackingStorage(0 )
264
272
val mpt =
265
273
if (ethCompatibleStorage) domain.EthereumUInt256Mpt .storageMpt(rootHash, storage)
@@ -268,7 +276,8 @@ class BlockchainImpl(
268
276
}
269
277
270
278
private def persistBestBlocksData (): Unit = {
271
- appStateStorage.putBestBlockNumber(getBestBlockNumber())
279
+ appStateStorage
280
+ .putBestBlockNumber(getBestBlockNumber())
272
281
.and(appStateStorage.putLatestCheckpointBlockNumber(getLatestCheckpointBlockNumber()))
273
282
.commit()
274
283
}
@@ -298,7 +307,8 @@ class BlockchainImpl(
298
307
override def getMptNodeByHash (hash : ByteString ): Option [MptNode ] =
299
308
stateStorage.getNode(hash)
300
309
301
- override def getTransactionLocation (txHash : ByteString ): Option [TransactionLocation ] = transactionMappingStorage.get(txHash)
310
+ override def getTransactionLocation (txHash : ByteString ): Option [TransactionLocation ] =
311
+ transactionMappingStorage.get(txHash)
302
312
303
313
override def storeBlockBody (blockHash : ByteString , blockBody : BlockBody ): DataSourceBatchUpdate = {
304
314
blockBodiesStorage.put(blockHash, blockBody).and(saveTxsLocations(blockHash, blockBody))
@@ -351,14 +361,15 @@ class BlockchainImpl(
351
361
val bestBlocks = bestKnownBlockAndLatestCheckpoint.get()
352
362
// as we are decreasing block numbers in memory more often than in storage,
353
363
// we can't use here getBestBlockNumber / getLatestCheckpointBlockNumber
354
- val bestBlockNumber = if (bestBlocks.bestBlockNumber != 0 ) bestBlocks.bestBlockNumber else appStateStorage.getBestBlockNumber()
364
+ val bestBlockNumber =
365
+ if (bestBlocks.bestBlockNumber != 0 ) bestBlocks.bestBlockNumber else appStateStorage.getBestBlockNumber()
355
366
val latestCheckpointNumber = {
356
- if (bestBlocks.latestCheckpointNumber != 0 ) bestBlocks.latestCheckpointNumber
367
+ if (bestBlocks.latestCheckpointNumber != 0 ) bestBlocks.latestCheckpointNumber
357
368
else appStateStorage.getLatestCheckpointBlockNumber()
358
369
}
359
370
360
371
val blockNumberMappingUpdates = {
361
- maybeBlockHeader.fold(blockNumberMappingStorage.emptyBatchUpdate)( h =>
372
+ maybeBlockHeader.fold(blockNumberMappingStorage.emptyBatchUpdate)(h =>
362
373
if (getHashByBlockNumber(h.number).contains(blockHash))
363
374
removeBlockNumberMapping(h.number)
364
375
else blockNumberMappingStorage.emptyBatchUpdate
@@ -369,19 +380,22 @@ class BlockchainImpl(
369
380
case Some (header) =>
370
381
if (header.hasCheckpoint && header.number == latestCheckpointNumber) {
371
382
val prev = findPreviousCheckpointBlockNumber(header.number, header.number)
372
- prev.map { num =>
373
- (appStateStorage.putLatestCheckpointBlockNumber(num), Some (num))
374
- }.getOrElse {
375
- (appStateStorage.removeLatestCheckpointBlockNumber(), Some (0 ))
376
- }
383
+ prev
384
+ .map { num =>
385
+ (appStateStorage.putLatestCheckpointBlockNumber(num), Some (num))
386
+ }
387
+ .getOrElse {
388
+ (appStateStorage.removeLatestCheckpointBlockNumber(), Some (0 ))
389
+ }
377
390
} else (appStateStorage.emptyBatchUpdate, None )
378
391
case None =>
379
392
(appStateStorage.emptyBatchUpdate, None )
380
393
}
381
394
382
- val newBestBlockNumber : BigInt = if (bestBlockNumber >= 1 ) bestBlockNumber - 1 else 0
395
+ val newBestBlockNumber : BigInt = if (bestBlockNumber >= 1 ) bestBlockNumber - 1 else 0
383
396
384
- blockHeadersStorage.remove(blockHash)
397
+ blockHeadersStorage
398
+ .remove(blockHash)
385
399
.and(blockBodiesStorage.remove(blockHash))
386
400
.and(totalDifficultyStorage.remove(blockHash))
387
401
.and(receiptStorage.remove(blockHash))
@@ -394,7 +408,8 @@ class BlockchainImpl(
394
408
395
409
maybeBlockHeader.foreach { h =>
396
410
if (withState) {
397
- val bestBlocksUpdates = appStateStorage.putBestBlockNumber(newBestBlockNumber)
411
+ val bestBlocksUpdates = appStateStorage
412
+ .putBestBlockNumber(newBestBlockNumber)
398
413
.and(checkpointUpdates)
399
414
stateStorage.onBlockRollback(h.number, bestBlockNumber)(() => bestBlocksUpdates.commit())
400
415
}
@@ -408,8 +423,8 @@ class BlockchainImpl(
408
423
*/
409
424
@ tailrec
410
425
private def findPreviousCheckpointBlockNumber (
411
- blockNumberToCheck : BigInt ,
412
- latestCheckpointBlockNumber : BigInt
426
+ blockNumberToCheck : BigInt ,
427
+ latestCheckpointBlockNumber : BigInt
413
428
): Option [BigInt ] = {
414
429
if (blockNumberToCheck > 0 ) {
415
430
val maybePreviousCheckpointBlockNumber = for {
@@ -432,19 +447,21 @@ class BlockchainImpl(
432
447
}
433
448
434
449
private def removeTxsLocations (stxs : Seq [SignedTransaction ]): DataSourceBatchUpdate = {
435
- stxs.map(_.hash).foldLeft(transactionMappingStorage.emptyBatchUpdate) {
436
- case (updates, hash) => updates.and(transactionMappingStorage.remove(hash))
450
+ stxs.map(_.hash).foldLeft(transactionMappingStorage.emptyBatchUpdate) { case (updates, hash) =>
451
+ updates.and(transactionMappingStorage.remove(hash))
437
452
}
438
453
}
439
454
440
455
override type S = InMemoryWorldStateProxyStorage
441
456
override type WS = InMemoryWorldStateProxy
442
457
443
- override def getWorldStateProxy (blockNumber : BigInt ,
444
- accountStartNonce : UInt256 ,
445
- stateRootHash : Option [ByteString ],
446
- noEmptyAccounts : Boolean ,
447
- ethCompatibleStorage : Boolean ): InMemoryWorldStateProxy =
458
+ override def getWorldStateProxy (
459
+ blockNumber : BigInt ,
460
+ accountStartNonce : UInt256 ,
461
+ stateRootHash : Option [ByteString ],
462
+ noEmptyAccounts : Boolean ,
463
+ ethCompatibleStorage : Boolean
464
+ ): InMemoryWorldStateProxy =
448
465
InMemoryWorldStateProxy (
449
466
evmCodeStorage,
450
467
stateStorage.getBackingStorage(blockNumber),
@@ -456,18 +473,20 @@ class BlockchainImpl(
456
473
)
457
474
458
475
// FIXME Maybe we can use this one in regular execution too and persist underlying storage when block execution is successful
459
- override def getReadOnlyWorldStateProxy (blockNumber : Option [BigInt ],
460
- accountStartNonce : UInt256 ,
461
- stateRootHash : Option [ByteString ],
462
- noEmptyAccounts : Boolean ,
463
- ethCompatibleStorage : Boolean ): InMemoryWorldStateProxy =
476
+ override def getReadOnlyWorldStateProxy (
477
+ blockNumber : Option [BigInt ],
478
+ accountStartNonce : UInt256 ,
479
+ stateRootHash : Option [ByteString ],
480
+ noEmptyAccounts : Boolean ,
481
+ ethCompatibleStorage : Boolean
482
+ ): InMemoryWorldStateProxy =
464
483
InMemoryWorldStateProxy (
465
484
evmCodeStorage,
466
485
stateStorage.getReadOnlyStorage,
467
486
accountStartNonce,
468
487
(number : BigInt ) => getBlockHeaderByNumber(number).map(_.hash),
469
488
stateRootHash,
470
- noEmptyAccounts = false ,
489
+ noEmptyAccounts = noEmptyAccounts ,
471
490
ethCompatibleStorage = ethCompatibleStorage
472
491
)
473
492
0 commit comments