|
| 1 | +package io.iohk.ethereum.ledger |
| 2 | + |
| 3 | +import akka.testkit.TestProbe |
| 4 | +import akka.util.ByteString |
| 5 | +import cats.data.NonEmptyList |
| 6 | +import io.iohk.ethereum.blockchain.sync.regular.BlockImporter.NewCheckpoint |
| 7 | +import io.iohk.ethereum.blockchain.sync.regular.{BlockFetcher, BlockImporter} |
| 8 | +import io.iohk.ethereum.checkpointing.CheckpointingTestHelpers |
| 9 | +import io.iohk.ethereum.consensus.blocks.CheckpointBlockGenerator |
| 10 | +import io.iohk.ethereum.domain._ |
| 11 | +import io.iohk.ethereum.mpt.MerklePatriciaTrie |
| 12 | +import io.iohk.ethereum.utils.Config.SyncConfig |
| 13 | +import io.iohk.ethereum.utils.Config |
| 14 | +import io.iohk.ethereum.{Fixtures, ObjectGenerators, crypto} |
| 15 | +import io.iohk.ethereum.ledger.Ledger.BlockResult |
| 16 | +import monix.execution.Scheduler |
| 17 | +import org.scalamock.scalatest.MockFactory |
| 18 | +import org.scalatest.BeforeAndAfterAll |
| 19 | +import org.scalatest.flatspec.AsyncFlatSpecLike |
| 20 | +import org.scalatest.matchers.should.Matchers |
| 21 | + |
| 22 | +import scala.concurrent.duration._ |
| 23 | + |
| 24 | +class BlockImporterItSpec extends MockFactory with TestSetupWithVmAndValidators with AsyncFlatSpecLike with Matchers with BeforeAndAfterAll { |
| 25 | + |
| 26 | + implicit val testScheduler = Scheduler.fixedPool("test", 32) |
| 27 | + |
| 28 | + override def afterAll(): Unit = { |
| 29 | + testScheduler.shutdown() |
| 30 | + testScheduler.awaitTermination(60.second) |
| 31 | + } |
| 32 | + |
| 33 | + val blockQueue = BlockQueue(blockchain, SyncConfig(Config.config)) |
| 34 | + |
| 35 | + val genesis = Block( |
| 36 | + Fixtures.Blocks.Genesis.header.copy(stateRoot = ByteString(MerklePatriciaTrie.EmptyRootHash)), |
| 37 | + Fixtures.Blocks.Genesis.body |
| 38 | + ) |
| 39 | + val genesisWeight = ChainWeight.zero.increase(genesis.header) |
| 40 | + |
| 41 | + blockchain.save(genesis, Seq(), genesisWeight, saveAsBestBlock = true) |
| 42 | + |
| 43 | + lazy val checkpointBlockGenerator: CheckpointBlockGenerator = new CheckpointBlockGenerator |
| 44 | + |
| 45 | + val fetcherProbe = TestProbe() |
| 46 | + val ommersPoolProbe = TestProbe() |
| 47 | + val broadcasterProbe = TestProbe() |
| 48 | + val pendingTransactionsManagerProbe = TestProbe() |
| 49 | + val supervisor = TestProbe() |
| 50 | + |
| 51 | + val emptyWorld: InMemoryWorldStateProxy = |
| 52 | + blockchain.getWorldStateProxy( |
| 53 | + -1, |
| 54 | + UInt256.Zero, |
| 55 | + ByteString(MerklePatriciaTrie.EmptyRootHash), |
| 56 | + noEmptyAccounts = false, |
| 57 | + ethCompatibleStorage = true |
| 58 | + ) |
| 59 | + |
| 60 | + override lazy val ledger = new TestLedgerImpl(successValidators) { |
| 61 | + override private[ledger] lazy val blockExecution = new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation) { |
| 62 | + override def executeAndValidateBlock(block: Block, alreadyValidated: Boolean = false): Either[BlockExecutionError, Seq[Receipt]] = |
| 63 | + Right(BlockResult(emptyWorld).receipts) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + val blockImporter = system.actorOf( |
| 68 | + BlockImporter.props( |
| 69 | + fetcherProbe.ref, |
| 70 | + ledger, |
| 71 | + blockchain, |
| 72 | + syncConfig, |
| 73 | + ommersPoolProbe.ref, |
| 74 | + broadcasterProbe.ref, |
| 75 | + pendingTransactionsManagerProbe.ref, |
| 76 | + checkpointBlockGenerator, |
| 77 | + supervisor.ref |
| 78 | + )) |
| 79 | + |
| 80 | + val genesisBlock = blockchain.genesisBlock |
| 81 | + val block1: Block = getBlock(genesisBlock.number + 1, parent = genesisBlock.header.hash) |
| 82 | + // new chain is shorter but has a higher weight |
| 83 | + val newBlock2: Block = getBlock(genesisBlock.number + 2, difficulty = 108, parent = block1.header.hash) |
| 84 | + val newBlock3: Block = getBlock(genesisBlock.number + 3, difficulty = 300, parent = newBlock2.header.hash) |
| 85 | + val oldBlock2: Block = getBlock(genesisBlock.number + 2, difficulty = 102, parent = block1.header.hash) |
| 86 | + val oldBlock3: Block = getBlock(genesisBlock.number + 3, difficulty = 103, parent = oldBlock2.header.hash) |
| 87 | + val oldBlock4: Block = getBlock(genesisBlock.number + 4, difficulty = 104, parent = oldBlock3.header.hash) |
| 88 | + |
| 89 | + val weight1 = ChainWeight.totalDifficultyOnly(block1.header.difficulty) |
| 90 | + val newWeight2 = weight1.increase(newBlock2.header) |
| 91 | + val newWeight3 = newWeight2.increase(newBlock3.header) |
| 92 | + val oldWeight2 = weight1.increase(oldBlock2.header) |
| 93 | + val oldWeight3 = oldWeight2.increase(oldBlock3.header) |
| 94 | + val oldWeight4 = oldWeight3.increase(oldBlock4.header) |
| 95 | + |
| 96 | + //saving initial main chain |
| 97 | + blockchain.save(block1, Nil, weight1, saveAsBestBlock = true) |
| 98 | + blockchain.save(oldBlock2, Nil, oldWeight2, saveAsBestBlock = true) |
| 99 | + blockchain.save(oldBlock3, Nil, oldWeight3, saveAsBestBlock = true) |
| 100 | + blockchain.save(oldBlock4, Nil, oldWeight4, saveAsBestBlock = true) |
| 101 | + |
| 102 | + val oldBranch = List(oldBlock2, oldBlock3, oldBlock4) |
| 103 | + val newBranch = List(newBlock2, newBlock3) |
| 104 | + |
| 105 | + blockImporter ! BlockImporter.Start |
| 106 | + |
| 107 | + "BlockImporter" should "not discard blocks of the main chain if the reorganisation failed" in { |
| 108 | + |
| 109 | + //ledger with not mocked blockExecution |
| 110 | + val ledger = new TestLedgerImpl(successValidators) |
| 111 | + val blockImporter = system.actorOf( |
| 112 | + BlockImporter.props( |
| 113 | + fetcherProbe.ref, |
| 114 | + ledger, |
| 115 | + blockchain, |
| 116 | + syncConfig, |
| 117 | + ommersPoolProbe.ref, |
| 118 | + broadcasterProbe.ref, |
| 119 | + pendingTransactionsManagerProbe.ref, |
| 120 | + checkpointBlockGenerator, |
| 121 | + supervisor.ref |
| 122 | + )) |
| 123 | + |
| 124 | + blockImporter ! BlockImporter.Start |
| 125 | + blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(newBranch)) |
| 126 | + |
| 127 | + Thread.sleep(1000) |
| 128 | + //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 |
| 129 | + blockchain.getBestBlock().get shouldEqual oldBlock4 |
| 130 | + } |
| 131 | + |
| 132 | + it should "return a correct new best block after reorganising longer chain to a shorter one if its weight is bigger" in { |
| 133 | + |
| 134 | + //returning discarded initial chain |
| 135 | + blockchain.save(oldBlock2, Nil, oldWeight2, saveAsBestBlock = true) |
| 136 | + blockchain.save(oldBlock3, Nil, oldWeight3, saveAsBestBlock = true) |
| 137 | + blockchain.save(oldBlock4, Nil, oldWeight4, saveAsBestBlock = true) |
| 138 | + |
| 139 | + blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(newBranch)) |
| 140 | + |
| 141 | + Thread.sleep(200) |
| 142 | + blockchain.getBestBlock().get shouldEqual newBlock3 |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + it should "switch to a branch with a checkpoint" in { |
| 147 | + |
| 148 | + val checkpoint = ObjectGenerators.fakeCheckpointGen(3, 3).sample.get |
| 149 | + val oldBlock5WithCheckpoint: Block = checkpointBlockGenerator.generate(oldBlock4, checkpoint) |
| 150 | + blockchain.save(oldBlock5WithCheckpoint, Nil, oldWeight4, saveAsBestBlock = true) |
| 151 | + |
| 152 | + val newBranch = List(newBlock2, newBlock3) |
| 153 | + |
| 154 | + blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(newBranch)) |
| 155 | + |
| 156 | + Thread.sleep(200) |
| 157 | + blockchain.getBestBlock().get shouldEqual oldBlock5WithCheckpoint |
| 158 | + blockchain.getLatestCheckpointBlockNumber() shouldEqual oldBlock5WithCheckpoint.header.number |
| 159 | + } |
| 160 | + |
| 161 | + it should "switch to a branch with a newer checkpoint" in { |
| 162 | + |
| 163 | + val checkpoint = ObjectGenerators.fakeCheckpointGen(3, 3).sample.get |
| 164 | + val newBlock4WithCheckpoint: Block = checkpointBlockGenerator.generate(newBlock3, checkpoint) |
| 165 | + blockchain.save(newBlock4WithCheckpoint, Nil, newWeight3, saveAsBestBlock = true) |
| 166 | + |
| 167 | + val newBranch = List(newBlock4WithCheckpoint) |
| 168 | + |
| 169 | + blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(newBranch)) |
| 170 | + |
| 171 | + Thread.sleep(200) |
| 172 | + blockchain.getBestBlock().get shouldEqual newBlock4WithCheckpoint |
| 173 | + blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock4WithCheckpoint.header.number |
| 174 | + } |
| 175 | + |
| 176 | + it should "return a correct checkpointed block after receiving a request for generating a new checkpoint" in { |
| 177 | + |
| 178 | + val parent = blockchain.getBestBlock().get |
| 179 | + val newBlock5: Block = getBlock(genesisBlock.number + 5, difficulty = 104, parent = parent.header.hash) |
| 180 | + val newWeight5 = newWeight3.increase(newBlock5.header) |
| 181 | + |
| 182 | + blockchain.save(newBlock5, Nil, newWeight5, saveAsBestBlock = true) |
| 183 | + |
| 184 | + val signatures = CheckpointingTestHelpers.createCheckpointSignatures( |
| 185 | + Seq(crypto.generateKeyPair(secureRandom)), |
| 186 | + newBlock5.hash |
| 187 | + ) |
| 188 | + blockImporter ! NewCheckpoint(newBlock5.hash, signatures) |
| 189 | + |
| 190 | + val checkpointBlock = checkpointBlockGenerator.generate(newBlock5, Checkpoint(signatures)) |
| 191 | + |
| 192 | + Thread.sleep(1000) |
| 193 | + blockchain.getBestBlock().get shouldEqual checkpointBlock |
| 194 | + blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock5.header.number + 1 |
| 195 | + } |
| 196 | +} |
0 commit comments