Skip to content

Commit 650fec5

Browse files
authored
Wire ForkedChainRef to graphql and rpc_utils (#2936)
* fixes * Wire ForkedChainRef to graphql and rpc_utils
1 parent a7ab984 commit 650fec5

File tree

5 files changed

+30
-28
lines changed

5 files changed

+30
-28
lines changed

nimbus/graphql/ethapi.nim

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import
2121
".."/[transaction, evm/state, config, constants],
2222
../transaction/call_evm,
2323
../core/[tx_pool, tx_pool/tx_item],
24+
../core/chain/forked_chain,
2425
../common/common,
2526
web3/eth_api_types
2627

@@ -75,6 +76,7 @@ type
7576
chainDB: CoreDbRef
7677
ethNode: EthereumNode
7778
txPool: TxPoolRef
79+
chain: ForkedChainRef
7880

7981
{.push gcsafe, raises: [].}
8082
{.pragma: apiRaises, raises: [].}
@@ -154,28 +156,28 @@ proc getStateDB(com: CommonRef, header: Header): LedgerRef {.deprecated: "Ledge
154156

155157
proc getBlockByNumber(ctx: GraphqlContextRef, number: Node): RespResult =
156158
try:
157-
let header = ?ctx.chainDB.getBlockHeader(toBlockNumber(number))
159+
let header = ?ctx.chain.headerByNumber(toBlockNumber(number))
158160
ok(headerNode(ctx, header))
159161
except ValueError as exc:
160162
err(exc.msg)
161163

162164
proc getBlockByNumber(ctx: GraphqlContextRef, number: base.BlockNumber): RespResult =
163-
let header = ?ctx.chainDB.getBlockHeader(number)
165+
let header = ?ctx.chain.headerByNumber(number)
164166
ok(headerNode(ctx, header))
165167

166168
proc getBlockByHash(ctx: GraphqlContextRef, hash: Node): RespResult =
167169
try:
168-
let header = ?ctx.chainDB.getBlockHeader(toHash(hash))
170+
let header = ?ctx.chain.headerByHash(toHash(hash))
169171
ok(headerNode(ctx, header))
170172
except ValueError as exc:
171173
err(exc.msg)
172174

173175
proc getBlockByHash(ctx: GraphqlContextRef, hash: Hash32): RespResult =
174-
let header = ?ctx.chainDB.getBlockHeader(hash)
176+
let header = ?ctx.chain.headerByHash(hash)
175177
ok(headerNode(ctx, header))
176178

177179
proc getLatestBlock(ctx: GraphqlContextRef): RespResult =
178-
let header = ?ctx.chainDB.getCanonicalHead()
180+
let header = ctx.chain.latestHeader
179181
ok(headerNode(ctx, header))
180182

181183
proc getTxCount(ctx: GraphqlContextRef, txRoot: Hash32): RespResult =
@@ -1295,7 +1297,7 @@ proc queryLogs(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.
12951297
proc queryGasPrice(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
12961298
let ctx = GraphqlContextRef(ud)
12971299
try:
1298-
bigIntNode(calculateMedianGasPrice(ctx.chainDB))
1300+
bigIntNode(calculateMedianGasPrice(ctx.chain))
12991301
except CatchableError as em:
13001302
err("can't get gasPrice: " & em.msg)
13011303

@@ -1420,23 +1422,24 @@ proc initEthApi(ctx: GraphqlContextRef) =
14201422
echo res.error
14211423
quit(QuitFailure)
14221424

1423-
proc setupGraphqlContext*(com: CommonRef,
1425+
proc setupGraphqlContext*(chain: ForkedChainRef,
14241426
ethNode: EthereumNode,
14251427
txPool: TxPoolRef): GraphqlContextRef =
14261428
let ctx = GraphqlContextRef(
1427-
chainDB: com.db,
1428-
com : com,
1429+
chainDB: chain.com.db,
1430+
com : chain.com,
14291431
ethNode: ethNode,
1430-
txPool : txPool
1432+
txPool : txPool,
1433+
chain : chain,
14311434
)
14321435
graphql.init(ctx)
14331436
ctx.initEthApi()
14341437
ctx
14351438

1436-
proc setupGraphqlHttpHandler*(com: CommonRef,
1439+
proc setupGraphqlHttpHandler*(chain: ForkedChainRef,
14371440
ethNode: EthereumNode,
14381441
txPool: TxPoolRef): GraphqlHttpHandlerRef =
1439-
let ctx = setupGraphqlContext(com, ethNode, txPool)
1442+
let ctx = setupGraphqlContext(chain, ethNode, txPool)
14401443
GraphqlHttpHandlerRef.new(ctx)
14411444

14421445
{.pop.}

nimbus/rpc.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ proc addHttpServices(handlers: var seq[RpcHandlerProc],
148148
# json-rpc have no reliable identification
149149

150150
if conf.graphqlEnabled:
151-
let ctx = setupGraphqlContext(com, nimbus.ethNode, nimbus.txPool)
151+
let ctx = setupGraphqlContext(nimbus.chainRef, nimbus.ethNode, nimbus.txPool)
152152
let server = GraphqlHttpHandlerRef.new(ctx)
153153
handlers.addHandler(server)
154154
info "GraphQL API enabled", url = "http://" & $address
@@ -196,7 +196,7 @@ proc addServices(handlers: var seq[RpcHandlerProc],
196196
# The order is important: graphql, ws, rpc
197197

198198
if conf.graphqlEnabled:
199-
let ctx = setupGraphqlContext(com, nimbus.ethNode, nimbus.txPool)
199+
let ctx = setupGraphqlContext(nimbus.chainRef, nimbus.ethNode, nimbus.txPool)
200200
let server = GraphqlHttpHandlerRef.new(ctx)
201201
handlers.addHandler(server)
202202
info "GraphQL API enabled", url = "http://" & $address

nimbus/rpc/rpc_utils.nim

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import
1313
std/[sequtils, algorithm],
1414
./rpc_types,
1515
./params,
16-
../db/core_db,
1716
../db/ledger,
1817
../constants, stint,
1918
../utils/utils,
2019
../transaction,
2120
../transaction/call_evm,
2221
../core/eip4844,
22+
../core/chain/forked_chain,
2323
../evm/types,
2424
../evm/state,
2525
../evm/precompiles,
@@ -29,13 +29,11 @@ import
2929
../common/common,
3030
web3/eth_api_types
3131

32-
proc calculateMedianGasPrice*(chain: CoreDbRef): GasInt {.raises: [RlpError].} =
32+
proc calculateMedianGasPrice*(chain: ForkedChainRef): GasInt =
3333
const minGasPrice = 30_000_000_000.GasInt
3434
var prices = newSeqOfCap[GasInt](64)
35-
let header = chain.getCanonicalHead().valueOr:
36-
return minGasPrice
37-
for encodedTx in chain.getBlockTransactionData(header.txRoot):
38-
let tx = decodeTx(encodedTx)
35+
let blk = chain.latestBlock
36+
for tx in blk.transactions:
3937
prices.add(tx.gasPrice)
4038

4139
if prices.len > 0:
@@ -57,9 +55,10 @@ proc calculateMedianGasPrice*(chain: CoreDbRef): GasInt {.raises: [RlpError].} =
5755
# re-enable the "query.gasPrice" test case (remove `skip = true`).
5856
result = max(result, minGasPrice)
5957

60-
proc unsignedTx*(tx: TransactionArgs, chain: CoreDbRef, defaultNonce: AccountNonce, chainId: ChainId): Transaction
61-
{.gcsafe, raises: [CatchableError].} =
62-
58+
proc unsignedTx*(tx: TransactionArgs,
59+
chain: ForkedChainRef,
60+
defaultNonce: AccountNonce,
61+
chainId: ChainId): Transaction =
6362
var res: Transaction
6463

6564
if tx.to.isSome:

nimbus/rpc/server_api.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ proc ledgerFromTag(api: ServerAPIRef, blockTag: BlockTag): Result[LedgerRef, str
101101
let header = ?api.headerFromTag(blockTag)
102102
if not api.chain.stateReady(header):
103103
api.chain.replaySegment(header.blockHash)
104-
104+
105105
ok(LedgerRef.init(api.com.db))
106106

107107
proc blockFromTag(api: ServerAPIRef, blockTag: BlockTag): Result[Block, string] =
@@ -389,7 +389,7 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
389389

390390
server.rpc("eth_gasPrice") do() -> Web3Quantity:
391391
## Returns an integer of the current gas price in wei.
392-
w3Qty(calculateMedianGasPrice(api.com.db).uint64)
392+
w3Qty(calculateMedianGasPrice(api.chain).uint64)
393393

394394
server.rpc("eth_accounts") do() -> seq[eth_types.Address]:
395395
## Returns a list of addresses owned by client.
@@ -474,7 +474,7 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
474474
let
475475
accDB = api.ledgerFromTag(blockId("latest")).valueOr:
476476
raise newException(ValueError, "Latest Block not found")
477-
tx = unsignedTx(data, api.chain.db, accDB.getNonce(address) + 1, api.com.chainId)
477+
tx = unsignedTx(data, api.chain, accDB.getNonce(address) + 1, api.com.chainId)
478478
eip155 = api.com.isEIP155(api.chain.latestNumber)
479479
signedTx = signTransaction(tx, acc.privateKey, eip155)
480480
return rlp.encode(signedTx)
@@ -495,7 +495,7 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
495495
let
496496
accDB = api.ledgerFromTag(blockId("latest")).valueOr:
497497
raise newException(ValueError, "Latest Block not found")
498-
tx = unsignedTx(data, api.chain.db, accDB.getNonce(address) + 1, api.com.chainId)
498+
tx = unsignedTx(data, api.chain, accDB.getNonce(address) + 1, api.com.chainId)
499499
eip155 = api.com.isEIP155(api.chain.latestNumber)
500500
signedTx = signTransaction(tx, acc.privateKey, eip155)
501501
networkPayload =

tests/test_graphql.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ proc graphqlMain*() =
8686
chain = setupChain()
8787
txPool = TxPoolRef.new(chain)
8888

89-
let ctx = setupGraphqlContext(chain.com, ethNode, txPool)
89+
let ctx = setupGraphqlContext(chain, ethNode, txPool)
9090
when isMainModule:
9191
ctx.main(caseFolder, purgeSchema = false)
9292
else:

0 commit comments

Comments
 (0)