Skip to content

Commit

Permalink
Refactor some test scenarios to not create new blocks after every new tx
Browse files Browse the repository at this point in the history
- In order to have a better validation of real use of TLOAD/TSTORE, we changed some DSL tests to not create
a new block after every tx. This way, we validate that the transient storage it's correctly managed across the different
tx.

- Small refactor was added in the MutableTrie creation with a better name stating that we are creating an in memory
trie.
  • Loading branch information
fmacleal committed Dec 20, 2024
1 parent 2a3262c commit adab6b3
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 272 deletions.
12 changes: 8 additions & 4 deletions rskj-core/src/main/java/org/ethereum/db/MutableRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,19 @@ public class MutableRepository implements Repository {
private final IReadWrittenKeysTracker tracker;

public MutableRepository(TrieStore trieStore, Trie trie) {
this(new MutableTrieImpl(trieStore, trie), new MutableTrieImpl(new TrieStoreImpl(new HashMapDB()), new Trie()));
this(new MutableTrieImpl(trieStore, trie), aInMemoryMutableTrie());
}

public MutableRepository(MutableTrie mutableTrie) {
this(mutableTrie, new MutableTrieImpl(new TrieStoreImpl(new HashMapDB()), new Trie()));
this(mutableTrie, aInMemoryMutableTrie());
}

public MutableRepository(MutableTrie mutableTrie, IReadWrittenKeysTracker tracker) {
this(mutableTrie, new MutableTrieImpl(new TrieStoreImpl(new HashMapDB()), new Trie()), tracker);
this(mutableTrie, aInMemoryMutableTrie(), tracker);
}

private static MutableTrieImpl aInMemoryMutableTrie() {
return new MutableTrieImpl(new TrieStoreImpl(new HashMapDB()), new Trie());
}

public MutableRepository(MutableTrie mutableTrie, MutableTrie transientTrie) {
Expand Down Expand Up @@ -452,7 +456,7 @@ public void addTransientStorageBytes(RskAddress addr, DataWord key, byte[] value

@Override
public void clearTransientStorage() {
this.transientTrie = new MutableTrieImpl(new TrieStoreImpl(new HashMapDB()), new Trie());
this.transientTrie = aInMemoryMutableTrie();
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ void testInConstructorAndCodeCreateContext() throws FileNotFoundException, DslPr
processor.processCommands(parser);

String txTestTransientStorageCreateContextsContract = "txTestTransientStorageCreateContextsContract";
assertTransactionReceiptWithStatus(world, txTestTransientStorageCreateContextsContract, "b01", true);
assertTransactionReceiptWithStatus(world, txTestTransientStorageCreateContextsContract, "b01", true, 2);

String txInConstructorAndCode = "txInConstructorAndCode";
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txInConstructorAndCode, "b02", true);
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txInConstructorAndCode, "b01", true, 2);
Assertions.assertEquals(6, TransactionReceiptUtil.getEventCount(txReceipt, "OK", null));
}

Expand All @@ -190,10 +190,10 @@ void testAccrossConstructorAndCodeV0CreateContext() throws FileNotFoundException
processor.processCommands(parser);

String txTestTransientStorageCreateContextsContract = "txTestTransientStorageCreateContextsContract";
assertTransactionReceiptWithStatus(world, txTestTransientStorageCreateContextsContract, "b01", true);
assertTransactionReceiptWithStatus(world, txTestTransientStorageCreateContextsContract, "b01", true, 2);

String txAcrossConstructorAndCodeV0 = "txAcrossConstructorAndCodeV0";
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txAcrossConstructorAndCodeV0, "b02", true);
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txAcrossConstructorAndCodeV0, "b01", true, 2);
Assertions.assertEquals(6, TransactionReceiptUtil.getEventCount(txReceipt, "OK", null));
}

Expand Down Expand Up @@ -238,11 +238,11 @@ void testDynamicExecutionContextSimpleScenario() throws FileNotFoundException, D
assertTransactionReceiptWithStatus(world, txCallAndDelegateCallSimpleTest, "b01", true);

String txExecuteCallCode = "txExecuteCallCode";
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txExecuteCallCode, "b02", true);
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txExecuteCallCode, "b02", true,2);
Assertions.assertEquals(5, TransactionReceiptUtil.getEventCount(txReceipt, "OK", null));

