1
- var UI = ( function ( UI , $ , undefined ) {
2
- function getFirstConsistentTail ( tails , i , inconsistentTails ) {
3
- if ( ! tails [ i ] ) {
4
- return Promise . resolve ( false )
5
- }
6
- return iota . api . isPromotable ( tails [ i ] . hash ) . then ( state => {
7
- if ( state && isAboveMaxDepth ( tails [ i ] ) ) {
8
- return tails [ i ]
9
- }
1
+ function isAboveMaxDepth ( tx ) {
2
+ if ( ! tx ) {
3
+ return false
4
+ }
10
5
11
- inconsistentTails . add ( tails [ i ] . hash )
6
+ if ( tx . attachmentTimestamp > Date . now ( ) ) {
7
+ return false
8
+ }
12
9
13
- return getFirstConsistentTail ( tails , i ++ , inconsistentTails )
14
- } )
10
+ return ( Date . now ( ) - parseInt ( tx . attachmentTimestamp ) ) < ( 11 * 60 * 1000 )
11
+ }
12
+
13
+ function getPromotableTail ( tails , i ) {
14
+ if ( ! Number . isInteger ( i ) ) {
15
+ i = 0
15
16
}
16
17
17
- function isAboveMaxDepth ( tx ) {
18
- if ( tx . attachmentTimestamp > Date . now ( ) ) {
19
- return false
20
- }
21
- return ( Date . now ( ) - parseInt ( tx . attachmentTimestamp ) ) < ( 11 * 60 * 1000 )
18
+ if ( i === 0 ) {
19
+ tails = tails . filter ( tx => isAboveMaxDepth ( tx ) ) . sort ( ( a , b ) => b . attachmentTimestamp - a . attachmentTimestamp )
22
20
}
23
21
24
- const TTL = 30 * 60 * 1000
22
+ if ( ! tails [ i ] ) {
23
+ return Promise . resolve ( false )
24
+ }
25
25
26
- function hasTimeToLive ( origin ) {
27
- if ( ! origin ) {
28
- return false
26
+ return iota . api . isPromotable ( tails [ i ] . hash ) . then ( state => {
27
+ if ( state && isAboveMaxDepth ( tails [ i ] ) ) {
28
+ return tails [ i ]
29
29
}
30
- if ( origin . attachmentTimestamp > Date . now ( ) ) {
31
- return false
32
- }
33
- return ( Date . now ( ) - origin . attachmentTimestamp ) < TTL
34
- }
30
+
31
+ return getPromotableTail ( tails , ++ i )
32
+ } ) . catch ( ( ) => {
33
+ return false
34
+ } )
35
+ }
36
+
37
+ var UI = ( function ( UI , $ , undefined ) {
35
38
36
39
UI . handleHistory = function ( ) {
37
40
var modal ;
38
41
39
42
const bundlesToTailsMap = new Map ( )
40
- const bundlesToOriginsMap = new Map ( )
41
- const promotableTailsMap = new Map ( )
42
- const inconsistentTails = new Set ( )
43
-
44
- let _isRenderingModal = false
45
43
46
44
$ ( "#history-stack" ) . on ( "click" , ".show-bundle" , function ( e ) {
47
45
e . preventDefault ( ) ;
48
46
49
- if ( _isRenderingModal ) {
50
- return false
51
- }
52
-
53
- _isRenderingModal = true
54
-
55
47
var hash = $ ( this ) . closest ( "li" ) . data ( "hash" ) ;
56
48
var bundleHash = $ ( this ) . closest ( "li" ) . data ( "bundle" )
57
49
var $modal = $ ( "#bundle-modal" ) ;
@@ -63,7 +55,6 @@ var UI = (function(UI, $, undefined) {
63
55
64
56
iota . api . getBundle ( hash , ( error , transactions ) => {
65
57
if ( error ) {
66
- _isRenderingModal = false
67
58
68
59
return
69
60
}
@@ -117,7 +108,6 @@ var UI = (function(UI, $, undefined) {
117
108
118
109
modal = $modal . remodal ( options ) ;
119
110
modal . open ( ) ;
120
- _isRenderingModal = false
121
111
}
122
112
123
113
if ( persistence ) {
@@ -127,30 +117,15 @@ var UI = (function(UI, $, undefined) {
127
117
} else {
128
118
iota . api . findTransactionObjects ( { bundles : [ transactions [ 0 ] . bundle ] } , ( err , txs ) => {
129
119
if ( err ) {
130
- _isRenderingModal = false
131
-
132
120
return
133
121
}
134
122
135
123
const bundleHash = txs [ 0 ] . bundle
136
- const consistentTail = bundlesToTailsMap . get ( bundleHash )
124
+ const tail = bundlesToTailsMap . get ( bundleHash )
137
125
let tails = txs . filter ( tx => tx . currentIndex === 0 )
138
126
139
- if ( consistentTail && consistentTail . bundle === bundleHash ) {
140
- tails = tails . filter ( tx => tx . hash !== consistentTail . hash )
141
- tails . unshift ( consistentTail )
142
- }
143
-
144
- if ( ! promotableTailsMap . has ( bundleHash ) ) {
145
- promotableTailsMap . set ( bundleHash , tails )
146
- }
147
-
148
- let promotableTails = promotableTailsMap . get ( bundleHash ) || [ ]
149
-
150
127
iota . api . getLatestInclusion ( tails . map ( tx => tx . hash ) , ( err , inclusionStates ) => {
151
128
if ( err ) {
152
- _isRenderingModal = false
153
-
154
129
return
155
130
}
156
131
@@ -163,32 +138,20 @@ var UI = (function(UI, $, undefined) {
163
138
}
164
139
165
140
renderBundleModal ( persistence , false , false , status )
166
- } else if ( consistentTail &&
167
- ! inconsistentTails . has ( consistentTail . hash ) &&
168
- isAboveMaxDepth ( consistentTail ) &&
169
- hasTimeToLive ( bundlesToOriginsMap . get ( bundleHash ) ) ) {
141
+ } else if ( tail && isAboveMaxDepth ( tail ) ) {
170
142
renderBundleModal ( false , true , false , 'pending' )
171
143
} else {
172
- promotableTails = promotableTails . filter ( tx => ! inconsistentTails . has ( tx . hash ) && isAboveMaxDepth ( tx ) )
173
-
174
- getFirstConsistentTail ( promotableTails , 0 , inconsistentTails )
144
+ getPromotableTail ( tails , 0 )
175
145
. then ( consistentTail => {
176
146
if ( consistentTail ) {
177
147
bundlesToTailsMap . set ( bundleHash , consistentTail )
178
-
179
- if ( ! bundlesToOriginsMap . has ( bundleHash ) ) {
180
- bundlesToOriginsMap . set ( consistentTail )
181
- }
182
-
183
148
renderBundleModal ( false , true , false , 'pending' )
184
149
} else {
185
150
bundlesToTailsMap . delete ( bundleHash )
186
- promotableTails = [ ]
187
-
188
151
renderBundleModal ( false , false , true , 'pending' )
189
152
}
190
153
} ) . catch ( ( ) => {
191
- _isRenderingModal = false
154
+ renderBundleModal ( false , false , true , 'pending' )
192
155
} )
193
156
}
194
157
} )
@@ -202,7 +165,6 @@ var UI = (function(UI, $, undefined) {
202
165
203
166
const hash = $ ( this ) . data ( "hash" ) ;
204
167
const bundleHash = $ ( this ) . data ( "bundle" )
205
- let promotableTails = promotableTailsMap . get ( bundleHash ) || [ ]
206
168
207
169
if ( ! hash ) {
208
170
console . log ( "UI.reattach/rebroadcast: No hash" ) ;
@@ -275,31 +237,32 @@ var UI = (function(UI, $, undefined) {
275
237
$ ( '#reattach-btn' ) . removeAttr ( 'disabled' )
276
238
}
277
239
278
- function _promote ( tail ) {
240
+ function _promote ( tail , count , i , skipCheck ) {
241
+ console . log ( tail )
279
242
UI . isDoingPOW = true
280
243
281
244
const spamTransfer = [ { address : '9' . repeat ( 81 ) , value : 0 , message : '' , tag : '' } ]
282
245
283
- if ( ! isAboveMaxDepth ( tail ) ) {
284
- promotableTails = promotableTails . filter ( tx => ! inconsistentTails . has ( tx . hash ) && isAboveMaxDepth ( tx ) )
246
+ if ( ! skipCheck && ! isAboveMaxDepth ( tail ) ) {
247
+ _resetUI ( 'promote_bellow_max_depth_error' )
285
248
286
- return getFirstConsistentTail ( promotableTails , 0 , inconsistentTails )
287
- . then ( newConsistentTail => {
288
- if ( newConsistentTail && hasTimeToLive ( bundlesToOriginsMap . get ( bundleHash ) ) ) {
289
- bundlesToTailsMap . set ( bundleHash , newConsistentTail )
249
+ bundlesToTailsMap . delete ( bundleHash )
290
250
291
- setTimeout ( ( ) => _promote ( newConsistentTail ) , 0 )
292
- } else {
293
- _resetUI ( 'promote_bellow_max_depth_error' )
251
+ $ ( '#reattach-btn' ) . show ( )
252
+ $ ( '#promote-btn' ) . loadingReset ( 'promote' )
253
+ $ ( '#promote-btn' ) . hide ( )
254
+ }
294
255
295
- bundlesToTailsMap . delete ( bundleHash )
296
- promotableTails = [ ]
256
+ $ ( '#promote-btn' ) . loadingReset ( )
297
257
298
- $ ( '#reattach-btn' ) . show ( )
299
- $ ( '#promote-btn' ) . hide ( )
300
- }
301
- } )
258
+ var $bar = $ ( '<span class="progress" style="display:block"><div class="slider"><div class="line"></div><div class="break dot1"></div><div class="break dot2"></div><div class="break dot3"></div></div></span>' )
259
+
260
+ if ( i === 1 ) {
261
+ $ ( '#promote-btn' ) . append ( $bar )
302
262
}
263
+ $ ( '#promote-btn .progress' ) . show ( )
264
+
265
+ $ ( '#promote-btn' ) . loadingUpdate ( UI . t ( 'promoting' ) + ' ' + i + '/' + count )
303
266
304
267
iota . api . promoteTransaction (
305
268
tail . hash ,
@@ -312,32 +275,23 @@ var UI = (function(UI, $, undefined) {
312
275
313
276
if ( err ) {
314
277
if ( err . message . indexOf ( 'Inconsistent subtangle' ) > - 1 ) {
315
- inconsistentTails . add ( tail . hash )
316
-
317
- promotableTails = promotableTails . filter ( tx => ! inconsistentTails . has ( tx . hash ) && isAboveMaxDepth ( tx ) )
318
-
319
- getFirstConsistentTail ( promotableTails , 0 , inconsistentTails )
320
- . then ( newConsistentTail => {
321
- if ( newConsistentTail && hasTimeToLive ( bundlesToOriginsMap . get ( bundleHash ) ) ) {
322
- bundlesToTailsMap . set ( bundleHash , newConsistentTail )
323
-
324
- setTimeout ( ( ) => _promote ( newConsistentTail ) , 0 )
325
- } else {
326
- _resetUI ( 'promote_inconsistent_subtangle_error' )
278
+ _resetUI ( 'promote_inconsistent_subtangle_error' )
327
279
328
- bundlesToTailsMap . delete ( bundleHash )
329
- promotableTails = [ ]
280
+ bundlesToTailsMap . delete ( bundleHash )
330
281
331
- $ ( '#promote-btn' ) . hide ( )
332
- $ ( '#reattach-btn' ) . show ( )
333
- }
334
- } )
282
+ $ ( '#promote-btn' ) . hide ( )
283
+ $ ( '#promote-btn' ) . loadingReset ( 'promote' )
284
+ $ ( '#reattach-btn' ) . show ( )
335
285
} else {
336
286
_resetUI ( err . message )
337
287
}
338
288
} else {
339
- bundlesToTailsMap . set ( bundleHash , res [ 0 ] )
340
- promotableTails . unshift ( res [ 0 ] )
289
+ if ( i < count ) {
290
+ setTimeout ( ( ) => _promote ( tail , count , ++ i , true ) , 1000 )
291
+ return
292
+ }
293
+
294
+ $ ( '#promote-btn .progress' ) . hide ( )
341
295
342
296
UI . updateState ( 1000 )
343
297
@@ -346,7 +300,7 @@ var UI = (function(UI, $, undefined) {
346
300
}
347
301
)
348
302
}
349
- _promote ( bundlesToTailsMap . get ( bundleHash ) )
303
+ _promote ( bundlesToTailsMap . get ( bundleHash ) , 5 , 1 , false )
350
304
} else {
351
305
UI . isDoingPOW = true ;
352
306
iota . api . replayBundle ( hash , connection . depth , connection . minWeightMagnitude , function ( error , bundle ) {
@@ -360,7 +314,7 @@ var UI = (function(UI, $, undefined) {
360
314
if ( ! UI . isFocused ( ) ) {
361
315
UI . notifyDesktop ( "transaction_reattached_successfully" ) ;
362
316
}
363
- $ ( "#reattach-btn" ) . loadingSuccess ( "reattach_completed" ) ;
317
+ // $("#reattach-btn").loadingSuccess("reattach_completed");
364
318
$ ( "#bundle-modal .persistence" ) . hide ( ) ;
365
319
366
320
$ ( '#reattach-btn' ) . hide ( )
@@ -370,8 +324,6 @@ var UI = (function(UI, $, undefined) {
370
324
UI . updateState ( 1000 ) ;
371
325
372
326
bundlesToTailsMap . set ( bundle [ 0 ] . bundle , bundle [ 0 ] )
373
- promotableTails . push ( bundle [ 0 ] )
374
- bundlesToOriginsMap . set ( bundle [ 0 ] . bundle , bundle [ 0 ] )
375
327
}
376
328
377
329
UI . isLocked = false ;
0 commit comments