2024-09-25 03:28:45 +00:00
|
|
|
# Fluffy
|
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
json_rpc/rpcserver,
|
|
|
|
chronicles,
|
2024-10-04 21:21:26 +00:00
|
|
|
web3/[eth_api_types, conversions],
|
2024-09-25 03:28:45 +00:00
|
|
|
../network/state/state_endpoints
|
|
|
|
|
2024-09-25 14:44:46 +00:00
|
|
|
template getOrRaise(stateNetwork: Opt[StateNetwork]): StateNetwork =
|
|
|
|
let sn = stateNetwork.valueOr:
|
|
|
|
raise newException(ValueError, "state sub-network not enabled")
|
|
|
|
sn
|
|
|
|
|
2024-09-25 03:28:45 +00:00
|
|
|
proc installDebugApiHandlers*(rpcServer: RpcServer, stateNetwork: Opt[StateNetwork]) =
|
|
|
|
rpcServer.rpc("debug_getBalanceByStateRoot") do(
|
2024-10-04 21:21:26 +00:00
|
|
|
address: Address, stateRoot: Hash32
|
2024-09-25 03:28:45 +00:00
|
|
|
) -> UInt256:
|
|
|
|
## Returns the balance of the account of given address.
|
|
|
|
##
|
2024-10-04 21:21:26 +00:00
|
|
|
## address: address to check for balance.
|
2024-09-25 03:28:45 +00:00
|
|
|
## stateRoot: the state root used to search the state trie.
|
|
|
|
## Returns integer of the current balance in wei.
|
|
|
|
|
2024-09-25 14:44:46 +00:00
|
|
|
let
|
|
|
|
sn = stateNetwork.getOrRaise()
|
2024-10-04 21:21:26 +00:00
|
|
|
balance = (await sn.getBalanceByStateRoot(stateRoot, address)).valueOr:
|
2024-09-25 14:44:46 +00:00
|
|
|
raise newException(ValueError, "Unable to get balance")
|
2024-09-25 03:28:45 +00:00
|
|
|
|
|
|
|
return balance
|
|
|
|
|
|
|
|
rpcServer.rpc("debug_getTransactionCountByStateRoot") do(
|
2024-10-04 21:21:26 +00:00
|
|
|
address: Address, stateRoot: Hash32
|
2024-09-25 03:28:45 +00:00
|
|
|
) -> Quantity:
|
|
|
|
## Returns the number of transactions sent from an address.
|
|
|
|
##
|
2024-10-04 21:21:26 +00:00
|
|
|
## address: address.
|
2024-09-25 03:28:45 +00:00
|
|
|
## stateRoot: the state root used to search the state trie.
|
|
|
|
## Returns integer of the number of transactions send from this address.
|
|
|
|
|
2024-09-25 14:44:46 +00:00
|
|
|
let
|
|
|
|
sn = stateNetwork.getOrRaise()
|
2024-10-04 21:21:26 +00:00
|
|
|
nonce = (await sn.getTransactionCountByStateRoot(stateRoot, address)).valueOr:
|
2024-09-25 14:44:46 +00:00
|
|
|
raise newException(ValueError, "Unable to get transaction count")
|
2024-09-25 03:28:45 +00:00
|
|
|
|
|
|
|
return nonce.Quantity
|
|
|
|
|
|
|
|
rpcServer.rpc("debug_getStorageAtByStateRoot") do(
|
2024-10-04 21:21:26 +00:00
|
|
|
address: Address, slot: UInt256, stateRoot: Hash32
|
2024-09-25 03:28:45 +00:00
|
|
|
) -> FixedBytes[32]:
|
|
|
|
## Returns the value from a storage position at a given address.
|
|
|
|
##
|
2024-10-04 21:21:26 +00:00
|
|
|
## address: address of the storage.
|
2024-09-25 03:28:45 +00:00
|
|
|
## slot: integer of the position in the storage.
|
|
|
|
## stateRoot: the state root used to search the state trie.
|
|
|
|
## Returns: the value at this storage position.
|
|
|
|
|
2024-09-25 14:44:46 +00:00
|
|
|
let
|
|
|
|
sn = stateNetwork.getOrRaise()
|
2024-10-04 21:21:26 +00:00
|
|
|
slotValue = (await sn.getStorageAtByStateRoot(stateRoot, address, slot)).valueOr:
|
2024-09-25 14:44:46 +00:00
|
|
|
raise newException(ValueError, "Unable to get storage slot")
|
2024-09-25 03:28:45 +00:00
|
|
|
|
|
|
|
return FixedBytes[32](slotValue.toBytesBE())
|
|
|
|
|
|
|
|
rpcServer.rpc("debug_getCodeByStateRoot") do(
|
2024-10-04 21:21:26 +00:00
|
|
|
address: Address, stateRoot: Hash32
|
2024-09-25 03:28:45 +00:00
|
|
|
) -> seq[byte]:
|
|
|
|
## Returns code at a given address.
|
|
|
|
##
|
2024-10-04 21:21:26 +00:00
|
|
|
## address: address
|
2024-09-25 03:28:45 +00:00
|
|
|
## stateRoot: the state root used to search the state trie.
|
|
|
|
## Returns the code from the given address.
|
|
|
|
|
2024-09-25 14:44:46 +00:00
|
|
|
let
|
|
|
|
sn = stateNetwork.getOrRaise()
|
2024-10-04 21:21:26 +00:00
|
|
|
bytecode = (await sn.getCodeByStateRoot(stateRoot, address)).valueOr:
|
2024-09-25 14:44:46 +00:00
|
|
|
raise newException(ValueError, "Unable to get code")
|
2024-09-25 03:28:45 +00:00
|
|
|
|
|
|
|
return bytecode.asSeq()
|
|
|
|
|
|
|
|
rpcServer.rpc("debug_getProofByStateRoot") do(
|
2024-10-04 21:21:26 +00:00
|
|
|
address: Address, slots: seq[UInt256], stateRoot: Hash32
|
2024-09-25 03:28:45 +00:00
|
|
|
) -> ProofResponse:
|
|
|
|
## Returns information about an account and storage slots along with account
|
|
|
|
## and storage proofs which prove the existence of the values in the state.
|
|
|
|
##
|
2024-10-04 21:21:26 +00:00
|
|
|
## address: address of the account.
|
2024-09-25 03:28:45 +00:00
|
|
|
## slots: integers of the positions in the storage to return.
|
|
|
|
## stateRoot: the state root used to search the state trie.
|
|
|
|
## Returns: the proof response containing the account, account proof and storage proof
|
|
|
|
|
2024-09-25 14:44:46 +00:00
|
|
|
let
|
|
|
|
sn = stateNetwork.getOrRaise()
|
2024-10-04 21:21:26 +00:00
|
|
|
proofs = (await sn.getProofsByStateRoot(stateRoot, address, slots)).valueOr:
|
2024-09-25 14:44:46 +00:00
|
|
|
raise newException(ValueError, "Unable to get proofs")
|
2024-09-25 03:28:45 +00:00
|
|
|
|
|
|
|
var storageProof = newSeqOfCap[StorageProof](slots.len)
|
|
|
|
for i, slot in slots:
|
|
|
|
let (slotKey, slotValue) = proofs.slots[i]
|
|
|
|
storageProof.add(
|
|
|
|
StorageProof(
|
|
|
|
key: slotKey,
|
|
|
|
value: slotValue,
|
|
|
|
proof: seq[RlpEncodedBytes](proofs.slotProofs[i]),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return ProofResponse(
|
2024-10-04 21:21:26 +00:00
|
|
|
address: address,
|
2024-09-25 03:28:45 +00:00
|
|
|
accountProof: seq[RlpEncodedBytes](proofs.accountProof),
|
|
|
|
balance: proofs.account.balance,
|
2024-10-04 21:21:26 +00:00
|
|
|
nonce: Quantity(proofs.account.nonce),
|
|
|
|
codeHash: proofs.account.codeHash,
|
|
|
|
storageHash: proofs.account.storageRoot,
|
2024-09-25 03:28:45 +00:00
|
|
|
storageProof: storageProof,
|
|
|
|
)
|