Skip to content
This repository was archived by the owner on Jan 9, 2025. It is now read-only.

Commit a1f2456

Browse files
fix: create restores correct state upon failure (#985)
<!--- Please provide a general summary of your changes in the title above --> <!-- Give an estimate of the time you spent on this PR in terms of work days. Did you spend 0.5 days on this PR or rather 2 days? --> **DEPENDS ON #982** Time spent on this PR: 0.4d ## Pull request type <!-- Please try to limit your pull request to one type, submit multiple pull requests if needed. --> Please check the type of change your PR introduces: - [ ] Bugfix - [ ] Feature - [ ] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes, no api changes) - [ ] Build related changes - [ ] Documentation content changes - [ ] Other (please describe): ## What is the current behavior? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> Resolves #<Issue number> ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - Fixes restoration of the previous state upon CREATE failure. Previous implementation did not take into account the possibliity that the commitment of the code would fail. - - <!-- Reviewable:start --> - - - This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/kkrt-labs/kakarot/985) <!-- Reviewable:end --> Co-authored-by: Clément Walter <clement0walter@gmail.com>
1 parent 1b86d8a commit a1f2456

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

blockchain-tests-skip.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ testname:
4949
- create2collisionStorage_d1g0v0_Shanghai
5050
- create2collisionStorage_d2g0v0_Shanghai
5151
stCreateTest:
52-
- CreateAddressWarmAfterFail_d11g0v1_Shanghai
53-
- CreateAddressWarmAfterFail_d12g0v1_Shanghai
54-
- CreateAddressWarmAfterFail_d2g0v1_Shanghai
55-
- CreateAddressWarmAfterFail_d3g0v1_Shanghai
5652
- CreateOOGafterMaxCodesize_d2g0v0_Shanghai
5753
- CreateOOGafterMaxCodesize_d3g0v0_Shanghai
5854
- CreateOOGafterMaxCodesize_d5g0v0_Shanghai

src/kakarot/instructions/system_operations.cairo

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,14 @@ namespace SystemOperations {
7979
let (bytecode: felt*) = alloc();
8080
Memory.load_n(size.low, bytecode, offset.low);
8181
82-
// Get target address
8382
let target_address = CreateHelper.get_evm_address(
8483
evm.message.address.evm, popped_len, popped, size.low, bytecode
8584
);
8685
86+
// @dev: performed before eventual subsequent early-returns of this function
87+
// to mark the account as warm EIP-2929
88+
let target_account = State.get_account(target_address);
89+
8790
// Get message call gas
8891
let (gas_limit, _) = unsigned_div_rem(evm.gas_left, 64);
8992
let gas_limit = evm.gas_left - gas_limit;
@@ -95,11 +98,6 @@ namespace SystemOperations {
9598
return evm;
9699
}
97100
98-
// TODO: Clear return data
99-
// @dev: performed before eventual early-returns of this function
100-
// to mark the account as warm
101-
let target_account = State.get_account(target_address);
102-
103101
// Check sender balance and nonce
104102
let sender = State.get_account(evm.message.address.evm);
105103
let is_nonce_overflow = Helpers.is_zero(Constants.MAX_NONCE - sender.nonce);
@@ -878,6 +876,14 @@ namespace CallHelper {
878876
let is_reverted = is_not_zero(evm.reverted);
879877
Stack.push_uint128(1 - is_reverted);
880878

879+
// Restore parent state if the call has reverted
880+
if (evm.reverted != FALSE) {
881+
tempvar state = evm.message.parent.state;
882+
} else {
883+
tempvar state = state;
884+
}
885+
let state = cast([ap - 1], model.State*);
886+
881887
if (evm.reverted == Errors.EXCEPTIONAL_HALT) {
882888
// If the call has halted exceptionnaly, the return_data is empty
883889
// and nothing is copied to memory, and the gas is not returned;
@@ -1050,6 +1056,7 @@ namespace CreateHelper {
10501056
}
10511057

10521058
// @notice At the end of a sub-context initiated with CREATE or CREATE2, the calling context's stack is updated.
1059+
// @dev Restores the parent state if the sub-context has reverted.
10531060
// @param evm The pointer to the calling context.
10541061
// @return EVM The pointer to the updated calling context.
10551062
func finalize_parent{
@@ -1074,6 +1081,9 @@ namespace CreateHelper {
10741081
10751082
tempvar stack_code = new Uint256(low=0, high=0);
10761083
Stack.push(stack_code);
1084+
1085+
tempvar state = evm.message.parent.state;
1086+
10771087
tempvar evm = new model.EVM(
10781088
message=evm.message.parent.evm.message,
10791089
return_data_len=return_data_len,
@@ -1107,6 +1117,8 @@ namespace CreateHelper {
11071117
Stack.push(address);
11081118

11091119
if (success == FALSE) {
1120+
tempvar state = evm.message.parent.state;
1121+
11101122
tempvar evm = new model.EVM(
11111123
message=evm.message.parent.evm.message,
11121124
return_data_len=0,

src/kakarot/interpreter.cairo

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -710,11 +710,6 @@ namespace Interpreter {
710710

711711
let stack = evm.message.parent.stack;
712712
let memory = evm.message.parent.memory;
713-
if (evm.reverted == FALSE) {
714-
tempvar state = state;
715-
} else {
716-
tempvar state = evm.message.parent.state;
717-
}
718713

719714
if (evm.message.is_create != FALSE) {
720715
let evm = CreateHelper.finalize_parent(evm);

0 commit comments

Comments
 (0)