From e104153379824690bd471d9902c82586fab278a8 Mon Sep 17 00:00:00 2001 From: andri lim Date: Sat, 12 Jan 2019 19:48:28 +0700 Subject: [PATCH] add premix and browser launcher code --- nimbus/launcher.nim | 40 ++++++++++++++++++++++++++++++++++++++++ nimbus/tracer.nim | 13 +++++++++---- premix/dumper.nim | 2 +- 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 nimbus/launcher.nim diff --git a/nimbus/launcher.nim b/nimbus/launcher.nim new file mode 100644 index 000000000..20d912f67 --- /dev/null +++ b/nimbus/launcher.nim @@ -0,0 +1,40 @@ +import os, osproc, json + +when defined(windows): + const + premixExecutable = "premix.exe" + browserLauncher = "cmd /c start" +elif defined(macos): + const + premixExecutable = "premix" + browserLauncher = "open" +else: + const + premixExecutable = "premix" + browserLauncher = "xdg-open" + +proc getPremixDir(): (bool, string) = + var premixDir = [ + "." , + ".." & DirSep & "premix" + ] + + for c in premixDir: + if fileExists(c & DirSep & premixExecutable): + return (true, c) + + result = (false, ".") + +proc launchPremix*(fileName: string, metaData: JsonNode) = + let + (premixAvailable, premixDir) = getPremixDir() + premixExe = premixDir & DirSep & premixExecutable + + writeFile(premixDir & DirSep & fileName, metaData.pretty) + + if premixAvailable: + if execCmd(premixExe & " " & premixDir & DirSep & fileName) == 0: + if execCmd(browserLauncher & " " & premixDir & DirSep & "index.html") != 0: + echo "failed to launch default browser" + else: + echo "failed to execute premix debugging tool" diff --git a/nimbus/tracer.nim b/nimbus/tracer.nim index 8fdaeb659..a3f50c832 100644 --- a/nimbus/tracer.nim +++ b/nimbus/tracer.nim @@ -2,7 +2,7 @@ import db/[db_chain, state_db, capturedb], eth_common, utils, json, constants, vm_state, vm_types, transaction, p2p/executor, eth_trie/db, nimcrypto, strutils, ranges, ./utils/addresses, - chronicles, rpc/hexstrings + chronicles, rpc/hexstrings, launcher proc getParentHeader(self: BaseChainDB, header: BlockHeader): BlockHeader = self.getBlockHeader(header.parentHash) @@ -230,7 +230,8 @@ proc traceTransactions*(chainDB: BaseChainDB, header: BlockHeader, blockBody: Bl for i in 0 ..< blockBody.transactions.len: result.add traceTransaction(chainDB, header, blockBody, i, {DisableState}) -proc dumpDebuggingMetaData*(chainDB: BaseChainDB, header: BlockHeader, blockBody: BlockBody, receipts: seq[Receipt]) = +proc dumpDebuggingMetaData*(chainDB: BaseChainDB, header: BlockHeader, + blockBody: BlockBody, receipts: seq[Receipt], launchDebugger = true) = let blockNumber = header.blockNumber @@ -249,5 +250,9 @@ proc dumpDebuggingMetaData*(chainDB: BaseChainDB, header: BlockHeader, blockBody } metaData.dumpMemoryDB(memoryDB) - # this is a placeholder until premix debugging tool is ready - writeFile("debug" & $blockNumber & ".json", metaData.pretty()) + + let jsonFileName = "debug" & $blockNumber & ".json" + if launchDebugger: + launchPremix(jsonFileName, metaData) + else: + writeFile(jsonFileName, metaData.pretty()) diff --git a/premix/dumper.nim b/premix/dumper.nim index 083cd5e82..9f20b693c 100644 --- a/premix/dumper.nim +++ b/premix/dumper.nim @@ -26,7 +26,7 @@ proc dumpDebug(chainDB: BaseChainDB, blockNumber: Uint256) = captureChainDB.setHead(parent, true) let validationResult = processBlock(captureChainDB, parent, header, body, vmState) - dumpDebuggingMetaData(captureChainDB, header, body, vmState.receipts) + dumpDebuggingMetaData(captureChainDB, header, body, vmState.receipts, false) proc main() = let conf = getConfiguration()