Skip to content

Commit 96198f9

Browse files
Merge pull request #450 from mxaddict/master
This PR is a combined fix for issues #445 and #446
2 parents 073ef14 + bc692d8 commit 96198f9

19 files changed

Lines changed: 252 additions & 100 deletions

qa/pull-tester/rpc-tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
'cfund-paymentrequestvotelist.py',
168168
'reject-version-bit.py',
169169
'getcoldstakingaddress.py',
170+
'getstakereport.py',
170171
'coldstaking_staking.py',
171172
'coldstaking_spending.py',
172173
'staticr-staking-amount.py',

qa/rpc-tests/coldstaking_spending.py

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/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.
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+
56
import decimal
67
from test_framework.test_framework import NavCoinTestFramework
7-
from test_framework.util import *
8+
from test_framework.staticr_util import *
9+
810
class ColdStakingSpending(NavCoinTestFramework):
911
"""Tests spending and staking to/from a spending wallet."""
1012
# set up num of nodes
@@ -20,23 +22,12 @@ def setup_network(self, split=False):
2022
def run_test(self):
2123
self.nodes[0].staking(False)
2224

23-
"""generate first 300 blocks to lock in softfork, verify coldstaking is active"""
24-
25-
slow_gen(self.nodes[0], 100)
26-
# verify that cold staking has started
27-
assert(self.nodes[0].getblockchaininfo()["bip9_softforks"]["coldstaking"]["status"] == "started")
28-
slow_gen(self.nodes[0], 100)
29-
# verify that cold staking is locked_in
30-
assert(self.nodes[0].getblockchaininfo()["bip9_softforks"]["coldstaking"]["status"] == "locked_in")
31-
slow_gen(self.nodes[0], 100)
32-
# verify that cold staking is active
33-
assert(self.nodes[0].getblockchaininfo()["bip9_softforks"]["coldstaking"]["status"] == "active")
34-
35-
"""set up transaction-related constants and addresses"""
25+
# Make it to the static rewards fork!
26+
activate_staticr(self.nodes[0])
3627

3728
# declare transaction-related constants
3829
SENDING_FEE= 0.00010000
39-
MIN_COLDSTAKING_SENDING_FEE = 0.0028850
30+
MIN_COLDSTAKING_SENDING_FEE = 0.0033947
4031
BLOCK_REWARD = 50
4132
# generate address owned by the wallet
4233
spending_address_public_key = self.nodes[0].getnewaddress()
@@ -55,7 +46,7 @@ def run_test(self):
5546
balance_before_send = self.nodes[0].getbalance()
5647
staking_weight_before_send = self.nodes[0].getstakinginfo()["weight"]
5748
# check wallet staking weight roughly equals wallet balance
58-
assert(round(staking_weight_before_send / 100000000.0, -5) == round(balance_before_send, -5))
49+
assert_equal(round(staking_weight_before_send / 100000000.0, -5), round(balance_before_send, -5))
5950

6051
"""send navcoin to our coldstaking address, grab balance & staking weight"""
6152

@@ -70,22 +61,22 @@ def run_test(self):
7061
assert(len(listunspent_txs) > 0)
7162
# asserts that the number of utxo recieved is only 1:
7263
assert(len(listunspent_txs) == 1)
73-
# asserts if amount recieved is what it should be; ~59812449.99711600 NAV
74-
assert(listunspent_txs[0]["amount"] <= Decimal('59812449.99711600'))
64+
# asserts if amount recieved is what it should be; ~59814699.99660530 NAV
65+
assert_equal(listunspent_txs[0]["amount"], Decimal('59814699.99660530'))
7566
# grabs updated wallet balance and staking weight
7667
balance_post_send_one = self.nodes[0].getbalance()
7768
staking_weight_post_send = self.nodes[0].getstakinginfo()["weight"]
7869

