Skip to content

Commit

Permalink
Fixes for Mekong testnet: EIP-7702 gas related (#2912)
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko authored Dec 5, 2024
1 parent 4c37682 commit dc81863
Show file tree
Hide file tree
Showing 16 changed files with 263 additions and 28 deletions.
20 changes: 11 additions & 9 deletions hive_integration/nodocker/engine/engine_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,6 @@ proc maybeChainId(n: Opt[Quantity]): Opt[ChainId] =
return Opt.none(ChainId)
Opt.some(n.get.ChainId)

proc maybeInt(n: Opt[Quantity]): Opt[int] =
if n.isNone:
return Opt.none(int)
Opt.some(n.get.int)

proc toBlockHeader*(bc: BlockObject): Header =
Header(
number : distinctBase(bc.number),
Expand Down Expand Up @@ -311,7 +306,7 @@ proc toTransactions*(txs: openArray[TxOrHash]): seq[Transaction] =
type
RPCReceipt* = object
txHash*: Hash32
txIndex*: int
txIndex*: uint64
blockHash*: Hash32
blockNumber*: uint64
sender*: Address
Expand Down Expand Up @@ -341,7 +336,7 @@ type
payload*: seq[byte]
nonce*: AccountNonce
to*: Opt[Address]
txIndex*: Opt[int]
txIndex*: Opt[uint64]
value*: UInt256
v*: uint64
r*: UInt256
Expand All @@ -355,7 +350,7 @@ type
proc toRPCReceipt(rec: ReceiptObject): RPCReceipt =
RPCReceipt(
txHash: rec.transactionHash,
txIndex: rec.transactionIndex.int,
txIndex: rec.transactionIndex.uint64,
blockHash: rec.blockHash,
blockNumber: rec.blockNumber.uint64,
sender: rec.`from`,
Expand Down Expand Up @@ -387,7 +382,7 @@ proc toRPCTx(tx: eth_api.TransactionObject): RPCTx =
payload: tx.input,
nonce: tx.nonce.AccountNonce,
to: tx.to,
txIndex: maybeInt(tx.transactionIndex),
txIndex: maybeU64(tx.transactionIndex),
value: tx.value,
v: tx.v.uint64,
r: tx.r,
Expand Down Expand Up @@ -509,6 +504,13 @@ proc txReceipt*(client: RpcClient, txHash: Hash32): Result[RPCReceipt, string] =
return err("failed to get receipt: " & txHash.data.toHex)
return ok(res.toRPCReceipt)

proc getReceipt*(client: RpcClient, txHash: Hash32): Result[ReceiptObject, string] =
wrapTry:
let res = waitFor client.eth_getTransactionReceipt(txHash)
if res.isNil:
return err("failed to get receipt: " & txHash.data.toHex)
return ok(res)

proc txByHash*(client: RpcClient, txHash: Hash32): Result[RPCTx, string] =
wrapTry:
let res = waitFor client.eth_getTransactionByHash(txHash)
Expand Down
2 changes: 1 addition & 1 deletion nimbus/db/aristo/aristo_compute.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{.push raises: [].}

import
std/[strformat, math],
std/strformat,
chronicles,
eth/common,
results,
Expand Down
8 changes: 7 additions & 1 deletion nimbus/evm/computation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,14 @@ func getGasRefund*(c: Computation): GasInt =
# EIP-2183 guarantee that sum of all child gasRefund
# should never go below zero
doAssert(c.msg.depth == 0 and c.gasMeter.gasRefunded >= 0)
var gasRefunded = c.vmState.gasRefunded
if c.isSuccess:
result = GasInt c.gasMeter.gasRefunded
gasRefunded += c.gasMeter.gasRefunded

GasInt gasRefunded

func addRefund*(c: Computation, amount: int64) =
c.vmState.gasRefunded += amount

# Using `proc` as `selfDestructLen()` might be `proc` in logging mode
proc refundSelfDestruct*(c: Computation) =
Expand Down
15 changes: 1 addition & 14 deletions nimbus/evm/interpreter/op_handlers/oph_call.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import
../op_codes,
../utils/utils_numeric,
./oph_defs,
./oph_helpers,
chronicles,
eth/common,
eth/common/eth_types,
Expand Down Expand Up @@ -76,20 +77,6 @@ proc gasCallEIP2929(c: Computation, address: Address): GasInt =
# the form of a constant `gasCall`
return ColdAccountAccessCost - WarmStorageReadCost

proc delegateResolutionCost(c: Computation, address: Address): GasInt =
when evmc_enabled:
if c.host.accessAccount(address) == EVMC_ACCESS_COLD:
ColdAccountAccessCost
else:
WarmStorageReadCost
else:
c.vmState.mutateStateDB:
if not db.inAccessList(address):
db.accessList(address)
return ColdAccountAccessCost
else:
return WarmStorageReadCost

proc updateStackAndParams(q: var LocalParams; c: Computation) =
c.stack.lsTop(0)

Expand Down
16 changes: 15 additions & 1 deletion nimbus/evm/interpreter/op_handlers/oph_helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,28 @@ func checkInStaticContext*(c: Computation): EvmResultVoid =

ok()

proc delegateResolutionCost*(c: Computation, address: Address): GasInt =
when defined(evmc_enabled):
if c.host.accessAccount(address) == EVMC_ACCESS_COLD:
ColdAccountAccessCost
else:
WarmStorageReadCost
else:
c.vmState.mutateStateDB:
if not db.inAccessList(address):
db.accessList(address)
return ColdAccountAccessCost
else:
return WarmStorageReadCost

proc gasEip7702CodeCheck*(c: Computation; address: Address): GasInt =
let code = when defined(evmc_enabled):
CodeBytesRef.init(c.host.copyCode(address))
else:
c.vmState.readOnlyStateDB.getCode(address)
let delegateTo = parseDelegationAddress(code).valueOr:
return 0
c.gasEip2929AccountCheck(delegateTo)
c.delegateResolutionCost(delegateTo)

# ------------------------------------------------------------------------------
# End
Expand Down
1 change: 1 addition & 0 deletions nimbus/evm/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type
gasCosts* : GasCosts
blobGasUsed* : uint64
allLogs* : seq[Log] # EIP-6110
gasRefunded* : int64 # Global gasRefunded counter

Computation* = ref object
# The execution computation
Expand Down
9 changes: 7 additions & 2 deletions nimbus/transaction/call_common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ proc setupHost(call: CallParams, keepStack: bool): TransactionHost =
# All other defaults in `TransactionHost` are fine.
)

# reset global gasRefund counter each time
# EVM called for a new transaction
vmState.gasRefunded = 0
let gasRefund = if call.sysCall: 0
else: preExecComputation(vmState, call)
let isPrecompile =
Expand Down Expand Up @@ -213,7 +216,7 @@ proc setupHost(call: CallParams, keepStack: bool): TransactionHost =
host.computation = newComputation(
vmState, call.sysCall, cMsg, isPrecompile = isPrecompile, keepStack = keepStack)

host.computation.gasMeter.refundGas(gasRefund)
host.computation.addRefund(gasRefund)
vmState.captureStart(host.computation, call.sender, call.to,
call.isCreate, call.input,
call.gasLimit, call.value)
Expand Down Expand Up @@ -278,7 +281,9 @@ proc calculateAndPossiblyRefundGas(host: TransactionHost, call: CallParams): Gas
# Calculated gas used, taking into account refund rules.
if call.noRefund:
result = c.gasMeter.gasRemaining
elif not c.shouldBurnGas:
else:
if c.shouldBurnGas:
c.gasMeter.gasRemaining = 0
let maxRefund = (call.gasLimit - c.gasMeter.gasRemaining) div MaxRefundQuotient
let refund = min(c.getGasRefund(), maxRefund)
c.gasMeter.returnGas(refund)
Expand Down
18 changes: 18 additions & 0 deletions tools/t8n/t8n_test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,24 @@ const
output: T8nOutput(alloc: true, result: true),
expOut: "exp.json",
),
TestSpec(
name: "Mekong test, EIP-7702 gasRefunded",
base: "testdata/00-526",
input: t8nInput(
"alloc.json", "txs.rlp", "env.json", "Prague", "", "7078815900"
),
output: T8nOutput(alloc: true, result: true),
expOut: "exp.json",
),
TestSpec(
name: "Mekong test, EIP-7702 gasRefunded when computation fails",
base: "testdata/00-527",
input: t8nInput(
"alloc.json", "txs.rlp", "env.json", "Prague", "", "7078815900"
),
output: T8nOutput(alloc: true, result: true),
expOut: "exp.json",
),
]