String txExecuteDelegateCall = "txExecuteDelegateCall";
txReceipt = assertTransactionReceiptWithStatus(world, txExecuteDelegateCall, "b03", true);
txReceipt = assertTransactionReceiptWithStatus(world, txExecuteDelegateCall, "b02", true, 2);
Assertions.assertEquals(5, TransactionReceiptUtil.getEventCount(txReceipt, "OK", null));
}

Expand All @@ -257,15 +257,15 @@ void testDynamicExecutionContextWithRevert() throws FileNotFoundException, DslPr
assertTransactionReceiptWithStatus(world, txDynamicExecutionContextWithRevertTest, "b01", true);

String txExecuteCallCode = "txExecuteCallCode";
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txExecuteCallCode, "b02", true);
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txExecuteCallCode, "b02", true, 3);
Assertions.assertEquals(3, TransactionReceiptUtil.getEventCount(txReceipt, "OK", null));

String txExecuteDelegateCall = "txExecuteDelegateCall";
txReceipt = assertTransactionReceiptWithStatus(world, txExecuteDelegateCall, "b03", true);
txReceipt = assertTransactionReceiptWithStatus(world, txExecuteDelegateCall, "b02", true, 3);
Assertions.assertEquals(3, TransactionReceiptUtil.getEventCount(txReceipt, "OK", null));

String txExecuteCall = "txExecuteCall";
txReceipt = assertTransactionReceiptWithStatus(world, txExecuteCall, "b04", true);
txReceipt = assertTransactionReceiptWithStatus(world, txExecuteCall, "b02", true, 3);
Assertions.assertEquals(3, TransactionReceiptUtil.getEventCount(txReceipt, "OK", null));
}

Expand All @@ -280,15 +280,15 @@ void testDynamicExecutionContextWithInvalid() throws FileNotFoundException, DslP
assertTransactionReceiptWithStatus(world, txDynamicExecutionContextWithInvalidTest, "b01", true);

String txExecuteCallCode = "txExecuteCallCode";
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txExecuteCallCode, "b02", true);
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txExecuteCallCode, "b02", true, 3);
Assertions.assertEquals(3, TransactionReceiptUtil.getEventCount(txReceipt, "OK", null));

String txExecuteDelegateCall = "txExecuteDelegateCall";
txReceipt = assertTransactionReceiptWithStatus(world, txExecuteDelegateCall, "b03", true);
txReceipt = assertTransactionReceiptWithStatus(world, txExecuteDelegateCall, "b02", true, 3);
Assertions.assertEquals(3, TransactionReceiptUtil.getEventCount(txReceipt, "OK", null));

String txExecuteCall = "txExecuteCall";
txReceipt = assertTransactionReceiptWithStatus(world, txExecuteCall, "b04", true);
txReceipt = assertTransactionReceiptWithStatus(world, txExecuteCall, "b02", true, 3);
Assertions.assertEquals(3, TransactionReceiptUtil.getEventCount(txReceipt, "OK", null));
}

Expand All @@ -303,15 +303,15 @@ void testDynamicExecutionContextWithStackOverflow() throws FileNotFoundException
assertTransactionReceiptWithStatus(world, txDynamicExecutionContextWithStackOverflowTest, "b01", true);

String txExecuteCallCode = "txExecuteCallCode";
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txExecuteCallCode, "b02", true);
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txExecuteCallCode, "b02", true,3);
Assertions.assertEquals(3, TransactionReceiptUtil.getEventCount(txReceipt, "OK", null));

String txExecuteDelegateCall = "txExecuteDelegateCall";
txReceipt = assertTransactionReceiptWithStatus(world, txExecuteDelegateCall, "b03", true);
txReceipt = assertTransactionReceiptWithStatus(world, txExecuteDelegateCall, "b02", true, 3);
Assertions.assertEquals(3, TransactionReceiptUtil.getEventCount(txReceipt, "OK", null));

String txExecuteCall = "txExecuteCall";
txReceipt = assertTransactionReceiptWithStatus(world, txExecuteCall, "b04", true);
txReceipt = assertTransactionReceiptWithStatus(world, txExecuteCall, "b02", true, 3);
Assertions.assertEquals(3, TransactionReceiptUtil.getEventCount(txReceipt, "OK", null));
}

