Wire ForkedChainRef to graphql and rpc_utils (#2936)

* fixes

* Wire ForkedChainRef to graphql and rpc_utils
This commit is contained in:
andri lim 2024-12-13 14:34:32 +07:00 committed by GitHub
parent a7ab984304
commit 650fec5a26
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 30 additions and 28 deletions

View File

@ -21,6 +21,7 @@ import
".."/[transaction, evm/state, config, constants], ".."/[transaction, evm/state, config, constants],
../transaction/call_evm, ../transaction/call_evm,
../core/[tx_pool, tx_pool/tx_item], ../core/[tx_pool, tx_pool/tx_item],
../core/chain/forked_chain,
../common/common, ../common/common,
web3/eth_api_types web3/eth_api_types
@ -75,6 +76,7 @@ type
chainDB: CoreDbRef chainDB: CoreDbRef
ethNode: EthereumNode ethNode: EthereumNode
txPool: TxPoolRef txPool: TxPoolRef
chain: ForkedChainRef
{.push gcsafe, raises: [].} {.push gcsafe, raises: [].}
{.pragma: apiRaises, raises: [].} {.pragma: apiRaises, raises: [].}
@ -154,28 +156,28 @@ proc getStateDB(com: CommonRef, header: Header): LedgerRef {.deprecated: "Ledge
proc getBlockByNumber(ctx: GraphqlContextRef, number: Node): RespResult = proc getBlockByNumber(ctx: GraphqlContextRef, number: Node): RespResult =
try: try:
let header = ?ctx.chainDB.getBlockHeader(toBlockNumber(number)) let header = ?ctx.chain.headerByNumber(toBlockNumber(number))
ok(headerNode(ctx, header)) ok(headerNode(ctx, header))
except ValueError as exc: except ValueError as exc:
err(exc.msg) err(exc.msg)
proc getBlockByNumber(ctx: GraphqlContextRef, number: base.BlockNumber): RespResult = proc getBlockByNumber(ctx: GraphqlContextRef, number: base.BlockNumber): RespResult =
let header = ?ctx.chainDB.getBlockHeader(number) let header = ?ctx.chain.headerByNumber(number)
ok(headerNode(ctx, header)) ok(headerNode(ctx, header))
proc getBlockByHash(ctx: GraphqlContextRef, hash: Node): RespResult = proc getBlockByHash(ctx: GraphqlContextRef, hash: Node): RespResult =
try: try:
let header = ?ctx.chainDB.getBlockHeader(toHash(hash)) let header = ?ctx.chain.headerByHash(toHash(hash))
ok(headerNode(ctx, header)) ok(headerNode(ctx, header))
except ValueError as exc: except ValueError as exc:
err(exc.msg) err(exc.msg)
proc getBlockByHash(ctx: GraphqlContextRef, hash: Hash32): RespResult = proc getBlockByHash(ctx: GraphqlContextRef, hash: Hash32): RespResult =
let header = ?ctx.chainDB.getBlockHeader(hash) let header = ?ctx.chain.headerByHash(hash)
ok(headerNode(ctx, header)) ok(headerNode(ctx, header))
proc getLatestBlock(ctx: GraphqlContextRef): RespResult = proc getLatestBlock(ctx: GraphqlContextRef): RespResult =
let header = ?ctx.chainDB.getCanonicalHead() let header = ctx.chain.latestHeader
ok(headerNode(ctx, header)) ok(headerNode(ctx, header))
proc getTxCount(ctx: GraphqlContextRef, txRoot: Hash32): RespResult = proc getTxCount(ctx: GraphqlContextRef, txRoot: Hash32): RespResult =
@ -1295,7 +1297,7 @@ proc queryLogs(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.
proc queryGasPrice(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} = proc queryGasPrice(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
let ctx = GraphqlContextRef(ud) let ctx = GraphqlContextRef(ud)
try: try:
bigIntNode(calculateMedianGasPrice(ctx.chainDB)) bigIntNode(calculateMedianGasPrice(ctx.chain))
except CatchableError as em: except CatchableError as em:
err("can't get gasPrice: " & em.msg) err("can't get gasPrice: " & em.msg)
@ -1420,23 +1422,24 @@ proc initEthApi(ctx: GraphqlContextRef) =
echo res.error echo res.error
quit(QuitFailure) quit(QuitFailure)
proc setupGraphqlContext*(com: CommonRef, proc setupGraphqlContext*(chain: ForkedChainRef,
ethNode: EthereumNode, ethNode: EthereumNode,
txPool: TxPoolRef): GraphqlContextRef = txPool: TxPoolRef): GraphqlContextRef =
let ctx = GraphqlContextRef( let ctx = GraphqlContextRef(
chainDB: com.db, chainDB: chain.com.db,
com : com, com : chain.com,
ethNode: ethNode, ethNode: ethNode,
txPool : txPool txPool : txPool,
chain : chain,
) )
graphql.init(ctx) graphql.init(ctx)
ctx.initEthApi() ctx.initEthApi()
ctx ctx
proc setupGraphqlHttpHandler*(com: CommonRef, proc setupGraphqlHttpHandler*(chain: ForkedChainRef,
ethNode: EthereumNode, ethNode: EthereumNode,
txPool: TxPoolRef): GraphqlHttpHandlerRef = txPool: TxPoolRef): GraphqlHttpHandlerRef =
let ctx = setupGraphqlContext(com, ethNode, txPool) let ctx = setupGraphqlContext(chain, ethNode, txPool)
GraphqlHttpHandlerRef.new(ctx) GraphqlHttpHandlerRef.new(ctx)
{.pop.} {.pop.}

View File

@ -148,7 +148,7 @@ proc addHttpServices(handlers: var seq[RpcHandlerProc],
# json-rpc have no reliable identification # json-rpc have no reliable identification
if conf.graphqlEnabled: if conf.graphqlEnabled:
let ctx = setupGraphqlContext(com, nimbus.ethNode, nimbus.txPool) let ctx = setupGraphqlContext(nimbus.chainRef, nimbus.ethNode, nimbus.txPool)
let server = GraphqlHttpHandlerRef.new(ctx) let server = GraphqlHttpHandlerRef.new(ctx)
handlers.addHandler(server) handlers.addHandler(server)
info "GraphQL API enabled", url = "http://" & $address info "GraphQL API enabled", url = "http://" & $address
@ -196,7 +196,7 @@ proc addServices(handlers: var seq[RpcHandlerProc],
# The order is important: graphql, ws, rpc # The order is important: graphql, ws, rpc
if conf.graphqlEnabled: if conf.graphqlEnabled:
let ctx = setupGraphqlContext(com, nimbus.ethNode, nimbus.txPool) let ctx = setupGraphqlContext(nimbus.chainRef, nimbus.ethNode, nimbus.txPool)
let server = GraphqlHttpHandlerRef.new(ctx) let server = GraphqlHttpHandlerRef.new(ctx)
handlers.addHandler(server) handlers.addHandler(server)
info "GraphQL API enabled", url = "http://" & $address info "GraphQL API enabled", url = "http://" & $address

View File

@ -13,13 +13,13 @@ import
std/[sequtils, algorithm], std/[sequtils, algorithm],
./rpc_types, ./rpc_types,
./params, ./params,
../db/core_db,
../db/ledger, ../db/ledger,
../constants, stint, ../constants, stint,
../utils/utils, ../utils/utils,
../transaction, ../transaction,
../transaction/call_evm, ../transaction/call_evm,
../core/eip4844, ../core/eip4844,
../core/chain/forked_chain,
../evm/types, ../evm/types,
../evm/state, ../evm/state,
../evm/precompiles, ../evm/precompiles,
@ -29,13 +29,11 @@ import
../common/common, ../common/common,
web3/eth_api_types web3/eth_api_types
proc calculateMedianGasPrice*(chain: CoreDbRef): GasInt {.raises: [RlpError].} = proc calculateMedianGasPrice*(chain: ForkedChainRef): GasInt =
const minGasPrice = 30_000_000_000.GasInt const minGasPrice = 30_000_000_000.GasInt
var prices = newSeqOfCap[GasInt](64) var prices = newSeqOfCap[GasInt](64)
let header = chain.getCanonicalHead().valueOr: let blk = chain.latestBlock
return minGasPrice for tx in blk.transactions:
for encodedTx in chain.getBlockTransactionData(header.txRoot):
let tx = decodeTx(encodedTx)
prices.add(tx.gasPrice) prices.add(tx.gasPrice)
if prices.len > 0: if prices.len > 0:
@ -57,9 +55,10 @@ proc calculateMedianGasPrice*(chain: CoreDbRef): GasInt {.raises: [RlpError].} =
# re-enable the "query.gasPrice" test case (remove `skip = true`). # re-enable the "query.gasPrice" test case (remove `skip = true`).
result = max(result, minGasPrice) result = max(result, minGasPrice)
proc unsignedTx*(tx: TransactionArgs, chain: CoreDbRef, defaultNonce: AccountNonce, chainId: ChainId): Transaction proc unsignedTx*(tx: TransactionArgs,
{.gcsafe, raises: [CatchableError].} = chain: ForkedChainRef,
defaultNonce: AccountNonce,
chainId: ChainId): Transaction =
var res: Transaction var res: Transaction
if tx.to.isSome: if tx.to.isSome:

View File

@ -389,7 +389,7 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
server.rpc("eth_gasPrice") do() -> Web3Quantity: server.rpc("eth_gasPrice") do() -> Web3Quantity:
## Returns an integer of the current gas price in wei. ## Returns an integer of the current gas price in wei.
w3Qty(calculateMedianGasPrice(api.com.db).uint64) w3Qty(calculateMedianGasPrice(api.chain).uint64)
server.rpc("eth_accounts") do() -> seq[eth_types.Address]: server.rpc("eth_accounts") do() -> seq[eth_types.Address]:
## Returns a list of addresses owned by client. ## Returns a list of addresses owned by client.
@ -474,7 +474,7 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
let let
accDB = api.ledgerFromTag(blockId("latest")).valueOr: accDB = api.ledgerFromTag(blockId("latest")).valueOr:
raise newException(ValueError, "Latest Block not found") raise newException(ValueError, "Latest Block not found")
tx = unsignedTx(data, api.chain.db, accDB.getNonce(address) + 1, api.com.chainId) tx = unsignedTx(data, api.chain, accDB.getNonce(address) + 1, api.com.chainId)
eip155 = api.com.isEIP155(api.chain.latestNumber) eip155 = api.com.isEIP155(api.chain.latestNumber)
signedTx = signTransaction(tx, acc.privateKey, eip155) signedTx = signTransaction(tx, acc.privateKey, eip155)
return rlp.encode(signedTx) return rlp.encode(signedTx)
@ -495,7 +495,7 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
let let
accDB = api.ledgerFromTag(blockId("latest")).valueOr: accDB = api.ledgerFromTag(blockId("latest")).valueOr:
raise newException(ValueError, "Latest Block not found") raise newException(ValueError, "Latest Block not found")
tx = unsignedTx(data, api.chain.db, accDB.getNonce(address) + 1, api.com.chainId) tx = unsignedTx(data, api.chain, accDB.getNonce(address) + 1, api.com.chainId)
eip155 = api.com.isEIP155(api.chain.latestNumber) eip155 = api.com.isEIP155(api.chain.latestNumber)
signedTx = signTransaction(tx, acc.privateKey, eip155) signedTx = signTransaction(tx, acc.privateKey, eip155)
networkPayload = networkPayload =

View File

@ -86,7 +86,7 @@ proc graphqlMain*() =
chain = setupChain() chain = setupChain()
txPool = TxPoolRef.new(chain) txPool = TxPoolRef.new(chain)
let ctx = setupGraphqlContext(chain.com, ethNode, txPool) let ctx = setupGraphqlContext(chain, ethNode, txPool)
when isMainModule: when isMainModule:
ctx.main(caseFolder, purgeSchema = false) ctx.main(caseFolder, purgeSchema = false)
else: else: