From d3f88929da4272f2bf8093953d768036c1c1b254 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Wed, 2 Oct 2019 15:38:14 +0300 Subject: [PATCH] Integrate nim-prompt --- .gitmodules | 10 ++++++++++ beacon_chain.nimble | 1 + beacon_chain/beacon_node.nim | 34 +++++++++++++++++++++++++++++++++- docker/Dockerfile | 2 +- nim.cfg | 7 +++++++ vendor/nim-chronicles | 2 +- vendor/nim-prompt | 1 + vendor/nim-stew | 2 +- vendor/nim-unicodedb | 1 + 9 files changed, 56 insertions(+), 4 deletions(-) create mode 160000 vendor/nim-prompt create mode 160000 vendor/nim-unicodedb diff --git a/.gitmodules b/.gitmodules index 0c40d7b48..d45c444e6 100644 --- a/.gitmodules +++ b/.gitmodules @@ -121,3 +121,13 @@ url = https://github.com/arnetheduck/nim-result.git ignore = dirty branch = master +[submodule "vendor/nim-prompt"] + path = vendor/nim-prompt + url = https://github.com/status-im/nim-prompt.git + ignore = dirty + branch = master +[submodule "vendor/nim-unicodedb"] + path = vendor/nim-unicodedb + url = https://github.com/nitely/nim-unicodedb.git + ignore = dirty + branch = master diff --git a/beacon_chain.nimble b/beacon_chain.nimble index 0482719c9..bd4f1b4a5 100644 --- a/beacon_chain.nimble +++ b/beacon_chain.nimble @@ -29,6 +29,7 @@ requires "nim >= 0.19.0", "nimcrypto", "serialization", "stew", + "prompt", "web3", "yaml" diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index ad9edfd02..85e738439 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -1,9 +1,14 @@ import + # Standard library net, sequtils, tables, osproc, random, strutils, times, strformat, + + # Nimble packages stew/shims/os, stew/[objects, bitseqs], chronos, chronicles, confutils, metrics, json_serialization/std/[options, sets], serialization/errors, eth/trie/db, eth/trie/backends/rocksdb_backend, eth/async_utils, + + # Local modules spec/[datatypes, digest, crypto, beaconstate, helpers, validator, state_transition_block, network], conf, time, state_transition, fork_choice, ssz, beacon_chain_db, @@ -16,6 +21,7 @@ const networkMetadataFile = "network.json" genesisFile = "genesis.json" testnetsBaseUrl = "https://serenity-testnets.status.im" + hasPrompt = not defined(withoutPrompt) declareGauge beacon_slot, "Latest slot of the beacon chain state" declareGauge beacon_head_slot, "Slot of the head block of the beacon chain" @@ -825,6 +831,32 @@ proc start(node: BeaconNode, headState: BeaconState) = node.addLocalValidators(headState) node.run() +when hasPrompt: + from unicode import Rune + import terminal, prompt + + proc providePromptCompletions*(line: seq[Rune], cursorPos: int): seq[string] = + # TODO + # The completions should be generated with the general-purpose command-line + # parsing API of Confutils + result = @[] + + proc initPrompt(node: BeaconNode) = + doAssert defaultChroniclesStream.outputs.len > 0 + doAssert defaultChroniclesStream.output is DynamicOutput + + if isatty(stdout): + var p = Prompt.init("nimbus > ", providePromptCompletions) + p.useHistoryFile() + + defaultChroniclesStream.output.writer = + proc (logLevel: LogLevel, msg: LogOutputStr) {.gcsafe.} = + p.writeLine(msg) + else: + defaultChroniclesStream.output.writer = + proc (logLevel: LogLevel, msg: LogOutputStr) {.gcsafe.} = + stdout.writeLine(msg) + when isMainModule: randomize() let config = BeaconNodeConf.load(version = fullVersionStr()) @@ -855,7 +887,6 @@ when isMainModule: stderr.write "Please regenerate the deposit files by running makeDeposits again\n" quit 1 - var startTime = uint64(times.toUnix(times.getTime()) + config.genesisOffset) initialState = initialize_beacon_state_from_eth1( @@ -933,6 +964,7 @@ when isMainModule: createPidFile(config.dataDir.string / "beacon_node.pid") var node = waitFor BeaconNode.init(config) + when hasPrompt: initPrompt(node) # TODO slightly ugly to rely on node.stateCache state here.. if node.nickname != "": diff --git a/docker/Dockerfile b/docker/Dockerfile index e0fe66748..29cc637fd 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -47,7 +47,7 @@ RUN cd nim-beacon-chain \ -d:"SLOTS_PER_EPOCH=${SLOTS_PER_EPOCH}" \ -d:"SECONDS_PER_SLOT=${SECONDS_PER_SLOT}" \ -d:"chronicles_log_level=DEBUG" \ - -d:"chronicles_sinks=json" \ + -d:"testnet_docker_node" \ c beacon_chain/beacon_node.nim # --------------------------------- # diff --git a/nim.cfg b/nim.cfg index 0004c89dd..1afe69587 100644 --- a/nim.cfg +++ b/nim.cfg @@ -13,6 +13,13 @@ --tlsEmulation:off @end +@if testnet_docker_node: + -d:"chronicles_sinks=json" + -d:"withoutPrompt" +@else: + -d:"chronicles_default_output_device=dynamic" +@end + --threads:on --opt:speed --excessiveStackTrace:on diff --git a/vendor/nim-chronicles b/vendor/nim-chronicles index 743f14cf6..a2ea33956 160000 --- a/vendor/nim-chronicles +++ b/vendor/nim-chronicles @@ -1 +1 @@ -Subproject commit 743f14cf68f213a0cbead5e6dfc00ee3edc7fe1e +Subproject commit a2ea339569878720571bbadabdde72b55c411d78 diff --git a/vendor/nim-prompt b/vendor/nim-prompt new file mode 160000 index 000000000..b0ea27c34 --- /dev/null +++ b/vendor/nim-prompt @@ -0,0 +1 @@ +Subproject commit b0ea27c34ca7aaee42333541f37b182fa239c844 diff --git a/vendor/nim-stew b/vendor/nim-stew index 5f1dc751c..2bdd2fab6 160000 --- a/vendor/nim-stew +++ b/vendor/nim-stew @@ -1 +1 @@ -Subproject commit 5f1dc751ca436e599c59df939344a641f935dd4d +Subproject commit 2bdd2fab6e9bdcdcb51ea7e09d077f36a29faf4c diff --git a/vendor/nim-unicodedb b/vendor/nim-unicodedb new file mode 160000 index 000000000..2afc384ae --- /dev/null +++ b/vendor/nim-unicodedb @@ -0,0 +1 @@ +Subproject commit 2afc384ae17d4bd30078164fbfc2356b06d1a7f4