diff --git a/src/test/scala/units/BlockBriefValidationTestSuite.scala b/src/test/scala/units/BlockBriefValidationTestSuite.scala index ada4ebf5..f7364a4a 100644 --- a/src/test/scala/units/BlockBriefValidationTestSuite.scala +++ b/src/test/scala/units/BlockBriefValidationTestSuite.scala @@ -27,7 +27,14 @@ class BlockBriefValidationTestSuite extends BaseIntegrationTestSuite { } "otherwise ignoring" in test { d => - val ecBlock = d.createEcBlockBuilder("0", minerRewardL2Address = EthAddress.empty, parent = d.ecGenesisBlock).build() + val ecBlock = d + .createEcBlockBuilder( + "0", + minerRewardL2Address = EthAddress.empty, + parent = d.ecGenesisBlock, + epochNumber = d.blockchain.height + ) + .build() step(s"Receive ecBlock ${ecBlock.hash} from a peer") d.receiveNetworkBlock(ecBlock, miner.account) diff --git a/src/test/scala/units/BlockFullValidationTestSuite.scala b/src/test/scala/units/BlockFullValidationTestSuite.scala index 69b3c164..0ffd10fb 100644 --- a/src/test/scala/units/BlockFullValidationTestSuite.scala +++ b/src/test/scala/units/BlockFullValidationTestSuite.scala @@ -22,10 +22,9 @@ class BlockFullValidationTestSuite extends BaseIntegrationTestSuite { "Full validation when the block is available on EL and CL" - { "doesn't happen for finalized blocks" in withExtensionDomain(defaultSettings.copy(initialMiners = List(reliable))) { d => - val ecBlock = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(ecBlockLogs) - step("Start new epoch for ecBlock") d.advanceNewBlocks(reliable.address) + val ecBlock = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(ecBlockLogs) d.advanceConsensusLayerChanged() step(s"Receive ecBlock ${ecBlock.hash} from a peer") @@ -42,7 +41,7 @@ class BlockFullValidationTestSuite extends BaseIntegrationTestSuite { d.waitForWorking("Block considered validated and following") { s => val vs = s.fullValidationStatus - vs.validated should contain(ecBlock.hash) + vs.lastValidatedBlock.hash shouldBe ecBlock.hash vs.lastElWithdrawalIndex shouldBe empty is[FollowingChain](s.chainStatus) @@ -51,10 +50,9 @@ class BlockFullValidationTestSuite extends BaseIntegrationTestSuite { "happens for not finalized blocks" - { "successful validation updates the chain information" in withExtensionDomain() { d => - val ecBlock = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(ecBlockLogs) - step("Start new epoch for ecBlock") d.advanceNewBlocks(reliable.address) + val ecBlock = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(ecBlockLogs) d.advanceConsensusLayerChanged() step(s"Receive ecBlock ${ecBlock.hash} from a peer") @@ -73,7 +71,7 @@ class BlockFullValidationTestSuite extends BaseIntegrationTestSuite { d.waitForWorking("Block considered validated") { s => val vs = s.fullValidationStatus - vs.validated should contain(ecBlock.hash) + vs.lastValidatedBlock.hash shouldBe ecBlock.hash vs.lastElWithdrawalIndex.value shouldBe -1L } } @@ -107,12 +105,8 @@ class BlockFullValidationTestSuite extends BaseIntegrationTestSuite { d.receiveNetworkBlock(ecBlock2, malfunction.account) d.triggerScheduledTasks() - d.waitForWorking("Block considered validated and forking") { s => - val vs = s.fullValidationStatus - vs.validated should contain(ecBlock2.hash) - vs.lastElWithdrawalIndex shouldBe empty - - is[WaitForNewChain](s.chainStatus) + d.waitForCS[WaitForNewChain]("Forking") { cs => + cs.chainSwitchInfo.referenceBlock.hash shouldBe ecBlock1.hash } } diff --git a/src/test/scala/units/BlockIssuesForgingTestSuite.scala b/src/test/scala/units/BlockIssuesForgingTestSuite.scala index 1d0f223e..a8ced208 100644 --- a/src/test/scala/units/BlockIssuesForgingTestSuite.scala +++ b/src/test/scala/units/BlockIssuesForgingTestSuite.scala @@ -26,18 +26,18 @@ class BlockIssuesForgingTestSuite extends BaseIntegrationTestSuite { "We're on the main chain and" - { def test(f: (ExtensionDomain, EcBlock, Int) => Unit): Unit = withExtensionDomain() { d => - val ecBlock1 = d.createEcBlockBuilder("0", otherMiner1).buildAndSetLogs() - step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBlock1 ${ecBlock1.hash}") + step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBlock1") d.advanceNewBlocks(otherMiner1.address) + val ecBlock1 = d.createEcBlockBuilder("0", otherMiner1).buildAndSetLogs() d.ecClients.addKnown(ecBlock1) d.appendMicroBlockAndVerify(d.chainContract.extendMainChain(otherMiner1.account, ecBlock1)) d.waitForCS[FollowingChain]() { s => s.nodeChainInfo.lastBlock.hash shouldBe ecBlock1.hash } - val ecBlock2 = d.createEcBlockBuilder("0-0", otherMiner1, ecBlock1).rewardPrevMiner().buildAndSetLogs() - step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBlock2 ${ecBlock2.hash}") + step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBlock2") d.advanceNewBlocks(otherMiner1.address) + val ecBlock2 = d.createEcBlockBuilder("0-0", otherMiner1, ecBlock1).rewardPrevMiner().buildAndSetLogs() val ecBlock2Epoch = d.blockchain.height d.appendMicroBlockAndVerify(d.chainContract.extendMainChain(otherMiner1.account, ecBlock2)) @@ -76,9 +76,9 @@ class BlockIssuesForgingTestSuite extends BaseIntegrationTestSuite { "We're on the alternative chain and" - { "EC-block comes within timeout - then we continue forging" in withExtensionDomain() { d => - val ecBlock1 = d.createEcBlockBuilder("0", otherMiner1).buildAndSetLogs() - step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBlock1 ${ecBlock1.hash}") + step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBlock1") d.advanceNewBlocks(otherMiner1.address) + val ecBlock1 = d.createEcBlockBuilder("0", otherMiner1).buildAndSetLogs() d.ecClients.addKnown(ecBlock1) d.appendMicroBlockAndVerify(d.chainContract.extendMainChain(otherMiner1.account, ecBlock1)) @@ -87,9 +87,9 @@ class BlockIssuesForgingTestSuite extends BaseIntegrationTestSuite { s.nodeChainInfo.lastBlock.hash shouldBe ecBlock1.hash } - val ecBadBlock2 = d.createEcBlockBuilder("0-0", otherMiner1, ecBlock1).rewardPrevMiner().buildAndSetLogs() - step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBadBlock2 ${ecBadBlock2.hash}") + step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBadBlock2") d.advanceNewBlocks(otherMiner1.address) + val ecBadBlock2 = d.createEcBlockBuilder("0-0", otherMiner1, ecBlock1).rewardPrevMiner().buildAndSetLogs() d.appendMicroBlockAndVerify(d.chainContract.extendMainChain(otherMiner1.account, ecBadBlock2)) d.waitForCS[FollowingChain]() { s => @@ -97,9 +97,9 @@ class BlockIssuesForgingTestSuite extends BaseIntegrationTestSuite { s.nodeChainInfo.lastBlock.hash shouldBe ecBadBlock2.hash } - val ecBlock2 = d.createEcBlockBuilder("0-1", otherMiner2, ecBlock1).rewardPrevMiner().buildAndSetLogs() - step(s"Start a new epoch of otherMiner2 ${otherMiner2.address} with alternative chain ecBlock2 ${ecBlock2.hash}") + step(s"Start a new epoch of otherMiner2 ${otherMiner2.address} with alternative chain ecBlock2") d.advanceNewBlocks(otherMiner2.address) + val ecBlock2 = d.createEcBlockBuilder("0-1", otherMiner2, ecBlock1).rewardPrevMiner().buildAndSetLogs() d.waitForCS[WaitForNewChain]() { s => s.chainSwitchInfo.referenceBlock.hash shouldBe ecBlock1.hash @@ -120,9 +120,9 @@ class BlockIssuesForgingTestSuite extends BaseIntegrationTestSuite { s.nextExpectedBlock shouldBe empty } - val ecBlock3 = d.createEcBlockBuilder("0-1-1", otherMiner2, parent = ecBlock2).rewardPrevMiner(1).buildAndSetLogs() - step(s"Continue an alternative chain by otherMiner2 ${otherMiner2.address} with ecBlock3 ${ecBlock3.hash}") + step(s"Continue an alternative chain by otherMiner2 ${otherMiner2.address} with ecBlock3") d.advanceNewBlocks(otherMiner2.address) + val ecBlock3 = d.createEcBlockBuilder("0-1-1", otherMiner2, parent = ecBlock2).rewardPrevMiner(1).buildAndSetLogs() val ecBlock3Epoch = d.blockchain.height d.appendMicroBlockAndVerify(d.chainContract.extendAltChain(otherMiner2.account, ecBlock3, chainId = 1)) @@ -153,9 +153,9 @@ class BlockIssuesForgingTestSuite extends BaseIntegrationTestSuite { } "We mined before the alternative chain before and EC-block doesn't come - then we still wait for it" in withExtensionDomain() { d => - val ecBlock1 = d.createEcBlockBuilder("0", otherMiner1).buildAndSetLogs() - step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBlock1 ${ecBlock1.hash}") + step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBlock1") d.advanceNewBlocks(otherMiner1.address) + val ecBlock1 = d.createEcBlockBuilder("0", otherMiner1).buildAndSetLogs() d.ecClients.addKnown(ecBlock1) d.appendMicroBlockAndVerify(d.chainContract.extendMainChain(otherMiner1.account, ecBlock1)) @@ -164,9 +164,9 @@ class BlockIssuesForgingTestSuite extends BaseIntegrationTestSuite { s.nodeChainInfo.lastBlock.hash shouldBe ecBlock1.hash } - val ecBadBlock2 = d.createEcBlockBuilder("0-0", otherMiner1, ecBlock1).rewardPrevMiner().buildAndSetLogs() - step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBadBlock2 ${ecBadBlock2.hash}") + step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBadBlock2") d.advanceNewBlocks(otherMiner1.address) + val ecBadBlock2 = d.createEcBlockBuilder("0-0", otherMiner1, ecBlock1).rewardPrevMiner().buildAndSetLogs() d.appendMicroBlockAndVerify(d.chainContract.extendMainChain(otherMiner1.account, ecBadBlock2)) d.waitForCS[FollowingChain]() { s => @@ -174,9 +174,9 @@ class BlockIssuesForgingTestSuite extends BaseIntegrationTestSuite { s.nodeChainInfo.lastBlock.hash shouldBe ecBadBlock2.hash } - val ecBlock2 = d.createEcBlockBuilder("0-1", thisMiner, ecBlock1).rewardPrevMiner().buildAndSetLogs() - step(s"Start a new epoch of thisMiner ${thisMiner.address} with alternative chain ecBlock2 ${ecBlock2.hash}") + step(s"Start a new epoch of thisMiner ${thisMiner.address} with alternative chain ecBlock2") d.advanceNewBlocks(thisMiner.address) + val ecBlock2 = d.createEcBlockBuilder("0-1", thisMiner, ecBlock1).rewardPrevMiner().buildAndSetLogs() d.ecClients.willForge(ecBlock2) d.ecClients.willForge(d.createEcBlockBuilder("0-1-i", thisMiner, ecBlock2).buildAndSetLogs()) @@ -193,9 +193,9 @@ class BlockIssuesForgingTestSuite extends BaseIntegrationTestSuite { ci.lastBlock.hash shouldBe ecBlock2.hash } - val ecBadBlock3 = d.createEcBlockBuilder("0-1-1", otherMiner2, ecBlock2).rewardMiner(otherMiner2.elRewardAddress, 1).buildAndSetLogs() - step(s"Continue an alternative chain by otherMiner2 ${otherMiner2.address} with ecBadBlock3 ${ecBadBlock3.hash}") + step(s"Continue an alternative chain by otherMiner2 ${otherMiner2.address} with ecBadBlock3") d.advanceNewBlocks(otherMiner2.address) + val ecBadBlock3 = d.createEcBlockBuilder("0-1-1", otherMiner2, ecBlock2).rewardMiner(otherMiner2.elRewardAddress, 1).buildAndSetLogs() d.appendMicroBlockAndVerify(d.chainContract.extendAltChain(otherMiner2.account, ecBadBlock3, chainId = 1)) d.waitForCS[FollowingChain]() { s => @@ -219,9 +219,9 @@ class BlockIssuesForgingTestSuite extends BaseIntegrationTestSuite { "We haven't mined the alternative chain before and EC-block doesn't come - then we wait for a new alternative chain" in withExtensionDomain() { d => - val ecBlock1 = d.createEcBlockBuilder("0", otherMiner1).buildAndSetLogs() - step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBlock1 ${ecBlock1.hash}") + step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBlock1") d.advanceNewBlocks(otherMiner1.address) + val ecBlock1 = d.createEcBlockBuilder("0", otherMiner1).buildAndSetLogs() d.ecClients.addKnown(ecBlock1) d.appendMicroBlockAndVerify(d.chainContract.extendMainChain(otherMiner1.account, ecBlock1)) @@ -230,9 +230,9 @@ class BlockIssuesForgingTestSuite extends BaseIntegrationTestSuite { s.nodeChainInfo.lastBlock.hash shouldBe ecBlock1.hash } - val ecBadBlock2 = d.createEcBlockBuilder("0-0", otherMiner1, ecBlock1).rewardPrevMiner().buildAndSetLogs() - step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBadBlock2 ${ecBadBlock2.hash}") + step(s"Start a new epoch of otherMiner1 ${otherMiner1.address} with ecBadBlock2") d.advanceNewBlocks(otherMiner1.address) + val ecBadBlock2 = d.createEcBlockBuilder("0-0", otherMiner1, ecBlock1).rewardPrevMiner().buildAndSetLogs() d.appendMicroBlockAndVerify(d.chainContract.extendMainChain(otherMiner1.account, ecBadBlock2)) d.waitForCS[FollowingChain]() { s => @@ -240,9 +240,9 @@ class BlockIssuesForgingTestSuite extends BaseIntegrationTestSuite { s.nodeChainInfo.lastBlock.hash shouldBe ecBadBlock2.hash } - val ecBlock2 = d.createEcBlockBuilder("0-1", otherMiner2, ecBlock1).rewardPrevMiner().buildAndSetLogs() - step(s"Start a new epoch of otherMiner2 ${otherMiner2.address} with alternative chain ecBlock2 ${ecBlock2.hash}") + step(s"Start a new epoch of otherMiner2 ${otherMiner2.address} with alternative chain ecBlock2") d.advanceNewBlocks(otherMiner2.address) + val ecBlock2 = d.createEcBlockBuilder("0-1", otherMiner2, ecBlock1).rewardPrevMiner().buildAndSetLogs() d.waitForCS[WaitForNewChain]() { s => s.chainSwitchInfo.referenceBlock.hash shouldBe ecBlock1.hash @@ -263,9 +263,9 @@ class BlockIssuesForgingTestSuite extends BaseIntegrationTestSuite { s.nextExpectedBlock shouldBe empty } - val ecBlock3 = d.createEcBlockBuilder("0-1-1", otherMiner2, ecBlock2).rewardPrevMiner(1).buildAndSetLogs() - step(s"Continue an alternative chain by otherMiner2 ${otherMiner2.address} with ecBlock3 ${ecBlock3.hash}") + step(s"Continue an alternative chain by otherMiner2 ${otherMiner2.address} with ecBlock3") d.advanceNewBlocks(otherMiner2.address) + val ecBlock3 = d.createEcBlockBuilder("0-1-1", otherMiner2, ecBlock2).rewardPrevMiner(1).buildAndSetLogs() d.appendMicroBlockAndVerify(d.chainContract.extendAltChain(otherMiner2.account, ecBlock3, chainId = 1)) d.waitForCS[FollowingChain]() { s => diff --git a/src/test/scala/units/E2CTransfersTestSuite.scala b/src/test/scala/units/E2CTransfersTestSuite.scala index 1c2b8382..6881ccf5 100644 --- a/src/test/scala/units/E2CTransfersTestSuite.scala +++ b/src/test/scala/units/E2CTransfersTestSuite.scala @@ -52,15 +52,15 @@ class E2CTransfersTestSuite extends BaseIntegrationTestSuite { ) withExtensionDomain(settings) { d => - val ecBlock = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(ecBlockLogs) + step(s"Start new epoch for ecBlock") + d.advanceNewBlocks(reliable.address) + val ecBlock = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(ecBlockLogs) def tryWithdraw(): Either[Throwable, BlockId] = d.appendMicroBlockE( d.chainContract.withdraw(transferReceiver1, ecBlock, transfer1Proofs, 0, transfer1.amount), d.chainContract.withdraw(transferReceiver2, ecBlock, transfer2Proofs, 1, transfer2.amount) ) - step(s"Start new epoch for ecBlock ${ecBlock.hash}") - d.advanceNewBlocks(reliable.address) tryWithdraw() should produce("not found for the contract address") step("Append a CL micro block with ecBlock confirmation") @@ -88,10 +88,9 @@ class E2CTransfersTestSuite extends BaseIntegrationTestSuite { ) ) { case (index, errorMessage) => withExtensionDomain() { d => - val ecBlock = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(ecBlockLogs) - - step(s"Start new epoch with ecBlock ${ecBlock.hash}") + step(s"Start new epoch with ecBlock") d.advanceNewBlocks(reliable.address) + val ecBlock = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(ecBlockLogs) d.ecClients.addKnown(ecBlock) d.appendMicroBlockAndVerify(d.chainContract.extendMainChain(reliable.account, ecBlock, e2CTransfersRootHashHex)) d.advanceConsensusLayerChanged() @@ -111,10 +110,9 @@ class E2CTransfersTestSuite extends BaseIntegrationTestSuite { ) ) { amount => withExtensionDomain() { d => - val ecBlock = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(ecBlockLogs) - - step(s"Start new epoch with ecBlock ${ecBlock.hash}") + step(s"Start new epoch with ecBlock") d.advanceNewBlocks(reliable.address) + val ecBlock = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(ecBlockLogs) d.ecClients.addKnown(ecBlock) d.appendMicroBlockAndVerify(d.chainContract.extendMainChain(reliable.account, ecBlock, e2CTransfersRootHashHex)) d.advanceConsensusLayerChanged() @@ -127,13 +125,12 @@ class E2CTransfersTestSuite extends BaseIntegrationTestSuite { } "Can't get transferred tokens if the data is incorrect and able if it is correct" in withExtensionDomain() { d => + step(s"Start new epoch with ecBlock") + d.advanceNewBlocks(reliable.address) val ecBlock = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(ecBlockLogs) - def tryWithdraw(): Either[Throwable, BlockId] = d.appendMicroBlockE(d.chainContract.withdraw(transferReceiver, ecBlock, transferProofs, 0, transfer.amount)) - step(s"Start new epoch with ecBlock ${ecBlock.hash}") - d.advanceNewBlocks(reliable.address) tryWithdraw() should produce("not found for the contract address") d.ecClients.addKnown(ecBlock) d.appendMicroBlockAndVerify(d.chainContract.extendMainChain(reliable.account, ecBlock, e2CTransfersRootHashHex)) @@ -152,13 +149,12 @@ class E2CTransfersTestSuite extends BaseIntegrationTestSuite { ) withExtensionDomain(settings) { d => + step(s"Start new epoch with ecBlock") + d.advanceNewBlocks(reliable.address) val ecBlock = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(ecBlockLogs) - def tryWithdraw(): Either[Throwable, BlockId] = d.appendMicroBlockE(d.chainContract.withdraw(transferReceiver, ecBlock, transferProofs, 0, transfer.amount)) - step(s"Start new epoch with ecBlock ${ecBlock.hash}") - d.advanceNewBlocks(reliable.address) tryWithdraw() should produce("not found for the contract address") d.ecClients.addKnown(ecBlock) d.appendMicroBlockAndVerify(d.chainContract.extendMainChain(reliable.account, ecBlock, e2CTransfersRootHashHex)) @@ -186,14 +182,12 @@ class E2CTransfersTestSuite extends BaseIntegrationTestSuite { ) ) { transferEvent => withExtensionDomain(settings) { d => - val transferEvents = List(transferEvent) - val ecBlock1 = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(transferEvents) - + step(s"Start new epoch with ecBlock1") + d.advanceNewBlocks(reliable.address) + val ecBlock1 = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(List(transferEvent)) def tryWithdraw(): Either[Throwable, BlockId] = d.appendMicroBlockE(d.chainContract.withdraw(transferReceiver, ecBlock1, transferProofs, 0, transfer.amount)) - step(s"Start new epoch with ecBlock1 ${ecBlock1.hash}") - d.advanceNewBlocks(reliable.address) d.ecClients.willForge(ecBlock1) d.advanceConsensusLayerChanged() @@ -213,11 +207,9 @@ class E2CTransfersTestSuite extends BaseIntegrationTestSuite { "Fails on wrong data" in { val settings = defaultSettings.withEnabledElMining withExtensionDomain(settings) { d => - val transferEvents = List(transferEvent.copy(data = "d3ad884fa04292")) - val ecBlock1 = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(transferEvents) - - step(s"Start new epoch with ecBlock1 ${ecBlock1.hash}") + step(s"Start new epoch with ecBlock1") d.advanceNewBlocks(reliable.address) + val ecBlock1 = d.createEcBlockBuilder("0", reliable).buildAndSetLogs(List(transferEvent.copy(data = "d3ad884fa04292"))) d.ecClients.willForge(ecBlock1) d.ecClients.willForge(d.createEcBlockBuilder("0-0", reliable).build()) @@ -231,19 +223,19 @@ class E2CTransfersTestSuite extends BaseIntegrationTestSuite { "Can't get transferred tokens from a fork and can after the fork becomes a main chain" in { val settings = defaultSettings.copy(initialMiners = List(reliable, malfunction)).withEnabledElMining withExtensionDomain(settings) { d => + step(s"Start a new epoch of malfunction miner ${malfunction.address} with ecBlock1") + d.advanceNewBlocks(malfunction.address) // Need this block, because we can not rollback to the genesis block val ecBlock1 = d.createEcBlockBuilder("0", malfunction).buildAndSetLogs() - step(s"Start a new epoch of malfunction miner ${malfunction.address} with ecBlock1 ${ecBlock1.hash}") - d.advanceNewBlocks(malfunction.address) d.advanceConsensusLayerChanged() d.ecClients.addKnown(ecBlock1) d.appendMicroBlockAndVerify(d.chainContract.extendMainChain(malfunction.account, ecBlock1)) d.advanceConsensusLayerChanged() - val ecBadBlock2 = d.createEcBlockBuilder("0-0", malfunction, ecBlock1).rewardPrevMiner().buildAndSetLogs(ecBlockLogs) - step(s"Try to append a block with a wrong transfers root hash ${ecBadBlock2.hash}") + step(s"Try to append a block with a wrong transfers root hash") d.advanceNewBlocks(malfunction.address) + val ecBadBlock2 = d.createEcBlockBuilder("0-0", malfunction, ecBlock1).rewardPrevMiner().buildAndSetLogs(ecBlockLogs) d.advanceConsensusLayerChanged() // No root hash in extendMainChain tx @@ -255,9 +247,9 @@ class E2CTransfersTestSuite extends BaseIntegrationTestSuite { s.chainSwitchInfo.referenceBlock.hash shouldBe ecBlock1.hash } - val ecBlock2 = d.createEcBlockBuilder("0-1", reliable, ecBlock1).rewardPrevMiner().buildAndSetLogs(ecBlockLogs) - step(s"Start an alternative chain by a reliable miner ${reliable.address} with ecBlock2 ${ecBlock2.hash}") + step(s"Start an alternative chain by a reliable miner ${reliable.address} with ecBlock2") d.advanceNewBlocks(reliable.address) + val ecBlock2 = d.createEcBlockBuilder("0-1", reliable, ecBlock1).rewardPrevMiner().buildAndSetLogs(ecBlockLogs) d.ecClients.willForge(ecBlock2) // Prepare a following block, because we start mining it immediately d.ecClients.willForge(d.createEcBlockBuilder("0-1-1", reliable, ecBlock2).build()) @@ -287,9 +279,9 @@ class E2CTransfersTestSuite extends BaseIntegrationTestSuite { tryWithdraw() should produce("is not finalized") } - val ecBlock3 = d.createEcBlockBuilder("0-1-1-1", reliable, ecBlock2).rewardPrevMiner(1).buildAndSetLogs() - step(s"Moving whole network to the alternative chain with ecBlock3 ${ecBlock3.hash}") + step(s"Moving whole network to the alternative chain with ecBlock3") d.advanceNewBlocks(reliable.address) + val ecBlock3 = d.createEcBlockBuilder("0-1-1-1", reliable, ecBlock2).rewardPrevMiner(1).buildAndSetLogs() d.ecClients.willForge(ecBlock3) d.advanceConsensusLayerChanged() diff --git a/src/test/scala/units/ExtensionDomain.scala b/src/test/scala/units/ExtensionDomain.scala index c7e87c9f..bb2502cf 100644 --- a/src/test/scala/units/ExtensionDomain.scala +++ b/src/test/scala/units/ExtensionDomain.scala @@ -12,13 +12,6 @@ import com.wavesplatform.database.{RDB, RocksDBWriter} import com.wavesplatform.events.UtxEvent import com.wavesplatform.extensions.Context import com.wavesplatform.history.Domain -import units.* -import units.ELUpdater.calculateRandao -import units.client.contract.HasConsensusLayerDappTxHelpers.EmptyE2CTransfersRootHashHex -import units.client.http.model.{EcBlock, TestEcBlocks} -import units.client.{L2BlockLike, TestEcClients} -import units.eth.{EthereumConstants, Gwei} -import units.network.TestBlocksObserver import com.wavesplatform.lang.ValidationError import com.wavesplatform.lang.v1.compiler.Terms import com.wavesplatform.mining.MultiDimensionalMiningConstraint @@ -43,8 +36,8 @@ import net.ceedubs.ficus.Ficus.* import org.scalatest.exceptions.TestFailedException import org.web3j.abi.datatypes.generated.Uint256 import play.api.libs.json.* -import units.ELUpdater.* import units.ELUpdater.State.{ChainStatus, Working} +import units.ELUpdater.* import units.ExtensionDomain.* import units.client.contract.HasConsensusLayerDappTxHelpers import units.client.contract.HasConsensusLayerDappTxHelpers.EmptyE2CTransfersRootHashHex @@ -87,6 +80,7 @@ class ExtensionDomain( baseFeePerGas = Uint256.DEFAULT, gasLimit = 0, gasUsed = 0, + prevRandao = EthereumConstants.EmptyPrevRandaoHex, withdrawals = Vector.empty ) @@ -349,14 +343,23 @@ class ExtensionDomain( utxPool.close() } - def createEcBlockBuilder(hashPath: String, miner: ElMinerSettings, parent: EcBlock = ecGenesisBlock): TestEcBlockBuilder = - createEcBlockBuilder(hashPath, miner.elRewardAddress, parent) + def createEcBlockBuilder( + hashPath: String, + miner: ElMinerSettings, + parent: EcBlock = ecGenesisBlock, + epochNumber: Int = blockchain.height + ): TestEcBlockBuilder = + createEcBlockBuilder(hashPath, miner.elRewardAddress, parent, epochNumber) - def createEcBlockBuilder(hashPath: String, minerRewardL2Address: EthAddress, parent: EcBlock): TestEcBlockBuilder = + def createEcBlockBuilder(hashPath: String, minerRewardL2Address: EthAddress, parent: EcBlock, epochNumber: Int): TestEcBlockBuilder = TestEcBlockBuilder(ecClients, elMinerDefaultReward, l2Config.blockDelay, parent = parent).updateBlock( _.copy( hash = TestEcBlockBuilder.createBlockHash(hashPath), - minerRewardL2Address = minerRewardL2Address + minerRewardL2Address = minerRewardL2Address, + prevRandao = calculateRandao( + blockchain.vrf(epochNumber).getOrElse(throw new RuntimeException(s"VRF is empty for epoch $epochNumber")), + parent.hash + ) ) ) diff --git a/src/test/scala/units/TestEcBlockBuilder.scala b/src/test/scala/units/TestEcBlockBuilder.scala index 817d6453..a1ae7ee0 100644 --- a/src/test/scala/units/TestEcBlockBuilder.scala +++ b/src/test/scala/units/TestEcBlockBuilder.scala @@ -54,6 +54,7 @@ object TestEcBlockBuilder { baseFeePerGas = Uint256.DEFAULT, gasLimit = 0, gasUsed = 0, + prevRandao = EthereumConstants.EmptyPrevRandaoHex, withdrawals = Vector.empty ), parent diff --git a/src/test/scala/units/client/TestEcClients.scala b/src/test/scala/units/client/TestEcClients.scala index 01004425..0e52db0f 100644 --- a/src/test/scala/units/client/TestEcClients.scala +++ b/src/test/scala/units/client/TestEcClients.scala @@ -6,7 +6,6 @@ import com.wavesplatform.state.Blockchain import com.wavesplatform.utils.ScorexLogging import monix.execution.atomic.{Atomic, AtomicInt} import play.api.libs.json.JsObject -import units.ELUpdater.calculateRandao import units.client.TestEcClients.* import units.client.engine.EngineApiClient.PayloadId import units.client.engine.model.Withdrawal @@ -14,54 +13,44 @@ import units.client.engine.{EngineApiClient, LoggedEngineApiClient} import units.client.http.model.* import units.client.http.{EcApiClient, LoggedEcApiClient} import units.collections.ListOps.* -import units.eth.{EthAddress, EthereumConstants} +import units.eth.EthAddress import units.{BlockHash, Job, NetworkL2Block} -import scala.collection.View -import scala.util.chaining.scalaUtilChainingOps - class TestEcClients private ( knownBlocks: Atomic[Map[BlockHash, ChainId]], - chains: Atomic[Map[ChainId, List[TestEcBlock]]], + chains: Atomic[Map[ChainId, List[EcBlock]]], currChainIdValue: AtomicInt, blockchain: Blockchain ) extends ScorexLogging { def this(genesis: EcBlock, blockchain: Blockchain) = this( knownBlocks = Atomic(Map(genesis.hash -> 0)), - chains = Atomic(Map(0 -> List(TestEcBlock(genesis, EthereumConstants.EmptyPrevRandaoHex)))), + chains = Atomic(Map(0 -> List(genesis))), currChainIdValue = AtomicInt(0), blockchain = blockchain ) private def currChainId: ChainId = currChainIdValue.get() - def addKnown(ecBlock: EcBlock, epochNumber: Int = blockchain.height): TestEcBlock = { + def addKnown(ecBlock: EcBlock): EcBlock = { knownBlocks.transform(_.updated(ecBlock.hash, currChainId)) - mkTestEcBlock(ecBlock, epochNumber).tap(prependToCurrentChain) + prependToCurrentChain(ecBlock) + ecBlock } - private def mkTestEcBlock(ecBlock: EcBlock, epochNumber: Int): TestEcBlock = TestEcBlock( - ecBlock, - calculateRandao( - blockchain.vrf(epochNumber).getOrElse(throw new RuntimeException(s"VRF is empty for epoch $epochNumber")), - ecBlock.parentHash - ) - ) - - private def prependToCurrentChain(b: TestEcBlock): Unit = + private def prependToCurrentChain(b: EcBlock): Unit = prependToChain(currChainId, b) - private def prependToChain(chainId: ChainId, b: TestEcBlock): Unit = + private def prependToChain(chainId: ChainId, b: EcBlock): Unit = chains.transform { chains => chains.updated(chainId, b :: chains(chainId)) } - private def currChain: View[EcBlock] = - chains.get().getOrElse(currChainId, throw new RuntimeException(s"Unknown chain $currChainId")).view.map(_.ecBlock) + private def currChain: List[EcBlock] = + chains.get().getOrElse(currChainId, throw new RuntimeException(s"Unknown chain $currChainId")) private val forgingBlocks = Atomic(List.empty[ForgingBlock]) - def willForge(ecBlock: EcBlock, epochNumber: Int = blockchain.height): Unit = - forgingBlocks.transform(ForgingBlock(mkTestEcBlock(ecBlock, epochNumber)) :: _) + def willForge(ecBlock: EcBlock): Unit = + forgingBlocks.transform(ForgingBlock(ecBlock) :: _) private val logs = Atomic(Map.empty[GetLogsRequest, List[GetLogsResponseEntry]]) def setBlockLogs(hash: BlockHash, topic: String, blockLogs: List[GetLogsResponseEntry]): Unit = { @@ -82,7 +71,7 @@ class TestEcClients private ( case Some(cid) => currChainIdValue.set(cid) chains.transform { chains => - chains.updated(cid, chains(cid).dropWhile(_.ecBlock.hash != blockHash)) + chains.updated(cid, chains(cid).dropWhile(_.hash != blockHash)) } log.debug(s"Curr chain: ${currChain.map(_.hash).mkString(" <- ")}") "VALID" @@ -102,10 +91,10 @@ class TestEcClients private ( ): Job[PayloadId] = forgingBlocks .get() - .collectFirst { case fb if fb.testBlock.ecBlock.parentHash == lastBlockHash => fb } match { + .collectFirst { case fb if fb.testBlock.parentHash == lastBlockHash => fb } match { case None => throw new RuntimeException( - s"Can't find a suitable block among: ${forgingBlocks.get().map(_.testBlock.ecBlock.hash).mkString(", ")}. Call willForge" + s"Can't find a suitable block among: ${forgingBlocks.get().map(_.testBlock.hash).mkString(", ")}. Call willForge" ) case Some(fb) => fb.payloadId.asRight @@ -113,26 +102,25 @@ class TestEcClients private ( override def getPayload(payloadId: PayloadId): Job[JsObject] = forgingBlocks.transformAndExtract(_.withoutFirst { fb => fb.payloadId == payloadId }) match { - case Some(fb) => TestEcBlocks.toPayload(fb.testBlock.ecBlock, fb.testBlock.prevRandao).asRight + case Some(fb) => TestEcBlocks.toPayload(fb.testBlock, fb.testBlock.prevRandao).asRight case None => throw new RuntimeException( - s"Can't find payload $payloadId among: ${forgingBlocks.get().map(_.testBlock.ecBlock.hash).mkString(", ")}. Call willForge" + s"Can't find payload $payloadId among: ${forgingBlocks.get().map(_.testBlock.hash).mkString(", ")}. Call willForge" ) } override def applyNewPayload(payload: JsObject): Job[Option[BlockHash]] = { - val newBlock = NetworkL2Block(payload).explicitGet().toEcBlock - val newTestBlock = TestEcBlock(newBlock, (payload \ "prevRandao").as[String]) + val newBlock = NetworkL2Block(payload).explicitGet().toEcBlock knownBlocks.get().get(newBlock.parentHash) match { case Some(cid) => val chain = chains.get()(cid) - if (newBlock.parentHash == chain.head.ecBlock.hash) { - prependToChain(cid, newTestBlock) + if (newBlock.parentHash == chain.head.hash) { + prependToChain(cid, newBlock) knownBlocks.transform(_.updated(newBlock.hash, cid)) } else { // Rollback log.debug(s"A rollback using ${newBlock.hash} detected") val newCid = currChainIdValue.incrementAndGet() - val newChain = newTestBlock :: chain.dropWhile(_.ecBlock.hash != newBlock.parentHash) + val newChain = newBlock :: chain.dropWhile(_.hash != newBlock.parentHash) chains.transform(_.updated(newCid, newChain)) knownBlocks.transform(_.updated(newBlock.hash, newCid)) } @@ -159,8 +147,8 @@ class TestEcClients private ( for { cid <- knownBlocks.get().get(hash) c <- chains.get().get(cid) - b <- c.find(_.ecBlock.hash == hash) - } yield b.ecBlock + b <- c.find(_.hash == hash) + } yield b }.asRight override def getBlockByHashJson(hash: BlockHash, fullTxs: Boolean): Job[Option[JsObject]] = @@ -188,11 +176,9 @@ object TestEcClients { private type ChainId = Int - private case class ForgingBlock(payloadId: String, testBlock: TestEcBlock) + private case class ForgingBlock(payloadId: String, testBlock: EcBlock) private object ForgingBlock { - def apply(testBlock: TestEcBlock): ForgingBlock = - new ForgingBlock(testBlock.ecBlock.hash.take(16), testBlock) + def apply(testBlock: EcBlock): ForgingBlock = + new ForgingBlock(testBlock.hash.take(16), testBlock) } - - case class TestEcBlock(ecBlock: EcBlock, prevRandao: String) }