Skip to content

Commit d1ebf2e

Browse files
committed
[Tests] Add functional test for import staking address/key
1 parent 3bd5579 commit d1ebf2e

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

src/wallet/rpcdump.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ UniValue importprivkey(const UniValue& params, bool fHelp)
139139

140140
if (fRescan) {
141141
CBlockIndex *pindex = chainActive.Genesis();
142-
if (fStakingAddress) {
142+
if (fStakingAddress && Params().NetworkID() != CBaseChainParams::REGTEST) {
143143
// cold staking was activated after nBlockTimeProtocolV2. No need to scan the whole chain
144144
pindex = chainActive[Params().BlockStartTimeProtocolV2()];
145145
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2020 The PIVX 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+
'''
7+
Tests importprivkey and importaddress with staking keys/addresses.
8+
Node0 generates staking addresses and sends delegations to them.
9+
Node1 imports and rescans. The test checks that cold utxos and staking balance is updated.
10+
'''
11+
12+
from time import sleep
13+
14+
from test_framework.test_framework import PivxTestFramework
15+
from test_framework.util import (
16+
assert_equal,
17+
DecimalAmt,
18+
sync_blocks,
19+
)
20+
21+
class ImportStakingTest(PivxTestFramework):
22+
23+
def set_test_params(self):
24+
self.num_nodes = 2
25+
self.extra_args = [['-staking=0']] * self.num_nodes
26+
self.extra_args[0].append('-sporkkey=932HEevBSujW2ud7RfB1YF91AFygbBRQj3de3LyaCRqNzKKgWXi')
27+
28+
def log_title(self):
29+
title = "*** Starting %s ***" % self.__class__.__name__
30+
underline = "-" * len(title)
31+
description = "Tests importprivkey and importaddress with staking keys/addresses."
32+
self.log.info("\n\n%s\n%s\n%s\n", title, underline, description)
33+
34+
def run_test(self):
35+
NUM_OF_DELEGATIONS = 4 # Create 2*NUM_OF_DELEGATIONS staking addresses
36+
self.log_title()
37+
self.log.info("Activating cold staking spork")
38+
assert_equal("success", self.activate_spork(0, "SPORK_17_COLDSTAKING_ENFORCEMENT"))
39+
40+
# Create cold staking addresses and delegations
41+
self.log.info("Creating new staking addresses and sending delegations")
42+
staking_addresses = [self.nodes[0].getnewstakingaddress("label %d" % i)
43+
for i in range(2 * NUM_OF_DELEGATIONS)]
44+
delegations = []
45+
for i, sa in enumerate(staking_addresses):
46+
# delegate 10 PIV
47+
delegations.append(self.nodes[0].delegatestake(sa, 10)['txid'])
48+
# mine a block and check staking balance
49+
self.nodes[0].generate(1)
50+
assert_equal(self.nodes[0].getcoldstakingbalance(), DecimalAmt(10 * (i+1)))
51+
sync_blocks(self.nodes)
52+
53+
# Export keys
54+
self.log.info("Exporting keys and importing in node 1")
55+
priv_keys = [self.nodes[0].dumpprivkey(x) for x in staking_addresses]
56+
57+
# Import keys of addresses 0-(NUM_OF_DELEGATIONS-1) (and rescan)
58+
assert_equal(self.nodes[1].getcoldstakingbalance(), DecimalAmt(0))
59+
for i, pk in enumerate(priv_keys[:NUM_OF_DELEGATIONS]):
60+
self.nodes[1].importprivkey(pk, "label %d" % i, True, True)
61+
val = self.nodes[1].validateaddress(staking_addresses[i])
62+
assert_equal(val['ismine'], True)
63+
assert_equal(val['isstaking'], True)
64+
assert_equal(val['iswatchonly'], False)
65+
assert_equal(self.nodes[1].getcoldstakingbalance(), DecimalAmt(10 * (i + 1)))
66+
self.log.info("Balance of node 1 checks out")
67+
coldutxos = [x['txid'] for x in self.nodes[1].listcoldutxos()]
68+
assert_equal(len(coldutxos), NUM_OF_DELEGATIONS)
69+
assert_equal(len([x for x in coldutxos if x in delegations]), NUM_OF_DELEGATIONS)
70+
self.log.info("Delegation list of node 1 checks out")
71+
72+
# Import remaining addresses as watch-only (and rescan again)
73+
self.log.info("Importing addresses (watch-only)")
74+
for i, sa in enumerate(staking_addresses[NUM_OF_DELEGATIONS:]):
75+
self.nodes[1].importaddress(sa, "label %d" % i, True)
76+
# !TODO: add watch-only support in the core (balance and txes)
77+
# Currently the only way to check the addressbook without the key here
78+
# is to verify the account with validateaddress
79+
val = self.nodes[1].validateaddress(sa)
80+
assert_equal(val['ismine'], False)
81+
assert_equal(val['isstaking'], True)
82+
assert_equal(val['iswatchonly'], True)
83+
assert_equal(self.nodes[1].getcoldstakingbalance(), DecimalAmt(10 * NUM_OF_DELEGATIONS))
84+
self.log.info("Balance of node 1 checks out")
85+
86+
87+
88+
if __name__ == '__main__':
89+
ImportStakingTest().main()

0 commit comments

Comments
 (0)