132 lines
3.6 KiB
C++
132 lines
3.6 KiB
C++
|
|
||
|
#include "darksend-relay.h"
|
||
|
|
||
|
|
||
|
CDarkSendRelay::CDarkSendRelay()
|
||
|
{
|
||
|
vinMasternode = CTxIn();
|
||
|
nBlockHeight = 0;
|
||
|
nRelayType = 0;
|
||
|
in = CTxIn();
|
||
|
out = CTxOut();
|
||
|
strSharedKey = "";
|
||
|
}
|
||
|
|
||
|
CDarkSendRelay::CDarkSendRelay(CTxIn& vinMasternodeIn, vector<unsigned char>& vchSigIn, int nBlockHeightIn, int nRelayTypeIn, CTxIn& in2, CTxOut& out2, std::string strSharedKeyIn)
|
||
|
{
|
||
|
vinMasternode = vinMasternodeIn;
|
||
|
vchSig = vchSigIn;
|
||
|
nBlockHeight = nBlockHeightIn;
|
||
|
nRelayType = nRelayTypeIn;
|
||
|
in = in2;
|
||
|
out = out2;
|
||
|
strSharedKey = strSharedKeyIn;
|
||
|
}
|
||
|
|
||
|
std::string CDarkSendRelay::ToString()
|
||
|
{
|
||
|
std::ostringstream info;
|
||
|
|
||
|
info << "vin: " << vinMasternode.ToString() <<
|
||
|
" nBlockHeight: " << (int)nBlockHeight <<
|
||
|
" nRelayType: " << (int)nRelayType <<
|
||
|
" in " << in.ToString() <<
|
||
|
" out " << out.ToString();
|
||
|
|
||
|
return info.str();
|
||
|
}
|
||
|
|
||
|
bool CDarkSendRelay::Sign()
|
||
|
{
|
||
|
if(!fMasterNode) return false;
|
||
|
|
||
|
std::string strMessage = in.ToString() + out.ToString();
|
||
|
|
||
|
CKey key2;
|
||
|
CPubKey pubkey2;
|
||
|
std::string errorMessage = "";
|
||
|
|
||
|
if(!darkSendSigner.SetKey(strSharedKey, errorMessage, key2, pubkey2))
|
||
|
{
|
||
|
LogPrintf("CDarkSendRelay():Relay - ERROR: Invalid masternodeprivkey: '%s'\n", errorMessage.c_str());
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if(!darkSendSigner.SignMessage(strMessage, errorMessage, vchSig2, key2)) {
|
||
|
LogPrintf("CDarkSendRelay():Relay - Sign message failed");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if(!darkSendSigner.VerifyMessage(pubkey2, vchSig, strMessage, errorMessage)) {
|
||
|
LogPrintf("CDarkSendRelay():Relay - Verify message failed");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool CDarkSendRelay::VerifyMessage(std::string strSharedKey)
|
||
|
{
|
||
|
if(!fMasterNode) return false;
|
||
|
|
||
|
std::string strMessage = in.ToString() + out.ToString();
|
||
|
|
||
|
CKey key2;
|
||
|
CPubKey pubkey2;
|
||
|
std::string errorMessage = "";
|
||
|
|
||
|
if(!darkSendSigner.SetKey(strSharedKey, errorMessage, key2, pubkey2))
|
||
|
{
|
||
|
LogPrintf("CDarkSendRelay():Relay - ERROR: Invalid masternodeprivkey: '%s'\n", errorMessage.c_str());
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if(!darkSendSigner.SignMessage(strMessage, errorMessage, vchSig2, key2)) {
|
||
|
LogPrintf("CDarkSendRelay():Relay - Sign message failed");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if(!darkSendSigner.VerifyMessage(pubkey2, vchSig, strMessage, errorMessage)) {
|
||
|
LogPrintf("CDarkSendRelay():Relay - Verify message failed");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void CDarkSendRelay::Relay()
|
||
|
{
|
||
|
int nCount = std::min(mnodeman.CountEnabled(), 20);
|
||
|
int nRank1 = (rand() % nCount)+1;
|
||
|
int nRank2 = (rand() % nCount)+1;
|
||
|
|
||
|
//keep picking another second number till we get one that doesn't match
|
||
|
while(nRank1 == nRank2) nRank2 = (rand() % nCount)+1;
|
||
|
|
||
|
//printf("rank 1 - rank2 %d %d \n", nRank1, nRank2);
|
||
|
|
||
|
//relay this message through 2 separate nodes for redundancy
|
||
|
RelayThroughNode(nRank1);
|
||
|
RelayThroughNode(nRank2);
|
||
|
}
|
||
|
|
||
|
void CDarkSendRelay::RelayThroughNode(int nRank)
|
||
|
{
|
||
|
CMasternode* pmn = mnodeman.GetMasternodeByRank(nRank, nBlockHeight, MIN_POOL_PEER_PROTO_VERSION);
|
||
|
|
||
|
if(pmn){
|
||
|
//printf("RelayThroughNode %s\n", pmn->addr.ToString().c_str());
|
||
|
if(ConnectNode((CAddress)pmn->addr, NULL, true)){
|
||
|
//printf("Connected\n");
|
||
|
CNode* pNode = FindNode(pmn->addr);
|
||
|
if(pNode)
|
||
|
{
|
||
|
//printf("Found\n");
|
||
|
pNode->PushMessage("dsr", (*this));
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
//printf("RelayThroughNode NULL\n");
|
||
|
}
|
||
|
}
|