neobytes/src/spork.cpp

240 lines
9.0 KiB
C++
Raw Normal View History

// Copyright (c) 2014-2016 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2015-02-09 21:54:51 +01:00
#include "darksend.h"
2015-02-12 05:05:09 +01:00
#include "main.h"
#include "spork.h"
2015-02-09 21:54:51 +01:00
#include <boost/lexical_cast.hpp>
2015-02-09 21:54:51 +01:00
class CSporkMessage;
class CSporkManager;
CSporkManager sporkManager;
2015-02-09 21:54:51 +01:00
std::map<uint256, CSporkMessage> mapSporks;
void CSporkManager::ProcessSpork(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
2015-02-09 21:54:51 +01:00
{
if(fLiteMode) return; // disable all Dash specific functionality
2015-02-09 21:54:51 +01:00
if (strCommand == NetMsgType::SPORK) {
2015-02-09 21:54:51 +01:00
CDataStream vMsg(vRecv);
CSporkMessage spork;
vRecv >> spork;
uint256 hash = spork.GetHash();
std::string strLogMsg;
{
LOCK(cs_main);
if(!chainActive.Tip()) return;
strLogMsg = strprintf("SPORK -- hash: %s id: %d value: %10d bestHeight: %d peer=%d", hash.ToString(), spork.nSporkID, spork.nValue, chainActive.Height(), pfrom->id);
}
if(mapSporksActive.count(spork.nSporkID)) {
if (mapSporksActive[spork.nSporkID].nTimeSigned >= spork.nTimeSigned) {
LogPrint("spork", "%s seen\n", strLogMsg);
2015-02-09 21:54:51 +01:00
return;
} else {
LogPrintf("%s updated\n", strLogMsg);
2015-02-09 21:54:51 +01:00
}
} else {
LogPrintf("%s new\n", strLogMsg);
2015-02-09 21:54:51 +01:00
}
if(!spork.CheckSignature()) {
LogPrintf("CSporkManager::ProcessSpork -- invalid signature\n");
2015-02-09 21:54:51 +01:00
Misbehaving(pfrom->GetId(), 100);
return;
}
2015-02-11 21:39:33 +01:00
mapSporks[hash] = spork;
2015-02-09 21:54:51 +01:00
mapSporksActive[spork.nSporkID] = spork;
spork.Relay();
2015-02-09 21:54:51 +01:00
2015-02-12 05:05:09 +01:00
//does a task if needed
ExecuteSpork(spork.nSporkID, spork.nValue);
} else if (strCommand == NetMsgType::GETSPORKS) {
2015-02-09 21:54:51 +01:00
std::map<int, CSporkMessage>::iterator it = mapSporksActive.begin();
while(it != mapSporksActive.end()) {
pfrom->PushMessage(NetMsgType::SPORK, it->second);
2015-02-09 21:54:51 +01:00
it++;
}
}
}
void CSporkManager::ExecuteSpork(int nSporkID, int nValue)
{
//correct fork via spork technology
if(nSporkID == SPORK_12_RECONSIDER_BLOCKS && nValue > 0) {
LogPrintf("CSporkManager::ExecuteSpork -- Reconsider Last %d Blocks\n", nValue);
ReprocessBlocks(nValue);
}
}
bool CSporkManager::UpdateSpork(int nSporkID, int64_t nValue)
{
CSporkMessage spork = CSporkMessage(nSporkID, nValue, GetTime());
if(spork.Sign(strMasterPrivKey)) {
spork.Relay();
mapSporks[spork.GetHash()] = spork;
mapSporksActive[nSporkID] = spork;
return true;
}
return false;
}
2015-02-09 21:54:51 +01:00
// grab the spork, otherwise say it's off
bool CSporkManager::IsSporkActive(int nSporkID)
2015-02-09 21:54:51 +01:00
{
int64_t r = -1;
2015-02-09 21:54:51 +01:00
if(mapSporksActive.count(nSporkID)){
2015-02-11 15:47:21 +01:00
r = mapSporksActive[nSporkID].nValue;
2015-02-09 21:54:51 +01:00
} else {
switch (nSporkID) {
case SPORK_2_INSTANTSEND_ENABLED: r = SPORK_2_INSTANTSEND_ENABLED_DEFAULT; break;
case SPORK_3_INSTANTSEND_BLOCK_FILTERING: r = SPORK_3_INSTANTSEND_BLOCK_FILTERING_DEFAULT; break;
case SPORK_5_INSTANTSEND_MAX_VALUE: r = SPORK_5_INSTANTSEND_MAX_VALUE_DEFAULT; break;
case SPORK_8_MASTERNODE_PAYMENT_ENFORCEMENT: r = SPORK_8_MASTERNODE_PAYMENT_ENFORCEMENT_DEFAULT; break;
case SPORK_9_SUPERBLOCKS_ENABLED: r = SPORK_9_SUPERBLOCKS_ENABLED_DEFAULT; break;
case SPORK_10_MASTERNODE_PAY_UPDATED_NODES: r = SPORK_10_MASTERNODE_PAY_UPDATED_NODES_DEFAULT; break;
case SPORK_12_RECONSIDER_BLOCKS: r = SPORK_12_RECONSIDER_BLOCKS_DEFAULT; break;
case SPORK_13_OLD_SUPERBLOCK_FLAG: r = SPORK_13_OLD_SUPERBLOCK_FLAG_DEFAULT; break;
default:
LogPrint("spork", "CSporkManager::IsSporkActive -- Unknown Spork ID %d\n", nSporkID);
r = 4070908800; // 2099-1-1 i.e. off by default
break;
}
2015-02-09 21:54:51 +01:00
}
return r < GetTime();
}
2015-02-11 15:47:21 +01:00
// grab the value of the spork on the network, or the default
int64_t CSporkManager::GetSporkValue(int nSporkID)
2015-02-11 15:47:21 +01:00
{
if (mapSporksActive.count(nSporkID))
return mapSporksActive[nSporkID].nValue;
switch (nSporkID) {
case SPORK_2_INSTANTSEND_ENABLED: return SPORK_2_INSTANTSEND_ENABLED_DEFAULT;
case SPORK_3_INSTANTSEND_BLOCK_FILTERING: return SPORK_3_INSTANTSEND_BLOCK_FILTERING_DEFAULT;
case SPORK_5_INSTANTSEND_MAX_VALUE: return SPORK_5_INSTANTSEND_MAX_VALUE_DEFAULT;
case SPORK_8_MASTERNODE_PAYMENT_ENFORCEMENT: return SPORK_8_MASTERNODE_PAYMENT_ENFORCEMENT_DEFAULT;
case SPORK_9_SUPERBLOCKS_ENABLED: return SPORK_9_SUPERBLOCKS_ENABLED_DEFAULT;
case SPORK_10_MASTERNODE_PAY_UPDATED_NODES: return SPORK_10_MASTERNODE_PAY_UPDATED_NODES_DEFAULT;
case SPORK_12_RECONSIDER_BLOCKS: return SPORK_12_RECONSIDER_BLOCKS_DEFAULT;
case SPORK_13_OLD_SUPERBLOCK_FLAG: return SPORK_13_OLD_SUPERBLOCK_FLAG_DEFAULT;
default:
LogPrint("spork", "CSporkManager::GetSporkValue -- Unknown Spork ID %d\n", nSporkID);
return -1;
2015-02-11 15:47:21 +01:00
}
}
int CSporkManager::GetSporkIDByName(std::string strName)
2015-02-12 05:05:09 +01:00
{
if (strName == "SPORK_2_INSTANTSEND_ENABLED") return SPORK_2_INSTANTSEND_ENABLED;
if (strName == "SPORK_3_INSTANTSEND_BLOCK_FILTERING") return SPORK_3_INSTANTSEND_BLOCK_FILTERING;
if (strName == "SPORK_5_INSTANTSEND_MAX_VALUE") return SPORK_5_INSTANTSEND_MAX_VALUE;
if (strName == "SPORK_8_MASTERNODE_PAYMENT_ENFORCEMENT") return SPORK_8_MASTERNODE_PAYMENT_ENFORCEMENT;
if (strName == "SPORK_9_SUPERBLOCKS_ENABLED") return SPORK_9_SUPERBLOCKS_ENABLED;
if (strName == "SPORK_10_MASTERNODE_PAY_UPDATED_NODES") return SPORK_10_MASTERNODE_PAY_UPDATED_NODES;
if (strName == "SPORK_12_RECONSIDER_BLOCKS") return SPORK_12_RECONSIDER_BLOCKS;
if (strName == "SPORK_13_OLD_SUPERBLOCK_FLAG") return SPORK_13_OLD_SUPERBLOCK_FLAG;
LogPrint("spork", "CSporkManager::GetSporkIDByName -- Unknown Spork name '%s'\n", strName);
return -1;
2015-08-03 01:08:37 +02:00
}
2015-08-02 23:59:28 +02:00
std::string CSporkManager::GetSporkNameByID(int nSporkID)
{
switch (nSporkID) {
case SPORK_2_INSTANTSEND_ENABLED: return "SPORK_2_INSTANTSEND_ENABLED";
case SPORK_3_INSTANTSEND_BLOCK_FILTERING: return "SPORK_3_INSTANTSEND_BLOCK_FILTERING";
case SPORK_5_INSTANTSEND_MAX_VALUE: return "SPORK_5_INSTANTSEND_MAX_VALUE";
case SPORK_8_MASTERNODE_PAYMENT_ENFORCEMENT: return "SPORK_8_MASTERNODE_PAYMENT_ENFORCEMENT";
case SPORK_9_SUPERBLOCKS_ENABLED: return "SPORK_9_SUPERBLOCKS_ENABLED";
case SPORK_10_MASTERNODE_PAY_UPDATED_NODES: return "SPORK_10_MASTERNODE_PAY_UPDATED_NODES";
case SPORK_12_RECONSIDER_BLOCKS: return "SPORK_12_RECONSIDER_BLOCKS";
case SPORK_13_OLD_SUPERBLOCK_FLAG: return "SPORK_13_OLD_SUPERBLOCK_FLAG";
default:
LogPrint("spork", "CSporkManager::GetSporkNameByID -- Unknown Spork ID %d\n", nSporkID);
return "Unknown";
}
2015-02-12 05:05:09 +01:00
}
bool CSporkManager::SetPrivKey(std::string strPrivKey)
2015-02-09 21:54:51 +01:00
{
CSporkMessage spork;
2015-02-09 21:54:51 +01:00
2016-09-30 00:19:00 +02:00
spork.Sign(strPrivKey);
if(spork.CheckSignature()){
// Test signing successful, proceed
LogPrintf("CSporkManager::SetPrivKey -- Successfully initialized as spork signer\n");
strMasterPrivKey = strPrivKey;
return true;
} else {
2015-02-09 21:54:51 +01:00
return false;
}
}
bool CSporkMessage::Sign(std::string strSignKey)
2015-02-09 21:54:51 +01:00
{
CKey key;
CPubKey pubkey;
std::string strError = "";
std::string strMessage = boost::lexical_cast<std::string>(nSporkID) + boost::lexical_cast<std::string>(nValue) + boost::lexical_cast<std::string>(nTimeSigned);
2015-02-09 21:54:51 +01:00
if(!darkSendSigner.GetKeysFromSecret(strSignKey, key, pubkey)) {
LogPrintf("CSporkMessage::Sign -- GetKeysFromSecret() failed, invalid spork key %s\n", strSignKey);
2015-02-09 21:54:51 +01:00
return false;
}
if(!darkSendSigner.SignMessage(strMessage, vchSig, key)) {
LogPrintf("CSporkMessage::Sign -- SignMessage() failed\n");
2015-02-09 21:54:51 +01:00
return false;
}
if(!darkSendSigner.VerifyMessage(pubkey, vchSig, strMessage, strError)) {
LogPrintf("CSporkMessage::Sign -- VerifyMessage() failed, error: %s\n", strError);
2015-02-09 21:54:51 +01:00
return false;
}
return true;
}
bool CSporkMessage::CheckSignature()
2015-02-09 21:54:51 +01:00
{
//note: need to investigate why this is failing
std::string strError = "";
std::string strMessage = boost::lexical_cast<std::string>(nSporkID) + boost::lexical_cast<std::string>(nValue) + boost::lexical_cast<std::string>(nTimeSigned);
CPubKey pubkey(ParseHex(Params().SporkPubKey()));
2015-02-09 21:54:51 +01:00
if(!darkSendSigner.VerifyMessage(pubkey, vchSig, strMessage, strError)) {
LogPrintf("CSporkMessage::CheckSignature -- VerifyMessage() failed, error: %s\n", strError);
2015-02-09 21:54:51 +01:00
return false;
}
return true;
2015-02-09 21:54:51 +01:00
}
void CSporkMessage::Relay()
2015-02-09 21:54:51 +01:00
{
CInv inv(MSG_SPORK, GetHash());
RelayInv(inv);
2015-04-03 00:51:08 +02:00
}