Skip to content

Commit

Permalink
core/vm: add selfdestruct opcode test after Cancun
Browse files Browse the repository at this point in the history
This commit adds a test to ensure selfdestruct opcode after Cancun still creates
write protection error in read only mode and halts the code execution.
  • Loading branch information
minh-bq committed Aug 20, 2024
1 parent 1fb2fc0 commit 12b579b
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions core/vm/instructions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,3 +953,45 @@ func TestOpMCopy(t *testing.T) {
}
}
}

// Test that selfdestruct still creates write protection error in read only mode and halts the code execution
func TestSelfdestructCancun(t *testing.T) {
statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
env := NewEVM(BlockContext{BlockNumber: common.Big0}, TxContext{}, statedb, params.TestChainConfig, Config{})
contractAddr := common.Address{0x2}
contract := &Contract{
Code: []byte{
byte(PUSH1), byte(0x1), // dummy address
byte(SELFDESTRUCT),
},
self: contractRef{addr: contractAddr},
Gas: 100_000_000,
}

statedb.AddAddressToAccessList(contractAddr)
_, err := env.interpreter.Run(contract, nil, true)
if !errors.Is(err, ErrWriteProtection) {
t.Fatalf("Expect error %v got %v", ErrWriteProtection, err)
}

// Expect selfdestruct to stop the code execution in the contract
// so SSTORE instruction is not executed
contract.Code = []byte{
byte(PUSH1), byte(0x1), // dummy address
byte(SELFDESTRUCT),
byte(PUSH1), byte(0x1), // value
byte(PUSH1), byte(0x1), // storage slot
byte(SSTORE),
byte(STOP),
}

_, err = env.interpreter.Run(contract, nil, false)
if err != nil {
t.Fatal(err)
}

value := statedb.GetState(contractAddr, common.BigToHash(common.Big1)).Big()
if value.Cmp(common.Big0) != 0 {
t.Fatalf("Expect storage slot to be %d got %d", common.Big0.Uint64(), value.Uint64())
}
}

0 comments on commit 12b579b

Please sign in to comment.