From 3bf0fe73281c1f320b44d1c0781744ed4deebb87 Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Thu, 3 Aug 2017 11:42:24 -0400 Subject: [PATCH] Transaction Models and updated config requires --- .gitignore | 3 -- config/config.js | 20 +++++++++++ index.js | 2 +- lib/api/index.js | 2 +- lib/db/index.js | 2 +- lib/node/index.js | 74 +++++++++++++++++++++++------------------ lib/util/index.js | 3 ++ models/input.js | 14 ++++++++ models/output.js | 13 ++++++++ models/transaction.js | 33 ++++++++++++++++++ test/data/bcoin-tx.json | 40 ++++++++++++++++++++++ 11 files changed, 167 insertions(+), 39 deletions(-) create mode 100644 config/config.js create mode 100644 models/input.js create mode 100644 models/output.js create mode 100644 models/transaction.js create mode 100644 test/data/bcoin-tx.json diff --git a/.gitignore b/.gitignore index a424d31..76d5d4a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,3 @@ -# Config -config/config.js - ## Default gitignore below this line # Logs diff --git a/config/config.js b/config/config.js new file mode 100644 index 0000000..17747b7 --- /dev/null +++ b/config/config.js @@ -0,0 +1,20 @@ +const config = { + bcoin: { + network: 'main', + db: 'leveldb', + checkpoints: true, + workers: true, + logLevel: 'info', + }, + mongodb: { + uri: 'mongodb://localhost/bitcore', + options: { + useMongoClient: true, + }, + }, + api: { + port: 3000, + }, +}; + +module.exports = config; diff --git a/index.js b/index.js index 40c2934..559a8b9 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ const node = require('./lib/node'); -const config = require('./config/config.js'); +const config = require('./config/config'); const logger = require('./lib/logger'); const Api = require('./lib/api'); diff --git a/lib/api/index.js b/lib/api/index.js index 7a58ae9..1eac2f0 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -1,7 +1,7 @@ const express = require('express'); const app = express(); const mongoose = require('mongoose'); -const config = require('../../config/config.js'); +const config = require('../../config/config'); const Block = require('../../models/block.js'); const BLOCK_LIMIT = 200; diff --git a/lib/db/index.js b/lib/db/index.js index 8b64ea1..cce9817 100644 --- a/lib/db/index.js +++ b/lib/db/index.js @@ -1,5 +1,5 @@ const mongoose = require('mongoose'); -const config = require('../../config/config.js'); +const config = require('../../config/config'); mongoose.connect(config.mongodb.uri, config.mongodb.options); diff --git a/lib/node/index.js b/lib/node/index.js index 40edae7..a6eaec0 100644 --- a/lib/node/index.js +++ b/lib/node/index.js @@ -1,11 +1,11 @@ const FullNode = require('bcoin/lib/node/fullnode'); -const config = require('../../config/config.js'); +const config = require('../../config/config'); 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'); - +const BlockModel = require('../../models/block'); +const TxModel = require('../../models/transaction'); function start() { node.open() @@ -18,39 +18,47 @@ function start() { 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, - }); +function processBlock(entry, block, cb) { + block.hash = util.revHex(block.hash().toString('hex')); + const b = new BlockModel({ + hash: block.hash, + size: block.size, + height: block.height, + version: block.version, + merkleRoot: block.merkleRoot, + tx: block.txs.map((tx) => { + processTx(tx); + return 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); + } + }); +} + +function processTx(tx) { + console.log(util.revHex(tx.hash().toString('hex'))); - b.save((err) => { - if (err) { - console.log(err.message); - } - }); - } } module.exports = { start, -} \ No newline at end of file +}; diff --git a/lib/util/index.js b/lib/util/index.js index 918b6ce..890b14e 100644 --- a/lib/util/index.js +++ b/lib/util/index.js @@ -1,5 +1,8 @@ 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/models/input.js b/models/input.js new file mode 100644 index 0000000..82cda75 --- /dev/null +++ b/models/input.js @@ -0,0 +1,14 @@ +const mongoose = require('mongoose'); +const Schema = mongoose.Schema; + +const Input = new Schema({ + utxo: String, + vout: Number, + address: String, + amount: Number, + wallets: { type: [Schema.Types.ObjectId] }, +}); + +const Input = mongoose.model('Input', Input); + +module.exports = Input; \ No newline at end of file diff --git a/models/output.js b/models/output.js new file mode 100644 index 0000000..80429c2 --- /dev/null +++ b/models/output.js @@ -0,0 +1,13 @@ +const mongoose = require('mongoose'); +const Schema = mongoose.Schema; + +const Output = new Schema({ + address: String, + amount: Number, + vout: Number, + wallets: { type: [Schema.Types.ObjectId] }, +}); + +const Output = mongoose.model('Output', Output); + +module.exports = Output; \ No newline at end of file diff --git a/models/transaction.js b/models/transaction.js new file mode 100644 index 0000000..a107ba0 --- /dev/null +++ b/models/transaction.js @@ -0,0 +1,33 @@ +const Schema = mongoose.Schema; + +const TransactionSchema = new Schema({ + txid: String, + chain: String, + blockHeight: Number, + blockHash: String, + blockTime: Date, + blockTimeNormalized: Date, + inputs: [Input], + outputs: [Output], + coinbase: Boolean, + fee: Number, + inputsProcessed: Boolean, + wallets: { type: [Schema.Types.ObjectId] }, +}); + +TransactionSchema.index({ txid: 1 }, { unique: true }); +TransactionSchema.index({ blockHeight: 1, wallets: 1 }); +TransactionSchema.index({ blockHash: 1 }); +TransactionSchema.index({ blockTime: 1 }); +TransactionSchema.index({ blockTimeNormalized: 1, wallets: 1 }); + +TransactionSchema.index({ 'outputs.address': 1 }); +TransactionSchema.index({ 'inputs.address': 1 }); +TransactionSchema.index({ wallets: 1 }, { sparse: true }); +TransactionSchema.index({ 'inputs.wallets': 1 }, { sparse: true }); +TransactionSchema.index({ 'outputs.wallets': 1 }, { sparse: true }); + +const Block = mongoose.model('Transaction', TransactionSchema); + +module.exports = Block; + diff --git a/test/data/bcoin-tx.json b/test/data/bcoin-tx.json new file mode 100644 index 0000000..4d0aead --- /dev/null +++ b/test/data/bcoin-tx.json @@ -0,0 +1,40 @@ +{ hash: 'ed1be20876b90b857a4ddb32e72f334df997cf45f5a33b3578f0dda65abacd57', + witnessHash: 'ed1be20876b90b857a4ddb32e72f334df997cf45f5a33b3578f0dda65abacd57', + size: 404, + virtualSize: 404, + value: '7.49', + fee: '0.0', + rate: '0.0', + minFee: '0.00000404', + height: -1, + block: null, + ts: 0, + date: null, + index: -1, + version: 1, + flag: 1, + inputs: + [ { type: 'pubkeyhash', + subtype: undefined, + address: , + script: , + witness: , + redeem: undefined, + sequence: 4294967295, + prevout: , + coin: null }, + { type: 'pubkeyhash', + subtype: undefined, + address: , + script: , + witness: , + redeem: undefined, + sequence: 4294967295, + prevout: , + coin: null } ], + outputs: + [ { type: 'pubkeyhash', + value: '7.49', + script: , + address: } ], + locktime: 0 }