From 21070d510d932d762d3bb08fe563575c86a8af87 Mon Sep 17 00:00:00 2001 From: andri lim Date: Wed, 28 Nov 2018 09:25:36 +0700 Subject: [PATCH] add TransactionKey to persistTransactions and add more fat to debug_traceTransaction --- nimbus/config.nim | 3 ++- nimbus/db/db_chain.nim | 15 ++++++++++----- nimbus/nimbus.nim | 2 -- nimbus/p2p/chain.nim | 2 +- nimbus/rpc/debug.nim | 15 ++++++++++++--- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/nimbus/config.nim b/nimbus/config.nim index d6e95c65b..758346039 100644 --- a/nimbus/config.nim +++ b/nimbus/config.nim @@ -254,6 +254,7 @@ proc processRpcApiList(v: string, flags: var set[RpcFlags]): ConfigStatus = 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 @@ -555,7 +556,7 @@ NETWORKING OPTIONS: API AND CONSOLE OPTIONS: --rpc Enable the HTTP-RPC server --rpcbind: HTTP-RPC server will bind to given comma separated address:port pairs (default: 127.0.0.1:8545) - --rpcapi: Enable specific set of rpc api from comma separated list(eth, debug) + --rpcapi: Enable specific set of rpc api from comma separated list(eth, shh, debug) LOGGING AND DEBUGGING OPTIONS: --debug Enable debug mode diff --git a/nimbus/db/db_chain.nim b/nimbus/db/db_chain.nim index a2906d1d5..7caddeb4a 100644 --- a/nimbus/db/db_chain.nim +++ b/nimbus/db/db_chain.nim @@ -100,14 +100,19 @@ 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, transactions: openarray[Transaction]) = - var tr = initHexaryTrie(self.db) - for i, t in transactions: - tr.put(rlp.encode(i).toRange, rlp.encode(t).toRange) +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) diff --git a/nimbus/nimbus.nim b/nimbus/nimbus.nim index 08a60c63d..d24f45367 100644 --- a/nimbus/nimbus.nim +++ b/nimbus/nimbus.nim @@ -110,8 +110,6 @@ proc start(): NimbusObject = if status != syncSuccess: echo "Block sync failed: ", status - #runForever() - nimbus.state = Running result = nimbus diff --git a/nimbus/p2p/chain.nim b/nimbus/p2p/chain.nim index a1c7fe007..d00d78270 100644 --- a/nimbus/p2p/chain.nim +++ b/nimbus/p2p/chain.nim @@ -163,7 +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(bodies[i].transactions) + c.db.persistTransactions(headers[i].blockNumber, bodies[i].transactions) transaction.commit() diff --git a/nimbus/rpc/debug.nim b/nimbus/rpc/debug.nim index 25b26ca9d..fc99e3ac7 100644 --- a/nimbus/rpc/debug.nim +++ b/nimbus/rpc/debug.nim @@ -10,7 +10,8 @@ import strutils, hexstrings, eth_p2p, options, ../db/[db_chain, state_db, storage_types], - json_rpc/rpcserver, json, macros, rpc_utils + json_rpc/rpcserver, json, macros, rpc_utils, + eth_common type TraceTxOptions = object @@ -20,6 +21,10 @@ type 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 @@ -32,5 +37,9 @@ proc setupDebugRpc*(chain: BaseChainDB, rpcsrv: RpcServer) = ## * 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). - var hashData = strToHash(data.string) - + let + txHash = strToHash(data.string) + txDetails = chain.getTransactionKey(txHash) + blockHeader = chain.getBlockHeader(txDetails.blockNumber) + blockHash = chain.getBlockHash(txDetails.blockNumber) + blockBody = getBlockBody(blockHash)