Expand Down Expand Up @@ -353,14 +353,14 @@ void testDynamicReentrancyContextsTstoreBeforeRevertOrInvalidHasNoEffect() throw
processor.processCommands(parser);

String txTstorageDynamicReentrancyContextContract = "txTstorageDynamicReentrancyContextContract";
assertTransactionReceiptWithStatus(world, txTstorageDynamicReentrancyContextContract, "b01", true);
assertTransactionReceiptWithStatus(world, txTstorageDynamicReentrancyContextContract, "b01", true, 3);

String txTestReentrantContextRevert = "txTestReentrantContextRevert";
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txTestReentrantContextRevert, "b02", true);
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txTestReentrantContextRevert, "b01", true, 3);
Assertions.assertEquals(3, TransactionReceiptUtil.getEventCount(txReceipt, "OK", null));

String txTestReentrantContextInvalid = "txTestReentrantContextInvalid";
assertTransactionReceiptWithStatus(world, txTestReentrantContextInvalid, "b03", false);
assertTransactionReceiptWithStatus(world, txTestReentrantContextInvalid, "b01", false, 3);
}

@Test
Expand Down Expand Up @@ -414,13 +414,13 @@ void testReentrancyContextsTstoreAfterReentrantCall() throws FileNotFoundExcepti
processor.processCommands(parser);

String txTstorageReentrancyContextTestContract = "txTstorageReentrancyContextTestContract";
assertTransactionReceiptWithStatus(world, txTstorageReentrancyContextTestContract, "b01", true);
assertTransactionReceiptWithStatus(world, txTstorageReentrancyContextTestContract, "b01", true, 3);

String txTstoreInReentrantCall = "txTstoreInReentrantCall";
assertTransactionReceiptWithStatus(world, txTstoreInReentrantCall, "b02", true);
assertTransactionReceiptWithStatus(world, txTstoreInReentrantCall, "b01", true, 3);

String txCheckValuesStoredInTstorage = "txCheckValuesStoredInTstorage";
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txCheckValuesStoredInTstorage, "b03", true);
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txCheckValuesStoredInTstorage, "b01", true, 3);
Assertions.assertEquals(3, TransactionReceiptUtil.getEventCount(txReceipt, "OK", null));
}

Expand Down Expand Up @@ -580,38 +580,28 @@ void testTloadTstoreCheckingBetweenTransactions() throws FileNotFoundException,
assertTransactionReceiptWithStatus(world, txTloadTstoreCheckingBetweenTransactionsContract, "b01", true);

String txTstoreAndTloadSomeValue = "txTstoreAndTloadSomeValue";
Transaction txCreation = world.getTransactionByName(txTstoreAndTloadSomeValue);
assertNotNull(txCreation);

Block block = world.getBlockByName("b02");
assertEquals(2, block.getTransactionsList().size());

TransactionReceipt txReceipt = world.getTransactionReceiptByName(txTstoreAndTloadSomeValue);
assertNotNull(txReceipt);
byte[] status = txReceipt.getStatus();
assertNotNull(status);
assertTrue(txReceipt.isSuccessful());
TransactionReceipt txReceipt = assertTransactionReceiptWithStatus(world, txTstoreAndTloadSomeValue, "b02", true, 2);

Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txReceipt, "ValueStored", new String[]{"uint256","uint256"}));
Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txReceipt, "ValueLoaded", new String[]{"uint256","uint256"}));

String txTloadSomeValueAndCheckWithExpected = "txTloadSomeValueAndCheckWithExpected";
TransactionReceipt txReceipt2 = world.getTransactionReceiptByName(txTloadSomeValueAndCheckWithExpected);
assertNotNull(txReceipt2);
status = txReceipt2.getStatus();
assertNotNull(status);
assertTrue(txReceipt2.isSuccessful());
TransactionReceipt txReceipt2 = assertTransactionReceiptWithStatus(world, txTloadSomeValueAndCheckWithExpected, "b02", true, 2);

Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txReceipt2, "ValueLoaded", new String[]{"uint256","uint256"}));
Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txReceipt2, "OK", null));
}

private static TransactionReceipt assertTransactionReceiptWithStatus(World world, String txName, String blockName, boolean withSuccess) {
return assertTransactionReceiptWithStatus(world, txName, blockName, withSuccess, 1);
}

