Skip to content
This repository was archived by the owner on Feb 26, 2021. It is now read-only.

Commit 1297b86

Browse files
authored
Merge pull request #815 from chrisdukakis/hotfix/promote
Promotion feature fixes & improvements
2 parents 445ae8e + 86e64ac commit 1297b86

File tree

4 files changed

+171
-274
lines changed

4 files changed

+171
-274
lines changed

locales/en/translation.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@
282282
"transaction_reattached_successfully":"Transaction reattached successfully",
283283
"transaction_promoted_successfully":"Transaction promoted",
284284
"reattach_completed":"Reattach completed",
285-
"promote_completed":"Promote transaction sent",
285+
"promote_completed":"Promotion completed",
286286
"promote_inconsistent_subtangle_error": "Cannot promote transaction. Use reattach button",
287287
"promote_bellow_max_depth_error": "Cannot promote transaction. Use reattach button",
288288
"transfer_completed":"Transfer completed",

ui/bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"remodal": "^1.0.7",
1313
"toastr": "^2.1.2",
1414
"async": "^2.1.4",
15-
"iota.lib.js": "^0.4.5",
15+
"iota.lib.js": "^0.4.6",
1616
"i18next": "^8.3.0",
1717
"i18next-xhr-backend": "^1.4.1",
1818
"jquery-i18next": "^1.2.0",

ui/js/ui.history.js

Lines changed: 63 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,49 @@
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+
}
105

11-
inconsistentTails.add(tails[i].hash)
6+
if (tx.attachmentTimestamp > Date.now()) {
7+
return false
8+
}
129

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
1516
}
1617

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)
2220
}
2321

