Skip to content

Commit e3f1c38

Browse files
authored
[ETCM-939] refactor Ledger (by removing it) (#1026)
1 parent e631789 commit e3f1c38

File tree

75 files changed

+1105
-1429
lines changed

Some content is hidden

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

75 files changed

+1105
-1429
lines changed

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import io.iohk.ethereum.mpt.MerklePatriciaTrie
1515
import io.iohk.ethereum.utils.Config.SyncConfig
1616
import io.iohk.ethereum.utils.Config
1717
import io.iohk.ethereum.{Fixtures, Mocks, NormalPatience, ObjectGenerators, Timeouts, crypto}
18-
import io.iohk.ethereum.ledger.Ledger.BlockResult
18+
import io.iohk.ethereum.ledger.BlockResult
1919
import monix.execution.Scheduler
2020
import org.scalamock.scalatest.MockFactory
2121
import org.scalatest.BeforeAndAfterAll
@@ -41,7 +41,7 @@ class BlockImporterItSpec
4141
testScheduler.awaitTermination(60.second)
4242
}
4343

44-
val blockQueue = BlockQueue(blockchain, SyncConfig(Config.config))
44+
override lazy val blockQueue = BlockQueue(blockchain, SyncConfig(Config.config))
4545

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

84-
override lazy val ledger = new TestLedgerImpl(successValidators) {
85-
override private[ledger] lazy val blockExecution =
84+
override lazy val blockImport = mkBlockImport(
85+
validators = successValidators,
86+
blockExecutionOpt = Some(
8687
new BlockExecution(
8788
blockchain,
8889
blockchainReader,
8990
storagesInstance.storages.evmCodeStorage,
9091
blockchainConfig,
9192
consensus.blockPreparator,
92-
blockValidation
93+
new BlockValidation(consensus, blockchainReader, blockQueue)
9394
) {
9495
override def executeAndValidateBlock(
9596
block: Block,
9697
alreadyValidated: Boolean = false
9798
): Either[BlockExecutionError, Seq[Receipt]] =
9899
Right(BlockResult(emptyWorld).receipts)
99100
}
100-
}
101+
)
102+
)
103+
// }
101104