proc main() =
Expand Down
30 changes: 30 additions & 0 deletions tools/t8n/testdata/00-526/alloc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"0x8a0a19589531694250d570040a0c4b74576919b8": {
"nonce": "0x00",
"balance": "0x0de0b6b3a7640000",
"code": "0x600060006000600060007310000000000000000000000000000000000000015af1600155600060006000600060007310000000000000000000000000000000000000025af16002553d600060003e600051600355",
"storage": {
"0x01": "0x0100",
"0x02": "0x0100",
"0x03": "0x0100"
}
},
"0x000000000000000000000000000000000000aaaa": {
"nonce": "0x00",
"balance": "0x4563918244f40000",
"code": "0x58808080600173703c4b2bd70c169f5717101caee543299fc946c75af100",
"storage": {}
},
"0x000000000000000000000000000000000000bbbb": {
"nonce": "0x00",
"balance": "0x29a2241af62c0000",
"code": "0x6042805500",
"storage": {}
},
"0xf4b6e5999189244f8278099cb4c365b7867d1923": {
"nonce": "0x0C",
"balance": "0x6124fee993bc0000",
"code": "0x",
"storage": {}
}
}
14 changes: 14 additions & 0 deletions tools/t8n/testdata/00-526/env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"currentCoinbase": "0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"currentGasLimit": "71794957647893862",
"currentNumber": "1",
"currentTimestamp": "1000",
"currentRandom": "0",
"currentDifficulty": "0",
"blockHashes": {},
"ommers": [],
"currentBaseFee": "7",
"parentUncleHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"withdrawals": [],
"parentBeaconBlockRoot": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
60 changes: 60 additions & 0 deletions tools/t8n/testdata/00-526/exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"alloc": {
"0x000000000000000000000000000000000000aaaa": {
"code": "0x58808080600173703c4b2bd70c169f5717101caee543299fc946c75af100",
"balance": "0x4563918244f40000"
},
"0x000000000000000000000000000000000000bbbb": {
"code": "0x6042805500",
"balance": "0x29a2241af62c0000"
},
"0x8a0a19589531694250d570040a0c4b74576919b8": {
"code": "0x600060006000600060007310000000000000000000000000000000000000015af1600155600060006000600060007310000000000000000000000000000000000000025af16002553d600060003e600051600355",
"balance": "0xde0b6b3a7640000",
"storage": {
"0x0000000000000000000000000000000000000000000000000000000000000003": "0x0000000000000000000000000000000000000000000000000000000000000100",
"0x0000000000000000000000000000000000000000000000000000000000000001": "0x0000000000000000000000000000000000000000000000000000000000000100",
"0x0000000000000000000000000000000000000000000000000000000000000002": "0x0000000000000000000000000000000000000000000000000000000000000100"
}
},
"0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba": {
"balance": "0x880dd8b9af0"
},
"0xf4b6e5999189244f8278099cb4c365b7867d1923": {
"balance": "0x6124f668b62c26ea",
"nonce": "0xe"
}
},
"result": {
"stateRoot": "0xa18cd473605cb85ab32bccdde99db947d4d1d04ae26e618e5d07e64a7058cd24",
"txRoot": "0x8099d6b28dffe977a3d69caeeed8a73ed98fef4055804802f4605c94299f5af4",
"receiptsRoot": "0xa4f9f4360a7bb0ddb8a1d666616ea2e0cdba3df8bc1337b4a94302e7afff5e17",
"logsHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"receipts": [
{
"root": "0x",
"status": "0x1",
"cumulativeGasUsed": "0x9b2a",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"logs": null,
"transactionHash": "0xb147d4f82578e0565a0560150676c349a4a2b7b747575932c1f6dbbb4809e02c",
"contractAddress": "0x0000000000000000000000000000000000000000",
"gasUsed": "0x9b2a",
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"transactionIndex": "0x0",
"type": "0x4"
}
],
"currentDifficulty": null,
"gasUsed": "0x9b2a",
"currentBaseFee": "0x7",
"withdrawalsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"requestsHash": "0x6036c41849da9c076ed79654d434017387a88fb833c2856b32e18218b3341c5f",
"requests": [
"0x",
"0x",
"0x"
]
}
}
1 change: 1 addition & 0 deletions tools/t8n/testdata/00-526/txs.rlp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"0xf90320b9031d04f903198501a5ee289c0c840e078998840e0789a08301c44e94f4b6e5999189244f8278099cb4c365b7867d192380b90244a6d0ad6100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000060000000000000000000000000af1a36b3a0238d614aaafae245d62ba879fd86d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044a9059cbb000000000000000000000000800f2c1f4c8be1aad97abb91b3be5aa910acb6af000000000000000000000000000000000000000000000000002386f26fc10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000a3608cd6013a39d2c3a256dc9010d4526a4b7b9600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044a9059cbb000000000000000000000000800f2c1f4c8be1aad97abb91b3be5aa910acb6af000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000c0f861f85f8501a5ee289c9400000000000000000000000000000000000000000d80a0345e118cdbba2285f44f05a1096c764f3db67c00346c3ef9526f9c4d49575a73a057bca4ea7460f56c545fdfe52fd155b103640ee2ae206e755e3b050c0b80535380a078298cbd7d65f9a85b9987637edcbc136327252a04ab95c87f8e085186c1eadca053ba7a8aea1eafbe5df2a5e3ea2b6172052953125c795a89b7be4cc0731fbd55"
24 changes: 24 additions & 0 deletions tools/t8n/testdata/00-527/alloc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"0x8a0a19589531694250d570040a0c4b74576919b8": {
"nonce": "0x00",
"balance": "0x0de0b6b3a7640000",
"code": "0x600060006000600060007310000000000000000000000000000000000000015af1600155600060006000600060007310000000000000000000000000000000000000025af16002553d600060003e600051600355",
"storage": {
"0x01": "0x0100",
"0x02": "0x0100",
"0x03": "0x0100"
}
},
"0x0ad3da17498ca898902e106a04cb08d9c27c09ed": {
"nonce": "0x01",
"balance": "0x00",
"code": "0x608060405234801561001057600080fd5b50600436106100415760003560e01c8063329b9d5114610046578063affed0e01461005b578063f82722af14610076575b600080fd5b610059610054366004610113565b610089565b005b61006460025481565b60405190815260200160405180910390f35b61005961008436600461014b565b6100a0565b33301461009557600080fd5b600091909155600155565b6000856001600160a01b031684866040516100bb919061023e565b60006040518083038185875af1925050503d80600081146100f8576040519150601f19603f3d011682016040523d82523d6000602084013e6100fd565b606091505b505090508061010b57600080fd5b505050505050565b6000806040838503121561012657600080fd5b50508035926020909101359150565b634e487b7160e01b600052604160045260246000fd5b600080600080600060a0868803121561016357600080fd5b85356001600160a01b038116811461017a57600080fd5b9450602086013567ffffffffffffffff81111561019657600080fd5b8601601f810188136101a757600080fd5b803567ffffffffffffffff8111156101c1576101c1610135565b604051601f8201601f19908116603f0116810167ffffffffffffffff811182821017156101f0576101f0610135565b6040528181528282016020018a101561020857600080fd5b81602084016020830137600091810160200191909152959895975050505060408401359360608101359360809091013592509050565b6000825160005b8181101561025f5760208186018101518583015201610245565b50600092019182525091905056fea2646970667358221220575bc0fbb50b5ef78375a62149f15a5bf43dc0ffcebf45c4e2e641d86c9fb2a664736f6c634300081c0033",
"storage": {}
},
"0xb90d45eb2f6495ff3d744e07161647e0a5eb5621": {
"nonce": "0x02",
"balance": "0x6124fee993bc0000",
"code": "0x",
"storage": {}
}
}
14 changes: 14 additions & 0 deletions tools/t8n/testdata/00-527/env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"currentCoinbase": "0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"currentGasLimit": "71794957647893862",
"currentNumber": "1",
"currentTimestamp": "1000",
"currentRandom": "0",
"currentDifficulty": "0",
"blockHashes": {},
"ommers": [],
"currentBaseFee": "7",
"parentUncleHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"withdrawals": [],
"parentBeaconBlockRoot": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
Loading

0 comments on commit dc81863

Please sign in to comment.