Skip to content

Commit

Permalink
Merge branch 'DASHD-develop' of https://github.com/decenomy/DSW
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-at-decenomy committed Jan 25, 2023
2 parents 1da5299 + 1272597 commit 57a8c74
Show file tree
Hide file tree
Showing 23 changed files with 307 additions and 142 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dnl require autoconf 2.60 (AS_ECHO/AS_ECHO_N)
AC_PREREQ([2.60])
define(_CLIENT_VERSION_MAJOR, 2)
define(_CLIENT_VERSION_MINOR, 1)
define(_CLIENT_VERSION_REVISION, 1)
define(_CLIENT_VERSION_REVISION, 2)
define(_CLIENT_VERSION_BUILD, 0)
define(_CLIENT_VERSION_RC, 0)
define(_CLIENT_VERSION_IS_RELEASE, true)
Expand Down
2 changes: 1 addition & 1 deletion src/activemasternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void CActiveMasternode::ManageStatus()
pmn = mnodeman.Find(pubKeyMasternode);
if (pmn != nullptr) {
pmn->Check();
if (pmn->IsEnabled() && pmn->protocolVersion == PROTOCOL_VERSION)
if (pmn->IsEnabled())
EnableHotColdMasterNode(pmn->vin, pmn->addr);
}
}
Expand Down
47 changes: 32 additions & 15 deletions src/activemasternodeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,18 @@

CActiveMasternodeConfig activeMasternodeConfig;

CActiveMasternodeConfig::CActiveMasternodeEntry& CActiveMasternodeConfig::add(std::string strAlias, std::string strMasterNodePrivKey)
void CActiveMasternodeConfig::add(std::string strAlias, std::string strMasterNodePrivKey)
{
CActiveMasternodeEntry cme(strAlias, strMasterNodePrivKey);
vEntries.push_back(cme);
return vEntries.back();
}

void CActiveMasternodeConfig::remove(std::string strAlias)
bool CActiveMasternodeConfig::Load(std::string& strErr)
{
int pos = -1;
for (int i = 0; i < ((int)vEntries.size()); ++i) {
CActiveMasternodeEntry e = vEntries[i];
if (e.strAlias == strAlias) {
pos = i;
break;
}
}
vEntries.erase(vEntries.begin() + pos);
}
auto backup = vEntries;

vEntries.clear();

bool CActiveMasternodeConfig::read(std::string& strErr)
{
int linenumber = 1;
fs::path pathActiveMasternodeConfigFile = GetActiveMasternodeConfigFile();
fs::ifstream streamConfig(pathActiveMasternodeConfigFile);
Expand Down Expand Up @@ -73,13 +63,15 @@ bool CActiveMasternodeConfig::read(std::string& strErr)
strErr = _("Could not parse activemasternode.conf") + "\n" +
strprintf(_("Line: %d"), linenumber) + "\n\"" + line + "\"";
streamConfig.close();
vEntries = backup;
return false;
}
}

if (strAlias.empty()) {
strErr = _("alias cannot be empty in activemasternode.conf");
streamConfig.close();
vEntries = backup;
return false;
}

Expand All @@ -89,3 +81,28 @@ bool CActiveMasternodeConfig::read(std::string& strErr)
streamConfig.close();
return true;
}

uint256 CActiveMasternodeConfig::GetFileHash() {
fs::path pathActiveMasternodeConfigFile = GetActiveMasternodeConfigFile();
fs::ifstream streamConfig(pathActiveMasternodeConfigFile);

if (!streamConfig.good()) {
return UINT256_ZERO;
}

//get length of file
streamConfig.seekg(0, streamConfig.end);
size_t length = streamConfig.tellg();
streamConfig.seekg(0, streamConfig.beg);

//read file
if (length > 0) {
std::vector<char> buffer;
buffer.resize(length);
streamConfig.read(&buffer[0], length);

return Hash(&buffer[0], &buffer[0] + length);
}

return UINT256_ZERO;
}
10 changes: 5 additions & 5 deletions src/activemasternodeconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifndef SRC_ACTIVEMASTERNODECONFIG_H_
#define SRC_ACTIVEMASTERNODECONFIG_H_

