Skip to content

Commit 020bc51

Browse files
authored
Fluffy: Implement debug_callByStateRoot RPC endpoint (#3186)
1 parent d98a602 commit 020bc51

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

fluffy/rpc/rpc_calls/rpc_debug_calls.nim

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# fluffy
2-
# Copyright (c) 2021-2024 Status Research & Development GmbH
2+
# Copyright (c) 2021-2025 Status Research & Development GmbH
33
# Licensed and distributed under either of
44
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
55
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@@ -19,3 +19,10 @@ createRpcSigsFromNim(RpcClient):
1919
): FixedBytes[32]
2020

2121
proc debug_getCodeByStateRoot(data: Address, stateRoot: Hash32): seq[byte]
22+
proc debug_getProofByStateRoot(
23+
address: Address, slots: seq[UInt256], stateRoot: Hash32
24+
): ProofResponse
25+
26+
proc debug_callByStateRoot(
27+
tx: TransactionArgs, stateRoot: Hash32, blockId: Opt[BlockIdentifier]
28+
): seq[byte]

fluffy/rpc/rpc_calls/rpc_eth_calls.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,5 @@ createRpcSigsFromNim(RpcClient):
4747
proc eth_getProof(
4848
address: Address, slots: seq[UInt256], blockId: BlockIdentifier
4949
): ProofResponse
50+
51+
proc eth_call(tx: TransactionArgs, blockId: BlockIdentifier): seq[byte]

fluffy/rpc/rpc_debug_api.nim

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,27 @@ import
1111
json_rpc/rpcserver,
1212
chronicles,
1313
web3/[eth_api_types, conversions],
14-
../network/state/state_endpoints
14+
../network/state/state_endpoints,
15+
../evm/[async_evm, async_evm_portal_backend]
1516

1617
template getOrRaise(stateNetwork: Opt[StateNetwork]): StateNetwork =
1718
let sn = stateNetwork.valueOr:
1819
raise newException(ValueError, "state sub-network not enabled")
1920
sn
2021

22+
template getOrRaise(asyncEvm: Opt[AsyncEvm]): AsyncEvm =
23+
let evm = asyncEvm.valueOr:
24+
raise
25+
newException(ValueError, "portal evm requires state sub-network to be enabled")
26+
evm
27+
2128
proc installDebugApiHandlers*(rpcServer: RpcServer, stateNetwork: Opt[StateNetwork]) =
29+
let asyncEvm =
30+
if stateNetwork.isSome():
31+
Opt.some(AsyncEvm.init(stateNetwork.get().toAsyncEvmStateBackend()))
32+
else:
33+
Opt.none(AsyncEvm)
34+
2235
rpcServer.rpc("debug_getBalanceByStateRoot") do(
2336
address: Address, stateRoot: Hash32
2437
) -> UInt256:
@@ -120,3 +133,37 @@ proc installDebugApiHandlers*(rpcServer: RpcServer, stateNetwork: Opt[StateNetwo
120133
storageHash: proofs.account.storageRoot,
121134
storageProof: storageProof,
122135
)
136+
137+
rpcServer.rpc("debug_callByStateRoot") do(
138+
tx: TransactionArgs,
139+
stateRoot: Hash32,
140+
quantityTag: Opt[RtBlockIdentifier],
141+
optimisticStateFetch: Opt[bool]
142+
) -> seq[byte]:
143+
# TODO: add documentation
144+
145+
# This endpoint can be used to test eth_call without requiring the history network
146+
# to be enabled. This is useful for scenarios where we don't yet have the block headers
147+
# seeded into the history network.
148+
149+
if tx.to.isNone():
150+
raise newException(ValueError, "to address is required")
151+
152+
let quantityTag = quantityTag.valueOr:
153+
blockId(0.uint64)
154+
if quantityTag.kind == bidAlias:
155+
raise newException(ValueError, "tag not yet implemented")
156+
157+
let
158+
evm = asyncEvm.getOrRaise()
159+
header = Header(stateRoot: stateRoot, number: quantityTag.number.uint64)
160+
optimisticStateFetch = optimisticStateFetch.valueOr:
161+
true
162+
163+
let callResult = (await evm.call(header, tx, optimisticStateFetch)).valueOr:
164+
raise newException(ValueError, error)
165+
166+
if callResult.error.len() > 0:
167+
raise newException(ValueError, callResult.error)
168+
169+
callResult.output

fluffy/rpc/rpc_eth_api.nim

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -448,17 +448,10 @@ proc installEthApiHandlers*(
448448
evm = asyncEvm.getOrRaise()
449449
header = (await hn.getVerifiedBlockHeader(quantityTag.number.uint64)).valueOr:
450450
raise newException(ValueError, "Unable to get block header")
451+
optimisticStateFetch = optimisticStateFetch.valueOr:
452+
true
451453
452-
let callResult = (
453-
await evm.call(
454-
header,
455-
tx,
456-
if optimisticStateFetch.isNone():
457-
true
458-
else:
459-
optimisticStateFetch.get(),
460-
)
461-
).valueOr:
454+
let callResult = (await evm.call(header, tx, optimisticStateFetch)).valueOr:
462455
raise newException(ValueError, error)
463456
464457
if callResult.error.len() > 0:

0 commit comments

Comments
 (0)