Added Utils, db, and updated block schema

This commit is contained in:
tenthirtyone 2017-08-02 17:02:01 -04:00
parent 6b7496e0c7
commit ee160d9e7b
7 changed files with 4375 additions and 24 deletions

View File

@ -1,18 +1,18 @@
const FullNode = require('bcoin/lib/node/fullnode');
const mongoose = require('mongoose');
const config = require('./config/config.js');
const config = require(`${__dirname}/config/config.js`);
const logger = require('./lib/logger');
const Block = require('./models/block');
const Server = require('./lib/server');
mongoose.connect(config.mongodb.uri, config.mongodb.options);
const db = require('./lib/db');
const util = require('./lib/util');
const node = new FullNode(config.bcoin);
logger.log('debug',
'Debug mode started');
const node = new FullNode(config.bcoin);
(async () => {
logger.log('debug',
'Starting Full Node');
await node.open();
await node.connect();
@ -29,38 +29,38 @@ const node = new FullNode(config.bcoin);
})();
function processBlock(block) {
block.hash = revHex(block.hash().toString('hex'));
block.hash = util.revHex(block.hash().toString('hex'));
logger.log('debug',
`New Block Height: ${block.height}, Hash: ${block.hash}`);
let b = new Block({
mainChain: true,
height: block.height,
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,
timeNormalized: block.ts,
nonce: block.nonce,
previousBlockHash: block.prevBlock,
bits: block.bits,
difficulty: block.bits,
chainwork: 0,
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);
}
})
});
}
function revHex(hexString) {
let out = '';
for (let i = 0; i < hexString.length; i += 2) {
out = hexString.slice(i, i + 2) + out;
}
return out;
}
Server.listen(3000, function() {
Server.listen(3000, () => {
console.log('listening on port 3000');
})
});

6
lib/db/index.js Normal file
View File

@ -0,0 +1,6 @@
const mongoose = require('mongoose');
const config = require('../../config/config.js');
mongoose.connect(config.mongodb.uri, config.mongodb.options);
module.exports = mongoose;

View File

@ -10,4 +10,14 @@ app.get('/block/:blockhash', (req, res) => {
res.send(req.params.blockhash);
});
app.get('/blocks', (req, res) => {
res.send({
blocks: [],
length: 0,
pagination: {
next:
}
})
});
module.exports = app;

12
lib/util/index.js Normal file
View File

@ -0,0 +1,12 @@
function revHex(hex) {
let rev = '';
for (let i = 0; i < hex.length; i += 2) {
rev = hex.slice(i, i + 2) + rev;
}
return rev;
}
module.exports = {
revHex,
};

View File

@ -0,0 +1,31 @@
{ hash: '000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6',
height: -1,
size: 215,
virtualSize: 215,
date: '2010-01-06T03:37:04Z',
version: '00000001',
prevBlock: '00000000920c6a17d60885cd045953e2449197770745d269903d6de71a5d5ea0',
merkleRoot: 'e73bbe87262db43b5d36d05dbb0f6d6fcfaaac86e5340e6c72523bbc179469be',
commitmentHash: null,
ts: 1262749024,
bits: 486594666,
nonce: 134870029,
txs:
[ { hash: 'e73bbe87262db43b5d36d05dbb0f6d6fcfaaac86e5340e6c72523bbc179469be',
witnessHash: 'e73bbe87262db43b5d36d05dbb0f6d6fcfaaac86e5340e6c72523bbc179469be',
size: 134,
virtualSize: 134,
value: '50.0',
fee: '0.0',
rate: '0.0',
minFee: '0.00000134',
height: -1,
block: null,
ts: 0,
date: null,
index: 0,
version: 1,
flag: 1,
inputs: [Array],
outputs: [Array],
locktime: 0 } ] }

File diff suppressed because it is too large Load Diff

7
test/model.test.js Normal file
View File

@ -0,0 +1,7 @@
const db = require('../lib/db');
const Block = require('../models/block.js');
Block.findOne({}, function(err, block) {
console.log(err)
console.log(block)
})