Skip to content

Commit 98d653c

Browse files
core/vm: implement the remaining gas changes for 7904
1 parent 7949a92 commit 98d653c

File tree

5 files changed

+188
-23
lines changed

5 files changed

+188
-23
lines changed

core/vm/eips.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ func enable7904(jt *JumpTable) {
569569
jt[SMOD].constantGas = GasBaseCost
570570
jt[ADDMOD].constantGas = GasFastCost
571571
jt[MULMOD].constantGas = GasMidCost
572-
//jt[EXP].constantGas = GasMidCost
572+
jt[EXP].dynamicGas = gasExpEIP7904
573573
jt[SIGNEXTEND].constantGas = GasBaseCost
574574
jt[LT].constantGas = GasBaseCost
575575
jt[GT].constantGas = GasBaseCost
@@ -586,22 +586,34 @@ func enable7904(jt *JumpTable) {
586586
jt[SHR].constantGas = GasBaseCost
587587
jt[SAR].constantGas = GasBaseCost
588588
jt[ADDRESS].constantGas = GasBaseCost
589-
//jt[BALANCE].constantGas = GasBaseCost
589+
590+
jt[BALANCE].constantGas = params.WarmStorageReadCostEIP7904
591+
jt[BALANCE].dynamicGas = gasEip7904AccountCheck
590592

591593
jt[ORIGIN].constantGas = GasBaseCost
592594
jt[CALLER].constantGas = GasBaseCost
593595
jt[CALLVALUE].constantGas = GasBaseCost
594596
jt[CALLDATALOAD].constantGas = GasBaseCost
595597
jt[CALLDATASIZE].constantGas = GasBaseCost
596-
//jt[CALLDATACOPY].constantGas = GasBaseCost
598+
599+
jt[CALLDATACOPY].constantGas = GasBaseCost
600+
jt[CALLDATACOPY].dynamicGas = gasCallDataCopy7904
601+
597602
jt[CODESIZE].constantGas = GasBaseCost
598-
//jt[CODECOPY].constantGas = GasBaseCost
603+
604+
jt[CODECOPY].constantGas = GasBaseCost
605+
jt[CODECOPY].dynamicGas = gasCodeCopy7904
606+
599607
jt[GASPRICE].constantGas = GasBaseCost
600-
//jt[EXTCODESIZE].constantGas = GasBaseCost
601-
//jt[EXTCODECOPY].constantGas = GasBaseCost
608+
jt[EXTCODESIZE].dynamicGas = gasEip7904AccountCheck
609+
610+
jt[EXTCODECOPY].dynamicGas = gasExtCodeCopy7904
611+
602612
jt[RETURNDATASIZE].constantGas = GasBaseCost
603-
//jt[RETURNDATACOPY].constantGas = GasBaseCost
604-
//jt[EXTCODEHASH].constantGas = GasBaseCost
613+
jt[RETURNDATACOPY].dynamicGas = gasReturnDataCopy7904
614+
615+
jt[EXTCODEHASH].dynamicGas = gasEip7904AccountCheck
616+
605617
jt[COINBASE].constantGas = GasBaseCost
606618
jt[TIMESTAMP].constantGas = GasBaseCost
607619
jt[NUMBER].constantGas = GasBaseCost
@@ -610,16 +622,18 @@ func enable7904(jt *JumpTable) {
610622
jt[SELFBALANCE].constantGas = GasBaseCost
611623
jt[POP].constantGas = GasBaseCost
612624
jt[MLOAD].constantGas = GasBaseCost
613-
//jt[MSTORE].constantGas = GasBaseCost
614-
//jt[MSTORE8].constantGas = GasBaseCost
625+
jt[MSTORE].constantGas = GasBaseCost
626+
jt[MSTORE8].constantGas = GasBaseCost
615627
jt[JUMP].constantGas = GasBaseCost
616628
jt[JUMPI].constantGas = GasBaseCost
617629
jt[PC].constantGas = GasBaseCost
618630
jt[MSIZE].constantGas = GasBaseCost
619631
jt[TLOAD].constantGas = GasWarmStorageCost
620632
jt[TSTORE].constantGas = GasWarmStorageCost
621633
jt[JUMPDEST].constantGas = GasBaseCost
622-
//jt[MCOPY].constantGas = GasBaseCost
634+
635+
jt[MCOPY].dynamicGas = gasMcopy7904
636+
623637
jt[PUSH0].constantGas = GasBaseCost
624638
for i := PUSH1; i < PUSH32; i++ {
625639
jt[i].constantGas = GasBaseCost
@@ -633,4 +647,12 @@ func enable7904(jt *JumpTable) {
633647
for i := SWAP1; i < SWAP16; i++ {
634648
jt[i].constantGas = GasBaseCost
635649
}
650+
651+
jt[SLOAD].dynamicGas = gasSLoadEIP7904
652+
jt[SSTORE].dynamicGas = gasSStoreEIP7904
653+
jt[CALL].dynamicGas = gasCallEIP7904
654+
jt[STATICCALL].dynamicGas = gasStaticCallEIP7904
655+
jt[DELEGATECALL].dynamicGas = gasDelegateCallEIP7904
656+
657+
jt[CALLCODE].dynamicGas = gasCallCodeEIP7904
636658
}

core/vm/gas.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ const (
3333
GasFastCost uint64 = 2
3434
GasMidCost uint64 = 3
3535
GasWarmStorageCost uint64 = 5
36+
GasExpBaseCost uint64 = 2
37+
GasExpPerByteCost uint64 = 4
3638
)
3739

3840
// callGas returns the actual gas cost of the call.

core/vm/gas_table.go

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,49 @@ func memoryCopierGas(stackpos int) gasFunc {
8888
}
8989
}
9090

91+
// memoryCopierGas7904 creates the gas functions for the following opcodes, and takes
92+
// the stack position of the operand which determines the size of the data to copy
93+
// as argument:
94+
// CALLDATACOPY (stack position 2)
95+
// CODECOPY (stack position 2)
96+
// MCOPY (stack position 2)
97+
// EXTCODECOPY (stack position 3)
98+
// RETURNDATACOPY (stack position 2)
99+
func memoryCopierGas7904(stackpos int) gasFunc {
100+
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
101+
// Gas for expanding the memory
102+
gas, err := memoryGasCost(mem, memorySize)
103+
if err != nil {
104+
return 0, err
105+
}
106+
// And gas for copying data, charged per word at param.CopyGas
107+
words, overflow := stack.Back(stackpos).Uint64WithOverflow()
108+
if overflow {
109+
return 0, ErrGasUintOverflow
110+
}
111+
112+
if words, overflow = math.SafeMul(toWordSize(words), params.CopyGasEIP7904); overflow {
113+
return 0, ErrGasUintOverflow
114+
}
115+
116+
if gas, overflow = math.SafeAdd(gas, words); overflow {
117+
return 0, ErrGasUintOverflow
118+
}
119+
return gas, nil
120+
}
121+
}
122+
91123
var (
92-
gasCallDataCopy = memoryCopierGas(2)
93-
gasCodeCopy = memoryCopierGas(2)
94-
gasMcopy = memoryCopierGas(2)
95-
gasExtCodeCopy = memoryCopierGas(3)
96-
gasReturnDataCopy = memoryCopierGas(2)
124+
gasCallDataCopy = memoryCopierGas(2)
125+
gasCodeCopy = memoryCopierGas(2)
126+
gasMcopy = memoryCopierGas(2)
127+
gasExtCodeCopy = memoryCopierGas(3)
128+
gasReturnDataCopy = memoryCopierGas(2)
129+
gasCallDataCopy7904 = memoryCopierGas7904(2)
130+
gasCodeCopy7904 = memoryCopierGas7904(2)
131+
gasMcopy7904 = memoryCopierGas7904(2)
132+
gasExtCodeCopy7904 = memoryCopierGas7904(3)
133+
gasReturnDataCopy7904 = memoryCopierGas7904(2)
97134
)
98135

99136
func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
@@ -368,6 +405,19 @@ func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memor
368405
return gas, nil
369406
}
370407

408+
func gasExpEIP7904(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
409+
expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8)
410+
411+
var (
412+
gas = expByteLen * params.ExpByteEIP7904 // no overflow check required. Max is 256 * ExpByte gas
413+
overflow bool
414+
)
415+
if gas, overflow = math.SafeAdd(gas, params.ExpGasEIP7904); overflow {
416+
return 0, ErrGasUintOverflow
417+
}
418+
return gas, nil
419+
}
420+
371421
func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
372422
var (
373423
gas uint64

core/vm/operations_acl.go

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"github.com/ethereum/go-ethereum/params"
2727
)
2828

