forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWhisperHost.cpp
289 lines (251 loc) · 6.79 KB
/
WhisperHost.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file WhisperHost.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#include "WhisperHost.h"
#include <libdevcore/CommonIO.h>
#include <libdevcore/Log.h>
#include <libp2p/All.h>
#include "WhisperDB.h"
using namespace std;
using namespace dev;
using namespace dev::p2p;
using namespace dev::shh;
WhisperHost::WhisperHost(bool _storeMessagesInDB): Worker("shh"), m_storeMessagesInDB(_storeMessagesInDB)
{
loadMessagesFromBD();
}
WhisperHost::~WhisperHost()
{
saveMessagesToBD();
}
void WhisperHost::streamMessage(h256 _m, RLPStream& _s) const
{
UpgradableGuard l(x_messages);
if (m_messages.count(_m))
{
UpgradeGuard ll(l);
auto const& m = m_messages.at(_m);
// cnote << "streamRLP: " << m.expiry() << m.ttl() << m.topic() << toHex(m.data());
m.streamRLP(_s);
}
}
void WhisperHost::inject(Envelope const& _m, WhisperPeer* _p)
{
// this function processes both outgoing messages originated both by local host (_p == null)
// and incoming messages from remote peers (_p != null)
//cnote << this << ": inject: " << _m.expiry() << _m.ttl() << _m.topic() << toHex(_m.data());
if (_m.isExpired())
return;
auto h = _m.sha3();
{
UpgradableGuard l(x_messages);
if (m_messages.count(h))
return;
UpgradeGuard ll(l);
m_messages[h] = _m;
m_expiryQueue.insert(make_pair(_m.expiry(), h));
}
// rating of incoming message from remote host is assessed according to the following criteria:
// 1. installed watch match; 2. bloom filter match; 2. ttl; 3. proof of work
int rating = 0;
DEV_GUARDED(m_filterLock)
if (_m.matchesBloomFilter(m_bloom))
{
++rating;
for (auto const& f: m_filters)
if (f.second.filter.matches(_m))
for (auto& i: m_watches)
if (i.second.id == f.first) // match one of the watches
{
i.second.changes.push_back(h);
rating += 2;
}
}
if (_p) // incoming message from remote peer
{
rating *= 256;
unsigned ttlReward = (256 > _m.ttl() ? 256 - _m.ttl() : 0);
rating += ttlReward;
rating *= 256;
rating += _m.workProved();
}
// TODO p2p: capability-based rating
for (auto i: peerSessions())
{
auto w = capabilityFromSession<WhisperPeer>(*i.first).get();
if (w == _p)
w->addRating(rating);
else
w->noteNewMessage(h, _m);
}
}
unsigned WhisperHost::installWatch(shh::Topics const& _t)
{
InstalledFilter f(_t);
h256 h = f.filter.sha3();
unsigned ret = 0;
DEV_GUARDED(m_filterLock)
{
auto it = m_filters.find(h);
if (m_filters.end() == it)
m_filters.insert(make_pair(h, f));
else
it->second.refCount++;
m_bloom.addRaw(f.filter.exportBloom());
ret = m_watches.size() ? m_watches.rbegin()->first + 1 : 0;
m_watches[ret] = ClientWatch(h);
cwatshh << "+++" << ret << h;
}
noteAdvertiseTopicsOfInterest();
return ret;
}
void WhisperHost::uninstallWatch(unsigned _i)
{
cwatshh << "XXX" << _i;
DEV_GUARDED(m_filterLock)
{
auto it = m_watches.find(_i);
if (it == m_watches.end())
return;
auto id = it->second.id;
m_watches.erase(it);
auto fit = m_filters.find(id);
if (fit != m_filters.end())
{
m_bloom.removeRaw(fit->second.filter.exportBloom());
if (!--fit->second.refCount)
m_filters.erase(fit);
}
}
noteAdvertiseTopicsOfInterest();
}
h256s WhisperHost::watchMessages(unsigned _watchId)
{
h256s ret;
auto wit = m_watches.find(_watchId);
if (wit == m_watches.end())
return ret;
TopicFilter f;
{
Guard l(m_filterLock);
auto fit = m_filters.find(wit->second.id);
if (fit == m_filters.end())
return ret;
f = fit->second.filter;
}
ReadGuard l(x_messages);
for (auto const& m: m_messages)
if (f.matches(m.second))
ret.push_back(m.first);
return ret;
}
h256s WhisperHost::checkWatch(unsigned _watchId)
{
h256s ret;
cleanup();
dev::Guard l(m_filterLock);
try
{
ret = m_watches.at(_watchId).changes;
m_watches.at(_watchId).changes.clear();
}
catch (...)
{
}
return ret;
}
void WhisperHost::doWork()
{
for (auto i: peerSessions())
capabilityFromSession<WhisperPeer>(*i.first)->sendMessages();
cleanup();
}
void WhisperHost::cleanup()
{
// remove old messages.
// should be called every now and again.
uint64_t now = utcTime();
WriteGuard l(x_messages);
for (auto it = m_expiryQueue.begin(); it != m_expiryQueue.end() && it->first <= now; it = m_expiryQueue.erase(it))
m_messages.erase(it->second);
}
void WhisperHost::noteAdvertiseTopicsOfInterest()
{
for (auto i: peerSessions())
capabilityFromSession<WhisperPeer>(*i.first)->noteAdvertiseTopicsOfInterest();
}
bool WhisperHost::isWatched(Envelope const& _e) const
{
DEV_GUARDED(m_filterLock)
if (_e.matchesBloomFilter(m_bloom))
for (auto const& f: m_filters)
if (f.second.filter.matches(_e))
for (auto const& i: m_watches)
if (i.second.id == f.first)
return true;
return false;
}
void WhisperHost::saveMessagesToBD()
{
if (!m_storeMessagesInDB)
return;
try
{
WhisperMessagesDB db;
ReadGuard g(x_messages);
uint64_t now = utcTime();
for (auto const& m: m_messages)
if (m.second.expiry() > now)
if (isWatched(m.second))
db.saveSingleMessage(m.first, m.second);
}
catch(FailedToOpenLevelDB const& ex)
{
LOG(WARNING) << "Exception in WhisperHost::saveMessagesToBD() - failed to open DB:" << ex.what();
}
catch(Exception const& ex)
{
LOG(WARNING) << "Exception in WhisperHost::saveMessagesToBD():" << ex.what();
}
catch(...)
{
LOG(WARNING) << "Unknown Exception in WhisperHost::saveMessagesToBD()";
}
}
void WhisperHost::loadMessagesFromBD()
{
if (!m_storeMessagesInDB)
return;
try
{
map<h256, Envelope> m;
WhisperMessagesDB db;
db.loadAllMessages(m);
WriteGuard g(x_messages);
m_messages.swap(m);
for (auto const& msg: m)
m_expiryQueue.insert(make_pair(msg.second.expiry(), msg.first));
}
catch(Exception const& ex)
{
LOG(WARNING) << "Exception in WhisperHost::loadMessagesFromBD():" << ex.what();
}
catch(...)
{
LOG(WARNING) << "Unknown Exception in WhisperHost::loadMessagesFromBD()";
}
}