From aaef1c72736bc402849a28d91f523127c7048c16 Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Thu, 3 Aug 2017 03:08:11 -0400 Subject: [PATCH] bcoin node moved to its own file --- index.js | 66 ++--------------------------------------------- lib/node/index.js | 49 +++++++++++++++++++++++++++++++++++ lib/util/index.js | 3 --- package-lock.json | 18 +++++++------ 4 files changed, 61 insertions(+), 75 deletions(-) create mode 100644 lib/node/index.js diff --git a/index.js b/index.js index 93400f7..93c851e 100644 --- a/index.js +++ b/index.js @@ -1,74 +1,12 @@ -const FullNode = require('bcoin/lib/node/fullnode'); -const config = require(`${__dirname}/config/config.js`); +const node = require('./lib/node'); const logger = require('./lib/logger'); -const Block = require('./models/block'); const Api = require('./lib/api'); -const db = require('./lib/db'); -const util = require('./lib/util'); -const node = new FullNode(config.bcoin); logger.log('debug', 'Debug mode started'); -node.open() - .then() - -node.open() -.then(() => { - node.connect().then(() => { - node.startSync(); - }); -}); - -node.chain.on('connect', (entry, block) => { - console.log(entry.height); - processBlock(entry, block); -}); - -node.mempool.on('tx', (tx) => { - console.log(tx); -}); - -node.chain.on('full', () => { - node.mempool.getHistory().then(console.log); -}); - -function processBlock(entry, block) { - block.hash = util.revHex(block.hash().toString('hex')); - logger.log('debug', - `New Block Height: ${block.height}, Hash: ${block.hash}`); - - console.log(entry); - - const b = new Block({ - hash: block.hash, - size: block.size, - height: block.height, - version: block.version, - merkleRoot: block.merkleRoot, - tx: block.txs.map(tx => util.revHex(tx.hash().toString('hex'))), - time: block.ts, - nonce: block.nonce, - bits: block.bits, - difficulty: block.bits, - chainwork: entry.chainwork, - confirmations: 0, - previousBlockHash: block.previousBlockHash, - nextBlockHash: 0, - reward: 0, - timeNormalized: block.ts, - isMainChain: true, - poolInfo: Object, - transactionCount: block.txs.length, - }); - b.save((err) => { - if (err) { - console.log(err); - } - }); -} - Api.listen(3000, () => { logger.log('debug', 'listening on port 3000'); }); + diff --git a/lib/node/index.js b/lib/node/index.js new file mode 100644 index 0000000..5df301b --- /dev/null +++ b/lib/node/index.js @@ -0,0 +1,49 @@ +const FullNode = require('bcoin/lib/node/fullnode'); +const config = require('../../config/config.js'); +const node = new FullNode(config.bcoin); +const BlockSchema = require('../../models/block'); +const logger = require('../../lib/logger'); +const db = require('../../lib/db'); +const util = require('../../lib/util'); + +node.open() +.then(() => { + node.connect().then(() => { + node.startSync(); + }); +}); + +node.chain.on('connect', (entry, block) => { + processBlock(entry, block); +}); + +function processBlock(entry, block, cb) { + block.hash = util.revHex(block.hash().toString('hex')) + const b = new BlockSchema({ + hash: block.hash, + size: block.size, + height: block.height, + version: block.version, + merkleRoot: block.merkleRoot, + tx: block.txs.map(tx => util.revHex(tx.hash().toString('hex'))), + time: block.ts, + nonce: block.nonce, + bits: block.bits, + difficulty: block.bits, + chainwork: entry.chainwork, + confirmations: 0, + previousBlockHash: block.previousBlockHash, + nextBlockHash: 0, + reward: 0, + timeNormalized: block.ts, + isMainChain: true, + poolInfo: Object, + transactionCount: block.txs.length, + }); + + b.save((err) => { + if (err) { + console.log(err.message); + } + }); +} diff --git a/lib/util/index.js b/lib/util/index.js index 890b14e..918b6ce 100644 --- a/lib/util/index.js +++ b/lib/util/index.js @@ -1,8 +1,5 @@ function revHex(hex) { let rev = ''; - for (let i = 0; i < hex.length; i += 2) { - rev = hex.slice(i, i + 2) + rev; - } return rev; } diff --git a/package-lock.json b/package-lock.json index e21c84a..1af0fb3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -58,14 +58,6 @@ "resolved": "https://registry.npmjs.org/arraybuffer.slice/-/arraybuffer.slice-0.0.6.tgz", "integrity": "sha1-8zshWfBTKj8xB6JywMz70a0peco=" }, - "async": { - "version": "2.1.4", - "resolved": "https://registry.npmjs.org/async/-/async-2.1.4.tgz", - "integrity": "sha1-LSFgx3iAMuTdbL4lAvH5osj2zeQ=", - "requires": { - "lodash": "4.17.4" - } - }, "backo2": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz", @@ -1028,6 +1020,16 @@ "muri": "1.2.2", "regexp-clone": "0.0.1", "sliced": "1.0.1" + }, + "dependencies": { + "async": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/async/-/async-2.1.4.tgz", + "integrity": "sha1-LSFgx3iAMuTdbL4lAvH5osj2zeQ=", + "requires": { + "lodash": "4.17.4" + } + } } }, "mpath": {