forked from bitcoin/bitcoin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete implementation of Proof-of-Service (not debugged)
- Loading branch information
Evan Duffield
committed
Mar 14, 2015
1 parent
3fdb010
commit bb7672f
Showing
13 changed files
with
427 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
|
||
|
||
|
||
#include "bignum.h" | ||
#include "sync.h" | ||
#include "net.h" | ||
#include "key.h" | ||
#include "util.h" | ||
#include "script.h" | ||
#include "base58.h" | ||
#include "protocol.h" | ||
#include "activemasternode.h" | ||
#include "masternodeman.h" | ||
#include "spork.h" | ||
#include <boost/lexical_cast.hpp> | ||
#include "masternodeman.h" | ||
|
||
using namespace std; | ||
using namespace boost; | ||
|
||
std::map<uint256, CMasternodeScanningError> mapMasternodeScanningErrors; | ||
CMasternodeScanning mnscan; | ||
|
||
/* | ||
Masternode - Proof of Service | ||
-- What it checks | ||
1.) Making sure Masternodes have their ports open | ||
2.) Are responding to requests made by the network | ||
-- How it works | ||
When a block comes in, DoMasternodePOS is executed if the client is a | ||
masternode. Using the deterministic ranking algorithm up to 1% of the masternode | ||
network is checked each block. | ||
A port is opened from Masternode A to Masternode B, if successful then nothing happens. | ||
If there is an error, a CMasternodeScanningError object is propagated with an error code. | ||
Errors are applied to the Masternodes and a score is incremented within the masternode object, | ||
after a threshold is met, the masternode goes into an error state. Each cycle the score is | ||
decreased, so if the masternode comes back online it will return to the list. | ||
Masternodes in a error state do not receive payment. | ||
-- Future expansion | ||
We want to be able to prove the nodes have many qualities such as a specific CPU speed, bandwidth, | ||
and dedicated storage. E.g. We could require a full node be a computer running 2GHz with 10GB of space. | ||
*/ | ||
|
||
void ProcessMessageMasternodePOS(CNode* pfrom, std::string& strCommand, CDataStream& vRecv) | ||
{ | ||
if(fLiteMode) return; //disable all darksend/masternode related functionality | ||
if(!IsSporkActive(SPORK_7_MASTERNODE_SCANNING)) return; | ||
if(IsInitialBlockDownload()) return; | ||
|
||
if (strCommand == "mnse") //Masternode Scanning Error | ||
{ | ||
LogPrintf("ProcessMessageMasternodePOS::mnse\n"); | ||
CDataStream vMsg(vRecv); | ||
CMasternodeScanningError mnse; | ||
vRecv >> mnse; | ||
|
||
CInv inv(MSG_MASTERNODE_SCANNING_ERROR, mnse.GetHash()); | ||
pfrom->AddInventoryKnown(inv); | ||
|
||
if(mapMasternodeScanningErrors.count(mnse.GetHash())){ | ||
return; | ||
} | ||
mapMasternodeScanningErrors.insert(make_pair(mnse.GetHash(), mnse)); | ||
|
||
if(!mnse.IsValid()) | ||
{ | ||
LogPrintf("MasternodePOS::mnse - Invalid object\n"); | ||
return; | ||
} | ||
|
||
// Lowest masternodes in rank check the highest each block | ||
int a = mnodeman.GetMasternodeRank(mnse.vinMasternodeA, mnse.nBlockHeight, MIN_MASTERNODE_POS_PROTO_VERSION); | ||
if(a > GetCountScanningPerBlock()) | ||
{ | ||
LogPrintf("MasternodePOS::mnse - MasternodeA ranking is too high\n"); | ||
return; | ||
} | ||
|
||
int b = mnodeman.GetMasternodeRank(mnse.vinMasternodeB, mnse.nBlockHeight, MIN_MASTERNODE_POS_PROTO_VERSION); | ||
if(b < mnodeman.CountMasternodesAboveProtocol(MIN_MASTERNODE_POS_PROTO_VERSION)-GetCountScanningPerBlock()) | ||
{ | ||
LogPrintf("MasternodePOS::mnse - MasternodeB ranking is too low\n"); | ||
return; | ||
} | ||
|
||
if(!mnse.SignatureValid()){ | ||
LogPrintf("MasternodePOS::mnse - Bad masternode message\n"); | ||
return; | ||
} | ||
|
||
CMasternode* pmn = mnodeman.Find(mnse.vinMasternodeB); | ||
if(pmn == NULL) return; | ||
|
||
|
||
pmn->ApplyScanningError(mnse); | ||
mnse.Relay(); | ||
} | ||
} | ||
|
||
// Returns how many masternodes are allowed to scan each block | ||
int GetCountScanningPerBlock() | ||
{ | ||
return std::max(1, mnodeman.CountMasternodesAboveProtocol(MIN_MASTERNODE_POS_PROTO_VERSION)/100); | ||
} | ||
|
||
|
||
void CMasternodeScanning::CleanMasternodeScanningErrors() | ||
{ | ||
if(chainActive.Tip() == NULL) return; | ||
|
||
std::map<uint256, CMasternodeScanningError>::iterator it = mapMasternodeScanningErrors.begin(); | ||
|
||
while(it != mapMasternodeScanningErrors.end()) { | ||
if(GetTime() > it->second.nExpiration){ //keep them for an hour | ||
LogPrintf("Removing old masternode scanning error %s\n", it->second.GetHash().ToString().c_str()); | ||
|
||
mapMasternodeScanningErrors.erase(it++); | ||
} else { | ||
it++; | ||
} | ||
} | ||
|
||
} | ||
|
||
// Check other masternodes to make sure they're running correctly | ||
void CMasternodeScanning::DoMasternodePOSChecks() | ||
{ | ||
if(!fMasterNode) return; | ||
|
||
int a = mnodeman.GetMasternodeRank(activeMasternode.vin, chainActive.Tip()->nHeight, MIN_MASTERNODE_POS_PROTO_VERSION); | ||
if(a > GetCountScanningPerBlock()){ | ||
// we don't need to do anything this block | ||
return; | ||
} | ||
|
||
// The lowest ranking nodes (Masternode A) check the highest ranking nodes (Masternode B) | ||
CMasternode* pmn = mnodeman.GetMasternodeByRank(mnodeman.CountMasternodesAboveProtocol(MIN_MASTERNODE_POS_PROTO_VERSION)-a, chainActive.Tip()->nHeight, MIN_MASTERNODE_POS_PROTO_VERSION); | ||
if(pmn == NULL) return; | ||
|
||
// -- first check : Port is open | ||
|
||
if(!ConnectNode((CAddress)pmn->addr, NULL, true)){ | ||
// we couldn't connect to the node, let's send a scanning error | ||
CMasternodeScanningError mnse(activeMasternode.vin, pmn->vin, SCANNING_ERROR_NO_RESPONSE, chainActive.Tip()->nHeight); | ||
mnse.Sign(); | ||
mnse.Relay(); | ||
} | ||
|
||
// success | ||
CMasternodeScanningError mnse(activeMasternode.vin, pmn->vin, SCANNING_SUCCESS, chainActive.Tip()->nHeight); | ||
mnse.Sign(); | ||
mnse.Relay(); | ||
} | ||
|
||
bool CMasternodeScanningError::SignatureValid() | ||
{ | ||
std::string errorMessage; | ||
std::string strMessage = vinMasternodeB.ToString() + vinMasternodeB.ToString() + | ||
boost::lexical_cast<std::string>(nBlockHeight) + boost::lexical_cast<std::string>(nErrorType); | ||
|
||
CMasternode* pmn = mnodeman.Find(vinMasternodeA); | ||
|
||
if(pmn == NULL) | ||
{ | ||
LogPrintf("CMasternodeScanningError::SignatureValid() - Unknown Masternode\n"); | ||
return false; | ||
} | ||
|
||
CScript pubkey; | ||
pubkey.SetDestination(pmn->pubkey2.GetID()); | ||
CTxDestination address1; | ||
ExtractDestination(pubkey, address1); | ||
CBitcoinAddress address2(address1); | ||
|
||
if(!darkSendSigner.VerifyMessage(pmn->pubkey2, vchMasterNodeSignature, strMessage, errorMessage)) { | ||
LogPrintf("CMasternodeScanningError::SignatureValid() - Verify message failed\n"); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool CMasternodeScanningError::Sign() | ||
{ | ||
std::string errorMessage; | ||
|
||
CKey key2; | ||
CPubKey pubkey2; | ||
std::string strMessage = vinMasternodeB.ToString() + vinMasternodeB.ToString() + | ||
boost::lexical_cast<std::string>(nBlockHeight) + boost::lexical_cast<std::string>(nErrorType); | ||
|
||
if(!darkSendSigner.SetKey(strMasterNodePrivKey, errorMessage, key2, pubkey2)) | ||
{ | ||
LogPrintf("CMasternodeScanningError::Sign() - ERROR: Invalid masternodeprivkey: '%s'\n", errorMessage.c_str()); | ||
return false; | ||
} | ||
|
||
CScript pubkey; | ||
pubkey.SetDestination(pubkey2.GetID()); | ||
CTxDestination address1; | ||
ExtractDestination(pubkey, address1); | ||
CBitcoinAddress address2(address1); | ||
//LogPrintf("signing pubkey2 %s \n", address2.ToString().c_str()); | ||
|
||
if(!darkSendSigner.SignMessage(strMessage, errorMessage, vchMasterNodeSignature, key2)) { | ||
LogPrintf("CMasternodeScanningError::Sign() - Sign message failed"); | ||
return false; | ||
} | ||
|
||
if(!darkSendSigner.VerifyMessage(pubkey2, vchMasterNodeSignature, strMessage, errorMessage)) { | ||
LogPrintf("CMasternodeScanningError::Sign() - Verify message failed"); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void CMasternodeScanningError::Relay() | ||
{ | ||
CInv inv(MSG_MASTERNODE_SCANNING_ERROR, GetHash()); | ||
|
||
vector<CInv> vInv; | ||
vInv.push_back(inv); | ||
LOCK(cs_vNodes); | ||
BOOST_FOREACH(CNode* pnode, vNodes){ | ||
pnode->PushMessage("inv", vInv); | ||
} | ||
} |
Oops, something went wrong.