7970
"""check balance decreased by just the fees"""
80-
71+
8172
# difference between balance after sending and previous balance is the same when block reward is removed
82-
# values are converted to string and "00" is added to right of == operand because values must have equal num of
73+
# values are converted to string and "00" is added to right of == operand because values must have equal num of
8374
# decimals
8475
assert(str(balance_post_send_one - BLOCK_REWARD) <= (str(float(balance_before_send) - MIN_COLDSTAKING_SENDING_FEE) + "00"))
85-
76+
8677
"""check staking weight now == 0 (we don't hold the staking key)"""
87-
88-
# sent ~all funds to coldstaking address where we do not own the staking key hence our
78+
79+
# sent ~all funds to coldstaking address where we do not own the staking key hence our
8980
# staking weight will be 0 as our recieved BLOCK_REWARD navcoin isn't mature enough to count towards
9081
# our staking weight
9182
assert((staking_weight_post_send / 100000000.0) - BLOCK_REWARD <= 1)
@@ -98,10 +89,10 @@ def run_test(self):
9889
to_be_sent = round(float(balance_post_send_one) * float(0.5) - SENDING_FEE, 8)
9990
self.nodes[0].sendtoaddress(address_Y_public_key, (to_be_sent))
10091
# put transaction in new block & update blockchain
101-
slow_gen(self.nodes[0], 1)
92+
slow_gen(self.nodes[0], 1)
10293
# wallet balance after sending
10394
balance_post_send_two = self.nodes[0].getbalance()
104-
#check balance will not be less than ~half our balance before sending - this
95+
#check balance will not be less than ~half our balance before sending - this
10596
# will occurs if we send to an address we do not own
10697
assert(balance_post_send_two - BLOCK_REWARD >= (float(balance_post_send_one) * float(0.5) - SENDING_FEE))
10798

@@ -111,7 +102,7 @@ def run_test(self):
111102
self.nodes[0].sendtoaddress(coldstaking_address_spending, round(float(balance_post_send_two) - SENDING_FEE, 8))
112103
slow_gen(self.nodes[0], 1)
113104
listunspent_txs = [n for n in self.nodes[0].listunspent() if n["address"] == coldstaking_address_spending]
114-
# send funds to a third party address using a signed raw transaction
105+
# send funds to a third party address using a signed raw transaction
115106
# get unspent tx inputs
116107
self.send_raw_transaction(decoded_raw_transaction = listunspent_txs[0], \
117108
to_address = address_Y_public_key, \
@@ -120,7 +111,7 @@ def run_test(self):
120111
)
121112
# put transaction in new block & update blockchain
122113
slow_gen(self.nodes[0], 1)
123-
# get new balance
114+
# get new balance
124115
balance_post_send_three = self.nodes[0].getbalance()
125116
# we expect our balance to be zero
126117
assert(balance_post_send_three - (BLOCK_REWARD * 2) == 0)
@@ -138,13 +129,13 @@ def run_test(self):
138129
self.nodes[0].sendtoaddress(spending_address_public_key, float(current_balance) * 0.5 - 1)
139130
slow_gen(self.nodes[0], 1)
140131
# our balance should be the same minus fees, as we own the address we sent to
141-
assert(self.nodes[0].getbalance() >= current_balance - 1 + BLOCK_REWARD)
132+
assert(self.nodes[0].getbalance() >= current_balance - 1 + BLOCK_REWARD)
142133
send_worked = True
143134
except Exception as e:
144135
print(e)
145136

146137
assert(send_worked == True)
147-
138+
148139
slow_gen(self.nodes[0], 1)
149140

150141
# send to our staking address
@@ -155,11 +146,11 @@ def run_test(self):
155146
self.nodes[0].sendtoaddress(staking_address_public_key, float(self.nodes[0].getbalance()) * 0.5 - 1)
156147
slow_gen(self.nodes[0], 1)
157148
# our balance should be half minus fees, as we dont own the address we sent to
158-
assert(self.nodes[0].getbalance() - BLOCK_REWARD <= float(current_balance) * 0.5 - 1 + 2)
149+
assert(self.nodes[0].getbalance() - BLOCK_REWARD <= float(current_balance) * 0.5 - 1 + 2)
159150
send_worked = True
160151
except Exception as e:
161152
print(e)
162-
153+
163154
assert(send_worked == True)
164155

