Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cancun] [eip6780] apply danno's clarification: disable eth burn on preexisting contract when inheritor equals contract address #6006

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Nethermind/Nethermind.Evm.Test/Eip6780Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,40 @@ public void self_destruct_not_in_same_transaction(ulong timestamp, bool onlyOnSa
AssertDestroyed();
}

[TestCase(0ul, false)]
[TestCase(MainnetSpecProvider.CancunBlockTimestamp, true)]
public void self_destruct_not_in_same_transaction_should_not_burn(ulong timestamp, bool onlyOnSameTransaction)
{
_selfDestructCode = Prepare.EvmCode
.SELFDESTRUCT(_contractAddress)
.Done;
_initCode = Prepare.EvmCode
.ForInitOf(_selfDestructCode)
.Done;
byte[] contractCall = Prepare.EvmCode
.Call(_contractAddress, 100000)
.Op(Instruction.STOP).Done;
Transaction initTx = Build.A.Transaction.WithCode(_initCode).WithValue(99.Ether()).WithGasLimit(_gasLimit).SignedAndResolved(_ecdsa, TestItem.PrivateKeyA).TestObject;
Transaction tx1 = Build.A.Transaction.WithCode(contractCall).WithGasLimit(_gasLimit).WithNonce(1).SignedAndResolved(_ecdsa, TestItem.PrivateKeyA).TestObject;
Block block = Build.A.Block.WithNumber(BlockNumber)
.WithTimestamp(timestamp)
.WithTransactions(initTx, tx1).WithGasLimit(2 * _gasLimit).TestObject;

_processor.Execute(initTx, block.Header, NullTxTracer.Instance);
UInt256 contractBalanceAfterInit = TestState.GetBalance(_contractAddress);
_processor.Execute(tx1, block.Header, NullTxTracer.Instance);

contractBalanceAfterInit.Should().Be(99.Ether());
if (onlyOnSameTransaction)
TestState.GetBalance(_contractAddress).Should().Be(99.Ether()); // not burnt
else
TestState.GetBalance(_contractAddress).Should().Be(0); // burnt
if (onlyOnSameTransaction)
AssertNotDestroyed();
else
AssertDestroyed();
}

[Test]
public void self_destruct_in_same_transaction()
{
Expand Down
6 changes: 5 additions & 1 deletion src/Nethermind/Nethermind.Evm/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2340,7 +2340,8 @@ private bool InstructionSelfDestruct<TTracing>(EvmState vmState, ref EvmStack<TT
if (!ChargeAccountAccessGas(ref gasAvailable, vmState, inheritor, spec, false)) return false;

Address executingAccount = vmState.Env.ExecutingAccount;
if (!spec.SelfdestructOnlyOnSameTransaction || vmState.CreateList.Contains(executingAccount))
bool createInSameTx = vmState.CreateList.Contains(executingAccount);
if (!spec.SelfdestructOnlyOnSameTransaction || createInSameTx)
vmState.DestroyList.Add(executingAccount);

UInt256 result = _state.GetBalance(executingAccount);
Expand All @@ -2365,6 +2366,9 @@ private bool InstructionSelfDestruct<TTracing>(EvmState vmState, ref EvmStack<TT
_state.AddToBalance(inheritor, result, spec);
}

if (spec.SelfdestructOnlyOnSameTransaction && !createInSameTx && inheritor.Equals(executingAccount))
return true; // dont burn eth when contract is not destroyed per EIP clarification

_state.SubtractFromBalance(executingAccount, result, spec);
return true;
Comment on lines +2369 to 2373
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't:

        if (!spec.SelfdestructOnlyOnSameTransaction || createInSameTx || !inheritor.Equals(executingAccount))
        {
            _state.SubtractFromBalance(executingAccount, result, spec);
        }

        return true;

be more readable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to specify only the condition where the burn does not happen. I find it less confusing than specifying all the conditions that it does burn.

}
Expand Down
Loading