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

Proposal changes #1577

Merged
merged 15 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from 12 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
2 changes: 1 addition & 1 deletion src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4247,7 +4247,7 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor
if (obj.context.size() > MAX_PROP_CONTEXT_SIZE)
return Res::Err("proposal context cannot be more than %d bytes", MAX_PROP_CONTEXT_SIZE);

if (obj.contexthash.size() > MAX_PROP_CONTEXT_SIZE)
if (obj.contextHash.size() > MAX_PROP_CONTEXT_SIZE)
return Res::Err("proposal context hash cannot be more than %d bytes", MAX_PROP_CONTEXT_SIZE);

if (obj.nCycles < 1 || obj.nCycles > MAX_CYCLES)
Expand Down
7 changes: 4 additions & 3 deletions src/masternodes/proposals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Res CPropsView::CreateProp(const CPropId& propId, uint32_t height, const CCreate
WriteBy<ByCycle>(keyPair, i);
}
}
prop.finalHeight = height;
prop.proposalEndHeight = height;
WriteBy<ByType>(propId, prop);
return Res::Ok();
}
Expand All @@ -88,6 +88,7 @@ std::optional<CPropObject> CPropsView::GetProp(const CPropId& propId)
if (auto cycle = ReadBy<ByStatus, uint8_t>(key)) {
prop->cycle = *cycle;
prop->status = status;
prop->cycleEndHeight = prop->creationHeight + (prop->votingPeriod - prop->creationHeight % prop->votingPeriod) + prop->votingPeriod * *cycle;
return true;
}
return false;
Expand Down Expand Up @@ -143,8 +144,8 @@ Res CPropsView::UpdatePropStatus(const CPropId& propId, uint32_t height, CPropSt
}
}