29-
func makeGasSStoreFunc(clearingRefund uint64) gasFunc {
29+
func makeGasSStoreFunc(clearingRefund uint64, warmLoadCosts uint64) gasFunc {
3030
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
3131
// If we fail the minimum gas availability invariant, fail (0)
3232
if contract.Gas <= params.SstoreSentryGasEIP2200 {
@@ -50,7 +50,7 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc {
5050
if current == value { // noop (1)
5151
// EIP 2200 original clause:
5252
// return params.SloadGasEIP2200, nil
53-
return cost + params.WarmStorageReadCostEIP2929, nil // SLOAD_GAS
53+
return cost + warmLoadCosts, nil // SLOAD_GAS
5454
}
5555
if original == current {
5656
if original == (common.Hash{}) { // create slot (2.1.1)
@@ -74,19 +74,19 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc {
7474
if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1)
7575
// EIP 2200 Original clause:
7676
//evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.SloadGasEIP2200)
77-
evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.WarmStorageReadCostEIP2929)
77+
evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - warmLoadCosts)
7878
} else { // reset to original existing slot (2.2.2.2)
7979
// EIP 2200 Original clause:
8080
// evm.StateDB.AddRefund(params.SstoreResetGasEIP2200 - params.SloadGasEIP2200)
8181
// - SSTORE_RESET_GAS redefined as (5000 - COLD_SLOAD_COST)
8282
// - SLOAD_GAS redefined as WARM_STORAGE_READ_COST
8383
// Final: (5000 - COLD_SLOAD_COST) - WARM_STORAGE_READ_COST
84-
evm.StateDB.AddRefund((params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929) - params.WarmStorageReadCostEIP2929)
84+
evm.StateDB.AddRefund((params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929) - warmLoadCosts)
8585
}
8686
}
8787
// EIP-2200 original clause:
8888
//return params.SloadGasEIP2200, nil // dirty update (2.2)
89-
return cost + params.WarmStorageReadCostEIP2929, nil // dirty update (2.2)
89+
return cost + warmLoadCosts, nil // dirty update (2.2)
9090
}
9191
}
9292

