Raven Core  3.0.0
P2P Digital Currency
messages.h
Go to the documentation of this file.
1 // Copyright (c) 2018 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 
6 #ifndef RAVENCOIN_MESSAGES_H
7 #define RAVENCOIN_MESSAGES_H
8 
9 #include <uint256.h>
10 #include <serialize.h>
11 
12 class CMessage;
13 class COutPoint;
14 
15 // Message Database caches
16 extern std::set<COutPoint> setDirtyMessagesRemove;
17 extern std::map<COutPoint, CMessage> mapDirtyMessagesAdd;
18 extern std::map<COutPoint, CMessage> mapDirtyMessagesOrphaned;
19 
20 // Message Channel Database caches
21 extern std::set<std::string> setDirtyChannelsAdd;
22 extern std::set<std::string> setDirtyChannelsRemove;
23 extern std::set<std::string> setSubscribedChannelsAskedForFalse;
24 
25 // Spam prevention address index
26 extern std::set<std::string> setDirtySeenAddressAdd;
27 extern std::set<std::string> setAddressAskedForFalse;
28 
29 // Lock for messaging
31 
33 bool IsChannelSubscribed(const std::string &name); // Is this channel marked as spamA
34 
35 bool GetMessage(const COutPoint &out, CMessage &message);
36 
37 void AddChannel(const std::string &name);
38 
39 void RemoveChannel(const std::string &name);
40 
41 void AddMessage(const CMessage &message);
42 
43 void RemoveMessage(const CMessage &message);
44 void RemoveMessage(const COutPoint &out);
45 
46 void OrphanMessage(const CMessage &message);
47 void OrphanMessage(const COutPoint &out);
48 
49 bool ScanForMessageChannels(std::string& strError);
50 bool IsAddressSeen(const std::string &address); // Has this address already been sent an asset before
51 void AddAddressSeen(const std::string &address);
52 
53 enum class MessageStatus {
54  READ = 0,
55  UNREAD = 1,
56  EXPIRED = 2,
57  SPAM = 3,
58  HIDDEN = 4,
59  ORPHAN = 5,
60  MSG_ERROR = 6
61 };
62 
63 int8_t IntFromMessageStatus(MessageStatus status);
64 MessageStatus MessageStatusFromInt(int8_t nStatus);
65 
66 std::string MessageStatusToString(MessageStatus status);
67 
68 class CMessage {
69 public:
70 
72  std::string strName;
73  std::string ipfsHash;
74  int64_t time;
75  int64_t nExpiredTime;
78 
79  CMessage();
80 
81  void SetNull() {
82  nExpiredTime = 0;
83  out = COutPoint();
84  strName = "";
85  ipfsHash = "";
86  time = 0;
87  status = MessageStatus::MSG_ERROR;
88  nBlockHeight = 0;
89  }
90 
91  std::string ToString() const {
92  return strprintf("CMessage(%s, Name=%s, Message=%s, Expires=%u, Time=%u, BlockHeight=%u)", out.ToString(), strName,
93  EncodeAssetData(ipfsHash), nExpiredTime, time, nBlockHeight);
94  }
95 
96  CMessage(const COutPoint &out, const std::string &strName, const std::string &ipfsHash, const int64_t &nExpiredTime,
97  const int64_t &time);
98 
99  bool operator<(const CMessage &rhs) const {
100  return out < rhs.out;
101  }
102 
104 
105  template<typename Stream, typename Operation>
106  inline void SerializationOp(Stream &s, Operation ser_action) {
107  READWRITE(out);
108  READWRITE(strName);
109  READWRITE(ipfsHash);
110  READWRITE(time);
111  READWRITE(nExpiredTime);
112  READWRITE(nBlockHeight);
113 
114  if (ser_action.ForRead()) {
115  int8_t nStatus = 6;
116  ::Unserialize(s, nStatus);
117  status = MessageStatusFromInt(nStatus);
118  } else {
119  ::Serialize(s, IntFromMessageStatus(status));
120  }
121  }
122 };
123 
124 class CZMQMessage {
125 public:
127  std::string assetName;
128  std::string ipfsHash;
129  int64_t nExpireTime;
130 
131  CZMQMessage(const CMessage& message) {
132  this->blockHeight = message.nBlockHeight;
133  this->assetName = message.strName;
134  this->ipfsHash = message.ipfsHash;
135  this->nExpireTime = message.nExpiredTime;
136  }
137 
138  std::string createJsonString();
139 };
140 
141 #endif //RAVENCOIN_MESSAGES_H
std::set< COutPoint > setDirtyMessagesRemove
Definition: messages.cpp:15
std::string ToString() const
Definition: messages.h:91
#define READWRITE(obj)
Definition: serialize.h:163
int64_t time
Definition: messages.h:74
void OrphanMessage(const CMessage &message)
Definition: messages.cpp:181
#define strprintf
Definition: tinyformat.h:1054
std::map< COutPoint, CMessage > mapDirtyMessagesOrphaned
Definition: messages.cpp:17
std::map< COutPoint, CMessage > mapDirtyMessagesAdd
Definition: messages.cpp:16
std::set< std::string > setDirtySeenAddressAdd
Definition: messages.cpp:23
void SetNull()
Definition: messages.h:81
int nBlockHeight
Definition: messages.h:77
void AddMessage(const CMessage &message)
Definition: messages.cpp:149
std::string ToString() const
Definition: transaction.cpp:14
std::set< std::string > setSubscribedChannelsAskedForFalse
Definition: messages.cpp:21
MessageStatus
Definition: messages.h:53
void SerializationOp(Stream &s, Operation ser_action)
Definition: messages.h:106
MessageStatus status
Definition: messages.h:76
std::set< std::string > setAddressAskedForFalse
Definition: messages.cpp:24
void Serialize(Stream &s, char a)
Definition: serialize.h:182
void RemoveMessage(const CMessage &message)
Definition: messages.cpp:159
bool IsAddressSeen(const std::string &address)
Definition: messages.cpp:267
COutPoint out
Definition: messages.h:71
std::set< std::string > setDirtyChannelsAdd
Definition: messages.cpp:19
std::string ipfsHash
Definition: messages.h:128
void AddAddressSeen(const std::string &address)
Definition: messages.cpp:293
bool ScanForMessageChannels(std::string &strError)
Definition: messages.cpp:189
int blockHeight
Definition: messages.h:126
std::string ipfsHash
Definition: messages.h:73
std::string strName
Definition: messages.h:72
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
int64_t nExpiredTime
Definition: messages.h:75
MessageStatus MessageStatusFromInt(int8_t nStatus)
Definition: messages.cpp:34
CZMQMessage(const CMessage &message)
Definition: messages.h:131
bool operator<(const CMessage &rhs) const
Definition: messages.h:99
void RemoveChannel(const std::string &name)
Definition: messages.cpp:140
int64_t nExpireTime
Definition: messages.h:129
ADD_SERIALIZE_METHODS
Definition: messages.h:103
CCriticalSection cs_messaging
Definition: messages.cpp:26
bool GetMessage(const COutPoint &out, CMessage &message)
Definition: messages.cpp:100
void Unserialize(Stream &s, char &a)
Definition: serialize.h:194
bool IsChannelSubscribed(const std::string &name)
Definition: messages.cpp:67
size_t GetMessageDirtyCacheSize()
Definition: messages.cpp:299
std::string EncodeAssetData(std::string decoded)
Definition: assets.cpp:3716
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...
Definition: sync.h:92
void AddChannel(const std::string &name)
Definition: messages.cpp:130
std::string assetName
Definition: messages.h:127
std::set< std::string > setDirtyChannelsRemove
Definition: messages.cpp:20
int8_t IntFromMessageStatus(MessageStatus status)
Definition: messages.cpp:29
std::string MessageStatusToString(MessageStatus status)
Definition: messages.cpp:39