From 693f00b37fb9708dc6e76163da3b8d943ddec00c Mon Sep 17 00:00:00 2001 From: Adrian Barwicki Date: Sat, 3 Nov 2018 22:26:46 +0100 Subject: [PATCH] bchNode node extracted --- example/rpc-calls.js | 4 +++- index.js | 3 ++- pandacash-core/bchNode.js | 47 +++++++++++++++++++++++++++++++++++++++ pandacash-core/index.js | 24 +++++--------------- 4 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 pandacash-core/bchNode.js diff --git a/example/rpc-calls.js b/example/rpc-calls.js index 087f24d..437874f 100644 --- a/example/rpc-calls.js +++ b/example/rpc-calls.js @@ -1,4 +1,6 @@ -const blockchain = require('../pandacash-core/rpc'); +const PandaCashRPC = require('../pandacash-core/rpc'); + +const blockchain = new PandaCashRPC("127.0.0.1", 48332, "regtest"); blockchain.getinfo().then(result => { console.log(result); diff --git a/index.js b/index.js index 8c17c0b..4c8e140 100755 --- a/index.js +++ b/index.js @@ -5,7 +5,8 @@ const figlet = require('figlet'); const pandacashCore = require('./pandacash-core'); const bootstrap = () => { - pandacashCore.startNode().then(() => { + pandacashCore.startNode() + .then(() => { pandacashCore.seedAccounts(); pandacashCore.startApi(); diff --git a/pandacash-core/bchNode.js b/pandacash-core/bchNode.js new file mode 100644 index 0000000..6d875ae --- /dev/null +++ b/pandacash-core/bchNode.js @@ -0,0 +1,47 @@ +const bcash = require('bcash'); + +const FullNode = bcash.FullNode; + +let node; + +const startNode = (opts) => new Promise((resolve, reject) => { + if (node) { + return console.log("Node is already initialized."); + } + + node = new FullNode({ + file: true, + argv: true, + env: true, + logFile: true, + logConsole: opts.debug, + logLevel: 'debug', + + // Start up a blockchain, mempool, and miner using in-memory + // databases (stored in a red-black tree instead of on-disk). + memory: true, + network: "regtest", + workers: true, + listen: false, + loader: require + }); + + (async () => { + await node.ensure(); + await node.open(); + await node.connect(); + + node.startSync(); + + resolve(node); + })().catch((err) => { + console.error(err.stack); + + reject(); + }); +}); + +module.exports = { + startNode +}; + diff --git a/pandacash-core/index.js b/pandacash-core/index.js index e0b092f..6a82870 100644 --- a/pandacash-core/index.js +++ b/pandacash-core/index.js @@ -5,6 +5,7 @@ const path = require('path'); const yargs = require('yargs'); const initArgs = require("./args") const pkg = require('../package.json'); +const bchNode = require('./bchNode'); const PandaCashRPC = require('./rpc'); var detailedVersion = `Pandacash CLI v${pkg.version}`; @@ -60,17 +61,13 @@ async function startNode() { * b) --prefix=${__dirname}/../.bcash */ - const cmd = path.dirname(require.resolve('bcash/package.json')) + `/bin/bcash --network=regtest`; - - blockchainStdout = _exec(cmd).stdout; - - if (options.debug) { - enableLogging(); - } + bchNode.startNode({ + debug: options.debug + }); await nodeAvailable(); - console.log(`Bitcoin Cash blockchain restarted and listens at port ${NODE_PORT}`); + console.log(`Bitcoin Cash blockchain started and listens at port ${NODE_PORT}`); } async function nodeAvailable() { @@ -104,14 +101,6 @@ async function seedAccounts() { await blockchain.generate([ 500, keyPairs[0].address ]) } -function enableLogging() { - console.log('Enabling logging of debug.db.'); - - blockchainStdout && blockchainStdout.on('data', function(data) { - console.log(data.toString()); - }); -} - /** * Starts the wrapper around the RPC commands * We currently use the Bitbox implementation. @@ -172,6 +161,5 @@ module.exports = { startNode, seedAccounts, startApi, - printPandaMessage, - enableLogging + printPandaMessage } \ No newline at end of file