Skip to content

Commit 3e4cb3d

Browse files
committed
core: fixups for devnet-2
1 parent 0c42202 commit 3e4cb3d

File tree

5 files changed

+46
-24
lines changed

5 files changed

+46
-24
lines changed

cmd/evm/internal/t8ntool/transaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func Transaction(ctx *cli.Context) error {
180180
r.Error = errors.New("gas * maxFeePerGas exceeds 256 bits")
181181
}
182182
// Check whether the init code size has been exceeded.
183-
if chainConfig.IsShanghai(new(big.Int), 0) && tx.To() == nil && len(tx.Data()) > params.MaxInitCodeSize {
183+
if chainConfig.IsShanghai(new(big.Int), 0) && tx.To() == nil && len(tx.Data()) > params.MaxInitCodeSizeEIP3860 {
184184
r.Error = errors.New("max initcode size exceeded")
185185
}
186186
results = append(results, r)

core/state/statedb.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,10 @@ func (s *StateDB) Prepare(rules params.Rules, sender, coinbase common.Address, d
13611361
al.AddAddress(sender)
13621362
if dst != nil {
13631363
al.AddAddress(*dst)
1364+
// TODO: add for devnet-3
1365+
// if rules.IsOsaka {
1366+
// al.AddAddressCode(*dst)
1367+
// }
13641368
// If it's a create-tx, the destination will be added inside evm.create
13651369
}
13661370
for _, addr := range precompiles {

core/txpool/validation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types
8787
return fmt.Errorf("%w: type %d rejected, pool not yet in Prague", core.ErrTxTypeNotSupported, tx.Type())
8888
}
8989
// Check whether the init code size has been exceeded
90-
if rules.IsShanghai && tx.To() == nil && len(tx.Data()) > params.MaxInitCodeSize {
91-
return fmt.Errorf("%w: code size %v, limit %v", core.ErrMaxInitCodeSizeExceeded, len(tx.Data()), params.MaxInitCodeSize)
90+
if rules.IsShanghai && tx.To() == nil && len(tx.Data()) > params.MaxInitCodeSizeEIP7907 {
91+
return fmt.Errorf("%w: code size %v, limit %v", core.ErrMaxInitCodeSizeExceeded, len(tx.Data()), params.MaxInitCodeSizeEIP7907)
9292
}
9393
// Transactions can't be negative. This may never happen using RLP decoded
9494
// transactions but may occur for transactions created using the RPC.

core/vm/operations_acl.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package vm
1818

1919
import (
2020
"errors"
21+
"fmt"
2122

2223
"github.com/ethereum/go-ethereum/common"
2324
"github.com/ethereum/go-ethereum/common/math"
@@ -318,12 +319,25 @@ var (
318319
gasCallCodeEIP7907 = makeCallVariantGasCallEIP7907(gasCallCode)
319320
)
320321

321-
func getColdCodeAccessGasCost(evm *EVM, addr common.Address) uint64 {
322-
codeSize := evm.StateDB.GetCodeSize(addr)
323-
if codeSize <= params.MaxCodeSizeEIP7907 {
322+
// Rounds n up to the nearest multiple of 32.
323+
func ceil32(n int) uint64 {
324+
r := n % 32
325+
if r == 0 {
326+
return uint64(n)
327+
} else {
328+
return uint64(n + 32 - r)
329+
}
330+
}
331+
332+
func calcColdCodeAccessGasCost(evm *EVM, addr common.Address) uint64 {
333+
size := evm.StateDB.GetCodeSize(addr)
334+
// Only charge additional access cost for contracts larger than old limit.
335+
if size <= params.MaxCodeSizeEIP170 {
324336
return 0
325337
}
326-
return (uint64(codeSize) - params.MaxCodeSizeEIP7907) * 2 / 32
338+
excess := ceil32(size - params.MaxCodeSizeEIP170)
339+
fmt.Println("excess", excess, "cost", (excess*params.CodeReadPerWordGasEIP7907)/32)
340+
return (excess * params.CodeReadPerWordGasEIP7907) / 32
327341
}
328342

329343
func makeCallVariantGasCallEIP7907(oldCalculator gasFunc) gasFunc {
@@ -346,16 +360,20 @@ func makeCallVariantGasCallEIP7907(oldCalculator gasFunc) gasFunc {
346360
}
347361
total += coldCost
348362
}
363+
349364
// Check code presence in the access list
350365
if !evm.StateDB.AddressCodeInAccessList(addr) {
351-
cost := getColdCodeAccessGasCost(evm, addr)
366+
cost := calcColdCodeAccessGasCost(evm, addr)
352367
evm.StateDB.AddAddressCodeToAccessList(addr)
353368
if !contract.UseGas(cost, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) {
354369
return 0, ErrOutOfGas
355370
}
356371
total += cost
357372
}
358373

374+
// TODO: reading code here would defeat the purpose of separate charging, so
375+
// we should first see if the code size is 23 bytes before parsing.
376+
359377
// Check if code is a delegation and if so, charge for resolution.
360378
if target, ok := types.ParseDelegation(evm.StateDB.GetCode(addr)); ok {
361379
var cost uint64
@@ -370,7 +388,7 @@ func makeCallVariantGasCallEIP7907(oldCalculator gasFunc) gasFunc {
370388
}
371389
total += cost
372390
if !evm.StateDB.AddressCodeInAccessList(target) {
373-
cost = getColdCodeAccessGasCost(evm, target)
391+
cost = calcColdCodeAccessGasCost(evm, target)
374392
evm.StateDB.AddAddressCodeToAccessList(target)
375393
if !contract.UseGas(cost, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) {
376394
return 0, ErrOutOfGas
@@ -410,27 +428,25 @@ func gasExtCodeCopyEIP7907(evm *EVM, contract *Contract, stack *Stack, mem *Memo
410428
return 0, err
411429
}
412430
addr := common.Address(stack.peek().Bytes20())
413-
var total uint64
414431
// Check slot presence in the access list
415432
if !evm.StateDB.AddressInAccessList(addr) {
416433
evm.StateDB.AddAddressToAccessList(addr)
434+
var overflow bool
417435
// We charge (cold-warm), since 'warm' is already charged as constantGas
418-
419-
if !contract.UseGas(gas, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) {
420-
return 0, ErrOutOfGas
436+
if gas, overflow = math.SafeAdd(gas, params.ColdAccountAccessCostEIP2929-params.WarmStorageReadCostEIP2929); overflow {
437+
return 0, ErrGasUintOverflow
421438
}
422-
total += gas
423439
}
424440

425441
// Check address code presence in the access list
426442
if !evm.StateDB.AddressCodeInAccessList(addr) {
427-
cost := getColdCodeAccessGasCost(evm, addr)
443+
cost := calcColdCodeAccessGasCost(evm, addr)
428444
evm.StateDB.AddAddressCodeToAccessList(addr)
429-
if !contract.UseGas(cost, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) {
430-
return 0, ErrOutOfGas
445+
var overflow bool
446+
// We charge (cold-warm), since 'warm' is already charged as constantGas
447+
if gas, overflow = math.SafeAdd(gas, cost); overflow {
448+
return 0, ErrGasUintOverflow
431449
}
432-
total += cost
433450
}
434-
435-
return total, nil
451+
return gas, nil
436452
}

params/protocol_params.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,12 @@ const (
132132
DefaultElasticityMultiplier = 2 // Bounds the maximum gas limit an EIP-1559 block may have.
133133
InitialBaseFee = 1000000000 // Initial base fee for EIP-1559 blocks.
134134

135-
MaxCodeSizeEIP170 = 24576 // Maximum bytecode to permit for a contract
136-
MaxCodeSizeEIP7907 = 268435456 // Maximum bytecode permitted per contract after EIP-7907
137-
MaxInitCodeSizeEIP3860 = 2 * MaxCodeSizeEIP170 // Maximum initcode to permit in a creation transaction and create instructions
138-
MaxInitCodeSizeEIP7907 = 2 * MaxCodeSizeEIP7907 // Maximum initcode to permit in a creation transaction and create instructions
135+
MaxCodeSizeEIP170 = 24576 // Maximum bytecode to permit for a contract
136+
MaxInitCodeSizeEIP3860 = 2 * MaxCodeSizeEIP170 // Maximum initcode to permit in a creation transaction and create instructions
137+
138+
MaxCodeSizeEIP7907 = 262144 // Maximum bytecode permitted per contract after EIP-7907
139+
MaxInitCodeSizeEIP7907 = 2 * MaxCodeSizeEIP7907 // Maximum initcode to permit in a creation transaction and create instructions
140+
CodeReadPerWordGasEIP7907 = 2 // Cost per word to read code from disk.
139141

140142
// Precompiled contract gas prices
141143

0 commit comments

Comments
 (0)