Skip to content

Commit

Permalink
Addapt fee redistribution test to current situation (#1728)
Browse files Browse the repository at this point in the history
* Addapt test to current situation

* Fix lint
  • Loading branch information
Mixa84 authored Feb 7, 2023
1 parent 3e92abf commit 784fe57
Showing 1 changed file with 66 additions and 53 deletions.
119 changes: 66 additions & 53 deletions test/functional/feature_on_chain_government_fee_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
from test_framework.util import (
assert_equal,
)

from decimal import ROUND_DOWN, Decimal

VOTING_PERIOD=70

class CFPFeeDistributionTest(DefiTestFramework):
def set_test_params(self):
self.num_nodes = 4
Expand Down Expand Up @@ -41,6 +42,8 @@ def test_cfp_fee_distribution(self, amount, expectedFee, burnPct, vote, cycles=2
# Generate a block
self.nodes[0].generate(1)
self.sync_blocks()
proposalCreationHeight = self.nodes[0].getblockcount()

self.nodes[2].generate(1)
self.sync_blocks()

Expand All @@ -54,56 +57,66 @@ def test_cfp_fee_distribution(self, amount, expectedFee, burnPct, vote, cycles=2
self.nodes[0].setgov({"ATTRIBUTES":{'v0/gov/proposals/fee_burn_pct':'40%'}})
self.nodes[0].generate(1)

# Vote on proposal
self.nodes[0].votegov(propId, self.mn0, vote)
self.nodes[0].generate(1)
self.sync_blocks()
self.nodes[1].votegov(propId, self.mn1, vote)
self.nodes[1].generate(1)
self.sync_blocks()
self.nodes[2].votegov(propId, self.mn2, vote)
self.nodes[2].generate(1)
self.sync_blocks()
expectedAmount = Decimal(expectedFee * (100 - burnPct) / 100 / 3).quantize(Decimal('1E-8'), rounding=ROUND_DOWN)

# Calculate cycle
cycle1 = 103 + (103 % 70) + 70
finalHeight = cycle1 + (cycle1 % 70) + 70

# Check total votes
result = self.nodes[0].listgovproposalvotes(propId, "all")
assert_equal(len(result), 3)

# Move to final height
self.nodes[0].generate(finalHeight - self.nodes[0].getblockcount())
self.sync_blocks()

expectedAmount = '{}@DFI'.format(Decimal(expectedFee * (100 - burnPct) / 100 / 3).quantize(Decimal('1E-8'), rounding=ROUND_DOWN))

mn0 = self.nodes[0].getmasternode(self.mn0)[self.mn0]
account0 = self.nodes[0].getaccount(mn0['ownerAuthAddress'])
assert_equal(account0[0], expectedAmount)
history = self.nodes[0].listaccounthistory(mn0['ownerAuthAddress'], {"txtype": "ProposalFeeRedistribution"})
assert_equal(account0[0], history[0]['amounts'][0])

mn1 = self.nodes[0].getmasternode(self.mn1)[self.mn1]
account1 = self.nodes[0].getaccount(mn1['ownerAuthAddress'])
assert_equal(account1[0], expectedAmount)
history = self.nodes[0].listaccounthistory(mn1['ownerAuthAddress'], {"txtype": "ProposalFeeRedistribution"})
assert_equal(account0[0], history[0]['amounts'][0])

# Fee should be redistributed to reward address
mn2 = self.nodes[0].getmasternode(self.mn2)[self.mn2]
account2 = self.nodes[0].getaccount(mn2['ownerAuthAddress'])
assert_equal(account2[0], expectedAmount)
history = self.nodes[0].listaccounthistory(mn2['ownerAuthAddress'], {"txtype": "ProposalFeeRedistribution"})
assert_equal(account0[0], history[0]['amounts'][0])

# mn3 did not vote on proposal
mn3 = self.nodes[0].getmasternode(self.mn3)[self.mn3]
account3 = self.nodes[0].getaccount(mn3['ownerAuthAddress'])
assert_equal(account3, [])
history = self.nodes[0].listaccounthistory(mn3['ownerAuthAddress'], {"txtype": "ProposalFeeRedistribution"})
assert_equal(history, [])
cycleAlignment = proposalCreationHeight + (VOTING_PERIOD - proposalCreationHeight % VOTING_PERIOD)
votingCycles = 0
for cycle in range(cycles):
result = self.nodes[0].listgovproposals()
if result[0]['status'] == "Voting":
votingCycles += 1
# Vote on proposal
self.nodes[0].votegov(propId, self.mn0, vote)
self.nodes[0].generate(1)
self.sync_blocks()
self.nodes[1].votegov(propId, self.mn1, vote)
self.nodes[1].generate(1)
self.sync_blocks()
self.nodes[2].votegov(propId, self.mn2, vote)
self.nodes[2].generate(1)
self.sync_blocks()

# Check total votes
result = self.nodes[0].listgovproposalvotes(propId, "all")
assert_equal(len(result), 3)

cycleEnd = cycleAlignment + (cycle + 1) * VOTING_PERIOD
# Move to cycle end height
self.nodes[0].generate(cycleEnd - self.nodes[0].getblockcount())
self.sync_blocks()

mn0 = self.nodes[0].getmasternode(self.mn0)[self.mn0]
account0 = self.nodes[0].getaccount(mn0['ownerAuthAddress'])
assert_equal(account0[0], '{}@DFI'.format(expectedAmount * votingCycles))
history = self.nodes[0].listaccounthistory(mn0['ownerAuthAddress'], {"txtype": "ProposalFeeRedistribution"})
assert_equal(len(history), votingCycles)
for i in range(votingCycles):
assert_equal(history[i]['amounts'][0], '{}@DFI'.format(expectedAmount))

mn1 = self.nodes[0].getmasternode(self.mn1)[self.mn1]
account1 = self.nodes[0].getaccount(mn1['ownerAuthAddress'])
assert_equal(account1[0], '{}@DFI'.format(expectedAmount* votingCycles))
history = self.nodes[0].listaccounthistory(mn1['ownerAuthAddress'], {"txtype": "ProposalFeeRedistribution"})
assert_equal(len(history), votingCycles)
for i in range(votingCycles):
assert_equal(history[i]['amounts'][0], '{}@DFI'.format(expectedAmount))

# Fee should be redistributed to reward address
mn2 = self.nodes[0].getmasternode(self.mn2)[self.mn2]
account2 = self.nodes[0].getaccount(mn2['ownerAuthAddress'])
assert_equal(account2[0], '{}@DFI'.format(expectedAmount* votingCycles))
history = self.nodes[0].listaccounthistory(mn2['ownerAuthAddress'], {"txtype": "ProposalFeeRedistribution"})
assert_equal(len(history), votingCycles)
for i in range(votingCycles):
assert_equal(history[i]['amounts'][0], '{}@DFI'.format(expectedAmount))

# mn3 did not vote on proposal
mn3 = self.nodes[0].getmasternode(self.mn3)[self.mn3]
account3 = self.nodes[0].getaccount(mn3['ownerAuthAddress'])
assert_equal(account3, [])
history = self.nodes[0].listaccounthistory(mn3['ownerAuthAddress'], {"txtype": "ProposalFeeRedistribution"})
assert_equal(history, [])

self.rollback_to(height, nodes=[0, 1, 2, 3])

Expand Down Expand Up @@ -146,21 +159,21 @@ def run_test(self):

self.test_cfp_fee_distribution(amount=50, expectedFee=10, burnPct=50, vote="yes")
self.test_cfp_fee_distribution(amount=100, expectedFee=10, burnPct=50, vote="yes")
self.test_cfp_fee_distribution(amount=1000, expectedFee=10, burnPct=50, vote="yes")
self.test_cfp_fee_distribution(amount=1000, expectedFee=10, burnPct=50, vote="no", changeFeeAndBurnPCT=True)
self.test_cfp_fee_distribution(amount=1000, expectedFee=10, burnPct=50, vote="yes", changeFeeAndBurnPCT=True)
self.test_cfp_fee_distribution(amount=1000, expectedFee=10, burnPct=50, vote="no", changeFeeAndBurnPCT=True)

self.nodes[0].setgov({"ATTRIBUTES":{'v0/gov/proposals/fee_burn_pct':'30%'}})
self.nodes[0].generate(1)
self.sync_blocks()

self.test_cfp_fee_distribution(amount=1000, expectedFee=10, burnPct=30, vote="neutral",)
self.test_cfp_fee_distribution(amount=1000, expectedFee=10, burnPct=30, vote="neutral")

self.nodes[0].setgov({"ATTRIBUTES":{'v0/gov/proposals/cfp_fee':'2%'}})
self.nodes[0].generate(1)
self.sync_blocks()

self.test_cfp_fee_distribution(amount=1000, expectedFee=20, burnPct=30, vote="yes", cycles=1)
self.test_cfp_fee_distribution(amount=1000, expectedFee=20, burnPct=30, vote="yes", cycles=3)
self.test_cfp_fee_distribution(amount=1000, expectedFee=20, burnPct=30, vote="yes", cycles=3, changeFeeAndBurnPCT=True)

if __name__ == '__main__':
CFPFeeDistributionTest().main ()

0 comments on commit 784fe57

Please sign in to comment.