Skip to content

Commit 45584ab

Browse files
committed
Better UTXO fetching
- add fetching-utxo percent signal - reload UTXOs only on demand - sweepAll dialog opens only if hw/rpc connected - permit broadcast of undecoded TX
1 parent c6197f4 commit 45584ab

File tree

5 files changed

+84
-45
lines changed

5 files changed

+84
-45
lines changed

src/mainWindow.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class MainWindow(QWidget):
3838
# signal: RPC list has been reloaded (emitted by updateRPClist)
3939
sig_RPClistReloaded = pyqtSignal()
4040

41+
# signal: UTXO list loading percent (emitted by load_utxos_thread in tabRewards)
42+
sig_UTXOsLoading = pyqtSignal(int)
43+
4144
# signal: UTXO list has been reloaded (emitted by load_utxos_thread in tabRewards)
4245
sig_UTXOsLoaded = pyqtSignal()
4346

src/qt/dlg_sweepAll.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ def __init__(self, main_tab):
2727
self.setupUI()
2828
# Connect GUI buttons
2929
self.connectButtons()
30+
# Connect reloadUTXO signal
31+
self.main_tab.caller.sig_UTXOsLoading.connect(self.update_loading_utxos)
3032
# Connect reloadUTXO signal
3133
self.main_tab.caller.sig_UTXOsLoaded.connect(self.display_utxos)
3234

3335

3436
# Called each time before exec_ in showDialog
3537
def load_data(self):
38+
# clear table
39+
self.ui.tableW.setRowCount(0)
3640
# load last used destination from cache
3741
self.ui.edt_destination.setText(self.main_tab.caller.parent.cache.get("lastAddress"))
3842
# load useSwiftX check from cache
@@ -189,7 +193,7 @@ def onButtonSend(self):
189193

190194
except Exception as e:
191195
err_msg = "Exception in onButtonSend"
192-
printException(getCallerName(), getFunctionName(), err_msg, e.args)
196+
printException(getCallerName(), getFunctionName(), err_msg, e)
193197

194198

195199

@@ -204,32 +208,32 @@ def AbortSend(self):
204208
# Activated by signal sigTxdone from hwdevice
205209
def FinishSend(self, serialized_tx, amount_to_send):
206210
self.AbortSend()
207-
QApplication.processEvents()
208211
if not self.txFinished:
209212
try:
210213
self.txFinished = True
211-
self.close()
212214
tx_hex = serialized_tx.hex()
213215
printDbg("Raw signed transaction: " + tx_hex)
214216
printDbg("Amount to send :" + amount_to_send)
215217

216218
if len(tx_hex) > 90000:
217219
mess = "Transaction's length exceeds 90000 bytes. Select less UTXOs and try again."
218-
myPopUp_sb(self.main_tab.caller, "warn", 'transaction Warning', mess)
220+
myPopUp_sb(self.main_tab.caller, "crit", 'transaction Warning', mess)
219221

220222
else:
221223
decodedTx = self.main_tab.caller.rpcClient.decodeRawTransaction(tx_hex)
222-
if decodedTx is None:
223-
raise Exception("Unable to decode TX - connection to RPC server lost.")
224-
destination = decodedTx.get("vout")[0].get("scriptPubKey").get("addresses")[0]
225-
amount = decodedTx.get("vout")[0].get("value")
226-
message = '<p>Broadcast signed transaction?</p><p>Destination address:<br><b>%s</b></p>' % destination
227-
message += '<p>Amount: <b>%s</b> PIV<br>' % str(amount)
228-
message += 'Fees: <b>%s</b> PIV <br>Size: <b>%d</b> Bytes</p>' % (str(round(self.currFee / 1e8, 8) ), len(tx_hex)/2)
229-
224+
if decodedTx is not None:
225+
destination = decodedTx.get("vout")[0].get("scriptPubKey").get("addresses")[0]
226+
amount = decodedTx.get("vout")[0].get("value")
227+
message = '<p>Broadcast signed transaction?</p><p>Destination address:<br><b>%s</b></p>' % destination
228+
message += '<p>Amount: <b>%s</b> PIV<br>' % str(amount)
229+
message += 'Fees: <b>%s</b> PIV <br>Size: <b>%d</b> Bytes</p>' % (str(round(self.currFee / 1e8, 8) ), len(tx_hex)/2)
230+
else:
231+
message = '<p>Unable to decode TX- Broadcast anyway?</p>'
232+
230233
mess1 = QMessageBox(QMessageBox.Information, 'Send transaction', message)
231234
mess1.setDetailedText(json.dumps(decodedTx, indent=4, sort_keys=False))
232235
mess1.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
236+
233237
reply = mess1.exec_()
234238
if reply == QMessageBox.Yes:
235239
txid = self.main_tab.caller.rpcClient.sendRawTransaction(tx_hex, self.useSwiftX())
@@ -248,6 +252,9 @@ def FinishSend(self, serialized_tx, amount_to_send):
248252
except Exception as e:
249253
err_msg = "Exception in FinishSend"
250254
printException(getCallerName(), getFunctionName(), err_msg, e)
255+
256+
finally:
257+
self.accept()
251258

