Skip to content

Commit 487743f

Browse files
authored
Proper setup of test_rpc (#2969)
Instead of using ancient/dirty code to setup the rpc test, now using newest method from TxPool and ForkedChain. Also fix some bugs in server_api discovered when using this new setup.
1 parent 557a960 commit 487743f

File tree

4 files changed

+277
-266
lines changed

4 files changed

+277
-266
lines changed

nimbus/core/chain/forked_chain.nim

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -682,11 +682,11 @@ func isInMemory*(c: ForkedChainRef, blockHash: Hash32): bool =
682682
func memoryBlock*(c: ForkedChainRef, blockHash: Hash32): BlockDesc =
683683
c.blocks.getOrDefault(blockHash)
684684

685-
func memoryTransaction*(c: ForkedChainRef, txHash: Hash32): Opt[Transaction] =
685+
func memoryTransaction*(c: ForkedChainRef, txHash: Hash32): Opt[(Transaction, BlockNumber)] =
686686
let (blockHash, index) = c.txRecords.getOrDefault(txHash, (Hash32.default, 0'u64))
687687
c.blocks.withValue(blockHash, val) do:
688-
return Opt.some(val.blk.transactions[index])
689-
return Opt.none(Transaction)
688+
return Opt.some( (val.blk.transactions[index], val.blk.header.number) )
689+
return Opt.none((Transaction, BlockNumber))
690690

691691
proc latestBlock*(c: ForkedChainRef): Block =
692692
c.blocks.withValue(c.cursorHash, val) do:
@@ -740,6 +740,9 @@ proc blockByNumber*(c: ForkedChainRef, number: BlockNumber): Result[Block, strin
740740
if number < c.baseHeader.number:
741741
return c.db.getEthBlock(number)
742742

743+
if number == c.baseHeader.number:
744+
return c.db.getEthBlock(c.baseHash)
745+
743746
shouldNotKeyError "blockByNumber":
744747
var prevHash = c.cursorHash
745748
while prevHash != c.baseHash:

nimbus/rpc/common.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ proc setupCommonRpc*(node: EthereumNode, conf: NimbusConf, server: RpcServer) =
4545
let numPeers = node.numPeers
4646
result = numPeers < conf.maxPeers
4747

48-
server.rpc("net_peerCount") do() -> Web3Quantity:
48+
server.rpc("net_peerCount") do() -> Quantity:
4949
let peerCount = uint node.numPeers
5050
result = w3Qty(peerCount)
5151

nimbus/rpc/server_api.nim

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ proc getTotalDifficulty*(api: ServerAPIRef, blockHash: Hash32): UInt256 =
5050
return totalDifficulty
5151

5252
proc getProof*(
53-
accDB: LedgerRef, address: eth_types.Address, slots: seq[UInt256]
53+
accDB: LedgerRef, address: Address, slots: seq[UInt256]
5454
): ProofResponse =
5555
let
5656
acc = accDB.getEthAccount(address)
@@ -142,7 +142,7 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
142142

143143
server.rpc("eth_getTransactionCount") do(
144144
data: Address, blockTag: BlockTag
145-
) -> Web3Quantity:
145+
) -> Quantity:
146146
## Returns the number of transactions ak.s. nonce sent from an address.
147147
let
148148
ledger = api.ledgerFromTag(blockTag).valueOr:
@@ -151,11 +151,11 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
151151
nonce = ledger.getNonce(address)
152152
Quantity(nonce)
153153

154-
server.rpc("eth_blockNumber") do() -> Web3Quantity:
154+
server.rpc("eth_blockNumber") do() -> Quantity:
155155
## Returns integer of the current block number the client is on.
156156
Quantity(api.chain.latestNumber)
157157

158-
server.rpc("eth_chainId") do() -> Web3Quantity:
158+
server.rpc("eth_chainId") do() -> Quantity:
159159
return Quantity(distinctBase(api.com.chainId))
160160

161161
server.rpc("eth_getCode") do(data: Address, blockTag: BlockTag) -> seq[byte]:
@@ -376,7 +376,7 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
376376

377377
idx.inc
378378

379-
server.rpc("eth_estimateGas") do(args: TransactionArgs) -> Web3Quantity:
379+
server.rpc("eth_estimateGas") do(args: TransactionArgs) -> Quantity:
380380
## Generates and returns an estimate of how much gas is necessary to allow the transaction to complete.
381381
## The transaction will not be added to the blockchain. Note that the estimate may be significantly more than
382382
## the amount of gas actually used by the transaction, for a variety of reasons including EVM mechanics and node performance.
@@ -392,64 +392,64 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
392392
raise newException(ValueError, "rpcEstimateGas error: " & $error.code)
393393
Quantity(gasUsed)
394394

395-
server.rpc("eth_gasPrice") do() -> Web3Quantity:
395+
server.rpc("eth_gasPrice") do() -> Quantity:
396396
## Returns an integer of the current gas price in wei.
397397
w3Qty(calculateMedianGasPrice(api.chain).uint64)
398398

399-
server.rpc("eth_accounts") do() -> seq[eth_types.Address]:
399+
server.rpc("eth_accounts") do() -> seq[Address]:
400400
## Returns a list of addresses owned by client.
401-
result = newSeqOfCap[eth_types.Address](ctx.am.numAccounts)
401+
result = newSeqOfCap[Address](ctx.am.numAccounts)
402402
for k in ctx.am.addresses:
403403
result.add k
404404

405-
server.rpc("eth_getBlockTransactionCountByHash") do(data: Hash32) -> Web3Quantity:
405+
server.rpc("eth_getBlockTransactionCountByHash") do(data: Hash32) -> Quantity:
406406
## Returns the number of transactions in a block from a block matching the given block hash.
407407
##
408408
## data: hash of a block
409409
## Returns integer of the number of transactions in this block.
410410
let blk = api.chain.blockByHash(data).valueOr:
411411
raise newException(ValueError, "Block not found")
412412

413-
Web3Quantity(blk.transactions.len)
413+
Quantity(blk.transactions.len)
414414

415415
server.rpc("eth_getBlockTransactionCountByNumber") do(
416416
blockTag: BlockTag
417-
) -> Web3Quantity:
417+
) -> Quantity:
418418
## Returns the number of transactions in a block from a block matching the given block number.
419419
##
420420
## blockTag: integer of a block number, or the string "latest", "earliest" or "pending", see the default block parameter.
421421
## Returns integer of the number of transactions in this block.
422422
let blk = api.blockFromTag(blockTag).valueOr:
423423
raise newException(ValueError, "Block not found")
424424

425-
Web3Quantity(blk.transactions.len)
425+
Quantity(blk.transactions.len)
426426

427-
server.rpc("eth_getUncleCountByBlockHash") do(data: Hash32) -> Web3Quantity:
427+
server.rpc("eth_getUncleCountByBlockHash") do(data: Hash32) -> Quantity:
428428
## Returns the number of uncles in a block from a block matching the given block hash.
429429
##
430430
## data: hash of a block.
431431
## Returns integer of the number of uncles in this block.
432432
let blk = api.chain.blockByHash(data).valueOr:
433433
raise newException(ValueError, "Block not found")
434434

435-
Web3Quantity(blk.uncles.len)
435+
Quantity(blk.uncles.len)
436436

437-
server.rpc("eth_getUncleCountByBlockNumber") do(blockTag: BlockTag) -> Web3Quantity:
437+
server.rpc("eth_getUncleCountByBlockNumber") do(blockTag: BlockTag) -> Quantity:
438438
## Returns the number of uncles in a block from a block matching the given block number.
439439
##
440440
## blockTag: integer of a block number, or the string "latest", see the default block parameter.
441441
## Returns integer of the number of uncles in this block.
442442
let blk = api.blockFromTag(blockTag).valueOr:
443443
raise newException(ValueError, "Block not found")
444444

445-
Web3Quantity(blk.uncles.len)
445+
Quantity(blk.uncles.len)
446446

447447
template sign(privateKey: PrivateKey, message: string): seq[byte] =
448448
# message length encoded as ASCII representation of decimal
449449
let msgData = "\x19Ethereum Signed Message:\n" & $message.len & message
450450
@(sign(privateKey, msgData.toBytes()).toRaw())
451451

452-
server.rpc("eth_sign") do(data: eth_types.Address, message: seq[byte]) -> seq[byte]:
452+
server.rpc("eth_sign") do(data: Address, message: seq[byte]) -> seq[byte]:
453453
## The sign method calculates an Ethereum specific signature with: sign(keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))).
454454
## By adding a prefix to the message makes the calculated signature recognisable as an Ethereum specific signature.
455455
## This prevents misuse where a malicious DApp can sign arbitrary data (e.g. transaction) and use the signature to impersonate the victim.
@@ -537,16 +537,15 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
537537
if res.isOk:
538538
return populateTransactionObject(res.get().tx, Opt.none(Hash32), Opt.none(uint64))
539539

540-
let txDetails = api.chain.db.getTransactionKey(txHash).valueOr:
541-
return nil
542-
if txDetails.index < 0:
540+
block blockOne:
543541
let
544542
(blockHash, txid) = api.chain.txRecords(txHash)
545-
tx = api.chain.memoryTransaction(txHash).valueOr:
546-
return nil
547-
return populateTransactionObject(tx, Opt.some(blockHash), Opt.some(txid))
548-
# TODO: include block number
543+
(tx, number) = api.chain.memoryTransaction(txHash).valueOr:
544+
break blockOne
545+
return populateTransactionObject(tx, Opt.some(blockHash), Opt.some(number), Opt.some(txid))
549546

547+
let txDetails = api.chain.db.getTransactionKey(txHash).valueOr:
548+
return nil
550549
let header = api.chain.db.getBlockHeader(txDetails.blockNumber).valueOr:
551550
return nil
552551
let tx = api.chain.db.getTransactionByIndex(header.txRoot, uint16(txDetails.index)).valueOr:
@@ -559,7 +558,7 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
559558
)
560559

561560
server.rpc("eth_getTransactionByBlockHashAndIndex") do(
562-
data: Hash32, quantity: Web3Quantity
561+
data: Hash32, quantity: Quantity
563562
) -> TransactionObject:
564563
## Returns information about a transaction by block hash and transaction index position.
565564
##
@@ -578,7 +577,7 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
578577
)
579578

