Skip to content

Commit

Permalink
fix(MaxInitcodeSize): add and update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vovchyk committed Sep 20, 2024
1 parent 85879d1 commit fd22722
Showing 1 changed file with 41 additions and 22 deletions.
63 changes: 41 additions & 22 deletions rskj-core/src/test/java/co/rsk/core/TransactionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,7 @@
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.BlockFactory;
import org.ethereum.core.BlockTxSignatureCache;
import org.ethereum.core.CallTransaction;
import org.ethereum.core.ImmutableTransaction;
import org.ethereum.core.ReceivedTxSignatureCache;
import org.ethereum.core.Repository;
import org.ethereum.core.Transaction;
import org.ethereum.core.TransactionBuilder;
import org.ethereum.core.TransactionExecutor;
import org.ethereum.core.*;
import org.ethereum.crypto.ECKey;
import org.ethereum.crypto.HashUtil;
import org.ethereum.crypto.signature.Secp256k1;
Expand All @@ -49,20 +40,12 @@
import org.ethereum.vm.PrecompiledContracts;
import org.ethereum.vm.program.ProgramResult;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mockito;

import java.math.BigInteger;
import java.util.List;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;

class TransactionTest {
Expand Down Expand Up @@ -485,16 +468,52 @@ void testTransactionCostWithRSKIP400Enabled() {
}

@Test
void testTransactionCostWithRSKIP438Enabled() {
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};
void testTransactionCostForContractCreationWithRSKIP438Disabled() {
byte[] bytes = new byte[]{-8, 76, -128, 8, -126, -61, 80, -128, 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);

// make sure that it's contract creation
assertEquals(RskAddress.nullAddress(), txInBlock.getReceiveAddress());

Constants constants = Mockito.mock(Constants.class);
Mockito.doReturn(BridgeMainNetConstants.getInstance()).when(constants).getBridgeConstants();
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
Mockito.doReturn(false).when(activations).isActive(Mockito.eq(ConsensusRule.RSKIP438));

assertEquals(53068, txInBlock.transactionCost(constants, activations, new BlockTxSignatureCache(new ReceivedTxSignatureCache())));
}

@Test
void testTransactionCostForContractCreationWithRSKIP438Enabled() {
byte[] bytes = new byte[]{-8, 76, -128, 8, -126, -61, 80, -128, 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);

// make sure that it's contract creation
assertEquals(RskAddress.nullAddress(), txInBlock.getReceiveAddress());

Constants constants = Mockito.mock(Constants.class);
Mockito.doReturn(BridgeMainNetConstants.getInstance()).when(constants).getBridgeConstants();
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
Mockito.doReturn(true).when(activations).isActive(Mockito.eq(ConsensusRule.RSKIP438));

assertEquals(53070, txInBlock.transactionCost(constants, activations, new BlockTxSignatureCache(new ReceivedTxSignatureCache())));
}

@Test
void testTransactionCostForContractCreationWithNullDataAndRSKIP438Enabled() {
byte[] bytes = new byte[]{-8, 75, -128, 8, -126, -61, 80, -128, 7, -128, 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);

// make sure that data is null and it's contract creation
assertNull(txInBlock.getData());
assertEquals(RskAddress.nullAddress(), txInBlock.getReceiveAddress());

Constants constants = Mockito.mock(Constants.class);
Mockito.doReturn(BridgeMainNetConstants.getInstance()).when(constants).getBridgeConstants();
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
Mockito.doReturn(true).when(activations).isActive(Mockito.eq(ConsensusRule.RSKIP438));

assertEquals(21068L, txInBlock.transactionCost(constants, activations, new BlockTxSignatureCache(new ReceivedTxSignatureCache())));
assertEquals(53000, txInBlock.transactionCost(constants, activations, new BlockTxSignatureCache(new ReceivedTxSignatureCache())));
}
}

0 comments on commit fd22722

Please sign in to comment.