private static TransactionReceipt assertTransactionReceiptWithStatus(World world, String txName, String blockName, boolean withSuccess, int numberOfTransactionsInBlock) {
Transaction txCreation = world.getTransactionByName(txName);
assertNotNull(txCreation);

Block block = world.getBlockByName(blockName);
assertEquals(1, block.getTransactionsList().size());
assertEquals(numberOfTransactionsInBlock, block.getTransactionsList().size());

TransactionReceipt txReceipt = world.getTransactionReceiptByName(txName);
assertNotNull(txReceipt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,6 @@ transaction_build txTestTransientStorageCreateContextsContract
gas 1000000
build

# Create block to hold txTestTransientStorageCreateContextsContract transaction
block_build b01
parent g00
transactions txTestTransientStorageCreateContextsContract
gasLimit 1200000
build

# Connect block
block_connect b01

# Check b01 is best block
assert_best b01

# Create transaction to execute txInConstructorAndCode transaction
transaction_build txAcrossConstructorAndCodeV0
sender acc1
Expand All @@ -129,14 +116,14 @@ transaction_build txAcrossConstructorAndCodeV0
build

# Create block to hold txAcrossConstructorAndCodeV0 transaction
block_build b02
parent b01
transactions txAcrossConstructorAndCodeV0
block_build b01
parent g00
transactions txTestTransientStorageCreateContextsContract txAcrossConstructorAndCodeV0
gasLimit 3000000
build

# Connect block
block_connect b02
block_connect b01

# Check b02 is best block
assert_best b02
# Check b01 is best block
assert_best b01
Original file line number Diff line number Diff line change
Expand Up @@ -107,41 +107,28 @@ transaction_build txExecuteCallCode
contract txCallAndDelegateCallSimpleTest
value 0
data a61face90000000000000000000000000000000000000000000000000000000000000001
gas 1000000
build

# Create block to hold txExecuteCallCode transaction
block_build b02
parent b01
transactions txExecuteCallCode
gasLimit 2000000
gas 300000
build

# Connect block
block_connect b02

# Check b02 is best block
assert_best b02

# Create transaction to execute txExecuteDelegateCall transaction
transaction_build txExecuteDelegateCall
sender acc1
nonce 2
contract txCallAndDelegateCallSimpleTest
value 0
data a61face90000000000000000000000000000000000000000000000000000000000000000
gas 1000000
gas 300000
build

# Create block to hold txExecuteDelegateCall transaction
block_build b03
parent b02
transactions txExecuteDelegateCall
gasLimit 2000000
# Create block to hold transactions
block_build b02
parent b01
transactions txExecuteCallCode txExecuteDelegateCall
gasLimit 1500000
build

# Connect block
block_connect b03
block_connect b02

# Check b03 is best block
assert_best b03
# Check b02 is best block
assert_best b02
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,6 @@ transaction_build txExecuteCallCode
gas 500000
build

# Create block to hold txExecuteCallCode transaction
block_build b02
parent b01
transactions txExecuteCallCode
gasLimit 550000
build

# Connect block
block_connect b02

# Check b02 is best block
assert_best b02

# Create transaction to execute txExecuteDelegateCall transaction
transaction_build txExecuteDelegateCall
sender acc1
Expand All @@ -129,19 +116,6 @@ transaction_build txExecuteDelegateCall
gas 500000
build

# Create block to hold txExecuteDelegateCall transaction
block_build b03
parent b02
transactions txExecuteDelegateCall
gasLimit 550000
build

# Connect block
block_connect b03

# Check b03 is best block
assert_best b03

# Create transaction to execute txExecuteDelegateCall transaction
transaction_build txExecuteCall
sender acc1
Expand All @@ -152,15 +126,15 @@ transaction_build txExecuteCall
gas 500000
build

# Create block to hold txExecuteCall transaction
block_build b04
parent b03
transactions txExecuteCall
gasLimit 550000
# Create block to hold transactions
block_build b02
parent b01
transactions txExecuteCallCode txExecuteDelegateCall txExecuteCall
gasLimit 2000000
build

# Connect block
block_connect b04
block_connect b02

# Check b04 is best block
assert_best b04
# Check b02 is best block
assert_best b02
Loading

0 comments on commit adab6b3

Please sign in to comment.