mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-11 12:54:13 +00:00
Setup test rpc server, initial call to eth_getBalance
This commit is contained in:
parent
989ac892aa
commit
79abeafa41
@ -6,13 +6,42 @@
|
||||
# at your option.
|
||||
# This file may not be copied, modified, or distributed except according to
|
||||
# those terms.
|
||||
import json_rpc/server, nimcrypto
|
||||
import ../config
|
||||
import strutils, nimcrypto, eth_common, stint, eth_trie/[memdb, types]
|
||||
import
|
||||
json_rpc/server, ../vm_state, ../logging, ../db/[db_chain, state_db],
|
||||
../constants, ../config
|
||||
|
||||
proc setupCommonRPC*(server: RpcServer) =
|
||||
server.rpc("web3_clientVersion") do() -> string:
|
||||
result = NimbusIdent
|
||||
|
||||
server.rpc("web3_sha3") do(data: string) -> string:
|
||||
var rawdata = fromHex(data)
|
||||
var rawdata = nimcrypto.fromHex(data)
|
||||
result = "0x" & $keccak_256.digest(rawdata)
|
||||
|
||||
server.rpc("eth_getBalance") do(address: array[20, byte], quantityTag: string) -> int:
|
||||
## Returns the balance of the account of given address.
|
||||
##
|
||||
## data: address to check for balance.
|
||||
## quantityTag: integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
|
||||
## Returns integer of the current balance in wei.
|
||||
var blockNum: BlockNumber
|
||||
let qt = quantityTag.toLowerAscii
|
||||
case quantityTag
|
||||
of "latest": discard # TODO: Get latest block
|
||||
of "earliest": blockNum = GENESIS_BLOCK_NUMBER
|
||||
of "pending": discard # TODO
|
||||
else:
|
||||
# Note: `fromHex` can raise ValueError on bad data.
|
||||
blockNum = stint.fromHex(UInt256, quantityTag)
|
||||
|
||||
let header = BlockHeader(blockNumber: blockNum)
|
||||
var
|
||||
memDb = newMemDB()
|
||||
vmState = newBaseVMState(header, newBaseChainDB(trieDB memDb))
|
||||
let
|
||||
account_db = vmState.readOnlyStateDb
|
||||
balance = account_db.get_balance(address)
|
||||
|
||||
return balance.toInt
|
||||
|
||||
|
67
tests/rpcclient/ethcallsigs.nim
Normal file
67
tests/rpcclient/ethcallsigs.nim
Normal file
@ -0,0 +1,67 @@
|
||||
## This module contains signatures for the Ethereum client RPCs.
|
||||
## The signatures are not imported directly, but read and processed with parseStmt,
|
||||
## then a procedure body is generated to marshal native Nim parameters to json and visa versa.
|
||||
import json, stint, eth_common
|
||||
|
||||
proc web3_clientVersion(): string
|
||||
proc web3_sha3(data: string): string
|
||||
proc net_version(): string
|
||||
proc net_peerCount(): int
|
||||
proc net_listening(): bool
|
||||
proc eth_protocolVersion(): string
|
||||
proc eth_syncing(): JsonNode
|
||||
proc eth_coinbase(): string
|
||||
proc eth_mining(): bool
|
||||
proc eth_hashrate(): int
|
||||
proc eth_gasPrice(): int64
|
||||
proc eth_accounts(): seq[array[20, byte]]
|
||||
proc eth_blockNumber(): int
|
||||
proc eth_getBalance(data: array[20, byte], quantityTag: string): int
|
||||
proc eth_getStorageAt(data: array[20, byte], quantity: int, quantityTag: string): seq[byte]
|
||||
proc eth_getTransactionCount(data: array[20, byte], quantityTag: string)
|
||||
proc eth_getBlockTransactionCountByHash(data: array[32, byte])
|
||||
proc eth_getBlockTransactionCountByNumber(quantityTag: string)
|
||||
proc eth_getUncleCountByBlockHash(data: array[32, byte])
|
||||
proc eth_getUncleCountByBlockNumber(quantityTag: string)
|
||||
proc eth_getCode(data: array[20, byte], quantityTag: string): seq[byte]
|
||||
proc eth_sign(data: array[20, byte], message: seq[byte]): seq[byte]
|
||||
|
||||
# TODO: Use eth_common types
|
||||
|
||||
#[proc eth_sendTransaction(obj: EthSend): UInt256
|
||||
proc eth_sendRawTransaction(data: string, quantityTag: int): UInt256
|
||||
proc eth_call(call: EthCall, quantityTag: string): UInt256
|
||||
proc eth_estimateGas(call: EthCall, quantityTag: string): UInt256
|
||||
proc eth_getBlockByHash(data: array[32, byte], fullTransactions: bool): BlockObject
|
||||
proc eth_getBlockByNumber(quantityTag: string, fullTransactions: bool): BlockObject
|
||||
proc eth_getTransactionByHash(data: Uint256): TransactionObject
|
||||
proc eth_getTransactionByBlockHashAndIndex(data: UInt256, quantity: int): TransactionObject
|
||||
proc eth_getTransactionByBlockNumberAndIndex(quantityTag: string, quantity: int): TransactionObject
|
||||
proc eth_getTransactionReceipt(data: UInt256): ReceiptObject
|
||||
proc eth_getUncleByBlockHashAndIndex(data: UInt256, quantity: int64): BlockObject
|
||||
proc eth_getUncleByBlockNumberAndIndex(quantityTag: string, quantity: int64): BlockObject
|
||||
proc eth_getCompilers(): seq[string]
|
||||
proc eth_compileLLL(): seq[byte]
|
||||
proc eth_compileSolidity(): seq[byte]
|
||||
proc eth_compileSerpent(): seq[byte]
|
||||
proc eth_newFilter(filterOptions: FilterOptions): int
|
||||
proc eth_newBlockFilter(): int
|
||||
proc eth_newPendingTransactionFilter(): int
|
||||
proc eth_uninstallFilter(filterId: int): bool
|
||||
proc eth_getFilterChanges(filterId: int): seq[LogObject]
|
||||
proc eth_getFilterLogs(filterId: int): seq[LogObject]
|
||||
proc eth_getLogs(filterOptions: FilterOptions): seq[LogObject]
|
||||
proc eth_getWork(): seq[UInt256]
|
||||
proc eth_submitWork(nonce: int64, powHash: Uint256, mixDigest: Uint256): bool
|
||||
proc eth_submitHashrate(hashRate: UInt256, id: Uint256): bool
|
||||
proc shh_post(): string
|
||||
proc shh_version(message: WhisperPost): bool
|
||||
proc shh_newIdentity(): array[60, byte]
|
||||
proc shh_hasIdentity(identity: array[60, byte]): bool
|
||||
proc shh_newGroup(): array[60, byte]
|
||||
proc shh_addToGroup(identity: array[60, byte]): bool
|
||||
proc shh_newFilter(filterOptions: FilterOptions, to: array[60, byte], topics: seq[UInt256]): int
|
||||
proc shh_uninstallFilter(id: int): bool
|
||||
proc shh_getFilterChanges(id: int): seq[WhisperMessage]
|
||||
proc shh_getMessages(id: int): seq[WhisperMessage]
|
||||
]#
|
37
tests/test_rpc.nim
Normal file
37
tests/test_rpc.nim
Normal file
@ -0,0 +1,37 @@
|
||||
import
|
||||
unittest, json, strformat,
|
||||
json_rpc/[rpcserver, rpcclient],
|
||||
../nimbus/rpc/common, ../nimbus/constants, ../nimbus/nimbus/account,
|
||||
eth_common
|
||||
|
||||
from os import getCurrentDir, DirSep
|
||||
from strutils import rsplit
|
||||
template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]
|
||||
|
||||
## Generate client convenience marshalling wrappers from forward declarations
|
||||
## For testing, ethcallsigs needs to be kept in sync with ../nimbus/rpc/common
|
||||
const sigPath = &"{sourceDir}{DirSep}rpcclient{DirSep}ethcallsigs.nim"
|
||||
createRpcSigs(RpcSocketClient, sigPath)
|
||||
|
||||
# TODO: Include other transports such as Http
|
||||
var srv = newRpcSocketServer(["localhost:8545"])
|
||||
var client = newRpcSocketClient()
|
||||
|
||||
# Create Ethereum RPCs
|
||||
setupCommonRpc(srv)
|
||||
|
||||
srv.start()
|
||||
waitFor client.connect("localhost", Port(8545))
|
||||
|
||||
suite "Server/Client RPC":
|
||||
var acct = newAccount(balance = 100.u256)
|
||||
test "eth_getBalance":
|
||||
expect ValueError:
|
||||
# check error is raised on null address
|
||||
let
|
||||
blockNumStr = "1"
|
||||
address = ZERO_ADDRESS
|
||||
var r = waitFor client.eth_getBalance(address, blockNumStr)
|
||||
|
||||
srv.stop()
|
||||
srv.close()
|
Loading…
x
Reference in New Issue
Block a user