Skip to content

Commit

Permalink
Merge dashpay#917: Move broadcast creation to CMasternodeBroadcast
Browse files Browse the repository at this point in the history
8ee7b8a Move broadcast creation to CMasternodeBroadcast
  • Loading branch information
UdjinM6 authored and schinzelh committed Jul 29, 2016
1 parent 3e0eee4 commit 8f9544c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 83 deletions.
73 changes: 1 addition & 72 deletions src/activemasternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void CActiveMasternode::ManageStatus()

// send to all nodes
CMasternodeBroadcast mnb;
if(!CreateBroadcast(vin, service, keyCollateralAddress, pubKeyCollateralAddress, keyMasternode, pubKeyMasternode, errorMessage, mnb)) {
if(!CMasternodeBroadcast::Create(vin, service, keyCollateralAddress, pubKeyCollateralAddress, keyMasternode, pubKeyMasternode, errorMessage, mnb)) {
notCapableReason = "Error on CreateBroadcast: " + errorMessage;
LogPrintf("CActiveMasternode::ManageStatus() - %s\n", notCapableReason);
return;
Expand Down Expand Up @@ -193,77 +193,6 @@ bool CActiveMasternode::SendMasternodePing(std::string& errorMessage) {
notCapableReason = errorMessage;
return false;
}

}

bool CActiveMasternode::CreateBroadcast(std::string strService, std::string strKeyMasternode, std::string strTxHash, std::string strOutputIndex, std::string& errorMessage, CMasternodeBroadcast &mnb, bool fOffline) {
CTxIn vin;
CPubKey pubKeyCollateralAddress;
CKey keyCollateralAddress;
CPubKey pubKeyMasternode;
CKey keyMasternode;

//need correct blocks to send ping
if(!fOffline && !masternodeSync.IsBlockchainSynced()) {
errorMessage = "Sync in progress. Must wait until sync is complete to start Masternode";
LogPrintf("CActiveMasternode::CreateBroadcast() - %s\n", errorMessage);
return false;
}

if(!darkSendSigner.SetKey(strKeyMasternode, errorMessage, keyMasternode, pubKeyMasternode))
{
errorMessage = strprintf("Can't find keys for masternode %s - %s", strService, errorMessage);
LogPrintf("CActiveMasternode::CreateBroadcast() - %s\n", errorMessage);
return false;
}

if(!pwalletMain->GetMasternodeVinAndKeys(vin, pubKeyCollateralAddress, keyCollateralAddress, strTxHash, strOutputIndex)) {
errorMessage = strprintf("Could not allocate vin %s:%s for masternode %s", strTxHash, strOutputIndex, strService);
LogPrintf("CActiveMasternode::CreateBroadcast() - %s\n", errorMessage);
return false;
}

CService service = CService(strService);
int mainnetDefaultPort = Params(CBaseChainParams::MAIN).GetDefaultPort();
if(Params().NetworkIDString() == CBaseChainParams::MAIN) {
if(service.GetPort() != mainnetDefaultPort) {
errorMessage = strprintf("Invalid port %u for masternode %s - only %d is supported on mainnet.", service.GetPort(), strService, mainnetDefaultPort);
LogPrintf("CActiveMasternode::CreateBroadcast() - %s\n", errorMessage);
return false;
}
} else if(service.GetPort() == mainnetDefaultPort) {
errorMessage = strprintf("Invalid port %u for masternode %s - %d is only supported on mainnet.", service.GetPort(), strService, mainnetDefaultPort);
LogPrintf("CActiveMasternode::CreateBroadcast() - %s\n", errorMessage);
return false;
}

addrman.Add(CAddress(service), CNetAddr("127.0.0.1"), 2*60*60);

return CreateBroadcast(vin, CService(strService), keyCollateralAddress, pubKeyCollateralAddress, keyMasternode, pubKeyMasternode, errorMessage, mnb);
}

bool CActiveMasternode::CreateBroadcast(CTxIn vin, CService service, CKey keyCollateralAddress, CPubKey pubKeyCollateralAddress, CKey keyMasternode, CPubKey pubKeyMasternode, std::string &errorMessage, CMasternodeBroadcast &mnb) {
// wait for reindex and/or import to finish
if (fImporting || fReindex) return false;

CMasternodePing mnp(vin);
if(!mnp.Sign(keyMasternode, pubKeyMasternode)){
errorMessage = strprintf("Failed to sign ping, vin: %s", vin.ToString());
LogPrintf("CActiveMasternode::CreateBroadcast() - %s\n", errorMessage);
mnb = CMasternodeBroadcast();
return false;
}

mnb = CMasternodeBroadcast(service, vin, pubKeyCollateralAddress, pubKeyMasternode, PROTOCOL_VERSION);
mnb.lastPing = mnp;
if(!mnb.Sign(keyCollateralAddress)){
errorMessage = strprintf("Failed to sign broadcast, vin: %s", vin.ToString());
LogPrintf("CActiveMasternode::CreateBroadcast() - %s\n", errorMessage);
mnb = CMasternodeBroadcast();
return false;
}

return true;
}

// when starting a Masternode, this can enable to run as a hot wallet with no funds
Expand Down
5 changes: 0 additions & 5 deletions src/activemasternode.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ class CActiveMasternode
/// Ping Masternode
bool SendMasternodePing(std::string& errorMessage);

/// Create Masternode broadcast, needs to be relayed manually after that
bool CreateBroadcast(CTxIn vin, CService service, CKey key, CPubKey pubKey, CKey keyMasternode, CPubKey pubKeyMasternode, std::string &errorMessage, CMasternodeBroadcast &mnb);

public:
// Initialized by init.cpp
// Keys for the main Masternode
Expand All @@ -54,8 +51,6 @@ class CActiveMasternode
void ManageStatus();
std::string GetStatus();

/// Create Masternode broadcast, needs to be relayed manually after that
bool CreateBroadcast(std::string strService, std::string strKey, std::string strTxHash, std::string strOutputIndex, std::string& errorMessage, CMasternodeBroadcast &mnb, bool fOffline = false);

/// Enable cold wallet mode (run a Masternode with no funds)
bool EnableHotColdMasterNode(CTxIn& vin, CService& addr);
Expand Down
69 changes: 69 additions & 0 deletions src/masternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,75 @@ CMasternodeBroadcast::CMasternodeBroadcast(const CMasternode& mn)
nLastScanningErrorBlockHeight = mn.nLastScanningErrorBlockHeight;
}

