nimbus-eth1/nimbus/rpc/p2p.nim

48 lines
1.9 KiB
Nim
Raw Normal View History

2018-06-20 17:27:32 +00:00
# Nimbus
# Copyright (c) 2018 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
2018-07-31 17:18:43 +00:00
import
nimcrypto, json_rpc/server, eth_p2p, hexstrings, strutils, stint,
../config, ../vm_state, ../constants, eth_trie/[memdb, types],
../db/[db_chain, state_db], eth_common
2018-08-06 16:18:24 +00:00
func headerFromTag(chain:BaseChainDB, blockTag: string): BlockHeader =
2018-08-03 16:16:10 +00:00
let tag = blockTag.toLowerAscii
2018-07-31 17:18:43 +00:00
case tag
of "latest": result = chain.getCanonicalHead()
of "earliest": result = chain.getCanonicalBlockHeaderByNumber(GENESIS_BLOCK_NUMBER)
of "pending":
2018-08-06 16:18:24 +00:00
#TODO: Implement get pending block
2018-07-31 17:18:43 +00:00
raise newException(ValueError, "Pending tag not yet implemented")
else:
# Raises are trapped and wrapped in JSON when returned to the user.
2018-08-06 16:18:24 +00:00
tag.validateHexQuantity
2018-07-31 17:18:43 +00:00
let blockNum = stint.fromHex(UInt256, tag)
result = chain.getCanonicalBlockHeaderByNumber(blockNum)
2018-06-20 17:27:32 +00:00
proc setupP2PRPC*(server: EthereumNode, rpcsrv: RpcServer) =
rpcsrv.rpc("net_version") do() -> uint:
2018-06-20 17:27:32 +00:00
let conf = getConfiguration()
result = conf.net.networkId
2018-07-31 17:18:43 +00:00
rpcsrv.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.
template chain: untyped = BaseChainDB(server.chain) # TODO: Sensible casting
let
header = chain.headerFromTag(quantityTag)
vmState = newBaseVMState(header, chain)
account_db = vmState.readOnlyStateDb
balance = account_db.get_balance(address)
return balance.toInt