@@ -29,10 +29,12 @@ function spec() {
29
29
} ;
30
30
31
31
Sync . prototype . getPrevNextBlock = function ( blockHash , blockEnd , opts , cb ) {
32
+
32
33
var that = this ;
33
34
34
35
// recursion end.
35
36
if ( ! blockHash || ( blockEnd && blockEnd == blockHash ) ) {
37
+ console . log ( "Reach end:" , blockHash , blockEnd ) ;
36
38
return cb ( ) ;
37
39
}
38
40
@@ -100,8 +102,9 @@ function spec() {
100
102
console . log ( "ERROR: @%s: %s [count: block_count: %d]" , blockHash , err , that . block_count ) ;
101
103
102
104
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
+ }
105
108
106
109
if ( opts . next && blockInfo . result . nextblockhash )
107
110
return that . getPrevNextBlock ( blockInfo . result . nextblockhash , blockEnd , opts , cb ) ;
@@ -113,30 +116,53 @@ function spec() {
113
116
Sync . prototype . storeBlock = function ( block , cb ) {
114
117
var that = this ;
115
118
116
- Block . customCreate ( block , function ( err , b ) {
119
+ Block . customCreate ( block , function ( err , block , inserted_txs ) {
117
120
118
- if ( b && that . opts . broadcast_blocks ) {
119
- sockets . broadcast_block ( b ) ;
121
+ if ( block && that . opts . broadcast_blocks ) {
122
+ sockets . broadcast_block ( block ) ;
120
123
}
121
124
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 ) ;
125
128
} ) ;
126
129
}
127
130
128
- that . tx_count += block . tx . length ;
131
+ if ( inserted_txs )
132
+ that . tx_count += inserted_txs . length ;
129
133
130
134
return cb ( ) ;
131
135
} ) ;
132
136
} ;
133
137
134
- Sync . prototype . syncBlocks = function ( start , end , cb ) {
138
+
139
+ Sync . prototype . storeTxs = function ( txs , inTime , cb ) {
135
140
var that = this ;
136
141
137
- console . log ( 'Syncing Blocks, starting from: %s end: %s ' , start , end ) ;
142
+ var time = inTime ? inTime : Math . round ( new Date ( ) . getTime ( ) / 1000 ) ;
138
143
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 ) ;
140
166
} ;
141
167
142
168
// This is not currently used. Transactions are represented by txid only
@@ -266,30 +292,32 @@ function spec() {
266
292
var retry_attemps = 100 ;
267
293
var retry_secs = 2 ;
268
294
295
+ var block_best ;
296
+
269
297
this . db . once ( 'open' , function ( ) {
270
298
async . series ( [
271
299
function ( cb ) {
272
300
if ( opts . destroy ) {
273
301
console . log ( 'Deleting Blocks...' ) ;
274
302
that . db . collections . blocks . drop ( cb ) ;
275
303
} else {
276
- cb ( ) ;
304
+ return cb ( ) ;
277
305
}
278
306
} ,
279
307
function ( cb ) {
280
308
if ( opts . destroy ) {
281
309
console . log ( 'Deleting TXs...' ) ;
282
310
that . db . collections . transactions . drop ( cb ) ;
283
311
} else {
284
- cb ( ) ;
312
+ return cb ( ) ;
285
313
}
286
314
} ,
287
315
function ( cb ) {
288
316
if ( opts . destroy ) {
289
317
console . log ( 'Deleting TXItems...' ) ;
290
318
that . db . collections . transactionitems . drop ( cb ) ;
291
319
} else {
292
- cb ( ) ;
320
+ return cb ( ) ;
293
321
}
294
322
} ,
295
323
function ( cb ) {
@@ -301,11 +329,34 @@ function spec() {
301
329
} ) ;
302
330
} ,
303
331
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
+
304
344
function sync ( ) {
305
345
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
+ }
307
358
308
- that . syncBlocks ( startingBlockHash , null , function ( err ) {
359
+ that . syncBlocks ( start , end , isForward , function ( err ) {
309
360
310
361
if ( err && err . message . match ( / E C O N N R E F U S E D / ) && retry_attemps -- ) {
311
362
setTimeout ( function ( ) {
@@ -321,9 +372,6 @@ function spec() {
321
372
if ( ! opts . skip_blocks ) {
322
373
sync ( ) ;
323
374
}
324
- } ,
325
- ] , function ( err ) {
326
- return next ( err , that . block_count ) ;
327
375
} ) ;
328
376
} ) ;
329
377
} ;
0 commit comments