#include "uint256.h"
#include <string>
#include <vector>

Expand All @@ -29,17 +30,16 @@ class CActiveMasternodeConfig
}
};

void clear();
bool read(std::string& strErr);
CActiveMasternodeConfig::CActiveMasternodeEntry& add(std::string strAlias, std::string strMasterNodePrivKey);
void remove(std::string strAlias);
bool Load(std::string& strErr);
uint256 GetFileHash();

std::vector<CActiveMasternodeEntry>& getEntries()
std::vector<CActiveMasternodeEntry>& Entries()
{
return vEntries;
}
private:
std::vector<CActiveMasternodeEntry> vEntries;
void add(std::string strAlias, std::string strMasterNodePrivKey);
};

#endif /* SRC_ACTIVEMASTERNODECONFIG_H_ */
93 changes: 91 additions & 2 deletions src/activemasternodeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,106 @@
#include <vector>

void CActiveMasternodeMan::ManageStatus() {

LOCK(cs);

std::string strErr;
if(!Reload(strErr)) {
LogPrintf("CActiveMasternodeMan::ManageStatus() - %s\n", strErr);
}

for(auto& amn : vActiveMasternodes) {
amn.ManageStatus();
}
}

void CActiveMasternodeMan::ResetStatus() {

LOCK(cs);

std::string strErr;
if(!Reload(strErr)) {
LogPrintf("CActiveMasternodeMan::ManageStatus() - %s\n", strErr);
}

for(auto& amn : vActiveMasternodes) {
amn.ResetStatus();
}
}

