Skip to content

[ETCM-939] refactor Ledger (by removing it) #1026

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 24 commits into from
Jun 29, 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
27 changes: 15 additions & 12 deletions src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import io.iohk.ethereum.mpt.MerklePatriciaTrie
import io.iohk.ethereum.utils.Config.SyncConfig
import io.iohk.ethereum.utils.Config
import io.iohk.ethereum.{Fixtures, Mocks, NormalPatience, ObjectGenerators, Timeouts, crypto}
import io.iohk.ethereum.ledger.Ledger.BlockResult
import io.iohk.ethereum.ledger.BlockResult
import monix.execution.Scheduler
import org.scalamock.scalatest.MockFactory
import org.scalatest.BeforeAndAfterAll
Expand All @@ -41,7 +41,7 @@ class BlockImporterItSpec
testScheduler.awaitTermination(60.second)
}

val blockQueue = BlockQueue(blockchain, SyncConfig(Config.config))
override lazy val blockQueue = BlockQueue(blockchain, SyncConfig(Config.config))

val genesis = Block(
Fixtures.Blocks.Genesis.header.copy(stateRoot = ByteString(MerklePatriciaTrie.EmptyRootHash)),
Expand Down Expand Up @@ -81,29 +81,33 @@ class BlockImporterItSpec
.validate(parentHash, blockNumber, ommers, getBlockHeaderByHash, getNBlocksBack)
}

override lazy val ledger = new TestLedgerImpl(successValidators) {
override private[ledger] lazy val blockExecution =
override lazy val blockImport = mkBlockImport(
validators = successValidators,
blockExecutionOpt = Some(
new BlockExecution(
blockchain,
blockchainReader,
storagesInstance.storages.evmCodeStorage,
blockchainConfig,
consensus.blockPreparator,
blockValidation
new BlockValidation(consensus, blockchainReader, blockQueue)
) {
override def executeAndValidateBlock(
block: Block,
alreadyValidated: Boolean = false
): Either[BlockExecutionError, Seq[Receipt]] =
Right(BlockResult(emptyWorld).receipts)
}
}
)
)
// }

