Skip to content

Commit

Permalink
Change min and max confirmation requirements, add new rpc call to get…
Browse files Browse the repository at this point in the history
… migration count
  • Loading branch information
blondfrogs committed Feb 2, 2022
1 parent b99668e commit b19d6c7
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 22 deletions.
85 changes: 81 additions & 4 deletions src/rpc/zelnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,10 @@ void GetDeterministicListData(UniValue& listData, const std::string& strFilter,
else
info.push_back(std::make_pair("lastpaid", 0));

if (data.nCollateral > 0) {
info.push_back(std::make_pair("amount", FormatMoney(data.nCollateral)));
}

info.push_back(std::make_pair("rank", count++));

listData.push_back(info);
Expand Down Expand Up @@ -783,6 +787,10 @@ UniValue getdoslist(const UniValue& params, bool fHelp)
int nEligibleIn = ZELNODE_DOS_REMOVE_AMOUNT - (nCurrentHeight - data.nAddedBlockHeight);
info.push_back(std::make_pair("eligible_in", nEligibleIn));

if (data.nCollateral > 0) {
info.push_back(std::make_pair("amount", FormatMoney(data.nCollateral)));
}

mapOrderedDosList[nEligibleIn].emplace_back(info);
}

Expand Down Expand Up @@ -839,13 +847,15 @@ UniValue getstartlist(const UniValue& params, bool fHelp)
info.push_back(std::make_pair("added_height", data.nAddedBlockHeight));
info.push_back(std::make_pair("payment_address", EncodeDestination(data.collateralPubkey.GetID())));


// TODO, when merged with the code that increasese the start tx expiration to 80 -> ZELNODE_START_TX_EXPIRATION_HEIGHT
// TODO Grab the expiration height with the new function that was created that takes into account he block height :)
int nCurrentHeight = chainActive.Height();
int nExpiresIn = ZELNODE_START_TX_EXPIRATION_HEIGHT - (nCurrentHeight - data.nAddedBlockHeight);
int nExpiresIn = GetZelnodeExpirationCount(nCurrentHeight) - (nCurrentHeight - data.nAddedBlockHeight);

info.push_back(std::make_pair("expires_in", nExpiresIn));

if (data.nCollateral > 0) {
info.push_back(std::make_pair("amount", FormatMoney(data.nCollateral)));
}

mapOrderedStartList[nExpiresIn].emplace_back(info);
}

Expand Down Expand Up @@ -932,6 +942,10 @@ UniValue getzelnodestatus (const UniValue& params, bool fHelp)
info.push_back(std::make_pair("lastpaid", std::to_string(chainActive[data.nLastPaidHeight]->nTime)));
else
info.push_back(std::make_pair("lastpaid", 0));

if (data.nCollateral > 0) {
info.push_back(std::make_pair("amount", FormatMoney(data.nCollateral)));
}
}

return info;
Expand Down Expand Up @@ -1053,6 +1067,68 @@ UniValue getzelnodecount (const UniValue& params, bool fHelp)
return NullUniValue;
}

UniValue getmigrationcount (const UniValue& params, bool fHelp)
{
if (fHelp || (params.size() > 0))
throw runtime_error(
"getmigrationcount\n"
"\nGet zelnode migration count values\n"

"\nResult:\n"
"{\n"
" \"total-old\": n, (numeric) Total zelnodes\n"
" \"total-new\": n, (numeric) Total zelnodes\n"
"}\n"

"\nExamples:\n" +
HelpExampleCli("getmigrationcount", "") + HelpExampleRpc("getmigrationcount", ""));

if (IsDZelnodeActive())
{
int nTotalOld = 0;
int nTotalNew = 0;
std::vector<int> vOldNodeCount(GetNumberOfTiers());
std::vector<int> vNewNodeCount(GetNumberOfTiers());
{
LOCK(g_zelnodeCache.cs);
g_zelnodeCache.CountMigration(nTotalOld, nTotalNew,vOldNodeCount, vNewNodeCount);
}

std::map<int,pair<string,string> > words;
words.insert(make_pair(0, make_pair("basic-enabled", "cumulus-enabled")));
words.insert(make_pair(1, make_pair("super-enabled", "nimbus-enabled")));
words.insert(make_pair(2, make_pair("bamf-enabled", "stratus-enabled")));

UniValue oldTierCount(UniValue::VOBJ);
oldTierCount.pushKV("total-old", nTotalOld);
for (int i = 0; i < vOldNodeCount.size(); i++) {
if (words.count(i)) {
oldTierCount.push_back(Pair(words.at(i).second + "-old", vOldNodeCount[i]));
} else {
oldTierCount.push_back(Pair("unnamed-enabled-old", vOldNodeCount[i]));
}
}

UniValue newTierCount(UniValue::VOBJ);
newTierCount.pushKV("total-new", nTotalNew);
for (int i = 0; i < vNewNodeCount.size(); i++) {
if (words.count(i)) {
newTierCount.push_back(Pair(words.at(i).second + "-new", vNewNodeCount[i]));
} else {
newTierCount.push_back(Pair("unnamed-enabled-new", vNewNodeCount[i]));
}
}

UniValue result(UniValue::VARR);

result.push_back(oldTierCount);
result.push_back(newTierCount);
return result;
}

return NullUniValue;
}