bool CMasternodeBroadcast::Create(std::string strService, std::string strKeyMasternode, std::string strTxHash, std::string strOutputIndex, std::string& strErrorMessage, CMasternodeBroadcast &mnb, bool fOffline)
{
CTxIn txin;
CPubKey pubKeyCollateral;
CKey keyCollateral;
CPubKey pubKeyMasternodeNew;
CKey keyMasternodeNew;

//need correct blocks to send ping
if(!fOffline && !masternodeSync.IsBlockchainSynced()) {
strErrorMessage = "Sync in progress. Must wait until sync is complete to start Masternode";
LogPrintf("CMasternodeBroadcast::Create -- %s\n", strErrorMessage);
return false;
}

if(!darkSendSigner.SetKey(strKeyMasternode, strErrorMessage, keyMasternodeNew, pubKeyMasternodeNew)) {
strErrorMessage = strprintf("Can't find keys for masternode %s, error: %s", strService, strErrorMessage);
LogPrintf("CMasternodeBroadcast::Create -- %s\n", strErrorMessage);
return false;
}

if(!pwalletMain->GetMasternodeVinAndKeys(txin, pubKeyCollateral, keyCollateral, strTxHash, strOutputIndex)) {
strErrorMessage = strprintf("Could not allocate txin %s:%s for masternode %s", strTxHash, strOutputIndex, strService);
LogPrintf("CMasternodeBroadcast::Create -- %s\n", strErrorMessage);
return false;
}

CService service = CService(strService);
uint16_t mainnetDefaultPort = Params(CBaseChainParams::MAIN).GetDefaultPort();
if(Params().NetworkIDString() == CBaseChainParams::MAIN) {
if(service.GetPort() != mainnetDefaultPort) {
strErrorMessage = strprintf("Invalid port %u for masternode %s, only %u is supported on mainnet.", service.GetPort(), strService, mainnetDefaultPort);
LogPrintf("CMasternodeBroadcast::Create -- %s\n", strErrorMessage);
return false;
}
} else if(service.GetPort() == mainnetDefaultPort) {
strErrorMessage = strprintf("Invalid port %u for masternode %s, %u is the only supported on mainnet.", service.GetPort(), strService, mainnetDefaultPort);
LogPrintf("CMasternodeBroadcast::Create -- %s\n", strErrorMessage);
return false;
}

return Create(txin, CService(strService), keyCollateral, pubKeyCollateral, keyMasternodeNew, pubKeyMasternodeNew, strErrorMessage, mnb);
}