@@ -108,6 +108,24 @@ func gasSLoadEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, me
108108
return params.WarmStorageReadCostEIP2929, nil
109109
}
110110

111+
// gasSLoadEIP7904 calculates dynamic gas for SLOAD according to EIP-7904
112+
// For SLOAD, if the (address, storage_key) pair (where address is the address of the contract
113+
// whose storage is being read) is not yet in accessed_storage_keys,
114+
// charge 2100 gas and add the pair to accessed_storage_keys.
115+
// If the pair is already in accessed_storage_keys, charge 5 gas.
116+
func gasSLoadEIP7904(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
117+
loc := stack.peek()
118+
slot := common.Hash(loc.Bytes32())
119+
// Check slot presence in the access list
120+
if _, slotPresent := evm.StateDB.SlotInAccessList(contract.Address(), slot); !slotPresent {
121+
// If the caller cannot afford the cost, this change will be rolled back
122+
// If he does afford it, we can skip checking the same thing later on, during execution
123+
evm.StateDB.AddSlotToAccessList(contract.Address(), slot)
124+
return params.ColdSloadCostEIP2929, nil
125+
}
126+
return params.WarmStorageReadCostEIP7904, nil
127+
}
128+
111129
// gasExtCodeCopyEIP2929 implements extcodecopy according to EIP-2929
112130
// EIP spec:
113131
// > If the target is not in accessed_addresses,
@@ -152,6 +170,25 @@ func gasEip2929AccountCheck(evm *EVM, contract *Contract, stack *Stack, mem *Mem
152170
return 0, nil
153171
}
154172

