Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

spork.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2019 The Dash Core developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef SPORK_H
6 #define SPORK_H
7 
8 #include <hash.h>
9 #include <net.h>
10 #include <utilstrencodings.h>
11 #include <key.h>
12 
13 #include <unordered_map>
14 #include <unordered_set>
15 
16 class CSporkMessage;
17 class CSporkManager;
18 
19 /*
20  Don't ever reuse these IDs for other sporks
21  - This would result in old clients getting confused about which spork is for what
22 */
23 enum SporkId : int32_t {
31 
33 };
34 template<> struct is_serializable_enum<SporkId> : std::true_type {};
35 
36 namespace std
37 {
38  template<> struct hash<SporkId>
39  {
40  std::size_t operator()(SporkId const& id) const noexcept
41  {
42  return std::hash<int>{}(id);
43  }
44  };
45 }
46 
47 struct CSporkDef
48 {
50  int64_t defaultValue{0};
51  std::string name;
52 };
53 
54 extern std::vector<CSporkDef> sporkDefs;
56 
76 {
77 private:
78  std::vector<unsigned char> vchSig;
79 
80 public:
82  int64_t nValue;
83  int64_t nTimeSigned;
84 
87  nValue(nValue),
89  {}
90 
92  nSporkID((SporkId)0),
93  nValue(0),
94  nTimeSigned(0)
95  {}
96 
97 
99 
100  template <typename Stream, typename Operation>
101  inline void SerializationOp(Stream& s, Operation ser_action) {
103  READWRITE(nValue);
105  READWRITE(vchSig);
106  }
107 
111  uint256 GetHash() const;
112 
118  uint256 GetSignatureHash() const;
119 
123  bool Sign(const CKey& key);
124 
129  bool CheckSignature(const CKeyID& pubKeyId) const;
130 
138  bool GetSignerKeyID(CKeyID& retKeyidSporkSigner);
139 
143  void Relay(CConnman& connman);
144 };
145 
152 {
153 private:
154  static const std::string SERIALIZATION_VERSION_STRING;
155 
156  std::unordered_map<SporkId, CSporkDef*> sporkDefsById;
157  std::unordered_map<std::string, CSporkDef*> sporkDefsByName;
158 
160  std::unordered_map<uint256, CSporkMessage> mapSporksByHash;
161  std::unordered_map<SporkId, std::map<CKeyID, CSporkMessage> > mapSporksActive;
162 
163  std::set<CKeyID> setSporkPubKeyIDs;
166 
171  bool SporkValueIsActive(SporkId nSporkID, int64_t& nActiveValueRet) const;
172 
173 public:
174 
175  CSporkManager();
176 
178 
179  template <typename Stream, typename Operation>
180  inline void SerializationOp(Stream& s, Operation ser_action) {
181  std::string strVersion;
182  if(ser_action.ForRead()) {
183  READWRITE(strVersion);
184  if (strVersion != SERIALIZATION_VERSION_STRING) {
185  return;
186  }
187  } else {
188  strVersion = SERIALIZATION_VERSION_STRING;
189  READWRITE(strVersion);
190  }
191  // we don't serialize pubkey ids because pubkeys should be
192  // hardcoded or be setted with cmdline or options, should
193  // not reuse pubkeys from previous dashd run
196  // we don't serialize private key to prevent its leakage
197  }
198 
205  void Clear();
206 
215  void CheckAndRemove();
216 
224  void ProcessSpork(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman);
225 
230  bool UpdateSpork(SporkId nSporkID, int64_t nValue, CConnman& connman);
231 
241  bool IsSporkActive(SporkId nSporkID);
242 
247  int64_t GetSporkValue(SporkId nSporkID);
248 
252  SporkId GetSporkIDByName(const std::string& strName);
253 
257  std::string GetSporkNameByID(SporkId nSporkID);
258 
267  bool GetSporkByHash(const uint256& hash, CSporkMessage &sporkRet);
268 
276  bool SetSporkAddress(const std::string& strAddress);
277 
285  bool SetMinSporkKeys(int minSporkKeys);
286 
294  bool SetPrivKey(const std::string& strPrivKey);
295 
299  std::string ToString() const;
300 };
301 
302 #endif
ADD_SERIALIZE_METHODS
Definition: spork.h:98
std::size_t operator()(SporkId const &id) const noexcept
Definition: spork.h:40
bool UpdateSpork(SporkId nSporkID, int64_t nValue, CConnman &connman)
UpdateSpork is used by the spork RPC command to set a new spork value, sign and broadcast the spork m...
Definition: spork.cpp:185
bool Sign(const CKey &key)
Sign will sign the spork message with the given key.
Definition: spork.cpp:339
#define READWRITE(obj)
Definition: serialize.h:165
If none of the specialized versions above matched and T is an enum, default to calling Serialize/Unse...
Definition: params.h:197
CCriticalSection cs
Definition: spork.h:159
SporkId GetSporkIDByName(const std::string &strName)
GetSporkIDByName returns the internal Spork ID given the spork name.
Definition: spork.cpp:235
std::string name
Definition: spork.h:51
int64_t nTimeSigned
Definition: spork.h:83
void ProcessSpork(CNode *pfrom, const std::string &strCommand, CDataStream &vRecv, CConnman &connman)
ProcessSpork is used to handle the &#39;getsporks&#39; and &#39;spork&#39; p2p messages.
Definition: spork.cpp:114
Definition: box.hpp:161
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:103
uint256 GetSignatureHash() const
GetSignatureHash returns the hash of the serialized spork message without the signature included...
Definition: spork.cpp:330
Sporks are network parameters used primarily to prevent forking and turn on/off certain features...
Definition: spork.h:75
CKey sporkPrivKey
Definition: spork.h:165
std::string ToString() const
ToString returns the string representation of the SporkManager.
Definition: spork.cpp:319
bool SetSporkAddress(const std::string &strAddress)
SetSporkAddress is used to set a public key ID which will be used to verify spork signatures...
Definition: spork.cpp:269
ADD_SERIALIZE_METHODS
Definition: spork.h:177
SporkId nSporkID
Definition: spork.h:81
std::unordered_map< SporkId, std::map< CKeyID, CSporkMessage > > mapSporksActive
Definition: spork.h:161
std::size_t size_t
Definition: bits.hpp:21
void CheckAndRemove()
CheckAndRemove is defined to fulfill an interface as part of the on-disk cache used to cache sporks b...
Definition: spork.cpp:69
CSporkManager sporkManager
Definition: spork.cpp:29
std::set< CKeyID > setSporkPubKeyIDs
Definition: spork.h:163
CSporkMessage(SporkId nSporkID, int64_t nValue, int64_t nTimeSigned)
Definition: spork.h:85
CSporkMessage()
Definition: spork.h:91
CSporkManager is a higher-level class which manages the node&#39;s spork messages, rules for which sporks...
Definition: spork.h:151
int64_t GetSporkValue(SporkId nSporkID)
GetSporkValue returns the spork value given a Spork ID.
Definition: spork.cpp:217
bool GetSporkByHash(const uint256 &hash, CSporkMessage &sporkRet)
GetSporkByHash returns a spork message given a hash of the spork message.
Definition: spork.cpp:255
std::string GetSporkNameByID(SporkId nSporkID)
GetSporkNameByID returns the spork name as a string, given a Spork ID.
Definition: spork.cpp:245
Definition: net.h:136
void SerializationOp(Stream &s, Operation ser_action)
Definition: spork.h:101
static const std::string SERIALIZATION_VERSION_STRING
Definition: spork.h:154
SporkId
Definition: spork.h:23
SporkId sporkId
Definition: spork.h:49
uint256 GetHash() const
GetHash returns the double-sha256 hash of the serialized spork message.
Definition: spork.cpp:325
void SerializationOp(Stream &s, Operation ser_action)
Definition: spork.h:180
256-bit opaque blob.
Definition: uint256.h:123
std::unordered_map< uint256, CSporkMessage > mapSporksByHash
Definition: spork.h:160
std::vector< unsigned char > vchSig
Definition: spork.h:78
bool GetSignerKeyID(CKeyID &retKeyidSporkSigner)
GetSignerKeyID is used to recover the spork address of the key used to sign this spork message...
Definition: spork.cpp:403
void Relay(CConnman &connman)
Relay is used to send this spork message to other peers.
Definition: spork.cpp:425
int64_t nValue
Definition: spork.h:82
std::vector< CSporkDef > sporkDefs
Definition: spork.cpp:19
bool CheckSignature(const CKeyID &pubKeyId) const
CheckSignature will ensure the spork signature matches the provided public key hash.
Definition: spork.cpp:379
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
std::unordered_map< std::string, CSporkDef * > sporkDefsByName
Definition: spork.h:157
int nMinSporkKeys
Definition: spork.h:164
void Clear()
Clear is used to clear all in-memory active spork messages.
Definition: spork.cpp:60
bool SporkValueIsActive(SporkId nSporkID, int64_t &nActiveValueRet) const
SporkValueIsActive is used to get the value agreed upon by the majority of signed spork messages for ...
Definition: spork.cpp:39
An encapsulated private key.
Definition: key.h:27
bool IsSporkActive(SporkId nSporkID)
IsSporkActive returns a bool for time-based sporks, and should be used to determine whether the spork...
Definition: spork.cpp:211
Information about a peer.
Definition: net.h:800
CSporkManager()
Definition: spork.cpp:31
bool SetMinSporkKeys(int minSporkKeys)
SetMinSporkKeys is used to set the required spork signer threshold, for a spork to be considered acti...
Definition: spork.cpp:281
bool SetPrivKey(const std::string &strPrivKey)
SetPrivKey is used to set a spork key to enable setting / signing of spork values.
Definition: spork.cpp:292
int64_t defaultValue
Definition: spork.h:50
Wrapped mutex: supports recursive locking, but no waiting TODO: We should move away from using the re...
Definition: sync.h:94
std::unordered_map< SporkId, CSporkDef * > sporkDefsById
Definition: spork.h:156
Released under the MIT license