Skip to content

Commit

Permalink
feat: Superblock creation (Sentinel elimination) (#5525)
Browse files Browse the repository at this point in the history
## Issue being fixed or feature implemented

Implementation of issue dashpay/dash-issues#43

## What was done?

Masternode will try to create, sign and submit a Superblock (GovTrigger)
during the `nSuperblockMaturityWindow`.

## How Has This Been Tested?

## Breaking Changes


## Checklist:
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have added or updated relevant unit/integration/functional/e2e
tests
- [ ] I have made corresponding changes to the documentation
- [x] I have assigned this pull request to a milestone _(for repository
code-owners and collaborators only)_

---------

Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
Co-authored-by: PastaPastaPasta <6443210+PastaPastaPasta@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 29, 2023
1 parent 27be6e6 commit ceb84d5
Show file tree
Hide file tree
Showing 9 changed files with 487 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ class CRegTestParams : public CChainParams {
consensus.nSuperblockStartBlock = 1500;
consensus.nSuperblockStartHash = uint256(); // do not check this on regtest
consensus.nSuperblockCycle = 10;
consensus.nSuperblockMaturityWindow = 10; // This is equal to SB cycle on regtest
consensus.nSuperblockMaturityWindow = 2;
consensus.nGovernanceMinQuorum = 1;
consensus.nGovernanceFilterElements = 100;
consensus.nMasternodeMinimumConfirmations = 1;
Expand Down
73 changes: 65 additions & 8 deletions src/governance/classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <timedata.h>
#include <util/strencodings.h>
#include <validation.h>
#include <util/moneystr.h>
#include <util/underlying.h>

#include <univalue.h>
Expand Down Expand Up @@ -385,6 +386,7 @@ void CSuperblockManager::ExecuteBestSuperblock(CGovernanceManager& governanceMan
// All checks are done in CSuperblock::IsValid via IsBlockValueValid and IsBlockPayeeValid,
// tip wouldn't be updated if anything was wrong. Mark this trigger as executed.
pSuperblock->SetExecuted();
governanceManager.ResetVotedFundingTrigger();
}
}

Expand Down Expand Up @@ -429,12 +431,19 @@ CSuperblock::
// NEXT WE GET THE PAYMENT INFORMATION AND RECONSTRUCT THE PAYMENT VECTOR
std::string strAddresses = obj["payment_addresses"].get_str();
std::string strAmounts = obj["payment_amounts"].get_str();
ParsePaymentSchedule(strAddresses, strAmounts);
std::string strProposalHashes = obj["proposal_hashes"].get_str();
ParsePaymentSchedule(strAddresses, strAmounts, strProposalHashes);

LogPrint(BCLog::GOBJECT, "CSuperblock -- nBlockHeight = %d, strAddresses = %s, strAmounts = %s, vecPayments.size() = %d\n",
nBlockHeight, strAddresses, strAmounts, vecPayments.size());
}

CSuperblock::CSuperblock(int nBlockHeight, std::vector<CGovernancePayment> vecPayments) : nBlockHeight(nBlockHeight), vecPayments(std::move(vecPayments))
{
nStatus = SeenObjectStatus::Valid; //TODO: Investigate this
nGovObjHash = GetHash();
}

CGovernanceObject* CSuperblock::GetGovernanceObject(CGovernanceManager& governanceManager)
{
AssertLockHeld(governanceManager.cs);
Expand Down Expand Up @@ -493,20 +502,22 @@ CAmount CSuperblock::GetPaymentsLimit(int nBlockHeight)
return nPaymentsLimit;
}

