Skip to content

Commit

Permalink
Fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmoreliovlabs committed Jun 15, 2023
1 parent 9216fae commit 63554e0
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 182 deletions.
37 changes: 36 additions & 1 deletion rskj-core/src/test/java/co/rsk/jsontestsuite/LocalStateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@

package co.rsk.jsontestsuite;

import co.rsk.config.TestSystemProperties;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.config.blockchain.upgrades.ConsensusRule;
import org.ethereum.jsontestsuite.GitHubJSONTestSuite;
import org.ethereum.jsontestsuite.JSONReader;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.MethodOrderer;
import org.mockito.Mockito;

import java.io.IOException;
import java.util.Arrays;
Expand All @@ -58,6 +63,36 @@
@SuppressWarnings("squid:S1607") // many @Disabled annotations for diverse reasons
class LocalStateTest {

private TestSystemProperties config;

@BeforeEach
public 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 = 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")
public void stSingleTest() throws IOException {
String json = getJSON("stSystemOperationsTest");
Expand Down Expand Up @@ -245,7 +280,7 @@ void stInitCodeTest() throws IOException {
excluded.add("CallRecursiveContract");


GitHubJSONTestSuite.runStateTest(json, excluded);
GitHubJSONTestSuite.runStateTest(json, excluded, config);
}

@Test
Expand Down
63 changes: 47 additions & 16 deletions rskj-core/src/test/java/co/rsk/mine/TransactionModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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();
Expand All @@ -163,7 +196,6 @@ void sendTransactionMustNotBeMined() {

@Test
void sendTransactionMustBeMined() {
World world = new World();
BlockChainImpl blockchain = world.getBlockChain();

TrieStore trieStore = world.getTrieStore();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -281,7 +313,6 @@ void sendRawTransactionWithoutAutoMining() {

@Test
void testGasEstimation() {
World world = new World();
Blockchain blockchain = world.getBlockChain();

TrieStore trieStore = world.getTrieStore();
Expand Down
Loading

0 comments on commit 63554e0

Please sign in to comment.