Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of ForEachAccount #1765

Merged
merged 8 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/masternodes/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,15 +549,20 @@ UniValue gettokenbalances(const JSONRPCRequest& request) {
CCustomCSView mnview(*pcustomcsview);
auto targetHeight = ::ChainActive().Height() + 1;

mnview.ForEachAccount([&](CScript const & account) {
if (IsMineCached(*pwallet, account) == ISMINE_SPENDABLE) {
mnview.CalculateOwnerRewards(account, targetHeight);
mnview.ForEachBalance([&](CScript const & owner, CTokenAmount balance) {
return account == owner && totalBalances.Add(balance);
}, {account, DCT_ID{}});
std::set<CScript> calculatedOwners;

mnview.ForEachBalance([&](CScript const & owner, CTokenAmount balance) {
if (IsMineCached(*pwallet, owner)) {
if (calculatedOwners.count(owner) == 0) {
mnview.CalculateOwnerRewards(owner, targetHeight);
calculatedOwners.emplace(owner);
}
totalBalances.Add(balance);
}

return true;
});

auto it = totalBalances.balances.lower_bound(start);
for (size_t i = 0; it != totalBalances.balances.end() && i < limit; it++, i++) {
auto bal = CTokenAmount{(*it).first, (*it).second};
Expand Down
14 changes: 9 additions & 5 deletions test/functional/feature_on_chain_government_fee_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,16 @@ 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):
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))
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):
Expand All @@ -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):
Expand Down Expand Up @@ -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()
Expand All @@ -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 ()