580579
server.rpc("eth_getTransactionByBlockNumberAndIndex") do(
581-
quantityTag: BlockTag, quantity: Web3Quantity
580+
quantityTag: BlockTag, quantity: Quantity
582581
) -> TransactionObject:
583582
## Returns information about a transaction by block number and transaction index position.
584583
##
@@ -597,7 +596,7 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
597596
)
598597

599598
server.rpc("eth_getProof") do(
600-
data: eth_types.Address, slots: seq[UInt256], quantityTag: BlockTag
599+
data: Address, slots: seq[UInt256], quantityTag: BlockTag
601600
) -> ProofResponse:
602601
## Returns information about an account and storage slots (if the account is a contract
603602
## and the slots are requested) along with account and storage proofs which prove the
@@ -662,7 +661,7 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
662661
except CatchableError as exc:
663662
return AccessListResult(error: Opt.some("createAccessList error: " & exc.msg))
664663

665-
server.rpc("eth_blobBaseFee") do() -> Web3Quantity:
664+
server.rpc("eth_blobBaseFee") do() -> Quantity:
666665
## Returns the base fee per blob gas in wei.
667666
let header = api.headerFromTag(blockId("latest")).valueOr:
668667
raise newException(ValueError, "Block not found")
@@ -677,7 +676,7 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
677676
return w3Qty blobBaseFee.truncate(uint64)
678677

679678
server.rpc("eth_getUncleByBlockHashAndIndex") do(
680-
data: Hash32, quantity: Web3Quantity
679+
data: Hash32, quantity: Quantity
681680
) -> BlockObject:
682681
## Returns information about a uncle of a block by hash and uncle index position.
683682
##
@@ -701,7 +700,7 @@ proc setupServerAPI*(api: ServerAPIRef, server: RpcServer, ctx: EthContext) =
701700
)
702701

703702
server.rpc("eth_getUncleByBlockNumberAndIndex") do(
704-
quantityTag: BlockTag, quantity: Web3Quantity
703+
quantityTag: BlockTag, quantity: Quantity
705704
) -> BlockObject:
706705
# Returns information about a uncle of a block by number and uncle index position.
707706
##

0 commit comments

Comments
 (0)