Skip to content

Commit b4e451a

Browse files
author
Leonor Boga
committed
ETCM-944 - Refactoring - extract methods getWorldStateProxy and getReadOnlyWorldStateProxy from Blockchain
1 parent 70cf7cf commit b4e451a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+585
-385
lines changed

src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ class BlockImporterItSpec
5959
val pendingTransactionsManagerProbe = TestProbe()
6060
val supervisor = TestProbe()
6161

62-
val emptyWorld: InMemoryWorldStateProxy =
63-
blockchain.getWorldStateProxy(
64-
-1,
65-
UInt256.Zero,
66-
ByteString(MerklePatriciaTrie.EmptyRootHash),
67-
noEmptyAccounts = false,
68-
ethCompatibleStorage = true
69-
)
62+
val emptyWorld = InMemoryWorldStateProxy(
63+
storagesInstance.storages.evmCodeStorage,
64+
blockchain.getBackingStorage(-1),
65+
blockchain,
66+
blockchainConfig.accountStartNonce,
67+
ByteString(MerklePatriciaTrie.EmptyRootHash),
68+
noEmptyAccounts = false,
69+
ethCompatibleStorage = true
70+
)
7071

7172
override protected lazy val successValidators: Validators = new Mocks.MockValidatorsAlwaysSucceed {
7273
override val ommersValidator: OmmersValidator = (
@@ -82,7 +83,13 @@ class BlockImporterItSpec
8283

8384
override lazy val ledger = new TestLedgerImpl(successValidators) {
8485
override private[ledger] lazy val blockExecution =
85-
new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation) {
86+
new BlockExecution(
87+
blockchain,
88+
storagesInstance.storages.evmCodeStorage,
89+
blockchainConfig,
90+
consensus.blockPreparator,
91+
blockValidation
92+
) {
8693
override def executeAndValidateBlock(
8794
block: Block,
8895
alreadyValidated: Boolean = false

src/it/scala/io/iohk/ethereum/sync/util/CommonFakePeer.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import io.iohk.ethereum.db.components.{RocksDbDataSourceComponent, Storages}
1414
import io.iohk.ethereum.db.dataSource.{RocksDbConfig, RocksDbDataSource}
1515
import io.iohk.ethereum.db.storage.pruning.{ArchivePruning, PruningMode}
1616
import io.iohk.ethereum.db.storage.{AppStateStorage, Namespaces}
17-
import io.iohk.ethereum.domain.{Block, Blockchain, BlockchainImpl, ChainWeight}
17+
import io.iohk.ethereum.domain.{Block, Blockchain, BlockchainImpl, ChainWeight, UInt256}
1818
import io.iohk.ethereum.security.SecureRandomBuilder
1919
import io.iohk.ethereum.ledger.InMemoryWorldStateProxy
2020
import io.iohk.ethereum.mpt.MerklePatriciaTrie
@@ -241,10 +241,12 @@ abstract class CommonFakePeer(peerName: String, fakePeerCustomConfig: FakePeerCu
241241
)
242242

243243
private def getMptForBlock(block: Block) = {
244-
bl.getWorldStateProxy(
245-
blockNumber = block.number,
246-
accountStartNonce = blockchainConfig.accountStartNonce,
247-
stateRootHash = block.header.stateRoot,
244+
InMemoryWorldStateProxy(
245+
storagesInstance.storages.evmCodeStorage,
246+
bl.getBackingStorage(block.number),
247+
bl,
248+
blockchainConfig.accountStartNonce,
249+
block.header.stateRoot,
248250
noEmptyAccounts = EvmConfig.forBlock(block.number, blockchainConfig).noEmptyAccounts,
249251
ethCompatibleStorage = blockchainConfig.ethCompatibleStorage
250252
)

src/it/scala/io/iohk/ethereum/sync/util/RegularSyncItSpecUtils.scala

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ import io.iohk.ethereum.sync.util.SyncCommonItSpecUtils.FakePeerCustomConfig.def
3232
import io.iohk.ethereum.sync.util.SyncCommonItSpecUtils._
3333
import io.iohk.ethereum.transactions.PendingTransactionsManager
3434
import io.iohk.ethereum.utils._
35-
import io.iohk.ethereum.vm.EvmConfig
3635
import monix.eval.Task
3736
import monix.execution.Scheduler
3837
import akka.actor.typed.scaladsl.adapter._
3938
import io.iohk.ethereum.blockchain.sync.regular.BlockFetcher.AdaptedMessageFromEventBus
39+
import io.iohk.ethereum.mpt.MerklePatriciaTrie
4040

4141
import scala.concurrent.duration._
4242
object RegularSyncItSpecUtils {
@@ -61,7 +61,15 @@ object RegularSyncItSpecUtils {
6161
val fullConfig = FullConsensusConfig(consensusConfig, specificConfig)
6262
val vm = VmSetup.vm(VmConfig(config), blockchainConfig, testMode = false)
6363
val consensus =
64-
PoWConsensus(vm, bl, blockchainConfig, fullConfig, ValidatorsExecutorAlwaysSucceed, NoAdditionalPoWData)
64+
PoWConsensus(
65+
vm,
66+
storagesInstance.storages.evmCodeStorage,
67+
bl,
68+
blockchainConfig,
69+
fullConfig,
70+
ValidatorsExecutorAlwaysSucceed,
71+
NoAdditionalPoWData
72+
)
6573
consensus
6674
}
6775

@@ -73,7 +81,14 @@ object RegularSyncItSpecUtils {
7381
)
7482

7583
lazy val ledger: Ledger =
76-
new LedgerImpl(bl, blockchainConfig, syncConfig, buildEthashConsensus(), Scheduler.global)
84+
new LedgerImpl(
85+
bl,
86+
storagesInstance.storages.evmCodeStorage,
87+
blockchainConfig,
88+
syncConfig,
89+
buildEthashConsensus(),
90+
Scheduler.global
91+
)
7792

7893
lazy val ommersPool: ActorRef = system.actorOf(OmmersPool.props(bl, 1), "ommers-pool")
7994

@@ -197,12 +212,14 @@ object RegularSyncItSpecUtils {
197212
}
198213

199214
private def getMptForBlock(block: Block) = {
200-
bl.getWorldStateProxy(
201-
blockNumber = block.number,
202-
accountStartNonce = blockchainConfig.accountStartNonce,
203-
stateRootHash = block.header.stateRoot,
204-
noEmptyAccounts = EvmConfig.forBlock(block.number, blockchainConfig).noEmptyAccounts,
205-
ethCompatibleStorage = blockchainConfig.ethCompatibleStorage
215+
InMemoryWorldStateProxy(
216+
storagesInstance.storages.evmCodeStorage,
217+
bl.getBackingStorage(block.number),
218+
bl,
219+
UInt256.Zero,
220+
ByteString(MerklePatriciaTrie.EmptyRootHash),
221+
noEmptyAccounts = false,
222+
ethCompatibleStorage = true
206223
)
207224
}
208225

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package io.iohk.ethereum.txExecTest
22

33
import java.util.concurrent.Executors
4-
5-
import io.iohk.ethereum.domain.Receipt
4+
import io.iohk.ethereum.domain.{BlockchainImpl, BlockchainStorages, Receipt}
65
import io.iohk.ethereum.ledger.{BlockExecution, BlockQueue, BlockValidation, Ledger}
76
import io.iohk.ethereum.txExecTest.util.FixtureProvider
87
import io.iohk.ethereum.utils.Config
@@ -18,47 +17,28 @@ class ContractTest extends AnyFlatSpec with Matchers {
1817
val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(4))
1918
val noErrors = a[Right[_, Seq[Receipt]]]
2019

21-
"Ledger" should "transfer ether" in new ScenarioSetup {
20+
"Ledger" should "execute and validate" in new ScenarioSetup {
2221
val fixtures: FixtureProvider.Fixture = FixtureProvider.loadFixtures("/txExecTest/purchaseContract")
23-
24-
val testBlockchainStorages = FixtureProvider.prepareStorages(0, fixtures)
22+
val testBlockchainStorages = FixtureProvider.prepareStorages(2, fixtures)
2523

2624
//block only with ether transfers
2725
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
28-
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
26+
val blockExecution = new BlockExecution(
27+
blockchain,
28+
testBlockchainStorages.evmCodeStorage,
29+
blockchainConfig,
30+
consensus.blockPreparator,
31+
blockValidation
32+
)
33+
34+
// transfer ether
2935
blockExecution.executeAndValidateBlock(fixtures.blockByNumber(1)) shouldBe noErrors
30-
}
31-
32-
it should "deploy contract" in new ScenarioSetup {
33-
val fixtures: FixtureProvider.Fixture = FixtureProvider.loadFixtures("/txExecTest/purchaseContract")
3436

35-
val testBlockchainStorages = FixtureProvider.prepareStorages(1, fixtures)
36-
37-
//contract creation
38-
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
39-
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
37+
// deploy contract
4038
blockExecution.executeAndValidateBlock(fixtures.blockByNumber(2)) shouldBe noErrors
41-
}
42-
43-
it should "execute contract call" in new ScenarioSetup {
44-
val fixtures: FixtureProvider.Fixture = FixtureProvider.loadFixtures("/txExecTest/purchaseContract")
4539

46-
val testBlockchainStorages = FixtureProvider.prepareStorages(2, fixtures)
47-
48-
//block with ether transfers and contract call
49-
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
50-
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
51-
blockExecution.executeAndValidateBlock(fixtures.blockByNumber(3)) shouldBe noErrors
52-
}
53-
54-
it should "execute contract that pays 2 accounts" in new ScenarioSetup {
55-
val fixtures: FixtureProvider.Fixture = FixtureProvider.loadFixtures("/txExecTest/purchaseContract")
56-
57-
val testBlockchainStorages = FixtureProvider.prepareStorages(2, fixtures)
58-
59-
//block contains contract paying 2 accounts
60-
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
61-
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
40+
// execute contract call
41+
// execute contract that pays 2 accounts
6242
blockExecution.executeAndValidateBlock(fixtures.blockByNumber(3)) shouldBe noErrors
6343
}
6444
}

src/it/scala/io/iohk/ethereum/txExecTest/ECIP1017Test.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ class ECIP1017Test extends AnyFlatSpec with Matchers {
7878
val storages = FixtureProvider.prepareStorages(blockToExecute - 1, fixtures)
7979
val blockchain = BlockchainImpl(storages)
8080
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
81-
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
81+
val blockExecution = new BlockExecution(
82+
blockchain,
83+
testBlockchainStorages.evmCodeStorage,
84+
blockchainConfig,
85+
consensus.blockPreparator,
86+
blockValidation
87+
)
8288
blockExecution.executeAndValidateBlock(fixtures.blockByNumber(blockToExecute)) shouldBe noErrors
8389
}
8490
}

src/it/scala/io/iohk/ethereum/txExecTest/ForksTest.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ class ForksTest extends AnyFlatSpec with Matchers {
6969
val storages = FixtureProvider.prepareStorages(blockToExecute - 1, fixtures)
7070
val blockchain = BlockchainImpl(storages)
7171
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
72-
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
72+
val blockExecution = new BlockExecution(
73+
blockchain,
74+
testBlockchainStorages.evmCodeStorage,
75+
blockchainConfig,
76+
consensus.blockPreparator,
77+
blockValidation
78+
)
7379
blockExecution.executeAndValidateBlock(fixtures.blockByNumber(blockToExecute)) shouldBe noErrors
7480
}
7581
}

src/it/scala/io/iohk/ethereum/txExecTest/util/DumpChainApp.scala

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import io.iohk.ethereum.db.dataSource.{DataSourceBatchUpdate, RocksDbDataSource}
1212
import io.iohk.ethereum.db.storage.NodeStorage.{NodeEncoded, NodeHash}
1313
import io.iohk.ethereum.db.storage.TransactionMappingStorage.TransactionLocation
1414
import io.iohk.ethereum.db.storage.pruning.{ArchivePruning, PruningMode}
15-
import io.iohk.ethereum.db.storage.{AppStateStorage, StateStorage}
15+
import io.iohk.ethereum.db.storage.{AppStateStorage, MptStorage, StateStorage}
1616
import io.iohk.ethereum.domain.BlockHeader.HeaderExtraFields.HefEmpty
1717
import io.iohk.ethereum.domain.{Blockchain, UInt256, _}
1818
import io.iohk.ethereum.jsonrpc.ProofService.{EmptyStorageValueProof, StorageProof, StorageProofKey, StorageValueProof}
@@ -190,22 +190,6 @@ class BlockchainMock(genesisHash: ByteString) extends Blockchain {
190190
override type S = InMemoryWorldStateProxyStorage
191191
override type WS = InMemoryWorldStateProxy
192192

193-
override def getWorldStateProxy(
194-
blockNumber: BigInt,
195-
accountStartNonce: UInt256,
196-
stateRootHash: ByteString,
197-
noEmptyAccounts: Boolean,
198-
ethCompatibleStorage: Boolean
199-
): InMemoryWorldStateProxy = ???
200-
201-
override def getReadOnlyWorldStateProxy(
202-
blockNumber: Option[BigInt],
203-
accountStartNonce: UInt256,
204-
stateRootHash: ByteString,
205-
noEmptyAccounts: Boolean,
206-
ethCompatibleStorage: Boolean
207-
): InMemoryWorldStateProxy = ???
208-
209193
def getBestBlockNumber(): BigInt = ???
210194

211195
def saveBlockNumber(number: BigInt, hash: NodeHash): Unit = ???
@@ -217,4 +201,8 @@ class BlockchainMock(genesisHash: ByteString) extends Blockchain {
217201
override def save(block: Block, receipts: Seq[Receipt], weight: ChainWeight, saveAsBestBlock: Boolean): Unit = ???
218202

219203
override def getLatestCheckpointBlockNumber(): BigInt = ???
204+
205+
override def getBackingStorage(blockNumber: BigInt): MptStorage = ???
206+
207+
override def getReadOnlyStorage(): MptStorage = ???
220208
}

src/it/scala/io/iohk/ethereum/txExecTest/util/FixtureProvider.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ object FixtureProvider {
6565
val blocksToInclude = fixtures.blockByNumber.toSeq.sortBy { case (number, _) => number }.takeWhile {
6666
case (number, _) => number <= blockNumber
6767
}
68-
val blockchain = BlockchainImpl(storages)
6968

7069
blocksToInclude.foreach { case (_, block) =>
7170
val receiptsUpdates = fixtures.receipts
7271
.get(block.header.hash)
7372
.map(r => storages.receiptStorage.put(block.header.hash, r))
7473
.getOrElse(storages.receiptStorage.emptyBatchUpdate)
74+
7575
storages.blockBodiesStorage
7676
.put(block.header.hash, fixtures.blockBodies(block.header.hash))
7777
.and(storages.blockHeadersStorage.put(block.header.hash, fixtures.blockHeaders(block.header.hash)))

src/main/scala/io/iohk/ethereum/consensus/ConsensusBuilder.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ trait ConsensusBuilder {
1919
*/
2020
trait StdConsensusBuilder extends ConsensusBuilder {
2121
self: VmBuilder
22+
with StorageBuilder
2223
with BlockchainBuilder
2324
with BlockchainConfigBuilder
2425
with ConsensusConfigBuilder
@@ -43,7 +44,15 @@ trait StdConsensusBuilder extends ConsensusBuilder {
4344
case Protocol.PoW | Protocol.MockedPow => NoAdditionalPoWData
4445
case Protocol.RestrictedPoW => RestrictedPoWMinerData(nodeKey)
4546
}
46-
val consensus = PoWConsensus(vm, blockchain, blockchainConfig, fullConfig, validators, additionalPoWData)
47+
val consensus = PoWConsensus(
48+
vm,
49+
storagesInstance.storages.evmCodeStorage,
50+
blockchain,
51+
blockchainConfig,
52+
fullConfig,
53+
validators,
54+
additionalPoWData
55+
)
4756
consensus
4857
}
4958

src/main/scala/io/iohk/ethereum/consensus/blocks/BlockGeneratorSkeleton.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import io.iohk.ethereum.consensus.pow.blocks.Ommers
88
import io.iohk.ethereum.consensus.validators.std.MptListValidator.intByteArraySerializable
99
import io.iohk.ethereum.crypto.kec256
1010
import io.iohk.ethereum.db.dataSource.EphemDataSource
11-
import io.iohk.ethereum.db.storage.StateStorage
11+
import io.iohk.ethereum.db.storage.{EvmCodeStorage, StateStorage}
1212
import io.iohk.ethereum.domain._
1313
import io.iohk.ethereum.domain.BlockHeader.HeaderExtraFields._
1414
import io.iohk.ethereum.consensus.pow.blocks.OmmersSeqEnc
@@ -22,7 +22,6 @@ import io.iohk.ethereum.utils.ByteUtils.or
2222
* This is a skeleton for a generic [[io.iohk.ethereum.consensus.blocks.BlockGenerator BlockGenerator]].
2323
*/
2424
abstract class BlockGeneratorSkeleton(
25-
blockchain: Blockchain,
2625
blockchainConfig: BlockchainConfig,
2726
consensusConfig: ConsensusConfig,
2827
difficultyCalc: DifficultyCalculator,
@@ -82,6 +81,7 @@ abstract class BlockGeneratorSkeleton(
8281
): BlockHeader
8382

8483
protected def prepareBlock(
84+
evmCodeStorage: EvmCodeStorage,
8585
parent: Block,
8686
transactions: Seq[SignedTransaction],
8787
beneficiary: Address,
@@ -97,7 +97,7 @@ abstract class BlockGeneratorSkeleton(
9797
val body = newBlockBody(transactionsForBlock, x)
9898
val block = Block(header, body)
9999

100-
blockPreparator.prepareBlock(block, parent.header, initialWorldStateBeforeExecution) match {
100+
blockPreparator.prepareBlock(evmCodeStorage, block, parent.header, initialWorldStateBeforeExecution) match {
101101
case PreparedBlock(prepareBlock, BlockResult(_, gasUsed, receipts), stateRoot, updatedWorld) =>
102102
val receiptsLogs: Seq[Array[Byte]] =
103103
BloomFilter.EmptyBloomFilter.toArray +: receipts.map(_.logsBloomFilter.toArray)

src/main/scala/io/iohk/ethereum/consensus/blocks/NoOmmersBlockGenerator.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import io.iohk.ethereum.domain._
66
import io.iohk.ethereum.ledger.{BlockPreparator, InMemoryWorldStateProxy}
77
import io.iohk.ethereum.utils.BlockchainConfig
88
import io.iohk.ethereum.consensus.ConsensusMetrics
9+
import io.iohk.ethereum.db.storage.EvmCodeStorage
910

1011
abstract class NoOmmersBlockGenerator(
11-
blockchain: Blockchain,
12+
evmCodeStorage: EvmCodeStorage,
1213
blockchainConfig: BlockchainConfig,
1314
consensusConfig: ConsensusConfig,
1415
blockPreparator: BlockPreparator,
1516
difficultyCalc: DifficultyCalculator,
1617
blockTimestampProvider: BlockTimestampProvider = DefaultBlockTimestampProvider
1718
) extends BlockGeneratorSkeleton(
18-
blockchain,
1919
blockchainConfig,
2020
consensusConfig,
2121
difficultyCalc,
@@ -50,7 +50,16 @@ abstract class NoOmmersBlockGenerator(
5050
val blockNumber = pHeader.number + 1
5151

5252
val prepared =
53-
prepareBlock(parent, transactions, beneficiary, blockNumber, blockPreparator, x, initialWorldStateBeforeExecution)
53+
prepareBlock(
54+
evmCodeStorage,
55+
parent,
56+
transactions,
57+
beneficiary,
58+
blockNumber,
59+
blockPreparator,
60+
x,
61+
initialWorldStateBeforeExecution
62+
)
5463
cache.updateAndGet((t: List[PendingBlockAndState]) => (prepared :: t).take(blockCacheSize))
5564

5665
prepared

0 commit comments

Comments
 (0)