Skip to content

Commit

Permalink
feat(evm): disable SELFDESTRUCT only if the zktrie config is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Pangssu committed Jan 31, 2024
1 parent 3579b97 commit dc9dbfd
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 84 deletions.
4 changes: 2 additions & 2 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3088,7 +3088,7 @@ func TestSelfDestructDisabled(t *testing.T) {
)

gspec := &Genesis{
Config: params.TestChainConfig,
Config: params.KromaTestConfig,
Alloc: GenesisAlloc{
address: {Balance: funds},
// The address 0xAAAAA selfdestructs if called
Expand All @@ -3103,7 +3103,7 @@ func TestSelfDestructDisabled(t *testing.T) {
}
genesis := gspec.MustCommit(db, trie.NewDatabase(db, trie.GetHashDefaults(gspec.Config.Zktrie)))

_, receipts := GenerateChain(params.TestChainConfig, genesis, engine, db, 1, func(i int, b *BlockGen) {
_, receipts := GenerateChain(params.KromaTestConfig, genesis, engine, db, 1, func(i int, b *BlockGen) {
b.SetCoinbase(common.Address{1})
// One transaction to AA, to kill it
tx, _ := types.SignTx(types.NewTransaction(0, aa,
Expand Down
29 changes: 10 additions & 19 deletions core/vm/eips.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,8 @@ func enable2929(jt *JumpTable) {
jt[DELEGATECALL].constantGas = params.WarmStorageReadCostEIP2929
jt[DELEGATECALL].dynamicGas = gasDelegateCallEIP2929

// [Scroll: START]
// NOTE: SELFDESTRUCT is disabled in Kroma. This is not meant to disable
// forever this opcode. Once zkevm spec can cover it, we need to re-enable it.
// jt[SELFDESTRUCT].constantGas = params.SelfdestructGasEIP150
// jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP2929
// [Scroll: END]
jt[SELFDESTRUCT].constantGas = params.SelfdestructGasEIP150
jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP2929
}

// enable3529 enabled "EIP-3529: Reduction in refunds":
Expand All @@ -161,12 +157,7 @@ func enable2929(jt *JumpTable) {
// - Reduces max refunds to 20% gas
func enable3529(jt *JumpTable) {
jt[SSTORE].dynamicGas = gasSStoreEIP3529

// [Scroll: START]
// NOTE: SELFDESTRUCT is disabled in Kroma. This is not meant to disable
// forever this opcode. Once zkevm spec can cover it, we need to re-enable it.
// jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP3529
// [Scroll: END]
jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP3529
}

// enable3198 applies EIP-3198 (BASEFEE Opcode)
Expand Down Expand Up @@ -318,11 +309,11 @@ func enable7516(jt *JumpTable) {

// enable6780 applies EIP-6780 (deactivate SELFDESTRUCT)
func enable6780(jt *JumpTable) {
//jt[SELFDESTRUCT] = &operation{
// execute: opSelfdestruct6780,
// dynamicGas: gasSelfdestructEIP3529,
// constantGas: params.SelfdestructGasEIP150,
// minStack: minStack(1, 0),
// maxStack: maxStack(1, 0),
//}
jt[SELFDESTRUCT] = &operation{
execute: opSelfdestruct6780,
dynamicGas: gasSelfdestructEIP3529,
constantGas: params.SelfdestructGasEIP150,
minStack: minStack(1, 0),
maxStack: maxStack(1, 0),
}
}
6 changes: 0 additions & 6 deletions core/vm/gas_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,6 @@ func gasStaticCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memo
return gas, nil
}

// [Scroll: START]
/*
NOTE: SELFDESTRUCT is disabled in Kroma. This is not meant to disable
forever this opcode. Once zkevm spec can cover it, we need to re-enable it.
func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
var gas uint64
// EIP150 homestead gas reprice fork:
Expand All @@ -481,5 +477,3 @@ func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, me
}
return gas, nil
}
*/
// [Scroll: END]
14 changes: 4 additions & 10 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func opCreate(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]b
stackvalue := size

scope.Contract.UseGas(gas)
//TODO: use uint256.Int instead of converting with toBig()
// TODO: use uint256.Int instead of converting with toBig()
var bigVal = big0
if !value.IsZero() {
bigVal = value.ToBig()
Expand Down Expand Up @@ -637,7 +637,7 @@ func opCreate2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]
scope.Contract.UseGas(gas)
// reuse size int for stackvalue
stackvalue := size
//TODO: use uint256.Int instead of converting with toBig()
// TODO: use uint256.Int instead of converting with toBig()
bigEndowment := big0
if !endowment.IsZero() {
bigEndowment = endowment.ToBig()
Expand Down Expand Up @@ -677,7 +677,7 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byt
return nil, ErrWriteProtection
}
var bigVal = big0
//TODO: use uint256.Int instead of converting with toBig()
// TODO: use uint256.Int instead of converting with toBig()
// By using big0 here, we save an alloc for the most common case (non-ether-transferring contract calls),
// but it would make more sense to extend the usage of uint256.Int
if !value.IsZero() {
Expand Down Expand Up @@ -714,7 +714,7 @@ func opCallCode(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([
// Get arguments from the memory.
args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))

//TODO: use uint256.Int instead of converting with toBig()
// TODO: use uint256.Int instead of converting with toBig()
var bigVal = big0
if !value.IsZero() {
gas += params.CallStipend
Expand Down Expand Up @@ -816,10 +816,6 @@ func opStop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byt
return nil, errStopToken
}

// [Scroll: START]
/*
NOTE: SELFDESTRUCT is disabled in Kroma. This is not meant to disable
forever this opcode. Once zkevm spec can cover it, we need to re-enable it.
func opSelfdestruct(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
if interpreter.readOnly {
return nil, ErrWriteProtection
Expand Down Expand Up @@ -850,8 +846,6 @@ func opSelfdestruct6780(pc *uint64, interpreter *EVMInterpreter, scope *ScopeCon
}
return nil, errStopToken
}
*/
// [Scroll: END]

// following functions are used by the instruction jump table

Expand Down
10 changes: 10 additions & 0 deletions core/vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ func NewEVMInterpreter(evm *EVM) *EVMInterpreter {
default:
table = &frontierInstructionSet
}
// [Scroll: START]
// NOTE: SELFDESTRUCT is disabled in Kroma. This is not meant to disable
// forever this opcode. Once zkEVM spec can cover it, we need to re-enable it.
if evm.chainConfig.Zktrie {
table[SELFDESTRUCT] = &operation{
execute: opUndefined,
maxStack: maxStack(0, 0),
}
}
// [Scroll: END]
var extraEips []int
if len(evm.Config.ExtraEips) > 0 {
// Deep-copy jumptable to prevent modification of opcodes in other tables
Expand Down
19 changes: 6 additions & 13 deletions core/vm/jump_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,19 +1046,12 @@ func newFrontierInstructionSet() JumpTable {
maxStack: maxStack(2, 0),
memorySize: memoryReturn,
},
// [Scroll: START]
/*
NOTE: SELFDESTRUCT is disabled in Kroma. This is not meant to disable
forever this opcode. Once zkevm spec can cover it, we need to re-enable it.
SELFDESTRUCT: {
execute: opSelfdestruct,
dynamicGas: gasSelfdestruct,
minStack: minStack(1, 0),
maxStack: maxStack(1, 0),
},
*/
SELFDESTRUCT: nil,
// [Scroll: END]
SELFDESTRUCT: {
execute: opSelfdestruct,
dynamicGas: gasSelfdestruct,
minStack: minStack(1, 0),
maxStack: maxStack(1, 0),
},
}

// Fill all unassigned slots with opUndefined.
Expand Down
21 changes: 6 additions & 15 deletions core/vm/operations_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc {
if original == value {
if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1)
// EIP 2200 Original clause:
//evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.SloadGasEIP2200)
// evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.SloadGasEIP2200)
evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.WarmStorageReadCostEIP2929)
} else { // reset to original existing slot (2.2.2.2)
// EIP 2200 Original clause:
Expand All @@ -90,7 +90,7 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc {
}
}
// EIP-2200 original clause:
//return params.SloadGasEIP2200, nil // dirty update (2.2)
// return params.SloadGasEIP2200, nil // dirty update (2.2)
return cost + params.WarmStorageReadCostEIP2929, nil // dirty update (2.2)
}
}
Expand Down Expand Up @@ -196,13 +196,9 @@ var (
gasDelegateCallEIP2929 = makeCallVariantGasCallEIP2929(gasDelegateCall)
gasStaticCallEIP2929 = makeCallVariantGasCallEIP2929(gasStaticCall)
gasCallCodeEIP2929 = makeCallVariantGasCallEIP2929(gasCallCode)
// [Scroll: START]
/*
gasSelfdestructEIP2929 = makeSelfdestructGasFn(true)
// gasSelfdestructEIP3529 implements the changes in EIP-2539 (no refunds)
gasSelfdestructEIP3529 = makeSelfdestructGasFn(false)
*/
// [Scroll: END]
gasSelfdestructEIP2929 = makeSelfdestructGasFn(true)
// gasSelfdestructEIP3529 implements the changes in EIP-2539 (no refunds)
gasSelfdestructEIP3529 = makeSelfdestructGasFn(false)

// gasSStoreEIP2929 implements gas cost for SSTORE according to EIP-2929
//
Expand All @@ -214,7 +210,7 @@ var (
// SLOAD_GAS 800 = WARM_STORAGE_READ_COST
// SSTORE_RESET_GAS 5000 5000 - COLD_SLOAD_COST
//
//The other parameters defined in EIP 2200 are unchanged.
// The other parameters defined in EIP 2200 are unchanged.
// see gasSStoreEIP2200(...) in core/vm/gas_table.go for more info about how EIP 2200 is specified
gasSStoreEIP2929 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP2200)

Expand All @@ -223,9 +219,6 @@ var (
gasSStoreEIP3529 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP3529)
)

// [Scroll: START]
/*
SELFDESTRUCT is disabled in Kroma
// makeSelfdestructGasFn can create the selfdestruct dynamic gas function for EIP-2929 and EIP-2539
func makeSelfdestructGasFn(refundsEnabled bool) gasFunc {
gasFunc := func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
Expand All @@ -249,5 +242,3 @@ func makeSelfdestructGasFn(refundsEnabled bool) gasFunc {
}
return gasFunc
}
*/
// [Scroll: END]
34 changes: 15 additions & 19 deletions core/vm/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ func (d *dummyChain) GetHeader(h common.Hash, n uint64) *types.Header {
s := common.LeftPadBytes(big.NewInt(int64(n-1)).Bytes(), 32)
copy(parentHash[:], s)

//parentHash := common.Hash{byte(n - 1)}
//fmt.Printf("GetHeader(%x, %d) => header with parent %x\n", h, n, parentHash)
// parentHash := common.Hash{byte(n - 1)}
// fmt.Printf("GetHeader(%x, %d) => header with parent %x\n", h, n, parentHash)
return fakeHeader(n, parentHash)
}

Expand Down Expand Up @@ -358,7 +358,7 @@ func benchmarkNonModifyingCode(gas uint64, code []byte, name string, tracerCode
})
}

//cfg.State.CreateAccount(cfg.Origin)
// cfg.State.CreateAccount(cfg.Origin)
// set the receiver's (the executing contract) code for execution.
cfg.State.SetCode(destination, code)
vmenv.Call(sender, destination, nil, gas, cfg.Value)
Expand Down Expand Up @@ -468,8 +468,8 @@ func BenchmarkSimpleLoop(b *testing.B) {
byte(vm.JUMP),
}

//tracer := vm.NewJSONLogger(nil, os.Stdout)
//Execute(loopingCode, nil, &Config{
// tracer := vm.NewJSONLogger(nil, os.Stdout)
// Execute(loopingCode, nil, &Config{
// EVMConfig: vm.Config{
// Debug: true,
// Tracer: tracer,
Expand All @@ -482,8 +482,8 @@ func BenchmarkSimpleLoop(b *testing.B) {
benchmarkNonModifyingCode(100000000, callEOA, "call-EOA-100M", "", b)
benchmarkNonModifyingCode(100000000, callRevertingContractWithInput, "call-reverting-100M", "", b)

//benchmarkNonModifyingCode(10000000, staticCallIdentity, "staticcall-identity-10M", b)
//benchmarkNonModifyingCode(10000000, loopingCode, "loop-10M", b)
// benchmarkNonModifyingCode(10000000, staticCallIdentity, "staticcall-identity-10M", b)
// benchmarkNonModifyingCode(10000000, loopingCode, "loop-10M", b)
}

// TestEip2929Cases contains various testcases that are used for
Expand Down Expand Up @@ -543,13 +543,13 @@ func TestEip2929Cases(t *testing.T) {
{ // EXTCODECOPY
code := []byte{
// extcodecopy( 0xff,0,0,0,0)
byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, //length, codeoffset, memoffset
byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, // length, codeoffset, memoffset
byte(vm.PUSH1), 0xff, byte(vm.EXTCODECOPY),
// extcodecopy( 0xff,0,0,0,0)
byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, //length, codeoffset, memoffset
byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, // length, codeoffset, memoffset
byte(vm.PUSH1), 0xff, byte(vm.EXTCODECOPY),
// extcodecopy( this,0,0,0,0)
byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, //length, codeoffset, memoffset
byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, byte(vm.PUSH1), 0x00, // length, codeoffset, memoffset
byte(vm.ADDRESS), byte(vm.EXTCODECOPY),

byte(vm.STOP),
Expand Down Expand Up @@ -656,11 +656,7 @@ func TestColdAccountAccessCost(t *testing.T) {
byte(vm.PUSH1), 0xff, byte(vm.SELFDESTRUCT),
},
step: 1,
// [Scroll: START]
// TODO(chokobole): Reenable this test.
// want: 7600,
want: 0,
// [Scroll: END]
want: 7600,
},
} {
tracer := vm.NewStructLogger(nil)
Expand Down Expand Up @@ -751,7 +747,7 @@ func TestRuntimeJSTracer(t *testing.T) {
// outsize, outoffset, insize, inoffset
byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, byte(vm.PUSH1), 0,
byte(vm.PUSH1), 0, // value
byte(vm.PUSH1), 0xbb, //address
byte(vm.PUSH1), 0xbb, // address
byte(vm.GAS), // gas
byte(vm.CALL),
byte(vm.POP),
Expand All @@ -764,7 +760,7 @@ func TestRuntimeJSTracer(t *testing.T) {
// outsize, outoffset, insize, inoffset
byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, byte(vm.PUSH1), 0,
byte(vm.PUSH1), 0, // value
byte(vm.PUSH1), 0xcc, //address
byte(vm.PUSH1), 0xcc, // address
byte(vm.GAS), // gas
byte(vm.CALLCODE),
byte(vm.POP),
Expand All @@ -776,7 +772,7 @@ func TestRuntimeJSTracer(t *testing.T) {
code: []byte{
// outsize, outoffset, insize, inoffset
byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, byte(vm.PUSH1), 0,
byte(vm.PUSH1), 0xdd, //address
byte(vm.PUSH1), 0xdd, // address
byte(vm.GAS), // gas
byte(vm.STATICCALL),
byte(vm.POP),
Expand All @@ -788,7 +784,7 @@ func TestRuntimeJSTracer(t *testing.T) {
code: []byte{
// outsize, outoffset, insize, inoffset
byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, byte(vm.PUSH1), 0, byte(vm.PUSH1), 0,
byte(vm.PUSH1), 0xee, //address
byte(vm.PUSH1), 0xee, // address
byte(vm.GAS), // gas
byte(vm.DELEGATECALL),
byte(vm.POP),
Expand Down
1 change: 1 addition & 0 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ var (
conf.TerminalTotalDifficultyPassed = true
conf.BedrockBlock = big.NewInt(5)
conf.Kroma = &KromaConfig{EIP1559Elasticity: 50, EIP1559Denominator: 10}
conf.Zktrie = true
return &conf
}()
)
Expand Down

0 comments on commit dc9dbfd

Please sign in to comment.