102105
val blockImporter = system.actorOf(
103106
BlockImporter.props(
104107
fetcherProbe.ref,
105-
ledger,
108+
blockImport,
106109
blockchain,
110+
new BranchResolution(blockchain, blockchainReader),
107111
syncConfig,
108112
ommersPoolProbe.ref,
109113
broadcasterProbe.ref,
@@ -141,13 +145,12 @@ class BlockImporterItSpec
141145

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

144-
//ledger with not mocked blockExecution
145-
val ledger = new TestLedgerImplNotMockedBlockExecution(successValidators)
146148
val blockImporter = system.actorOf(
147149
BlockImporter.props(
148150
fetcherProbe.ref,
149-
ledger,
151+
mkBlockImport(validators = successValidators),
150152
blockchain,
153+
new BranchResolution(blockchain, blockchainReader),
151154
syncConfig,
152155
ommersPoolProbe.ref,
153156
broadcasterProbe.ref,
@@ -254,12 +257,12 @@ class BlockImporterItSpec
254257
val newBlock: Block = getBlock(genesisBlock.number + 5, difficulty = 104, parent = parent.header.hash)
255258
val invalidBlock = newBlock.copy(header = newBlock.header.copy(beneficiary = Address(111).bytes))
256259

257-
val ledger = new TestLedgerImplNotMockedBlockExecution(successValidators)
258260
val blockImporter = system.actorOf(
259261
BlockImporter.props(
260262
fetcherProbe.ref,
261-
ledger,
263+
mkBlockImport(validators = successValidators),
262264
blockchain,
265+
new BranchResolution(blockchain, blockchainReader),
263266
syncConfig,
264267
ommersPoolProbe.ref,
265268
broadcasterProbe.ref,

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,21 @@ object RegularSyncItSpecUtils {
8181
"peers-client"
8282
)
8383

84-
lazy val ledger: Ledger =
85-
new LedgerImpl(
84+
lazy val consensus = buildEthashConsensus()
85+
86+
lazy val blockQueue = BlockQueue(bl, syncConfig)
87+
lazy val blockValidation = new BlockValidation(consensus, blockchainReader, blockQueue)
88+
lazy val blockExecution =
89+
new BlockExecution(
8690
bl,
8791
blockchainReader,
8892
storagesInstance.storages.evmCodeStorage,
8993
blockchainConfig,
90-
syncConfig,
91-
buildEthashConsensus(),
92-
Scheduler.global
94+
consensus.blockPreparator,
95+
blockValidation
9396
)
97+
lazy val blockImport: BlockImport =
98+
new BlockImport(bl, blockchainReader, blockQueue, blockValidation, blockExecution, Scheduler.global)
9499

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

@@ -123,8 +128,9 @@ object RegularSyncItSpecUtils {
123128
lazy val blockImporter = system.actorOf(
124129
BlockImporter.props(
125130
fetcher.toClassic,
126-
ledger,
131+
blockImport,
127132
bl,
133+
new BranchResolution(bl, blockchainReader),
128134
syncConfig,
129135
ommersPool,
130136
broadcasterRef,
@@ -138,8 +144,9 @@ object RegularSyncItSpecUtils {
138144
peersClient,
139145
etcPeerManager,
140146
peerEventBus,
141-
ledger,
147+
blockImport,
142148
bl,
149+
new BranchResolution(bl, blockchainReader),
143150
validators.blockValidator,
144151
blacklist,
145152
testSyncConfig,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.iohk.ethereum.txExecTest
22

3+
import java.util.concurrent.Executors
34
import io.iohk.ethereum.domain.{BlockchainImpl, BlockchainReader, Receipt}
45
import io.iohk.ethereum.ledger.{BlockExecution, BlockQueue, BlockValidation}
56
import io.iohk.ethereum.txExecTest.util.FixtureProvider

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package io.iohk.ethereum.txExecTest
22

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

77
trait ScenarioSetup extends EphemBlockchainTestSetup {
88
protected val testBlockchainStorages: BlockchainStorages

src/main/scala/io/iohk/ethereum/blockchain/sync/SyncController.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import io.iohk.ethereum.blockchain.sync.regular.RegularSync
66
import io.iohk.ethereum.consensus.validators.Validators
77
import io.iohk.ethereum.db.storage.{AppStateStorage, EvmCodeStorage, FastSyncStateStorage, NodeStorage}
88
import io.iohk.ethereum.domain.{Blockchain, BlockchainReader}
9-
import io.iohk.ethereum.ledger.Ledger
109
import io.iohk.ethereum.utils.Config.SyncConfig
10+
import io.iohk.ethereum.ledger.BlockImport
11+
import io.iohk.ethereum.ledger.BranchResolution
1112

1213
class SyncController(
1314
appStateStorage: AppStateStorage,
@@ -16,7 +17,7 @@ class SyncController(
1617
evmCodeStorage: EvmCodeStorage,
1718
nodeStorage: NodeStorage,
1819
fastSyncStateStorage: FastSyncStateStorage,
19-
ledger: Ledger,
20+
blockImport: BlockImport,
2021
validators: Validators,
2122
peerEventBus: ActorRef,
2223
pendingTransactionsManager: ActorRef,
@@ -104,8 +105,9 @@ class SyncController(
104105
peersClient,
105106
etcPeerManager,
106107
peerEventBus,
107-
ledger,
108+
blockImport,
108109
blockchain,
110+
new BranchResolution(blockchain, blockchainReader),
109111
validators.blockValidator,
110112
blacklist,
111113
syncConfig,
@@ -130,7 +132,7 @@ object SyncController {
130132
evmCodeStorage: EvmCodeStorage,
131133
nodeStorage: NodeStorage,
132134
syncStateStorage: FastSyncStateStorage,
133-
ledger: Ledger,
135+
blockImport: BlockImport,
134136
validators: Validators,
135137
peerEventBus: ActorRef,
136138
pendingTransactionsManager: ActorRef,
@@ -147,7 +149,7 @@ object SyncController {
147149
evmCodeStorage,
148150
nodeStorage,
149151
syncStateStorage,
150-
ledger,
152+
blockImport,
151153
validators,
152154
peerEventBus,
153155
pendingTransactionsManager,

src/main/scala/io/iohk/ethereum/blockchain/sync/regular/BlockImporter.scala

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ import scala.concurrent.duration._
2626

2727
class BlockImporter(
2828
fetcher: ActorRef,
29-
ledger: Ledger,
29+
blockImport: BlockImport,
3030
blockchain: Blockchain,
31+
branchResolution: BranchResolution,
3132
syncConfig: SyncConfig,
3233
ommersPool: ActorRef,
3334
broadcaster: ActorRef,
@@ -183,7 +184,7 @@ class BlockImporter(
183184
Task.now((importedBlocks, None))
184185
} else {
185186
val restOfBlocks = blocks.tail
186-
ledger
187+
blockImport
187188
.importBlock(blocks.head)
188189
.flatMap {
189190
case BlockImportedToTop(_) =>
@@ -223,7 +224,7 @@ class BlockImporter(
223224
importWith(
224225
{
225226
Task(doLog(importMessages.preImport()))
226-
.flatMap(_ => ledger.importBlock(block))
227+
.flatMap(_ => blockImport.importBlock(block))
227228
.tap(importMessages.messageForImportResult _ andThen doLog)
228229
.tap {
229230
case BlockImportedToTop(importedBlocksData) =>
@@ -279,7 +280,7 @@ class BlockImporter(
279280

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

317318
object BlockImporter {
319+
// scalastyle:off parameter.number
318320
def props(
319321
fetcher: ActorRef,
320-
ledger: Ledger,
322+
blockImport: BlockImport,
321323
blockchain: Blockchain,
324+
branchResolution: BranchResolution,
322325
syncConfig: SyncConfig,
323326
ommersPool: ActorRef,
324327
broadcaster: ActorRef,
@@ -328,8 +331,9 @@ object BlockImporter {
328331
Props(
329332
new BlockImporter(
330333
fetcher,
331-
ledger,
334+
blockImport,
332335
blockchain,
336+
branchResolution,
333337
syncConfig,
334338
ommersPool,
335339
broadcaster,

src/main/scala/io/iohk/ethereum/blockchain/sync/regular/RegularSync.scala

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@ import io.iohk.ethereum.blockchain.sync.SyncProtocol.Status.Progress
88
import io.iohk.ethereum.blockchain.sync.regular.RegularSync.{NewCheckpoint, ProgressProtocol, ProgressState}
99
import io.iohk.ethereum.consensus.validators.BlockValidator
1010
import io.iohk.ethereum.domain.{Block, Blockchain}
11-
import io.iohk.ethereum.ledger.Ledger
1211
import io.iohk.ethereum.utils.ByteStringUtils
1312
import io.iohk.ethereum.utils.Config.SyncConfig
1413
import akka.actor.typed.scaladsl.adapter._
1514
import io.iohk.ethereum.blockchain.sync.regular.BlockFetcher.InternalLastBlockImport
15+
import io.iohk.ethereum.ledger.BranchResolution
16+
import io.iohk.ethereum.ledger.BlockImport
1617

1718
class RegularSync(
1819
peersClient: ActorRef,
1920
etcPeerManager: ActorRef,
2021
peerEventBus: ActorRef,
21-
ledger: Ledger,
22+
blockImport: BlockImport,
2223
blockchain: Blockchain,
24+
branchResolution: BranchResolution,
2325
blockValidator: BlockValidator,
2426
blacklist: Blacklist,
2527
syncConfig: SyncConfig,
@@ -46,8 +48,9 @@ class RegularSync(
4648
context.actorOf(
4749
BlockImporter.props(
4850
fetcher.toClassic,
49-
ledger,
51+
blockImport,
5052
blockchain,
53+
branchResolution,
5154
syncConfig,
5255
ommersPool,
5356
broadcaster,
@@ -116,8 +119,9 @@ object RegularSync {
116119
peersClient: ActorRef,
117120
etcPeerManager: ActorRef,
118121
peerEventBus: ActorRef,
119-
ledger: Ledger,
122+
blockImport: BlockImport,
120123
blockchain: Blockchain,
124+
branchResolution: BranchResolution,
121125
blockValidator: BlockValidator,
122126
blacklist: Blacklist,
123127
syncConfig: SyncConfig,
@@ -130,8 +134,9 @@ object RegularSync {
130134
peersClient,
131135
etcPeerManager,
132136
peerEventBus,
133-
ledger,
137+
blockImport,
134138
blockchain,
139+
branchResolution,
135140
blockValidator,
136141
blacklist,
137142
syncConfig,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import io.iohk.ethereum.consensus.pow.miners.MinerProtocol
66
import io.iohk.ethereum.consensus.pow.miners.MockedMiner.{MockedMinerProtocol, MockedMinerResponse}
77
import io.iohk.ethereum.consensus.validators.Validators
88
import io.iohk.ethereum.ledger.BlockPreparator
9-
import io.iohk.ethereum.ledger.Ledger.VMImpl
9+
import io.iohk.ethereum.ledger.VMImpl
1010
import io.iohk.ethereum.nodebuilder.Node
1111
import monix.eval.Task
1212

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ trait StdTestConsensusBuilder
2121
with VmConfigBuilder
2222
with ActorSystemBuilder
2323
with BlockchainBuilder
24+
with BlockQueueBuilder
25+
with BlockImportBuilder
2426
with StorageBuilder
2527
with BlockchainConfigBuilder
2628
with NodeKeyBuilder
2729
with SecureRandomBuilder
30+
with SyncConfigBuilder
2831
with ConsensusConfigBuilder
2932
with ShutdownHookBuilder
3033
with Logger

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ 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
15-
import io.iohk.ethereum.ledger.Ledger.{BlockResult, PreparedBlock}
15+
import io.iohk.ethereum.ledger.{BlockResult, PreparedBlock}
1616
import io.iohk.ethereum.ledger.{BlockPreparator, BloomFilter, InMemoryWorldStateProxy}
1717
import io.iohk.ethereum.mpt.{ByteArraySerializable, MerklePatriciaTrie}
1818
import io.iohk.ethereum.utils.BlockchainConfig

src/main/scala/io/iohk/ethereum/consensus/pow/PoWConsensus.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import io.iohk.ethereum.db.storage.EvmCodeStorage
1919
import io.iohk.ethereum.domain.{Blockchain, BlockchainReader, BlockchainImpl}
2020
import io.iohk.ethereum.jsonrpc.AkkaTaskOps.TaskActorOps
2121
import io.iohk.ethereum.ledger.BlockPreparator
22-
import io.iohk.ethereum.ledger.Ledger.VMImpl
22+
import io.iohk.ethereum.ledger.VMImpl
2323
import io.iohk.ethereum.nodebuilder.Node
2424
import io.iohk.ethereum.utils.{BlockchainConfig, Logger}
2525
import monix.eval.Task

src/main/scala/io/iohk/ethereum/consensus/validators/Validators.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ trait Validators {
1111
def blockHeaderValidator: BlockHeaderValidator
1212
def signedTransactionValidator: SignedTransactionValidator
1313

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

0 commit comments

Comments
 (0)