Skip to content

Commit 67d7870

Browse files
author
Leonor Boga
authored
ETCM 944 Extract instantiation of InMemoryWorldStateProxy from Blockchain class (#1025)
* ETCM-944 - Refactoring - extract methods getWorldStateProxy and getReadOnlyWorldStateProxy from Blockchain * ETCM-944 Rename test class to SchedulerStateSpec
1 parent 127524c commit 67d7870

Some content is hidden

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

51 files changed

+612
-432
lines changed

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

Lines changed: 18 additions & 12 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.getBackingMptStorage(-1),
65+
(number: BigInt) => blockchain.getBlockHeaderByNumber(number).map(_.hash),
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
@@ -134,7 +141,7 @@ class BlockImporterItSpec
134141
"BlockImporter" should "not discard blocks of the main chain if the reorganisation failed" in {
135142

136143
//ledger with not mocked blockExecution
137-
val ledger = new TestLedgerImpl(successValidators)
144+
val ledger = new TestLedgerImplNotMockedBlockExecution(successValidators)
138145
val blockImporter = system.actorOf(
139146
BlockImporter.props(
140147
fetcherProbe.ref,
@@ -246,7 +253,7 @@ class BlockImporterItSpec
246253
val newBlock: Block = getBlock(genesisBlock.number + 5, difficulty = 104, parent = parent.header.hash)
247254
val invalidBlock = newBlock.copy(header = newBlock.header.copy(beneficiary = Address(111).bytes))
248255

249-
val ledger = new TestLedgerImpl(successValidators)
256+
val ledger = new TestLedgerImplNotMockedBlockExecution(successValidators)
250257
val blockImporter = system.actorOf(
251258
BlockImporter.props(
252259
fetcherProbe.ref,
@@ -264,7 +271,6 @@ class BlockImporterItSpec
264271
blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(List(invalidBlock)))
265272

266273
eventually {
267-
268274
val msg = fetcherProbe
269275
.fishForMessage(Timeouts.longTimeout) {
270276
case BlockFetcher.FetchStateNode(_, _) => true

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.getBackingMptStorage(block.number),
247+
(number: BigInt) => bl.getBlockHeaderByNumber(number).map(_.hash),
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.getBackingMptStorage(block.number),
218+
(number: BigInt) => bl.getBlockHeaderByNumber(number).map(_.hash),
219+
UInt256.Zero,
220+
ByteString(MerklePatriciaTrie.EmptyRootHash),
221+
noEmptyAccounts = false,
222+
ethCompatibleStorage = true
206223
)
207224
}
208225

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,41 @@
11
package io.iohk.ethereum.txExecTest
22

3-
import java.util.concurrent.Executors
4-
5-
import io.iohk.ethereum.domain.Receipt
6-
import io.iohk.ethereum.ledger.{BlockExecution, BlockQueue, BlockValidation, Ledger}
3+
import io.iohk.ethereum.domain.{BlockchainImpl, Receipt}
4+
import io.iohk.ethereum.ledger.{BlockExecution, BlockQueue, BlockValidation}
75
import io.iohk.ethereum.txExecTest.util.FixtureProvider
86
import io.iohk.ethereum.utils.Config
97
import org.scalatest.flatspec.AnyFlatSpec
108
import org.scalatest.matchers.should.Matchers
119

12-
import scala.concurrent.ExecutionContext
13-
1410
class ContractTest extends AnyFlatSpec with Matchers {
1511
val blockchainConfig = Config.blockchains.blockchainConfig
1612
val syncConfig = Config.SyncConfig(Config.config)
17-
val vm = new Ledger.VMImpl
18-
val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(4))
1913
val noErrors = a[Right[_, Seq[Receipt]]]
2014

21-
"Ledger" should "transfer ether" in new ScenarioSetup {
15+
"Ledger" should "execute and validate" in new ScenarioSetup {
2216
val fixtures: FixtureProvider.Fixture = FixtureProvider.loadFixtures("/txExecTest/purchaseContract")
17+
lazy val testBlockchainStorages = FixtureProvider.prepareStorages(2, fixtures)
2318

24-
val testBlockchainStorages = FixtureProvider.prepareStorages(0, fixtures)
19+
override lazy val blockchain = BlockchainImpl(testBlockchainStorages)
2520

2621
//block only with ether transfers
2722
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
28-
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
23+
val blockExecution = new BlockExecution(
24+
blockchain,
25+
testBlockchainStorages.evmCodeStorage,
26+
blockchainConfig,
27+
consensus.blockPreparator,
28+
blockValidation
29+
)
30+
31+
// transfer ether
2932
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")
3433

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)
34+
// deploy contract
4035
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")
4536

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)
37+
// execute contract call
38+
// execute contract that pays 2 accounts
6239
blockExecution.executeAndValidateBlock(fixtures.blockByNumber(3)) shouldBe noErrors
6340
}
6441
}

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

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

3-
import java.util.concurrent.Executors
4-
import io.iohk.ethereum.domain.{Address, BlockchainImpl, Receipt, UInt256}
5-
import io.iohk.ethereum.ledger._
3+
import io.iohk.ethereum.domain.{Address, Receipt, UInt256}
4+
import io.iohk.ethereum.ledger.{BlockExecution, BlockQueue, BlockValidation}
65
import io.iohk.ethereum.txExecTest.util.FixtureProvider
76
import io.iohk.ethereum.utils.{BlockchainConfig, ForkBlockNumbers, MonetaryPolicyConfig}
87
import org.scalatest.flatspec.AnyFlatSpec
98
import org.scalatest.matchers.should.Matchers
109

11-
import scala.concurrent.ExecutionContext
12-
1310
class ECIP1017Test extends AnyFlatSpec with Matchers {
1411

1512
val EraDuration = 3
@@ -53,13 +50,9 @@ class ECIP1017Test extends AnyFlatSpec with Matchers {
5350
gasTieBreaker = false,
5451
treasuryAddress = Address(0)
5552
)
56-
val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(4))
57-
5853
val noErrors = a[Right[_, Seq[Receipt]]]
5954
}
6055

61-
val vm = new Ledger.VMImpl
62-
6356
/**
6457
* Tests the block reward calculation through out all the monetary policy through all the eras till block
6558
* mining reward goes to zero. Block mining reward is tested till era 200 (that starts at block number 602)
@@ -72,13 +65,17 @@ class ECIP1017Test extends AnyFlatSpec with Matchers {
7265
val startBlock = 1
7366
val endBlock = 602
7467

75-
protected val testBlockchainStorages = FixtureProvider.prepareStorages(startBlock, fixtures)
68+
protected val testBlockchainStorages = FixtureProvider.prepareStorages(endBlock, fixtures)
7669

7770
(startBlock to endBlock) foreach { blockToExecute =>
78-
val storages = FixtureProvider.prepareStorages(blockToExecute - 1, fixtures)
79-
val blockchain = BlockchainImpl(storages)
8071
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
81-
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+
)
8279
blockExecution.executeAndValidateBlock(fixtures.blockByNumber(blockToExecute)) shouldBe noErrors
8380
}
8481
}

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package io.iohk.ethereum.txExecTest
22

3-
import java.util.concurrent.Executors
4-
import io.iohk.ethereum.domain.{Address, BlockchainImpl, Receipt, UInt256}
3+
import io.iohk.ethereum.domain.{Address, Receipt, UInt256}
54
import io.iohk.ethereum.ledger.{BlockExecution, BlockQueue, BlockValidation}
65
import io.iohk.ethereum.txExecTest.util.FixtureProvider
76
import io.iohk.ethereum.utils.{BlockchainConfig, ForkBlockNumbers, MonetaryPolicyConfig}
87
import org.scalatest.flatspec.AnyFlatSpec
98
import org.scalatest.matchers.should.Matchers
109

11-
import scala.concurrent.ExecutionContext
12-
13-
// scalastyle:off magic.number
1410
class ForksTest extends AnyFlatSpec with Matchers {
1511

1612
trait TestSetup extends ScenarioSetup {
@@ -52,9 +48,7 @@ class ForksTest extends AnyFlatSpec with Matchers {
5248
ethCompatibleStorage = true,
5349
treasuryAddress = Address(0)
5450
)
55-
5651
val noErrors = a[Right[_, Seq[Receipt]]]
57-
val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(4))
5852
}
5953

6054
"Ledger" should "execute blocks with respect to forks" in new TestSetup {
@@ -63,13 +57,17 @@ class ForksTest extends AnyFlatSpec with Matchers {
6357
val startBlock = 1
6458
val endBlock = 11
6559

66-
protected val testBlockchainStorages = FixtureProvider.prepareStorages(startBlock, fixtures)
60+
protected val testBlockchainStorages = FixtureProvider.prepareStorages(endBlock, fixtures)
6761

6862
(startBlock to endBlock) foreach { blockToExecute =>
69-
val storages = FixtureProvider.prepareStorages(blockToExecute - 1, fixtures)
70-
val blockchain = BlockchainImpl(storages)
7163
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
72-
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
64+
val blockExecution = new BlockExecution(
65+
blockchain,
66+
testBlockchainStorages.evmCodeStorage,
67+
blockchainConfig,
68+
consensus.blockPreparator,
69+
blockValidation
70+
)
7371
blockExecution.executeAndValidateBlock(fixtures.blockByNumber(blockToExecute)) shouldBe noErrors
7472
}
7573
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package io.iohk.ethereum.txExecTest
22

3-
import io.iohk.ethereum.blockchain.sync
3+
import io.iohk.ethereum.blockchain.sync.EphemBlockchainTestSetup
44
import io.iohk.ethereum.domain.{BlockchainImpl, BlockchainStorages}
55
import io.iohk.ethereum.ledger.Ledger.VMImpl
66

7-
trait ScenarioSetup extends sync.ScenarioSetup {
7+
trait ScenarioSetup extends EphemBlockchainTestSetup {
88
protected val testBlockchainStorages: BlockchainStorages
9-
109
override lazy val blockchain: BlockchainImpl = BlockchainImpl(testBlockchainStorages)
1110
override lazy val vm: VMImpl = new VMImpl
1211
}

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 getBackingMptStorage(blockNumber: BigInt): MptStorage = ???
206+
207+
override def getReadOnlyMptStorage(): MptStorage = ???
220208
}

0 commit comments

Comments
 (0)