Skip to content

Commit

Permalink
tests: removed duplicate code from cfund tests (#318)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
marcus290 authored and alex v committed Nov 6, 2018
1 parent f0ea24c commit 7730c7b
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 442 deletions.
10 changes: 5 additions & 5 deletions qa/rpc-tests/cfund-donate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

from test_framework.test_framework import NavCoinTestFramework
from test_framework.util import *
from test_framework.cfund_util import *


class DonateCommunityFundTest(NavCoinTestFramework):
Expand All @@ -16,13 +16,13 @@ def __init__(self):
self.num_nodes = 1

def setup_network(self, split=False):
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
self.is_network_split = False
self.nodes = self.setup_nodes()
self.is_network_split = split

def run_test(self):
self.nodes[0].generate(300)
activate_cfund(self.nodes[0])
self.nodes[0].donatefund(100)
self.nodes[0].generate(1)
slow_gen(self.nodes[0], 1)

# Verify the available coins in the fund
assert(self.nodes[0].cfundstats()["funds"]["available"] == 100)
Expand Down
37 changes: 7 additions & 30 deletions qa/rpc-tests/cfund-listproposals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

from test_framework.test_framework import NavCoinTestFramework
from test_framework.util import *
from test_framework.cfund_util import *

import time

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

def setup_network(self, split=False):
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
self.is_network_split = False
self.nodes = self.setup_nodes()
self.is_network_split = split

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

# Make sure the cfund is active
self.activate_cfund()
activate_cfund(self.nodes[0])

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

# Move to the end of the 0th voting cycle
self.end_cycle()
end_cycle(self.nodes[0])

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

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

# Accept payment request
self.nodes[0].paymentrequestvote(payreq0, "yes")
self.end_cycle()
end_cycle(self.nodes[0])

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

def activate_cfund(self):
slow_gen(self.nodes[0] , 100)
# Verify the Community Fund is started
assert (self.nodes[0].getblockchaininfo()["bip9_softforks"]["communityfund"]["status"] == "started")

slow_gen(self.nodes[0] , 100)
# Verify the Community Fund is locked_in
assert (self.nodes[0].getblockchaininfo()["bip9_softforks"]["communityfund"]["status"] == "locked_in")

slow_gen(self.nodes[0] , 100)
# Verify the Community Fund is active
assert (self.nodes[0].getblockchaininfo()["bip9_softforks"]["communityfund"]["status"] == "active")

def end_cycle(self):
# Move to the end of the cycle
slow_gen(self.nodes[0] , self.nodes[0].cfundstats()["votingPeriod"]["ending"] - self.nodes[0].cfundstats()["votingPeriod"]["current"])

def start_new_cycle(self):
# Move one past the end of the cycle
slow_gen(self.nodes[0] , self.nodes[0].cfundstats()["votingPeriod"]["ending"] - self.nodes[0].cfundstats()["votingPeriod"]["current"] + 1)



if __name__ == '__main__':
CommunityFundProposalsTest().main()
46 changes: 16 additions & 30 deletions qa/rpc-tests/cfund-paymentrequest-extract-funds.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

from test_framework.test_framework import NavCoinTestFramework
from test_framework.util import *
from test_framework.cfund_util import *

import time

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

def setup_network(self, split=False):
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
self.is_network_split = False
self.nodes = self.setup_nodes()
self.is_network_split = split

def run_test(self):
self.nodes[0].staking(False)
self.slow_gen(300)
activate_cfund(self.nodes[0])
self.nodes[0].donatefund(1000)

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

time.sleep(0.2)

self.nodes[0].proposalvote(proposalid0, "yes")
self.slow_gen(1)
self.start_new_cycle()
slow_gen(self.nodes[0], 1)
end_cycle(self.nodes[0])
locked_accepted = self.nodes[0].cfundstats()["funds"]["locked"]

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

self.slow_gen(1)
slow_gen(self.nodes[0], 1)

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

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

self.start_new_cycle()
end_cycle(self.nodes[0])

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

self.slow_gen(1)
self.start_new_cycle()
slow_gen(self.nodes[0], 1)
end_cycle(self.nodes[0])

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

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

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

self.slow_gen(1)
self.start_new_cycle()
slow_gen(self.nodes[0], 1)
end_cycle(self.nodes[0])
time.sleep(0.2)

self.slow_gen(1)
self.start_new_cycle()
slow_gen(self.nodes[0], 1)
end_cycle(self.nodes[0])
time.sleep(0.2)

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

def start_new_cycle(self):
# Move to the end of the cycle
self.slow_gen(self.nodes[0].cfundstats()["votingPeriod"]["ending"] - self.nodes[0].cfundstats()["votingPeriod"]["current"])


def slow_gen(self, count):
total = count
blocks = []
while total > 0:
now = min(total, 10)
blocks.extend(self.nodes[0].generate(now))
total -= now
time.sleep(0.1)
return blocks

if __name__ == '__main__':
CommunityFundPaymentRequestExtractFundsTest().main()
63 changes: 24 additions & 39 deletions qa/rpc-tests/cfund-paymentrequest-state-accept.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

from test_framework.test_framework import NavCoinTestFramework
from test_framework.util import *
from test_framework.cfund_util import *

import time

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

def setup_network(self, split=False):
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
self.is_network_split = False
self.nodes = self.setup_nodes()
self.is_network_split = split

def run_test(self):
self.slow_gen(300)
activate_cfund(self.nodes[0])
self.nodes[0].donatefund(100)

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

time.sleep(0.2)

self.nodes[0].proposalvote(proposalid0, "yes")
self.slow_gen(1)
self.start_new_cycle()
slow_gen(self.nodes[0], 1)
end_cycle(self.nodes[0])
locked_accepted = self.nodes[0].cfundstats()["funds"]["locked"]

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

# Create a payment request
paymentrequestid0 = self.nodes[0].createpaymentrequest(proposalid0, 1, "test0")["hash"]
self.slow_gen(1)
slow_gen(self.nodes[0], 1)

# Payment request initial state at beginning of cycle

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

self.nodes[0].paymentrequestvote(paymentrequestid0, "yes")
self.slow_gen(yes_votes)
slow_gen(self.nodes[0], yes_votes)
self.nodes[0].paymentrequestvote(paymentrequestid0, "no")
self.slow_gen(total_votes - yes_votes)
slow_gen(self.nodes[0], total_votes - yes_votes)
self.nodes[0].paymentrequestvote(paymentrequestid0, "remove")

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

self.start_new_cycle()
end_cycle(self.nodes[0])
time.sleep(0.2)

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

self.nodes[0].paymentrequestvote(paymentrequestid0, "yes")
self.slow_gen(yes_votes)
slow_gen(self.nodes[0], yes_votes)
self.nodes[0].paymentrequestvote(paymentrequestid0, "no")
self.slow_gen(total_votes - yes_votes)
slow_gen(self.nodes[0], total_votes - yes_votes)
self.nodes[0].paymentrequestvote(paymentrequestid0, "remove")

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

self.start_new_cycle()
end_cycle(self.nodes[0])
time.sleep(0.2)

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

self.nodes[0].paymentrequestvote(paymentrequestid0, "yes")
self.slow_gen(yes_votes)
slow_gen(self.nodes[0], yes_votes)
self.nodes[0].paymentrequestvote(paymentrequestid0, "no")
blocks = self.slow_gen(total_votes - yes_votes)
blocks = slow_gen(self.nodes[0], total_votes - yes_votes)
self.nodes[0].paymentrequestvote(paymentrequestid0, "remove")

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

self.nodes[0].paymentrequestvote(paymentrequestid0, "yes")
self.slow_gen(1)
slow_gen(self.nodes[0], 1)
self.nodes[0].paymentrequestvote(paymentrequestid0, "remove")


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

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

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

paymentrequestid1 = self.nodes[0].createpaymentrequest(proposalid0, 4, "test1")["hash"]
paymentrequestid2 = self.nodes[0].createpaymentrequest(proposalid0, 4, "test2")["hash"]
self.slow_gen(1)
slow_gen(self.nodes[0], 1)

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

self.nodes[0].paymentrequestvote(paymentrequestid1, "yes")
self.nodes[0].paymentrequestvote(paymentrequestid2, "yes")
self.slow_gen(yes_votes)
slow_gen(self.nodes[0], yes_votes)
self.nodes[0].paymentrequestvote(paymentrequestid1, "no")
self.nodes[0].paymentrequestvote(paymentrequestid2, "no")
blocks = self.slow_gen(total_votes - yes_votes)
blocks = slow_gen(self.nodes[0], total_votes - yes_votes)
self.nodes[0].paymentrequestvote(paymentrequestid1, "remove")
self.nodes[0].paymentrequestvote(paymentrequestid2, "remove")

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

time.sleep(0.2)

self.start_new_cycle()
blocks=self.slow_gen(1)
end_cycle(self.nodes[0])
blocks = slow_gen(self.nodes[0], 1)

# Check status after acceptance

Expand All @@ -201,20 +201,5 @@ def run_test(self):
pass


def start_new_cycle(self):
# Move to the end of the cycle
self.slow_gen(self.nodes[0].cfundstats()["votingPeriod"]["ending"] - self.nodes[0].cfundstats()["votingPeriod"]["current"])


def slow_gen(self, count):
total = count
blocks = []
while total > 0:
now = min(total, 10)
blocks.extend(self.nodes[0].generate(now))
total -= now
time.sleep(0.1)
return blocks

if __name__ == '__main__':
CommunityFundPaymentRequestsTest().main()
Loading

0 comments on commit 7730c7b

Please sign in to comment.