Transaction Models and updated config requires

This commit is contained in:
tenthirtyone 2017-08-03 11:42:24 -04:00
parent 5d7957c5fb
commit 3bf0fe7328
11 changed files with 167 additions and 39 deletions

3
.gitignore vendored
View File

@ -1,6 +1,3 @@
# Config
config/config.js
## Default gitignore below this line
# Logs

20
config/config.js Normal file
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

14
models/input.js Normal file
View File

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

13
models/output.js Normal file
View File

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

33
models/transaction.js Normal file
View File

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

40
test/data/bcoin-tx.json Normal file
View File

@ -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: <Address: type=pubkeyhash version=-1 str=1C8gfoC9B77sea4buoYNwCBwFxpdCp9TCp>,
script: <Script: 0x49 0x3046022100d850be1963dc544faf861827179572cc8989188d9a1e94b68a3ab5453c295432022100da2e198ebc13cee9b706b879e0fc57ebba3b69aba96ab4b5dec0db480c55a11401 0x41 0x046b49c0b7124c4a31dc2f497f9240a04d3898cbeabdc99afafbc9fded9426ce73c23e6a8b9b0a72351c16f86ef1a5750cc5ebddc1ec96e9a10ad9fbf5c46c0e00>,
witness: <Witness: >,
redeem: undefined,
sequence: 4294967295,
prevout: <Outpoint: 18b6eb72c8f7df40904b1d8a01f5dd0a1fb2dad7df67a2a7fa18c0a831f001e3/1>,
coin: null },
{ type: 'pubkeyhash',
subtype: undefined,
address: <Address: type=pubkeyhash version=-1 str=1C8gfoC9B77sea4buoYNwCBwFxpdCp9TCp>,
script: <Script: 0x47 0x304402201b07884070a5af58dbb2239d68c15da533cb44f86691c117f32567c02caa94680220247bce95ecea9ba6a95d72a677fef3f06960a091dd0c52cb084df7adda5adb1a01 0x41 0x046b49c0b7124c4a31dc2f497f9240a04d3898cbeabdc99afafbc9fded9426ce73c23e6a8b9b0a72351c16f86ef1a5750cc5ebddc1ec96e9a10ad9fbf5c46c0e00>,
witness: <Witness: >,
redeem: undefined,
sequence: 4294967295,
prevout: <Outpoint: c1b63ca51404d07d2b850c333a20b78a5a25c9eb027c20e72eed6557a4ea367f/1>,
coin: null } ],
outputs:
[ { type: 'pubkeyhash',
value: '7.49',
script: <Script: OP_DUP OP_HASH160 0x14 0x711df121c2d1132e99df2986fc9086a79d928576 OP_EQUALVERIFY OP_CHECKSIG>,
address: <Address: type=pubkeyhash version=-1 str=1BK7E5XRMmboPYnLGSbEUgAJFSGBaebLkN> } ],
locktime: 0 }