252259

253260

@@ -265,7 +272,15 @@ def updateFee(self):
265272
self.ui.feeLine.setEnabled(False)
266273
else:
267274
self.ui.feeLine.setValue(self.suggestedFee)
268-
self.ui.feeLine.setEnabled(True)
275+
self.ui.feeLine.setEnabled(True)
276+
277+
278+
279+
def update_loading_utxos(self, percent):
280+
self.ui.lblMessage.setVisible(True)
281+
self.ui.lblMessage.setText("Loading rewards...%d%%" % percent)
282+
283+
269284

270285

271286

@@ -331,10 +346,8 @@ def setupUi(self, SweepAllDlg):
331346
self.loadingLine.hide()
332347
self.loadingLinePercent.hide()
333348
myForm.addRow(QLabel("Total Rewards: "), hBox)
334-
hBox = QHBoxLayout()
335349
self.noOfUtxosLine = QLabel("<b>0</b>")
336-
hBox.addWidget(self.noOfUtxosLine)
337-
myForm.addRow(QLabel("Total number of UTXOs: "), hBox)
350+
myForm.addRow(QLabel("Total number of UTXOs: "), self.noOfUtxosLine)
338351
hBox = QHBoxLayout()
339352
self.edt_destination = QLineEdit()
340353
self.edt_destination.setToolTip("PIVX address to transfer rewards to")

src/tabMain.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ def onStartMN(self, data=None):
254254

255255

256256
def onSweepAllRewards(self):
257+
if not self.caller.rpcConnected or self.caller.hwStatus != 2:
258+
myPopUp_sb(self.caller, "crit", 'SPMT - hw/rpc device check', "Connect to RPC server and HW device first")
259+
return None
257260
try:
258261
self.sweepAllDlg.showDialog()
259262

src/tabRewards.py

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def __init__(self, caller):
6464
self.caller.hwdevice.sigTxdone.connect(self.FinishSend)
6565
self.caller.hwdevice.sigTxabort.connect(self.onCancel)
6666
self.caller.hwdevice.tx_progress.connect(self.updateProgressPercent)
67+
self.caller.sig_UTXOsLoading.connect(self.update_loading_utxos)
6768
self.caller.sig_UTXOsLoaded.connect(self.display_mn_utxos)
6869

6970

@@ -210,6 +211,8 @@ def load_utxos_thread(self, ctrl):
210211

211212
self.apiConnected = True
212213

