Skip to content

Commit b115c2d

Browse files
digitalotenalex v
authored andcommitted
Fix for importaddress returning incorrect amounts (#479)
* Fix for importaddress returning incorrect amounts * Updated tests case to test cold staking address import as well * Updated UI and functional tests to match discussed changes on PR #479 * Updated var names * Updated test case * Updated test case to test each address on different nodes * Added check for spending/staking address against watched addresses on cold staking transactions
1 parent 926c49a commit b115c2d

File tree

7 files changed

+181
-3
lines changed

7 files changed

+181
-3
lines changed

qa/pull-tester/rpc-tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
'reject-version-bit.py',
170170
'getcoldstakingaddress.py',
171171
'getstakereport.py',
172+
'importaddress.py',
172173
'getstakinginfo.py',
173174
'coldstaking_staking.py',
174175
'coldstaking_spending.py',

qa/rpc-tests/importaddress.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2018 The Navcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
from test_framework.test_framework import NavCoinTestFramework
7+
from test_framework.staticr_util import *
8+
9+
class ImportAddressTest(NavCoinTestFramework):
10+
11+
def __init__(self):
12+
super().__init__()
13+
self.setup_clean_chain = True
14+
self.num_nodes = 6
15+
16+
def setup_network(self, split=False):
17+
self.nodes = self.setup_nodes()
18+
connect_nodes(self.nodes[0], 1)
19+
connect_nodes(self.nodes[1], 2)
20+
connect_nodes(self.nodes[2], 3)
21+
connect_nodes(self.nodes[3], 4)
22+
connect_nodes(self.nodes[4], 5)
23+
connect_nodes(self.nodes[5], 0)
24+
self.is_network_split = False
25+
26+
def run_test(self):
27+
# Turn off staking until we need it
28+
self.nodes[0].staking(False)
29+
self.nodes[1].staking(False)
30+
self.nodes[2].staking(False)
31+
self.nodes[3].staking(False)
32+
self.nodes[4].staking(False)
33+
self.nodes[5].staking(False)
34+
35+
# Generate genesis block
36+
activate_staticr(self.nodes[0])
37+
self.sync_all()
38+
39+
# Create a normal address
40+
address = self.nodes[0].getnewaddress()
41+
42+
# Create a spending address
43+
spending_address = self.nodes[0].getnewaddress()
44+
45+
# Create a staking address
46+
staking_address = self.nodes[1].getnewaddress()
47+
48+
# Create the cold address
49+
coldstaking_address = self.nodes[0].getcoldstakingaddress(staking_address, spending_address)
50+
51+
# Send some nav to new addresses
52+
self.nodes[0].sendtoaddress(address, 512)
53+
self.nodes[0].sendtoaddress(coldstaking_address, 256)
54+
self.nodes[0].sendtoaddress(coldstaking_address, 128)
55+
self.nodes[0].generate(6)
56+
self.sync_all()
57+
58+
# Import address with balances
59+
self.nodes[2].importaddress(address)
60+
self.nodes[3].importaddress(spending_address)
61+
self.nodes[4].importaddress(staking_address)
62+
self.nodes[5].importaddress(coldstaking_address)
63+
64+
# Assert transactions list has the old transactions with correct amounts
65+
transactions = self.nodes[2].listtransactions("*", 1, 0, True)
66+
assert_equal(512, transactions[0]['amount'])
67+
68+
transactions = self.nodes[3].listtransactions("*", 2, 0, True)
69+
assert_equal(128, transactions[0]['amount'])
70+
assert_equal(256, transactions[1]['amount'])
71+
72+
transactions = self.nodes[4].listtransactions("*", 2, 0, True)
73+
assert_equal(128, transactions[0]['amount'])
74+
assert_equal(256, transactions[1]['amount'])
75+
76+
transactions = self.nodes[5].listtransactions("*", 2, 0, True)
77+
assert_equal(128, transactions[0]['amount'])
78+
assert_equal(256, transactions[1]['amount'])
79+
80+
# Assert total balance
81+
assert_equal(512, self.nodes[2].getbalance("*", 1, True))
82+
assert_equal(384, self.nodes[3].getbalance("*", 1, True))
83+
assert_equal(384, self.nodes[4].getbalance("*", 1, True))
84+
assert_equal(384, self.nodes[5].getbalance("*", 1, True))
85+
86+
87+
if __name__ == '__main__':
88+
ImportAddressTest().main()

src/qt/forms/overviewpage.ui

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,66 @@
349349
</property>
350350
</widget>
351351
</item>
352+
<item row="4" column="1">
353+
<widget class="QLabel" name="labelWatchedBalance">
354+
<property name="font">
355+
<font>
356+
<pointsize>10</pointsize>
357+
<weight>50</weight>
358+
<bold>false</bold>
359+
<stylestrategy>PreferAntialias</stylestrategy>
360+
</font>
361+
</property>
362+
<property name="styleSheet">
363+
<string notr="true">color: #727272</string>
364+
</property>
365+
<property name="text">
366+
<string>TextLabel</string>
367+
</property>
368+
<property name="alignment">
369+
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
370+
</property>
371+
</widget>
372+
</item>
373+
<item row="4" column="0">
374+
<widget class="QLabel" name="labelWatchedBalanceText">
375+
<property name="sizePolicy">
376+
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
377+
<horstretch>0</horstretch>
378+
<verstretch>0</verstretch>
379+
</sizepolicy>
380+
</property>
381+
<property name="minimumSize">
382+
<size>
383+
<width>70</width>
384+
<height>17</height>
385+
</size>
386+
</property>
387+
<property name="maximumSize">
388+
<size>
389+
<width>70</width>
390+
<height>17</height>
391+
</size>
392+
</property>
393+
<property name="font">
394+
<font>
395+
<pointsize>10</pointsize>
396+
<weight>50</weight>
397+
<bold>false</bold>
398+
<stylestrategy>PreferAntialias</stylestrategy>
399+
</font>
400+
</property>
401+
<property name="styleSheet">
402+
<string notr="true">color: #727272</string>
403+
</property>
404+
<property name="text">
405+
<string>Watched</string>
406+
</property>
407+
<property name="alignment">
408+
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
409+
</property>
410+
</widget>
411+
</item>
352412
<item row="2" column="1">
353413
<widget class="QLabel" name="labelStaking">
354414
<property name="font">
@@ -409,7 +469,7 @@
409469
</property>
410470
</widget>
411471
</item>
412-
<item row="4" column="1">
472+
<item row="5" column="1">
413473
<widget class="QLabel" name="labelTotal">
414474
<property name="font">
415475
<font>
@@ -430,7 +490,7 @@
430490
</property>
431491
</widget>
432492
</item>
433-
<item row="4" column="0">
493+
<item row="5" column="0">
434494
<widget class="QLabel" name="label_8">
435495
<property name="sizePolicy">
436496
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">

src/qt/overviewpage.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ OverviewPage::OverviewPage(const PlatformStyle *platformStyle, QWidget *parent)
119119
currentStakingBalance(-1),
120120
currentColdStakingBalance(-1),
121121
currentImmatureBalance(-1),
122+
currentTotalBalance(-1),
122123
currentWatchOnlyBalance(-1),
123124
currentWatchUnconfBalance(-1),
124125
currentWatchImmatureBalance(-1),
126+
currentWatchOnlyTotalBalance(-1),
125127
txdelegate(new TxViewDelegate(platformStyle)),
126128
filter(0)
127129
{
@@ -179,15 +181,18 @@ void OverviewPage::setBalance(const CAmount& balance, const CAmount& unconfirmed
179181
currentStakingBalance = stakingBalance;
180182
currentColdStakingBalance = coldStakingBalance;
181183
currentImmatureBalance = immatureBalance;
184+
currentTotalBalance = balance + unconfirmedBalance + immatureBalance;
182185
currentWatchOnlyBalance = watchOnlyBalance;
183186
currentWatchUnconfBalance = watchUnconfBalance;
184187
currentWatchImmatureBalance = watchImmatureBalance;
188+
currentWatchOnlyTotalBalance = watchOnlyBalance + watchUnconfBalance + watchImmatureBalance;
185189
ui->labelBalance->setText(NavCoinUnits::formatWithUnit(unit, balance, false, NavCoinUnits::separatorAlways));
186190
ui->labelUnconfirmed->setText(NavCoinUnits::formatWithUnit(unit, unconfirmedBalance, false, NavCoinUnits::separatorAlways));
187191
ui->labelStaking->setText(NavCoinUnits::formatWithUnit(unit, stakingBalance, false, NavCoinUnits::separatorAlways));
188192
ui->labelColdStaking->setText(NavCoinUnits::formatWithUnit(unit, coldStakingBalance, false, NavCoinUnits::separatorAlways));
189193
ui->labelImmature->setText(NavCoinUnits::formatWithUnit(unit, immatureBalance, false, NavCoinUnits::separatorAlways));
190-
ui->labelTotal->setText(NavCoinUnits::formatWithUnit(unit, balance + unconfirmedBalance + stakingBalance, false, NavCoinUnits::separatorAlways));
194+
ui->labelWatchedBalance->setText(NavCoinUnits::formatWithUnit(unit, currentWatchOnlyTotalBalance, false, NavCoinUnits::separatorAlways));
195+
ui->labelTotal->setText(NavCoinUnits::formatWithUnit(unit, currentTotalBalance + currentWatchOnlyTotalBalance, false, NavCoinUnits::separatorAlways));
191196

192197
updateStakeReportNow();
193198

@@ -201,6 +206,11 @@ void OverviewPage::setBalance(const CAmount& balance, const CAmount& unconfirmed
201206
ui->labelColdStaking->setVisible(showColdStaking);
202207
ui->labelColdStakingText->setVisible(showColdStaking);
203208

209+
bool showWatchOnly = currentWatchOnlyTotalBalance != 0;
210+
211+
ui->labelWatchedBalance->setVisible(showWatchOnly);
212+
ui->labelWatchedBalanceText->setVisible(showWatchOnly);
213+
204214
// only show immature (newly mined) balance if it's non-zero, so as not to complicate things
205215
// for the non-mining users
206216
bool showImmature = false;

src/qt/overviewpage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ public Q_SLOTS:
7575
CAmount currentStakingBalance;
7676
CAmount currentColdStakingBalance;
7777
CAmount currentImmatureBalance;
78+
CAmount currentTotalBalance;
7879
CAmount currentWatchOnlyBalance;
7980
CAmount currentWatchUnconfBalance;
8081
CAmount currentWatchImmatureBalance;
82+
CAmount currentWatchOnlyTotalBalance;
8183

8284
TxViewDelegate *txdelegate;
8385
TransactionFilterProxy *filter;

src/script/ismine.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "ismine.h"
77

8+
#include "base58.h"
89
#include "key.h"
910
#include "keystore.h"
1011
#include "script/script.h"
@@ -79,6 +80,20 @@ isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
7980
return ISMINE_SPENDABLE;
8081
else if (fStakable)
8182
return ISMINE_STAKABLE;
83+
84+
// if spending/staking address is being watched
85+
CNavCoinAddress spendingAddress(keyID);
86+
CNavCoinAddress stakingAddress(keyID2);
87+
CScript spendingScript = GetScriptForDestination(spendingAddress.Get());
88+
CScript stakingScript = GetScriptForDestination(stakingAddress.Get());
89+
SignatureData sigs;
90+
91+
if (keystore.HaveWatchOnly(spendingScript)) {
92+
return ProduceSignature(DummySignatureCreator(&keystore), spendingScript, sigs) ? ISMINE_WATCH_SOLVABLE : ISMINE_WATCH_UNSOLVABLE;
93+
} else if (keystore.HaveWatchOnly(stakingScript)) {
94+
return ProduceSignature(DummySignatureCreator(&keystore), stakingScript, sigs) ? ISMINE_WATCH_SOLVABLE : ISMINE_WATCH_UNSOLVABLE;
95+
}
96+
8297
break;
8398
}
8499
case TX_SCRIPTHASH:

src/wallet/rpcdump.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ UniValue importaddress(const UniValue& params, bool fHelp)
245245
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NavCoin address or script");
246246
}
247247

248+
pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value'
249+
248250
if (fRescan)
249251
{
250252
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true);

0 commit comments

Comments
 (0)