From 4a7935f461460d408c4741991ad483aaed6c8257 Mon Sep 17 00:00:00 2001 From: Bushstar Date: Thu, 16 Feb 2023 07:34:24 +0000 Subject: [PATCH 1/3] Add missing calc pool rewards --- src/masternodes/validation.cpp | 5 +++++ ...re_on_chain_government_fee_distribution.py | 22 +++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/masternodes/validation.cpp b/src/masternodes/validation.cpp index e4447d0453..76d177a344 100644 --- a/src/masternodes/validation.cpp +++ b/src/masternodes/validation.cpp @@ -2184,6 +2184,11 @@ static void ProcessProposalEvents(const CBlockIndex* pindex, CCustomCSView& cach if (!res) { LogPrintf("Proposal fee redistribution failed: %s Address: %s Amount: %d\n", res.msg, scriptPubKey.GetHex(), amountPerVoter); } + + if (pindex->nHeight >= chainparams.GetConsensus().NextNetworkUpgradeHeight) { + subView.CalculateOwnerRewards(scriptPubKey, pindex->nHeight); + } + subView.Flush(); } diff --git a/test/functional/feature_on_chain_government_fee_distribution.py b/test/functional/feature_on_chain_government_fee_distribution.py index 2e6f262831..4365a1a7bf 100755 --- a/test/functional/feature_on_chain_government_fee_distribution.py +++ b/test/functional/feature_on_chain_government_fee_distribution.py @@ -20,10 +20,10 @@ def set_test_params(self): self.num_nodes = 4 self.setup_clean_chain = True self.extra_args = [ - ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101'], - ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101'], - ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101'], - ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101'], + ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101', '-nextnetworkupgradeheight=101'], + ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101', '-nextnetworkupgradeheight=101'], + ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101', '-nextnetworkupgradeheight=101'], + ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101', '-nextnetworkupgradeheight=101'], ] def test_cfp_fee_distribution(self, amount, expectedFee, burnPct, vote, cycles=2, changeFeeAndBurnPCT = False): @@ -91,6 +91,7 @@ def test_cfp_fee_distribution(self, amount, expectedFee, burnPct, vote, cycles=2 mn0 = self.nodes[0].getmasternode(self.mn0)[self.mn0] account0 = self.nodes[0].getaccount(mn0['ownerAuthAddress']) assert_equal(account0[0], '{}@DFI'.format(expectedAmount * votingCycles)) + assert_equal(self.nodes[0].gettokenbalances(), [f"{expectedAmount * votingCycles}@0"]) history = self.nodes[0].listaccounthistory(mn0['ownerAuthAddress'], {"txtype": "ProposalFeeRedistribution"}) assert_equal(len(history), votingCycles) for i in range(votingCycles): @@ -98,7 +99,8 @@ def test_cfp_fee_distribution(self, amount, expectedFee, burnPct, vote, cycles=2 mn1 = self.nodes[0].getmasternode(self.mn1)[self.mn1] account1 = self.nodes[0].getaccount(mn1['ownerAuthAddress']) - assert_equal(account1[0], '{}@DFI'.format(expectedAmount* votingCycles)) + assert_equal(account1[0], '{}@DFI'.format(expectedAmount * votingCycles)) + assert_equal(self.nodes[1].gettokenbalances(), [f"{expectedAmount * votingCycles}@0"]) history = self.nodes[0].listaccounthistory(mn1['ownerAuthAddress'], {"txtype": "ProposalFeeRedistribution"}) assert_equal(len(history), votingCycles) for i in range(votingCycles): @@ -107,7 +109,8 @@ def test_cfp_fee_distribution(self, amount, expectedFee, burnPct, vote, cycles=2 # 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)) + assert_equal(account2[0], '{}@DFI'.format(expectedAmount * votingCycles)) + assert_equal(self.nodes[2].gettokenbalances(), [f"{expectedAmount * votingCycles}@0"]) history = self.nodes[0].listaccounthistory(mn2['ownerAuthAddress'], {"txtype": "ProposalFeeRedistribution"}) assert_equal(len(history), votingCycles) for i in range(votingCycles): @@ -158,11 +161,11 @@ def setup(self): assert_equal(self.nodes[0].getblockcount(), 101) # activate on-chain governance - self.nodes[0].setgov({"ATTRIBUTES":{'v0/params/feature/gov':'true'}}) + self.nodes[0].setgov({"ATTRIBUTES": {'v0/params/feature/gov': 'true'}}) self.nodes[0].generate(1) # activate fee redistribution - self.nodes[0].setgov({"ATTRIBUTES":{'v0/gov/proposals/fee_redistribution':'true'}}) + self.nodes[0].setgov({"ATTRIBUTES": {'v0/gov/proposals/fee_redistribution': 'true'}}) self.nodes[0].generate(1) self.sync_blocks() @@ -187,7 +190,8 @@ def run_test(self): 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, changeFeeAndBurnPCT=True) + self.test_cfp_fee_distribution(amount=1000, expectedFee=20, burnPct=30, vote="yes", cycles=3, + changeFeeAndBurnPCT=True) if __name__ == '__main__': CFPFeeDistributionTest().main () From 9fde8234dfbc47484daa47f3dfca885a9ee8c570 Mon Sep 17 00:00:00 2001 From: Bushstar Date: Wed, 1 Mar 2023 16:05:53 +0800 Subject: [PATCH 2/3] Add other missing CalculateOwnerRewards calls --- src/masternodes/mn_checks.cpp | 8 +++++--- src/masternodes/validation.cpp | 4 +--- .../feature_on_chain_government_fee_distribution.py | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/masternodes/mn_checks.cpp b/src/masternodes/mn_checks.cpp index 6fc1ad4c09..3dff709f63 100644 --- a/src/masternodes/mn_checks.cpp +++ b/src/masternodes/mn_checks.cpp @@ -1570,6 +1570,8 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor { Require(token->symbol == "BTC" && token->name == "Bitcoin" && token->IsDAT(), "Only Bitcoin can be swapped in " + obj.name); + mnview.CalculateOwnerRewards(script, height); + Require(mnview.SubBalance(script, {id, amount})); const CTokenCurrencyPair btcUsd{"BTC", "USD"}; @@ -1678,9 +1680,7 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor { CDataStructureV0 liveKey{AttributeTypes::Live, ParamIDs::Economy, economyKey}; auto balances = attributes->GetValue(liveKey, CBalances{}); - if (height >= static_cast(consensus.FortCanningCrunchHeight)) { - CalculateOwnerRewards(obj.owner); - } + CalculateOwnerRewards(obj.owner); if (obj.withdraw) { CTokenAmount totalFutures{}; @@ -3130,6 +3130,8 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor { } } + mnview.CalculateOwnerRewards(obj.to, height); + return mnview.AddBalance(obj.to, obj.amount); } diff --git a/src/masternodes/validation.cpp b/src/masternodes/validation.cpp index 6256e958d7..9e4e03768c 100644 --- a/src/masternodes/validation.cpp +++ b/src/masternodes/validation.cpp @@ -2187,9 +2187,7 @@ static void ProcessProposalEvents(const CBlockIndex* pindex, CCustomCSView& cach LogPrintf("Proposal fee redistribution failed: %s Address: %s Amount: %d\n", res.msg, scriptPubKey.GetHex(), amountPerVoter); } - if (pindex->nHeight >= chainparams.GetConsensus().NextNetworkUpgradeHeight) { - subView.CalculateOwnerRewards(scriptPubKey, pindex->nHeight); - } + subView.CalculateOwnerRewards(scriptPubKey, pindex->nHeight); subView.Flush(); } diff --git a/test/functional/feature_on_chain_government_fee_distribution.py b/test/functional/feature_on_chain_government_fee_distribution.py index e3d8dc6380..d8b2bc26d3 100755 --- a/test/functional/feature_on_chain_government_fee_distribution.py +++ b/test/functional/feature_on_chain_government_fee_distribution.py @@ -18,10 +18,10 @@ def set_test_params(self): self.num_nodes = 4 self.setup_clean_chain = True self.extra_args = [ - ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101', '-nextnetworkupgradeheight=101'], - ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101', '-nextnetworkupgradeheight=101'], - ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101', '-nextnetworkupgradeheight=101'], - ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101', '-nextnetworkupgradeheight=101'], + ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101'], + ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101'], + ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101'], + ['-dummypos=0', '-txnotokens=0', '-rpc-governance-accept-neutral', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101'], ] def test_cfp_fee_distribution(self, amount, expectedFee, burnPct, vote, cycles=2, changeFeeAndBurnPCT = False): From 26c5b050052d22ca1259e39eed9afded2c62774b Mon Sep 17 00:00:00 2001 From: Bushstar Date: Fri, 3 Mar 2023 18:33:18 +0800 Subject: [PATCH 3/3] Add fork guards --- src/masternodes/mn_checks.cpp | 12 +++++++++--- src/masternodes/validation.cpp | 4 +++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/masternodes/mn_checks.cpp b/src/masternodes/mn_checks.cpp index 3dff709f63..5eea4abb8f 100644 --- a/src/masternodes/mn_checks.cpp +++ b/src/masternodes/mn_checks.cpp @@ -1570,7 +1570,9 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor { Require(token->symbol == "BTC" && token->name == "Bitcoin" && token->IsDAT(), "Only Bitcoin can be swapped in " + obj.name); - mnview.CalculateOwnerRewards(script, height); + if (height >= static_cast(consensus.NextNetworkUpgradeHeight)) { + mnview.CalculateOwnerRewards(script, height); + } Require(mnview.SubBalance(script, {id, amount})); @@ -1680,7 +1682,9 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor { CDataStructureV0 liveKey{AttributeTypes::Live, ParamIDs::Economy, economyKey}; auto balances = attributes->GetValue(liveKey, CBalances{}); - CalculateOwnerRewards(obj.owner); + if (height >= static_cast(consensus.FortCanningCrunchHeight)) { + CalculateOwnerRewards(obj.owner); + } if (obj.withdraw) { CTokenAmount totalFutures{}; @@ -3130,7 +3134,9 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor { } } - mnview.CalculateOwnerRewards(obj.to, height); + if (height >= static_cast(consensus.NextNetworkUpgradeHeight)) { + mnview.CalculateOwnerRewards(obj.to, height); + } return mnview.AddBalance(obj.to, obj.amount); } diff --git a/src/masternodes/validation.cpp b/src/masternodes/validation.cpp index 9e4e03768c..6256e958d7 100644 --- a/src/masternodes/validation.cpp +++ b/src/masternodes/validation.cpp @@ -2187,7 +2187,9 @@ static void ProcessProposalEvents(const CBlockIndex* pindex, CCustomCSView& cach LogPrintf("Proposal fee redistribution failed: %s Address: %s Amount: %d\n", res.msg, scriptPubKey.GetHex(), amountPerVoter); } - subView.CalculateOwnerRewards(scriptPubKey, pindex->nHeight); + if (pindex->nHeight >= chainparams.GetConsensus().NextNetworkUpgradeHeight) { + subView.CalculateOwnerRewards(scriptPubKey, pindex->nHeight); + } subView.Flush(); }