24-
const TTL = 30 * 60 * 1000
22+
if (!tails[i]) {
23+
return Promise.resolve(false)
24+
}
2525

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]
2929
}
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) {
3538

3639
UI.handleHistory = function() {
3740
var modal;
3841

3942
const bundlesToTailsMap = new Map()
40-
const bundlesToOriginsMap = new Map()
41-
const promotableTailsMap = new Map()
42-
const inconsistentTails = new Set()
43-
44-
let _isRenderingModal = false
4543

4644
$("#history-stack").on("click", ".show-bundle", function(e) {
4745
e.preventDefault();
4846

49-
if (_isRenderingModal) {
50-
return false
51-
}
52-
53-
_isRenderingModal = true
54-
5547
var hash = $(this).closest("li").data("hash");
5648
var bundleHash = $(this).closest("li").data("bundle")
5749
var $modal = $("#bundle-modal");
@@ -63,7 +55,6 @@ var UI = (function(UI, $, undefined) {
6355

6456
iota.api.getBundle(hash, (error, transactions) => {
6557
if (error) {
66-
_isRenderingModal = false
6758

6859
return
6960
}
@@ -117,7 +108,6 @@ var UI = (function(UI, $, undefined) {
117108

118109
modal = $modal.remodal(options);
119110
modal.open();
120-
_isRenderingModal = false
121111
}
122112

123113
if (persistence) {
@@ -127,30 +117,15 @@ var UI = (function(UI, $, undefined) {
127117
} else {
128118
iota.api.findTransactionObjects({bundles: [transactions[0].bundle]}, (err, txs) => {
129119
if (err) {
130-
_isRenderingModal = false
131-
132120
return
133121
}
134122

135123
const bundleHash = txs[0].bundle
136-
const consistentTail = bundlesToTailsMap.get(bundleHash)
124+
const tail = bundlesToTailsMap.get(bundleHash)
137125
let tails = txs.filter(tx => tx.currentIndex === 0)
138126

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-
150127
iota.api.getLatestInclusion(tails.map(tx => tx.hash), (err, inclusionStates) => {
151128
if (err) {
152-
_isRenderingModal = false
153-
154129
return
155130
}
156131

@@ -163,32 +138,20 @@ var UI = (function(UI, $, undefined) {
163138
}
164139

165140
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)) {
170142
renderBundleModal(false, true, false, 'pending')
171143
} else {
172-
promotableTails = promotableTails.filter(tx => !inconsistentTails.has(tx.hash) && isAboveMaxDepth(tx))
173-
174-
getFirstConsistentTail(promotableTails, 0, inconsistentTails)
144+
getPromotableTail(tails, 0)
175145
.then(consistentTail => {
176146
if (consistentTail) {
177147
bundlesToTailsMap.set(bundleHash, consistentTail)
178-
179-
if (!bundlesToOriginsMap.has(bundleHash)) {
180-
bundlesToOriginsMap.set(consistentTail)
181-
}
182-
183148
renderBundleModal(false, true, false, 'pending')
184149
} else {
185150
bundlesToTailsMap.delete(bundleHash)
186-
promotableTails = []
187-
188151
renderBundleModal(false, false, true, 'pending')
189152
}
190153
}).catch(() => {
191-
_isRenderingModal = false
154+
renderBundleModal(false, false, true, 'pending')
192155
})
193156
}
194157
})
@@ -202,7 +165,6 @@ var UI = (function(UI, $, undefined) {
202165

203166
const hash = $(this).data("hash");
204167
const bundleHash = $(this).data("bundle")
205-
let promotableTails = promotableTailsMap.get(bundleHash) || []
206168

207169
if (!hash) {
208170
console.log("UI.reattach/rebroadcast: No hash");
@@ -275,31 +237,32 @@ var UI = (function(UI, $, undefined) {
275237
$('#reattach-btn').removeAttr('disabled')
276238
}
277239

278-
function _promote (tail) {
240+
function _promote (tail, count, i, skipCheck) {
241+
console.log(tail)
279242
UI.isDoingPOW = true
280243

281244
const spamTransfer = [{address: '9'.repeat(81), value: 0, message: '', tag: ''}]
282245

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')
285248

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)
290250

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+
}
294255

295-
bundlesToTailsMap.delete(bundleHash)
296-
promotableTails = []
256+
$('#promote-btn').loadingReset()
297257

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)
302262
}
263+
$('#promote-btn .progress').show()
264+
265+
$('#promote-btn').loadingUpdate(UI.t('promoting') + ' ' + i + '/' + count)
303266

304267
iota.api.promoteTransaction(
305268
tail.hash,
@@ -312,32 +275,23 @@ var UI = (function(UI, $, undefined) {
312275

313276
if (err) {
314277
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')
327279

328-
bundlesToTailsMap.delete(bundleHash)
329-
promotableTails = []
280+
bundlesToTailsMap.delete(bundleHash)
330281

331-
$('#promote-btn').hide()
332-
$('#reattach-btn').show()
333-
}
334-
})
282+
$('#promote-btn').hide()
283+
$('#promote-btn').loadingReset('promote')
284+
$('#reattach-btn').show()
335285
} else {
336286
_resetUI(err.message)
337287
}
338288
} 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()
341295

342296
UI.updateState(1000)
343297

@@ -346,7 +300,7 @@ var UI = (function(UI, $, undefined) {
346300
}
347301
)
348302
}
349-
_promote(bundlesToTailsMap.get(bundleHash))
303+
_promote(bundlesToTailsMap.get(bundleHash), 5, 1, false)
350304
} else {
351305
UI.isDoingPOW = true;
352306
iota.api.replayBundle(hash, connection.depth, connection.minWeightMagnitude, function(error, bundle) {
@@ -360,7 +314,7 @@ var UI = (function(UI, $, undefined) {
360314
if (!UI.isFocused()) {
361315
UI.notifyDesktop("transaction_reattached_successfully");
362316
}
363-
$("#reattach-btn").loadingSuccess("reattach_completed");
317+
//$("#reattach-btn").loadingSuccess("reattach_completed");
364318
$("#bundle-modal .persistence").hide();
365319

366320
$('#reattach-btn').hide()
@@ -370,8 +324,6 @@ var UI = (function(UI, $, undefined) {
370324
UI.updateState(1000);
371325

372326
bundlesToTailsMap.set(bundle[0].bundle, bundle[0])
373-
promotableTails.push(bundle[0])
374-
bundlesToOriginsMap.set(bundle[0].bundle, bundle[0])
375327
}
376328

377329
UI.isLocked = false;

0 commit comments

Comments
 (0)