165156
def send_raw_transaction(self, decoded_raw_transaction, to_address, change_address, amount):
@@ -172,7 +163,7 @@ def send_raw_transaction(self, decoded_raw_transaction, to_address, change_addre
172163
assert(signresult["complete"])
173164
# send raw transaction
174165
return self.nodes[0].sendrawtransaction(signresult['hex'])
175-
166+
176167

177168
if __name__ == '__main__':
178169
ColdStakingSpending().main()

qa/rpc-tests/getstakereport.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
#import time
10+
11+
class GetStakeReport(NavCoinTestFramework):
12+
"""Tests getstakereport accounting."""
13+
14+
def __init__(self):
15+
super().__init__()
16+
self.setup_clean_chain = True
17+
self.num_nodes = 3
18+
19+
def setup_network(self, split=False):
20+
self.nodes = self.setup_nodes()
21+
connect_nodes(self.nodes[0], 1)
22+
connect_nodes(self.nodes[1], 2)
23+
connect_nodes(self.nodes[2], 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+
32+
# Make it to the static rewards fork!
33+
activate_staticr(self.nodes[0])
34+
self.sync_all()
35+
36+
# Use THE spending address
37+
spending_address_public_key = self.nodes[1].getnewaddress()
38+
spending_address_private_key = self.nodes[1].dumpprivkey(spending_address_public_key)
39+
40+
# Create a staking address
41+
staking_address_public_key = self.nodes[2].getnewaddress()
42+
staking_address_private_key = self.nodes[2].dumpprivkey(staking_address_public_key)
43+
44+
# Import the 2 keys into a third wallet
45+
self.nodes[0].importprivkey(spending_address_private_key)
46+
self.nodes[0].importprivkey(staking_address_private_key)
47+
48+
# Create the cold address
49+
coldstaking_address_staking = self.nodes[1].getcoldstakingaddress(staking_address_public_key, spending_address_public_key)
50+
51+
# Send funds to the spending address (leave some NAV for fees)
52+
self.nodes[0].sendtoaddress(spending_address_public_key, self.nodes[0].getbalance() - 1)
53+
self.nodes[0].generate(1)
54+
self.sync_all()
55+
56+
# Turn staking on
57+
self.nodes[1].staking(True)
58+
59+
# Stake a block
60+
self.stake_block(self.nodes[1])
61+
62+
# Turn staking off again
63+
self.nodes[1].staking(False)
64+
65+
# Load the last 24h stake amount for the wallets/nodes
66+
merged_address_last_24h = self.nodes[0].getstakereport()['Last 24H']
67+
spending_address_last_24h = self.nodes[1].getstakereport()['Last 24H']
68+
staking_address_last_24h = self.nodes[2].getstakereport()['Last 24H']
69+
# print('spending', spending_address_last_24h)
70+
# print('staking', staking_address_last_24h)
71+
# print('merged', merged_address_last_24h)
72+
73+
# Make sure we have staked 2 NAV to the spending address
74+
# So that means spending last 24h == 2
75+
# And staking last 24h == 0 We have not sent any coins yet
76+
# And merged will have the total of the spending + staking
77+
assert_equal('2.00', merged_address_last_24h)
78+
assert_equal('2.00', spending_address_last_24h)
79+
assert_equal('0.00', staking_address_last_24h)
80+
81+
# Send funds to the cold staking address (leave some NAV for fees)
82+
self.nodes[1].sendtoaddress(coldstaking_address_staking, self.nodes[1].getbalance() - 1)
83+
self.nodes[1].generate(1)
84+
self.sync_all()
85+
86+
# Turn staking on
87+
self.nodes[2].staking(True)
88+
89+
# Stake a block
90+
self.stake_block(self.nodes[2])
91+
92+
# Turn staking off again
93+
self.nodes[2].staking(False)
94+
95+
# Load the last 24h stake amount for the wallets/nodes
96+
merged_address_last_24h = self.nodes[0].getstakereport()['Last 24H']
97+
spending_address_last_24h = self.nodes[1].getstakereport()['Last 24H']
98+
staking_address_last_24h = self.nodes[2].getstakereport()['Last 24H']
99+
# print('spending', spending_address_last_24h)
100+
# print('staking', staking_address_last_24h)
101+
# print('merged', merged_address_last_24h)
102+
103+
# Make sure we staked 4 NAV in spending address (2 NAV via COLD Stake)
104+
# So that means spending last 24h == 4
105+
# And staking last 24h == 2 We stake 2 NAV via COLD already
106+
# And merged will have the total of the spending + staking
107+
assert_equal('4.00', merged_address_last_24h)
108+
assert_equal('4.00', spending_address_last_24h)
109+
assert_equal('2.00', staking_address_last_24h)
110+
111+
def stake_block(self, node):
112+
# Get the current block count to check against while we wait for a stake
113+
blockcount = node.getblockcount()
114+
115+
# wait for a new block to be mined
116+
while node.getblockcount() == blockcount:
117+
# print("waiting for a new block...")
118+
time.sleep(1)
119+
120+
# We got one
121+
# print("found a new block...")
122+
123+
# Make sure the blocks are mature before we check the report
124+
slow_gen(node, 5, 0.5)
125+
self.sync_all()
126+
127+
128+
if __name__ == '__main__':
129+
GetStakeReport().main()

qa/rpc-tests/mempool_spendcoinbase.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,33 @@ def setup_network(self):
3232
self.is_network_split = False
3333

3434
def run_test(self):
35-
slow_gen(self.nodes[0], 200)
35+
slow_gen(self.nodes[0], 10)
3636
chain_height = self.nodes[0].getblockcount()
37-
assert_equal(chain_height, 200)
37+
assert_equal(chain_height, 10)
3838
node0_address = self.nodes[0].getnewaddress()
3939

40-
# Coinbase at height chain_height-150+1 ok in mempool, should
41-
# get mined. Coinbase at height chain_height-150+2 is
40+
# Coinbase at height chain_height-5+1 ok in mempool, should
41+
# get mined. Coinbase at height chain_height-5+2 is
4242
# is too immature to spend.
43-
b = [ self.nodes[0].getblockhash(n) for n in range(151, 153) ]
43+
b = [ self.nodes[0].getblockhash(n) for n in range(6, 8) ]
4444
coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ]
4545
spends_raw = [ create_tx(self.nodes[0], txid, node0_address, 49.99) for txid in coinbase_txids ]
4646

47-
spend_151_id = self.nodes[0].sendrawtransaction(spends_raw[0])
47+
spend_6_id = self.nodes[0].sendrawtransaction(spends_raw[0])
4848

49-
# coinbase at height 152 should be too immature to spend
49+
# coinbase at height 7 should be too immature to spend
5050
assert_raises(JSONRPCException, self.nodes[0].sendrawtransaction, spends_raw[1])
5151

52-
# mempool should have just spend_151:
53-
assert_equal(self.nodes[0].getrawmempool(), [ spend_151_id ])
52+
# mempool should have just spend_6:
53+
assert_equal(self.nodes[0].getrawmempool(), [ spend_6_id ])
5454

5555
# mine a block, spend_151 should get confirmed
5656
slow_gen(self.nodes[0], 1)
5757
assert_equal(set(self.nodes[0].getrawmempool()), set())
5858

59-
# ... and now height 152 can be spent:
60-
spend_152_id = self.nodes[0].sendrawtransaction(spends_raw[1])
61-
assert_equal(self.nodes[0].getrawmempool(), [ spend_152_id ])
59+
# ... and now height 7 can be spent:
60+
spend_7_id = self.nodes[0].sendrawtransaction(spends_raw[1])
61+
assert_equal(self.nodes[0].getrawmempool(), [ spend_7_id ])
6262

6363
if __name__ == '__main__':
6464
MempoolSpendCoinbaseTest().main()

qa/rpc-tests/sendtoaddress.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ def run_test (self):
3535
slow_gen(self.nodes[0], 1)
3636
time.sleep(2)
3737
self.sync_all()
38-
slow_gen(self.nodes[1], 75)
38+
slow_gen(self.nodes[1], 30)
3939
self.sync_all()
40-
40+
4141
# Assert correct amount of NAV generated
4242
assert_equal(self.nodes[0].getbalance(), 59800000)
4343
assert_equal(self.nodes[1].getbalance(), 1250)
44-
44+
4545
# Make transactions to valid addresses
4646
txid0 = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 60)
4747
txid1 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), Decimal("10.0"))
@@ -155,4 +155,4 @@ def run_test (self):
155155

156156

157157
if __name__ == '__main__':
158-
SendToAddressTest ().main ()
158+
SendToAddressTest ().main ()

qa/rpc-tests/stakeimmaturebalance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/python3
22

33
from test_framework.test_framework import NavCoinTestFramework
4-
from test_framework.util import *
4+
from test_framework.staticr_util import *
55
import logging
66

77
'''
@@ -10,7 +10,7 @@
1010
node0 checks that immature balance does not affect stake weight
1111
'''
1212

13-
SENDING_FEE= 0.003
13+
SENDING_FEE= 0.003393
1414
BLOCK_REWARD = 50
1515

1616
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO, stream=sys.stdout)
@@ -28,7 +28,7 @@ def setup_network(self, split=False):
2828

2929
def run_test(self):
3030
addr = self.nodes[1].getnewaddress()
31-
slow_gen(self.nodes[0], 300) #300 for soft fork voting
31+
activate_staticr(self.nodes[0])
3232
self.nodes[0].sendtoaddress(addr, satoshi_round(float(self.nodes[0].getbalance()) - SENDING_FEE))
3333
slow_gen(self.nodes[0], 1)
3434
logging.info('Checking stake weight')

0 commit comments

Comments
 (0)