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 all 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
63 changes: 29 additions & 34 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,10 +559,19 @@ 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);
ret.pushKV("contextHash", prop->contextHash);
auto type = static_cast<CPropType>(prop->type);
ret.pushKV("type", CPropTypeToString(type));
if (valid && votes >= majorityThreshold) {
ret.pushKV("status", "Approved");
} else {
ret.pushKV("status", "Rejected");
}
ret.pushKV("currentCycle", static_cast<int32_t>(prop->cycle));
ret.pushKV("totalCycles", static_cast<int32_t>(prop->nCycles));
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 All @@ -571,28 +585,9 @@ UniValue getgovproposal(const JSONRPCRequest& request)
ret.pushKV("options", array);
}

if (valid && votes >= majorityThreshold) {
ret.pushKV("status", "Approved");
} else {
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));
}

auto target = Params().GetConsensus().pos.nTargetSpacing;
auto blocks = prop->finalHeight - targetHeight;
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));

if (blocks > Params().GetConsensus().blocksPerDay()) {
ret.pushKV("ends", strprintf("%d days", blocks * target / 60 / 60 / 24));
} else if (blocks > Params().GetConsensus().blocksPerDay() / 24) {
ret.pushKV("ends", strprintf("%d hours", blocks * target / 60 / 60));
} else {
ret.pushKV("ends", strprintf("%d minutes", blocks * target / 60));
}
return ret;
}

Expand Down
Loading