UniValue listzelnodeconf (const UniValue& params, bool fHelp)
{
std::string strFilter = "";
Expand Down Expand Up @@ -1246,6 +1322,7 @@ static const CRPCCommand commands[] =
{ "zelnode", "getdoslist", &getdoslist, false },
{ "zelnode", "getstartlist", &getstartlist, false },
{ "zelnode", "getzelnodecount", &getzelnodecount, false },
{ "zelnode", "getmigrationcount", &getmigrationcount, false },
{ "zelnode", "zelnodecurrentwinner", &zelnodecurrentwinner, false }, /* uses wallet if enabled */
{ "zelnode", "getzelnodestatus", &getzelnodestatus, false },
{ "zelnode", "listzelnodeconf", &listzelnodeconf, false },
Expand Down
2 changes: 1 addition & 1 deletion src/zelnode/activezelnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void ActiveZelnode::ManageDeterministricZelnode()
} else {
return;
}
} else if (g_zelnodeCache.CheckIfNeedsNextConfirm(activeZelnode.deterministicOutPoint)) {
} else if (g_zelnodeCache.CheckIfNeedsNextConfirm(activeZelnode.deterministicOutPoint, nHeight)) {
activeZelnode.BuildDeterministicConfirmTx(mutTx, ZelnodeUpdateType::UPDATE_CONFIRM);
LogPrintf("Time to Confirm Zelnode reached, Creating Update Confirm Transaction on height: %s for outpoint: %s\n", nHeight, activeZelnode.deterministicOutPoint.ToString());
} else {
Expand Down
62 changes: 50 additions & 12 deletions src/zelnode/zelnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ std::string TierToString(int tier)
return strStatus;
}

bool IsMigrationCollateralAmount(const CAmount& amount)
{
return amount == V2_ZELNODE_COLLAT_CUMULUS * COIN || amount == V2_ZELNODE_COLLAT_NIMBUS * COIN || amount == V2_ZELNODE_COLLAT_STRATUS * COIN;
}

