Line data Source code
1 : // Copyright (c) 2014-2016 The Dash developers 2 : // Copyright (c) 2016-2021 The PIVX Core developers 3 : // Distributed under the MIT/X11 software license, see the accompanying 4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 : 6 : #ifndef PIVX_SPORK_H 7 : #define PIVX_SPORK_H 8 : 9 : #include "hash.h" 10 : #include "key.h" 11 : #include "key_io.h" 12 : #include "messagesigner.h" 13 : #include "net.h" 14 : #include "sporkid.h" 15 : #include "sync.h" 16 : #include "util/system.h" 17 : 18 : #include "protocol.h" 19 : 20 : 21 : class CSporkMessage; 22 : class CSporkManager; 23 : 24 : extern std::vector<CSporkDef> sporkDefs; 25 : extern std::map<uint256, CSporkMessage> mapSporks; 26 : extern CSporkManager sporkManager; 27 : 28 : // 29 : // Spork Classes 30 : // Keep track of all of the network spork settings 31 : // 32 : 33 9386 : class CSporkMessage : public CSignedMessage 34 : { 35 : public: 36 : SporkId nSporkID; 37 : int64_t nValue; 38 : int64_t nTimeSigned; 39 : 40 6286 : CSporkMessage() : 41 : CSignedMessage(), 42 : nSporkID((SporkId)0), 43 : nValue(0), 44 5822 : nTimeSigned(0) 45 : {} 46 : 47 1472 : CSporkMessage(SporkId nSporkID, int64_t nValue, int64_t nTimeSigned) : 48 : CSignedMessage(), 49 : nSporkID(nSporkID), 50 : nValue(nValue), 51 1472 : nTimeSigned(nTimeSigned) 52 : { } 53 : 54 480 : uint256 GetHash() const { return HashQuark(BEGIN(nSporkID), END(nTimeSigned)); } 55 : 56 : // override CSignedMessage functions 57 : uint256 GetSignatureHash() const override; 58 : std::string GetStrMessage() const override; 59 : 60 : // - gets Params().SporkPubkey() 61 : const CPubKey GetPublicKey() const; 62 : const CPubKey GetPublicKeyOld() const; 63 : 64 : void Relay(); 65 : 66 8341 : SERIALIZE_METHODS(CSporkMessage, obj) { READWRITE(obj.nSporkID, obj.nValue, obj.nTimeSigned, obj.vchSig, obj.nMessVersion); } 67 : }; 68 : 69 : 70 : class CSporkManager 71 : { 72 : private: 73 : mutable RecursiveMutex cs; 74 : std::string strMasterPrivKey; 75 : std::map<SporkId, CSporkDef*> sporkDefsById; 76 : std::map<std::string, CSporkDef*> sporkDefsByName; 77 : std::map<SporkId, CSporkMessage> mapSporksActive; 78 : 79 : public: 80 : CSporkManager(); 81 : 82 : SERIALIZE_METHODS(CSporkManager, obj) { READWRITE(obj.mapSporksActive); } 83 : 84 : void Clear(); 85 : void LoadSporksFromDB(); 86 : 87 : bool ProcessSpork(CNode* pfrom, std::string& strCommand, CDataStream& vRecv, int& dosScore); 88 : int64_t GetSporkValue(SporkId nSporkID); 89 : // Create/Sign/Relay the spork message, and update the maps 90 : bool UpdateSpork(SporkId nSporkID, int64_t nValue); 91 : // Add spork message to mapSporks and mapSporksActive. 92 : // if flush=true, save to DB as well 93 : void AddOrUpdateSporkMessage(const CSporkMessage& spork, bool flush = false); 94 : 95 : bool IsSporkActive(SporkId nSporkID); 96 : std::string GetSporkNameByID(SporkId id); 97 : SporkId GetSporkIDByName(std::string strName); 98 : 99 : bool SetPrivKey(std::string strPrivKey); 100 : std::string ToString() const; 101 : 102 : // Process SPORK message, returning the banning score (or 0 if no banning is needed) 103 : int ProcessSporkMsg(CDataStream& vRecv); 104 : int ProcessSporkMsg(CSporkMessage& spork); 105 : // Process GETSPORKS message 106 : void ProcessGetSporks(CNode* pfrom, std::string& strCommand, CDataStream& vRecv); 107 : }; 108 : 109 : #endif // PIVX_SPORK_H