Skip to content

Commit

Permalink
bchNode node extracted
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianbarwicki committed Nov 3, 2018
1 parent 285cd18 commit 693f00b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
4 changes: 3 additions & 1 deletion example/rpc-calls.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
47 changes: 47 additions & 0 deletions pandacash-core/bchNode.js
Original file line number Diff line number Diff line change
@@ -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
};

24 changes: 6 additions & 18 deletions pandacash-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -172,6 +161,5 @@ module.exports = {
startNode,
seedAccounts,
startApi,
printPandaMessage,
enableLogging
printPandaMessage
}

0 comments on commit 693f00b

Please sign in to comment.