Skip to content

ETCM 944 Extract instantiation of InMemoryWorldStateProxy from Blockchain class #1025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ class BlockImporterItSpec
val pendingTransactionsManagerProbe = TestProbe()
val supervisor = TestProbe()

val emptyWorld: InMemoryWorldStateProxy =
blockchain.getWorldStateProxy(
-1,
UInt256.Zero,
ByteString(MerklePatriciaTrie.EmptyRootHash),
noEmptyAccounts = false,
ethCompatibleStorage = true
)
val emptyWorld = InMemoryWorldStateProxy(
storagesInstance.storages.evmCodeStorage,
blockchain.getBackingMptStorage(-1),
(number: BigInt) => blockchain.getBlockHeaderByNumber(number).map(_.hash),
blockchainConfig.accountStartNonce,
ByteString(MerklePatriciaTrie.EmptyRootHash),
noEmptyAccounts = false,
ethCompatibleStorage = true
)

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

override lazy val ledger = new TestLedgerImpl(successValidators) {
override private[ledger] lazy val blockExecution =
new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation) {
new BlockExecution(
blockchain,
storagesInstance.storages.evmCodeStorage,
blockchainConfig,
consensus.blockPreparator,
blockValidation
) {
override def executeAndValidateBlock(
block: Block,
alreadyValidated: Boolean = false
Expand Down Expand Up @@ -134,7 +141,7 @@ class BlockImporterItSpec
"BlockImporter" should "not discard blocks of the main chain if the reorganisation failed" in {

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

val ledger = new TestLedgerImpl(successValidators)
val ledger = new TestLedgerImplNotMockedBlockExecution(successValidators)
val blockImporter = system.actorOf(
BlockImporter.props(
fetcherProbe.ref,
Expand All @@ -264,7 +271,6 @@ class BlockImporterItSpec
blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(List(invalidBlock)))

eventually {

val msg = fetcherProbe
.fishForMessage(Timeouts.longTimeout) {
case BlockFetcher.FetchStateNode(_, _) => true
Expand Down
12 changes: 7 additions & 5 deletions src/it/scala/io/iohk/ethereum/sync/util/CommonFakePeer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import io.iohk.ethereum.db.components.{RocksDbDataSourceComponent, Storages}
import io.iohk.ethereum.db.dataSource.{RocksDbConfig, RocksDbDataSource}
import io.iohk.ethereum.db.storage.pruning.{ArchivePruning, PruningMode}
import io.iohk.ethereum.db.storage.{AppStateStorage, Namespaces}
import io.iohk.ethereum.domain.{Block, Blockchain, BlockchainImpl, ChainWeight}
import io.iohk.ethereum.domain.{Block, Blockchain, BlockchainImpl, ChainWeight, UInt256}
import io.iohk.ethereum.security.SecureRandomBuilder
import io.iohk.ethereum.ledger.InMemoryWorldStateProxy
import io.iohk.ethereum.mpt.MerklePatriciaTrie
Expand Down Expand Up @@ -241,10 +241,12 @@ abstract class CommonFakePeer(peerName: String, fakePeerCustomConfig: FakePeerCu
)

private def getMptForBlock(block: Block) = {
bl.getWorldStateProxy(
blockNumber = block.number,
accountStartNonce = blockchainConfig.accountStartNonce,
stateRootHash = block.header.stateRoot,
InMemoryWorldStateProxy(
storagesInstance.storages.evmCodeStorage,
bl.getBackingMptStorage(block.number),
(number: BigInt) => bl.getBlockHeaderByNumber(number).map(_.hash),
blockchainConfig.accountStartNonce,
block.header.stateRoot,
noEmptyAccounts = EvmConfig.forBlock(block.number, blockchainConfig).noEmptyAccounts,
ethCompatibleStorage = blockchainConfig.ethCompatibleStorage
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ import io.iohk.ethereum.sync.util.SyncCommonItSpecUtils.FakePeerCustomConfig.def
import io.iohk.ethereum.sync.util.SyncCommonItSpecUtils._
import io.iohk.ethereum.transactions.PendingTransactionsManager
import io.iohk.ethereum.utils._
import io.iohk.ethereum.vm.EvmConfig
import monix.eval.Task
import monix.execution.Scheduler
import akka.actor.typed.scaladsl.adapter._
import io.iohk.ethereum.blockchain.sync.regular.BlockFetcher.AdaptedMessageFromEventBus
import io.iohk.ethereum.mpt.MerklePatriciaTrie

import scala.concurrent.duration._
object RegularSyncItSpecUtils {
Expand All @@ -61,7 +61,15 @@ object RegularSyncItSpecUtils {
val fullConfig = FullConsensusConfig(consensusConfig, specificConfig)
val vm = VmSetup.vm(VmConfig(config), blockchainConfig, testMode = false)
val consensus =
PoWConsensus(vm, bl, blockchainConfig, fullConfig, ValidatorsExecutorAlwaysSucceed, NoAdditionalPoWData)
PoWConsensus(
vm,
storagesInstance.storages.evmCodeStorage,
bl,
blockchainConfig,
fullConfig,
ValidatorsExecutorAlwaysSucceed,
NoAdditionalPoWData
)
consensus
}

Expand All @@ -73,7 +81,14 @@ object RegularSyncItSpecUtils {
)

lazy val ledger: Ledger =
new LedgerImpl(bl, blockchainConfig, syncConfig, buildEthashConsensus(), Scheduler.global)
new LedgerImpl(
bl,
storagesInstance.storages.evmCodeStorage,
blockchainConfig,
syncConfig,
buildEthashConsensus(),
Scheduler.global
)

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

Expand Down Expand Up @@ -197,12 +212,14 @@ object RegularSyncItSpecUtils {
}

private def getMptForBlock(block: Block) = {
bl.getWorldStateProxy(
blockNumber = block.number,
accountStartNonce = blockchainConfig.accountStartNonce,
stateRootHash = block.header.stateRoot,
noEmptyAccounts = EvmConfig.forBlock(block.number, blockchainConfig).noEmptyAccounts,
ethCompatibleStorage = blockchainConfig.ethCompatibleStorage
InMemoryWorldStateProxy(
storagesInstance.storages.evmCodeStorage,
bl.getBackingMptStorage(block.number),
(number: BigInt) => bl.getBlockHeaderByNumber(number).map(_.hash),
UInt256.Zero,
ByteString(MerklePatriciaTrie.EmptyRootHash),
noEmptyAccounts = false,
ethCompatibleStorage = true
)
}

Expand Down
57 changes: 17 additions & 40 deletions src/it/scala/io/iohk/ethereum/txExecTest/ContractTest.scala
Original file line number Diff line number Diff line change
@@ -1,64 +1,41 @@
package io.iohk.ethereum.txExecTest

import java.util.concurrent.Executors

import io.iohk.ethereum.domain.Receipt
import io.iohk.ethereum.ledger.{BlockExecution, BlockQueue, BlockValidation, Ledger}
import io.iohk.ethereum.domain.{BlockchainImpl, Receipt}
import io.iohk.ethereum.ledger.{BlockExecution, BlockQueue, BlockValidation}
import io.iohk.ethereum.txExecTest.util.FixtureProvider
import io.iohk.ethereum.utils.Config
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import scala.concurrent.ExecutionContext

class ContractTest extends AnyFlatSpec with Matchers {
val blockchainConfig = Config.blockchains.blockchainConfig
val syncConfig = Config.SyncConfig(Config.config)
val vm = new Ledger.VMImpl
val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(4))
val noErrors = a[Right[_, Seq[Receipt]]]

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

val testBlockchainStorages = FixtureProvider.prepareStorages(0, fixtures)
override lazy val blockchain = BlockchainImpl(testBlockchainStorages)

//block only with ether transfers
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
val blockExecution = new BlockExecution(
blockchain,
testBlockchainStorages.evmCodeStorage,
blockchainConfig,
consensus.blockPreparator,
blockValidation
)

// transfer ether
blockExecution.executeAndValidateBlock(fixtures.blockByNumber(1)) shouldBe noErrors
}

it should "deploy contract" in new ScenarioSetup {
val fixtures: FixtureProvider.Fixture = FixtureProvider.loadFixtures("/txExecTest/purchaseContract")

val testBlockchainStorages = FixtureProvider.prepareStorages(1, fixtures)

//contract creation
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
// deploy contract
blockExecution.executeAndValidateBlock(fixtures.blockByNumber(2)) shouldBe noErrors
}

it should "execute contract call" in new ScenarioSetup {
val fixtures: FixtureProvider.Fixture = FixtureProvider.loadFixtures("/txExecTest/purchaseContract")

val testBlockchainStorages = FixtureProvider.prepareStorages(2, fixtures)

//block with ether transfers and contract call
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
blockExecution.executeAndValidateBlock(fixtures.blockByNumber(3)) shouldBe noErrors
}

it should "execute contract that pays 2 accounts" in new ScenarioSetup {
val fixtures: FixtureProvider.Fixture = FixtureProvider.loadFixtures("/txExecTest/purchaseContract")

val testBlockchainStorages = FixtureProvider.prepareStorages(2, fixtures)

//block contains contract paying 2 accounts
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
// execute contract call
// execute contract that pays 2 accounts
blockExecution.executeAndValidateBlock(fixtures.blockByNumber(3)) shouldBe noErrors
}
}
23 changes: 10 additions & 13 deletions src/it/scala/io/iohk/ethereum/txExecTest/ECIP1017Test.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package io.iohk.ethereum.txExecTest

import java.util.concurrent.Executors
import io.iohk.ethereum.domain.{Address, BlockchainImpl, Receipt, UInt256}
import io.iohk.ethereum.ledger._
import io.iohk.ethereum.domain.{Address, Receipt, UInt256}
import io.iohk.ethereum.ledger.{BlockExecution, BlockQueue, BlockValidation}
import io.iohk.ethereum.txExecTest.util.FixtureProvider
import io.iohk.ethereum.utils.{BlockchainConfig, ForkBlockNumbers, MonetaryPolicyConfig}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import scala.concurrent.ExecutionContext

class ECIP1017Test extends AnyFlatSpec with Matchers {

val EraDuration = 3
Expand Down Expand Up @@ -53,13 +50,9 @@ class ECIP1017Test extends AnyFlatSpec with Matchers {
gasTieBreaker = false,
treasuryAddress = Address(0)
)
val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(4))

val noErrors = a[Right[_, Seq[Receipt]]]
}

val vm = new Ledger.VMImpl

/**
* Tests the block reward calculation through out all the monetary policy through all the eras till block
* mining reward goes to zero. Block mining reward is tested till era 200 (that starts at block number 602)
Expand All @@ -72,13 +65,17 @@ class ECIP1017Test extends AnyFlatSpec with Matchers {
val startBlock = 1
val endBlock = 602

protected val testBlockchainStorages = FixtureProvider.prepareStorages(startBlock, fixtures)
protected val testBlockchainStorages = FixtureProvider.prepareStorages(endBlock, fixtures)

(startBlock to endBlock) foreach { blockToExecute =>
val storages = FixtureProvider.prepareStorages(blockToExecute - 1, fixtures)
val blockchain = BlockchainImpl(storages)
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
val blockExecution = new BlockExecution(
blockchain,
testBlockchainStorages.evmCodeStorage,
blockchainConfig,
consensus.blockPreparator,
blockValidation
)
blockExecution.executeAndValidateBlock(fixtures.blockByNumber(blockToExecute)) shouldBe noErrors
}
}
Expand Down
20 changes: 9 additions & 11 deletions src/it/scala/io/iohk/ethereum/txExecTest/ForksTest.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package io.iohk.ethereum.txExecTest

import java.util.concurrent.Executors
import io.iohk.ethereum.domain.{Address, BlockchainImpl, Receipt, UInt256}
import io.iohk.ethereum.domain.{Address, Receipt, UInt256}
import io.iohk.ethereum.ledger.{BlockExecution, BlockQueue, BlockValidation}
import io.iohk.ethereum.txExecTest.util.FixtureProvider
import io.iohk.ethereum.utils.{BlockchainConfig, ForkBlockNumbers, MonetaryPolicyConfig}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import scala.concurrent.ExecutionContext

// scalastyle:off magic.number
class ForksTest extends AnyFlatSpec with Matchers {

trait TestSetup extends ScenarioSetup {
Expand Down Expand Up @@ -52,9 +48,7 @@ class ForksTest extends AnyFlatSpec with Matchers {
ethCompatibleStorage = true,
treasuryAddress = Address(0)
)

val noErrors = a[Right[_, Seq[Receipt]]]
val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(4))
}

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

protected val testBlockchainStorages = FixtureProvider.prepareStorages(startBlock, fixtures)
protected val testBlockchainStorages = FixtureProvider.prepareStorages(endBlock, fixtures)

(startBlock to endBlock) foreach { blockToExecute =>
val storages = FixtureProvider.prepareStorages(blockToExecute - 1, fixtures)
val blockchain = BlockchainImpl(storages)
val blockValidation = new BlockValidation(consensus, blockchain, BlockQueue(blockchain, syncConfig))
val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation)
val blockExecution = new BlockExecution(
blockchain,
testBlockchainStorages.evmCodeStorage,
blockchainConfig,
consensus.blockPreparator,
blockValidation
)
blockExecution.executeAndValidateBlock(fixtures.blockByNumber(blockToExecute)) shouldBe noErrors
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/it/scala/io/iohk/ethereum/txExecTest/ScenarioSetup.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package io.iohk.ethereum.txExecTest

import io.iohk.ethereum.blockchain.sync
import io.iohk.ethereum.blockchain.sync.EphemBlockchainTestSetup
import io.iohk.ethereum.domain.{BlockchainImpl, BlockchainStorages}
import io.iohk.ethereum.ledger.Ledger.VMImpl

trait ScenarioSetup extends sync.ScenarioSetup {
trait ScenarioSetup extends EphemBlockchainTestSetup {
protected val testBlockchainStorages: BlockchainStorages

override lazy val blockchain: BlockchainImpl = BlockchainImpl(testBlockchainStorages)
override lazy val vm: VMImpl = new VMImpl
}
22 changes: 5 additions & 17 deletions src/it/scala/io/iohk/ethereum/txExecTest/util/DumpChainApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.iohk.ethereum.db.dataSource.{DataSourceBatchUpdate, RocksDbDataSource}
import io.iohk.ethereum.db.storage.NodeStorage.{NodeEncoded, NodeHash}
import io.iohk.ethereum.db.storage.TransactionMappingStorage.TransactionLocation
import io.iohk.ethereum.db.storage.pruning.{ArchivePruning, PruningMode}
import io.iohk.ethereum.db.storage.{AppStateStorage, StateStorage}
import io.iohk.ethereum.db.storage.{AppStateStorage, MptStorage, StateStorage}
import io.iohk.ethereum.domain.BlockHeader.HeaderExtraFields.HefEmpty
import io.iohk.ethereum.domain.{Blockchain, UInt256, _}
import io.iohk.ethereum.jsonrpc.ProofService.{EmptyStorageValueProof, StorageProof, StorageProofKey, StorageValueProof}
Expand Down Expand Up @@ -190,22 +190,6 @@ class BlockchainMock(genesisHash: ByteString) extends Blockchain {
override type S = InMemoryWorldStateProxyStorage
override type WS = InMemoryWorldStateProxy

override def getWorldStateProxy(
blockNumber: BigInt,
accountStartNonce: UInt256,
stateRootHash: ByteString,
noEmptyAccounts: Boolean,
ethCompatibleStorage: Boolean
): InMemoryWorldStateProxy = ???

override def getReadOnlyWorldStateProxy(
blockNumber: Option[BigInt],
accountStartNonce: UInt256,
stateRootHash: ByteString,
noEmptyAccounts: Boolean,
ethCompatibleStorage: Boolean
): InMemoryWorldStateProxy = ???

def getBestBlockNumber(): BigInt = ???

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

override def getLatestCheckpointBlockNumber(): BigInt = ???

override def getBackingMptStorage(blockNumber: BigInt): MptStorage = ???

override def getReadOnlyMptStorage(): MptStorage = ???
}
Loading