From bfe8258af1164ab9a591d4a7a91c0d63277c8321 Mon Sep 17 00:00:00 2001 From: David Nicholson Date: Thu, 21 Jan 2021 12:22:17 -0800 Subject: [PATCH] allow start with custom block time. fixes #36 --- package.json | 10 ++++++++ .../blockchainMonitor/blockchainState.ts | 2 +- src/extension/index.ts | 8 +++++++ .../neoExpress/neoExpressInstanceManager.ts | 24 ++++++++++++++++--- src/extension/util/ioHelpers.ts | 11 +++++++-- 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 0bee9fa..5d1ac2c 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "onCommand:neo3-visual-devtracker.express.create", "onCommand:neo3-visual-devtracker.express.reset", "onCommand:neo3-visual-devtracker.express.run", + "onCommand:neo3-visual-devtracker.express.runAdvanced", "onCommand:neo3-visual-devtracker.express.transfer", "onCommand:neo3-visual-devtracker.express.walletCreate", "onCommand:neo3-visual-devtracker.neo.contractDeploy", @@ -93,6 +94,11 @@ "dark": "resources/dark/play.svg" } }, + { + "command": "neo3-visual-devtracker.express.runAdvanced", + "title": "Start blockchain (with custom options)...", + "category": "Neo Express 3" + }, { "command": "neo3-visual-devtracker.express.transfer", "title": "Transfer assets", @@ -171,6 +177,10 @@ "command": "neo3-visual-devtracker.express.run", "when": "view == neo3-visual-devtracker.views.blockchains && viewItem == express" }, + { + "command": "neo3-visual-devtracker.express.runAdvanced", + "when": "view == neo3-visual-devtracker.views.blockchains && viewItem == express" + }, { "command": "neo3-visual-devtracker.express.transfer", "when": "view == neo3-visual-devtracker.views.blockchains && viewItem == express" diff --git a/src/extension/blockchainMonitor/blockchainState.ts b/src/extension/blockchainMonitor/blockchainState.ts index 8db8c02..422ea81 100644 --- a/src/extension/blockchainMonitor/blockchainState.ts +++ b/src/extension/blockchainMonitor/blockchainState.ts @@ -2,7 +2,7 @@ import * as bitset from "bitset"; import * as neonTypes from "@cityofzion/neon-core/lib/types"; import * as neonTx from "@cityofzion/neon-core/lib/tx"; -const MAX_REFRESH_INTERVAL_MS = 1000; // initially check every 1s but adapt according to observed block times +const MAX_REFRESH_INTERVAL_MS = 500; const INITIAL_REFRESH_INTERVAL_MS = 3000; const MIN_REFRESH_INTERVAL_MS = 1000 * 30; diff --git a/src/extension/index.ts b/src/extension/index.ts index d03cb6e..5c4a7ef 100644 --- a/src/extension/index.ts +++ b/src/extension/index.ts @@ -204,6 +204,14 @@ export async function activate(context: vscode.ExtensionContext) { (identifier) => neoExpressInstanceManager.run(identifier) ); + registerBlockchainInstanceCommand( + context, + "express", + blockchainsTreeDataProvider, + "neo3-visual-devtracker.express.runAdvanced", + (identifier) => neoExpressInstanceManager.runAdvanced(identifier) + ); + registerBlockchainInstanceCommand( context, "express", diff --git a/src/extension/neoExpress/neoExpressInstanceManager.ts b/src/extension/neoExpress/neoExpressInstanceManager.ts index 65276fc..c7eeb82 100644 --- a/src/extension/neoExpress/neoExpressInstanceManager.ts +++ b/src/extension/neoExpress/neoExpressInstanceManager.ts @@ -4,6 +4,7 @@ import ActiveConnection from "../activeConnection"; import BlockchainIdentifier from "../blockchainIdentifier"; import Log from "../../shared/log"; import NeoExpress from "./neoExpress"; +import IoHelpers from "../util/ioHelpers"; const LOG_PREFIX = "NeoExpressInstanceManager"; // VS Code does not offer an event-driven mechanism for detecting when a user closes a terminal, so polling is required: @@ -38,7 +39,7 @@ export default class NeoExpressInstanceManager { this.disposed = true; } - async run(identifer: BlockchainIdentifier) { + async run(identifer: BlockchainIdentifier, secondsPerBlock: number = 15) { if (identifer.blockchainType !== "express") { return; } @@ -63,7 +64,7 @@ export default class NeoExpressInstanceManager { "-i", child.configPath, "-s", - "15", + `${secondsPerBlock}`, `${child.index}` ); if (terminal) { @@ -77,7 +78,7 @@ export default class NeoExpressInstanceManager { "-i", identifer.configPath, "-s", - "15", + `${secondsPerBlock}`, `${identifer.index}` ); if (terminal) { @@ -99,6 +100,23 @@ export default class NeoExpressInstanceManager { this.onChangeEmitter.fire(); } + async runAdvanced(identifer: BlockchainIdentifier) { + if (identifer.blockchainType !== "express") { + return; + } + + const secondsPerBlock = await IoHelpers.enterNumber( + "How often (in seconds) should new blocks be produced?", + 15, + (n) => + Math.round(n) === n && n > 0 && n <= 3600 + ? null + : "Enter a whole number between 1 and 3600" + ); + + await this.run(identifer, secondsPerBlock); + } + async stop() { try { for (const terminal of this.terminals) { diff --git a/src/extension/util/ioHelpers.ts b/src/extension/util/ioHelpers.ts index 7a27d00..e386fad 100644 --- a/src/extension/util/ioHelpers.ts +++ b/src/extension/util/ioHelpers.ts @@ -43,11 +43,18 @@ export default class IoHelpers { return await IoHelpers.choosePassword(prompt, acceptEmptyString); } - static async enterNumber(prompt: string): Promise { + static async enterNumber( + prompt: string, + defaultValue?: number, + additionalValidation: (n: number) => string | null = () => null + ): Promise { const input = await vscode.window.showInputBox({ prompt, validateInput: (_) => - isNaN(parseFloat(_)) ? "Enter a numeric value" : null, + isNaN(parseFloat(_)) + ? "Enter a numeric value" + : additionalValidation(parseFloat(_)), + value: defaultValue ? `${defaultValue}` : undefined, }); if (input) { return parseFloat(input);