Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ETCM-311-fast_sync_improvements' of github.com:input-ou…
…tput-hk/mantis into ETCM-311-fast_sync_improvements
- Loading branch information
Showing
47 changed files
with
799 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,6 @@ metals.sbt | |
# Nix | ||
result | ||
.nix/ | ||
|
||
# sonarScan | ||
.scannerwork/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM nixos/nix | ||
|
||
RUN apk --update add git less openssh && \ | ||
rm -rf /var/lib/apt/lists/* && \ | ||
rm /var/cache/apk/* | ||
|
||
RUN nix-channel --add https://nixos.org/channels/nixpkgs-unstable nixpkgs | ||
RUN nix-channel --update | ||
|
||
RUN nix-build -A pythonFull '<nixpkgs>' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
sandbox = false | ||
experimental-features = nix-command flakes ca-references | ||
substituters = https://hydra.iohk.io https://cache.nixos.org https://mantis-ops.cachix.org | ||
trusted-public-keys = hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= mantis-ops.cachix.org-1:SornDcX8/9rFrpTjU+mAAb26sF8mUpnxgXNjmKGcglQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
logLevel := sbt.Level.Warn | ||
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.6.1") | ||
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.2") | ||
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "1.0.0") | ||
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.7.5") | ||
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0") | ||
addSbtPlugin("com.github.mwz" % "sbt-sonar" % "2.2.0") | ||
addSbtPlugin("com.lightbend.sbt" % "sbt-javaagent" % "0.1.6") | ||
addSbtPlugin("com.sksamuel.scapegoat" % "sbt-scapegoat" % "1.1.0") | ||
addSbtPlugin("com.thoughtworks.sbt-api-mappings" % "sbt-api-mappings" % "3.0.0") | ||
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.5.1") | ||
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0") | ||
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.0") | ||
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.7.5") | ||
addSbtPlugin("io.kamon" % "sbt-kanela-runner" % "2.0.5") | ||
addSbtPlugin("com.lightbend.sbt" % "sbt-javaagent" % "0.1.6") | ||
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.2") | ||
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "1.0.0") | ||
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.6.1") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
package io.iohk.ethereum.ledger | ||
|
||
import akka.testkit.TestProbe | ||
import akka.util.ByteString | ||
import cats.data.NonEmptyList | ||
import io.iohk.ethereum.blockchain.sync.regular.BlockImporter.NewCheckpoint | ||
import io.iohk.ethereum.blockchain.sync.regular.{BlockFetcher, BlockImporter} | ||
import io.iohk.ethereum.checkpointing.CheckpointingTestHelpers | ||
import io.iohk.ethereum.consensus.blocks.CheckpointBlockGenerator | ||
import io.iohk.ethereum.domain._ | ||
import io.iohk.ethereum.mpt.MerklePatriciaTrie | ||
import io.iohk.ethereum.utils.Config.SyncConfig | ||
import io.iohk.ethereum.utils.Config | ||
import io.iohk.ethereum.{Fixtures, ObjectGenerators, crypto} | ||
import io.iohk.ethereum.ledger.Ledger.BlockResult | ||
import monix.execution.Scheduler | ||
import org.scalamock.scalatest.MockFactory | ||
import org.scalatest.BeforeAndAfterAll | ||
import org.scalatest.flatspec.AsyncFlatSpecLike | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import scala.concurrent.duration._ | ||
|
||
class BlockImporterItSpec extends MockFactory with TestSetupWithVmAndValidators with AsyncFlatSpecLike with Matchers with BeforeAndAfterAll { | ||
|
||
implicit val testScheduler = Scheduler.fixedPool("test", 32) | ||
|
||
override def afterAll(): Unit = { | ||
testScheduler.shutdown() | ||
testScheduler.awaitTermination(60.second) | ||
} | ||
|
||
val blockQueue = BlockQueue(blockchain, SyncConfig(Config.config)) | ||
|
||
val genesis = Block( | ||
Fixtures.Blocks.Genesis.header.copy(stateRoot = ByteString(MerklePatriciaTrie.EmptyRootHash)), | ||
Fixtures.Blocks.Genesis.body | ||
) | ||
val genesisWeight = ChainWeight.zero.increase(genesis.header) | ||
|
||
blockchain.save(genesis, Seq(), genesisWeight, saveAsBestBlock = true) | ||
|
||
lazy val checkpointBlockGenerator: CheckpointBlockGenerator = new CheckpointBlockGenerator | ||
|
||
val fetcherProbe = TestProbe() | ||
val ommersPoolProbe = TestProbe() | ||
val broadcasterProbe = TestProbe() | ||
val pendingTransactionsManagerProbe = TestProbe() | ||
val supervisor = TestProbe() | ||
|
||
val emptyWorld: InMemoryWorldStateProxy = | ||
blockchain.getWorldStateProxy( | ||
-1, | ||
UInt256.Zero, | ||
ByteString(MerklePatriciaTrie.EmptyRootHash), | ||
noEmptyAccounts = false, | ||
ethCompatibleStorage = true | ||
) | ||
|
||
override lazy val ledger = new TestLedgerImpl(successValidators) { | ||
override private[ledger] lazy val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation) { | ||
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, | ||
blockchain, | ||
syncConfig, | ||
ommersPoolProbe.ref, | ||
broadcasterProbe.ref, | ||
pendingTransactionsManagerProbe.ref, | ||
checkpointBlockGenerator, | ||
supervisor.ref | ||
)) | ||
|
||
val genesisBlock = blockchain.genesisBlock | ||
val block1: Block = getBlock(genesisBlock.number + 1, parent = genesisBlock.header.hash) | ||
// new chain is shorter but has a higher weight | ||
val newBlock2: Block = getBlock(genesisBlock.number + 2, difficulty = 108, parent = block1.header.hash) | ||
val newBlock3: Block = getBlock(genesisBlock.number + 3, difficulty = 300, parent = newBlock2.header.hash) | ||
val oldBlock2: Block = getBlock(genesisBlock.number + 2, difficulty = 102, parent = block1.header.hash) | ||
val oldBlock3: Block = getBlock(genesisBlock.number + 3, difficulty = 103, parent = oldBlock2.header.hash) | ||
val oldBlock4: Block = getBlock(genesisBlock.number + 4, difficulty = 104, parent = oldBlock3.header.hash) | ||
|
||
val weight1 = ChainWeight.totalDifficultyOnly(block1.header.difficulty) | ||
val newWeight2 = weight1.increase(newBlock2.header) | ||
val newWeight3 = newWeight2.increase(newBlock3.header) | ||
val oldWeight2 = weight1.increase(oldBlock2.header) | ||
val oldWeight3 = oldWeight2.increase(oldBlock3.header) | ||
val oldWeight4 = oldWeight3.increase(oldBlock4.header) | ||
|
||
//saving initial main chain | ||
blockchain.save(block1, Nil, weight1, saveAsBestBlock = true) | ||
blockchain.save(oldBlock2, Nil, oldWeight2, saveAsBestBlock = true) | ||
blockchain.save(oldBlock3, Nil, oldWeight3, saveAsBestBlock = true) | ||
blockchain.save(oldBlock4, Nil, oldWeight4, saveAsBestBlock = true) | ||
|
||
val oldBranch = List(oldBlock2, oldBlock3, oldBlock4) | ||
val newBranch = List(newBlock2, newBlock3) | ||
|
||
blockImporter ! BlockImporter.Start | ||
|
||
"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 blockImporter = system.actorOf( | ||
BlockImporter.props( | ||
fetcherProbe.ref, | ||
ledger, | ||
blockchain, | ||
syncConfig, | ||
ommersPoolProbe.ref, | ||
broadcasterProbe.ref, | ||
pendingTransactionsManagerProbe.ref, | ||
checkpointBlockGenerator, | ||
supervisor.ref | ||
)) | ||
|
||
blockImporter ! BlockImporter.Start | ||
blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(newBranch)) | ||
|
||
Thread.sleep(1000) | ||
//because the blocks are not valid, we shouldn't reorganise, but at least stay with a current chain, and the best block of the current chain is oldBlock4 | ||
blockchain.getBestBlock().get shouldEqual oldBlock4 | ||
} | ||
|
||
it should "return a correct new best block after reorganising longer chain to a shorter one if its weight is bigger" in { | ||
|
||
//returning discarded initial chain | ||
blockchain.save(oldBlock2, Nil, oldWeight2, saveAsBestBlock = true) | ||
blockchain.save(oldBlock3, Nil, oldWeight3, saveAsBestBlock = true) | ||
blockchain.save(oldBlock4, Nil, oldWeight4, saveAsBestBlock = true) | ||
|
||
blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(newBranch)) | ||
|
||
Thread.sleep(200) | ||
blockchain.getBestBlock().get shouldEqual newBlock3 | ||
} | ||
|
||
|
||
it should "switch to a branch with a checkpoint" in { | ||
|
||
val checkpoint = ObjectGenerators.fakeCheckpointGen(3, 3).sample.get | ||
val oldBlock5WithCheckpoint: Block = checkpointBlockGenerator.generate(oldBlock4, checkpoint) | ||
blockchain.save(oldBlock5WithCheckpoint, Nil, oldWeight4, saveAsBestBlock = true) | ||
|
||
val newBranch = List(newBlock2, newBlock3) | ||
|
||
blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(newBranch)) | ||
|
||
Thread.sleep(200) | ||
blockchain.getBestBlock().get shouldEqual oldBlock5WithCheckpoint | ||
blockchain.getLatestCheckpointBlockNumber() shouldEqual oldBlock5WithCheckpoint.header.number | ||
} | ||
|
||
it should "switch to a branch with a newer checkpoint" in { | ||
|
||
val checkpoint = ObjectGenerators.fakeCheckpointGen(3, 3).sample.get | ||
val newBlock4WithCheckpoint: Block = checkpointBlockGenerator.generate(newBlock3, checkpoint) | ||
blockchain.save(newBlock4WithCheckpoint, Nil, newWeight3, saveAsBestBlock = true) | ||
|
||
val newBranch = List(newBlock4WithCheckpoint) | ||
|
||
blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(newBranch)) | ||
|
||
Thread.sleep(200) | ||
blockchain.getBestBlock().get shouldEqual newBlock4WithCheckpoint | ||
blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock4WithCheckpoint.header.number | ||
} | ||
|
||
it should "return a correct checkpointed block after receiving a request for generating a new checkpoint" in { | ||
|
||
val parent = blockchain.getBestBlock().get | ||
val newBlock5: Block = getBlock(genesisBlock.number + 5, difficulty = 104, parent = parent.header.hash) | ||
val newWeight5 = newWeight3.increase(newBlock5.header) | ||
|
||
blockchain.save(newBlock5, Nil, newWeight5, saveAsBestBlock = true) | ||
|
||
val signatures = CheckpointingTestHelpers.createCheckpointSignatures( | ||
Seq(crypto.generateKeyPair(secureRandom)), | ||
newBlock5.hash | ||
) | ||
blockImporter ! NewCheckpoint(newBlock5.hash, signatures) | ||
|
||
val checkpointBlock = checkpointBlockGenerator.generate(newBlock5, Checkpoint(signatures)) | ||
|
||
Thread.sleep(1000) | ||
blockchain.getBestBlock().get shouldEqual checkpointBlock | ||
blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock5.header.number + 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.