neobytes/src/darksend-relay.cpp

116 lines
3.3 KiB
C++
Raw Normal View History

#include "darksend.h"
2015-03-02 00:09:33 +01:00
#include "darksend-relay.h"
#include "messagesigner.h"
2015-03-02 00:09:33 +01:00
CDarkSendRelay::CDarkSendRelay()
{
vinMasternode = CTxIn();
nBlockHeight = 0;
nRelayType = 0;
in = CTxIn();
out = CTxOut();
}
2015-03-05 16:08:03 +01:00
CDarkSendRelay::CDarkSendRelay(CTxIn& vinMasternodeIn, vector<unsigned char>& vchSigIn, int nBlockHeightIn, int nRelayTypeIn, CTxIn& in2, CTxOut& out2)
2015-03-02 00:09:33 +01:00
{
vinMasternode = vinMasternodeIn;
vchSig = vchSigIn;
nBlockHeight = nBlockHeightIn;
nRelayType = nRelayTypeIn;
in = in2;
out = out2;
}
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();
}
2015-03-05 16:08:03 +01:00
bool CDarkSendRelay::Sign(std::string strSharedKey)
2015-03-02 00:09:33 +01:00
{
std::string strError = "";
2015-03-02 00:09:33 +01:00
std::string strMessage = in.ToString() + out.ToString();
CKey key2;
CPubKey pubkey2;
if(!CMessageSigner::GetKeysFromSecret(strSharedKey, key2, pubkey2)) {
LogPrintf("CDarkSendRelay::Sign -- GetKeysFromSecret() failed, invalid shared key %s\n", strSharedKey);
2015-03-02 00:09:33 +01:00
return false;
}
if(!CMessageSigner::SignMessage(strMessage, vchSig2, key2)) {
LogPrintf("CDarkSendRelay::Sign -- SignMessage() failed\n");
2015-03-02 00:09:33 +01:00
return false;
}
if(!CMessageSigner::VerifyMessage(pubkey2, vchSig2, strMessage, strError)) {
LogPrintf("CDarkSendRelay::Sign -- VerifyMessage() failed, error: %s\n", strError);
2015-03-02 00:09:33 +01:00
return false;
}
return true;
}
bool CDarkSendRelay::VerifyMessage(std::string strSharedKey)
{
std::string strError = "";
2015-03-02 00:09:33 +01:00
std::string strMessage = in.ToString() + out.ToString();
CKey key2;
CPubKey pubkey2;
if(!CMessageSigner::GetKeysFromSecret(strSharedKey, key2, pubkey2)) {
LogPrintf("CDarkSendRelay::VerifyMessage -- GetKeysFromSecret() failed, invalid shared key %s\n", strSharedKey);
2015-03-02 00:09:33 +01:00
return false;
}
if(!CMessageSigner::VerifyMessage(pubkey2, vchSig2, strMessage, strError)) {
LogPrintf("CDarkSendRelay::VerifyMessage -- VerifyMessage() failed, error: %s\n", strError);
2015-03-02 00:09:33 +01:00
return false;
}
return true;
}
void CDarkSendRelay::Relay()
{
int nCount = std::min(mnodeman.CountEnabled(MIN_PRIVATESEND_PEER_PROTO_VERSION), 20);
2015-03-02 00:09:33 +01:00
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_PRIVATESEND_PEER_PROTO_VERSION);
2015-03-02 00:09:33 +01:00
if(pmn != NULL){
2015-03-02 00:09:33 +01:00
//printf("RelayThroughNode %s\n", pmn->addr.ToString().c_str());
CNode* pnode = ConnectNode((CAddress)pmn->addr, NULL);
if(pnode) {
2015-03-02 00:09:33 +01:00
//printf("Connected\n");
pnode->PushMessage("dsr", (*this));
return;
2015-03-02 00:09:33 +01:00
}
} else {
//printf("RelayThroughNode NULL\n");
}
}