Skip to content

Commit

Permalink
[refactor] put currentConfig inside TestService
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurélien Richez committed May 27, 2021
1 parent 9e703d3 commit cd61845
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 28 deletions.
30 changes: 11 additions & 19 deletions src/main/scala/io/iohk/ethereum/jsonrpc/TestService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import io.iohk.ethereum.{crypto, domain, rlp}
import io.iohk.ethereum.domain.Block._
import io.iohk.ethereum.domain.{Account, Address, Block, BlockchainImpl, UInt256}
import io.iohk.ethereum.ledger._
import io.iohk.ethereum.testmode.{TestLedgerWrapper, TestmodeConsensus}
import io.iohk.ethereum.testmode.{TestServiceProvider, TestmodeConsensus}
import io.iohk.ethereum.transactions.PendingTransactionsManager
import io.iohk.ethereum.transactions.PendingTransactionsManager.PendingTransactionsResponse
import io.iohk.ethereum.utils.{ByteStringUtils, ForkBlockNumbers, Logger}
import io.iohk.ethereum.utils.{BlockchainConfig, ByteStringUtils, ForkBlockNumbers, Logger}
import monix.eval.Task
import monix.execution.Scheduler
import org.bouncycastle.util.encoders.Hex
Expand Down Expand Up @@ -109,7 +109,8 @@ class TestService(
pendingTransactionsManager: ActorRef,
consensusConfig: ConsensusConfig,
consensus: TestmodeConsensus,
testLedgerWrapper: TestLedgerWrapper
testLedgerWrapper: TestServiceProvider,
initialConfig: BlockchainConfig
)(implicit
scheduler: Scheduler
) extends Logger {
Expand All @@ -120,18 +121,10 @@ class TestService(
private var etherbase: Address = consensusConfig.coinbase
private var accountAddresses: List[String] = List()
private var accountRangeOffset = 0
private var currentConfig: BlockchainConfig = initialConfig

def setChainParams(request: SetChainParamsRequest): ServiceResponse[SetChainParamsResponse] = {
val newBlockchainConfig = testLedgerWrapper.blockchainConfig.copy(
homesteadBlockNumber = request.chainParams.blockchainParams.homesteadForkBlock,
eip150BlockNumber = request.chainParams.blockchainParams.EIP150ForkBlock,
byzantiumBlockNumber = request.chainParams.blockchainParams.byzantiumForkBlock,
constantinopleBlockNumber = request.chainParams.blockchainParams.constantinopleForkBlock,
istanbulBlockNumber = request.chainParams.blockchainParams.istanbulForkBlock,
accountStartNonce = UInt256(request.chainParams.blockchainParams.accountStartNonce),
networkId = 1,
bootstrapNodes = Set()
)
currentConfig = buildNewConfig(request.chainParams.blockchainParams)

val genesisData = GenesisData(
nonce = request.chainParams.genesis.nonce,
Expand All @@ -153,16 +146,15 @@ class TestService(
Try(blockchain.removeBlock(blockchain.genesisHeader.hash, withState = false))

// load the new genesis
val genesisDataLoader = new GenesisDataLoader(blockchain, newBlockchainConfig)
val genesisDataLoader = new GenesisDataLoader(blockchain, currentConfig)
genesisDataLoader.loadGenesisData(genesisData)

//save account codes to world state
storeGenesisAccountCodes(genesisData.alloc)
storeGenesisAccountStorageData(genesisData.alloc)

// update test ledger with new config
testLedgerWrapper.blockchainConfig = newBlockchainConfig
consensus.blockchainConfig = newBlockchainConfig
consensus.blockchainConfig = currentConfig

accountAddresses = genesisData.alloc.keys.toList
accountRangeOffset = 0
Expand All @@ -176,7 +168,7 @@ class TestService(
val istanbulForkBlockNumber: BigInt = blockchainParams.istanbulForkBlock.getOrElse(neverOccuringBlock)

// For block number which are not specified by retesteth, we try to align the number to another fork
testLedgerWrapper.blockchainConfig.copy(
currentConfig.copy(
forkBlockNumbers = ForkBlockNumbers(
frontierBlockNumber = 0,
homesteadBlockNumber = blockchainParams.homesteadForkBlock.getOrElse(neverOccuringBlock),
Expand Down Expand Up @@ -225,7 +217,7 @@ class TestService(
def mineBlocks(request: MineBlocksRequest): ServiceResponse[MineBlocksResponse] = {
def mineBlock(): Task[Unit] = {
getBlockForMining(blockchain.getBestBlock().get)
.flatMap(blockForMining => testLedgerWrapper.ledger.importBlock(blockForMining.block))
.flatMap(blockForMining => testLedgerWrapper.ledger(currentConfig).importBlock(blockForMining.block))
.map { res =>
log.info("Block mining result: " + res)
pendingTransactionsManager ! PendingTransactionsManager.ClearPendingTransactions
Expand Down Expand Up @@ -258,7 +250,7 @@ class TestService(
Try(decode(request.blockRlp).toBlock) match {
case Failure(_) => Task.now(Left(JsonRpcError(-1, "block validation failed!", None)))
case Success(value) =>
testLedgerWrapper.ledger
testLedgerWrapper.ledger(currentConfig)
.importBlock(value)
.flatMap(handleResult)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ trait TestServiceBuilder {
with TestLedgerBuilder =>

lazy val testService =
new TestService(blockchain, pendingTransactionsManager, consensusConfig, consensus, testLedgerWrapper)(scheduler)
new TestService(blockchain, pendingTransactionsManager, consensusConfig, consensus, testLedgerWrapper, blockchainConfig)(scheduler)
}

trait TestEthBlockServiceBuilder extends EthBlocksServiceBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ trait TestLedgerBuilder extends LedgerBuilder {

val scheduler = Scheduler(system.dispatchers.lookup("validation-context"))

lazy val testLedgerWrapper: TestLedgerWrapper =
new TestLedgerWrapper(blockchain, syncConfig, consensus, blockchainConfig, scheduler)
lazy val testLedgerWrapper: TestServiceProvider =
new TestServiceProvider(blockchain, syncConfig, consensus, scheduler)

private def testLedger: Ledger = testLedgerWrapper.ledger
private def testLedger: Ledger = testLedgerWrapper.ledger(blockchainConfig)

class TestLedgerProxy extends Ledger {
override def consensus: Consensus = testLedger.consensus
Expand All @@ -35,5 +35,5 @@ trait TestLedgerBuilder extends LedgerBuilder {
}

override lazy val ledger: Ledger = new TestLedgerProxy
override lazy val stxLedger: StxLedger = testLedgerWrapper.stxLedger
override lazy val stxLedger: StxLedger = testLedgerWrapper.stxLedger(blockchainConfig)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import io.iohk.ethereum.utils.Config.SyncConfig
import monix.execution.Scheduler

/** Provides a ledger instance with modifiable blockchain config (used in test mode). */
class TestLedgerWrapper(
class TestServiceProvider(
blockchain: BlockchainImpl,
syncConfig: SyncConfig,
consensus: Consensus,
var blockchainConfig: BlockchainConfig, // var as it's modifiable by test_ RPC endpoints
validationExecutionContext: Scheduler
) {

def ledger: Ledger = new LedgerImpl(blockchain, blockchainConfig, syncConfig, consensus, validationExecutionContext)
def stxLedger: StxLedger = new StxLedger(blockchain, blockchainConfig, consensus.blockPreparator)
def ledger(blockchainConfig: BlockchainConfig): Ledger = new LedgerImpl(blockchain, blockchainConfig, syncConfig, consensus, validationExecutionContext)
def stxLedger(blockchainConfig: BlockchainConfig): StxLedger = new StxLedger(blockchain, blockchainConfig, consensus.blockPreparator)
}

0 comments on commit cd61845

Please sign in to comment.