bool CMasternodeBroadcast::Create(CTxIn txin, CService service, CKey keyCollateral, CPubKey pubKeyCollateral, CKey keyMasternodeNew, CPubKey pubKeyMasternodeNew, std::string &strErrorMessage, CMasternodeBroadcast &mnb)
{
// wait for reindex and/or import to finish
if (fImporting || fReindex) return false;

CMasternodePing mnp(txin);
if(!mnp.Sign(keyMasternodeNew, pubKeyMasternodeNew)) {
strErrorMessage = strprintf("Failed to sign ping: %s", txin.ToString());
LogPrintf("CMasternodeBroadcast::Create -- %s\n", strErrorMessage);
mnb = CMasternodeBroadcast();
return false;
}

mnb = CMasternodeBroadcast(service, txin, pubKeyCollateral, pubKeyMasternodeNew, PROTOCOL_VERSION);
mnb.lastPing = mnp;
if(!mnb.Sign(keyCollateral)) {
strErrorMessage = strprintf("Failed to sign broadcast: %s", txin.ToString());
LogPrintf("CMasternodeBroadcast::Create -- %s\n", strErrorMessage);
mnb = CMasternodeBroadcast();
return false;
}

return true;
}

bool CMasternodeBroadcast::CheckAndUpdate(int& nDos)
{
nDos = 0;
Expand Down
4 changes: 4 additions & 0 deletions src/masternode.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ class CMasternodeBroadcast : public CMasternode
CMasternodeBroadcast(CService newAddr, CTxIn newVin, CPubKey newPubkey, CPubKey newPubkey2, int protocolVersionIn);
CMasternodeBroadcast(const CMasternode& mn);

/// Create Masternode broadcast, needs to be relayed manually after that
static bool Create(CTxIn txin, CService service, CKey keyCollateral, CPubKey pubKeyCollateral, CKey keyMasternodeNew, CPubKey pubKeyMasternodeNew, std::string &strErrorMessage, CMasternodeBroadcast &mnb);
static bool Create(std::string strService, std::string strKey, std::string strTxHash, std::string strOutputIndex, std::string& strErrorMessage, CMasternodeBroadcast &mnb, bool fOffline = false);

bool CheckAndUpdate(int& nDos);
bool CheckInputsAndAdd(int& nDos);
bool Sign(CKey& keyCollateralAddress);
Expand Down
4 changes: 2 additions & 2 deletions src/qt/masternodelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void MasternodeList::StartAlias(std::string strAlias)
std::string errorMessage;
CMasternodeBroadcast mnb;

bool result = activeMasternode.CreateBroadcast(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), errorMessage, mnb);
bool result = CMasternodeBroadcast::Create(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), errorMessage, mnb);

if(result) {
statusObj += "<br>Successfully started masternode." ;
Expand Down Expand Up @@ -135,7 +135,7 @@ void MasternodeList::StartAll(std::string strCommand)

if(strCommand == "start-missing" && pmn) continue;

bool result = activeMasternode.CreateBroadcast(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), errorMessage, mnb);
bool result = CMasternodeBroadcast::Create(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), errorMessage, mnb);

if(result) {
successful++;
Expand Down
8 changes: 4 additions & 4 deletions src/rpcmasternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ UniValue masternode(const UniValue& params, bool fHelp)
std::string errorMessage;
CMasternodeBroadcast mnb;

bool result = activeMasternode.CreateBroadcast(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), errorMessage, mnb);
bool result = CMasternodeBroadcast::Create(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), errorMessage, mnb);

statusObj.push_back(Pair("result", result ? "successful" : "failed"));
if(result) {
Expand Down Expand Up @@ -341,7 +341,7 @@ UniValue masternode(const UniValue& params, bool fHelp)
if(strCommand == "start-missing" && pmn) continue;
if(strCommand == "start-disabled" && pmn && pmn->IsEnabled()) continue;

bool result = activeMasternode.CreateBroadcast(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), errorMessage, mnb);
bool result = CMasternodeBroadcast::Create(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), errorMessage, mnb);

UniValue statusObj(UniValue::VOBJ);
statusObj.push_back(Pair("alias", mne.getAlias()));
Expand Down Expand Up @@ -684,7 +684,7 @@ UniValue masternodebroadcast(const UniValue& params, bool fHelp)
std::string errorMessage;
CMasternodeBroadcast mnb;

bool result = activeMasternode.CreateBroadcast(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), errorMessage, mnb, true);
bool result = CMasternodeBroadcast::Create(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), errorMessage, mnb, true);

statusObj.push_back(Pair("result", result ? "successful" : "failed"));
if(result) {
Expand Down Expand Up @@ -745,7 +745,7 @@ UniValue masternodebroadcast(const UniValue& params, bool fHelp)
CTxIn vin = CTxIn(uint256S(mne.getTxHash()), uint32_t(atoi(mne.getOutputIndex().c_str())));
CMasternodeBroadcast mnb;

bool result = activeMasternode.CreateBroadcast(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), errorMessage, mnb, true);
bool result = CMasternodeBroadcast::Create(mne.getIp(), mne.getPrivKey(), mne.getTxHash(), mne.getOutputIndex(), errorMessage, mnb, true);

UniValue statusObj(UniValue::VOBJ);
statusObj.push_back(Pair("alias", mne.getAlias()));
Expand Down

0 comments on commit 8f9544c

Please sign in to comment.