p2p sync algorithm working
This commit is contained in:
parent
b8a08d05a8
commit
9f290649e6
@ -65,10 +65,11 @@ NetworkMonitor.prototype.setupPeer = function(peer) {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
NetworkMonitor.prototype.requestBlocks = function(start) {
|
NetworkMonitor.prototype.requestBlocks = function(locator) {
|
||||||
$.checkArgument(_.isArray(start) ||
|
$.checkArgument(_.isArray(locator) && _.isString(locator[0]), 'start must be a block hash string array');
|
||||||
_.isString(start), 'start must be a block hash string or array');
|
this.peer.sendMessage(messages.GetBlocks({
|
||||||
this.peer.sendMessage(messages.GetBlocks(_.isArray(start) ? start : [start]));
|
starts: locator
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
NetworkMonitor.prototype.start = function() {
|
NetworkMonitor.prototype.start = function() {
|
||||||
|
|||||||
36
lib/node.js
36
lib/node.js
@ -6,8 +6,6 @@ var EventEmitter = require('eventemitter2').EventEmitter2;
|
|||||||
var bitcore = require('bitcore');
|
var bitcore = require('bitcore');
|
||||||
var _ = bitcore.deps._;
|
var _ = bitcore.deps._;
|
||||||
var config = require('config');
|
var config = require('config');
|
||||||
var p2p = require('bitcore-p2p');
|
|
||||||
var messages = new p2p.Messages();
|
|
||||||
var $ = bitcore.util.preconditions;
|
var $ = bitcore.util.preconditions;
|
||||||
var Promise = require('bluebird');
|
var Promise = require('bluebird');
|
||||||
var RPC = require('bitcoind-rpc');
|
var RPC = require('bitcoind-rpc');
|
||||||
@ -39,30 +37,27 @@ var BitcoreNode = function(bus, networkMonitor, blockService, transactionService
|
|||||||
this.blockCache = {};
|
this.blockCache = {};
|
||||||
this.inventory = {}; // blockHash -> bool (has data)
|
this.inventory = {}; // blockHash -> bool (has data)
|
||||||
|
|
||||||
|
/*
|
||||||
this.networkMonitor.on('inv', function(inventory) {
|
this.networkMonitor.on('inv', function(inventory) {
|
||||||
_.each(inventory, function(info) {
|
_.each(inventory, function(info) {
|
||||||
var hash = bitcore.util.buffer.reverse(info.hash).toString('hex');
|
var hash = bitcore.util.buffer.reverse(info.hash).toString('hex');
|
||||||
$.checkState(_.isUndefined(self.inventory[hash]));
|
$.checkState(_.isUndefined(self.inventory[hash]), hash);
|
||||||
if (info.type === 2) { // TODO: use static field from bitcore-p2p
|
if (self.inventory[hash] && info.type === 2) { // TODO: use static field from bitcore-p2p
|
||||||
self.inventory[hash] = false;
|
self.inventory[hash] = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
this.bus.register(bitcore.Block, function(block) {
|
this.bus.register(bitcore.Block, function(block) {
|
||||||
|
|
||||||
console.log('Block', block.id);
|
|
||||||
var prevHash = bitcore.util.buffer.reverse(block.header.prevHash).toString('hex');
|
var prevHash = bitcore.util.buffer.reverse(block.header.prevHash).toString('hex');
|
||||||
self.blockCache[block.hash] = block;
|
self.blockCache[block.hash] = block;
|
||||||
self.inventory[block.hash] = true;
|
self.inventory[block.hash] = true;
|
||||||
console.log('prevHash', prevHash);
|
|
||||||
console.log('height', self.blockchain.height[self.blockchain.tip]);
|
|
||||||
|
|
||||||
if (!self.blockchain.hasData(prevHash)) {
|
if (!self.blockchain.hasData(prevHash)) {
|
||||||
self.networkMonitor.requestBlocks(self.blockchain.getBlockLocator());
|
self.requestFromTip();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
console.log('block', block.id, 'height', self.blockchain.height[self.blockchain.tip] + 1);
|
||||||
|
|
||||||
var blockchainChanges = self.blockchain.proposeNewBlock(block);
|
var blockchainChanges = self.blockchain.proposeNewBlock(block);
|
||||||
Promise.each(blockchainChanges.unconfirmed, function(hash) {
|
Promise.each(blockchainChanges.unconfirmed, function(hash) {
|
||||||
@ -70,14 +65,15 @@ var BitcoreNode = function(bus, networkMonitor, blockService, transactionService
|
|||||||
})
|
})
|
||||||
.then(function() {
|
.then(function() {
|
||||||
return Promise.all(blockchainChanges.confirmed.map(function(hash) {
|
return Promise.all(blockchainChanges.confirmed.map(function(hash) {
|
||||||
|
self.blockCache[hash].height = self.blockchain.height[hash];
|
||||||
return self.blockService.confirm(self.blockCache[hash]);
|
return self.blockService.confirm(self.blockCache[hash]);
|
||||||
}));
|
}));
|
||||||
})
|
})
|
||||||
.then(function() {
|
.then(function() {
|
||||||
if (_.size(self.inventory) && _.all(_.values(self.inventory))) {
|
// TODO: include this
|
||||||
|
if (false && _.size(self.inventory) && _.all(_.values(self.inventory))) {
|
||||||
self.inventory = {};
|
self.inventory = {};
|
||||||
console.log('requesting ...', self.blockchain.getBlockLocator().length);
|
self.requestFromTip();
|
||||||
self.networkMonitor.requestBlocks(self.blockchain.getBlockLocator());
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(function(error) {
|
.catch(function(error) {
|
||||||
@ -98,6 +94,11 @@ var BitcoreNode = function(bus, networkMonitor, blockService, transactionService
|
|||||||
};
|
};
|
||||||
util.inherits(BitcoreNode, EventEmitter);
|
util.inherits(BitcoreNode, EventEmitter);
|
||||||
|
|
||||||
|
BitcoreNode.prototype.requestFromTip = function() {
|
||||||
|
var locator = this.blockchain.getBlockLocator();
|
||||||
|
this.networkMonitor.requestBlocks(locator);
|
||||||
|
};
|
||||||
|
|
||||||
BitcoreNode.create = function(opts) {
|
BitcoreNode.create = function(opts) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
|
|
||||||
@ -152,12 +153,7 @@ BitcoreNode.prototype.stop = function(reason) {
|
|||||||
BitcoreNode.prototype.sync = function() {
|
BitcoreNode.prototype.sync = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.networkMonitor.on('ready', function() {
|
this.networkMonitor.on('ready', function() {
|
||||||
self.blockService.getBlockchain().then(function(blockchain) {
|
self.networkMonitor.requestBlocks(self.blockchain.getBlockLocator());
|
||||||
self.networkMonitor.requestBlocks(self.blockchain.getBlockLocator());
|
|
||||||
}).catch(function(err) {
|
|
||||||
self.networkMonitor.stop();
|
|
||||||
throw err;
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -249,22 +249,9 @@ BlockService.prototype.confirm = function(block) {
|
|||||||
return self._setNextBlock(ops, block.header.prevHash, block);
|
return self._setNextBlock(ops, block.header.prevHash, block);
|
||||||
|
|
||||||
}).then(function() {
|
}).then(function() {
|
||||||
//console.log(2);
|
|
||||||
|
|
||||||
if (block.header.prevHash.toString('hex') !== NULLBLOCKHASH) {
|
|
||||||
//console.log(2.1);
|
|
||||||
return self.getBlock(block.header.prevHash, {
|
|
||||||
withoutTransactions: true
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
//console.log(2.2);
|
|
||||||
return GENESISPARENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
}).then(function(parent) {
|
|
||||||
//console.log(3);
|
//console.log(3);
|
||||||
|
|
||||||
return self._setBlockHeight(ops, block, parent.height + 1);
|
return self._setBlockHeight(ops, block, block.height);
|
||||||
|
|
||||||
}).then(function() {
|
}).then(function() {
|
||||||
//console.log(4);
|
//console.log(4);
|
||||||
@ -310,7 +297,6 @@ BlockService.prototype._setNextBlock = function(ops, prevBlockHash, block) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
BlockService.prototype._setBlockHeight = function(ops, block, height) {
|
BlockService.prototype._setBlockHeight = function(ops, block, height) {
|
||||||
block.height = height;
|
|
||||||
return Promise.try(function() {
|
return Promise.try(function() {
|
||||||
ops.push({
|
ops.push({
|
||||||
type: 'put',
|
type: 'put',
|
||||||
|
|||||||
@ -179,6 +179,7 @@ TransactionService.prototype._getAddressForInput = function(input) {
|
|||||||
hash, bitcore.Networks.defaultNetwork, bitcore.Address.PayToPublicKeyHash
|
hash, bitcore.Networks.defaultNetwork, bitcore.Address.PayToPublicKeyHash
|
||||||
);
|
);
|
||||||
} else if (script.isPublicKeyIn()) {
|
} else if (script.isPublicKeyIn()) {
|
||||||
|
/*
|
||||||
return self.getTransaction(input.prevTxId.toString('hex')).then(function(transaction) {
|
return self.getTransaction(input.prevTxId.toString('hex')).then(function(transaction) {
|
||||||
var outputScript = transaction.outputs[input.outputIndex].script;
|
var outputScript = transaction.outputs[input.outputIndex].script;
|
||||||
if (outputScript.isPublicKeyOut()) {
|
if (outputScript.isPublicKeyOut()) {
|
||||||
@ -189,6 +190,7 @@ TransactionService.prototype._getAddressForInput = function(input) {
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
} else {
|
} else {
|
||||||
return new bitcore.Script(script.chunks[script.chunks.length - 1]).toAddress();
|
return new bitcore.Script(script.chunks[script.chunks.length - 1]).toAddress();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user