173+
// gasEip7904AccountCheck checks whether the first stack item (as address) is present in the access list.
174+
// If it is, this method returns '0', otherwise 'cold-warm' gas, presuming that the opcode using it
175+
// is also using 'warm' as constant factor.
176+
// This method is used by:
177+
// - extcodehash,
178+
// - extcodesize,
179+
// - (ext) balance
180+
func gasEip7904AccountCheck(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
181+
addr := common.Address(stack.peek().Bytes20())
182+
// Check slot presence in the access list
183+
if !evm.StateDB.AddressInAccessList(addr) {
184+
// If the caller cannot afford the cost, this change will be rolled back
185+
evm.StateDB.AddAddressToAccessList(addr)
186+
// The warm storage read cost is already charged as constantGas
187+
return params.ColdAccountAccessCostEIP2929 - params.WarmStorageReadCostEIP7904, nil
188+
}
189+
return 0, nil
190+
}
191+
155192
func makeCallVariantGasCallEIP2929(oldCalculator gasFunc, addressPosition int) gasFunc {
156193
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
157194
addr := common.Address(stack.Back(addressPosition).Bytes20())
@@ -191,11 +228,55 @@ func makeCallVariantGasCallEIP2929(oldCalculator gasFunc, addressPosition int) g
191228
}
192229
}
193230

231+
func makeCallVariantGasCallEIP7904(oldCalculator gasFunc, addressPosition int) gasFunc {
232+
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
233+
addr := common.Address(stack.Back(addressPosition).Bytes20())
234+
// Check slot presence in the access list
235+
warmAccess := evm.StateDB.AddressInAccessList(addr)
236+
// The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so
237+
// the cost to charge for cold access, if any, is Cold - Warm
238+
coldCost := params.ColdAccountAccessCostEIP2929 - params.WarmStorageReadCostEIP7904
239+
if !warmAccess {
240+
evm.StateDB.AddAddressToAccessList(addr)
241+
// Charge the remaining difference here already, to correctly calculate available
242+
// gas for call
243+
if !contract.UseGas(coldCost, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) {
244+
return 0, ErrOutOfGas
245+
}
246+
}
247+
// Now call the old calculator, which takes into account
248+
// - create new account
249+
// - transfer value
250+
// - memory expansion
251+
// - 63/64ths rule
252+
gas, err := oldCalculator(evm, contract, stack, mem, memorySize)
253+
if warmAccess || err != nil {
254+
return gas, err
255+
}
256+
// In case of a cold access, we temporarily add the cold charge back, and also
257+
// add it to the returned gas. By adding it to the return, it will be charged
258+
// outside of this function, as part of the dynamic gas, and that will make it
259+
// also become correctly reported to tracers.
260+
contract.Gas += coldCost
261+
262+
var overflow bool
263+
if gas, overflow = math.SafeAdd(gas, coldCost); overflow {
264+
return 0, ErrGasUintOverflow
265+
}
266+
return gas, nil
267+
}
268+
}
269+
194270
var (
195271
gasCallEIP2929 = makeCallVariantGasCallEIP2929(gasCall, 1)
196272
gasDelegateCallEIP2929 = makeCallVariantGasCallEIP2929(gasDelegateCall, 1)
197273
gasStaticCallEIP2929 = makeCallVariantGasCallEIP2929(gasStaticCall, 1)
198274
gasCallCodeEIP2929 = makeCallVariantGasCallEIP2929(gasCallCode, 1)
275+
276+
gasCallEIP7904 = makeCallVariantGasCallEIP7904(gasCall, 1)
277+
gasDelegateCallEIP7904 = makeCallVariantGasCallEIP7904(gasDelegateCall, 1)
278+
gasStaticCallEIP7904 = makeCallVariantGasCallEIP7904(gasStaticCall, 1)
279+
gasCallCodeEIP7904 = makeCallVariantGasCallEIP7904(gasCallCode, 1)
199280
gasSelfdestructEIP2929 = makeSelfdestructGasFn(true)
200281
// gasSelfdestructEIP3529 implements the changes in EIP-3529 (no refunds)
201282
gasSelfdestructEIP3529 = makeSelfdestructGasFn(false)
@@ -212,11 +293,15 @@ var (
212293
//
213294
//The other parameters defined in EIP 2200 are unchanged.
214295
// see gasSStoreEIP2200(...) in core/vm/gas_table.go for more info about how EIP 2200 is specified
215-
gasSStoreEIP2929 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP2200)
296+
gasSStoreEIP2929 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP2200, params.WarmStorageReadCostEIP2929)
216297

