Skip to content

Commit

Permalink
Adding more scenarios of test with dynamic execution context
Browse files Browse the repository at this point in the history
  • Loading branch information
fmacleal committed Nov 4, 2024
1 parent d340722 commit 8bba650
Show file tree
Hide file tree
Showing 7 changed files with 733 additions and 8 deletions.
2 changes: 1 addition & 1 deletion rskj-core/src/main/resources/genesis/orchid-testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x434d272841",
"gasLimit": "0x4c4b40",
"gasLimit": "0x67c280",
"difficulty": "0x0000100000",
"coinbase": "0x3333333333333333333333333333333333333333",
"minimumGasPrice": "0x00",
Expand Down
2 changes: 1 addition & 1 deletion rskj-core/src/main/resources/genesis/rsk-unittests.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x686f727365",
"gasLimit": "0x2dc6c0",
"gasLimit": "0x5B8D80",
"difficulty": "0x0000000001",
"minimumGasPrice": "0x00",
"coinbase": "0x3333333333333333333333333333333333333333"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,91 @@ void testNoConstructorCodeCreateContext() throws FileNotFoundException, DslProce
}

@Test
void testDynamicCallCodeExecutionContext() throws FileNotFoundException, DslProcessorException {
DslParser parser = DslParser.fromResource("dsl/transaction_storage_rskip446/no_constructor_code_create_context.txt");
void testDynamicExecutionContextSimpleScenario() throws FileNotFoundException, DslProcessorException {
DslParser parser = DslParser.fromResource("dsl/transaction_storage_rskip446/dynamic_execution_context_simple.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);

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

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

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

@Test
void testDynamicExecutionContextWithRevert() throws FileNotFoundException, DslProcessorException {
DslParser parser = DslParser.fromResource("dsl/transaction_storage_rskip446/dynamic_execution_context_with_revert.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);

String txDynamicExecutionContextWithRevertTest = "txDynamicExecutionContextWithRevertTest";
assertTransactionReceiptWithStatus(world, txDynamicExecutionContextWithRevertTest, "b01", true);

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

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

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

@Test
void testDynamicExecutionContextWithInvalid() throws FileNotFoundException, DslProcessorException {
DslParser parser = DslParser.fromResource("dsl/transaction_storage_rskip446/dynamic_execution_context_with_invalid.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);

String txDynamicExecutionContextWithInvalidTest = "txDynamicExecutionContextWithInvalidTest";
assertTransactionReceiptWithStatus(world, txDynamicExecutionContextWithInvalidTest, "b01", true);

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

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

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

@Test
void testDynamicExecutionContextWithStackOverflow() throws FileNotFoundException, DslProcessorException {
DslParser parser = DslParser.fromResource("dsl/transaction_storage_rskip446/dynamic_execution_context_with_stack_overflow.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);

String txDynamicExecutionContextWithStackOverflowTest = "txDynamicExecutionContextWithStackOverflowTest";
assertTransactionReceiptWithStatus(world, txDynamicExecutionContextWithStackOverflowTest, "b01", true);

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

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

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

private static TransactionReceipt assertTransactionReceiptWithStatus(World world, String txName, String blockName, boolean withSuccess) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
comment

// CONTRACT CODE
pragma solidity ^0.8.24;

contract TestTransientStorageCallAndDelegateCall {

constructor() {
}

event OK();
event ERROR(string, uint256);

function testCallCodeAndDelegateCall(uint256 useCallCode) external {
// Deploy the Callee contract
address calleeAddress = address(new Callee());
uint256 success;
uint256 valueLoadedFrom0;
uint256 valueLoadedFrom1;
uint256 valueLoadedSstore2;
uint256 valueLoadedSstore3;
bytes4 executeSignature = bytes4(keccak256("execute()"));

assembly {
tstore(0, 420)
let availablePointer := mload(0x40)
mstore(availablePointer, executeSignature)

// If useCallCode is 0, execute callCode otherwise execute delegateCall
switch useCallCode
case 1 {
success := callcode(gas(), calleeAddress, 0, availablePointer, 0x4, availablePointer, 0x20)
}
default {
success := delegatecall(gas(), calleeAddress, availablePointer, 0x4, availablePointer, 0x20)
}
valueLoadedFrom0 := tload(0)
valueLoadedSstore2 := sload(2)
valueLoadedSstore3 := sload(3)
valueLoadedFrom1 := tload(1)
}

checkReturnValueExpected(success, 'Checking result callee execution', 1);
checkReturnValueExpected(valueLoadedFrom0, 'Checking value from tload 0', 420);
checkReturnValueExpected(valueLoadedSstore2, 'Checking value from sstore 2', 420);
checkReturnValueExpected(valueLoadedSstore3, 'Checking value from sstore 3', 69);
checkReturnValueExpected(valueLoadedFrom1, 'Checking value from tload 1', 69);
}

function checkReturnValueExpected(uint256 valueReceived, string memory message, uint256 expectedValue) private {
if( valueReceived == expectedValue){
emit OK();
} else {
emit ERROR(message, valueReceived);
}
}
}

contract Callee {

function execute() external {
assembly {
sstore(2, tload(0))
tstore(1, 69)
sstore(3, tload(1))
}
}
}

// CONTRACT BYTECODE

TestTransientStorageCallAndDelegateCall: 6080604052348015600e575f5ffd5b5061048e8061001c5f395ff3fe608060405234801561000f575f5ffd5b5060043610610029575f3560e01c8063a61face91461002d575b5f5ffd5b610047600480360381019061004291906102ec565b610049565b005b5f604051610056906102a9565b604051809103905ff08015801561006f573d5f5f3e3d5ffd5b5090505f5f5f5f5f5f7f614619540b5b5abe478b88f28a37eb328054be3b41a7570ad5e8b701113364c490506101a45f5d60405181815288600181146100bf576020826004848c5af497506100cc565b6020826004845f8d5af297505b505f5c95506002549350600354925060015c945050610122866040518060400160405280602081526020017f436865636b696e6720726573756c742063616c6c656520657865637574696f6e8152506001610232565b610164856040518060400160405280601b81526020017f436865636b696e672076616c75652066726f6d20746c6f6164203000000000008152506101a4610232565b6101a6836040518060400160405280601c81526020017f436865636b696e672076616c75652066726f6d207373746f72652032000000008152506101a4610232565b6101e7826040518060400160405280601c81526020017f436865636b696e672076616c75652066726f6d207373746f72652033000000008152506045610232565b610228846040518060400160405280601b81526020017f436865636b696e672076616c75652066726f6d20746c6f6164203100000000008152506045610232565b5050505050505050565b80830361026a577fd48fe2800bace8f5ca2450feacbd6efc681b1cd0115019bb49fa529b6171bf6760405160405180910390a16102a4565b7fc9e730d5b570f89e168eb8c3d29f8c396b957e540af248c95c9519ac47c2c69f828460405161029b929190610396565b60405180910390a15b505050565b6094806103c583390190565b5f5ffd5b5f819050919050565b6102cb816102b9565b81146102d5575f5ffd5b50565b5f813590506102e6816102c2565b92915050565b5f60208284031215610301576103006102b5565b5b5f61030e848285016102d8565b91505092915050565b5f81519050919050565b5f82825260208201905092915050565b8281835e5f83830152505050565b5f601f19601f8301169050919050565b5f61035982610317565b6103638185610321565b9350610373818560208601610331565b61037c8161033f565b840191505092915050565b610390816102b9565b82525050565b5f6040820190508181035f8301526103ae818561034f565b90506103bd6020830184610387565b939250505056fe6080604052348015600e575f5ffd5b50607a80601a5f395ff3fe6080604052348015600e575f5ffd5b50600436106026575f3560e01c80636146195414602a575b5f5ffd5b60306032565b005b5f5c600255604560015d60015c60035556fea26469706673582212201771c67495697664bc8f0b297a683b4e7dbb7e31feaf9cf735faf209a14d64fa64736f6c634300081c0033a2646970667358221220ce4a7e6da5344a1d5a34ebef6c0267ac15796a3a9529427da9ba17a9cc4bee0864736f6c634300081c0033

a61face9: testCallCodeAndDelegateCall(uint256)

end

# Create and fund new account
account_new acc1 10000000

# Create transaction to deploy TestTransientStorageCallAndDelegateCall contract
transaction_build txCallAndDelegateCallSimpleTest
sender acc1
receiverAddress 00
value 0
data 6080604052348015600e575f5ffd5b5061048e8061001c5f395ff3fe608060405234801561000f575f5ffd5b5060043610610029575f3560e01c8063a61face91461002d575b5f5ffd5b610047600480360381019061004291906102ec565b610049565b005b5f604051610056906102a9565b604051809103905ff08015801561006f573d5f5f3e3d5ffd5b5090505f5f5f5f5f5f7f614619540b5b5abe478b88f28a37eb328054be3b41a7570ad5e8b701113364c490506101a45f5d60405181815288600181146100bf576020826004848c5af497506100cc565b6020826004845f8d5af297505b505f5c95506002549350600354925060015c945050610122866040518060400160405280602081526020017f436865636b696e6720726573756c742063616c6c656520657865637574696f6e8152506001610232565b610164856040518060400160405280601b81526020017f436865636b696e672076616c75652066726f6d20746c6f6164203000000000008152506101a4610232565b6101a6836040518060400160405280601c81526020017f436865636b696e672076616c75652066726f6d207373746f72652032000000008152506101a4610232565b6101e7826040518060400160405280601c81526020017f436865636b696e672076616c75652066726f6d207373746f72652033000000008152506045610232565b610228846040518060400160405280601b81526020017f436865636b696e672076616c75652066726f6d20746c6f6164203100000000008152506045610232565b5050505050505050565b80830361026a577fd48fe2800bace8f5ca2450feacbd6efc681b1cd0115019bb49fa529b6171bf6760405160405180910390a16102a4565b7fc9e730d5b570f89e168eb8c3d29f8c396b957e540af248c95c9519ac47c2c69f828460405161029b929190610396565b60405180910390a15b505050565b6094806103c583390190565b5f5ffd5b5f819050919050565b6102cb816102b9565b81146102d5575f5ffd5b50565b5f813590506102e6816102c2565b92915050565b5f60208284031215610301576103006102b5565b5b5f61030e848285016102d8565b91505092915050565b5f81519050919050565b5f82825260208201905092915050565b8281835e5f83830152505050565b5f601f19601f8301169050919050565b5f61035982610317565b6103638185610321565b9350610373818560208601610331565b61037c8161033f565b840191505092915050565b610390816102b9565b82525050565b5f6040820190508181035f8301526103ae818561034f565b90506103bd6020830184610387565b939250505056fe6080604052348015600e575f5ffd5b50607a80601a5f395ff3fe6080604052348015600e575f5ffd5b50600436106026575f3560e01c80636146195414602a575b5f5ffd5b60306032565b005b5f5c600255604560015d60015c60035556fea26469706673582212201771c67495697664bc8f0b297a683b4e7dbb7e31feaf9cf735faf209a14d64fa64736f6c634300081c0033a2646970667358221220ce4a7e6da5344a1d5a34ebef6c0267ac15796a3a9529427da9ba17a9cc4bee0864736f6c634300081c0033
gas 1000000
build

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

# Connect block
block_connect b01

# Check b01 is best block
assert_best b01

# Create transaction to execute txExecuteCallCode transaction
transaction_build txExecuteCallCode
sender acc1
nonce 1
contract txCallAndDelegateCallSimpleTest
value 0
data a61face90000000000000000000000000000000000000000000000000000000000000001
gas 1000000
build

# Create block to hold txExecuteCallCode transaction
block_build b02
parent b01
transactions txExecuteCallCode
gasLimit 2000000
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
build

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

# Connect block
block_connect b03

# Check b03 is best block
assert_best b03
Loading

0 comments on commit 8bba650

Please sign in to comment.