PIVX Core  5.6.99
P2P Digital Currency
spork.h
Go to the documentation of this file.
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;
27 
28 //
29 // Spork Classes
30 // Keep track of all of the network spork settings
31 //
32 
34 {
35 public:
37  int64_t nValue;
38  int64_t nTimeSigned;
39 
42  nSporkID((SporkId)0),
43  nValue(0),
44  nTimeSigned(0)
45  {}
46 
50  nValue(nValue),
52  { }
53 
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  SERIALIZE_METHODS(CSporkMessage, obj) { READWRITE(obj.nSporkID, obj.nValue, obj.nTimeSigned, obj.vchSig, obj.nMessVersion); }
67 };
68 
69 
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);
105  // Process GETSPORKS message
106  void ProcessGetSporks(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
107 };
108 
109 #endif // PIVX_SPORK_H
Information about a peer.
Definition: net.h:669
An encapsulated public key.
Definition: pubkey.h:44
Base Class for all signed messages on the network.
Definition: messagesigner.h:63
void ProcessGetSporks(CNode *pfrom, std::string &strCommand, CDataStream &vRecv)
Definition: spork.cpp:174
void LoadSporksFromDB()
Definition: spork.cpp:47
std::string strMasterPrivKey
Definition: spork.h:74
std::map< SporkId, CSporkMessage > mapSporksActive
Definition: spork.h:77
bool UpdateSpork(SporkId nSporkID, int64_t nValue)
Definition: spork.cpp:193
std::map< std::string, CSporkDef * > sporkDefsByName
Definition: spork.h:76
bool SetPrivKey(std::string strPrivKey)
Definition: spork.cpp:265
SporkId GetSporkIDByName(std::string strName)
Definition: spork.cpp:245
std::string GetSporkNameByID(SporkId id)
Definition: spork.cpp:255
std::map< SporkId, CSporkDef * > sporkDefsById
Definition: spork.h:75
std::string ToString() const
Definition: spork.cpp:290
int64_t GetSporkValue(SporkId nSporkID)
Definition: spork.cpp:226
bool ProcessSpork(CNode *pfrom, std::string &strCommand, CDataStream &vRecv, int &dosScore)
Definition: spork.cpp:89
RecursiveMutex cs
Definition: spork.h:73
int ProcessSporkMsg(CDataStream &vRecv)
Definition: spork.cpp:101
void Clear()
Definition: spork.cpp:40
bool IsSporkActive(SporkId nSporkID)
Definition: spork.cpp:220
void AddOrUpdateSporkMessage(const CSporkMessage &spork, bool flush=false)
Definition: spork.cpp:206
CSporkManager()
Definition: spork.cpp:32
SERIALIZE_METHODS(CSporkManager, obj)
Definition: spork.h:82
CSporkMessage()
Definition: spork.h:40
CSporkMessage(SporkId nSporkID, int64_t nValue, int64_t nTimeSigned)
Definition: spork.h:47
uint256 GetSignatureHash() const override
Definition: spork.cpp:296
const CPubKey GetPublicKey() const
Definition: spork.cpp:313
const CPubKey GetPublicKeyOld() const
Definition: spork.cpp:318
uint256 GetHash() const
Definition: spork.h:54
int64_t nTimeSigned
Definition: spork.h:38
SporkId nSporkID
Definition: spork.h:36
SERIALIZE_METHODS(CSporkMessage, obj)
Definition: spork.h:66
std::string GetStrMessage() const override
Definition: spork.cpp:306
void Relay()
Definition: spork.cpp:323
int64_t nValue
Definition: spork.h:37
256-bit opaque blob.
Definition: uint256.h:138
uint256 HashQuark(const T1 pbegin, const T1 pend)
Definition: hash.h:345
UniValue spork(const JSONRPCRequest &request)
Definition: misc.cpp:286
#define READWRITE(...)
Definition: serialize.h:183
std::vector< CSporkDef > sporkDefs
Definition: spork.cpp:16
std::map< uint256, CSporkMessage > mapSporks
Definition: spork.cpp:30
CSporkManager sporkManager
Definition: spork.cpp:29
SporkId
Definition: sporkid.h:14
#define BEGIN(a)
Utilities for converting data from/to strings.
#define END(a)