217298
// gasSStoreEIP3529 implements gas cost for SSTORE according to EIP-3529
218299
// Replace `SSTORE_CLEARS_SCHEDULE` with `SSTORE_RESET_GAS + ACCESS_LIST_STORAGE_KEY_COST` (4,800)
219-
gasSStoreEIP3529 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP3529)
300+
gasSStoreEIP3529 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP3529, params.WarmStorageReadCostEIP2929)
301+
302+
// gasSStoreEIP7904 implements gas cost for SSTORE according to EIP-7904
303+
// Lowers `WARM_STORAGE_READ_COST`.
304+
gasSStoreEIP7904 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP3529, params.WarmStorageReadCostEIP7904)
220305
)
221306

222307
// makeSelfdestructGasFn can create the selfdestruct dynamic gas function for EIP-2929 and EIP-3529

params/protocol_params.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ const (
6969
ColdSloadCostEIP2929 = uint64(2100) // COLD_SLOAD_COST
7070
WarmStorageReadCostEIP2929 = uint64(100) // WARM_STORAGE_READ_COST
7171

72+
WarmStorageReadCostEIP7904 = uint64(5) // WARM_STORAGE_READ_COST
73+
7274
// In EIP-2200: SstoreResetGas was 5000.
7375
// In EIP-2929: SstoreResetGas was changed to '5000 - COLD_SLOAD_COST'.
7476
// In EIP-3529: SSTORE_CLEARS_SCHEDULE is defined as SSTORE_RESET_GAS + ACCESS_LIST_STORAGE_KEY_COST
@@ -119,6 +121,10 @@ const (
119121
// EXP has a dynamic portion depending on the size of the exponent
120122
ExpByteFrontier uint64 = 10 // was set to 10 in Frontier
121123
ExpByteEIP158 uint64 = 50 // was raised to 50 during Eip158 (Spurious Dragon)
124+
ExpGasEIP7904 uint64 = 2 // lowered to 2 during EIP7904
125+
ExpByteEIP7904 uint64 = 4 // was lowered to 4 during EIP7904
126+
127+
CopyGasEIP7904 uint64 = 1 // lowered
122128

123129
// Extcodecopy has a dynamic AND a static cost. This represents only the
124130
// static portion of the gas. It was changed during EIP 150 (Tangerine)

0 commit comments

Comments
 (0)