From 08348f8439e248cc2d4af4c6b4abcdecccca99bb Mon Sep 17 00:00:00 2001 From: Reynold Morel Date: Thu, 15 Jun 2023 14:58:20 -0400 Subject: [PATCH] Fixing tests --- .../co/rsk/mine/TransactionModuleTest.java | 63 +++-- .../test/java/co/rsk/test/DslFilesTest.java | 83 ++++-- .../src/test/java/co/rsk/test/World.java | 4 + .../co/rsk/test/builders/BlockBuilder.java | 10 +- .../jsontestsuite/GitHubStateTest.java | 26 +- .../org/ethereum/rpc/Web3ImplLogsTest.java | 259 ++++++++++-------- .../ethereum/vm/opcodes/RevertOpCodeTest.java | 39 ++- 7 files changed, 328 insertions(+), 156 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/mine/TransactionModuleTest.java b/rskj-core/src/test/java/co/rsk/mine/TransactionModuleTest.java index 5a69a05b808..8df22592c8d 100644 --- a/rskj-core/src/test/java/co/rsk/mine/TransactionModuleTest.java +++ b/rskj-core/src/test/java/co/rsk/mine/TransactionModuleTest.java @@ -80,6 +80,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; import java.math.BigInteger; import java.time.Clock; @@ -92,23 +93,45 @@ class TransactionModuleTest { private BlockFactory blockFactory; private SignatureCache signatureCache; private TransactionExecutorFactory transactionExecutorFactory; - private ActivationConfig activationConfig; private Constants constants; private Block executionBlock; + private ActivationConfig activationConfigSpy; + private ActivationConfig activationConfig; + private ActivationConfig.ForBlock activationConfigForBlock; + private World world; @BeforeEach void setUp() { executionBlock = Mockito.mock(Block.class); constants = Mockito.mock(Constants.class); config = Mockito.spy(new TestSystemProperties()); - activationConfig = Mockito.spy(config.getActivationConfig()); + activationConfig = config.getActivationConfig(); + activationConfigSpy = Mockito.spy(activationConfig); + + Mockito.when(executionBlock.getNumber()).thenReturn(10L); + Mockito.doReturn(activationConfigSpy).when(config).getActivationConfig(); + + Mockito.doReturn(false) + .when(activationConfigSpy).isActive(Mockito.eq(ConsensusRule.RSKIPXXX), Mockito.anyLong()); + + Mockito.doAnswer(i1 -> { + activationConfigForBlock = Mockito.spy(activationConfig.forBlock(i1.getArgument(0))); + + Mockito.doAnswer(i2 -> { + if (i2.getArgument(0).equals(ConsensusRule.RSKIPXXX)) { + return false; + } - Mockito.when(config.getActivationConfig()).thenReturn(activationConfig); + return i2.callRealMethod(); + }).when(activationConfigForBlock).isActive(Mockito.any()); - blockFactory = new BlockFactory(activationConfig); + return activationConfigForBlock; + }).when(activationConfigSpy).forBlock(Mockito.anyLong()); + + blockFactory = new BlockFactory(activationConfigSpy); signatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache()); - Mockito.when(executionBlock.getNumber()).thenReturn(10L); + world = new World(config); } @Test @@ -118,25 +141,35 @@ void testTransactionCostWithRSKIPXXXDisabled() { byte[] bytes = new byte[]{-8, 96, -128, 8, -126, -61, 80, -108, -31, 126, -117, -65, -39, -94, 75, -27, 104, -101, 13, -118, 50, 8, 31, -83, -40, -94, 59, 107, 7, -127, -1, 102, -96, -63, -110, 91, -2, 42, -19, 18, 4, 67, -64, 48, -45, -85, -123, 41, 14, -48, -124, 118, 21, -63, -39, -45, 67, 116, -103, 93, 37, 4, 88, -61, 49, -96, 77, -30, -116, 59, -58, -82, -95, 76, 46, 124, 115, -32, -80, 125, 30, -42, -75, -111, -49, -41, 121, -73, -121, -68, -41, 72, -120, 94, 82, 42, 17, 61}; Transaction txInBlock = new ImmutableTransaction(bytes); - Assertions.assertEquals(txInBlock.transactionCost(constants, activationConfig.forBlock(executionBlock.getNumber()), new BlockTxSignatureCache(new ReceivedTxSignatureCache())), 21068L); + Assertions.assertEquals(txInBlock.transactionCost(constants, activationConfigSpy.forBlock(executionBlock.getNumber()), new BlockTxSignatureCache(new ReceivedTxSignatureCache())), 21068L); } @Test void testTransactionCostWithRSKIPXXXEnabled() { when(executionBlock.getGasLimit()).thenReturn(BigInteger.valueOf(6800000).toByteArray()); + Mockito.doAnswer(i1 -> { + activationConfigForBlock = Mockito.spy(activationConfig.forBlock(i1.getArgument(0))); + + Mockito.doAnswer(i2 -> { + if (i2.getArgument(0).equals(ConsensusRule.RSKIPXXX)) { + return true; + } + + return i2.callRealMethod(); + }).when(activationConfigForBlock).isActive(Mockito.any()); + + return activationConfigForBlock; + }).when(activationConfigSpy).forBlock(Mockito.anyLong()); + byte[] bytes = new byte[]{-8, 96, -128, 8, -126, -61, 80, -108, -31, 126, -117, -65, -39, -94, 75, -27, 104, -101, 13, -118, 50, 8, 31, -83, -40, -94, 59, 107, 7, -127, -1, 102, -96, -63, -110, 91, -2, 42, -19, 18, 4, 67, -64, 48, -45, -85, -123, 41, 14, -48, -124, 118, 21, -63, -39, -45, 67, 116, -103, 93, 37, 4, 88, -61, 49, -96, 77, -30, -116, 59, -58, -82, -95, 76, 46, 124, 115, -32, -80, 125, 30, -42, -75, -111, -49, -41, 121, -73, -121, -68, -41, 72, -120, 94, 82, 42, 17, 61}; Transaction txInBlock = new ImmutableTransaction(bytes); - Mockito.doReturn(true) - .when(activationConfig).isActive(Mockito.eq(ConsensusRule.RSKIPXXX), Mockito.anyLong()); - - Assertions.assertEquals(txInBlock.transactionCost(constants, activationConfig.forBlock(executionBlock.getNumber()), new BlockTxSignatureCache(new ReceivedTxSignatureCache())), 21016L); + Assertions.assertEquals(txInBlock.transactionCost(constants, activationConfigSpy.forBlock(executionBlock.getNumber()), new BlockTxSignatureCache(new ReceivedTxSignatureCache())), 21016L); } @Test void sendTransactionMustNotBeMined() { - World world = new World(); BlockChainImpl blockchain = world.getBlockChain(); TrieStore trieStore = world.getTrieStore(); @@ -163,7 +196,6 @@ void sendTransactionMustNotBeMined() { @Test void sendTransactionMustBeMined() { - World world = new World(); BlockChainImpl blockchain = world.getBlockChain(); TrieStore trieStore = world.getTrieStore(); @@ -196,7 +228,7 @@ void sendTransactionMustBeMined() { @Test void sendSeveralTransactionsWithAutoMining() { ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB()); - World world = new World(receiptStore); + world = new World(config, receiptStore); BlockChainImpl blockchain = world.getBlockChain(); MiningMainchainView mainchainView = new MiningMainchainViewImpl(world.getBlockStore(), 1); @@ -229,7 +261,7 @@ true, createStateRootHandler(), repositoryLocator, void sendRawTransactionWithAutoMining() throws Exception { ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB()); - World world = new World(receiptStore); + world = new World(config, receiptStore); BlockChainImpl blockchain = world.getBlockChain(); TrieStore trieStore = world.getTrieStore(); @@ -258,7 +290,7 @@ void sendRawTransactionWithAutoMining() throws Exception { void sendRawTransactionWithoutAutoMining() { ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB()); - World world = new World(receiptStore); + world = new World(config, receiptStore); BlockChainImpl blockchain = world.getBlockChain(); TrieStore trieStore = world.getTrieStore(); @@ -281,7 +313,6 @@ void sendRawTransactionWithoutAutoMining() { @Test void testGasEstimation() { - World world = new World(); Blockchain blockchain = world.getBlockChain(); TrieStore trieStore = world.getTrieStore(); diff --git a/rskj-core/src/test/java/co/rsk/test/DslFilesTest.java b/rskj-core/src/test/java/co/rsk/test/DslFilesTest.java index 3028f8cb729..1a5df272df8 100644 --- a/rskj-core/src/test/java/co/rsk/test/DslFilesTest.java +++ b/rskj-core/src/test/java/co/rsk/test/DslFilesTest.java @@ -28,6 +28,9 @@ import com.typesafe.config.ConfigValueFactory; import org.bouncycastle.util.BigIntegers; import org.bouncycastle.util.encoders.Hex; +import org.ethereum.config.Constants; +import org.ethereum.config.blockchain.upgrades.ActivationConfig; +import org.ethereum.config.blockchain.upgrades.ConsensusRule; import org.ethereum.core.Block; import org.ethereum.core.Repository; import org.ethereum.core.Transaction; @@ -35,7 +38,9 @@ import org.ethereum.db.TransactionInfo; import org.ethereum.vm.DataWord; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import java.io.FileNotFoundException; import java.math.BigInteger; @@ -45,10 +50,39 @@ * Created by ajlopez on 8/7/2016. */ class DslFilesTest { + private World world; + + @BeforeEach + public void setup() { + TestSystemProperties config = Mockito.spy(new TestSystemProperties()); + ActivationConfig activationConfig = config.getActivationConfig(); + ActivationConfig activationConfigSpy = Mockito.spy(activationConfig); + + Mockito.doReturn(activationConfigSpy).when(config).getActivationConfig(); + + Mockito.doReturn(false) + .when(activationConfigSpy).isActive(Mockito.eq(ConsensusRule.RSKIPXXX), Mockito.anyLong()); + + Mockito.doAnswer(i1 -> { + ActivationConfig.ForBlock activationConfigForBlock = Mockito.spy(activationConfig.forBlock(i1.getArgument(0))); + + Mockito.doAnswer(i2 -> { + if (i2.getArgument(0).equals(ConsensusRule.RSKIPXXX)) { + return false; + } + + return i2.callRealMethod(); + }).when(activationConfigForBlock).isActive(Mockito.any()); + + return activationConfigForBlock; + }).when(activationConfigSpy).forBlock(Mockito.anyLong()); + + world = new World(config); + } + @Test void runAccounts01Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/accounts01.txt"); - World world = new World(); WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -58,7 +92,6 @@ void runAccounts01Resource() throws FileNotFoundException, DslProcessorException @Test void runTransfers01Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/transfers01.txt"); - World world = new World(); WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -71,7 +104,6 @@ void runTransfers01Resource() throws FileNotFoundException, DslProcessorExceptio @Test void runCreate01Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/create01.txt"); - World world = new World(); WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -96,7 +128,6 @@ void runCreate02Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/create02.txt"); - World world = new World(); WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -158,7 +189,6 @@ void runCreate02Resource() throws FileNotFoundException, DslProcessorException { @Test void runCreateContractAndPreserveBalance() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/create_and_preserve_balance.txt"); - World world = new World(); WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -186,7 +216,6 @@ void runCreateContractAndPreserveNoBalance() throws FileNotFoundException, DslPr @Test void runContracts01Resource() throws FileNotFoundException { DslParser parser = DslParser.fromResource("dsl/contracts01.txt"); - World world = new World(); WorldDslProcessor processor = new WorldDslProcessor(world); Assertions.assertDoesNotThrow(() -> processor.processCommands(parser)); } @@ -194,7 +223,7 @@ void runContracts01Resource() throws FileNotFoundException { @Test void runContracts02Resource() throws FileNotFoundException { DslParser parser = DslParser.fromResource("dsl/contracts02.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); Assertions.assertDoesNotThrow(() -> processor.processCommands(parser)); } @@ -202,7 +231,7 @@ void runContracts02Resource() throws FileNotFoundException { @Test void runContracts03Resource() throws FileNotFoundException { DslParser parser = DslParser.fromResource("dsl/contracts03.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); Assertions.assertDoesNotThrow(() -> processor.processCommands(parser)); } @@ -210,7 +239,7 @@ void runContracts03Resource() throws FileNotFoundException { @Test void runContracts04Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/contracts04.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); Assertions.assertDoesNotThrow(() -> processor.processCommands(parser)); } @@ -218,7 +247,7 @@ void runContracts04Resource() throws FileNotFoundException, DslProcessorExceptio @Test void runContracts05Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/contracts05.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); Assertions.assertDoesNotThrow(() -> processor.processCommands(parser)); } @@ -226,7 +255,7 @@ void runContracts05Resource() throws FileNotFoundException, DslProcessorExceptio @Test void runContracts06Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/contracts06.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); Assertions.assertDoesNotThrow(() -> processor.processCommands(parser)); } @@ -234,7 +263,7 @@ void runContracts06Resource() throws FileNotFoundException, DslProcessorExceptio @Test void runLogs01Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/logs01.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -279,7 +308,7 @@ void runLogs01Resource() throws FileNotFoundException, DslProcessorException { @Test void runContracts07Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/contracts07.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); Assertions.assertDoesNotThrow(() -> processor.processCommands(parser)); } @@ -287,7 +316,7 @@ void runContracts07Resource() throws FileNotFoundException, DslProcessorExceptio @Test void runBlocks01Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/blocks01.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -299,7 +328,7 @@ void runBlocks01Resource() throws FileNotFoundException, DslProcessorException { @Test void runBlocks02Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/blocks02.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -311,7 +340,7 @@ void runBlocks02Resource() throws FileNotFoundException, DslProcessorException { @Test void runBlocks03Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/blocks03.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -323,7 +352,7 @@ void runBlocks03Resource() throws FileNotFoundException, DslProcessorException { @Test void runUncles01Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/uncles01.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -337,7 +366,7 @@ void runUncles01Resource() throws FileNotFoundException, DslProcessorException { @Test void runUncles02Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/uncles02.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -352,7 +381,7 @@ void runUncles02Resource() throws FileNotFoundException, DslProcessorException { @Test void runUncles03Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/uncles03.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -367,7 +396,7 @@ void runUncles03Resource() throws FileNotFoundException, DslProcessorException { @Test void runUncles04Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/uncles04.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -382,7 +411,7 @@ void runUncles04Resource() throws FileNotFoundException, DslProcessorException { @Test void runUncles05Resource() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/uncles05.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -400,7 +429,7 @@ void runUncles05Resource() throws FileNotFoundException, DslProcessorException { @Test void runCreateAfterSuicide() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/createAfterSuicide.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -420,7 +449,7 @@ void runCreateAfterSuicide() throws FileNotFoundException, DslProcessorException @Test void runCodeSizeAfterSuicide() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/codeSizeAfterSuicide.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -442,7 +471,7 @@ void runCodeSizeAfterSuicide() throws FileNotFoundException, DslProcessorExcepti @Test void onReorganizationTxGetsReaddedToTxPool() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/reorganization.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -454,7 +483,7 @@ void onReorganizationTxGetsReaddedToTxPool() throws FileNotFoundException, DslPr @Test void onReorganizationTxDoesNotGetsReaddedToTxPoolIfPresentOnBothChains() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/reorganization_same_tx_on_both.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -465,7 +494,7 @@ void onReorganizationTxDoesNotGetsReaddedToTxPoolIfPresentOnBothChains() throws @Test void onReorganizationTxDoesNotGetsReaddedIfCompetingTxIsOnBlock() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/reorganization_different_tx_on_both_same_nonce.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -476,7 +505,7 @@ void onReorganizationTxDoesNotGetsReaddedIfCompetingTxIsOnBlock() throws FileNot @Test void onReorganizationTxDoesGetsReaddedIfNonCompetingTxIsOnBlock() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/reorganization_different_non_competing_tx.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); diff --git a/rskj-core/src/test/java/co/rsk/test/World.java b/rskj-core/src/test/java/co/rsk/test/World.java index ad909e61663..d3581bf531e 100644 --- a/rskj-core/src/test/java/co/rsk/test/World.java +++ b/rskj-core/src/test/java/co/rsk/test/World.java @@ -88,6 +88,10 @@ public World(ReceiptStore receiptStore) { this(new BlockChainBuilder().setReceiptStore(receiptStore)); } + public World(RskSystemProperties config, ReceiptStore receiptStore) { + this(new BlockChainBuilder().setConfig(config).setReceiptStore(receiptStore)); + } + @VisibleForTesting public World(BlockChainBuilder blockChainBuilder) { this(blockChainBuilder.build(), blockChainBuilder.getBlockStore(), blockChainBuilder.getReceiptStore(), blockChainBuilder.getTrieStore(), blockChainBuilder.getRepository(), blockChainBuilder.getTransactionPool(), null, diff --git a/rskj-core/src/test/java/co/rsk/test/builders/BlockBuilder.java b/rskj-core/src/test/java/co/rsk/test/builders/BlockBuilder.java index c680bf60e3f..05dfeca9314 100644 --- a/rskj-core/src/test/java/co/rsk/test/builders/BlockBuilder.java +++ b/rskj-core/src/test/java/co/rsk/test/builders/BlockBuilder.java @@ -19,6 +19,7 @@ package co.rsk.test.builders; import co.rsk.blockchain.utils.BlockGenerator; +import co.rsk.config.RskSystemProperties; import co.rsk.config.TestSystemProperties; import co.rsk.core.TransactionExecutorFactory; import co.rsk.core.bc.BlockExecutor; @@ -36,6 +37,7 @@ import java.math.BigInteger; import java.util.List; +import java.util.Optional; /** * Created by ajlopez on 8/6/2016. @@ -52,6 +54,7 @@ public class BlockBuilder { private byte[] gasLimit; private final BridgeSupportFactory bridgeSupportFactory; private BlockStore blockStore; + private RskSystemProperties config; public BlockBuilder(Blockchain blockChain, BridgeSupportFactory bridgeSupportFactory, BlockStore blockStore) { this.blockChain = blockChain; @@ -86,6 +89,11 @@ public BlockBuilder minGasPrice(BigInteger minGasPrice) { return this; } + public BlockBuilder config(RskSystemProperties config) { + this.config = config; + return this; + } + /** * This has to be called after .parent() in order to have any effect */ @@ -103,7 +111,7 @@ public Block build() { Block block = blockGenerator.createChildBlock(parent, txs, uncles, difficulty, this.minGasPrice, gasLimit); if (blockChain != null) { - final TestSystemProperties config = new TestSystemProperties(); + final RskSystemProperties config = Optional.ofNullable(this.config).orElse(new TestSystemProperties()); StateRootHandler stateRootHandler = new StateRootHandler(config.getActivationConfig(), new StateRootsStoreImpl(new HashMapDB())); BlockExecutor executor = new BlockExecutor( config.getActivationConfig(), diff --git a/rskj-core/src/test/java/org/ethereum/jsontestsuite/GitHubStateTest.java b/rskj-core/src/test/java/org/ethereum/jsontestsuite/GitHubStateTest.java index 75729f88f2a..90ca9222b61 100644 --- a/rskj-core/src/test/java/org/ethereum/jsontestsuite/GitHubStateTest.java +++ b/rskj-core/src/test/java/org/ethereum/jsontestsuite/GitHubStateTest.java @@ -20,9 +20,12 @@ package org.ethereum.jsontestsuite; import co.rsk.config.TestSystemProperties; +import org.ethereum.config.blockchain.upgrades.ActivationConfig; +import org.ethereum.config.blockchain.upgrades.ConsensusRule; import org.junit.jupiter.api.*; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Disabled; +import org.mockito.Mockito; import java.io.IOException; import java.util.Arrays; @@ -47,7 +50,28 @@ class GitHubStateTest { static void setup() { // TODO remove this after Homestead launch and shacommit update with actual block number // for this JSON test commit the Homestead block was defined as 900000 - config = new TestSystemProperties(); + config = Mockito.spy(new TestSystemProperties()); + ActivationConfig activationConfig = config.getActivationConfig(); + ActivationConfig activationConfigSpy = Mockito.spy(activationConfig); + + Mockito.doReturn(activationConfigSpy).when(config).getActivationConfig(); + + Mockito.doReturn(false) + .when(activationConfigSpy).isActive(Mockito.eq(ConsensusRule.RSKIPXXX), Mockito.anyLong()); + + Mockito.doAnswer(i1 -> { + ActivationConfig.ForBlock activationConfigForBlock = Mockito.spy(activationConfig.forBlock(i1.getArgument(0))); + + Mockito.doAnswer(i2 -> { + if (i2.getArgument(0).equals(ConsensusRule.RSKIPXXX)) { + return false; + } + + return i2.callRealMethod(); + }).when(activationConfigForBlock).isActive(Mockito.any()); + + return activationConfigForBlock; + }).when(activationConfigSpy).forBlock(Mockito.anyLong()); } @Disabled ("this method is mostly for hands-on convenient testing") diff --git a/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplLogsTest.java b/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplLogsTest.java index ca332119a26..9ccf80d7f1d 100644 --- a/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplLogsTest.java +++ b/rskj-core/src/test/java/org/ethereum/rpc/Web3ImplLogsTest.java @@ -26,6 +26,8 @@ import java.util.ArrayList; import java.util.List; +import org.ethereum.config.blockchain.upgrades.ActivationConfig; +import org.ethereum.config.blockchain.upgrades.ConsensusRule; import org.ethereum.core.*; import org.ethereum.datasource.HashMapDB; import org.ethereum.db.BlockStore; @@ -66,6 +68,7 @@ import co.rsk.trie.TrieStore; import co.rsk.util.HexUtils; import org.junit.jupiter.api.io.TempDir; +import org.mockito.Mockito; /** * Created by ajlopez on 30/11/2016. @@ -101,12 +104,12 @@ class Web3ImplLogsTest { private static final String GET_VALUE_METHOD_SIGNATURE = "20965255"; private static final String TRACKED_TEST_BLOCK_HASH = "0xafb368a4f74e51a3c6b6d72b049c4fc7bc7506251f13a3afa4fee4bece0e85eb"; private static final String UNTRACKED_TEST_BLOCK_HASH = "0xdea168a4f74e51a3eeb6d72b049c4fc7bc750dd51f13a3afa4fee4bece0e85eb"; - private final TestSystemProperties config = new TestSystemProperties(); + private TestSystemProperties config; @TempDir public Path tempDir; private Blockchain blockChain; - private MiningMainchainView mainchainView; + private MiningMainchainView mainchainView; private RepositoryLocator repositoryLocator; private TransactionPool transactionPool; private Ethereum eth; @@ -123,7 +126,30 @@ class Web3ImplLogsTest { @BeforeEach void setUp() { - RskTestFactory factory = new RskTestFactory(tempDir); + config = Mockito.spy(new TestSystemProperties()); + ActivationConfig activationConfig = config.getActivationConfig(); + ActivationConfig activationConfigSpy = Mockito.spy(activationConfig); + + Mockito.doReturn(activationConfigSpy).when(config).getActivationConfig(); + + Mockito.doReturn(false) + .when(activationConfigSpy).isActive(Mockito.eq(ConsensusRule.RSKIPXXX), Mockito.anyLong()); + + Mockito.doAnswer(i1 -> { + ActivationConfig.ForBlock activationConfigForBlock = Mockito.spy(activationConfig.forBlock(i1.getArgument(0))); + + Mockito.doAnswer(i2 -> { + if (i2.getArgument(0).equals(ConsensusRule.RSKIPXXX)) { + return false; + } + + return i2.callRealMethod(); + }).when(activationConfigForBlock).isActive(Mockito.any()); + + return activationConfigForBlock; + }).when(activationConfigSpy).forBlock(Mockito.anyLong()); + + RskTestFactory factory = new RskTestFactory(tempDir, config); blockChain = factory.getBlockchain(); blockStore = factory.getBlockStore(); trieStore = factory.getTrieStore(); @@ -160,8 +186,8 @@ void newFilterGetLogsInEmptyBlockchain() throws Exception { @Test void newFilterGetLogsAfterBlock() throws Exception { Account acc1 = new AccountBuilder(blockChain, - blockStore, - repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); + blockStore, + repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); web3.personal_newAccountWithSeed("notDefault"); FilterRequest fr = new FilterRequest(); @@ -175,8 +201,8 @@ void newFilterGetLogsAfterBlock() throws Exception { List txs = new ArrayList<>(); txs.add(tx); Block block1 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(genesis).transactions(txs).build(); + blockStore + ).trieStore(trieStore).parent(genesis).transactions(txs).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1)); Object[] logs = web3.eth_getFilterLogs(id); @@ -185,14 +211,14 @@ void newFilterGetLogsAfterBlock() throws Exception { assertNotNull(logs); assertEquals(1, logs.length); - assertEquals("0x" + tx.getContractAddress().toString(),((LogFilterElement)logs[0]).address); + assertEquals("0x" + tx.getContractAddress().toString(), ((LogFilterElement) logs[0]).address); } @Test void newFilterWithAccountAndTopicsCreatedAfterBlockAndGetLogs() throws Exception { Account acc1 = new AccountBuilder(blockChain, - blockStore, - repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); + blockStore, + repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); web3.personal_newAccountWithSeed("notDefault"); @@ -203,13 +229,13 @@ void newFilterWithAccountAndTopicsCreatedAfterBlockAndGetLogs() throws Exception List txs = new ArrayList<>(); txs.add(tx); Block block1 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(genesis).transactions(txs).build(); + blockStore + ).trieStore(trieStore).parent(genesis).transactions(txs).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1)); FilterRequest fr = new FilterRequest(); - fr.setAddress( ByteUtil.toHexString(tx.getContractAddress().getBytes())); - fr.setTopics(new Object[] { "06acbfb32bcf8383f3b0a768b70ac9ec234ea0f2d3b9c77fa6a2de69b919aad1" }); + fr.setAddress(ByteUtil.toHexString(tx.getContractAddress().getBytes())); + fr.setTopics(new Object[]{"06acbfb32bcf8383f3b0a768b70ac9ec234ea0f2d3b9c77fa6a2de69b919aad1"}); String id = web3.eth_newFilter(fr); Object[] logs = web3.eth_getFilterLogs(id); @@ -218,14 +244,14 @@ void newFilterWithAccountAndTopicsCreatedAfterBlockAndGetLogs() throws Exception assertNotNull(logs); assertEquals(1, logs.length); - assertEquals("0x" + tx.getContractAddress().toString(),((LogFilterElement)logs[0]).address); + assertEquals("0x" + tx.getContractAddress().toString(), ((LogFilterElement) logs[0]).address); } @Test void newFilterGetLogsTwiceAfterBlock() throws Exception { Account acc1 = new AccountBuilder(blockChain, - blockStore, - repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); + blockStore, + repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); web3.personal_newAccountWithSeed("notDefault"); @@ -240,8 +266,8 @@ void newFilterGetLogsTwiceAfterBlock() throws Exception { List txs = new ArrayList<>(); txs.add(tx); Block block1 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(genesis).transactions(txs).build(); + blockStore + ).trieStore(trieStore).parent(genesis).transactions(txs).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1)); web3.eth_getFilterLogs(id); @@ -251,7 +277,7 @@ void newFilterGetLogsTwiceAfterBlock() throws Exception { assertNotNull(logs); assertEquals(1, logs.length); - assertEquals("0x" + tx.getContractAddress().toString(),((LogFilterElement)logs[0]).address); + assertEquals("0x" + tx.getContractAddress().toString(), ((LogFilterElement) logs[0]).address); } @Test @@ -269,8 +295,8 @@ void newFilterGetChangesInEmptyBlockchain() throws Exception { @Test void newFilterGetChangesAfterBlock() throws Exception { Account acc1 = new AccountBuilder(blockChain, - blockStore, - repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); + blockStore, + repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); web3.personal_newAccountWithSeed("notDefault"); @@ -285,8 +311,8 @@ void newFilterGetChangesAfterBlock() throws Exception { List txs = new ArrayList<>(); txs.add(tx); Block block1 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(genesis).transactions(txs).build(); + blockStore + ).trieStore(trieStore).parent(genesis).transactions(txs).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1)); Object[] logs = web3.eth_getFilterChanges(id); @@ -295,7 +321,7 @@ void newFilterGetChangesAfterBlock() throws Exception { assertNotNull(logs); assertEquals(1, logs.length); - assertEquals("0x" + tx.getContractAddress().toString(),((LogFilterElement)logs[0]).address); + assertEquals("0x" + tx.getContractAddress().toString(), ((LogFilterElement) logs[0]).address); } @Test @@ -370,10 +396,10 @@ void getLogsFromBlockchainWithEventInContractCreation() throws Exception { assertNotNull(logs); assertEquals(1, logs.length); - String txhash = ((LogFilterElement)logs[0]).transactionHash; + String txhash = ((LogFilterElement) logs[0]).transactionHash; TransactionReceiptDTO txdto = web3.eth_getTransactionReceipt(txhash); - assertEquals(txdto.getContractAddress(),((LogFilterElement)logs[0]).address); + assertEquals(txdto.getContractAddress(), ((LogFilterElement) logs[0]).address); } @Test @@ -388,10 +414,10 @@ void getLogsTwiceFromBlockchainWithEventInContractCreation() throws Exception { assertNotNull(logs); assertEquals(1, logs.length); - String txhash = ((LogFilterElement)logs[0]).transactionHash; + String txhash = ((LogFilterElement) logs[0]).transactionHash; TransactionReceiptDTO txdto = web3.eth_getTransactionReceipt(txhash); - assertEquals(txdto.getContractAddress(),((LogFilterElement)logs[0]).address); + assertEquals(txdto.getContractAddress(), ((LogFilterElement) logs[0]).address); } @Test @@ -405,11 +431,11 @@ void getLogsFromBlockchainWithInvokeContract() throws Exception { assertNotNull(logs); assertEquals(2, logs.length); - String txhash = ((LogFilterElement)logs[0]).transactionHash; + String txhash = ((LogFilterElement) logs[0]).transactionHash; TransactionReceiptDTO txdto = web3.eth_getTransactionReceipt(txhash); - assertEquals(txdto.getContractAddress(),((LogFilterElement)logs[0]).address); - assertEquals(txdto.getContractAddress(),((LogFilterElement)logs[1]).address); + assertEquals(txdto.getContractAddress(), ((LogFilterElement) logs[0]).address); + assertEquals(txdto.getContractAddress(), ((LogFilterElement) logs[1]).address); } @Test @@ -474,11 +500,11 @@ void getLogsTwiceFromBlockchainWithInvokeContract() throws Exception { assertNotNull(logs); assertEquals(2, logs.length); - String txhash = ((LogFilterElement)logs[0]).transactionHash; + String txhash = ((LogFilterElement) logs[0]).transactionHash; TransactionReceiptDTO txdto = web3.eth_getTransactionReceipt(txhash); - assertEquals(txdto.getContractAddress(),((LogFilterElement)logs[0]).address); - assertEquals(txdto.getContractAddress(),((LogFilterElement)logs[1]).address); + assertEquals(txdto.getContractAddress(), ((LogFilterElement) logs[0]).address); + assertEquals(txdto.getContractAddress(), ((LogFilterElement) logs[1]).address); } @Test @@ -512,7 +538,7 @@ void getLogsFromBlockchainWithCallContractAndFilterByContractAddress() throws Ex Block block1 = blockChain.getBlockByNumber(1l); FilterRequest fr = new FilterRequest(); fr.setFromBlock("earliest"); - fr.setAddress( ByteUtil.toHexString(block1.getTransactionsList().get(0).getContractAddress().getBytes())); + fr.setAddress(ByteUtil.toHexString(block1.getTransactionsList().get(0).getContractAddress().getBytes())); Object[] logs = web3.eth_getLogs(fr); assertNotNull(logs); @@ -520,9 +546,9 @@ void getLogsFromBlockchainWithCallContractAndFilterByContractAddress() throws Ex String address = "0x" + fr.getAddress(); - assertEquals(address,((LogFilterElement)logs[0]).address); - assertEquals(address,((LogFilterElement)logs[1]).address); - assertEquals(address,((LogFilterElement)logs[2]).address); + assertEquals(address, ((LogFilterElement) logs[0]).address); + assertEquals(address, ((LogFilterElement) logs[1]).address); + assertEquals(address, ((LogFilterElement) logs[2]).address); } @Test @@ -531,7 +557,7 @@ void getLogsTwoceFromBlockchainWithCallContractAndFilterByContractAddress() thro Block block1 = blockChain.getBlockByNumber(1l); FilterRequest fr = new FilterRequest(); fr.setFromBlock("earliest"); - fr.setAddress( ByteUtil.toHexString(block1.getTransactionsList().get(0).getContractAddress().getBytes())); + fr.setAddress(ByteUtil.toHexString(block1.getTransactionsList().get(0).getContractAddress().getBytes())); web3.eth_getLogs(fr); Object[] logs = web3.eth_getLogs(fr); @@ -540,9 +566,9 @@ void getLogsTwoceFromBlockchainWithCallContractAndFilterByContractAddress() thro String address = "0x" + fr.getAddress(); - assertEquals(address,((LogFilterElement)logs[0]).address); - assertEquals(address,((LogFilterElement)logs[1]).address); - assertEquals(address,((LogFilterElement)logs[2]).address); + assertEquals(address, ((LogFilterElement) logs[0]).address); + assertEquals(address, ((LogFilterElement) logs[1]).address); + assertEquals(address, ((LogFilterElement) logs[2]).address); } @Test @@ -619,7 +645,7 @@ void getLogsFromBlockchainWithCallContractAndFilterByKnownTopic() throws Excepti assertNotNull(logs); String address = "0x" + ByteUtil.toHexString(block1.getTransactionsList().get(0).getContractAddress().getBytes()); assertEquals(1, logs.length); - assertEquals(address,((LogFilterElement)logs[0]).address); + assertEquals(address, ((LogFilterElement) logs[0]).address); } @Test @@ -637,7 +663,7 @@ void getLogsTwiceFromBlockchainWithCallContractAndFilterByKnownTopic() throws Ex assertNotNull(logs); String address = "0x" + ByteUtil.toHexString(block1.getTransactionsList().get(0).getContractAddress().getBytes()); assertEquals(1, logs.length); - assertEquals(address,((LogFilterElement)logs[0]).address); + assertEquals(address, ((LogFilterElement) logs[0]).address); } @Test @@ -655,7 +681,7 @@ void getLogsFromBlockchainWithCallContractAndFilterByKnownTopicInList() throws E assertNotNull(logs); String address = "0x" + ByteUtil.toHexString(block1.getTransactionsList().get(0).getContractAddress().getBytes()); assertEquals(1, logs.length); - assertEquals(address,((LogFilterElement)logs[0]).address); + assertEquals(address, ((LogFilterElement) logs[0]).address); } @Test @@ -674,7 +700,7 @@ void getLogsTwiceFromBlockchainWithCallContractAndFilterByKnownTopicInList() thr assertNotNull(logs); String address = "0x" + ByteUtil.toHexString(block1.getTransactionsList().get(0).getContractAddress().getBytes()); assertEquals(1, logs.length); - assertEquals(address,((LogFilterElement)logs[0]).address); + assertEquals(address, ((LogFilterElement) logs[0]).address); } @Test @@ -736,7 +762,7 @@ void getLogsFromBlockchainWithCallContractAndFilterByKnownTopicInListWithNull() assertNotNull(logs); String address = "0x" + ByteUtil.toHexString(block1.getTransactionsList().get(0).getContractAddress().getBytes()); assertEquals(1, logs.length); - assertEquals(address,((LogFilterElement)logs[0]).address); + assertEquals(address, ((LogFilterElement) logs[0]).address); } @Test @@ -754,7 +780,7 @@ void getLogsTwiceFromBlockchainWithCallContractAndFilterByKnownTopicInListWithNu assertNotNull(logs); String address = "0x" + ByteUtil.toHexString(block1.getTransactionsList().get(0).getContractAddress().getBytes()); assertEquals(1, logs.length); - assertEquals(address,((LogFilterElement)logs[0]).address); + assertEquals(address, ((LogFilterElement) logs[0]).address); } @Test @@ -890,9 +916,9 @@ void getLogsFromBlockchainWithEventInContractCreationReturnsAsExpectedWithBlockH assertNotNull(logs); assertEquals(1, logs.length); assertEquals(blockHash, ((LogFilterElement) logs[0]).blockHash); - String txhash = ((LogFilterElement)logs[0]).transactionHash; + String txhash = ((LogFilterElement) logs[0]).transactionHash; TransactionReceiptDTO txdto = web3.eth_getTransactionReceipt(txhash); - assertEquals(txdto.getContractAddress(),((LogFilterElement)logs[0]).address); + assertEquals(txdto.getContractAddress(), ((LogFilterElement) logs[0]).address); } @Test @@ -932,8 +958,8 @@ void getLogsThrowsExceptionWhenBlockHashIsUsedCombinedWithToBlock() { @Test void createMainContractWithoutEvents() throws Exception { Account acc1 = new AccountBuilder(blockChain, - blockStore, - repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); + blockStore, + repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); web3.personal_newAccountWithSeed("notDefault"); FilterRequest fr = new FilterRequest(); @@ -947,8 +973,8 @@ void createMainContractWithoutEvents() throws Exception { List txs = new ArrayList<>(); txs.add(tx); Block block1 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(genesis).transactions(txs).build(); + blockStore + ).trieStore(trieStore).parent(genesis).transactions(txs).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1)); Object[] logs = web3.eth_getFilterChanges(id); @@ -961,8 +987,8 @@ void createMainContractWithoutEvents() throws Exception { @Test void createCallerContractWithEvents() throws Exception { Account acc1 = new AccountBuilder(blockChain, - blockStore, - repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); + blockStore, + repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); web3.personal_newAccountWithSeed("notDefault"); FilterRequest fr = new FilterRequest(); @@ -976,8 +1002,8 @@ void createCallerContractWithEvents() throws Exception { List txs = new ArrayList<>(); txs.add(tx); Block block1 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(genesis).transactions(txs).build(); + blockStore + ).trieStore(trieStore).parent(genesis).transactions(txs).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1)); String mainAddress = tx.getContractAddress().toString(); @@ -989,8 +1015,8 @@ void createCallerContractWithEvents() throws Exception { List txs2 = new ArrayList<>(); txs2.add(tx2); Block block2 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(block1).transactions(txs2).build(); + blockStore + ).trieStore(trieStore).parent(block1).transactions(txs2).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block2)); Object[] logs = web3.eth_getFilterChanges(id); @@ -999,15 +1025,15 @@ void createCallerContractWithEvents() throws Exception { assertNotNull(logs); assertEquals(2, logs.length); - assertEquals("0x" + mainAddress, ((LogFilterElement)logs[0]).address); - assertEquals("0x" + callerAddress, ((LogFilterElement)logs[1]).address); + assertEquals("0x" + mainAddress, ((LogFilterElement) logs[0]).address); + assertEquals("0x" + callerAddress, ((LogFilterElement) logs[1]).address); } @Test void createCallerContractWithEventsOnInvoke() throws Exception { Account acc1 = new AccountBuilder(blockChain, - blockStore, - repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); + blockStore, + repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); web3.personal_newAccountWithSeed("notDefault"); FilterRequest fr = new FilterRequest(); @@ -1021,8 +1047,8 @@ void createCallerContractWithEventsOnInvoke() throws Exception { List txs = new ArrayList<>(); txs.add(tx); Block block1 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(genesis).transactions(txs).build(); + blockStore + ).trieStore(trieStore).parent(genesis).transactions(txs).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1)); String mainAddress = tx.getContractAddress().toString(); @@ -1034,8 +1060,8 @@ void createCallerContractWithEventsOnInvoke() throws Exception { List txs2 = new ArrayList<>(); txs2.add(tx2); Block block2 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(block1).transactions(txs2).build(); + blockStore + ).trieStore(trieStore).parent(block1).transactions(txs2).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block2)); Transaction tx3; @@ -1044,8 +1070,8 @@ void createCallerContractWithEventsOnInvoke() throws Exception { List txs3 = new ArrayList<>(); txs3.add(tx3); Block block3 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(block2).transactions(txs3).build(); + blockStore + ).trieStore(trieStore).parent(block2).transactions(txs3).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block3)); Object[] logs = web3.eth_getFilterChanges(id); @@ -1054,16 +1080,16 @@ void createCallerContractWithEventsOnInvoke() throws Exception { assertNotNull(logs); assertEquals(3, logs.length); - assertEquals("0x" + mainAddress, ((LogFilterElement)logs[0]).address); - assertEquals("0x" + callerAddress, ((LogFilterElement)logs[1]).address); - assertEquals("0x" + mainAddress, ((LogFilterElement)logs[2]).address); + assertEquals("0x" + mainAddress, ((LogFilterElement) logs[0]).address); + assertEquals("0x" + callerAddress, ((LogFilterElement) logs[1]).address); + assertEquals("0x" + mainAddress, ((LogFilterElement) logs[2]).address); } @Test void createCallerContractWithEventsOnInvokeUsingGetFilterLogs() throws Exception { Account acc1 = new AccountBuilder(blockChain, - blockStore, - repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); + blockStore, + repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); web3.personal_newAccountWithSeed("notDefault"); Block genesis = blockChain.getBlockByNumber(0); @@ -1073,8 +1099,8 @@ void createCallerContractWithEventsOnInvokeUsingGetFilterLogs() throws Exception List txs = new ArrayList<>(); txs.add(tx); Block block1 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(genesis).transactions(txs).build(); + blockStore + ).trieStore(trieStore).parent(genesis).transactions(txs).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1)); String mainAddress = tx.getContractAddress().toString(); @@ -1085,8 +1111,8 @@ void createCallerContractWithEventsOnInvokeUsingGetFilterLogs() throws Exception List txs2 = new ArrayList<>(); txs2.add(tx2); Block block2 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(block1).transactions(txs2).build(); + blockStore + ).trieStore(trieStore).parent(block1).transactions(txs2).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block2)); Transaction tx3; @@ -1095,12 +1121,12 @@ void createCallerContractWithEventsOnInvokeUsingGetFilterLogs() throws Exception List txs3 = new ArrayList<>(); txs3.add(tx3); Block block3 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(block2).transactions(txs3).build(); + blockStore + ).trieStore(trieStore).parent(block2).transactions(txs3).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block3)); FilterRequest fr = new FilterRequest(); - fr.setAddress( "0x" + mainAddress); + fr.setAddress("0x" + mainAddress); String id = web3.eth_newFilter(fr); Object[] logs = web3.eth_getFilterLogs(id); @@ -1109,7 +1135,7 @@ void createCallerContractWithEventsOnInvokeUsingGetFilterLogs() throws Exception assertNotNull(logs); assertEquals(1, logs.length); - assertEquals("0x" + mainAddress, ((LogFilterElement)logs[0]).address); + assertEquals("0x" + mainAddress, ((LogFilterElement) logs[0]).address); } private Web3Impl createWeb3() { @@ -1159,12 +1185,12 @@ null, new EthModuleWalletEnabled(wallet), null, private void addTwoEmptyBlocks() { Block genesis = blockChain.getBlockByNumber(0); Block block1 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(genesis).build(); + blockStore + ).trieStore(trieStore).parent(genesis).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1)); Block block2 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(block1).build(); + blockStore + ).trieStore(trieStore).parent(block1).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block2)); assertEquals(3, blockChain.getSize()); } @@ -1173,8 +1199,8 @@ private void addTwoEmptyBlocks() { private void addContractCreationWithoutEvents() { Account acc1 = new AccountBuilder(blockChain, - blockStore, - repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); + blockStore, + repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); Block genesis = blockChain.getBlockByNumber(0); @@ -1201,15 +1227,15 @@ function greet(string param) onlyOwner constant returns (string) { List txs = new ArrayList<>(); txs.add(tx); Block block1 = new BlockBuilder(blockChain, null, - blockStore - ).trieStore(trieStore).parent(genesis).transactions(txs).build(); + blockStore + ).trieStore(trieStore).parent(genesis).transactions(txs).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1)); web3.personal_newAccountWithSeed("notDefault"); } private void addEventInContractCreation() { - addEmptyBlockToBlockchain(blockChain, blockStore, repositoryLocator, trieStore); + addEmptyBlockToBlockchain(blockChain, blockStore, repositoryLocator, trieStore, config); web3.personal_newAccountWithSeed("notDefault"); } @@ -1219,7 +1245,21 @@ public static void addEmptyBlockToBlockchain( BlockStore blockStore, RepositoryLocator repositoryLocator, TrieStore trieStore) { - Account acc1 = new AccountBuilder(blockChain,blockStore,repositoryLocator) + addEmptyBlockToBlockchain( + blockChain, + blockStore, + repositoryLocator, + trieStore, + null); + } + + public static void addEmptyBlockToBlockchain( + Blockchain blockChain, + BlockStore blockStore, + RepositoryLocator repositoryLocator, + TrieStore trieStore, + TestSystemProperties config) { + Account acc1 = new AccountBuilder(blockChain, blockStore, repositoryLocator) .name("notDefault").balance(Coin.valueOf(10000000)).build(); Block genesis = blockChain.getBlockByNumber(0); @@ -1229,14 +1269,14 @@ public static void addEmptyBlockToBlockchain( List txs = new ArrayList<>(); txs.add(tx); Block block1 = new BlockBuilder(blockChain, null, blockStore) - .trieStore(trieStore).parent(genesis).transactions(txs).build(); + .trieStore(trieStore).parent(genesis).transactions(txs).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1)); } private void addContractInvoke() { Account acc1 = new AccountBuilder(blockChain, - blockStore, - repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); + blockStore, + repositoryLocator).name("notDefault").balance(Coin.valueOf(10000000)).build(); Block genesis = blockChain.getBlockByNumber(0); Transaction tx; @@ -1245,7 +1285,7 @@ private void addContractInvoke() { List txs = new ArrayList<>(); txs.add(tx); Block block1 = new BlockBuilder(blockChain, null, blockStore) - .trieStore(trieStore).parent(genesis).transactions(txs).build(); + .trieStore(trieStore).parent(genesis).transactions(txs).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1)); byte[] contractAddress = tx.getContractAddress().getBytes(); @@ -1254,7 +1294,7 @@ private void addContractInvoke() { List tx2s = new ArrayList<>(); tx2s.add(tx2); Block block2 = new BlockBuilder(blockChain, null, blockStore) - .trieStore(trieStore).parent(block1).transactions(tx2s).build(); + .trieStore(trieStore).parent(block1).transactions(tx2s).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block2)); web3.personal_newAccountWithSeed("default"); @@ -1278,7 +1318,7 @@ private List addContractInvokeManyTxPerBlock() { allTxs.add(tx); Block block1 = new BlockBuilder(blockChain, null, blockStore) - .trieStore(trieStore).parent(genesis).transactions(txs).build(); + .trieStore(trieStore).parent(genesis).transactions(txs).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1)); byte[] contractAddress = tx.getContractAddress().getBytes(); @@ -1295,7 +1335,7 @@ private List addContractInvokeManyTxPerBlock() { allTxs.add(tx3); Block block2 = new BlockBuilder(blockChain, null, blockStore) - .trieStore(trieStore).parent(block1).transactions(txs).build(); + .trieStore(trieStore).parent(block1).transactions(txs).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block2)); web3.personal_newAccountWithSeed("default"); @@ -1316,7 +1356,7 @@ private void addContractCall() { List txs = new ArrayList<>(); txs.add(tx); Block block1 = new BlockBuilder(blockChain, null, blockStore) - .trieStore(trieStore).parent(genesis).transactions(txs).build(); + .trieStore(trieStore).parent(genesis).transactions(txs).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1)); byte[] contractAddress = tx.getContractAddress().getBytes(); @@ -1326,14 +1366,14 @@ private void addContractCall() { List tx2s = new ArrayList<>(); tx2s.add(tx2); Block block2 = new BlockBuilder(blockChain, null, blockStore) - .trieStore(trieStore).parent(block1).transactions(tx2s).build(); + .trieStore(trieStore).parent(block1).transactions(tx2s).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block2)); Transaction tx3 = getContractTransactionWithCall(acc1, contractAddress); List tx3s = new ArrayList<>(); tx3s.add(tx3); Block block3 = new BlockBuilder(blockChain, null, blockStore) - .trieStore(trieStore).parent(block2).transactions(tx3s).build(); + .trieStore(trieStore).parent(block2).transactions(tx3s).config(config).build(); assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block3)); web3.personal_newAccountWithSeed("default"); @@ -1341,14 +1381,14 @@ private void addContractCall() { } private static Transaction getContractTransaction(Account acc1) { - return getContractTransaction(acc1,false); + return getContractTransaction(acc1, false); } //0.4.11+commit.68ef5810.Emscripten.clang WITH optimizations static final String compiled_0_4_11 = "6060604052341561000c57fe5b5b60466000819055507f06acbfb32bcf8383f3b0a768b70ac9ec234ea0f2d3b9c77fa6a2de69b919aad16000546040518082815260200191505060405180910390a15b5b61014e8061005f6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632096525514610046578063371303c01461006c575bfe5b341561004e57fe5b61005661007e565b6040518082815260200191505060405180910390f35b341561007457fe5b61007c6100c2565b005b60007f1ee041944547858a75ebef916083b6d4f5ae04bea9cd809334469dd07dbf441b6000546040518082815260200191505060405180910390a160005490505b90565b60006000815460010191905081905550600160026000548115156100e257fe5b061415157f6e61ef44ac2747ff8b84d353a908eb8bd5c3fb118334d57698c5cfc7041196ad6000546040518082815260200191505060405180910390a25b5600a165627a7a7230582092c7b2c0483b85227396e18149993b33243059af0f3bd0364f1dc36b8bbbcdae0029"; static final String compiled_unknown = "60606040526046600081905560609081527f06acbfb32bcf8383f3b0a768b70ac9ec234ea0f2d3b9c77fa6a2de69b919aad190602090a160aa8060426000396000f3606060405260e060020a60003504632096525581146024578063371303c0146060575b005b60a36000805460609081527f1ee041944547858a75ebef916083b6d4f5ae04bea9cd809334469dd07dbf441b90602090a1600060005054905090565b6022600080546001908101918290556060828152600290920614907f6e61ef44ac2747ff8b84d353a908eb8bd5c3fb118334d57698c5cfc7041196ad90602090a2565b5060206060f3"; - private static Transaction getContractTransaction(Account acc1,boolean withEvent) { + private static Transaction getContractTransaction(Account acc1, boolean withEvent) { /* contract compiled in data attribute of tx contract counter { event Incremented(bool indexed odd, uint x); @@ -1402,7 +1442,8 @@ private static Transaction getContractTransactionWithCall(Account acc1, byte[] r .build(); } - String compiledLogExample ="606060405234610000575b60bd806100186000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063195977a614603c575b6000565b34600057605460048080359060200190919050506056565b005b7ffd99bb34477b313b3e3b452b34d012d8315db36a1d63949d9d8f9d2573b05aff816040518082815260200191505060405180910390a15b505600a165627a7a72305820fb2550735b0655fb2fe03738be375a4c29ef1b6ff51004f869be19de0301f30b0029"; + String compiledLogExample = "606060405234610000575b60bd806100186000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063195977a614603c575b6000565b34600057605460048080359060200190919050506056565b005b7ffd99bb34477b313b3e3b452b34d012d8315db36a1d63949d9d8f9d2573b05aff816040518082815260200191505060405180910390a15b505600a165627a7a72305820fb2550735b0655fb2fe03738be375a4c29ef1b6ff51004f869be19de0301f30b0029"; + private Transaction getMainContractTransaction(Account acc1) { /* contract compiled in data attribute of tx contract main { @@ -1448,7 +1489,7 @@ function doSomething(address mainAddr) { .sender(acc1) .gasLimit(BigInteger.valueOf(1000000)) .gasPrice(BigInteger.ONE) - .data( compiledCaller + address) + .data(compiledCaller + address) .nonce(1) .build(); } @@ -1459,7 +1500,7 @@ private static Transaction getCallerContractTransactionWithInvoke(Account acc1, while (address.length() < 64) address = "0" + address; - CallTransaction.Function func = CallTransaction.Function.fromSignature("doSomething", new String[] { "address" }, new String[0]); + CallTransaction.Function func = CallTransaction.Function.fromSignature("doSomething", new String[]{"address"}, new String[0]); return new TransactionBuilder() .sender(acc1) diff --git a/rskj-core/src/test/java/org/ethereum/vm/opcodes/RevertOpCodeTest.java b/rskj-core/src/test/java/org/ethereum/vm/opcodes/RevertOpCodeTest.java index 5f94b354d45..bb779ef4b79 100644 --- a/rskj-core/src/test/java/org/ethereum/vm/opcodes/RevertOpCodeTest.java +++ b/rskj-core/src/test/java/org/ethereum/vm/opcodes/RevertOpCodeTest.java @@ -1,11 +1,16 @@ package org.ethereum.vm.opcodes; +import co.rsk.config.TestSystemProperties; import co.rsk.test.World; import co.rsk.test.dsl.DslParser; import co.rsk.test.dsl.DslProcessorException; import co.rsk.test.dsl.WorldDslProcessor; +import org.ethereum.config.blockchain.upgrades.ActivationConfig; +import org.ethereum.config.blockchain.upgrades.ConsensusRule; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import java.io.FileNotFoundException; @@ -42,10 +47,40 @@ function sendHalf() payable returns (uint8 sum) { } */ + private World world; + + @BeforeEach + void setUp() { + TestSystemProperties config = Mockito.spy(new TestSystemProperties()); + ActivationConfig activationConfig = config.getActivationConfig(); + ActivationConfig activationConfigSpy = Mockito.spy(activationConfig); + + Mockito.doReturn(activationConfigSpy).when(config).getActivationConfig(); + + Mockito.doReturn(false) + .when(activationConfigSpy).isActive(Mockito.eq(ConsensusRule.RSKIPXXX), Mockito.anyLong()); + + Mockito.doAnswer(i1 -> { + ActivationConfig.ForBlock activationConfigForBlock = Mockito.spy(activationConfig.forBlock(i1.getArgument(0))); + + Mockito.doAnswer(i2 -> { + if (i2.getArgument(0).equals(ConsensusRule.RSKIPXXX)) { + return false; + } + + return i2.callRealMethod(); + }).when(activationConfigForBlock).isActive(Mockito.any()); + + return activationConfigForBlock; + }).when(activationConfigSpy).forBlock(Mockito.anyLong()); + + world = new World(config); + } + @Test void runFullContractThenRunAndRevert() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/opcode_revert1.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser); @@ -58,7 +93,7 @@ void runFullContractThenRunAndRevert() throws FileNotFoundException, DslProcesso @Test void runAndRevertThenRunFullContract() throws FileNotFoundException, DslProcessorException { DslParser parser = DslParser.fromResource("dsl/opcode_revert2.txt"); - World world = new World(); + WorldDslProcessor processor = new WorldDslProcessor(world); processor.processCommands(parser);