bool CheckZelnodeTxSignatures(const CTransaction& transaction)
{
if (transaction.nType & ZELNODE_START_TX_TYPE) {
Expand Down Expand Up @@ -379,18 +384,28 @@ bool ZelnodeCache::CheckConfirmationHeights(const int nCurrentHeight, const COut
// Allow ip address changes at a different interval
if (fFluxActive) {
if (ip != data.ip) {
if (nCurrentHeight - data.nLastConfirmedBlockHeight >= ZELNODE_CONFIRM_UPDATE_MIN_HEIGHT_IP_CHANGE) {
if (nCurrentHeight - data.nLastConfirmedBlockHeight >= ZELNODE_CONFIRM_UPDATE_MIN_HEIGHT_IP_CHANGE_V1) {
return true;
}
}
}

if (nCurrentHeight - data.nLastConfirmedBlockHeight <= ZELNODE_CONFIRM_UPDATE_MIN_HEIGHT) {
// TODO - Remove this error message after release + 1 month and we don't see any problems
error("%s - %d - Confirmation to soon - %s -> Current Height: %d, lastConfirmed: %d\n", __func__,
__LINE__,
out.ToFullString(), nCurrentHeight, data.nLastConfirmedBlockHeight);
return false;
bool fHalvingActive = NetworkUpgradeActive(nCurrentHeight, Params().GetConsensus(), Consensus::UPGRADE_HALVING);
if (fHalvingActive) {
if (nCurrentHeight - data.nLastConfirmedBlockHeight <= ZELNODE_CONFIRM_UPDATE_MIN_HEIGHT_V2) {
error("%s - %d - Confirmation to soon - %s -> Current Height: %d, lastConfirmed: %d\n", __func__,
__LINE__,
out.ToFullString(), nCurrentHeight, data.nLastConfirmedBlockHeight);
return false;
}
} else {
if (nCurrentHeight - data.nLastConfirmedBlockHeight <= ZELNODE_CONFIRM_UPDATE_MIN_HEIGHT_V1) {
// TODO - Remove this error message after release + 1 month and we don't see any problems
error("%s - %d - Confirmation to soon - %s -> Current Height: %d, lastConfirmed: %d\n", __func__,
__LINE__,
out.ToFullString(), nCurrentHeight, data.nLastConfirmedBlockHeight);
return false;
}
}

return true;
Expand All @@ -414,11 +429,18 @@ bool ZelnodeCache::InConfirmTracker(const COutPoint& out)
return mapConfirmedZelnodeData.count(out);
}

bool ZelnodeCache::CheckIfNeedsNextConfirm(const COutPoint& out)
bool ZelnodeCache::CheckIfNeedsNextConfirm(const COutPoint& out, const int& p_nHeight)
{
LOCK(cs);

bool fHalvingActive = NetworkUpgradeActive(p_nHeight, Params().GetConsensus(), Consensus::UPGRADE_HALVING);

if (mapConfirmedZelnodeData.count(out)) {
return chainActive.Height() - mapConfirmedZelnodeData.at(out).nLastConfirmedBlockHeight > ZELNODE_CONFIRM_UPDATE_MIN_HEIGHT;
if (fHalvingActive) {
return p_nHeight - mapConfirmedZelnodeData.at(out).nLastConfirmedBlockHeight > ZELNODE_CONFIRM_UPDATE_MIN_HEIGHT_V2;
} else {
return p_nHeight - mapConfirmedZelnodeData.at(out).nLastConfirmedBlockHeight > ZELNODE_CONFIRM_UPDATE_MIN_HEIGHT_V1;
}
}

return false;
Expand Down Expand Up @@ -1137,14 +1159,30 @@ void ZelnodeCache::CountNetworks(int& ipv4, int& ipv6, int& onion, std::vector<i
}
}

void ZelnodeCache::CountMigration(int& nOldTotal, int& nNewTotal, std::vector<int>& vOldNodeCount, std::vector<int>& vNewNodeCount) {
for (const auto& entry : mapConfirmedZelnodeData) {
if (IsMigrationCollateralAmount(entry.second.nCollateral)) {
vNewNodeCount[entry.second.nTier - 1]++;
nNewTotal++;
} else {
vOldNodeCount[entry.second.nTier - 1]++;
nOldTotal++;
}
}
}

int GetZelnodeExpirationCount(const int& p_nHeight)
{
// Get the status on if Zelnode params1 is activated
bool fFluxActive = NetworkUpgradeActive(p_nHeight, Params().GetConsensus(), Consensus::UPGRADE_FLUX);
if (fFluxActive) {
return ZELNODE_CONFIRM_UPDATE_EXPIRATION_HEIGHT_PARAMS_1;
bool fHalvingActive = NetworkUpgradeActive(p_nHeight, Params().GetConsensus(), Consensus::UPGRADE_HALVING);

if (fHalvingActive) {
return ZELNODE_CONFIRM_UPDATE_EXPIRATION_HEIGHT_V3;
} else if (fFluxActive) {
return ZELNODE_CONFIRM_UPDATE_EXPIRATION_HEIGHT_V2;
} else {
return ZELNODE_CONFIRM_UPDATE_EXPIRATION_HEIGHT;
return ZELNODE_CONFIRM_UPDATE_EXPIRATION_HEIGHT_V1;
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/zelnode/zelnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
#define ZELNODE_DOS_REMOVE_AMOUNT 180

// How often a new confirmation transaction needs to be seen on chain to keep a node up and running
#define ZELNODE_CONFIRM_UPDATE_EXPIRATION_HEIGHT 60
#define ZELNODE_CONFIRM_UPDATE_EXPIRATION_HEIGHT_PARAMS_1 80
#define ZELNODE_CONFIRM_UPDATE_EXPIRATION_HEIGHT_V1 60
#define ZELNODE_CONFIRM_UPDATE_EXPIRATION_HEIGHT_V2 80
#define ZELNODE_CONFIRM_UPDATE_EXPIRATION_HEIGHT_V3 160

// Nodes are allowed to send a update confirm notification only after this many blocks past there last confirm
#define ZELNODE_CONFIRM_UPDATE_MIN_HEIGHT 40
#define ZELNODE_CONFIRM_UPDATE_MIN_HEIGHT_IP_CHANGE 5
#define ZELNODE_CONFIRM_UPDATE_MIN_HEIGHT_V1 40
#define ZELNODE_CONFIRM_UPDATE_MIN_HEIGHT_V2 120
#define ZELNODE_CONFIRM_UPDATE_MIN_HEIGHT_IP_CHANGE_V1 5


/// Mempool only
// Max signature time that we accept into the mempool
Expand Down Expand Up @@ -74,6 +77,7 @@ std::string TierToString(int tier);

bool CheckZelnodeTxSignatures(const CTransaction& transaction);
bool CheckBenchmarkSignature(const CTransaction& transaction);
bool IsMigrationCollateralAmount(const CAmount& amount);

// Locations
enum {
Expand Down Expand Up @@ -394,7 +398,7 @@ class ZelnodeCache {
bool InStartTracker(const COutPoint& out);
bool InDoSTracker(const COutPoint& out);
bool InConfirmTracker(const COutPoint& out);
bool CheckIfNeedsNextConfirm(const COutPoint& out);
bool CheckIfNeedsNextConfirm(const COutPoint& out, const int& p_nHeight);

bool GetNextPayment(CTxDestination& dest, int nTier, COutPoint& p_zelnodeOut);

Expand Down Expand Up @@ -427,6 +431,7 @@ class ZelnodeCache {
void DumpZelnodeCache();

void CountNetworks(int& ipv4, int& ipv6, int& onion, std::vector<int>& vNodeCount);
void CountMigration(int& nOldTotal, int& nNewTotal, std::vector<int>& vOldNodeCount, std::vector<int>& vNewNodeCount);

bool CheckConfirmationHeights(const int nHeight, const COutPoint& out, const std::string& ip);
};
Expand Down

0 comments on commit b19d6c7

Please sign in to comment.