moving forward with new sync
This commit is contained in:
parent
2468cedde4
commit
cac5264b64
@ -3,11 +3,11 @@ BitcoreNode:
|
||||
NetworkMonitor:
|
||||
host: localhost
|
||||
port: 8333
|
||||
Reporter: simple # none, simple, matrix
|
||||
Reporter: none # none, simple, matrix
|
||||
LevelUp: ./db
|
||||
RPC:
|
||||
user: username
|
||||
pass: password
|
||||
protocol: http
|
||||
host: 127.0.0.1
|
||||
port: 8332
|
||||
port: 18332
|
||||
|
||||
3
index.js
3
index.js
@ -4,6 +4,8 @@ var BitcoreNode = require('./lib/node');
|
||||
var reporters = require('./lib/reporters');
|
||||
var bitcore = require('bitcore');
|
||||
|
||||
BitcoreNode.errors = require('./lib/errors');
|
||||
|
||||
if (require.main === module) {
|
||||
var config = require('config');
|
||||
bitcore.Networks.defaultNetwork = bitcore.Networks.get(config.network);
|
||||
@ -27,6 +29,5 @@ if (require.main === module) {
|
||||
}
|
||||
|
||||
|
||||
BitcoreNode.errors = require('./lib/errors');
|
||||
|
||||
module.exports = BitcoreNode;
|
||||
|
||||
@ -6,6 +6,7 @@ var EventEmitter = require('eventemitter2').EventEmitter2;
|
||||
var bitcore = require('bitcore');
|
||||
var Networks = bitcore.Networks;
|
||||
var $ = bitcore.util.preconditions;
|
||||
var _ = bitcore.deps._;
|
||||
var p2p = require('bitcore-p2p');
|
||||
var Peer = p2p.Peer;
|
||||
var messages = new p2p.Messages();
|
||||
@ -38,13 +39,22 @@ NetworkMonitor.prototype.setupPeer = function(peer) {
|
||||
peer.sendMessage(messages.GetData(m.inventory));
|
||||
});
|
||||
peer.on('tx', function(m) {
|
||||
self.bus.process(m.transaction);
|
||||
self.bus.process(m.transaction)
|
||||
.catch(function(err) {
|
||||
self.stop(err);
|
||||
});
|
||||
//.catch(self.stop.bind(self));
|
||||
});
|
||||
peer.on('block', function(m) {
|
||||
self.bus.process(m.block);
|
||||
self.bus.process(m.block)
|
||||
.catch(function(err) {
|
||||
self.stop(err);
|
||||
});
|
||||
//.catch(self.stop.bind(self));
|
||||
});
|
||||
peer.on('error', function(err) {
|
||||
self.emit('error', err);
|
||||
self.stop(err);
|
||||
});
|
||||
peer.on('disconnect', function() {
|
||||
self.emit('disconnect');
|
||||
@ -55,11 +65,15 @@ NetworkMonitor.prototype.setupPeer = function(peer) {
|
||||
NetworkMonitor.prototype.start = function() {
|
||||
this.peer.connect();
|
||||
};
|
||||
NetworkMonitor.prototype.stop = function() {
|
||||
NetworkMonitor.prototype.stop = function(reason) {
|
||||
this.peer.disconnect();
|
||||
if (reason) {
|
||||
throw reason;
|
||||
}
|
||||
};
|
||||
|
||||
NetworkMonitor.prototype.syncFrom = function(start) {
|
||||
$.checkArgument(_.isString(start), 'start must be a block hash string');
|
||||
this.peer.sendMessage(messages.GetBlocks([start]));
|
||||
};
|
||||
|
||||
|
||||
@ -6,9 +6,10 @@ var Promise = require('bluebird');
|
||||
var RPC = require('bitcoind-rpc');
|
||||
var TransactionService = require('./transaction');
|
||||
var bitcore = require('bitcore');
|
||||
var Transaction = bitcore.Transaction;
|
||||
var config = require('config');
|
||||
|
||||
var BitcoreNode = require('../../');
|
||||
var errors = require('../errors');
|
||||
|
||||
var $ = bitcore.util.preconditions;
|
||||
var JSUtil = bitcore.util.js;
|
||||
@ -36,11 +37,11 @@ var helper = function(index) {
|
||||
};
|
||||
|
||||
var Index = {
|
||||
timestamp: 'bts-', // bts-<timestamp> -> hash for the block that was mined at this TS
|
||||
prev: 'prev-', // prev-<hash> -> parent hash
|
||||
next: 'nxt-', // nxt-<hash> -> hash for the next block in the main chain that is a child
|
||||
height: 'bh-', // bh-<hash> -> height (-1 means disconnected)
|
||||
tip: 'tip' // tip -> { hash: hex, height: int }, the latest tip
|
||||
timestamp: 'bts-', // bts-<timestamp> -> hash for the block that was mined at this TS
|
||||
prev: 'prev-', // prev-<hash> -> parent hash
|
||||
next: 'nxt-', // nxt-<hash> -> hash for the next block in the main chain that is a child
|
||||
height: 'bh-', // bh-<hash> -> height (-1 means disconnected)
|
||||
tip: 'tip' // tip -> { hash: hex, height: int }, the latest tip
|
||||
};
|
||||
_.extend(Index, {
|
||||
getNextBlock: helper(Index.next),
|
||||
@ -51,7 +52,7 @@ _.extend(Index, {
|
||||
}
|
||||
});
|
||||
|
||||
function BlockService (opts) {
|
||||
function BlockService(opts) {
|
||||
opts = _.extend({}, opts);
|
||||
this.database = opts.database || Promise.promisifyAll(new LevelUp(config.get('LevelUp')));
|
||||
this.rpc = opts.rpc || Promise.promisifyAll(new RPC(config.get('RPC')));
|
||||
@ -93,10 +94,10 @@ BlockService.prototype.unlock = function() {
|
||||
* merkle root hash
|
||||
* @return {bitcore.Block}
|
||||
*/
|
||||
BlockService.blockRPCtoBitcore = function(blockData, transactions) {
|
||||
$.checkArgument(_.all(transactions, function(transaction) {
|
||||
return transaction instanceof bitcore.Transaction;
|
||||
}), 'All transactions must be instances of bitcore.Transaction');
|
||||
BlockService.blockRPCtoBitcore = function(blockData) {
|
||||
console.log(blockData);
|
||||
$.checkArgument(blockData, 'blockData is required');
|
||||
$.checkArgument(blockData.previousblockhash, 'blockData.previousblockhash is required');
|
||||
var block = new bitcore.Block({
|
||||
header: new bitcore.BlockHeader({
|
||||
version: blockData.version,
|
||||
@ -107,13 +108,12 @@ BlockService.blockRPCtoBitcore = function(blockData, transactions) {
|
||||
time: blockData.time,
|
||||
nonce: blockData.nonce,
|
||||
bits: new bitcore.deps.bnjs(
|
||||
new bitcore.deps.Buffer(blockData.bits, 'hex')
|
||||
new bitcore.deps.Buffer(blockData.bits, 'hex')
|
||||
),
|
||||
merkleRoot: bitcore.util.buffer.reverse(
|
||||
new bitcore.deps.Buffer(blockData.merkleroot, 'hex')
|
||||
)
|
||||
}),
|
||||
transactions: transactions
|
||||
});
|
||||
block.height = blockData.height;
|
||||
return block;
|
||||
@ -126,8 +126,7 @@ BlockService.blockRPCtoBitcore = function(blockData, transactions) {
|
||||
* @return {Promise} a promise that will always be rejected
|
||||
*/
|
||||
var blockNotFound = function(err) {
|
||||
console.log(err, err.stack);
|
||||
return Promise.reject(new BitcoreNode.errors.Blocks.NotFound());
|
||||
throw new errors.Blocks.NotFound(err);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -136,7 +135,7 @@ var blockNotFound = function(err) {
|
||||
* @param {string} blockHash the hash of the block to be fetched
|
||||
* @return {Promise<Block>}
|
||||
*/
|
||||
BlockService.prototype.getBlock = function(blockHash) {
|
||||
BlockService.prototype.getBlock = function(blockHash, opts) {
|
||||
$.checkArgument(
|
||||
JSUtil.isHexa(blockHash) || bitcore.util.buffer.isBuffer(blockHash),
|
||||
'Block hash must be a buffer or hexa'
|
||||
@ -144,27 +143,33 @@ BlockService.prototype.getBlock = function(blockHash) {
|
||||
if (bitcore.util.buffer.isBuffer(blockHash)) {
|
||||
blockHash = bitcore.util.buffer.reverse(blockHash).toString('hex');
|
||||
}
|
||||
opts = opts || {};
|
||||
|
||||
var blockData;
|
||||
var self = this;
|
||||
|
||||
return Promise.try(function() {
|
||||
return self.rpc.getBlockAsync(blockHash);
|
||||
})
|
||||
.catch(blockNotFound)
|
||||
.then(function(block) {
|
||||
|
||||
return self.rpc.getBlockAsync(blockHash);
|
||||
blockData = block.result;
|
||||
|
||||
}).then(function(block) {
|
||||
if (opts.withoutTransactions) {
|
||||
return [];
|
||||
}
|
||||
|
||||
blockData = block.result;
|
||||
return Promise.all(blockData.tx.map(function(txId) {
|
||||
return self.transactionService.getTransaction(txId);
|
||||
}));
|
||||
return Promise.all(blockData.tx.map(function(txId) {
|
||||
return self.transactionService.getTransaction(txId);
|
||||
}));
|
||||
|
||||
}).then(function(transactions) {
|
||||
}).then(function(transactions) {
|
||||
|
||||
blockData.transactions = transactions;
|
||||
return BlockService.blockRPCtoBitcore(blockData);
|
||||
blockData.transactions = transactions;
|
||||
return BlockService.blockRPCtoBitcore(blockData);
|
||||
|
||||
}).catch(blockNotFound);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@ -182,11 +187,13 @@ BlockService.prototype.getBlockByHeight = function(height) {
|
||||
|
||||
return self.rpc.getBlockHash(height);
|
||||
|
||||
}).then(function(blockHash) {
|
||||
})
|
||||
.catch(blockNotFound)
|
||||
.then(function(blockHash) {
|
||||
|
||||
return self.getBlock(blockHash);
|
||||
|
||||
}).catch(blockNotFound);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@ -222,6 +229,15 @@ BlockService.prototype.getLatest = function() {
|
||||
*/
|
||||
BlockService.prototype.onBlock = function(block) {
|
||||
console.log('block', block);
|
||||
var events = [];
|
||||
return this.save(block)
|
||||
.then(function(block) {
|
||||
return undefined;
|
||||
block.transactions.forEach(function(tx) {
|
||||
events.push(tx);
|
||||
});
|
||||
return events;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@ -256,7 +272,9 @@ BlockService.prototype._confirmBlock = function(block) {
|
||||
}).then(function() {
|
||||
|
||||
if (block.header.prevHash.toString('hex') !== NULLBLOCKHASH) {
|
||||
return self.getBlock(block.header.prevHash);
|
||||
return self.getBlock(block.header.prevHash, {
|
||||
withoutTransactions: true
|
||||
});
|
||||
} else {
|
||||
return GENESISPARENT;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user