Skip to content

Commit 3a2d0fd

Browse files
committed
fix tx_handle and -R in sync
1 parent 2aefe71 commit 3a2d0fd

File tree

5 files changed

+77
-27
lines changed

5 files changed

+77
-27
lines changed

app/models/Block.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var BlockSchema = new Schema({
2626
unique: true,
2727
},
2828
time: Number,
29+
nextBlockHash: String,
2930
});
3031

3132
/**
@@ -52,12 +53,13 @@ BlockSchema.statics.customCreate = function(block, cb) {
5253

5354
newBlock.time = block.time ? block.time : Math.round(new Date().getTime() / 1000);
5455
newBlock.hash = block.hash;
56+
newBlock.nextBlockHash = block.nextBlockHash;
5557

5658
Transaction.createFromArray(block.tx, newBlock.time, function(err, inserted_txs) {
5759
if (err) return cb(err);
5860

5961
newBlock.save(function(err) {
60-
return cb(err, inserted_txs);
62+
return cb(err, newBlock, inserted_txs);
6163
});
6264
});
6365
};

lib/PeerSync.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function spec() {
5757
PeerSync.prototype.handle_tx = function(info) {
5858
var tx = info.message.tx.getStandardizedObject();
5959
console.log('[p2p_sync] Handle tx: ' + tx.hash);
60-
this.sync.storeTxs([tx.hash], function(err) {
60+
this.sync.storeTxs([tx.hash], null, function(err) {
6161
if (err) {
6262
console.log('[p2p_sync] Error in handle TX: ' + err);
6363
}
@@ -78,6 +78,7 @@ function spec() {
7878
this.sync.storeBlock({
7979
'hash': blockHash,
8080
'tx': tx_hashes,
81+
// TODO NEXT BLOCK / PREV BLOCK?
8182
},
8283
function(err) {
8384
if (err) {

lib/Sync.js

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ function spec() {
2929
};
3030

3131
Sync.prototype.getPrevNextBlock = function(blockHash, blockEnd, opts, cb) {
32+
3233
var that = this;
3334

3435
// recursion end.
3536
if (!blockHash || (blockEnd && blockEnd == blockHash) ) {
37+
console.log("Reach end:", blockHash, blockEnd);
3638
return cb();
3739
}
3840

@@ -100,8 +102,9 @@ function spec() {
100102
console.log("ERROR: @%s: %s [count: block_count: %d]", blockHash, err, that.block_count);
101103

102104
if (blockInfo && blockInfo.result) {
103-
if (opts.prev && blockInfo.result.prevblockhash)
104-
return that.getPrevNextBlock(blockInfo.result.prevblockhash, blockEnd, opts, cb);
105+
if (opts.prev && blockInfo.result.previousblockhash) {
106+
return that.getPrevNextBlock(blockInfo.result.previousblockhash, blockEnd, opts, cb);
107+
}
105108

106109
if (opts.next && blockInfo.result.nextblockhash)
107110
return that.getPrevNextBlock(blockInfo.result.nextblockhash, blockEnd, opts, cb);
@@ -113,30 +116,53 @@ function spec() {
113116
Sync.prototype.storeBlock = function(block, cb) {
114117
var that = this;
115118

116-
Block.customCreate(block, function(err, b){
119+
Block.customCreate(block, function(err, block, inserted_txs){
117120

118-
if (b && that.opts.broadcast_blocks) {
119-
sockets.broadcast_block(b);
121+
if (block && that.opts.broadcast_blocks) {
122+
sockets.broadcast_block(block);
120123
}
121124

122-
if (that.opts.broadcast_txs) {
123-
block.tx.each(function(tx) {
124-
sockets.broadcast_tx(new_tx);
125+
if (inserted_txs && that.opts.broadcast_txs) {
126+
inserted_txs.forEach(function(tx) {
127+
sockets.broadcast_tx(tx);
125128
});
126129
}
127130

128-
that.tx_count += block.tx.length;
131+
if (inserted_txs)
132+
that.tx_count += inserted_txs.length;
129133

130134
return cb();
131135
});
132136
};
133137

134-
Sync.prototype.syncBlocks = function(start, end, cb) {
138+
139+
Sync.prototype.storeTxs = function(txs, inTime, cb) {
135140
var that = this;
136141

137-
console.log('Syncing Blocks, starting from: %s end: %s ',start, end);
142+
var time = inTime ? inTime : Math.round(new Date().getTime() / 1000);
138143

139-
return that.getPrevNextBlock(start, end, { next: 1 }, cb);
144+
Transaction.createFromArray(txs, time, function(err, inserted_txs) {
145+
if (!err && inserted_txs && that.opts.broadcast_txs) {
146+
147+
inserted_txs.forEach(function(tx) {
148+
sockets.broadcast_tx(tx);
149+
});
150+
}
151+
152+
return cb(err);
153+
});
154+
};
155+
156+
157+
Sync.prototype.syncBlocks = function(start, end, isForward, cb) {
158+
var that = this;
159+
160+
console.log('Syncing Blocks, starting \n\tfrom: %s \n\tend: %s \n\tisForward:',
161+
start, end, isForward);
162+
163+
164+
return that.getPrevNextBlock( start, end,
165+
isForward ? { next: 1 } : { prev: 1}, cb);
140166
};
141167

142168
// This is not currently used. Transactions are represented by txid only
@@ -266,30 +292,32 @@ function spec() {
266292
var retry_attemps = 100;
267293
var retry_secs = 2;
268294

295+
var block_best;
296+
269297
this.db.once('open', function() {
270298
async.series([
271299
function(cb) {
272300
if (opts.destroy) {
273301
console.log('Deleting Blocks...');
274302
that.db.collections.blocks.drop(cb);
275303
} else {
276-
cb();
304+
return cb();
277305
}
278306
},
279307
function(cb) {
280308
if (opts.destroy) {
281309
console.log('Deleting TXs...');
282310
that.db.collections.transactions.drop(cb);
283311
} else {
284-
cb();
312+
return cb();
285313
}
286314
},
287315
function(cb) {
288316
if (opts.destroy) {
289317
console.log('Deleting TXItems...');
290318
that.db.collections.transactionitems.drop(cb);
291319
} else {
292-
cb();
320+
return cb();
293321
}
294322
},
295323
function(cb) {
@@ -301,11 +329,34 @@ function spec() {
301329
});
302330
},
303331
function(cb) {
332+
if (!opts.reverse) return cb();
333+
334+
that.rpc.getBestBlockHash(function(err, res) {
335+
if (err) cb(err);
336+
337+
block_best = res.result;
338+
return cb();
339+
});
340+
},
341+
], function(err) {
342+
343+
304344
function sync() {
305345

306-
var startingBlockHash = that.network.genesisBlock.hash.reverse().toString('hex');
346+
var start, end, isForward;
347+
348+
if (opts.reverse) {
349+
start = block_best;
350+
end = that.network.genesisBlock.hash.reverse().toString('hex');
351+
isForward = false;
352+
}
353+
else {
354+
start = that.network.genesisBlock.hash.reverse().toString('hex');
355+
end = null;
356+
isForward = true;
357+
}
307358

308-
that.syncBlocks( startingBlockHash, null, function(err) {
359+
that.syncBlocks(start, end, isForward, function(err) {
309360

310361
if (err && err.message.match(/ECONNREFUSED/) && retry_attemps--){
311362
setTimeout(function() {
@@ -321,9 +372,6 @@ function spec() {
321372
if (!opts.skip_blocks) {
322373
sync();
323374
}
324-
},
325-
], function(err) {
326-
return next(err, that.block_count);
327375
});
328376
});
329377
};

util/get_block.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var block_hash = process.argv[2] || '0000000000b6288775bbd326bedf324ca8717a15191
1111

1212
var rpc = new RpcClient(config.bitcoind);
1313

14-
var block = rpc.getBlock(block_hash, function(err, block) {
14+
var block = rpc.getBestBlockHash( function(err, block) {
1515

1616
console.log("Err:");
1717
console.log(err);

util/sync.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ var async = require('async');
1414
program
1515
.version(SYNC_VERSION)
1616
.option('-N --network [livenet]', 'Set bitcoin network [testnet]', 'testnet')
17-
.option('-D --destroy', 'Remove current DB (and start from there)', '0')
18-
.option('--skip_blocks', 'Sync blocks')
19-
.option('--skip_txs', 'Sync transactions')
17+
.option('-D --destroy', 'Remove current DB (and start from there)', 0)
18+
.option('-R --reverse', 'Sync backwards', 0)
2019
.parse(process.argv);
2120

2221
var sync = new Sync({
@@ -38,7 +37,7 @@ function(cb) {
3837
console.log('CRITICAL ERROR: ', err);
3938
}
4039
else {
41-
console.log('Done! [%d blocks]', count);
40+
console.log('Done! [%d blocks]', count, err);
4241
}
4342
cb();
4443
});

0 commit comments

Comments
 (0)