214+
total_num_of_utxos = 0
215+
mn_rewards = {}
213216
for mn in self.caller.masternode_list:
214217
# Load UTXOs from API client
215218
rewards = self.caller.apiClient.getAddressUtxos(
@@ -218,29 +221,42 @@ def load_utxos_thread(self, ctrl):
218221
if rewards is None:
219222
printError('API client not responding.')
220223
return
221-
222-
# for each UTXO
223-
for utxo in rewards:
224-
# get raw TX from RPC client
225-
rawtx = self.caller.rpcClient.getRawTransaction(utxo['tx_hash'])
226-
227-
# Don't save UTXO if raw TX is unavailable
228-
if rawtx is None:
229-
printError("Unable to get raw TX with hash=%s from RPC server" % utxo['tx_hash'])
230-
continue
231-
232-
# Add mn_name and raw_tx to UTXO and save it to DB
233-
else:
234-
utxo['mn_name'] = mn['name']
235-
utxo['raw_tx'] = rawtx
236-
self.caller.parent.db.addReward(utxo)
237-
224+
225+
mn_rewards[mn['name']] = rewards
226+
total_num_of_utxos += len(rewards)
227+
228+
printDbg("Number of UTXOs to load: %d" % total_num_of_utxos)
229+
curr_utxo = 0
230+
percent = 0
231+
232+
for mn in mn_rewards:
233+
# for each UTXO
234+
for utxo in mn_rewards[mn]:
235+
percent = int(100*curr_utxo / total_num_of_utxos)
236+
# get raw TX from RPC client
237+
rawtx = self.caller.rpcClient.getRawTransaction(utxo['tx_hash'])
238+
239+
# Don't save UTXO if raw TX is unavailable
240+
if rawtx is None:
241+
printError("Unable to get raw TX with hash=%s from RPC server" % utxo['tx_hash'])
242+
continue
243+
244+
# Add mn_name and raw_tx to UTXO and save it to DB
245+
else:
246+
utxo['mn_name'] = mn
247+
utxo['raw_tx'] = rawtx
248+
self.caller.parent.db.addReward(utxo)
249+
250+
# emit percent
251+
self.caller.sig_UTXOsLoading.emit(percent)
252+
curr_utxo += 1
253+
254+
self.caller.sig_UTXOsLoading.emit(100)
238255
printDbg("--# REWARDS table updated")
239256
self.utxoLoaded = True
240257
self.caller.sig_UTXOsLoaded.emit()
241258

242259

243-
244260

245261
def onCancel(self):
246262
self.ui.rewardsList.box.clearSelection()
@@ -422,23 +438,24 @@ def FinishSend(self, serialized_tx, amount_to_send):
422438

423439
else:
424440
decodedTx = self.caller.rpcClient.decodeRawTransaction(tx_hex)
425-
if decodedTx is None:
426-
raise Exception("Unable to decode TX - connection to RPC server lost.")
427-
destination = decodedTx.get("vout")[0].get("scriptPubKey").get("addresses")[0]
428-
amount = decodedTx.get("vout")[0].get("value")
429-
message = '<p>Broadcast signed transaction?</p><p>Destination address:<br><b>%s</b></p>' % destination
430-
message += '<p>Amount: <b>%s</b> PIV<br>' % str(amount)
431-
message += 'Fees: <b>%s</b> PIV <br>Size: <b>%d</b> Bytes</p>' % (str(round(self.currFee / 1e8, 8) ), len(tx_hex)/2)
432-
441+
if decodedTx is not None:
442+
destination = decodedTx.get("vout")[0].get("scriptPubKey").get("addresses")[0]
443+
amount = decodedTx.get("vout")[0].get("value")
444+
message = '<p>Broadcast signed transaction?</p><p>Destination address:<br><b>%s</b></p>' % destination
445+
message += '<p>Amount: <b>%s</b> PIV<br>' % str(amount)
446+
message += 'Fees: <b>%s</b> PIV <br>Size: <b>%d</b> Bytes</p>' % (str(round(self.currFee / 1e8, 8) ), len(tx_hex)/2)
447+
else:
448+
message = '<p>Unable to decode TX- Broadcast anyway?</p>'
449+
433450
mess1 = QMessageBox(QMessageBox.Information, 'Send transaction', message)
434451
mess1.setDetailedText(json.dumps(decodedTx, indent=4, sort_keys=False))
435452
mess1.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
453+
436454
reply = mess1.exec_()
437455
if reply == QMessageBox.Yes:
438456
txid = self.caller.rpcClient.sendRawTransaction(tx_hex, self.useSwiftX())
439457
if txid is None:
440458
raise Exception("Unable to send TX - connection to RPC server lost.")
441-
442459
mess2_text = "<p>Transaction successfully sent.</p>"
443460
mess2 = QMessageBox(QMessageBox.Information, 'transaction Sent', mess2_text)
444461
mess2.setDetailedText(txid)
@@ -508,6 +525,9 @@ def updateSelection(self, clicked_item=None):
508525
self.updateFee()
509526

510527

528+
529+
def update_loading_utxos(self, percent):
530+
self.ui.resetStatusLabel('<em><b style="color:purple">Checking explorer... %d%%</b></em>' % percent)
511531

512532

513533

src/version.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"number": "0.4.0",
3-
"tag": "e",
4-
"comments": ["bugfixes"]
3+
"tag": "f",
4+
"comments": ["better UTXO fetching"]
55
}

0 commit comments

Comments
 (0)