towards sync
This commit is contained in:
parent
b3ad83b9ee
commit
b8a08d05a8
@ -60,7 +60,7 @@ BlockChain.prototype.proposeNewBlock = function(block) {
|
||||
var commonAncestor;
|
||||
|
||||
var pointer = block.hash;
|
||||
while (!this.height[pointer]) {
|
||||
while (_.isUndefined(this.height[pointer])) {
|
||||
toConfirm.push(pointer);
|
||||
pointer = this.prev[pointer];
|
||||
}
|
||||
@ -116,7 +116,7 @@ BlockChain.prototype.unconfirm = function(hash) {
|
||||
|
||||
BlockChain.prototype.getBlockLocator = function() {
|
||||
$.checkState(this.tip);
|
||||
$.checkState(this.height[this.tip]);
|
||||
$.checkState(!_.isUndefined(this.height[this.tip]));
|
||||
|
||||
var result = [];
|
||||
var currentHeight = this.height[this.tip];
|
||||
@ -135,7 +135,7 @@ BlockChain.prototype.getBlockLocator = function() {
|
||||
};
|
||||
|
||||
BlockChain.prototype.hasData = function(hash) {
|
||||
return !!this.prev[hash];
|
||||
return !_.isUndefined(this.work[hash]);
|
||||
};
|
||||
|
||||
BlockChain.prototype.prune = function() {
|
||||
|
||||
@ -39,6 +39,7 @@ NetworkMonitor.prototype.setupPeer = function(peer) {
|
||||
self.emit('ready');
|
||||
});
|
||||
peer.on('inv', function(m) {
|
||||
self.emit('inv', m.inventory);
|
||||
// TODO only ask for data if tx or block is unknown
|
||||
peer.sendMessage(messages.GetData(m.inventory));
|
||||
});
|
||||
@ -66,7 +67,7 @@ NetworkMonitor.prototype.setupPeer = function(peer) {
|
||||
|
||||
NetworkMonitor.prototype.requestBlocks = function(start) {
|
||||
$.checkArgument(_.isArray(start) ||
|
||||
_.isString(start), 'start must be a block hash string or array');
|
||||
_.isString(start), 'start must be a block hash string or array');
|
||||
this.peer.sendMessage(messages.GetBlocks(_.isArray(start) ? start : [start]));
|
||||
};
|
||||
|
||||
|
||||
25
lib/node.js
25
lib/node.js
@ -4,6 +4,7 @@ var util = require('util');
|
||||
var EventEmitter = require('eventemitter2').EventEmitter2;
|
||||
|
||||
var bitcore = require('bitcore');
|
||||
var _ = bitcore.deps._;
|
||||
var config = require('config');
|
||||
var p2p = require('bitcore-p2p');
|
||||
var messages = new p2p.Messages();
|
||||
@ -36,12 +37,27 @@ var BitcoreNode = function(bus, networkMonitor, blockService, transactionService
|
||||
this.blockService = blockService;
|
||||
|
||||
this.blockCache = {};
|
||||
this.inventory = {}; // blockHash -> bool (has data)
|
||||
|
||||
|
||||
this.networkMonitor.on('inv', function(inventory) {
|
||||
_.each(inventory, function(info) {
|
||||
var hash = bitcore.util.buffer.reverse(info.hash).toString('hex');
|
||||
$.checkState(_.isUndefined(self.inventory[hash]));
|
||||
if (info.type === 2) { // TODO: use static field from bitcore-p2p
|
||||
self.inventory[hash] = false;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
this.bus.register(bitcore.Block, function(block) {
|
||||
|
||||
console.log('Block', block.id);
|
||||
var prevHash = bitcore.util.buffer.reverse(block.header.prevHash).toString('hex');
|
||||
self.blockCache[block.hash] = block;
|
||||
self.inventory[block.hash] = true;
|
||||
console.log('prevHash', prevHash);
|
||||
console.log('height', self.blockchain.height[self.blockchain.tip]);
|
||||
|
||||
if (!self.blockchain.hasData(prevHash)) {
|
||||
self.networkMonitor.requestBlocks(self.blockchain.getBlockLocator());
|
||||
@ -49,7 +65,6 @@ var BitcoreNode = function(bus, networkMonitor, blockService, transactionService
|
||||
}
|
||||
|
||||
var blockchainChanges = self.blockchain.proposeNewBlock(block);
|
||||
console.log('changes', blockchainChanges);
|
||||
Promise.each(blockchainChanges.unconfirmed, function(hash) {
|
||||
return self.blockService.unconfirm(self.blockCache[hash]);
|
||||
})
|
||||
@ -59,7 +74,11 @@ var BitcoreNode = function(bus, networkMonitor, blockService, transactionService
|
||||
}));
|
||||
})
|
||||
.then(function() {
|
||||
self.networkMonitor.requestBlocks(self.blockchain.getBlockLocator());
|
||||
if (_.size(self.inventory) && _.all(_.values(self.inventory))) {
|
||||
self.inventory = {};
|
||||
console.log('requesting ...', self.blockchain.getBlockLocator().length);
|
||||
self.networkMonitor.requestBlocks(self.blockchain.getBlockLocator());
|
||||
}
|
||||
})
|
||||
.catch(function(error) {
|
||||
self.stop(error);
|
||||
@ -115,7 +134,7 @@ BitcoreNode.prototype.start = function() {
|
||||
this.blockService.getBlockchain().then(function(blockchain) {
|
||||
if (!blockchain) {
|
||||
self.blockchain = new BlockChain();
|
||||
self.blockchain.proposeNewBlock(genesis);
|
||||
self.bus.process(genesis);
|
||||
}
|
||||
self.sync();
|
||||
self.networkMonitor.start();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user