diff --git a/src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala b/src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala index 33908ea701..bde35b871f 100644 --- a/src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala +++ b/src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala @@ -107,7 +107,7 @@ class BlockImporterItSpec blockchainWriter.save(oldBlock3, Nil, oldWeight3, saveAsBestBlock = true) blockchainWriter.save(oldBlock4, Nil, oldWeight4, saveAsBestBlock = true) // simulation of node restart - blockchain.saveBestKnownBlocks(blockchainReader.getBestBlockNumber() - 1) + blockchain.saveBestKnownBlocks(oldBlock3.header.hash, oldBlock3.number) blockchainWriter.save(newBlock4ParentOldBlock3, Nil, newBlock4WeightParentOldBlock3, saveAsBestBlock = true) //not reorganising anymore until oldBlock4(not part of the chain anymore), no block/ommer validation when not part of the chain, resolveBranch is returning UnknownBranch diff --git a/src/it/scala/io/iohk/ethereum/txExecTest/util/DumpChainApp.scala b/src/it/scala/io/iohk/ethereum/txExecTest/util/DumpChainApp.scala index 14138276b0..cf91caefef 100644 --- a/src/it/scala/io/iohk/ethereum/txExecTest/util/DumpChainApp.scala +++ b/src/it/scala/io/iohk/ethereum/txExecTest/util/DumpChainApp.scala @@ -196,7 +196,11 @@ class BlockchainMock(genesisHash: ByteString) extends Blockchain { def getBestBlockNumber(): BigInt = ??? - def saveBestKnownBlocks(bestBlockNumber: BigInt, latestCheckpointNumber: Option[BigInt] = None): Unit = ??? + override def saveBestKnownBlocks( + bestBlockhash: ByteString, + bestBlockNumber: BigInt, + latestCheckpointNumber: Option[BigInt] = None + ): Unit = ??? def getBestBlock(): Option[Block] = ??? @@ -205,4 +209,5 @@ class BlockchainMock(genesisHash: ByteString) extends Blockchain { override def getBackingMptStorage(blockNumber: BigInt): MptStorage = ??? override def getReadOnlyMptStorage(): MptStorage = ??? + } diff --git a/src/main/scala/io/iohk/ethereum/blockchain/sync/fast/FastSync.scala b/src/main/scala/io/iohk/ethereum/blockchain/sync/fast/FastSync.scala index c425ce22c1..23c412bf21 100644 --- a/src/main/scala/io/iohk/ethereum/blockchain/sync/fast/FastSync.scala +++ b/src/main/scala/io/iohk/ethereum/blockchain/sync/fast/FastSync.scala @@ -941,7 +941,7 @@ class FastSync( } def assignBlockchainWork(peerWithInfo: PeerWithInfo): Unit = { - val PeerWithInfo(peer, peerInfo) = peerWithInfo + val PeerWithInfo(peer, _) = peerWithInfo log.debug(s"Assigning blockchain work for peer [{}]", peer.id.value) if (syncState.receiptsQueue.nonEmpty) { requestReceipts(peer) diff --git a/src/main/scala/io/iohk/ethereum/consensus/ConsensusImpl.scala b/src/main/scala/io/iohk/ethereum/consensus/ConsensusImpl.scala index f2b899936c..e04ab69e2e 100644 --- a/src/main/scala/io/iohk/ethereum/consensus/ConsensusImpl.scala +++ b/src/main/scala/io/iohk/ethereum/consensus/ConsensusImpl.scala @@ -302,9 +302,9 @@ class ConsensusImpl( case BlockData(block, _, _) if block.hasCheckpoint => block.number }.maximumOption - val bestNumber = oldBranch.last.block.header.number - blockchain.saveBestKnownBlocks(bestNumber, checkpointNumber) - executedBlocks.foreach(data => blockQueue.enqueueBlock(data.block, bestNumber)) + val bestHeader = oldBranch.last.block.header + blockchain.saveBestKnownBlocks(bestHeader.hash, bestHeader.number, checkpointNumber) + executedBlocks.foreach(data => blockQueue.enqueueBlock(data.block, bestHeader.number)) newBranch.diff(executedBlocks.map(_.block)).headOption.foreach { block => blockQueue.removeSubtree(block.header.hash) diff --git a/src/main/scala/io/iohk/ethereum/db/storage/AppStateStorage.scala b/src/main/scala/io/iohk/ethereum/db/storage/AppStateStorage.scala index 91d7598a37..4339747f01 100644 --- a/src/main/scala/io/iohk/ethereum/db/storage/AppStateStorage.scala +++ b/src/main/scala/io/iohk/ethereum/db/storage/AppStateStorage.scala @@ -2,11 +2,15 @@ package io.iohk.ethereum.db.storage import java.math.BigInteger +import akka.util.ByteString + import scala.collection.immutable.ArraySeq import io.iohk.ethereum.db.dataSource.DataSource import io.iohk.ethereum.db.dataSource.DataSourceBatchUpdate import io.iohk.ethereum.db.storage.AppStateStorage._ +import io.iohk.ethereum.domain.appstate.BlockInfo +import io.iohk.ethereum.utils.Hex /** This class is used to store app state variables * Key: see AppStateStorage.Keys @@ -27,6 +31,16 @@ class AppStateStorage(val dataSource: DataSource) extends TransactionalKeyValueS def getBestBlockNumber(): BigInt = getBigInt(Keys.BestBlockNumber) + def getBestBlockInfo(): BlockInfo = + BlockInfo( // TODO ETCM-1090 provide the genesis hash as default + get(Keys.BestBlockHash).map(v => ByteString(Hex.decode(v))).getOrElse(ByteString.empty), + getBigInt(Keys.BestBlockNumber) + ) + + def putBestBlockInfo(b: BlockInfo): DataSourceBatchUpdate = + put(Keys.BestBlockNumber, b.number.toString) + .and(put(Keys.BestBlockHash, Hex.toHexString(b.hash.toArray))) + def putBestBlockNumber(bestBlockNumber: BigInt): DataSourceBatchUpdate = put(Keys.BestBlockNumber, bestBlockNumber.toString) @@ -72,9 +86,11 @@ object AppStateStorage { object Keys { val BestBlockNumber = "BestBlockNumber" + val BestBlockHash = "BestBlockHash" val FastSyncDone = "FastSyncDone" val EstimatedHighestBlock = "EstimatedHighestBlock" val SyncStartingBlock = "SyncStartingBlock" val LatestCheckpointBlockNumber = "LatestCheckpointBlockNumber" } + } diff --git a/src/main/scala/io/iohk/ethereum/domain/Blockchain.scala b/src/main/scala/io/iohk/ethereum/domain/Blockchain.scala index 34efff79b1..6e9e003934 100644 --- a/src/main/scala/io/iohk/ethereum/domain/Blockchain.scala +++ b/src/main/scala/io/iohk/ethereum/domain/Blockchain.scala @@ -7,6 +7,7 @@ import scala.annotation.tailrec import io.iohk.ethereum.db.dataSource.DataSourceBatchUpdate import io.iohk.ethereum.db.storage._ import io.iohk.ethereum.domain +import io.iohk.ethereum.domain.appstate.BlockInfo import io.iohk.ethereum.jsonrpc.ProofService.StorageProof import io.iohk.ethereum.ledger.InMemoryWorldStateProxy import io.iohk.ethereum.ledger.InMemoryWorldStateProxyStorage @@ -64,7 +65,11 @@ trait Blockchain { def removeBlock(hash: ByteString): Unit - def saveBestKnownBlocks(bestBlockNumber: BigInt, latestCheckpointNumber: Option[BigInt] = None): Unit + def saveBestKnownBlocks( + bestBlockHash: ByteString, + bestBlockNumber: BigInt, + latestCheckpointNumber: Option[BigInt] = None + ): Unit } @@ -126,20 +131,28 @@ class BlockchainImpl( def getReadOnlyMptStorage(): MptStorage = stateStorage.getReadOnlyStorage - override def saveBestKnownBlocks(bestBlockNumber: BigInt, latestCheckpointNumber: Option[BigInt] = None): Unit = + override def saveBestKnownBlocks( + bestBlockHash: ByteString, + bestBlockNumber: BigInt, + latestCheckpointNumber: Option[BigInt] = None + ): Unit = latestCheckpointNumber match { case Some(number) => - saveBestKnownBlockAndLatestCheckpointNumber(bestBlockNumber, number) + saveBestKnownBlockAndLatestCheckpointNumber(bestBlockHash, bestBlockNumber, number) case None => - saveBestKnownBlock(bestBlockNumber) + saveBestKnownBlock(bestBlockHash, bestBlockNumber) } - private def saveBestKnownBlock(bestBlockNumber: BigInt): Unit = - appStateStorage.putBestBlockNumber(bestBlockNumber).commit() + private def saveBestKnownBlock(bestBlockHash: ByteString, bestBlockNumber: BigInt): Unit = + appStateStorage.putBestBlockInfo(BlockInfo(bestBlockHash, bestBlockNumber)).commit() - private def saveBestKnownBlockAndLatestCheckpointNumber(number: BigInt, latestCheckpointNumber: BigInt): Unit = + private def saveBestKnownBlockAndLatestCheckpointNumber( + bestBlockHash: ByteString, + number: BigInt, + latestCheckpointNumber: BigInt + ): Unit = appStateStorage - .putBestBlockNumber(number) + .putBestBlockInfo(BlockInfo(bestBlockHash, number)) .and(appStateStorage.putLatestCheckpointBlockNumber(latestCheckpointNumber)) .commit() @@ -163,7 +176,6 @@ class BlockchainImpl( log.debug(s"Trying to remove block ${block.idTag}") val txList = block.body.transactionList - val bestBlockNumber = blockchainReader.getBestBlockNumber() val latestCheckpointNumber = getLatestCheckpointBlockNumber() val blockNumberMappingUpdates = @@ -171,7 +183,8 @@ class BlockchainImpl( removeBlockNumberMapping(block.number) else blockNumberMappingStorage.emptyBatchUpdate - val newBestBlockNumber: BigInt = (bestBlockNumber - 1).max(0) + val potentialNewBestBlockNumber: BigInt = (block.number - 1).max(0) + val potentialNewBestBlockHash: ByteString = block.header.parentHash val newLatestCheckpointNumber: BigInt = if (block.hasCheckpoint && block.number == latestCheckpointNumber) { findPreviousCheckpointBlockNumber(block.number, block.number) @@ -187,8 +200,8 @@ class BlockchainImpl( into the case of having an incomplete best block and so an inconsistent db */ val bestBlockNumberUpdates = - if (appStateStorage.getBestBlockNumber() > newBestBlockNumber) - appStateStorage.putBestBlockNumber(newBestBlockNumber) + if (appStateStorage.getBestBlockNumber() > potentialNewBestBlockNumber) + appStateStorage.putBestBlockInfo(BlockInfo(potentialNewBestBlockHash, potentialNewBestBlockNumber)) else appStateStorage.emptyBatchUpdate val latestCheckpointNumberUpdates = if (appStateStorage.getLatestCheckpointBlockNumber() > newLatestCheckpointNumber) @@ -197,7 +210,7 @@ class BlockchainImpl( log.debug( "Persisting app info data into database. Persisted block number is {}. Persisted checkpoint number is {}", - newBestBlockNumber, + potentialNewBestBlockNumber, newLatestCheckpointNumber ) @@ -215,7 +228,7 @@ class BlockchainImpl( log.debug( "Removed block with hash {}. New best block number - {}, new best checkpoint block number - {}", ByteStringUtils.hash2string(blockHash), - newBestBlockNumber, + potentialNewBestBlockNumber, newLatestCheckpointNumber ) } diff --git a/src/main/scala/io/iohk/ethereum/domain/BlockchainReader.scala b/src/main/scala/io/iohk/ethereum/domain/BlockchainReader.scala index d248d2e032..4eda16636c 100644 --- a/src/main/scala/io/iohk/ethereum/domain/BlockchainReader.scala +++ b/src/main/scala/io/iohk/ethereum/domain/BlockchainReader.scala @@ -13,6 +13,7 @@ import io.iohk.ethereum.domain.branch.Branch import io.iohk.ethereum.domain.branch.EmptyBranch import io.iohk.ethereum.mpt.MerklePatriciaTrie import io.iohk.ethereum.mpt.MptNode +import io.iohk.ethereum.utils.Hex import io.iohk.ethereum.utils.Logger class BlockchainReader( @@ -84,9 +85,17 @@ class BlockchainReader( //returns the best known block if it's available in the storage def getBestBlock(): Option[Block] = { - val bestBlockNumber = getBestBlockNumber() - log.debug("Trying to get best block with number {}", bestBlockNumber) - getBlockByNumber(bestBlockNumber) + val bestKnownBlockinfo = appStateStorage.getBestBlockInfo() + log.debug("Trying to get best block with number {}", bestKnownBlockinfo.number) + val bestBlock = getBlockByHash(bestKnownBlockinfo.hash) + if (bestBlock.isEmpty) { + log.error( + "Best block {} (number: {}) not found in storage.", + Hex.toHexString(bestKnownBlockinfo.hash.toArray), + bestKnownBlockinfo.number + ) + } + bestBlock } def genesisHeader: BlockHeader = diff --git a/src/main/scala/io/iohk/ethereum/domain/BlockchainWriter.scala b/src/main/scala/io/iohk/ethereum/domain/BlockchainWriter.scala index db7d31f6d9..b5ec94da2a 100644 --- a/src/main/scala/io/iohk/ethereum/domain/BlockchainWriter.scala +++ b/src/main/scala/io/iohk/ethereum/domain/BlockchainWriter.scala @@ -11,6 +11,7 @@ import io.iohk.ethereum.db.storage.ChainWeightStorage import io.iohk.ethereum.db.storage.ReceiptStorage import io.iohk.ethereum.db.storage.TransactionMappingStorage import io.iohk.ethereum.db.storage.TransactionMappingStorage.TransactionLocation +import io.iohk.ethereum.domain.appstate.BlockInfo import io.iohk.ethereum.utils.Logger class BlockchainWriter( @@ -31,14 +32,14 @@ class BlockchainWriter( block.header.number ) appStateStorage - .putBestBlockNumber(block.header.number) + .putBestBlockInfo(BlockInfo(block.header.hash, block.header.number)) .and(appStateStorage.putLatestCheckpointBlockNumber(block.header.number)) } else if (saveAsBestBlock) { log.debug( "New best known block number - {}", block.header.number ) - appStateStorage.putBestBlockNumber(block.header.number) + appStateStorage.putBestBlockInfo(BlockInfo(block.header.hash, block.header.number)) } else { appStateStorage.emptyBatchUpdate } diff --git a/src/main/scala/io/iohk/ethereum/domain/appstate/BlockInfo.scala b/src/main/scala/io/iohk/ethereum/domain/appstate/BlockInfo.scala new file mode 100644 index 0000000000..76359ae038 --- /dev/null +++ b/src/main/scala/io/iohk/ethereum/domain/appstate/BlockInfo.scala @@ -0,0 +1,5 @@ +package io.iohk.ethereum.domain.appstate + +import akka.util.ByteString + +case class BlockInfo(hash: ByteString, number: BigInt) diff --git a/src/main/scala/io/iohk/ethereum/nodebuilder/StdNode.scala b/src/main/scala/io/iohk/ethereum/nodebuilder/StdNode.scala index 784a9e2816..fd095b08bc 100644 --- a/src/main/scala/io/iohk/ethereum/nodebuilder/StdNode.scala +++ b/src/main/scala/io/iohk/ethereum/nodebuilder/StdNode.scala @@ -1,6 +1,7 @@ package io.iohk.ethereum.nodebuilder import akka.actor.typed.ActorSystem +import akka.util.ByteString import scala.concurrent.Await import scala.concurrent.ExecutionContext.Implicits.global @@ -18,6 +19,7 @@ import io.iohk.ethereum.network.discovery.PeerDiscoveryManager import io.iohk.ethereum.nodebuilder.tooling.PeriodicConsistencyCheck import io.iohk.ethereum.nodebuilder.tooling.StorageConsistencyChecker import io.iohk.ethereum.utils.Config +import io.iohk.ethereum.utils.Hex /** A standard node is everything Ethereum prescribes except the mining algorithm, * which is plugged in dynamically. @@ -32,6 +34,8 @@ abstract class BaseNode extends Node { def start(): Unit = { startMetricsClient() + fixDatabase() + loadGenesisData() runDBConsistencyCheck() @@ -132,6 +136,24 @@ abstract class BaseNode extends Node { tryAndLogFailure(() => Metrics.get().close()) tryAndLogFailure(() => storagesInstance.dataSource.close()) } + + def fixDatabase(): Unit = { + // FIXME this is a temporary solution to avoid an incompatibility due to the introduction of the best block hash + // We can remove this fix when we release an incompatible version. + val bestBlockInfo = storagesInstance.storages.appStateStorage.getBestBlockInfo() + if (bestBlockInfo.hash == ByteString.empty && bestBlockInfo.number > 0) { + log.warn("Fixing best block hash into database for block {}", bestBlockInfo.number) + storagesInstance.storages.blockNumberMappingStorage.get(bestBlockInfo.number) match { + case Some(hash) => + log.warn("Putting {} as the best block hash", Hex.toHexString(hash.toArray)) + storagesInstance.storages.appStateStorage.putBestBlockInfo(bestBlockInfo.copy(hash = hash)).commit() + case None => + log.error("No block found for number {} when trying to fix database", bestBlockInfo.number) + } + + } + + } } class StdNode extends BaseNode with StdMiningBuilder diff --git a/src/test/scala/io/iohk/ethereum/ObjectGenerators.scala b/src/test/scala/io/iohk/ethereum/ObjectGenerators.scala index 3269e557a1..72ccfadc09 100644 --- a/src/test/scala/io/iohk/ethereum/ObjectGenerators.scala +++ b/src/test/scala/io/iohk/ethereum/ObjectGenerators.scala @@ -33,8 +33,6 @@ trait ObjectGenerators { def intGen(min: Int, max: Int): Gen[Int] = Gen.choose(min, max) - def posIntGen(min: Int, max: Int): Gen[Int] = Gen.choose(min, max).suchThat(_ > 0) - def intGen: Gen[Int] = Gen.choose(Int.MinValue, Int.MaxValue) def longGen: Gen[Long] = Gen.choose(Long.MinValue, Long.MaxValue) diff --git a/src/test/scala/io/iohk/ethereum/blockchain/sync/SyncStateSchedulerSpec.scala b/src/test/scala/io/iohk/ethereum/blockchain/sync/SyncStateSchedulerSpec.scala index f1dc618ccc..a7b9eadc2a 100644 --- a/src/test/scala/io/iohk/ethereum/blockchain/sync/SyncStateSchedulerSpec.scala +++ b/src/test/scala/io/iohk/ethereum/blockchain/sync/SyncStateSchedulerSpec.scala @@ -252,7 +252,7 @@ class SyncStateSchedulerSpec buildScheduler() val header = Fixtures.Blocks.ValidBlock.header.copy(stateRoot = worldHash, number = 1) schedulerBlockchainWriter.storeBlockHeader(header).commit() - schedulerBlockchain.saveBestKnownBlocks(1) + schedulerBlockchain.saveBestKnownBlocks(header.hash, 1) var state = scheduler.initState(worldHash).get while (state.activeRequest.nonEmpty) { val (allMissingNodes1, state2) = scheduler.getAllMissingNodes(state) diff --git a/src/test/scala/io/iohk/ethereum/blockchain/sync/regular/RegularSyncSpec.scala b/src/test/scala/io/iohk/ethereum/blockchain/sync/regular/RegularSyncSpec.scala index 0436bd93f2..2d427bb949 100644 --- a/src/test/scala/io/iohk/ethereum/blockchain/sync/regular/RegularSyncSpec.scala +++ b/src/test/scala/io/iohk/ethereum/blockchain/sync/regular/RegularSyncSpec.scala @@ -738,7 +738,7 @@ class RegularSyncSpec goToTop() val num: BigInt = 42 - blockchain.saveBestKnownBlocks(num, Some(num)) + blockchain.saveBestKnownBlocks(testBlocks.head.hash, num, Some(num)) etcPeerManager.expectMsg(GetHandshakedPeers) etcPeerManager.reply(HandshakedPeers(handshakedPeers)) diff --git a/src/test/scala/io/iohk/ethereum/consensus/pow/validators/StdOmmersValidatorSpec.scala b/src/test/scala/io/iohk/ethereum/consensus/pow/validators/StdOmmersValidatorSpec.scala index 388e18a488..6ae54d7a6b 100644 --- a/src/test/scala/io/iohk/ethereum/consensus/pow/validators/StdOmmersValidatorSpec.scala +++ b/src/test/scala/io/iohk/ethereum/consensus/pow/validators/StdOmmersValidatorSpec.scala @@ -464,7 +464,7 @@ class StdOmmersValidatorSpec extends AnyFlatSpec with Matchers with ScalaCheckPr .and(blockchainWriter.storeBlock(block95)) .and(blockchainWriter.storeBlock(block96)) .commit() - blockchain.saveBestKnownBlocks(block96.number) + blockchain.saveBestKnownBlocks(block96.hash, block96.number) } } diff --git a/src/test/scala/io/iohk/ethereum/domain/BlockchainReaderSpec.scala b/src/test/scala/io/iohk/ethereum/domain/BlockchainReaderSpec.scala new file mode 100644 index 0000000000..643218fac2 --- /dev/null +++ b/src/test/scala/io/iohk/ethereum/domain/BlockchainReaderSpec.scala @@ -0,0 +1,25 @@ +package io.iohk.ethereum.domain + +import org.bouncycastle.util.encoders.Hex +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks + +import io.iohk.ethereum.ObjectGenerators +import io.iohk.ethereum.blockchain.sync.EphemBlockchainTestSetup +import io.iohk.ethereum.network.p2p.messages.BaseETH6XMessages.NewBlock +import io.iohk.ethereum.security.SecureRandomBuilder + +class BlockchainReaderSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyChecks with SecureRandomBuilder { + + val chainId: Option[Byte] = Hex.decode("3d").headOption + + "BlockchainReader" should "be able to get the best block after it was stored by BlockchainWriter" in new EphemBlockchainTestSetup { + forAll(ObjectGenerators.newBlockGen(secureRandom, chainId)) { case NewBlock(block, weight) => + blockchainWriter.save(block, Nil, ChainWeight(0, weight), true) + + blockchainReader.getBestBlock() shouldBe Some(block) + } + } + +} diff --git a/src/test/scala/io/iohk/ethereum/domain/BlockchainSpec.scala b/src/test/scala/io/iohk/ethereum/domain/BlockchainSpec.scala index b932c4ac09..b3d6e42f08 100644 --- a/src/test/scala/io/iohk/ethereum/domain/BlockchainSpec.scala +++ b/src/test/scala/io/iohk/ethereum/domain/BlockchainSpec.scala @@ -44,7 +44,7 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh it should "be able to store a block and retrieve it by number" in new EphemBlockchainTestSetup { val validBlock = Fixtures.Blocks.ValidBlock.block blockchainWriter.storeBlock(validBlock).commit() - blockchain.saveBestKnownBlocks(validBlock.number) + blockchain.saveBestKnownBlocks(validBlock.hash, validBlock.number) val block = blockchainReader.getBlockByNumber(blockchainReader.getBestBranch(), validBlock.header.number) block.isDefined should ===(true) validBlock should ===(block.get) @@ -61,7 +61,7 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh blockchainWriter.save(validBlock, Seq.empty, ChainWeight(100, 100), saveAsBestBlock = true) blockchainReader.isInChain(blockchainReader.getBestBranch(), validBlock.hash) should ===(true) // simulation of node restart - blockchain.saveBestKnownBlocks(validBlock.header.number - 1) + blockchain.saveBestKnownBlocks(validBlock.header.parentHash, validBlock.header.number - 1) blockchainReader.isInChain(blockchainReader.getBestBranch(), validBlock.hash) should ===(false) } @@ -151,7 +151,7 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh val headerWithAcc = validHeader.copy(stateRoot = ByteString(mptWithAcc.getRootHash)) blockchainWriter.storeBlockHeader(headerWithAcc).commit() - blockchain.saveBestKnownBlocks(headerWithAcc.number) + blockchain.saveBestKnownBlocks(headerWithAcc.hash, headerWithAcc.number) val retrievedAccount = blockchainReader.getAccount(blockchainReader.getBestBranch(), address, headerWithAcc.number) retrievedAccount shouldEqual Some(account) @@ -171,7 +171,7 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh val headerWithAcc = validHeader.copy(stateRoot = ByteString(mptWithAcc.getRootHash)) blockchainWriter.storeBlockHeader(headerWithAcc).commit() - blockchain.saveBestKnownBlocks(headerWithAcc.number) + blockchain.saveBestKnownBlocks(headerWithAcc.hash, headerWithAcc.number) //unhappy path val wrongAddress = Address(666) @@ -200,7 +200,7 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh val headerWithAcc = Fixtures.Blocks.ValidBlock.header.copy(stateRoot = ByteString(mptWithAcc.getRootHash)) blockchainWriter.storeBlockHeader(headerWithAcc).commit() - blockchain.saveBestKnownBlocks(headerWithAcc.number) + blockchain.saveBestKnownBlocks(headerWithAcc.hash, headerWithAcc.number) val wrongAddress = Address(666) val retrievedAccountProofWrong = @@ -214,7 +214,7 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh } it should "return correct best block number after saving and rolling back blocks" in new TestSetup { - forAll(posIntGen(min = 1, max = maxNumberBlocksToImport)) { numberBlocksToImport => + forAll(intGen(min = 1, max = maxNumberBlocksToImport)) { numberBlocksToImport => val testSetup = newSetup() import testSetup._ @@ -237,8 +237,9 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh blockchainReaderWithStubPersisting.getBestBlockNumber() shouldBe blocksToImport.last.number // Rollback blocks - val numberBlocksToRollback = intGen(0, numberBlocksToImport).sample.get - val (_, blocksToRollback) = blocksToImport.splitAt(numberBlocksToRollback) + val numberBlocksToKeep = intGen(0, numberBlocksToImport).sample.get + + val (_, blocksToRollback) = blocksToImport.splitAt(numberBlocksToKeep) // Randomly select the block rollback to persist (empty means no persistence) val blockRollbackToPersist = @@ -254,38 +255,13 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh blockchainWithStubPersisting.removeBlock(block.hash) } - val expectedPersistedBestBlock = calculatePersistedBestBlock( - blockImportToPersist.map(_.number), - blockRollbackToPersist.map(_.number), - blocksToRollback.map(_.number) - ) - blockchainReaderWithStubPersisting.getBestBlockNumber() shouldBe expectedPersistedBestBlock + blockchainReaderWithStubPersisting.getBestBlockNumber() shouldBe numberBlocksToKeep } } trait TestSetup extends MockFactory { val maxNumberBlocksToImport: Int = 30 - def calculatePersistedBestBlock( - blockImportPersisted: Option[BigInt], - blockRollbackPersisted: Option[BigInt], - blocksRolledback: Seq[BigInt] - ): BigInt = - (blocksRolledback, blockImportPersisted) match { - case (Nil, Some(bi)) => - // No blocks rolledback, last persist was the persist during import - bi - case (nonEmptyRolledbackBlocks, Some(bi)) => - // Last forced persist during apply/rollback - val maxForcedPersist = blockRollbackPersisted.fold(bi)(br => (br - 1).max(bi)) - - // The above number would have been decreased by any rollbacked blocks - (nonEmptyRolledbackBlocks.head - 1).min(maxForcedPersist) - case (_, None) => - // If persisted rollback, then it was decreased by the future rollbacks, if not no persistance was ever done - blockRollbackPersisted.fold(0: BigInt)(_ => blocksRolledback.head - 1) - } - trait StubPersistingBlockchainSetup { def stubStateStorage: StateStorage def blockchainStoragesWithStubPersisting: BlockchainStorages diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/EthBlocksServiceSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/EthBlocksServiceSpec.scala index 38a875738c..9d73335ce2 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/EthBlocksServiceSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/EthBlocksServiceSpec.scala @@ -45,7 +45,7 @@ class EthBlocksServiceSpec "EthBlocksService" should "answer eth_blockNumber with the latest block number" in new TestSetup { val bestBlockNumber = 10 - blockchain.saveBestKnownBlocks(bestBlockNumber) + blockchain.saveBestKnownBlocks(ByteString.empty, bestBlockNumber) val response = ethBlocksService.bestBlockNumber(BestBlockNumberRequest()).runSyncUnsafe(Duration.Inf).toOption.get response.bestBlockNumber shouldEqual bestBlockNumber @@ -153,7 +153,7 @@ class EthBlocksServiceSpec .storeBlock(blockToRequest) .and(blockchainWriter.storeChainWeight(blockToRequestHash, blockWeight)) .commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.header.number) (() => blockGenerator.getPendingBlockAndState).expects().returns(None) @@ -173,7 +173,7 @@ class EthBlocksServiceSpec .storeBlock(blockToRequest) .and(blockchainWriter.storeChainWeight(blockToRequestHash, blockWeight)) .commit() - blockchain.saveBestKnownBlocks(blockToRequest.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val request = BlockByNumberRequest(BlockParam.WithNumber(blockToRequestNumber), fullTxs = true) val response = ethBlocksService.getBlockByNumber(request).runSyncUnsafe(Duration.Inf).toOption.get @@ -191,7 +191,7 @@ class EthBlocksServiceSpec it should "answer eth_getBlockByNumber with the block response correctly when it's chain weight is not in blockchain" in new TestSetup { blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val request = BlockByNumberRequest(BlockParam.WithNumber(blockToRequestNumber), fullTxs = true) val response = ethBlocksService.getBlockByNumber(request).runSyncUnsafe(Duration.Inf).toOption.get @@ -210,7 +210,7 @@ class EthBlocksServiceSpec .storeBlock(blockToRequest) .and(blockchainWriter.storeChainWeight(blockToRequestHash, blockWeight)) .commit() - blockchain.saveBestKnownBlocks(blockToRequest.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val request = BlockByNumberRequest(BlockParam.WithNumber(blockToRequestNumber), fullTxs = true) val response = @@ -225,7 +225,7 @@ class EthBlocksServiceSpec it should "get transaction count by block number" in new TestSetup { blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val response = ethBlocksService.getBlockTransactionCountByNumber( GetBlockTransactionCountByNumberRequest(BlockParam.WithNumber(blockToRequest.header.number)) @@ -238,7 +238,7 @@ class EthBlocksServiceSpec it should "get transaction count by latest block number" in new TestSetup { blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.header.number) val response = ethBlocksService.getBlockTransactionCountByNumber(GetBlockTransactionCountByNumberRequest(BlockParam.Latest)) @@ -257,7 +257,7 @@ class EthBlocksServiceSpec it should "answer eth_getUncleByBlockHashAndIndex with None when there's no uncle" in new TestSetup { blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val uncleIndexToRequest = 0 val request = UncleByBlockHashAndIndexRequest(blockToRequestHash, uncleIndexToRequest) @@ -268,7 +268,7 @@ class EthBlocksServiceSpec it should "answer eth_getUncleByBlockHashAndIndex with None when there's no uncle in the requested index" in new TestSetup { blockchainWriter.storeBlock(blockToRequestWithUncles).commit() - blockchain.saveBestKnownBlocks(blockToRequestWithUncles.number) + blockchain.saveBestKnownBlocks(blockToRequestWithUncles.hash, blockToRequestWithUncles.number) val uncleIndexToRequest = 0 val request = UncleByBlockHashAndIndexRequest(blockToRequestHash, uncleIndexToRequest) @@ -361,7 +361,7 @@ class EthBlocksServiceSpec it should "answer eth_getUncleByBlockNumberAndIndex correctly when the requested index has one but there's no chain weight for it" in new TestSetup { blockchainWriter.storeBlock(blockToRequestWithUncles).commit() - blockchain.saveBestKnownBlocks(blockToRequestWithUncles.number) + blockchain.saveBestKnownBlocks(blockToRequestWithUncles.hash, blockToRequestWithUncles.number) val uncleIndexToRequest = 0 val request = UncleByBlockNumberAndIndexRequest(BlockParam.WithNumber(blockToRequestNumber), uncleIndexToRequest) @@ -378,7 +378,7 @@ class EthBlocksServiceSpec .storeBlock(blockToRequestWithUncles) .and(blockchainWriter.storeChainWeight(uncle.hash, uncleWeight)) .commit() - blockchain.saveBestKnownBlocks(blockToRequestWithUncles.number) + blockchain.saveBestKnownBlocks(blockToRequestWithUncles.hash, blockToRequestWithUncles.number) val uncleIndexToRequest = 0 val request = UncleByBlockNumberAndIndexRequest(BlockParam.WithNumber(blockToRequestNumber), uncleIndexToRequest) @@ -392,7 +392,7 @@ class EthBlocksServiceSpec it should "get uncle count by block number" in new TestSetup { blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val response = ethBlocksService.getUncleCountByBlockNumber(GetUncleCountByBlockNumberRequest(BlockParam.Latest)) @@ -412,7 +412,7 @@ class EthBlocksServiceSpec ) } - class TestSetup(implicit system: ActorSystem) extends MockFactory with EphemBlockchainTestSetup { + class TestSetup() extends MockFactory with EphemBlockchainTestSetup { val blockGenerator: PoWBlockGenerator = mock[PoWBlockGenerator] val appStateStorage: AppStateStorage = mock[AppStateStorage] diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/EthInfoServiceSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/EthInfoServiceSpec.scala index 474c0df756..58ec79416e 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/EthInfoServiceSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/EthInfoServiceSpec.scala @@ -102,7 +102,7 @@ class EthServiceSpec it should "execute call and return a value" in new TestSetup { blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val worldStateProxy = InMemoryWorldStateProxy( storagesInstance.storages.evmCodeStorage, @@ -132,7 +132,7 @@ class EthServiceSpec it should "execute estimateGas and return a value" in new TestSetup { blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val estimatedGas = BigInt(123) (stxLedger.binarySearchGasEstimation _).expects(*, *, *).returning(estimatedGas) diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/EthProofServiceSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/EthProofServiceSpec.scala index 776aaba6a1..0300815020 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/EthProofServiceSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/EthProofServiceSpec.scala @@ -214,7 +214,7 @@ class EthProofServiceSpec ) } - class TestSetup(implicit system: ActorSystem) extends MockFactory with EphemBlockchainTestSetup with ApisBuilder { + class TestSetup() extends MockFactory with EphemBlockchainTestSetup with ApisBuilder { val blockGenerator: PoWBlockGenerator = mock[PoWBlockGenerator] val address: Address = Address(ByteString(Hex.decode("abbb6bebfa05aa13e908eaa492bd7a8343760477"))) @@ -254,7 +254,7 @@ class EthProofServiceSpec val newBlockHeader: BlockHeader = blockToRequest.header.copy(stateRoot = ByteString(mpt.getRootHash)) val newblock: Block = blockToRequest.copy(header = newBlockHeader) blockchainWriter.storeBlock(newblock).commit() - blockchain.saveBestKnownBlocks(newblock.header.number) + blockchain.saveBestKnownBlocks(newblock.hash, newblock.number) val ethGetProof = new EthProofService(blockchain, blockchainReader, blockGenerator, blockchainConfig.ethCompatibleStorage) diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/EthTxServiceSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/EthTxServiceSpec.scala index 89eee0fc5b..fd60569b5c 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/EthTxServiceSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/EthTxServiceSpec.scala @@ -174,10 +174,12 @@ class EthTxServiceSpec } it should "return average gas price" in new TestSetup { - blockchain.saveBestKnownBlocks(42) + private val block: Block = + Block(Fixtures.Blocks.Block3125369.header.copy(number = 42), Fixtures.Blocks.Block3125369.body) blockchainWriter - .storeBlock(Block(Fixtures.Blocks.Block3125369.header.copy(number = 42), Fixtures.Blocks.Block3125369.body)) + .storeBlock(block) .commit() + blockchain.saveBestKnownBlocks(block.hash, block.number) val response = ethTxService.getGetGasPrice(GetGasPriceRequest()) response.runSyncUnsafe() shouldEqual Right(GetGasPriceResponse(BigInt("20000000000"))) @@ -185,7 +187,7 @@ class EthTxServiceSpec it should "getTransactionByBlockNumberAndIndexRequest return transaction by index" in new TestSetup { blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val txIndex: Int = 1 val request = GetTransactionByBlockNumberAndIndexRequest(BlockParam.Latest, txIndex) @@ -220,7 +222,7 @@ class EthTxServiceSpec it should "getRawTransactionByBlockNumberAndIndex return transaction by index" in new TestSetup { blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val txIndex: Int = 1 val request = GetTransactionByBlockNumberAndIndexRequest(BlockParam.Latest, txIndex) diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/EthUserServiceSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/EthUserServiceSpec.scala index 9e9a4fc226..f5d2144549 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/EthUserServiceSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/EthUserServiceSpec.scala @@ -49,7 +49,7 @@ class EthUserServiceSpec val newBlockHeader = blockToRequest.header.copy(stateRoot = ByteString(mpt.getRootHash)) val newblock = blockToRequest.copy(header = newBlockHeader) blockchainWriter.storeBlock(newblock).commit() - blockchain.saveBestKnownBlocks(newblock.header.number) + blockchain.saveBestKnownBlocks(newblock.hash, newblock.number) val response = ethUserService.getCode(GetCodeRequest(address, BlockParam.Latest)) @@ -71,7 +71,7 @@ class EthUserServiceSpec val newBlockHeader = blockToRequest.header.copy(stateRoot = ByteString(mpt.getRootHash)) val newblock = blockToRequest.copy(header = newBlockHeader) blockchainWriter.storeBlock(newblock).commit() - blockchain.saveBestKnownBlocks(newblock.header.number) + blockchain.saveBestKnownBlocks(newblock.hash, newblock.number) val response = ethUserService.getBalance(GetBalanceRequest(address, BlockParam.Latest)) @@ -84,7 +84,7 @@ class EthUserServiceSpec val newBlockHeader = blockToRequest.header val newblock = blockToRequest.copy(header = newBlockHeader) blockchainWriter.storeBlock(newblock).commit() - blockchain.saveBestKnownBlocks(newblock.header.number) + blockchain.saveBestKnownBlocks(newblock.hash, newblock.header.number) val response = ethUserService.getBalance(GetBalanceRequest(address, BlockParam.Latest)) @@ -114,7 +114,7 @@ class EthUserServiceSpec val newBlockHeader = blockToRequest.header.copy(stateRoot = ByteString(mpt.getRootHash)) val newblock = blockToRequest.copy(header = newBlockHeader) blockchainWriter.storeBlock(newblock).commit() - blockchain.saveBestKnownBlocks(newblock.header.number) + blockchain.saveBestKnownBlocks(newblock.hash, newblock.number) val response = ethUserService.getStorageAt(GetStorageAtRequest(address, 333, BlockParam.Latest)) response.runSyncUnsafe().map(v => UInt256(v.value)) shouldEqual Right(UInt256(123)) @@ -132,14 +132,14 @@ class EthUserServiceSpec val newBlockHeader = blockToRequest.header.copy(stateRoot = ByteString(mpt.getRootHash)) val newblock = blockToRequest.copy(header = newBlockHeader) blockchainWriter.storeBlock(newblock).commit() - blockchain.saveBestKnownBlocks(newblock.header.number) + blockchain.saveBestKnownBlocks(newblock.hash, newblock.number) val response = ethUserService.getTransactionCount(GetTransactionCountRequest(address, BlockParam.Latest)) response.runSyncUnsafe() shouldEqual Right(GetTransactionCountResponse(BigInt(999))) } - class TestSetup(implicit system: ActorSystem) extends MockFactory with EphemBlockchainTestSetup { + class TestSetup() extends MockFactory with EphemBlockchainTestSetup { lazy val ethUserService = new EthUserService( blockchain, blockchainReader, diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthLegacyTransactionSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthLegacyTransactionSpec.scala index b0a9c013ac..ddf95ca71a 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthLegacyTransactionSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthLegacyTransactionSpec.scala @@ -54,7 +54,7 @@ class JsonRpcControllerEthLegacyTransactionSpec val txIndexToRequest = blockToRequest.body.transactionList.size / 2 blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val request: JsonRpcRequest = newJsonRpcRequest( "eth_getTransactionByBlockHashAndIndex", @@ -77,7 +77,7 @@ class JsonRpcControllerEthLegacyTransactionSpec val txIndexToRequest = blockToRequest.body.transactionList.size / 2 blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val request: JsonRpcRequest = newJsonRpcRequest( "eth_getRawTransactionByBlockHashAndIndex", @@ -137,7 +137,7 @@ class JsonRpcControllerEthLegacyTransactionSpec val txIndex = 1 blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val request: JsonRpcRequest = newJsonRpcRequest( "eth_getTransactionByBlockNumberAndIndex", @@ -161,7 +161,7 @@ class JsonRpcControllerEthLegacyTransactionSpec val txIndex = 1 blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val request: JsonRpcRequest = newJsonRpcRequest( "eth_getTransactionByBlockNumberAndIndex", @@ -184,7 +184,7 @@ class JsonRpcControllerEthLegacyTransactionSpec val txIndex = 1 blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val request: JsonRpcRequest = newJsonRpcRequest( "eth_getTransactionByBlockNumberAndIndex", @@ -208,7 +208,7 @@ class JsonRpcControllerEthLegacyTransactionSpec val txIndex = 1 blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val request: JsonRpcRequest = newJsonRpcRequest( "eth_getRawTransactionByBlockNumberAndIndex", @@ -234,7 +234,7 @@ class JsonRpcControllerEthLegacyTransactionSpec val txIndex = 1 blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val request: JsonRpcRequest = newJsonRpcRequest( "eth_getRawTransactionByBlockNumberAndIndex", @@ -258,7 +258,7 @@ class JsonRpcControllerEthLegacyTransactionSpec val txIndex = 1 blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val request: JsonRpcRequest = newJsonRpcRequest( "eth_getRawTransactionByBlockNumberAndIndex", @@ -336,7 +336,7 @@ class JsonRpcControllerEthLegacyTransactionSpec val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val rpcRequest = newJsonRpcRequest( "eth_getBlockTransactionCountByHash", diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala index c4d911e33c..6f3d63eaef 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala @@ -80,7 +80,7 @@ class JsonRpcControllerEthSpec it should "handle eth_blockNumber request" in new JsonRpcControllerFixture { val bestBlockNumber = 10 - blockchain.saveBestKnownBlocks(bestBlockNumber) + blockchain.saveBestKnownBlocks(ByteString.empty, bestBlockNumber) val rpcRequest = newJsonRpcRequest("eth_blockNumber") val response = jsonRpcController.handleRequest(rpcRequest).runSyncUnsafe() @@ -177,7 +177,7 @@ class JsonRpcControllerEthSpec .storeBlock(blockToRequest) .and(blockchainWriter.storeChainWeight(blockToRequest.header.hash, blockWeight)) .commit() - blockchain.saveBestKnownBlocks(blockToRequest.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val request = newJsonRpcRequest( "eth_getBlockByNumber", @@ -199,7 +199,7 @@ class JsonRpcControllerEthSpec .storeBlock(blockToRequest) .and(blockchainWriter.storeChainWeight(blockToRequest.header.hash, blockWeight)) .commit() - blockchain.saveBestKnownBlocks(blockToRequest.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val request = newJsonRpcRequest( "eth_getBlockByNumber", @@ -221,7 +221,7 @@ class JsonRpcControllerEthSpec .storeBlock(blockToRequest) .and(blockchainWriter.storeChainWeight(blockToRequest.header.hash, blockWeight)) .commit() - blockchain.saveBestKnownBlocks(blockToRequest.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val request = newJsonRpcRequest( "eth_getBlockByNumber", @@ -265,7 +265,7 @@ class JsonRpcControllerEthSpec val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, BlockBody(Nil, Seq(uncle))) blockchainWriter.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.number) + blockchain.saveBestKnownBlocks(blockToRequest.hash, blockToRequest.number) val request: JsonRpcRequest = newJsonRpcRequest( "eth_getUncleByBlockNumberAndIndex", @@ -413,10 +413,10 @@ class JsonRpcControllerEthSpec } it should "eth_gasPrice" in new JsonRpcControllerFixture { - blockchainWriter - .storeBlock(Block(Fixtures.Blocks.Block3125369.header.copy(number = 42), Fixtures.Blocks.Block3125369.body)) - .commit() - blockchain.saveBestKnownBlocks(42) + private val block: Block = + Block(Fixtures.Blocks.Block3125369.header.copy(number = 42), Fixtures.Blocks.Block3125369.body) + blockchainWriter.storeBlock(block).commit() + blockchain.saveBestKnownBlocks(block.hash, block.number) val request: JsonRpcRequest = newJsonRpcRequest("eth_gasPrice") diff --git a/src/test/scala/io/iohk/ethereum/transactions/LegacyTransactionHistoryServiceSpec.scala b/src/test/scala/io/iohk/ethereum/transactions/LegacyTransactionHistoryServiceSpec.scala index 73068ad27e..28e5efad93 100644 --- a/src/test/scala/io/iohk/ethereum/transactions/LegacyTransactionHistoryServiceSpec.scala +++ b/src/test/scala/io/iohk/ethereum/transactions/LegacyTransactionHistoryServiceSpec.scala @@ -87,7 +87,7 @@ class LegacyTransactionHistoryServiceSpec .and(blockchainWriter.storeBlock(blockWithTxs2and3)) .and(blockchainWriter.storeReceipts(blockWithTxs2and3.hash, blockTx2And3Receipts)) .commit() - blockchain.saveBestKnownBlocks(blockWithTxs2and3.number) + blockchain.saveBestKnownBlocks(blockWithTxs2and3.hash, blockWithTxs2and3.number) } response <- transactionHistoryService.getAccountTransactions(address, BigInt(3125360) to BigInt(3125370)) } yield assert(response === expectedTxs) @@ -160,7 +160,7 @@ class LegacyTransactionHistoryServiceSpec ) def makeReceipts(block: Block): Seq[Receipt] = - block.body.transactionList.map(tx => Receipt(HashOutcome(block.hash), BigInt(21000), ByteString("foo"), Nil)) + block.body.transactionList.map(_ => Receipt(HashOutcome(block.hash), BigInt(21000), ByteString("foo"), Nil)) for { _ <- Task {