Raven Core  3.0.0
P2P Digital Currency
assettypes.h
Go to the documentation of this file.
1 // Copyright (c) 2019 The Raven Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef RAVENCOIN_NEWASSET_H
6 #define RAVENCOIN_NEWASSET_H
7 
8 #include <string>
9 #include <sstream>
10 #include <list>
11 #include <unordered_map>
12 #include "amount.h"
13 #include "script/standard.h"
14 #include "primitives/transaction.h"
15 
16 #define MAX_UNIT 8
17 #define MIN_UNIT 0
18 
19 class CAssetsCache;
20 
21 enum class AssetType
22 {
23  ROOT = 0,
24  SUB = 1,
25  UNIQUE = 2,
26  OWNER = 3,
27  MSGCHANNEL = 4,
28  VOTE = 5,
29  REISSUE = 6,
30  QUALIFIER = 7,
31  SUB_QUALIFIER = 8,
32  RESTRICTED = 9,
33  NULL_ADD_QUALIFIER = 10,
34  INVALID = 11
35 };
36 
37 enum class QualifierType
38 {
39  REMOVE_QUALIFIER = 0,
40  ADD_QUALIFIER = 1
41 };
42 
43 enum class RestrictedType
44 {
45  UNFREEZE_ADDRESS = 0,
46  FREEZE_ADDRESS= 1,
47  GLOBAL_UNFREEZE = 2,
48  GLOBAL_FREEZE = 3
49 };
50 
51 int IntFromAssetType(AssetType type);
52 AssetType AssetTypeFromInt(int nType);
53 
54 const char IPFS_SHA2_256 = 0x12;
55 const char TXID_NOTIFIER = 0x54;
56 const char IPFS_SHA2_256_LEN = 0x20;
57 
58 template <typename Stream, typename Operation>
59 bool ReadWriteAssetHash(Stream &s, Operation ser_action, std::string &strIPFSHash)
60 {
61  // assuming 34-byte IPFS SHA2-256 decoded hash (0x12, 0x20, 32 more bytes)
62  if (ser_action.ForRead())
63  {
64  strIPFSHash = "";
65  if (!s.empty() and s.size() >= 33) {
66  char _sha2_256;
67  ::Unserialize(s, _sha2_256);
68  std::basic_string<char> hash;
69  ::Unserialize(s, hash);
70 
71  std::ostringstream os;
72 
73  // If it is an ipfs hash, we put the Q and the m 'Qm' at the front
74  if (_sha2_256 == IPFS_SHA2_256)
76 
77  os << hash.substr(0, 32); // Get the 32 bytes of data
78  strIPFSHash = os.str();
79  return true;
80  }
81  }
82  else
83  {
84  if (strIPFSHash.length() == 34) {
86  ::Serialize(s, strIPFSHash.substr(2));
87  return true;
88  } else if (strIPFSHash.length() == 32) {
90  ::Serialize(s, strIPFSHash);
91  return true;
92  }
93  }
94  return false;
95 };
96 
97 class CNewAsset
98 {
99 public:
100  std::string strName; // MAX 31 Bytes
101  CAmount nAmount; // 8 Bytes
102  int8_t units; // 1 Byte
103  int8_t nReissuable; // 1 Byte
104  int8_t nHasIPFS; // 1 Byte
105  std::string strIPFSHash; // MAX 40 Bytes
106 
108  {
109  SetNull();
110  }
111 
112  CNewAsset(const std::string& strName, const CAmount& nAmount, const int& units, const int& nReissuable, const int& nHasIPFS, const std::string& strIPFSHash);
113  CNewAsset(const std::string& strName, const CAmount& nAmount);
114 
115  CNewAsset(const CNewAsset& asset);
116  CNewAsset& operator=(const CNewAsset& asset);
117 
118  void SetNull()
119  {
120  strName= "";
121  nAmount = 0;
122  units = int8_t(MAX_UNIT);
123  nReissuable = int8_t(0);
124  nHasIPFS = int8_t(0);
125  strIPFSHash = "";
126  }
127 
128  bool IsNull() const;
129  std::string ToString();
130 
131  void ConstructTransaction(CScript& script) const;
132  void ConstructOwnerTransaction(CScript& script) const;
133 
135 
136  template <typename Stream, typename Operation>
137  inline void SerializationOp(Stream& s, Operation ser_action)
138  {
139  READWRITE(strName);
140  READWRITE(nAmount);
141  READWRITE(units);
142  READWRITE(nReissuable);
143  READWRITE(nHasIPFS);
144  if (nHasIPFS == 1) {
145  ReadWriteAssetHash(s, ser_action, strIPFSHash);
146  }
147  }
148 };
149 
151 {
152 public:
153  bool operator()(const CNewAsset& s1, const CNewAsset& s2) const
154  {
155  return s1.strName < s2.strName;
156  }
157 };
158 
160 {
161 public:
163  int nHeight;
165 
166  CDatabasedAssetData(const CNewAsset& asset, const int& nHeight, const uint256& blockHash);
168 
169  void SetNull()
170  {
171  asset.SetNull();
172  nHeight = -1;
173  blockHash = uint256();
174  }
175 
177 
178  template <typename Stream, typename Operation>
179  inline void SerializationOp(Stream& s, Operation ser_action)
180  {
181  READWRITE(asset);
182  READWRITE(nHeight);
183  READWRITE(blockHash);
184  }
185 };
186 
188 {
189 public:
190  std::string strName;
192  std::string message;
193  int64_t nExpireTime;
194 
196  {
197  SetNull();
198  }
199 
200  void SetNull()
201  {
202  nAmount = 0;
203  strName = "";
204  message = "";
205  nExpireTime = 0;
206  }
207 
209 
210  template <typename Stream, typename Operation>
211  inline void SerializationOp(Stream& s, Operation ser_action)
212  {
213  READWRITE(strName);
214  READWRITE(nAmount);
215  bool validIPFS = ReadWriteAssetHash(s, ser_action, message);
216  if (validIPFS) {
217  if (ser_action.ForRead()) {
218  if (!s.empty() && s.size() >= sizeof(int64_t)) {
219  ::Unserialize(s, nExpireTime);
220  }
221  } else {
222  if (nExpireTime != 0) {
223  ::Serialize(s, nExpireTime);
224  }
225  }
226  }
227 
228  }
229 
230  CAssetTransfer(const std::string& strAssetName, const CAmount& nAmount, const std::string& message = "", const int64_t& nExpireTime = 0);
231  bool IsValid(std::string& strError) const;
232  void ConstructTransaction(CScript& script) const;
233  bool ContextualCheckAgainstVerifyString(CAssetsCache *assetCache, const std::string& address, std::string& strError) const;
234 };
235 
237 {
238 public:
239  std::string strName;
241  int8_t nUnits;
242  int8_t nReissuable;
243  std::string strIPFSHash;
244 
246  {
247  SetNull();
248  }
249 
250  void SetNull()
251  {
252  nAmount = 0;
253  strName = "";
254  nUnits = 0;
255  nReissuable = 1;
256  strIPFSHash = "";
257  }
258 
260 
261  template <typename Stream, typename Operation>
262  inline void SerializationOp(Stream& s, Operation ser_action)
263  {
264  READWRITE(strName);
265  READWRITE(nAmount);
266  READWRITE(nUnits);
267  READWRITE(nReissuable);
268  ReadWriteAssetHash(s, ser_action, strIPFSHash);
269  }
270 
271  CReissueAsset(const std::string& strAssetName, const CAmount& nAmount, const int& nUnits, const int& nReissuable, const std::string& strIPFSHash);
272  void ConstructTransaction(CScript& script) const;
273  bool IsNull() const;
274 };
275 
277 public:
278  std::string asset_name;
279  int8_t flag; // on/off but could be used to determine multiple options later on
280 
282  {
283  SetNull();
284  }
285 
286  void SetNull()
287  {
288  flag = -1;
289  asset_name = "";
290  }
291 
293 
294  template <typename Stream, typename Operation>
295  inline void SerializationOp(Stream& s, Operation ser_action)
296  {
297  READWRITE(asset_name);
298  READWRITE(flag);
299  }
300 
301  CNullAssetTxData(const std::string& strAssetname, const int8_t& nFlag);
302  bool IsValid(std::string& strError, CAssetsCache& assetCache, bool fForceCheckPrimaryAssetExists) const;
303  void ConstructTransaction(CScript& script) const;
304  void ConstructGlobalRestrictionTransaction(CScript &script) const;
305 };
306 
308 
309 public:
310  std::string verifier_string;
311 
313  {
314  SetNull();
315  }
316 
317  void SetNull()
318  {
319  verifier_string ="";
320  }
321 
323 
324  template <typename Stream, typename Operation>
325  inline void SerializationOp(Stream& s, Operation ser_action)
326  {
327  READWRITE(verifier_string);
328  }
329 
330  CNullAssetTxVerifierString(const std::string& verifier);
331  void ConstructTransaction(CScript& script) const;
332 };
333 
336 {
338  std::string address;
341 
342  CAssetCacheNewAsset(const CNewAsset& asset, const std::string& address, const int& blockHeight, const uint256& blockHash)
343  {
344  this->asset = asset;
345  this->address = address;
346  this->blockHash = blockHash;
347  this->blockHeight = blockHeight;
348  }
349 
350  bool operator<(const CAssetCacheNewAsset& rhs) const
351  {
352  return asset.strName < rhs.asset.strName;
353  }
354 };
355 
357 {
359  std::string address;
363 
364 
365  CAssetCacheReissueAsset(const CReissueAsset& reissue, const std::string& address, const COutPoint& out, const int& blockHeight, const uint256& blockHash)
366  {
367  this->reissue = reissue;
368  this->address = address;
369  this->out = out;
370  this->blockHash = blockHash;
371  this->blockHeight = blockHeight;
372  }
373 
374  bool operator<(const CAssetCacheReissueAsset& rhs) const
375  {
376  return out < rhs.out;
377  }
378 
379 };
380 
382 {
384  std::string address;
386 
387  CAssetCacheNewTransfer(const CAssetTransfer& transfer, const std::string& address, const COutPoint& out)
388  {
389  this->transfer = transfer;
390  this->address = address;
391  this->out = out;
392  }
393 
394  bool operator<(const CAssetCacheNewTransfer& rhs ) const
395  {
396  return out < rhs.out;
397  }
398 };
399 
401 {
402  std::string assetName;
403  std::string address;
404 
405  CAssetCacheNewOwner(const std::string& assetName, const std::string& address)
406  {
407  this->assetName = assetName;
408  this->address = address;
409  }
410 
411  bool operator<(const CAssetCacheNewOwner& rhs) const
412  {
413 
414  return assetName < rhs.assetName;
415  }
416 };
417 
419 {
420  std::string assetName;
421  std::string address;
423 
424  CAssetCacheUndoAssetAmount(const std::string& assetName, const std::string& address, const CAmount& nAmount)
425  {
426  this->assetName = assetName;
427  this->address = address;
428  this->nAmount = nAmount;
429  }
430 };
431 
433 {
434  std::string assetName;
435  std::string address;
437 
438  CAssetCacheSpendAsset(const std::string& assetName, const std::string& address, const CAmount& nAmount)
439  {
440  this->assetName = assetName;
441  this->address = address;
442  this->nAmount = nAmount;
443  }
444 };
445 
447  std::string assetName;
448  std::string address;
450 
451  CAssetCacheQualifierAddress(const std::string &assetName, const std::string &address, const QualifierType &type) {
452  this->assetName = assetName;
453  this->address = address;
454  this->type = type;
455  }
456 
457  bool operator<(const CAssetCacheQualifierAddress &rhs) const {
458  return assetName < rhs.assetName || (assetName == rhs.assetName && address < rhs.address);
459  }
460 
461  uint256 GetHash();
462 };
463 
465  std::string rootAssetName;
466  std::string address;
467 
468  CAssetCacheRootQualifierChecker(const std::string &assetName, const std::string &address) {
469  this->rootAssetName = assetName;
470  this->address = address;
471  }
472 
474  return rootAssetName < rhs.rootAssetName || (rootAssetName == rhs.rootAssetName && address < rhs.address);
475  }
476 
477  uint256 GetHash();
478 };
479 
481 {
482  std::string assetName;
483  std::string address;
485 
486  CAssetCacheRestrictedAddress(const std::string& assetName, const std::string& address, const RestrictedType& type)
487  {
488  this->assetName = assetName;
489  this->address = address;
490  this->type = type;
491  }
492 
494  {
495  return assetName < rhs.assetName || (assetName == rhs.assetName && address < rhs.address);
496  }
497 
498  uint256 GetHash();
499 };
500 
502 {
503  std::string assetName;
505 
506  CAssetCacheRestrictedGlobal(const std::string& assetName, const RestrictedType& type)
507  {
508  this->assetName = assetName;
509  this->type = type;
510  }
511 
512  bool operator<(const CAssetCacheRestrictedGlobal& rhs) const
513  {
514  return assetName < rhs.assetName;
515  }
516 };
517 
519 {
520  std::string assetName;
521  std::string verifier;
523 
524  CAssetCacheRestrictedVerifiers(const std::string& assetName, const std::string& verifier)
525  {
526  this->assetName = assetName;
527  this->verifier = verifier;
528  fUndoingRessiue = false;
529  }
530 
532  {
533  return assetName < rhs.assetName;
534  }
535 };
536 
537 // Least Recently Used Cache
538 template<typename cache_key_t, typename cache_value_t>
540 {
541 public:
542  typedef typename std::pair<cache_key_t, cache_value_t> key_value_pair_t;
543  typedef typename std::list<key_value_pair_t>::iterator list_iterator_t;
544 
545  CLRUCache(size_t max_size) : maxSize(max_size)
546  {
547  }
549  {
550  SetNull();
551  }
552 
553  void Put(const cache_key_t& key, const cache_value_t& value)
554  {
555  auto it = cacheItemsMap.find(key);
556  cacheItemsList.push_front(key_value_pair_t(key, value));
557  if (it != cacheItemsMap.end())
558  {
559  cacheItemsList.erase(it->second);
560  cacheItemsMap.erase(it);
561  }
562  cacheItemsMap[key] = cacheItemsList.begin();
563 
564  if (cacheItemsMap.size() > maxSize)
565  {
566  auto last = cacheItemsList.end();
567  last--;
568  cacheItemsMap.erase(last->first);
569  cacheItemsList.pop_back();
570  }
571  }
572 
573  void Erase(const cache_key_t& key)
574  {
575  auto it = cacheItemsMap.find(key);
576  if (it != cacheItemsMap.end())
577  {
578  cacheItemsList.erase(it->second);
579  cacheItemsMap.erase(it);
580  }
581  }
582 
583  const cache_value_t& Get(const cache_key_t& key)
584  {
585  auto it = cacheItemsMap.find(key);
586  if (it == cacheItemsMap.end())
587  {
588  throw std::range_error("There is no such key in cache");
589  }
590  else
591  {
592  cacheItemsList.splice(cacheItemsList.begin(), cacheItemsList, it->second);
593  return it->second->second;
594  }
595  }
596 
597  bool Exists(const cache_key_t& key) const
598  {
599  return cacheItemsMap.find(key) != cacheItemsMap.end();
600  }
601 
602  size_t Size() const
603  {
604  return cacheItemsMap.size();
605  }
606 
607 
608  void Clear()
609  {
610  cacheItemsMap.clear();
611  cacheItemsList.clear();
612  }
613 
614  void SetNull()
615  {
616  maxSize = 0;
617  Clear();
618  }
619 
620  size_t MaxSize() const
621  {
622  return maxSize;
623  }
624 
625 
626  void SetSize(const size_t size)
627  {
628  maxSize = size;
629  }
630 
631  const std::unordered_map<cache_key_t, list_iterator_t>& GetItemsMap()
632  {
633  return cacheItemsMap;
634  };
635 
636  const std::list<key_value_pair_t>& GetItemsList()
637  {
638  return cacheItemsList;
639  };
640 
641 
642  CLRUCache(const CLRUCache& cache)
643  {
644  this->cacheItemsList = cache.cacheItemsList;
645  this->cacheItemsMap = cache.cacheItemsMap;
646  this->maxSize = cache.maxSize;
647  }
648 
649 private:
650  std::list<key_value_pair_t> cacheItemsList;
651  std::unordered_map<cache_key_t, list_iterator_t> cacheItemsMap;
652  size_t maxSize;
653 };
654 
655 #endif //RAVENCOIN_NEWASSET_H
bool operator<(const CAssetCacheNewAsset &rhs) const
Definition: assettypes.h:350
bool operator<(const CAssetCacheNewOwner &rhs) const
Definition: assettypes.h:411
bool ReadWriteAssetHash(Stream &s, Operation ser_action, std::string &strIPFSHash)
Definition: assettypes.h:59
#define s2
Definition: sph_hamsi.c:214
void Erase(const cache_key_t &key)
Definition: assettypes.h:573
std::string assetName
Definition: assettypes.h:402
CAssetCacheUndoAssetAmount(const std::string &assetName, const std::string &address, const CAmount &nAmount)
Definition: assettypes.h:424
std::list< key_value_pair_t >::iterator list_iterator_t
Definition: assettypes.h:543
std::string strName
Definition: assettypes.h:239
int8_t units
Definition: assettypes.h:102
int8_t nReissuable
Definition: assettypes.h:242
#define READWRITE(obj)
Definition: serialize.h:163
RestrictedType
Definition: assettypes.h:43
const std::unordered_map< cache_key_t, list_iterator_t > & GetItemsMap()
Definition: assettypes.h:631
CAssetCacheQualifierAddress(const std::string &assetName, const std::string &address, const QualifierType &type)
Definition: assettypes.h:451
void Put(const cache_key_t &key, const cache_value_t &value)
Definition: assettypes.h:553
std::unordered_map< cache_key_t, list_iterator_t > cacheItemsMap
Definition: assettypes.h:651
THESE ARE ONLY TO BE USED WHEN ADDING THINGS TO THE CACHE DURING CONNECT AND DISCONNECT BLOCK...
Definition: assettypes.h:335
size_t Size() const
Definition: assettypes.h:602
CReissueAsset reissue
Definition: assettypes.h:358
CAssetCacheNewOwner(const std::string &assetName, const std::string &address)
Definition: assettypes.h:405
void SerializationOp(Stream &s, Operation ser_action)
Definition: assettypes.h:262
CAssetCacheRootQualifierChecker(const std::string &assetName, const std::string &address)
Definition: assettypes.h:468
int8_t nUnits
Definition: assettypes.h:241
void SetNull()
Definition: assettypes.h:250
std::string address
Definition: assettypes.h:338
void SetNull()
Definition: assettypes.h:614
std::string strName
Definition: assettypes.h:100
ADD_SERIALIZE_METHODS
Definition: assettypes.h:134
CAssetCacheReissueAsset(const CReissueAsset &reissue, const std::string &address, const COutPoint &out, const int &blockHeight, const uint256 &blockHash)
Definition: assettypes.h:365
CAssetCacheRestrictedGlobal(const std::string &assetName, const RestrictedType &type)
Definition: assettypes.h:506
bool Exists(const cache_key_t &key) const
Definition: assettypes.h:597
std::string strName
Definition: assettypes.h:190
void SerializationOp(Stream &s, Operation ser_action)
Definition: assettypes.h:137
std::string asset_name
Definition: assettypes.h:278
std::string strIPFSHash
Definition: assettypes.h:105
bool operator<(const CAssetCacheQualifierAddress &rhs) const
Definition: assettypes.h:457
void SetSize(const size_t size)
Definition: assettypes.h:626
int8_t nReissuable
Definition: assettypes.h:103
void Serialize(Stream &s, char a)
Definition: serialize.h:182
std::string assetName
Definition: assettypes.h:434
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
void SerializationOp(Stream &s, Operation ser_action)
Definition: assettypes.h:325
#define MAX_UNIT
Definition: assettypes.h:16
size_t MaxSize() const
Definition: assettypes.h:620
int8_t nHasIPFS
Definition: assettypes.h:104
size_t maxSize
Definition: assettypes.h:652
UniValue transfer(const JSONRPCRequest &request)
Definition: assets.cpp:1101
std::string address
Definition: assettypes.h:403
CAssetCacheRestrictedVerifiers(const std::string &assetName, const std::string &verifier)
Definition: assettypes.h:524
void SerializationOp(Stream &s, Operation ser_action)
Definition: assettypes.h:211
void SetNull()
Definition: assettypes.h:118
std::string address
Definition: assettypes.h:435
int64_t nExpireTime
Definition: assettypes.h:193
const char TXID_NOTIFIER
Definition: assettypes.h:55
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
void SerializationOp(Stream &s, Operation ser_action)
Definition: assettypes.h:295
bool operator<(const CAssetCacheReissueAsset &rhs) const
Definition: assettypes.h:374
std::pair< cache_key_t, cache_value_t > key_value_pair_t
Definition: assettypes.h:542
#define s1(x)
Definition: sha2.c:57
AssetType
Definition: assettypes.h:21
CAmount nAmount
Definition: assettypes.h:240
CLRUCache(size_t max_size)
Definition: assettypes.h:545
256-bit opaque blob.
Definition: uint256.h:123
CAmount nAmount
Definition: assettypes.h:101
const char IPFS_SHA2_256_LEN
Definition: assettypes.h:56
bool operator<(const CAssetCacheRestrictedAddress &rhs) const
Definition: assettypes.h:493
bool operator()(const CNewAsset &s1, const CNewAsset &s2) const
Definition: assettypes.h:153
const char IPFS_SHA2_256
Definition: assettypes.h:54
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:396
bool operator<(const CAssetCacheRestrictedVerifiers &rhs) const
Definition: assettypes.h:531
const std::list< key_value_pair_t > & GetItemsList()
Definition: assettypes.h:636
void SerializationOp(Stream &s, Operation ser_action)
Definition: assettypes.h:179
AssetType AssetTypeFromInt(int nType)
Definition: assettypes.cpp:12
void Clear()
Definition: assettypes.h:608
bool operator<(const CAssetCacheRestrictedGlobal &rhs) const
Definition: assettypes.h:512
CLRUCache(const CLRUCache &cache)
Definition: assettypes.h:642
std::string message
Definition: assettypes.h:192
CAssetCacheRestrictedAddress(const std::string &assetName, const std::string &address, const RestrictedType &type)
Definition: assettypes.h:486
bool operator<(const CAssetCacheNewTransfer &rhs) const
Definition: assettypes.h:394
void Unserialize(Stream &s, char &a)
Definition: serialize.h:194
CAssetCacheSpendAsset(const std::string &assetName, const std::string &address, const CAmount &nAmount)
Definition: assettypes.h:438
void SetNull()
Definition: assettypes.h:200
QualifierType
Definition: assettypes.h:37
CAssetCacheNewTransfer(const CAssetTransfer &transfer, const std::string &address, const COutPoint &out)
Definition: assettypes.h:387
std::list< key_value_pair_t > cacheItemsList
Definition: assettypes.h:650
int IntFromAssetType(AssetType type)
Definition: assettypes.cpp:8
const cache_value_t & Get(const cache_key_t &key)
Definition: assettypes.h:583
std::string strIPFSHash
Definition: assettypes.h:243
CAssetCacheNewAsset(const CNewAsset &asset, const std::string &address, const int &blockHeight, const uint256 &blockHash)
Definition: assettypes.h:342
CAmount nAmount
Definition: assettypes.h:191
bool operator<(const CAssetCacheRootQualifierChecker &rhs) const
Definition: assettypes.h:473
CAssetTransfer transfer
Definition: assettypes.h:383
UniValue reissue(const JSONRPCRequest &request)
Definition: assets.cpp:1465