void CSuperblock::ParsePaymentSchedule(const std::string& strPaymentAddresses, const std::string& strPaymentAmounts)
void CSuperblock::ParsePaymentSchedule(const std::string& strPaymentAddresses, const std::string& strPaymentAmounts, const std::string& strProposalHashes)
{
// SPLIT UP ADDR/AMOUNT STRINGS AND PUT IN VECTORS

std::vector<std::string> vecParsed1;
std::vector<std::string> vecParsed2;
std::vector<std::string> vecParsed3;
vecParsed1 = SplitBy(strPaymentAddresses, "|");
vecParsed2 = SplitBy(strPaymentAmounts, "|");
vecParsed3 = SplitBy(strProposalHashes, "|");

// IF THESE DON'T MATCH, SOMETHING IS WRONG

if (vecParsed1.size() != vecParsed2.size()) {
if (vecParsed1.size() != vecParsed2.size() || vecParsed1.size() != vecParsed3.size()) {
std::ostringstream ostr;
ostr << "CSuperblock::ParsePaymentSchedule -- Mismatched payments and amounts";
ostr << "CSuperblock::ParsePaymentSchedule -- Mismatched payments, amounts and proposalHashes";
LogPrintf("%s\n", ostr.str());
throw std::runtime_error(ostr.str());
}
Expand Down Expand Up @@ -535,9 +546,17 @@ void CSuperblock::ParsePaymentSchedule(const std::string& strPaymentAddresses, c

CAmount nAmount = ParsePaymentAmount(vecParsed2[i]);

LogPrint(BCLog::GOBJECT, "CSuperblock::ParsePaymentSchedule -- i = %d, amount string = %s, nAmount = %lld\n", i, vecParsed2[i], nAmount);
uint256 proposalHash;
if (!ParseHashStr(vecParsed3[i], proposalHash)){
std::ostringstream ostr;
ostr << "CSuperblock::ParsePaymentSchedule -- Invalid proposal hash : " << vecParsed3[i];
LogPrintf("%s\n", ostr.str());
throw std::runtime_error(ostr.str());
}

CGovernancePayment payment(dest, nAmount);
LogPrint(BCLog::GOBJECT, "CSuperblock::ParsePaymentSchedule -- i = %d, amount string = %s, nAmount = %lld, proposalHash = %s\n", i, vecParsed2[i], nAmount, proposalHash.ToString());

CGovernancePayment payment(dest, nAmount, proposalHash);
if (payment.IsValid()) {
vecPayments.push_back(payment);
} else {
Expand Down Expand Up @@ -692,10 +711,48 @@ bool CSuperblock::IsExpired(const CGovernanceManager& governanceManager) const
return false;
}

CGovernancePayment::CGovernancePayment(const CTxDestination& destIn, CAmount nAmountIn) :
std::vector<uint256> CSuperblock::GetProposalHashes() const
{
std::vector<uint256> res;

for (const auto& payment : vecPayments) {
res.push_back(payment.proposalHash);
}

return res;
}

std::string CSuperblock::GetHexStrData() const
{
// {\"event_block_height\": 879720, \"payment_addresses\": \"yd5KMREs3GLMe6mTJYr3YrH1juwNwrFCfB\", \"payment_amounts\": \"5.00000000\", \"proposal_hashes\": \"485817fddbcab6c55c9a6856dabc8b19ed79548bda8c01712daebc9f74f287f4\", \"type\": 2}\u0000

std::string str_addresses = Join(vecPayments, "|", [&](const auto& payment) {
CTxDestination dest;
ExtractDestination(payment.script, dest);
return EncodeDestination(dest);
});
std::string str_amounts = Join(vecPayments, "|", [&](const auto& payment) {
return FormatMoney(payment.nAmount);
});
std::string str_hashes = Join(vecPayments, "|", [&](const auto& payment) { return payment.proposalHash.ToString(); });

std::stringstream ss;
ss << "{";
ss << "\"event_block_height\": " << nBlockHeight << ", ";
ss << "\"payment_addresses\": \"" << str_addresses << "\", ";
ss << "\"payment_amounts\": \"" << str_amounts << "\", ";
ss << "\"proposal_hashes\": \"" << str_hashes << "\", ";
ss << "\"type\":" << 2;
ss << "}";

return HexStr(ss.str());
}

CGovernancePayment::CGovernancePayment(const CTxDestination& destIn, CAmount nAmountIn, const uint256& proposalHash) :
fValid(false),
script(),
nAmount(0)
nAmount(0),
proposalHash(proposalHash)
{
try {
script = GetScriptForDestination(destIn);
Expand Down
15 changes: 12 additions & 3 deletions src/governance/classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ using CSuperblock_sptr = std::shared_ptr<CSuperblock>;
// DECLARE GLOBAL VARIABLES FOR GOVERNANCE CLASSES
extern CGovernanceTriggerManager triggerman;

CAmount ParsePaymentAmount(const std::string& strAmount);

/**
* Trigger Manager
*
Expand Down Expand Up @@ -80,15 +82,17 @@ class CGovernancePayment
public:
CScript script;
CAmount nAmount;
uint256 proposalHash;

CGovernancePayment() :
fValid(false),
script(),
nAmount(0)
nAmount(0),
proposalHash(0)
{
}

CGovernancePayment(const CTxDestination& destIn, CAmount nAmountIn);
CGovernancePayment(const CTxDestination& destIn, CAmount nAmountIn, const uint256& proposalHash);

bool IsValid() const { return fValid; }
};
Expand Down Expand Up @@ -120,10 +124,11 @@ class CSuperblock : public CGovernanceObject
SeenObjectStatus nStatus;
std::vector<CGovernancePayment> vecPayments;

void ParsePaymentSchedule(const std::string& strPaymentAddresses, const std::string& strPaymentAmounts);
void ParsePaymentSchedule(const std::string& strPaymentAddresses, const std::string& strPaymentAmounts, const std::string& strProposalHashes);

public:
CSuperblock();
CSuperblock(int nBlockHeight, std::vector<CGovernancePayment> vecPayments);
explicit CSuperblock(uint256& nHash);

static bool IsValidBlockHeight(int nBlockHeight);
Expand All @@ -133,6 +138,8 @@ class CSuperblock : public CGovernanceObject
SeenObjectStatus GetStatus() const { return nStatus; }
void SetStatus(SeenObjectStatus nStatusIn) { nStatus = nStatusIn; }

std::string GetHexStrData() const;

// TELL THE ENGINE WE EXECUTED THIS EVENT
void SetExecuted() { nStatus = SeenObjectStatus::Executed; }

Expand All @@ -149,6 +156,8 @@ class CSuperblock : public CGovernanceObject

bool IsValid(const CTransaction& txNew, int nBlockHeight, CAmount blockReward);
bool IsExpired(const CGovernanceManager& governanceManager) const;

std::vector<uint256> GetProposalHashes() const;
};

#endif // BITCOIN_GOVERNANCE_CLASSES_H
Loading

0 comments on commit ceb84d5

Please sign in to comment.