Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 32c9295
Author: Shoham Chakraborty <[email protected]>
Date:   Fri Feb 3 13:11:51 2023 +0800

    Update test

commit 15fb0e5
Author: Shoham Chakraborty <[email protected]>
Date:   Fri Feb 3 12:38:41 2023 +0800

    Use VotingInfo, remove unknown votes from output

commit 0609603
Merge: e51834a faaad56
Author: Shoham Chakraborty <[email protected]>
Date:   Thu Feb 2 22:42:22 2023 +0800

    Merge branch 'master' into proposalvotes-stats

commit faaad56
Author: Shoham Chakraborty <[email protected]>
Date:   Thu Feb 2 20:09:41 2023 +0800

    Fix setgov crashes (#1719)

    Co-authored-by: Prasanna Loganathar <[email protected]>

commit 7807454
Author: Mihailo Milenkovic <[email protected]>
Date:   Thu Feb 2 11:13:43 2023 +0100

    Fix voting scenarios test (#1727)

commit e51834a
Author: Shoham Chakraborty <[email protected]>
Date:   Thu Feb 2 15:38:06 2023 +0800

    Fix test

commit 808ed6e
Author: Shoham Chakraborty <[email protected]>
Date:   Thu Feb 2 14:49:20 2023 +0800

    Redo neutral changes

commit 4a9c8f0
Merge: bc55ada b7b14c2
Author: Shoham Chakraborty <[email protected]>
Date:   Thu Feb 2 14:30:31 2023 +0800

    Merge branch 'master' into proposalvotes-stats

    # Conflicts:
    #	src/masternodes/rpc_proposals.cpp
    #	test/functional/feature_on_chain_government.py

commit bc55ada
Author: Shoham Chakraborty <[email protected]>
Date:   Thu Feb 2 14:27:51 2023 +0800

    Refactor tests

commit e47befd
Author: Shoham Chakraborty <[email protected]>
Date:   Thu Feb 2 14:25:13 2023 +0800

    Default cycles fix

commit 44d0a02
Author: Shoham Chakraborty <[email protected]>
Date:   Wed Feb 1 15:08:29 2023 +0800

    Fix isMine default

commit 7246e04
Author: Shoham Chakraborty <[email protected]>
Date:   Wed Feb 1 14:51:04 2023 +0800

    Fix multiple proposal output when aggregating

commit 8b072df
Author: Shoham Chakraborty <[email protected]>
Date:   Wed Feb 1 14:45:48 2023 +0800

    Fix output schema

commit 9fa408b
Author: Shoham Chakraborty <[email protected]>
Date:   Mon Jan 30 12:09:57 2023 +0800

    Add aggregate stats support to listgovproposalvotes
  • Loading branch information
shohamc1 committed Feb 3, 2023
1 parent b309361 commit 6c20d92
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 43 deletions.
30 changes: 18 additions & 12 deletions src/masternodes/mn_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,12 @@ UniValue setgov(const JSONRPCRequest& request) {
CCoinControl coinControl;

// Set change to selected foundation address
CTxDestination dest;
ExtractDestination(*auths.cbegin(), dest);
if (IsValidDestination(dest)) {
coinControl.destChange = dest;
if (!auths.empty()) {
CTxDestination dest;
ExtractDestination(*auths.cbegin(), dest);
if (IsValidDestination(dest)) {
coinControl.destChange = dest;
}
}

fund(rawTx, pwallet, optAuthTx, &coinControl);
Expand Down Expand Up @@ -673,10 +675,12 @@ UniValue unsetgov(const JSONRPCRequest& request) {
CCoinControl coinControl;

// Set change to selected foundation address
CTxDestination dest;
ExtractDestination(*auths.cbegin(), dest);
if (IsValidDestination(dest)) {
coinControl.destChange = dest;
if (!auths.empty()) {
CTxDestination dest;
ExtractDestination(*auths.cbegin(), dest);
if (IsValidDestination(dest)) {
coinControl.destChange = dest;
}
}

fund(rawTx, pwallet, optAuthTx, &coinControl);
Expand Down Expand Up @@ -767,10 +771,12 @@ UniValue setgovheight(const JSONRPCRequest& request) {
CCoinControl coinControl;

// Set change to selected foundation address
CTxDestination dest;
ExtractDestination(*auths.cbegin(), dest);
if (IsValidDestination(dest)) {
coinControl.destChange = dest;
if (!auths.empty()) {
CTxDestination dest;
ExtractDestination(*auths.cbegin(), dest);
if (IsValidDestination(dest)) {
coinControl.destChange = dest;
}
}

fund(rawTx, pwallet, optAuthTx, &coinControl);
Expand Down
42 changes: 17 additions & 25 deletions src/masternodes/rpc_proposals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
const bool DEFAULT_RPC_GOV_NEUTRAL = false;

struct VotingInfo {
int32_t votesPossible;
int32_t votesPresent;
int32_t votesYes;
int32_t votesPossible = 0;
int32_t votesPresent = 0;
int32_t votesYes = 0;
int32_t votesNo = 0;
int32_t votesNeutral = 0;
int32_t votesInvalid = 0;
};

UniValue proposalToJSON(const CProposalId &propId,
Expand Down Expand Up @@ -110,32 +113,22 @@ UniValue proposalVoteToJSON(const CProposalId &propId, uint8_t cycle, const uint
return ret;
}

struct VoteAccounting {
int totalVotes;
int yesVotes;
int neutralVotes;
int noVotes;
int unknownVotes;
};

void proposalVoteAccounting(const CProposalVoteType &vote, uint256 propId, std::map<std::string, VoteAccounting> &map) {
void proposalVoteAccounting(const CProposalVoteType &vote, uint256 propId, std::map<std::string, VotingInfo> &map) {
const auto voteString = CProposalVoteToString(vote);

if (map.find(propId.GetHex()) == map.end())
map.emplace(propId.GetHex(), VoteAccounting{0, 0, 0, 0, 0});
map.emplace(propId.GetHex(), VotingInfo{0, 0, 0, 0, 0, 0});

auto entry = &map.find(propId.GetHex())->second;

if (voteString == "YES")
++entry->yesVotes;
++entry->votesYes;
else if (voteString == "NEUTRAL")
++entry->neutralVotes;
++entry->votesNeutral;
else if (voteString == "NO")
++entry->noVotes;
else
++entry->unknownVotes;
++entry->votesNo;

++entry->totalVotes;
++entry->votesPresent;
}

/*
Expand Down Expand Up @@ -749,7 +742,7 @@ UniValue listgovproposalvotes(const JSONRPCRequest &request) {

UniValue ret(UniValue::VARR);

std::map<std::string, VoteAccounting> map;
std::map<std::string, VotingInfo> map;

view.ForEachProposalVote(
[&](const CProposalId &pId, uint8_t propCycle, const uint256 &id, CProposalVoteType vote) {
Expand Down Expand Up @@ -839,11 +832,10 @@ UniValue listgovproposalvotes(const JSONRPCRequest &request) {
UniValue stats(UniValue::VOBJ);

stats.pushKV("proposalId", entry.first);
stats.pushKV("total", entry.second.totalVotes);
stats.pushKV("yes", entry.second.yesVotes);
stats.pushKV("neutral", entry.second.neutralVotes);
stats.pushKV("no", entry.second.noVotes);
stats.pushKV("unknown", entry.second.unknownVotes);
stats.pushKV("total", entry.second.votesPresent);
stats.pushKV("yes", entry.second.votesYes);
stats.pushKV("neutral", entry.second.votesNeutral);
stats.pushKV("no", entry.second.votesNo);

ret.push_back(stats);
}
Expand Down
8 changes: 3 additions & 5 deletions test/functional/feature_on_chain_government.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,9 +685,9 @@ def run_test(self):
assert_equal(self.nodes[0].listgovproposals(
{"status": "voting", "pagination": {"start": tx1, "including_start": False, "limit": 1}}), nextProposal)

# self.test_aggregation(propId)
# self.test_default_cycles_fix()
# self.aggregate_all_votes()
self.test_aggregation(propId)
self.test_default_cycles_fix()
self.aggregate_all_votes()
self.test_valid_votes()

def test_aggregation(self, propId):
Expand All @@ -699,15 +699,13 @@ def test_aggregation(self, propId):
yesVotes = len([x for x in votes if x["vote"] == "YES"])
noVotes = len([x for x in votes if x["vote"] == "NO"])
neutralVotes = len([x for x in votes if x["vote"] == "NEUTRAL"])
unknownVotes = totalVotes - yesVotes - noVotes - neutralVotes

votes_aggregate = self.nodes[0].listgovproposalvotes(propId, 'all', -1, {}, True)[0]
assert_equal(votes_aggregate["proposalId"], propId)
assert_equal(votes_aggregate["total"], totalVotes)
assert_equal(votes_aggregate["yes"], yesVotes)
assert_equal(votes_aggregate["neutral"], neutralVotes)
assert_equal(votes_aggregate["no"], noVotes)
assert_equal(votes_aggregate["unknown"], unknownVotes)

def test_default_cycles_fix(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion test/functional/feature_on_chain_government_voting_scenarios.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True
self.extra_args = [
['-jellyfish_regtest=1', '-dummypos=0', '-txnotokens=0', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101', '-simulatemainnet=1', '-rpc-governance-accept-neutral=1'],
['-jellyfish_regtest=1', '-dummypos=0', '-txnotokens=0', '-amkheight=50', '-bayfrontheight=51', '-eunosheight=80', '-fortcanningheight=82', '-fortcanninghillheight=84', '-fortcanningroadheight=86', '-fortcanningcrunchheight=88', '-fortcanningspringheight=90', '-fortcanninggreatworldheight=94', '-grandcentralheight=101', '-rpc-governance-accept-neutral=1', '-simulatemainnet=1'],
]

def setup_masternodes(self, nMasternodes = 19):
Expand Down
153 changes: 153 additions & 0 deletions test/functional/try.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
from test_framework.test_framework import DefiTestFramework
import calendar
import time
from decimal import Decimal

class Verify (DefiTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True
self.extra_args = [
['-txnotokens=0', '-amkheight=5', '-bayfrontheight=5', '-eunosheight=10', '-fortcanningheight=11', '-fortcanninghillheight=12', '-fortcanningroadheight=13', '-fortcanningcrunchheight=14', '-fortcanningspringheight=15', '-grandcentralheight=16']]

def run_test(self):
node = self.nodes[0]

node.generate(200)

account0 = node.get_genesis_keys().ownerAuthAddress
symbolBTC = "BTC"
symbolTSLA = "TSLA"
symbolDFI = "DFI"
symboldUSD = "DUSD"


self.nodes[0].createtoken({
"symbol": symbolBTC,
"name": "BTC token",
"isDAT": True,
"collateralAddress": account0
})
self.nodes[0].createtoken({
"symbol": symboldUSD,
"name": "DUSD token",
"isDAT": True,
"collateralAddress": account0
})
self.nodes[0].generate(1)

oracle_address1 = self.nodes[0].getnewaddress("", "legacy")
price_feeds1 = [{"currency": "USD", "token": "DFI"},
{"currency": "USD", "token": "BTC"},
{"currency": "USD", "token": "TSLA"},
{"currency": "USD", "token": "GOOGL"}]
oracle_id1 = self.nodes[0].appointoracle(oracle_address1, price_feeds1, 10)
self.nodes[0].generate(1)
oracle1_prices = [{"currency": "USD", "tokenAmount": "10@TSLA"},
{"currency": "USD", "tokenAmount": "10@GOOGL"},
{"currency": "USD", "tokenAmount": "10@DFI"},
{"currency": "USD", "tokenAmount": "10@BTC"}]
timestamp = calendar.timegm(time.gmtime())
self.nodes[0].setoracledata(oracle_id1, timestamp, oracle1_prices)
self.nodes[0].generate(1)

idBTC = list(self.nodes[0].gettoken(symbolBTC).keys())[0]
idDFI = list(self.nodes[0].gettoken(symbolDFI).keys())[0]
iddUSD = list(self.nodes[0].gettoken(symboldUSD).keys())[0]
self.nodes[0].setcollateraltoken({
'token': idBTC,
'factor': 1,
'fixedIntervalPriceId': "BTC/USD"})
self.nodes[0].setcollateraltoken({
'token': idDFI,
'factor': 1,
'fixedIntervalPriceId': "DFI/USD"})
self.nodes[0].generate(1)

setLoanTokenTSLA = self.nodes[0].setloantoken({
'symbol': symbolTSLA,
'name': "Tesla stock token",
'fixedIntervalPriceId': "TSLA/USD",
'mintable': True,
'interest': 1})
self.nodes[0].createloanscheme(150, 5, 'LOAN150')
self.nodes[0].generate(10)
idTSLA = list(self.nodes[0].getloantoken(symbolTSLA)["token"])[0]

vaultId = self.nodes[0].createvault(account0, 'LOAN150')
self.nodes[0].minttokens(["2@" + symbolBTC])
node.utxostoaccount({account0: "2000@DFI"})
self.nodes[0].generate(1)

self.nodes[0].deposittovault(vaultId, account0, "2000@DFI")
self.nodes[0].generate(1)

poolOwner = self.nodes[0].getnewaddress("", "legacy")

self.nodes[0].createpoolpair({
"tokenA": iddUSD,
"tokenB": idTSLA,
"commission": Decimal('0.002'),
"status": True,
"ownerAddress": poolOwner,
"pairSymbol": "DUSD-TSLA",
}, [])
self.nodes[0].createpoolpair({
"tokenA": iddUSD,
"tokenB": idDFI,
"commission": Decimal('0.002'),
"status": True,
"ownerAddress": poolOwner,
"pairSymbol": "DUSD-DFI",
}, [])
node.generate(1)

self.nodes[0].minttokens(["4000@" + symboldUSD, "2000@" + symbolTSLA])
node.utxostoaccount({account0: "100@DFI"})
node.generate(1)

node.addpoolliquidity({
account0: ["2000@" + symboldUSD, "100@" + symbolDFI]
}, account0, [])
node.addpoolliquidity({
account0: ["2000@" + symboldUSD, "2000@" + symbolTSLA]
}, account0, [])
node.generate(1)

self.nodes[0].takeloan({
'vaultId': vaultId,
'amounts': "1@" + symbolTSLA})
self.nodes[0].generate(1)

self.nodes[0].paybackloan({
'vaultId': vaultId,
'from': account0,
'amounts': "0.5@" + symbolTSLA})
self.nodes[0].generate(1)

self.nodes[0].paybackloan({
'vaultId': vaultId,
'from': account0,
'amounts': "0.5@" + symbolTSLA})
self.nodes[0].generate(1)

# self.nodes[0].generate(6)

preinvalid = node.getburninfo()
blockhash = node.getblockhash(node.getblockcount() - 5)
node.invalidateblock(blockhash)
burninfo = node.getburninfo()
print(node.verifychain())

self.restart_node(0)

print(node.verifychain())
assert(node.getburninfo() == burninfo)

node.reconsiderblock(blockhash)
assert(node.getburninfo() == preinvalid)

raise Exception()

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

0 comments on commit 6c20d92

Please sign in to comment.