|
|
@ -29,10 +29,12 @@ function spec() { |
|
|
|
}; |
|
|
|
|
|
|
|
Sync.prototype.getPrevNextBlock = function(blockHash, blockEnd, opts, cb) { |
|
|
|
|
|
|
|
var that = this; |
|
|
|
|
|
|
|
// recursion end.
|
|
|
|
if (!blockHash || (blockEnd && blockEnd == blockHash) ) { |
|
|
|
console.log("Reach end:", blockHash, blockEnd); |
|
|
|
return cb(); |
|
|
|
} |
|
|
|
|
|
|
@ -100,8 +102,9 @@ function spec() { |
|
|
|
console.log("ERROR: @%s: %s [count: block_count: %d]", blockHash, err, that.block_count); |
|
|
|
|
|
|
|
if (blockInfo && blockInfo.result) { |
|
|
|
if (opts.prev && blockInfo.result.prevblockhash) |
|
|
|
return that.getPrevNextBlock(blockInfo.result.prevblockhash, blockEnd, opts, cb); |
|
|
|
if (opts.prev && blockInfo.result.previousblockhash) { |
|
|
|
return that.getPrevNextBlock(blockInfo.result.previousblockhash, blockEnd, opts, cb); |
|
|
|
} |
|
|
|
|
|
|
|
if (opts.next && blockInfo.result.nextblockhash) |
|
|
|
return that.getPrevNextBlock(blockInfo.result.nextblockhash, blockEnd, opts, cb); |
|
|
@ -113,30 +116,53 @@ function spec() { |
|
|
|
Sync.prototype.storeBlock = function(block, cb) { |
|
|
|
var that = this; |
|
|
|
|
|
|
|
Block.customCreate(block, function(err, b){ |
|
|
|
Block.customCreate(block, function(err, block, inserted_txs){ |
|
|
|
|
|
|
|
if (b && that.opts.broadcast_blocks) { |
|
|
|
sockets.broadcast_block(b); |
|
|
|
if (block && that.opts.broadcast_blocks) { |
|
|
|
sockets.broadcast_block(block); |
|
|
|
} |
|
|
|
|
|
|
|
if (that.opts.broadcast_txs) { |
|
|
|
block.tx.each(function(tx) { |
|
|
|
sockets.broadcast_tx(new_tx); |
|
|
|
if (inserted_txs && that.opts.broadcast_txs) { |
|
|
|
inserted_txs.forEach(function(tx) { |
|
|
|
sockets.broadcast_tx(tx); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
that.tx_count += block.tx.length; |
|
|
|
if (inserted_txs) |
|
|
|
that.tx_count += inserted_txs.length; |
|
|
|
|
|
|
|
return cb(); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
Sync.prototype.syncBlocks = function(start, end, cb) { |
|
|
|
|
|
|
|
Sync.prototype.storeTxs = function(txs, inTime, cb) { |
|
|
|
var that = this; |
|
|
|
|
|
|
|
console.log('Syncing Blocks, starting from: %s end: %s ',start, end); |
|
|
|
var time = inTime ? inTime : Math.round(new Date().getTime() / 1000); |
|
|
|
|
|
|
|
return that.getPrevNextBlock(start, end, { next: 1 }, cb); |
|
|
|
Transaction.createFromArray(txs, time, function(err, inserted_txs) { |
|
|
|
if (!err && inserted_txs && that.opts.broadcast_txs) { |
|
|
|
|
|
|
|
inserted_txs.forEach(function(tx) { |
|
|
|
sockets.broadcast_tx(tx); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
return cb(err); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Sync.prototype.syncBlocks = function(start, end, isForward, cb) { |
|
|
|
var that = this; |
|
|
|
|
|
|
|
console.log('Syncing Blocks, starting \n\tfrom: %s \n\tend: %s \n\tisForward:', |
|
|
|
start, end, isForward); |
|
|
|
|
|
|
|
|
|
|
|
return that.getPrevNextBlock( start, end, |
|
|
|
isForward ? { next: 1 } : { prev: 1}, cb); |
|
|
|
}; |
|
|
|
|
|
|
|
// This is not currently used. Transactions are represented by txid only
|
|
|
@ -266,6 +292,8 @@ function spec() { |
|
|
|
var retry_attemps = 100; |
|
|
|
var retry_secs = 2; |
|
|
|
|
|
|
|
var block_best; |
|
|
|
|
|
|
|
this.db.once('open', function() { |
|
|
|
async.series([ |
|
|
|
function(cb) { |
|
|
@ -273,7 +301,7 @@ function spec() { |
|
|
|
console.log('Deleting Blocks...'); |
|
|
|
that.db.collections.blocks.drop(cb); |
|
|
|
} else { |
|
|
|
cb(); |
|
|
|
return cb(); |
|
|
|
} |
|
|
|
}, |
|
|
|
function(cb) { |
|
|
@ -281,7 +309,7 @@ function spec() { |
|
|
|
console.log('Deleting TXs...'); |
|
|
|
that.db.collections.transactions.drop(cb); |
|
|
|
} else { |
|
|
|
cb(); |
|
|
|
return cb(); |
|
|
|
} |
|
|
|
}, |
|
|
|
function(cb) { |
|
|
@ -289,7 +317,7 @@ function spec() { |
|
|
|
console.log('Deleting TXItems...'); |
|
|
|
that.db.collections.transactionitems.drop(cb); |
|
|
|
} else { |
|
|
|
cb(); |
|
|
|
return cb(); |
|
|
|
} |
|
|
|
}, |
|
|
|
function(cb) { |
|
|
@ -301,11 +329,34 @@ function spec() { |
|
|
|
}); |
|
|
|
}, |
|
|
|
function(cb) { |
|
|
|
if (!opts.reverse) return cb(); |
|
|
|
|
|
|
|
that.rpc.getBestBlockHash(function(err, res) { |
|
|
|
if (err) cb(err); |
|
|
|
|
|
|
|
block_best = res.result; |
|
|
|
return cb(); |
|
|
|
}); |
|
|
|
}, |
|
|
|
], function(err) { |
|
|
|
|
|
|
|
|
|
|
|
function sync() { |
|
|
|
|
|
|
|
var startingBlockHash = that.network.genesisBlock.hash.reverse().toString('hex'); |
|
|
|
var start, end, isForward; |
|
|
|
|
|
|
|
if (opts.reverse) { |
|
|
|
start = block_best; |
|
|
|
end = that.network.genesisBlock.hash.reverse().toString('hex'); |
|
|
|
isForward = false; |
|
|
|
} |
|
|
|
else { |
|
|
|
start = that.network.genesisBlock.hash.reverse().toString('hex'); |
|
|
|
end = null; |
|
|
|
isForward = true; |
|
|
|
} |
|
|
|
|
|
|
|
that.syncBlocks( startingBlockHash, null, function(err) { |
|
|
|
that.syncBlocks(start, end, isForward, function(err) { |
|
|
|
|
|
|
|
if (err && err.message.match(/ECONNREFUSED/) && retry_attemps--){ |
|
|
|
setTimeout(function() { |
|
|
@ -321,9 +372,6 @@ function spec() { |
|
|
|
if (!opts.skip_blocks) { |
|
|
|
sync(); |
|
|
|
} |
|
|
|
}, |
|
|
|
], function(err) { |
|
|
|
return next(err, that.block_count); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|