Skip to content

Commit 6c39688

Browse files
committed
Merge pull request #61 from matiu/bug/05sync
Bug/05sync
2 parents ffce452 + 3a2d0fd commit 6c39688

File tree

9 files changed

+95
-41
lines changed

9 files changed

+95
-41
lines changed

app/models/Block.js

Lines changed: 10 additions & 5 deletions
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
/**
@@ -42,19 +43,23 @@ BlockSchema.path('title').validate(function(title) {
4243
* Statics
4344
*/
4445

45-
BlockSchema.statics.createTimestamped = function(block, cb) {
46+
BlockSchema.statics.customCreate = function(block, cb) {
4647

4748
var That= this;
48-
var now = Math.round(new Date().getTime() / 1000);
4949

5050
var BlockSchema = mongoose.model('Block', BlockSchema);
51+
5152
var newBlock = new That();
52-
newBlock.time = now;
5353

54-
Transaction.createFromArray(block.tx, function(err, inserted_txs) {
54+
newBlock.time = block.time ? block.time : Math.round(new Date().getTime() / 1000);
55+
newBlock.hash = block.hash;
56+
newBlock.nextBlockHash = block.nextBlockHash;
57+
58+
Transaction.createFromArray(block.tx, newBlock.time, function(err, inserted_txs) {
5559
if (err) return cb(err);
60+
5661
newBlock.save(function(err) {
57-
return cb(err, inserted_txs);
62+
return cb(err, newBlock, inserted_txs);
5863
});
5964
});
6065
};

app/models/Transaction.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,17 @@ TransactionSchema.statics.fromIdWithInfo = function(txid, cb) {
8787
};
8888

8989

90-
TransactionSchema.statics.createFromArray = function(txs, next) {
90+
TransactionSchema.statics.createFromArray = function(txs, time, next) {
9191
var that = this;
9292
if (!txs) return next();
9393
var mongo_txs = [];
94-
var now = Math.round(new Date().getTime() / 1000);
9594

9695
async.forEachLimit(txs, CONCURRENCY, function(txid, cb) {
9796

98-
that.explodeTransactionItems( txid, function(err) {
97+
that.explodeTransactionItems( txid, time, function(err) {
9998
if (err) return next(err);
10099

101-
that.create({txid: txid, time: now}, function(err, new_tx) {
100+
that.create({txid: txid, time: time}, function(err, new_tx) {
102101
if (err && ! err.toString().match(/E11000/)) return cb(err);
103102

104103
if (new_tx) mongo_txs.push(new_tx);
@@ -112,7 +111,7 @@ TransactionSchema.statics.createFromArray = function(txs, next) {
112111
};
113112

114113

115-
TransactionSchema.statics.explodeTransactionItems = function(txid, cb) {
114+
TransactionSchema.statics.explodeTransactionItems = function(txid, time, cb) {
116115

117116
this.queryInfo(txid, function(err, info) {
118117
if (err || !info) return cb(err);
@@ -131,7 +130,7 @@ TransactionSchema.statics.explodeTransactionItems = function(txid, cb) {
131130
value_sat : -1 * i.valueSat,
132131
addr : i.addr,
133132
index : i.n,
134-
ts : info.time,
133+
ts : time,
135134
}, next_in);
136135
}
137136
else {
@@ -155,7 +154,7 @@ TransactionSchema.statics.explodeTransactionItems = function(txid, cb) {
155154
value_sat : o.valueSat,
156155
addr : o.scriptPubKey.addresses[0],
157156
index : o.n,
158-
ts : info.time,
157+
ts : time,
159158
}, next_out);
160159
}
161160
else {

config/env/development.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
pass: process.env.BITCOIND_PASS || 'real_mystery',
1212
host: process.env.BITCOIND_HOST || '127.0.0.1',
1313
port: process.env.BITCOIND_PORT || '18332',
14+
disableAgent: true,
1415
},
1516
network: 'testnet',
1617
}

config/env/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
pass: process.env.BITCOIND_PASS || 'real_mystery',
1313
host: process.env.BITCOIND_HOST || '127.0.0.1',
1414
port: process.env.BITCOIND_PORT || '18332',
15-
keepConnectionAlive: false,
15+
disableAgent: true,
1616
},
1717
network: 'testnet',
1818
}

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: 69 additions & 21 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.createTimestamped(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
@@ -227,7 +253,7 @@ function spec() {
227253
}
228254

229255
// This will trigger an RPC call
230-
Transaction.explodeTransactionItems( tx.txid, function(err) {
256+
Transaction.explodeTransactionItems( tx.txid, tx.time, function(err) {
231257
if (proc++ % 1000 === 0) progress_bar('\tproc', pull, total);
232258
next(err);
233259
});
@@ -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
};

test/model/transaction.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ describe('Transaction', function(){
118118
// Remove first
119119
TransactionItem.remove({txid: v.txid}, function(err) {
120120

121-
Transaction.explodeTransactionItems(v.txid, function(err, tx) {
121+
var now = Math.round(new Date().getTime() / 1000);
122+
Transaction.explodeTransactionItems(v.txid, now, function(err, tx) {
122123
if (err) done(err);
123124

124125
TransactionItem

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)