Fix to locator
This commit is contained in:
parent
acfbef8a33
commit
1d7ff62a9e
@ -5,12 +5,16 @@ var $ = bitcore.util.preconditions;
|
||||
var _ = bitcore.deps._;
|
||||
|
||||
function BlockChain() {
|
||||
this.tip = null;
|
||||
this.tip = '0000000000000000000000000000000000000000000000000000000000000000';
|
||||
this.work = {
|
||||
'0000000000000000000000000000000000000000000000000000000000000000': 0
|
||||
};
|
||||
this.height = {};
|
||||
this.hashByHeight = {};
|
||||
this.height = {
|
||||
'0000000000000000000000000000000000000000000000000000000000000000': -1
|
||||
};
|
||||
this.hashByHeight = {
|
||||
'-1': '0000000000000000000000000000000000000000000000000000000000000000'
|
||||
};
|
||||
this.next = {};
|
||||
this.prev = {};
|
||||
}
|
||||
@ -44,7 +48,7 @@ BlockChain.prototype.proposeNewBlock = function(block) {
|
||||
$.checkArgument(block instanceof bitcore.Block, 'Argument is not a Block instance');
|
||||
var prevHash = bitcore.util.buffer.reverse(block.header.prevHash).toString('hex');
|
||||
|
||||
if (!this.work[prevHash]) {
|
||||
if (_.isUndefined(this.work[prevHash])) {
|
||||
throw new Error('No previous data to estimate work');
|
||||
}
|
||||
this.addData(block);
|
||||
@ -118,7 +122,9 @@ BlockChain.prototype.getBlockLocator = function() {
|
||||
var currentHeight = this.height[this.tip];
|
||||
var exponentialBackOff = 1;
|
||||
for (var i = 0; i < 10; i++) {
|
||||
result.push(this.hashByHeight[currentHeight--]);
|
||||
if (currentHeight >= 0) {
|
||||
result.push(this.hashByHeight[currentHeight--]);
|
||||
}
|
||||
}
|
||||
while (currentHeight > 0) {
|
||||
result.push(this.hashByHeight[currentHeight]);
|
||||
|
||||
@ -65,8 +65,9 @@ NetworkMonitor.prototype.setupPeer = function(peer) {
|
||||
};
|
||||
|
||||
NetworkMonitor.prototype.requestBlocks = function(start) {
|
||||
$.checkArgument(_.isString(start), 'start must be a block hash string');
|
||||
this.peer.sendMessage(messages.GetBlocks([start]));
|
||||
$.checkArgument(_.isArray(start) ||
|
||||
_.isString(start), 'start must be a block hash string or array');
|
||||
this.peer.sendMessage(messages.GetBlocks(_.isArray(start) ? start : [start]));
|
||||
};
|
||||
|
||||
NetworkMonitor.prototype.start = function() {
|
||||
|
||||
11
lib/node.js
11
lib/node.js
@ -48,12 +48,12 @@ var BitcoreNode = function(bus, networkMonitor, blockService, transactionService
|
||||
}
|
||||
|
||||
var blockchainChanges = self.blockchain.proposeNewBlock(block);
|
||||
Promise.series(blockchainChanges.unconfirmed.map(function(hash) {
|
||||
Promise.each(blockchainChanges.unconfirmed, function(hash) {
|
||||
return self.blockService.unconfirm(self.blockCache[hash]);
|
||||
}))
|
||||
.then(Promise.series(blockchainChanges.confirmed.map(function(hash) {
|
||||
})
|
||||
.then(Promise.each(blockchainChanges.confirmed, function(hash) {
|
||||
return self.blockService.confirm(self.blockCache[hash]);
|
||||
})))
|
||||
}))
|
||||
.then(function() {
|
||||
self.networkMonitor.requestBlocks(self.blockchain.getBlockLocator());
|
||||
});
|
||||
@ -105,10 +105,11 @@ BitcoreNode.create = function(opts) {
|
||||
|
||||
BitcoreNode.prototype.start = function() {
|
||||
var self = this;
|
||||
var genesis = bitcore.Block.fromBuffer(genesisBlocks[bitcore.Networks.defaultNetwork.name]);
|
||||
this.blockService.getBlockchain().then(function(blockchain) {
|
||||
if (!blockchain) {
|
||||
self.blockchain = new BlockChain();
|
||||
self.blockchain.setTip(genesis);
|
||||
self.blockchain.proposeNewBlock(genesis);
|
||||
}
|
||||
self.sync();
|
||||
self.networkMonitor.start();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user