bcoin node moved to its own file

This commit is contained in:
tenthirtyone 2017-08-03 03:08:11 -04:00
parent 3ee90bdbef
commit aaef1c7273
4 changed files with 61 additions and 75 deletions

View File

@ -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');
});

49
lib/node/index.js Normal file
View File

@ -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);
}
});
}

View File

@ -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;
}

18
package-lock.json generated
View File

@ -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": {