Merge pull request #181 from jangko/trace_transaction_part1

extend rpc api cli and implement debug_traceTransaction stub
This commit is contained in:
coffeepots 2018-11-28 16:02:44 +00:00 committed by GitHub
commit 03ca4ef24a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 144 additions and 34 deletions

View File

@ -89,6 +89,9 @@ type
RpcFlags* {.pure.} = enum
## RPC flags
Enabled ## RPC enabled
Eth ## enable eth_ set of RPC API
Shh ## enable shh_ set of RPC API
Debug ## enable debug_ set of RPC API
RpcConfiguration* = object
## JSON-RPC configuration object
@ -155,6 +158,9 @@ type
net*: NetConfiguration ## Network configuration
debug*: DebugConfiguration ## Debug configuration
const
defaultRpcApi = {RpcFlags.Eth, RpcFlags.Shh}
var nimbusConfig {.threadvar.}: NimbusConfiguration
proc getConfiguration*(): NimbusConfiguration {.gcsafe.}
@ -241,6 +247,19 @@ proc processAddressPortsList(v: string,
for a in tas6: o.add(a)
result = Success
proc processRpcApiList(v: string, flags: var set[RpcFlags]): ConfigStatus =
var list = newSeq[string]()
processList(v, list)
result = Success
for item in list:
case item.toLowerAscii()
of "eth": flags.incl RpcFlags.Eth
of "shh": flags.incl RpcFlags.Shh
of "debug": flags.incl RpcFlags.Debug
else:
warn "unknown rpc api", name = item
result = ErrorIncorrectOption
proc processENode(v: string, o: var ENode): ConfigStatus =
## Convert string to ENode.
let res = initENode(v, o)
@ -314,10 +333,18 @@ proc processRpcArguments(key, value: string): ConfigStatus =
let config = getConfiguration()
let skey = key.toLowerAscii()
if skey == "rpc":
config.rpc.flags.incl(RpcFlags.Enabled)
if RpcFlags.Enabled notin config.rpc.flags:
config.rpc.flags.incl(RpcFlags.Enabled)
config.rpc.flags.incl(defaultRpcApi)
elif skey == "rpcbind":
config.rpc.binds.setLen(0)
result = processAddressPortsList(value, config.rpc.binds)
elif skey == "rpcapi":
if RpcFlags.Enabled in config.rpc.flags:
config.rpc.flags.excl(defaultRpcApi)
else:
config.rpc.flags.incl(RpcFlags.Enabled)
result = processRpcApiList(value, config.rpc.flags)
else:
result = EmptyOption
@ -529,6 +556,7 @@ NETWORKING OPTIONS:
API AND CONSOLE OPTIONS:
--rpc Enable the HTTP-RPC server
--rpcbind:<value> HTTP-RPC server will bind to given comma separated address:port pairs (default: 127.0.0.1:8545)
--rpcapi:<value> Enable specific set of rpc api from comma separated list(eth, shh, debug)
LOGGING AND DEBUGGING OPTIONS:
--debug Enable debug mode

View File

@ -81,9 +81,6 @@ proc getBlockHeader*(self: BaseChainDB; n: BlockNumber): BlockHeader =
## Raises BlockNotFound error if the block is not in the DB.
self.getBlockHeader(self.getBlockHash(n))
proc getBlockBody*(self: BaseChainDB, h: Hash256, output: var BlockBody): bool =
discard # TODO:
proc getScore*(self: BaseChainDB; blockHash: Hash256): int =
rlp.decode(self.db.get(blockHashToScoreKey(blockHash).toOpenArray).toRange, int)
@ -103,19 +100,52 @@ iterator findNewAncestors(self: BaseChainDB; header: BlockHeader): BlockHeader =
else:
h = self.getBlockHeader(h.parentHash)
proc addBlockNumberToHashLookup(self: BaseChainDB; header: BlockHeader) =
proc addBlockNumberToHashLookup*(self: BaseChainDB; header: BlockHeader) =
self.db.put(blockNumberToHashKey(header.blockNumber).toOpenArray,
rlp.encode(header.hash))
proc persistTransactions*(self: BaseChainDB, blockNumber: BlockNumber, transactions: openArray[Transaction]) =
var trie = initHexaryTrie(self.db)
for idx, tx in transactions:
let
encodedTx = rlp.encode(tx).toRange
txHash = keccak256.digest(encodedTx.toOpenArray)
txKey: TransactionKey = (blockNumber, idx)
trie.put(rlp.encode(idx).toRange, encodedTx)
self.db.put(transactionHashToBlockKey(txHash).toOpenArray, rlp.encode(txKey))
iterator getBlockTransactionData(self: BaseChainDB, transactionRoot: Hash256): BytesRange =
var transactionDb = initHexaryTrie(self.db, transactionRoot)
var transactionIdx = 0
while true:
let transactionKey = rlp.encode(transactionIdx).toRange
if transactionKey in transactionDb:
yield transactionDb.get(transactionKey)
else:
break
inc transactionIdx
iterator getBlockTransactionHashes(self: BaseChainDB, blockHeader: BlockHeader): Hash256 =
## Returns an iterable of the transaction hashes from th block specified
## by the given block header.
doAssert(false, "TODO: Implement me")
# let all_encoded_transactions = self._get_block_transaction_data(
# blockHeader.transactionRoot,
# )
# for encoded_transaction in all_encoded_transactions:
# yield keccak(encoded_transaction)
for encodedTx in self.getBlockTransactionData(blockHeader.txRoot):
yield keccak256.digest(encodedTx.toOpenArray)
proc getBlockBody*(self: BaseChainDB, blockHash: Hash256, output: var BlockBody): bool =
var header: BlockHeader
if self.getBlockHeader(blockHash, header):
result = true
output.transactions = @[]
output.uncles = @[]
for encodedTx in self.getBlockTransactionData(header.txRoot):
output.transactions.add(rlp.decode(encodedTx, Transaction))
if header.ommersHash != EMPTY_UNCLE_HASH:
let encodedUncles = self.db.get(genericHashKey(header.ommersHash).toOpenArray).toRange
if encodedUncles.len != 0:
output.uncles = rlp.decode(encodedUncles, seq[BlockHeader])
else:
result = false
proc getTransactionKey*(self: BaseChainDB, transactionHash: Hash256): tuple[blockNumber: BlockNumber, index: int] {.inline.} =
let
@ -154,17 +184,6 @@ proc headerExists*(self: BaseChainDB; blockHash: Hash256): bool =
## Returns True if the header with the given block hash is in our DB.
self.db.contains(genericHashKey(blockHash).toOpenArray)
iterator getBlockTransactionData(self: BaseChainDB, transactionRoot: Hash256): BytesRange =
var transactionDb = initHexaryTrie(self.db, transactionRoot)
var transactionIdx = 0
while true:
let transactionKey = rlp.encode(transactionIdx).toRange
if transactionKey in transactionDb:
yield transactionDb.get(transactionKey)
else:
break
inc transactionIdx
iterator getReceipts*(self: BaseChainDB; header: BlockHeader; receiptClass: typedesc): Receipt =
var receiptDb = initHexaryTrie(self.db, header.receiptRoot)
var receiptIdx = 0
@ -245,7 +264,7 @@ proc persistBlockToDb*(self: BaseChainDB; blk: Block) =
#proc snapshot*(self: BaseChainDB): UUID =
# Snapshots are a combination of the state_root at the time of the
# snapshot and the id of the changeset from the journaled DB.
# snapshot and the id of the changeset from the journaled DB.
#return self.db.snapshot()
# proc commit*(self: BaseChainDB; checkpoint: UUID): void =

View File

@ -12,7 +12,7 @@ import
asyncdispatch2, json_rpc/rpcserver, eth_keys,
eth_p2p, eth_p2p/rlpx_protocols/[eth_protocol, les_protocol],
eth_p2p/blockchain_sync,
config, genesis, rpc/[common, p2p], p2p/chain,
config, genesis, rpc/[common, p2p, debug, whisper], p2p/chain,
eth_trie/db
const UseSqlite = false
@ -85,9 +85,15 @@ proc start(): NimbusObject =
nimbus.ethNode.chain = newChain(chainDB)
if RpcFlags.Enabled in conf.rpc.flags:
if RpcFlags.Eth in conf.rpc.flags:
setupEthRpc(nimbus.ethNode, chainDB, nimbus.rpcServer)
if RpcFlags.Shh in conf.rpc.flags:
setupWhisperRPC(nimbus.rpcServer)
if RpcFlags.Debug in conf.rpc.flags:
setupDebugRpc(chainDB, nimbus.rpcServer)
## Starting servers
nimbus.state = Starting
if RpcFlags.Enabled in conf.rpc.flags:

View File

@ -163,5 +163,7 @@ method persistBlocks*(c: Chain, headers: openarray[BlockHeader], bodies: openarr
discard c.db.persistHeaderToDb(headers[i])
assert(c.db.getCanonicalHead().blockHash == headers[i].blockHash)
c.db.persistTransactions(headers[i].blockNumber, bodies[i].transactions)
transaction.commit()

45
nimbus/rpc/debug.nim Normal file
View File

@ -0,0 +1,45 @@
# 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.
import
strutils, hexstrings, eth_p2p, options,
../db/[db_chain, state_db, storage_types],
json_rpc/rpcserver, json, macros, rpc_utils,
eth_common
type
TraceTxOptions = object
disableStorage: Option[bool]
disableMemory: Option[bool]
disableStack: Option[bool]
proc setupDebugRpc*(chain: BaseChainDB, rpcsrv: RpcServer) =
proc getBlockBody(hash: Hash256): BlockBody =
if not chain.getBlockBody(hash, result):
raise newException(ValueError, "Error when retrieving block body")
rpcsrv.rpc("debug_traceTransaction") do(data: HexDataStr, options: Option[TraceTxOptions]) -> JsonNode:
## The traceTransaction debugging method will attempt to run the transaction in the exact
## same manner as it was executed on the network. It will replay any transaction that may
## have been executed prior to this one before it will finally attempt to execute the
## transaction that corresponds to the given hash.
##
## In addition to the hash of the transaction you may give it a secondary optional argument,
## which specifies the options for this specific call. The possible options are:
##
## * disableStorage: BOOL. Setting this to true will disable storage capture (default = false).
## * disableMemory: BOOL. Setting this to true will disable memory capture (default = false).
## * disableStack: BOOL. Setting this to true will disable stack capture (default = false).
let
txHash = strToHash(data.string)
txDetails = chain.getTransactionKey(txHash)
blockHeader = chain.getBlockHeader(txDetails.blockNumber)
blockHash = chain.getBlockHash(txDetails.blockNumber)
blockBody = getBlockBody(blockHash)

View File

@ -13,7 +13,7 @@ import
eth_common, eth_p2p, eth_keys, eth_trie/db, rlp,
../utils/header, ../transaction, ../config, ../vm_state, ../constants,
../db/[db_chain, state_db, storage_types],
rpc_types
rpc_types, rpc_utils
#[
Note:
@ -28,14 +28,6 @@ import
proc `%`*(value: Time): JsonNode =
result = %value.toSeconds
func strToAddress(value: string): EthAddress = hexToPaddedByteArray[20](value)
func toHash(value: array[32, byte]): Hash256 {.inline.} =
result.data = value
func strToHash(value: string): Hash256 {.inline.} =
result = hexToPaddedByteArray[32](value).toHash
template balance(addressDb: AccountStateDb, address: EthAddress): GasInt =
# TODO: Account balance u256 but GasInt is int64?
cast[GasInt](addressDb.get_balance(address).data.lo)

18
nimbus/rpc/rpc_utils.nim Normal file
View File

@ -0,0 +1,18 @@
# 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.
import hexstrings, nimcrypto, eth_common, byteutils
func strToAddress*(value: string): EthAddress = hexToPaddedByteArray[20](value)
func toHash*(value: array[32, byte]): Hash256 {.inline.} =
result.data = value
func strToHash*(value: string): Hash256 {.inline.} =
result = hexToPaddedByteArray[32](value).toHash