if (p_prop->finalHeight != height) {
p_prop->finalHeight = height;
if (p_prop->proposalEndHeight != height) {
p_prop->proposalEndHeight = height;
WriteBy<ByType>(propId, *p_prop);
}
return Res::Ok();
Expand Down
9 changes: 5 additions & 4 deletions src/masternodes/proposals.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct CCreatePropMessage {
uint8_t nCycles;
std::string title;
std::string context;
std::string contexthash;
std::string contextHash;
uint8_t options;

ADD_SERIALIZE_METHODS;
Expand All @@ -65,7 +65,7 @@ struct CCreatePropMessage {
READWRITE(nCycles);
READWRITE(title);
READWRITE(context);
READWRITE(contexthash);
READWRITE(contextHash);
READWRITE(options);
}
};
Expand All @@ -91,7 +91,7 @@ struct CPropObject : public CCreatePropMessage {
explicit CPropObject(const CCreatePropMessage& other) : CCreatePropMessage(other) {}

uint32_t creationHeight{};
uint32_t finalHeight{};
uint32_t proposalEndHeight{};

uint32_t votingPeriod;
CAmount majority;
Expand All @@ -103,6 +103,7 @@ struct CPropObject : public CCreatePropMessage {
// memory only
CPropStatusType status{};
uint8_t cycle{};
uint32_t cycleEndHeight{};

ADD_SERIALIZE_METHODS;

Expand All @@ -111,7 +112,7 @@ struct CPropObject : public CCreatePropMessage {
{
READWRITEAS(CCreatePropMessage, *this);
READWRITE(creationHeight);
READWRITE(finalHeight);
READWRITE(proposalEndHeight);
READWRITE(votingPeriod);
READWRITE(majority);
READWRITE(quorum);
Expand Down
10 changes: 5 additions & 5 deletions src/masternodes/rpc_customtx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,18 +519,18 @@ class CCustomTxRpcVisitor
rpcInfo.pushKV("context", obj.context);
rpcInfo.pushKV("amount", ValueFromAmount(obj.nAmount));
rpcInfo.pushKV("cycles", int(obj.nCycles));
auto finalHeight = height;
auto proposalEndHeight = height;
bool emergency = obj.options & CPropOption::Emergency;
if (auto prop = mnview.GetProp(propId)) {
finalHeight = prop->finalHeight;
proposalEndHeight = prop->proposalEndHeight;
} else {
auto votingPeriod = (emergency ? mnview.GetEmergencyPeriodFromAttributes(type) : mnview.GetVotingPeriodFromAttributes());
finalHeight = height + (votingPeriod - height % votingPeriod);
proposalEndHeight = height + (votingPeriod - height % votingPeriod);
for (uint8_t i = 1; i <= obj.nCycles; ++i) {
finalHeight += votingPeriod;
proposalEndHeight += votingPeriod;
}
}
rpcInfo.pushKV("finalizeAfter", int64_t(finalHeight));
rpcInfo.pushKV("proposalEndHeight", int64_t(proposalEndHeight));
rpcInfo.pushKV("payoutAddress", ScriptToString(obj.address));
if (obj.options)
{
Expand Down
40 changes: 21 additions & 19 deletions src/masternodes/rpc_proposals.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include <masternodes/mn_rpc.h>
#include <masternodes/govvariables/attributes.h>

Expand All @@ -10,15 +9,16 @@ UniValue propToJSON(CPropId const& propId, CPropObject const& prop)
ret.pushKV("proposalId", propId.GetHex());
ret.pushKV("title", prop.title);
ret.pushKV("context", prop.context);
ret.pushKV("contexthash", prop.contexthash);
ret.pushKV("contextHash", prop.contextHash);
auto type = static_cast<CPropType>(prop.type);
ret.pushKV("type", CPropTypeToString(type));
auto status = static_cast<CPropStatusType>(prop.status);
ret.pushKV("status", CPropStatusToString(status));
ret.pushKV("amount", ValueFromAmount(prop.nAmount));
ret.pushKV("nextCycle", static_cast<int32_t>(prop.cycle));
ret.pushKV("currentCycle", static_cast<int32_t>(prop.cycle));
ret.pushKV("totalCycles", static_cast<int32_t>(prop.nCycles));
ret.pushKV("finalizeAfter", static_cast<int32_t>(prop.finalHeight));
ret.pushKV("cycleEndHeight", static_cast<int32_t>(prop.cycleEndHeight));
ret.pushKV("proposalEndHeight", static_cast<int32_t>(prop.proposalEndHeight));
ret.pushKV("payoutAddress", ScriptToString(prop.address));
if (prop.options)
{
Expand Down Expand Up @@ -92,7 +92,7 @@ UniValue creategovcfp(const JSONRPCRequest& request)

CAmount amount;
int cycles = 1;
std::string title, context, contexthash, addressStr;
std::string title, context, contextHash, addressStr;

const UniValue& data = request.params[0].get_obj();

Expand All @@ -109,7 +109,7 @@ UniValue creategovcfp(const JSONRPCRequest& request)
}

if (!data["contextHash"].isNull())
contexthash = data["contextHash"].get_str();
contextHash = data["contextHash"].get_str();

if (!data["cycles"].isNull())
cycles = data["cycles"].get_int();
Expand Down Expand Up @@ -139,7 +139,7 @@ UniValue creategovcfp(const JSONRPCRequest& request)
pm.nCycles = cycles;
pm.title = title;
pm.context = context;
pm.contexthash = contexthash;
pm.contextHash = contextHash;
pm.options = 0;

// encode
Expand Down Expand Up @@ -223,7 +223,7 @@ UniValue creategovvoc(const JSONRPCRequest& request)

RPCTypeCheck(request.params, { UniValue::VOBJ, UniValue::VARR }, true);

std::string title, context, contexthash;
std::string title, context, contextHash;
bool emergency = false;

const UniValue& data = request.params[0].get_obj();
Expand All @@ -241,7 +241,7 @@ UniValue creategovvoc(const JSONRPCRequest& request)
}

if (!data["contextHash"].isNull())
contexthash = data["contextHash"].get_str();
contextHash = data["contextHash"].get_str();

if (!data["emergency"].isNull())
{
Expand All @@ -254,7 +254,7 @@ UniValue creategovvoc(const JSONRPCRequest& request)
pm.nCycles = (emergency ? 1 : VOC_CYCLES);
pm.title = title;
pm.context = context;
pm.contexthash = contexthash;
pm.contextHash = contextHash;
pm.options = (emergency ? CPropOption::Emergency : 0);

// encode
Expand Down Expand Up @@ -493,7 +493,12 @@ UniValue getgovproposal(const JSONRPCRequest& request)
return propToJSON(propId, *prop);
}

auto targetHeight = view.GetLastHeight() + 1;
int targetHeight;
if (prop->status == CPropStatusType::Voting) {
targetHeight = view.GetLastHeight() + 1;
} else {
targetHeight = prop->cycleEndHeight;
}

std::set<uint256> activeMasternodes;
view.ForEachMasternode([&](uint256 const & mnId, CMasternode node) {
Expand Down Expand Up @@ -554,8 +559,8 @@ UniValue getgovproposal(const JSONRPCRequest& request)
ret.pushKV("proposalId", propId.GetHex());
ret.pushKV("title", prop->title);
ret.pushKV("context", prop->context);
if (!prop->contexthash.empty())
ret.pushKV("contexthash", prop->contexthash);
if (!prop->contextHash.empty())
ret.pushKV("contextHash", prop->contextHash);
auto type = static_cast<CPropType>(prop->type);
ret.pushKV("type", CPropTypeToString(type));

Expand All @@ -577,14 +582,11 @@ UniValue getgovproposal(const JSONRPCRequest& request)
ret.pushKV("status", "Rejected");
}

if (valid) {
ret.pushKV("approval", strprintf("%d.%02d of %d.%02d%%", votes / 100, votes % 100, majorityThreshold / 100, majorityThreshold % 100));
} else {
ret.pushKV("validity", strprintf("%d.%02d of %d.%02d%%", allVotes / 100, allVotes % 100, quorum / 100, quorum % 100));
}
ret.pushKV("votes", strprintf("%d.%02d of %d.%02d%%", votes / 100, votes % 100, majorityThreshold / 100, majorityThreshold % 100));
ret.pushKV("votingPercent", strprintf("%d.%02d of %d.%02d%%", allVotes / 100, allVotes % 100, quorum / 100, quorum % 100));

auto target = Params().GetConsensus().pos.nTargetSpacing;
auto blocks = prop->finalHeight - targetHeight;
auto blocks = prop->proposalEndHeight - targetHeight;
dcorral marked this conversation as resolved.
Show resolved Hide resolved

if (blocks > Params().GetConsensus().blocksPerDay()) {
ret.pushKV("ends", strprintf("%d days", blocks * target / 60 / 60 / 24));
Expand Down
32 changes: 18 additions & 14 deletions test/functional/feature_on_chain_government.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def run_test(self):

# Calculate cycle
cycle1 = creationHeight + (votingPeriod - creationHeight % votingPeriod) + votingPeriod
finalHeight = cycle1 + votingPeriod
proposalEndHeight = cycle1 + votingPeriod

# Check proposal and votes
result = self.nodes[0].listgovproposals()
Expand All @@ -159,10 +159,11 @@ def run_test(self):
assert_equal(result[0]["type"], "CommunityFundProposal")
assert_equal(result[0]["status"], "Voting")
assert_equal(result[0]["amount"], Decimal("100"))
assert_equal(result[0]["nextCycle"], 1)
assert_equal(result[0]["currentCycle"], 1)
assert_equal(result[0]["totalCycles"], 2)
assert_equal(result[0]["payoutAddress"], address)
assert_equal(result[0]["finalizeAfter"], finalHeight)
assert_equal(result[0]["proposalEndHeight"], proposalEndHeight)
assert_equal(result[0]["cycleEndHeight"], cycle1)

# Check individual MN votes
results = self.nodes[1].listgovvotes(tx, mn0)
Expand Down Expand Up @@ -192,7 +193,7 @@ def run_test(self):

# Check first cycle length
result = self.nodes[0].listgovproposals()
assert_equal(result[0]['nextCycle'], 1)
assert_equal(result[0]['currentCycle'], 1)

# Move to cycle payout
self.nodes[0].generate(1)
Expand All @@ -202,7 +203,7 @@ def run_test(self):
blockcount = self.nodes[0].getblockcount()

# Actually moved to next cycle at cycle1
assert_equal(result[0]['nextCycle'], 2)
assert_equal(result[0]['currentCycle'], 2)
assert_equal(blockcount, cycle1)

# First cycle should last for at least a votingPeriod
Expand All @@ -215,10 +216,10 @@ def run_test(self):
assert_equal(self.nodes[1].getaccount(address), ['100.00000000@DFI'])
result = self.nodes[0].listgovproposals()[0]
assert_equal(result["status"], "Voting")
assert_equal(result["nextCycle"], 2)
assert_equal(result["currentCycle"], 2)

# Move to just before final height
self.nodes[0].generate(finalHeight - self.nodes[0].getblockcount() - 1)
self.nodes[0].generate(proposalEndHeight - self.nodes[0].getblockcount() - 1)
bal = self.nodes[0].listcommunitybalances()['CommunityDevelopmentFunds']

# Move to final height
Expand Down Expand Up @@ -276,7 +277,7 @@ def run_test(self):
assert_equal(result["context"], context)
assert_equal(result["type"], "VoteOfConfidence")
assert_equal(result["status"], "Approved")
assert_equal(result["approval"], "75.00 of 66.67%")
assert_equal(result["votes"], "75.00 of 66.67%")
assert_equal(result["ends"], "1 days")

assert_equal(len(self.nodes[0].listgovproposals("all", "voting")), 1)
Expand Down Expand Up @@ -327,7 +328,7 @@ def run_test(self):
# Calculate cycle
votingPeriod = 100
cycle1 = creationHeight + (votingPeriod - creationHeight % votingPeriod) + votingPeriod
finalHeight = cycle1 + votingPeriod
proposalEndHeight = cycle1 + votingPeriod

# Check proposal and votes
result = self.nodes[0].listgovproposals("cfp","voting")
Expand All @@ -337,10 +338,11 @@ def run_test(self):
assert_equal(result[0]["type"], "CommunityFundProposal")
assert_equal(result[0]["status"], "Voting")
assert_equal(result[0]["amount"], Decimal("50"))
assert_equal(result[0]["nextCycle"], 1)
assert_equal(result[0]["currentCycle"], 1)
assert_equal(result[0]["totalCycles"], 2)
assert_equal(result[0]["payoutAddress"], address)
assert_equal(result[0]["finalizeAfter"], finalHeight)
assert_equal(result[0]["proposalEndHeight"], proposalEndHeight)
assert_equal(result[0]["cycleEndHeight"], cycle1)

# Check individual MN votes
results = self.nodes[1].listgovvotes(propId, mn0)
Expand Down Expand Up @@ -376,13 +378,15 @@ def run_test(self):
assert_equal(self.nodes[0].listcommunitybalances()['CommunityDevelopmentFunds'], bal + Decimal("19.23346268"))

# payout address
cycle2 = cycle1 + votingPeriod
assert_equal(self.nodes[1].getaccount(address), ['100.00000000@DFI'])
result = self.nodes[0].getgovproposal(propId)
assert_equal(result["status"], "Voting")
assert_equal(result["nextCycle"], 2)
assert_equal(result["currentCycle"], 2)
dcorral marked this conversation as resolved.
Show resolved Hide resolved
assert_equal(result["cycleEndHeight"], cycle2)

# Move to just before final height
self.nodes[0].generate(finalHeight - self.nodes[0].getblockcount() - 1)
self.nodes[0].generate(proposalEndHeight - self.nodes[0].getblockcount() - 1)
bal = self.nodes[0].listcommunitybalances()['CommunityDevelopmentFunds']

# Move to final height
Expand Down Expand Up @@ -450,7 +454,7 @@ def run_test(self):
assert_equal(result["context"], context)
assert_equal(result["type"], "VoteOfConfidence")
assert_equal(result["status"], "Approved")
assert_equal(result["approval"], "50.00 of 49.99%")
assert_equal(result["votes"], "50.00 of 49.99%")
assert_equal(result["ends"], "3 hours")

assert_equal(len(self.nodes[0].listgovproposals("all", "voting")), 1)
Expand Down