2015-07-15 15:13:41 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var util = require('util');
|
|
|
|
var chainlib = require('chainlib');
|
|
|
|
var BaseDB = chainlib.DB;
|
|
|
|
var Transaction = require('./transaction');
|
|
|
|
var async = require('async');
|
|
|
|
var bitcore = require('bitcore');
|
2015-07-21 13:59:08 -07:00
|
|
|
var $ = bitcore.util.preconditions;
|
2015-07-15 15:13:41 -07:00
|
|
|
var BufferWriter = bitcore.encoding.BufferWriter;
|
|
|
|
var errors = require('./errors');
|
|
|
|
var levelup = chainlib.deps.levelup;
|
|
|
|
var log = chainlib.log;
|
|
|
|
var Address = bitcore.Address;
|
2015-07-21 13:59:08 -07:00
|
|
|
var BaseModule = require('./module');
|
|
|
|
var AddressModule = require('./modules/address');
|
2015-07-15 15:13:41 -07:00
|
|
|
|
|
|
|
function DB(options) {
|
|
|
|
if(!options) {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseDB.call(this, options);
|
|
|
|
|
2015-07-16 13:17:00 -07:00
|
|
|
this.coinbaseAddress = options.coinbaseAddress;
|
2015-07-15 15:13:41 -07:00
|
|
|
this.coinbaseAmount = options.coinbaseAmount || 50 * 1e8;
|
|
|
|
this.Transaction = Transaction;
|
|
|
|
|
|
|
|
this.network = bitcore.Networks.get(options.network) || bitcore.Networks.testnet;
|
2015-07-21 13:59:08 -07:00
|
|
|
this.modules = [];
|
|
|
|
|
|
|
|
// Add address module
|
|
|
|
this.addModule(AddressModule);
|
|
|
|
|
|
|
|
// Add other modules
|
|
|
|
if(options.modules && options.modules.length) {
|
|
|
|
for(var i = 0; i < options.modules.length; i++) {
|
|
|
|
this.addModule(options.modules[i]);
|
|
|
|
}
|
|
|
|
}
|
2015-07-15 15:13:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(DB, BaseDB);
|
|
|
|
|
|
|
|
DB.prototype.getBlock = function(hash, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// get block from bitcoind
|
|
|
|
this.bitcoind.getBlock(hash, function(err, blockData) {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
callback(null, self.Block.fromBuffer(blockData));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-07-22 12:34:15 -07:00
|
|
|
DB.prototype.getPrevHash = function(blockHash, callback) {
|
|
|
|
var blockIndex = this.bitcoind.getBlockIndex(blockHash);
|
|
|
|
setImmediate(function() {
|
|
|
|
if (blockIndex) {
|
|
|
|
callback(null, blockIndex.prevHash);
|
|
|
|
} else {
|
|
|
|
callback(new Error('Could not get prevHash, block not found'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-07-15 15:13:41 -07:00
|
|
|
DB.prototype.putBlock = function(block, callback) {
|
2015-07-22 12:34:15 -07:00
|
|
|
// block is already stored in bitcoind
|
|
|
|
setImmediate(callback);
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
2015-07-16 11:09:10 -07:00
|
|
|
DB.prototype.getTransaction = function(txid, queryMempool, callback) {
|
2015-07-16 13:26:43 -07:00
|
|
|
this.bitcoind.getTransaction(txid, queryMempool, function(err, txBuffer) {
|
2015-07-16 11:09:10 -07:00
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-07-16 11:09:10 -07:00
|
|
|
callback(null, Transaction().fromBuffer(txBuffer));
|
|
|
|
});
|
|
|
|
};
|
2015-07-15 15:13:41 -07:00
|
|
|
|
|
|
|
DB.prototype.validateBlockData = function(block, callback) {
|
|
|
|
// bitcoind does the validation
|
2015-07-20 10:48:13 -07:00
|
|
|
setImmediate(callback);
|
|
|
|
};
|
|
|
|
|
2015-07-22 12:34:15 -07:00
|
|
|
DB.prototype._updatePrevHashIndex = function(block, callback) {
|
|
|
|
// bitcoind has the previous hash for each block
|
|
|
|
setImmediate(callback);
|
|
|
|
};
|
|
|
|
|
2015-07-20 10:48:13 -07:00
|
|
|
DB.prototype._updateWeight = function(hash, weight, callback) {
|
|
|
|
// bitcoind has all work for each block
|
|
|
|
setImmediate(callback);
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
DB.prototype.buildGenesisData = function() {
|
|
|
|
var coinbaseTx = this.buildCoinbaseTransaction();
|
|
|
|
var bw = new BufferWriter();
|
|
|
|
bw.writeVarintNum(1);
|
|
|
|
bw.write(coinbaseTx.toBuffer());
|
|
|
|
var merkleRoot = this.getMerkleRoot([coinbaseTx]);
|
|
|
|
var buffer = bw.concat();
|
|
|
|
return {
|
|
|
|
merkleRoot: merkleRoot,
|
|
|
|
buffer: buffer
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
DB.prototype.buildCoinbaseTransaction = function(transactions, data) {
|
2015-07-16 13:17:00 -07:00
|
|
|
if(!this.coinbaseAddress) {
|
|
|
|
throw new Error('coinbaseAddress required to build coinbase');
|
2015-07-15 15:13:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!data) {
|
|
|
|
data = bitcore.crypto.Random.getRandomBuffer(40);
|
|
|
|
}
|
|
|
|
|
|
|
|
var fees = 0;
|
|
|
|
|
|
|
|
if(transactions && transactions.length) {
|
|
|
|
fees = this.getInputTotal(transactions) - this.getOutputTotal(transactions, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
var coinbaseTx = new this.Transaction();
|
2015-07-16 13:17:00 -07:00
|
|
|
coinbaseTx.to(this.coinbaseAddress, this.coinbaseAmount + fees);
|
2015-07-15 15:13:41 -07:00
|
|
|
|
|
|
|
var script = bitcore.Script.buildDataOut(data);
|
|
|
|
|
|
|
|
var input = new bitcore.Transaction.Input({
|
|
|
|
prevTxId: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
|
|
outputIndex: 0xffffffff,
|
|
|
|
sequenceNumber: 4294967295,
|
|
|
|
script: script
|
|
|
|
});
|
|
|
|
|
|
|
|
coinbaseTx.inputs = [input];
|
|
|
|
return coinbaseTx;
|
|
|
|
};
|
|
|
|
|
|
|
|
DB.prototype.getOutputTotal = function(transactions, excludeCoinbase) {
|
|
|
|
var totals = transactions.map(function(tx) {
|
|
|
|
if(tx.isCoinbase() && excludeCoinbase) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return tx._getOutputAmount();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var grandTotal = totals.reduce(function(previousValue, currentValue) {
|
|
|
|
return previousValue + currentValue;
|
|
|
|
});
|
|
|
|
return grandTotal;
|
|
|
|
};
|
|
|
|
|
|
|
|
DB.prototype.getInputTotal = function(transactions) {
|
|
|
|
var totals = transactions.map(function(tx) {
|
|
|
|
if(tx.isCoinbase()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return tx._getInputAmount();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var grandTotal = totals.reduce(function(previousValue, currentValue) {
|
|
|
|
return previousValue + currentValue;
|
|
|
|
});
|
|
|
|
return grandTotal;
|
|
|
|
};
|
|
|
|
|
|
|
|
DB.prototype._onChainAddBlock = function(block, callback) {
|
|
|
|
log.debug('DB handling new chain block');
|
|
|
|
|
|
|
|
// Remove block from mempool
|
2015-07-21 13:59:08 -07:00
|
|
|
this.mempool.removeBlock(block.hash);
|
|
|
|
this.blockHandler(block, true, callback);
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
DB.prototype._onChainRemoveBlock = function(block, callback) {
|
2015-07-21 13:59:08 -07:00
|
|
|
log.debug('DB removing chain block');
|
|
|
|
this.blockHandler(block, false, callback);
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
2015-07-21 13:59:08 -07:00
|
|
|
DB.prototype.blockHandler = function(block, add, callback) {
|
2015-07-15 15:13:41 -07:00
|
|
|
var self = this;
|
2015-07-21 13:59:08 -07:00
|
|
|
var operations = [];
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-07-21 13:59:08 -07:00
|
|
|
async.eachSeries(
|
|
|
|
this.modules,
|
|
|
|
function(module, next) {
|
|
|
|
module['blockHandler'].call(module, block, add, function(err, ops) {
|
|
|
|
if(err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-07-21 13:59:08 -07:00
|
|
|
operations = operations.concat(ops);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-07-21 13:59:08 -07:00
|
|
|
log.debug('Updating the database with operations', operations);
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-07-21 13:59:08 -07:00
|
|
|
self.store.batch(operations, callback);
|
2015-07-20 12:38:56 -07:00
|
|
|
}
|
2015-07-21 13:59:08 -07:00
|
|
|
);
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
2015-07-21 13:59:08 -07:00
|
|
|
DB.prototype.getAPIMethods = function() {
|
|
|
|
var methods = [
|
2015-07-23 14:08:09 -07:00
|
|
|
['getBlock', this, this.getBlock, 1],
|
2015-07-21 13:59:08 -07:00
|
|
|
['getTransaction', this, this.getTransaction, 2]
|
|
|
|
];
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-07-21 13:59:08 -07:00
|
|
|
for(var i = 0; i < this.modules.length; i++) {
|
|
|
|
methods = methods.concat(this.modules[i]['methods'].call(this.modules[i]));
|
|
|
|
}
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-07-21 13:59:08 -07:00
|
|
|
return methods;
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
2015-07-21 13:59:08 -07:00
|
|
|
DB.prototype.addModule = function(Module) {
|
|
|
|
var module = new Module({
|
2015-07-23 12:13:06 -07:00
|
|
|
db: this
|
2015-07-16 11:09:10 -07:00
|
|
|
});
|
2015-07-21 13:59:08 -07:00
|
|
|
$.checkArgumentType(module, BaseModule);
|
|
|
|
this.modules.push(module);
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = DB;
|