Skip to content

Commit 7730c7b

Browse files
marcus290alex v
authored andcommitted
tests: removed duplicate code from cfund tests (#318)
* combined cfund test helper functions: activate_cfund, start_new_cycle, end_cycle, slow_gen * simplified setup-network using existing code * removed duplicate code from rawtx tests and minor linting fixes
1 parent f0ea24c commit 7730c7b

13 files changed

+226
-442
lines changed

qa/rpc-tests/cfund-donate.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
from test_framework.test_framework import NavCoinTestFramework
7-
from test_framework.util import *
7+
from test_framework.cfund_util import *
88

99

1010
class DonateCommunityFundTest(NavCoinTestFramework):
@@ -16,13 +16,13 @@ def __init__(self):
1616
self.num_nodes = 1
1717

1818
def setup_network(self, split=False):
19-
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
20-
self.is_network_split = False
19+
self.nodes = self.setup_nodes()
20+
self.is_network_split = split
2121

2222
def run_test(self):
23-
self.nodes[0].generate(300)
23+
activate_cfund(self.nodes[0])
2424
self.nodes[0].donatefund(100)
25-
self.nodes[0].generate(1)
25+
slow_gen(self.nodes[0], 1)
2626

2727
# Verify the available coins in the fund
2828
assert(self.nodes[0].cfundstats()["funds"]["available"] == 100)

qa/rpc-tests/cfund-listproposals.py

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
from test_framework.test_framework import NavCoinTestFramework
7-
from test_framework.util import *
7+
from test_framework.cfund_util import *
88

99
import time
1010

@@ -18,15 +18,14 @@ def __init__(self):
1818
self.num_nodes = 1
1919

2020
def setup_network(self, split=False):
21-
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
22-
self.is_network_split = False
21+
self.nodes = self.setup_nodes()
22+
self.is_network_split = split
2323

2424
def run_test(self):
2525
# Get cfund parameters
2626
blocks_per_voting_cycle = self.nodes[0].cfundstats()["consensus"]["blocksPerVotingCycle"]
2727

28-
# Make sure the cfund is active
29-
self.activate_cfund()
28+
activate_cfund(self.nodes[0])
3029

3130
# Donate to fund
3231
self.nodes[0].donatefund(100)
@@ -49,7 +48,7 @@ def run_test(self):
4948
assert (set(desc_lst) == set(created_proposals.keys()))
5049

5150
# Move to the end of the 0th voting cycle
52-
self.end_cycle()
51+
end_cycle(self.nodes[0])
5352

5453
# Verify we are still in the 0th voting cycle for all the created proposals
5554
desc_lst = []
@@ -77,7 +76,7 @@ def run_test(self):
7776
# Vote for proposals and move to end of the 1st voting cycle
7877
self.nodes[0].proposalvote(created_proposals["50_50_vote"], "no")
7978
# Proposals get accepted on the last block of the current cycle
80-
self.end_cycle()
79+
end_cycle(self.nodes[0])
8180

8281
# Verify proposal status and voting cycle for all the created proposals
8382
desc_lst = []
@@ -136,7 +135,7 @@ def run_test(self):
136135

137136
# Accept payment request
138137
self.nodes[0].paymentrequestvote(payreq0, "yes")
139-
self.end_cycle()
138+
end_cycle(self.nodes[0])
140139

141140
# Validate that the request was accepted and check paid amounts
142141
for proposal in self.nodes[0].listproposals():
@@ -146,28 +145,6 @@ def run_test(self):
146145
assert (proposal["paymentRequests"][0]["status"] == "accepted")
147146
assert (proposal["paymentRequests"][0]["state"] == 1)
148147

149-
def activate_cfund(self):
150-
slow_gen(self.nodes[0] , 100)
151-
# Verify the Community Fund is started
152-
assert (self.nodes[0].getblockchaininfo()["bip9_softforks"]["communityfund"]["status"] == "started")
153-
154-
slow_gen(self.nodes[0] , 100)
155-
# Verify the Community Fund is locked_in
156-
assert (self.nodes[0].getblockchaininfo()["bip9_softforks"]["communityfund"]["status"] == "locked_in")
157-
158-
slow_gen(self.nodes[0] , 100)
159-
# Verify the Community Fund is active
160-
assert (self.nodes[0].getblockchaininfo()["bip9_softforks"]["communityfund"]["status"] == "active")
161-
162-
def end_cycle(self):
163-
# Move to the end of the cycle
164-
slow_gen(self.nodes[0] , self.nodes[0].cfundstats()["votingPeriod"]["ending"] - self.nodes[0].cfundstats()["votingPeriod"]["current"])
165-
166-
def start_new_cycle(self):
167-
# Move one past the end of the cycle
168-
slow_gen(self.nodes[0] , self.nodes[0].cfundstats()["votingPeriod"]["ending"] - self.nodes[0].cfundstats()["votingPeriod"]["current"] + 1)
169-
170-
171148

172149
if __name__ == '__main__':
173150
CommunityFundProposalsTest().main()

qa/rpc-tests/cfund-paymentrequest-extract-funds.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
from test_framework.test_framework import NavCoinTestFramework
7-
from test_framework.util import *
7+
from test_framework.cfund_util import *
88

99
import time
1010

@@ -17,24 +17,24 @@ def __init__(self):
1717
self.num_nodes = 1
1818

1919
def setup_network(self, split=False):
20-
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
21-
self.is_network_split = False
20+
self.nodes = self.setup_nodes()
21+
self.is_network_split = split
2222

2323
def run_test(self):
2424
self.nodes[0].staking(False)
25-
self.slow_gen(300)
25+
activate_cfund(self.nodes[0])
2626
self.nodes[0].donatefund(1000)
2727

2828
# Create a proposal and accept by voting
2929
proposalid0 = self.nodes[0].createproposal(self.nodes[0].getnewaddress(), 100, 36000, "test")["hash"]
3030
locked_before = self.nodes[0].cfundstats()["funds"]["locked"]
31-
self.start_new_cycle()
31+
end_cycle(self.nodes[0])
3232

3333
time.sleep(0.2)
3434

3535
self.nodes[0].proposalvote(proposalid0, "yes")
36-
self.slow_gen(1)
37-
self.start_new_cycle()
36+
slow_gen(self.nodes[0], 1)
37+
end_cycle(self.nodes[0])
3838
locked_accepted = self.nodes[0].cfundstats()["funds"]["locked"]
3939

4040
time.sleep(0.2)
@@ -50,7 +50,7 @@ def run_test(self):
5050
for x in range(6):
5151
paymentRequests.append(self.nodes[0].createpaymentrequest(proposalid0, 20, "test0")["hash"])
5252

53-
self.slow_gen(1)
53+
slow_gen(self.nodes[0], 1)
5454

5555
# One of them should have been rejected at creation
5656
valid = 0
@@ -74,21 +74,21 @@ def run_test(self):
7474

7575
assert(self.nodes[0].cfundstats()["funds"]["locked"] == locked_accepted)
7676

77-
self.start_new_cycle()
77+
end_cycle(self.nodes[0])
7878

7979
# Lets reject one of them with votes
8080
preqid = paymentRequests.pop(0)
8181
self.nodes[0].paymentrequestvote(preqid, "no")
8282

83-
self.slow_gen(1)
84-
self.start_new_cycle()
83+
slow_gen(self.nodes[0], 1)
84+
end_cycle(self.nodes[0])
8585

8686
assert(self.nodes[0].getpaymentrequest(preqid)["state"] == 2)
8787
assert(self.nodes[0].getpaymentrequest(preqid)["status"] == "rejected")
8888

8989
# Add a new payment request
9090
paymentRequests.append(self.nodes[0].createpaymentrequest(proposalid0, 20, "test0")["hash"])
91-
self.slow_gen(1)
91+
slow_gen(self.nodes[0], 1)
9292

9393
for paymentReq in paymentRequests:
9494
assert(self.nodes[0].getpaymentrequest(paymentReq)["state"] == 0)
@@ -98,12 +98,12 @@ def run_test(self):
9898
for paymentReq in paymentRequests:
9999
self.nodes[0].paymentrequestvote(paymentReq, "yes")
100100

101-
self.slow_gen(1)
102-
self.start_new_cycle()
101+
slow_gen(self.nodes[0], 1)
102+
end_cycle(self.nodes[0])
103103
time.sleep(0.2)
104104

105-
self.slow_gen(1)
106-
self.start_new_cycle()
105+
slow_gen(self.nodes[0], 1)
106+
end_cycle(self.nodes[0])
107107
time.sleep(0.2)
108108

109109
# the 5 payment requests should be accepted
@@ -115,20 +115,6 @@ def run_test(self):
115115
# all the payment requests should have been validated
116116
assert(allAccepted is True)
117117

118-
def start_new_cycle(self):
119-
# Move to the end of the cycle
120-
self.slow_gen(self.nodes[0].cfundstats()["votingPeriod"]["ending"] - self.nodes[0].cfundstats()["votingPeriod"]["current"])
121-
122-
123-
def slow_gen(self, count):
124-
total = count
125-
blocks = []
126-
while total > 0:
127-
now = min(total, 10)
128-
blocks.extend(self.nodes[0].generate(now))
129-
total -= now
130-
time.sleep(0.1)
131-
return blocks
132118

133119
if __name__ == '__main__':
134120
CommunityFundPaymentRequestExtractFundsTest().main()

qa/rpc-tests/cfund-paymentrequest-state-accept.py

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
from test_framework.test_framework import NavCoinTestFramework
7-
from test_framework.util import *
7+
from test_framework.cfund_util import *
88

99
import time
1010

@@ -17,23 +17,23 @@ def __init__(self):
1717
self.num_nodes = 1
1818

1919
def setup_network(self, split=False):
20-
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
21-
self.is_network_split = False
20+
self.nodes = self.setup_nodes()
21+
self.is_network_split = split
2222

2323
def run_test(self):
24-
self.slow_gen(300)
24+
activate_cfund(self.nodes[0])
2525
self.nodes[0].donatefund(100)
2626

2727
# Create a proposal and accept by voting
2828
proposalid0 = self.nodes[0].createproposal(self.nodes[0].getnewaddress(), 10, 3600, "test")["hash"]
2929
locked_before = self.nodes[0].cfundstats()["funds"]["locked"]
30-
self.start_new_cycle()
30+
end_cycle(self.nodes[0])
3131

3232
time.sleep(0.2)
3333

3434
self.nodes[0].proposalvote(proposalid0, "yes")
35-
self.slow_gen(1)
36-
self.start_new_cycle()
35+
slow_gen(self.nodes[0], 1)
36+
end_cycle(self.nodes[0])
3737
locked_accepted = self.nodes[0].cfundstats()["funds"]["locked"]
3838

3939
time.sleep(0.2)
@@ -46,7 +46,7 @@ def run_test(self):
4646

4747
# Create a payment request
4848
paymentrequestid0 = self.nodes[0].createpaymentrequest(proposalid0, 1, "test0")["hash"]
49-
self.slow_gen(1)
49+
slow_gen(self.nodes[0], 1)
5050

5151
# Payment request initial state at beginning of cycle
5252

@@ -61,9 +61,9 @@ def run_test(self):
6161
yes_votes = int(total_votes * min_yes_votes) + 1
6262

6363
self.nodes[0].paymentrequestvote(paymentrequestid0, "yes")
64-
self.slow_gen(yes_votes)
64+
slow_gen(self.nodes[0], yes_votes)
6565
self.nodes[0].paymentrequestvote(paymentrequestid0, "no")
66-
self.slow_gen(total_votes - yes_votes)
66+
slow_gen(self.nodes[0], total_votes - yes_votes)
6767
self.nodes[0].paymentrequestvote(paymentrequestid0, "remove")
6868

6969
# Should still be in pending
@@ -72,7 +72,7 @@ def run_test(self):
7272
assert(self.nodes[0].getpaymentrequest(paymentrequestid0)["status"] == "pending")
7373
assert(self.nodes[0].cfundstats()["funds"]["locked"] == locked_accepted)
7474

75-
self.start_new_cycle()
75+
end_cycle(self.nodes[0])
7676
time.sleep(0.2)
7777

7878
# Payment request initial state at beginning of cycle
@@ -87,16 +87,16 @@ def run_test(self):
8787
yes_votes = int(total_votes * min_yes_votes)
8888

8989
self.nodes[0].paymentrequestvote(paymentrequestid0, "yes")
90-
self.slow_gen(yes_votes)
90+
slow_gen(self.nodes[0], yes_votes)
9191
self.nodes[0].paymentrequestvote(paymentrequestid0, "no")
92-
self.slow_gen(total_votes - yes_votes)
92+
slow_gen(self.nodes[0], total_votes - yes_votes)
9393
self.nodes[0].paymentrequestvote(paymentrequestid0, "remove")
9494

9595
assert(self.nodes[0].getpaymentrequest(paymentrequestid0)["state"] == 0)
9696
assert(self.nodes[0].getpaymentrequest(paymentrequestid0)["status"] == "pending")
9797
assert(self.nodes[0].cfundstats()["funds"]["locked"] == locked_accepted)
9898

99-
self.start_new_cycle()
99+
end_cycle(self.nodes[0])
100100
time.sleep(0.2)
101101

102102
# Payment request initial state at beginning of cycle
@@ -111,9 +111,9 @@ def run_test(self):
111111
yes_votes = int(total_votes * min_yes_votes) + 1
112112

113113
self.nodes[0].paymentrequestvote(paymentrequestid0, "yes")
114-
self.slow_gen(yes_votes)
114+
slow_gen(self.nodes[0], yes_votes)
115115
self.nodes[0].paymentrequestvote(paymentrequestid0, "no")
116-
blocks = self.slow_gen(total_votes - yes_votes)
116+
blocks = slow_gen(self.nodes[0], total_votes - yes_votes)
117117
self.nodes[0].paymentrequestvote(paymentrequestid0, "remove")
118118

119119
assert(self.nodes[0].getpaymentrequest(paymentrequestid0)["state"] == 0)
@@ -133,15 +133,15 @@ def run_test(self):
133133
# Vote again
134134

135135
self.nodes[0].paymentrequestvote(paymentrequestid0, "yes")
136-
self.slow_gen(1)
136+
slow_gen(self.nodes[0], 1)
137137
self.nodes[0].paymentrequestvote(paymentrequestid0, "remove")
138138

139139

140140
# Move to a new cycle...
141141
time.sleep(0.2)
142142

143-
self.start_new_cycle()
144-
blocks=self.slow_gen(1)
143+
end_cycle(self.nodes[0])
144+
blocks = slow_gen(self.nodes[0], 1)
145145
locked_after_payment = float(locked_accepted) - float(self.nodes[0].getpaymentrequest(paymentrequestid0)["requestedAmount"])
146146

147147
# Paymentrequest must be accepted now
@@ -155,7 +155,7 @@ def run_test(self):
155155

156156
paymentrequestid1 = self.nodes[0].createpaymentrequest(proposalid0, 4, "test1")["hash"]
157157
paymentrequestid2 = self.nodes[0].createpaymentrequest(proposalid0, 4, "test2")["hash"]
158-
self.slow_gen(1)
158+
slow_gen(self.nodes[0], 1)
159159

160160
assert(self.nodes[0].getpaymentrequest(paymentrequestid1)["state"] == 0)
161161
assert(self.nodes[0].getpaymentrequest(paymentrequestid1)["status"] == "pending")
@@ -164,10 +164,10 @@ def run_test(self):
164164

165165
self.nodes[0].paymentrequestvote(paymentrequestid1, "yes")
166166
self.nodes[0].paymentrequestvote(paymentrequestid2, "yes")
167-
self.slow_gen(yes_votes)
167+
slow_gen(self.nodes[0], yes_votes)
168168
self.nodes[0].paymentrequestvote(paymentrequestid1, "no")
169169
self.nodes[0].paymentrequestvote(paymentrequestid2, "no")
170-
blocks = self.slow_gen(total_votes - yes_votes)
170+
blocks = slow_gen(self.nodes[0], total_votes - yes_votes)
171171
self.nodes[0].paymentrequestvote(paymentrequestid1, "remove")
172172
self.nodes[0].paymentrequestvote(paymentrequestid2, "remove")
173173

@@ -178,8 +178,8 @@ def run_test(self):
178178

179179
time.sleep(0.2)
180180

181-
self.start_new_cycle()
182-
blocks=self.slow_gen(1)
181+
end_cycle(self.nodes[0])
182+
blocks = slow_gen(self.nodes[0], 1)
183183

184184
# Check status after acceptance
185185

@@ -201,20 +201,5 @@ def run_test(self):
201201
pass
202202

203203

204-
def start_new_cycle(self):
205-
# Move to the end of the cycle
206-
self.slow_gen(self.nodes[0].cfundstats()["votingPeriod"]["ending"] - self.nodes[0].cfundstats()["votingPeriod"]["current"])
207-
208-
209-
def slow_gen(self, count):
210-
total = count
211-
blocks = []
212-
while total > 0:
213-
now = min(total, 10)
214-
blocks.extend(self.nodes[0].generate(now))
215-
total -= now
216-
time.sleep(0.1)
217-
return blocks
218-
219204
if __name__ == '__main__':
220205
CommunityFundPaymentRequestsTest().main()

0 commit comments

Comments
 (0)