void CActiveMasternodeMan::Add(CActiveMasternode activeMasternode) {
vActiveMasternodes.push_back(activeMasternode);
bool CActiveMasternodeMan::Add(CActiveMasternodeConfig::CActiveMasternodeEntry ame, std::string& strErr) {

LOCK(cs);

auto strAlias = ame.strAlias;
auto strMasterNodePrivKey = ame.strMasterNodePrivKey;

if (strAlias.empty()) {
strErr = _("alias cannot be empty in activemasternode.conf");
return false;
}

CActiveMasternode amn;

amn.strAlias = strAlias;
amn.strMasterNodePrivKey = strMasterNodePrivKey;

CKey key;
CPubKey pubkey;

if (!CMessageSigner::GetKeysFromSecret(amn.strMasterNodePrivKey, key, pubkey)) {
strErr = _("Invalid masternodeprivkey. Please see documenation.");
return false;
}

amn.pubKeyMasternode = pubkey;

vActiveMasternodes.push_back(amn);

return true;
}

bool CActiveMasternodeMan::Load(std::string& strErr) {

LOCK(cs);

if (!activeMasternodeConfig.Load(strErr)) return false;

fileHash = activeMasternodeConfig.GetFileHash();

for(auto& ame : activeMasternodeConfig.Entries()) {
if(!Add(ame, strErr)) return false;
}

return true;
}

bool CActiveMasternodeMan::Reload(std::string& strErr) {

LOCK(cs);

auto hash = activeMasternodeConfig.GetFileHash();

if(fileHash != hash) {
if (!activeMasternodeConfig.Load(strErr)) return false;

auto backup = vActiveMasternodes;

vActiveMasternodes.clear();
for(auto& ame : activeMasternodeConfig.Entries()) {
if(!Add(ame, strErr)) {
vActiveMasternodes.clear();
vActiveMasternodes = backup;
return false;
}
}

fileHash = hash;

ResetStatus();

return true;
}

return true;
}
12 changes: 11 additions & 1 deletion src/activemasternodeman.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define ACTIVEMASTERNODES_H

#include "activemasternode.h"
#include "activemasternodeconfig.h"

#include <vector>

Expand All @@ -16,12 +17,21 @@ class CActiveMasternodeMan
{
private:
std::vector<CActiveMasternode> vActiveMasternodes;
uint256 fileHash = UINT256_ZERO;
// critical section to protect the inner data structures
mutable RecursiveMutex cs;
public:
/// Manage status of all Masternodes
void ManageStatus();
void ResetStatus();
std::vector<CActiveMasternode>& GetActiveMasternodes() { return vActiveMasternodes; }
void Add(CActiveMasternode activeMasternode);
bool Add(CActiveMasternodeConfig::CActiveMasternodeEntry ame, std::string& strErr);
bool Add(std::string strAlias, std::string strMasterNodePrivKey, std::string& strErr) {
return Add(CActiveMasternodeConfig::CActiveMasternodeEntry(strAlias, strMasterNodePrivKey), strErr);
}
bool Load(std::string& strErr);
bool Reload(std::string& strErr);
std::size_t Count() { return vActiveMasternodes.size(); }
};

#endif //ACTIVEMASTERNODES_H
14 changes: 8 additions & 6 deletions src/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "chain.h"
#include "masternode.h"
#include "masternodeman.h"
#include "legacy/stakemodifier.h" // for ComputeNextStakeModifier


Expand Down Expand Up @@ -250,13 +251,14 @@ CScript* CBlockIndex::GetPaidPayee()
if(paidPayee == nullptr || paidPayee->empty()) {
CBlock block;
if (nHeight <= chainActive.Height() && ReadBlockFromDisk(block, this)) {
const auto& tx = block.vtx[block.IsProofOfWork() ? 0 : 1];
auto amount = CMasternode::GetMasternodePayment(nHeight);

for (const CTxOut& out : tx.vout) {
if (out.nValue == amount
) {
paidPayee = new CScript(out.scriptPubKey);
auto mnpayee = block.GetPaidPayee(nHeight, amount);

if(!mnpayee.empty()) {
paidPayee = new CScript(mnpayee);
auto pmn = mnodeman.Find(mnpayee);
if(pmn) {
pmn->lastPaid = GetBlockTime();
}
}
}
Expand Down
56 changes: 8 additions & 48 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -917,42 +917,6 @@ void InitLogging()
LogPrintf("DashDiamond version %s (%s)\n", version_string, CLIENT_DATE);
}

bool AppInitActiveMasternode(std::string strAlias, std::string strMasterNodePrivKey)
{
if (strAlias.empty()) {
return UIError(_("activemasternode alias cannot be empty"));
}

CActiveMasternode activeMasternode;

activeMasternode.strAlias = strAlias;

activeMasternode.strMasterNodePrivKey = strMasterNodePrivKey;

std::string errorMessage;

CKey key;
CPubKey pubkey;

if (!CMessageSigner::GetKeysFromSecret(activeMasternode.strMasterNodePrivKey, key, pubkey)) {
return UIError(_("Invalid masternodeprivkey. Please see documenation."));
}

activeMasternode.pubKeyMasternode = pubkey;

amnodeman.Add(activeMasternode);

return true;
}

bool AppInitActiveMasternode(CActiveMasternodeConfig::CActiveMasternodeEntry activeMasternodeEntry)
{
return AppInitActiveMasternode(
activeMasternodeEntry.strAlias,
activeMasternodeEntry.strMasterNodePrivKey
);
}

/** Initialize dashdiamond.
* @pre Parameters should be parsed and config file should be read.
*/
Expand Down Expand Up @@ -1726,19 +1690,15 @@ bool AppInit2()
if (fMasterNode) {
LogPrintf("IS MASTER NODE\n");

//legacy
if(!GetArg("-masternodeprivkey", "").empty())
{
if(!AppInitActiveMasternode("legacy", GetArg("-masternodeprivkey", ""))) return false;
} else {
// multinode
std::string strErr;
if (!activeMasternodeConfig.read(strErr)) {
return UIError(strprintf(_("Error reading active masternode configuration file: %s"), strErr));
}
std::string strErr;
if (!amnodeman.Load(strErr)) {
return UIError(strprintf(_("Error reading active masternode configuration file: %s"), strErr));
}

for(auto& ame : activeMasternodeConfig.getEntries()) {
if(!AppInitActiveMasternode(ame)) return false;
// legacy
if (amnodeman.Count() == 0 && !GetArg("-masternodeprivkey", "").empty()) {
if (!amnodeman.Add("legacy", GetArg("-masternodeprivkey", ""), strErr)) {
return UIError(strprintf(_("Error reading masternodeprivkey: %s"), strErr));
}
}
}
Expand Down
Loading

0 comments on commit 57a8c74

Please sign in to comment.