val blockImporter = system.actorOf(
BlockImporter.props(
fetcherProbe.ref,
ledger,
blockImport,
blockchain,
new BranchResolution(blockchain, blockchainReader),
syncConfig,
ommersPoolProbe.ref,
broadcasterProbe.ref,
Expand Down Expand Up @@ -141,13 +145,12 @@ class BlockImporterItSpec

"BlockImporter" should "not discard blocks of the main chain if the reorganisation failed" in {

//ledger with not mocked blockExecution
val ledger = new TestLedgerImplNotMockedBlockExecution(successValidators)
val blockImporter = system.actorOf(
BlockImporter.props(
fetcherProbe.ref,
ledger,
mkBlockImport(validators = successValidators),
blockchain,
new BranchResolution(blockchain, blockchainReader),
syncConfig,
ommersPoolProbe.ref,
broadcasterProbe.ref,
Expand Down Expand Up @@ -254,12 +257,12 @@ 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 TestLedgerImplNotMockedBlockExecution(successValidators)
val blockImporter = system.actorOf(
BlockImporter.props(
fetcherProbe.ref,
ledger,
mkBlockImport(validators = successValidators),
blockchain,
new BranchResolution(blockchain, blockchainReader),
syncConfig,
ommersPoolProbe.ref,
broadcasterProbe.ref,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,21 @@ object RegularSyncItSpecUtils {
"peers-client"
)

lazy val ledger: Ledger =
new LedgerImpl(
lazy val consensus = buildEthashConsensus()

lazy val blockQueue = BlockQueue(bl, syncConfig)
lazy val blockValidation = new BlockValidation(consensus, blockchainReader, blockQueue)
lazy val blockExecution =
new BlockExecution(
bl,
blockchainReader,
storagesInstance.storages.evmCodeStorage,
blockchainConfig,
syncConfig,
buildEthashConsensus(),
Scheduler.global
consensus.blockPreparator,
blockValidation
)
lazy val blockImport: BlockImport =
new BlockImport(bl, blockchainReader, blockQueue, blockValidation, blockExecution, Scheduler.global)

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

Expand Down Expand Up @@ -123,8 +128,9 @@ object RegularSyncItSpecUtils {
lazy val blockImporter = system.actorOf(
BlockImporter.props(
fetcher.toClassic,
ledger,
blockImport,
bl,
new BranchResolution(bl, blockchainReader),
syncConfig,
ommersPool,
broadcasterRef,
Expand All @@ -138,8 +144,9 @@ object RegularSyncItSpecUtils {
peersClient,
etcPeerManager,
peerEventBus,
ledger,
blockImport,
bl,
new BranchResolution(bl, blockchainReader),
validators.blockValidator,
blacklist,
testSyncConfig,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.iohk.ethereum.txExecTest

import java.util.concurrent.Executors
import io.iohk.ethereum.domain.{BlockchainImpl, BlockchainReader, Receipt}
import io.iohk.ethereum.ledger.{BlockExecution, BlockQueue, BlockValidation}
import io.iohk.ethereum.txExecTest.util.FixtureProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.iohk.ethereum.txExecTest

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

trait ScenarioSetup extends EphemBlockchainTestSetup {
protected val testBlockchainStorages: BlockchainStorages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import io.iohk.ethereum.blockchain.sync.regular.RegularSync
import io.iohk.ethereum.consensus.validators.Validators
import io.iohk.ethereum.db.storage.{AppStateStorage, EvmCodeStorage, FastSyncStateStorage, NodeStorage}
import io.iohk.ethereum.domain.{Blockchain, BlockchainReader}
import io.iohk.ethereum.ledger.Ledger
import io.iohk.ethereum.utils.Config.SyncConfig
import io.iohk.ethereum.ledger.BlockImport
import io.iohk.ethereum.ledger.BranchResolution

class SyncController(
appStateStorage: AppStateStorage,
Expand All @@ -16,7 +17,7 @@ class SyncController(
evmCodeStorage: EvmCodeStorage,
nodeStorage: NodeStorage,
fastSyncStateStorage: FastSyncStateStorage,
ledger: Ledger,
blockImport: BlockImport,
validators: Validators,
peerEventBus: ActorRef,
pendingTransactionsManager: ActorRef,
Expand Down Expand Up @@ -104,8 +105,9 @@ class SyncController(
peersClient,
etcPeerManager,
peerEventBus,
ledger,
blockImport,
blockchain,
new BranchResolution(blockchain, blockchainReader),
validators.blockValidator,
blacklist,
syncConfig,
Expand All @@ -130,7 +132,7 @@ object SyncController {
evmCodeStorage: EvmCodeStorage,
nodeStorage: NodeStorage,
syncStateStorage: FastSyncStateStorage,
ledger: Ledger,
blockImport: BlockImport,
validators: Validators,
peerEventBus: ActorRef,
pendingTransactionsManager: ActorRef,
Expand All @@ -147,7 +149,7 @@ object SyncController {
evmCodeStorage,
nodeStorage,
syncStateStorage,
ledger,
blockImport,
validators,
peerEventBus,
pendingTransactionsManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import scala.concurrent.duration._

class BlockImporter(
fetcher: ActorRef,
ledger: Ledger,
blockImport: BlockImport,
blockchain: Blockchain,
branchResolution: BranchResolution,
syncConfig: SyncConfig,
ommersPool: ActorRef,
broadcaster: ActorRef,
Expand Down Expand Up @@ -183,7 +184,7 @@ class BlockImporter(
Task.now((importedBlocks, None))
} else {
val restOfBlocks = blocks.tail
ledger
blockImport
.importBlock(blocks.head)
.flatMap {
case BlockImportedToTop(_) =>
Expand Down Expand Up @@ -223,7 +224,7 @@ class BlockImporter(
importWith(
{
Task(doLog(importMessages.preImport()))
.flatMap(_ => ledger.importBlock(block))
.flatMap(_ => blockImport.importBlock(block))
.tap(importMessages.messageForImportResult _ andThen doLog)
.tap {
case BlockImportedToTop(importedBlocksData) =>
Expand Down Expand Up @@ -279,7 +280,7 @@ class BlockImporter(

// Either block from which we try resolve branch or list of blocks to be imported
private def resolveBranch(blocks: NonEmptyList[Block]): Either[BigInt, List[Block]] =
ledger.resolveBranch(blocks.map(_.header)) match {
branchResolution.resolveBranch(blocks.map(_.header)) match {
case NewBetterBranch(oldBranch) =>
val transactionsToAdd = oldBranch.flatMap(_.body.transactionList)
pendingTransactionsManager ! PendingTransactionsManager.AddUncheckedTransactions(transactionsToAdd)
Expand Down Expand Up @@ -315,10 +316,12 @@ class BlockImporter(
}

object BlockImporter {
// scalastyle:off parameter.number
def props(
fetcher: ActorRef,
ledger: Ledger,
blockImport: BlockImport,
blockchain: Blockchain,
branchResolution: BranchResolution,
syncConfig: SyncConfig,
ommersPool: ActorRef,
broadcaster: ActorRef,
Expand All @@ -328,8 +331,9 @@ object BlockImporter {
Props(
new BlockImporter(
fetcher,
ledger,
blockImport,
blockchain,
branchResolution,
syncConfig,
ommersPool,
broadcaster,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ import io.iohk.ethereum.blockchain.sync.SyncProtocol.Status.Progress
import io.iohk.ethereum.blockchain.sync.regular.RegularSync.{NewCheckpoint, ProgressProtocol, ProgressState}
import io.iohk.ethereum.consensus.validators.BlockValidator
import io.iohk.ethereum.domain.{Block, Blockchain}
import io.iohk.ethereum.ledger.Ledger
import io.iohk.ethereum.utils.ByteStringUtils
import io.iohk.ethereum.utils.Config.SyncConfig
import akka.actor.typed.scaladsl.adapter._
import io.iohk.ethereum.blockchain.sync.regular.BlockFetcher.InternalLastBlockImport
import io.iohk.ethereum.ledger.BranchResolution
import io.iohk.ethereum.ledger.BlockImport

class RegularSync(
peersClient: ActorRef,
etcPeerManager: ActorRef,
peerEventBus: ActorRef,
ledger: Ledger,
blockImport: BlockImport,
blockchain: Blockchain,
branchResolution: BranchResolution,
blockValidator: BlockValidator,
blacklist: Blacklist,
syncConfig: SyncConfig,
Expand All @@ -46,8 +48,9 @@ class RegularSync(
context.actorOf(
BlockImporter.props(
fetcher.toClassic,
ledger,
blockImport,
blockchain,
branchResolution,
syncConfig,
ommersPool,
broadcaster,
Expand Down Expand Up @@ -116,8 +119,9 @@ object RegularSync {
peersClient: ActorRef,
etcPeerManager: ActorRef,
peerEventBus: ActorRef,
ledger: Ledger,
blockImport: BlockImport,
blockchain: Blockchain,
branchResolution: BranchResolution,
blockValidator: BlockValidator,
blacklist: Blacklist,
syncConfig: SyncConfig,
Expand All @@ -130,8 +134,9 @@ object RegularSync {
peersClient,
etcPeerManager,
peerEventBus,
ledger,
blockImport,
blockchain,
branchResolution,
blockValidator,
blacklist,
syncConfig,
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/io/iohk/ethereum/consensus/Consensus.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.iohk.ethereum.consensus.pow.miners.MinerProtocol
import io.iohk.ethereum.consensus.pow.miners.MockedMiner.{MockedMinerProtocol, MockedMinerResponse}
import io.iohk.ethereum.consensus.validators.Validators
import io.iohk.ethereum.ledger.BlockPreparator
import io.iohk.ethereum.ledger.Ledger.VMImpl
import io.iohk.ethereum.ledger.VMImpl
import io.iohk.ethereum.nodebuilder.Node
import monix.eval.Task

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ trait StdTestConsensusBuilder
with VmConfigBuilder
with ActorSystemBuilder
with BlockchainBuilder
with BlockQueueBuilder
with BlockImportBuilder
with StorageBuilder
with BlockchainConfigBuilder
with NodeKeyBuilder
with SecureRandomBuilder
with SyncConfigBuilder
with ConsensusConfigBuilder
with ShutdownHookBuilder
with Logger
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.iohk.ethereum.db.storage.{EvmCodeStorage, StateStorage}
import io.iohk.ethereum.domain._
import io.iohk.ethereum.domain.BlockHeader.HeaderExtraFields._
import io.iohk.ethereum.consensus.pow.blocks.OmmersSeqEnc
import io.iohk.ethereum.ledger.Ledger.{BlockResult, PreparedBlock}
import io.iohk.ethereum.ledger.{BlockResult, PreparedBlock}
import io.iohk.ethereum.ledger.{BlockPreparator, BloomFilter, InMemoryWorldStateProxy}
import io.iohk.ethereum.mpt.{ByteArraySerializable, MerklePatriciaTrie}
import io.iohk.ethereum.utils.BlockchainConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import io.iohk.ethereum.db.storage.EvmCodeStorage
import io.iohk.ethereum.domain.{Blockchain, BlockchainReader, BlockchainImpl}
import io.iohk.ethereum.jsonrpc.AkkaTaskOps.TaskActorOps
import io.iohk.ethereum.ledger.BlockPreparator
import io.iohk.ethereum.ledger.Ledger.VMImpl
import io.iohk.ethereum.ledger.VMImpl
import io.iohk.ethereum.nodebuilder.Node
import io.iohk.ethereum.utils.{BlockchainConfig, Logger}
import monix.eval.Task
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait Validators {
def blockHeaderValidator: BlockHeaderValidator
def signedTransactionValidator: SignedTransactionValidator

// Note Ledger uses this in importBlock
// Note BlockImport uses this in importBlock
def validateBlockBeforeExecution(
block: Block,
getBlockHeaderByHash: GetBlockHeaderByHash,
Expand All @@ -23,8 +23,6 @@ trait Validators {
* - Doing BlockValidator.validateBlockReceipts validations involving the receipts
* - Validating the resulting gas used
*
* @note This method was originally provided by the [[io.iohk.ethereum.ledger.Ledger]].
*
* @param block to validate
* @param stateRootHash from the resulting state trie after executing the txs from the block
* @param receipts associated with the execution of each of the tx from the block
Expand Down
Loading