From e0c3095bdaaf486fc8d59bafdb2ce3df66a00e97 Mon Sep 17 00:00:00 2001 From: Vlad Gluhovsky Date: Wed, 3 Jun 2015 19:22:34 +0200 Subject: [PATCH 1/2] refactored the topic naming conventions --- alethzero/MainWin.cpp | 6 ++--- libweb3jsonrpc/WebThreeStubServerBase.cpp | 12 +++++----- libwhisper/Common.cpp | 24 +++++++++---------- libwhisper/Common.h | 28 +++++++++++------------ libwhisper/Interface.h | 24 ++++++++----------- libwhisper/Message.cpp | 20 ++++++++-------- libwhisper/Message.h | 20 ++++++++-------- libwhisper/WhisperHost.cpp | 4 ++-- libwhisper/WhisperHost.h | 6 ++--- test/libwhisper/whisperMessage.cpp | 8 +++---- 10 files changed, 73 insertions(+), 79 deletions(-) diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index 6a11736a4..59698bac3 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -1803,7 +1803,7 @@ void Main::on_accounts_doubleClicked() } } -static shh::FullTopic topicFromText(QString _s) +static shh::Topics topicFromText(QString _s) { shh::BuildTopic ret; while (_s.size()) @@ -2187,10 +2187,10 @@ void Main::refreshWhispers() shh::Envelope const& e = w.second; shh::Message m; for (pair const& i: m_server->ids()) - if (!!(m = e.open(shh::FullTopic(), i.second))) + if (!!(m = e.open(shh::Topics(), i.second))) break; if (!m) - m = e.open(shh::FullTopic()); + m = e.open(shh::Topics()); QString msg; if (m.from()) diff --git a/libweb3jsonrpc/WebThreeStubServerBase.cpp b/libweb3jsonrpc/WebThreeStubServerBase.cpp index 6b714f2ac..ff7b84dc4 100644 --- a/libweb3jsonrpc/WebThreeStubServerBase.cpp +++ b/libweb3jsonrpc/WebThreeStubServerBase.cpp @@ -267,7 +267,7 @@ static shh::Envelope toSealed(Json::Value const& _json, shh::Message const& _m, return _m.seal(_from, bt, ttl, workToProve); } -static pair toWatch(Json::Value const& _json) +static pair toWatch(Json::Value const& _json) { shh::BuildTopic bt; Public to; @@ -985,7 +985,7 @@ string WebThreeStubServerBase::shh_newFilter(Json::Value const& _json) try { - pair w = toWatch(_json); + pair w = toWatch(_json); auto ret = face()->installWatch(w.first); m_shhWatches.insert(make_pair(ret, w.second)); return toJS(ret); @@ -1025,10 +1025,10 @@ Json::Value WebThreeStubServerBase::shh_getFilterChanges(string const& _filterId if (pub) { cwarn << "Silently decrypting message from identity" << pub << ": User validation hook goes here."; - m = e.open(face()->fullTopic(id), m_shhIds[pub]); + m = e.open(face()->fullTopics(id), m_shhIds[pub]); } else - m = e.open(face()->fullTopic(id)); + m = e.open(face()->fullTopics(id)); if (!m) continue; ret.append(toJson(h, e, m)); @@ -1058,10 +1058,10 @@ Json::Value WebThreeStubServerBase::shh_getMessages(string const& _filterId) if (pub) { cwarn << "Silently decrypting message from identity" << pub << ": User validation hook goes here."; - m = e.open(face()->fullTopic(id), m_shhIds[pub]); + m = e.open(face()->fullTopics(id), m_shhIds[pub]); } else - m = e.open(face()->fullTopic(id)); + m = e.open(face()->fullTopics(id)); if (!m) continue; ret.append(toJson(h, e, m)); diff --git a/libwhisper/Common.cpp b/libwhisper/Common.cpp index dd9172c70..526072842 100644 --- a/libwhisper/Common.cpp +++ b/libwhisper/Common.cpp @@ -28,26 +28,26 @@ using namespace dev; using namespace dev::p2p; using namespace dev::shh; -CollapsedTopicPart dev::shh::collapse(FullTopicPart const& _p) +AbridgedTopic dev::shh::abridge(Topic const& _p) { - return CollapsedTopicPart(sha3(_p)); + return AbridgedTopic(sha3(_p)); } -CollapsedTopic dev::shh::collapse(FullTopic const& _fullTopic) +AbridgedTopics dev::shh::abridge(Topics const& _topics) { - CollapsedTopic ret; - ret.reserve(_fullTopic.size()); - for (auto const& ft: _fullTopic) - ret.push_back(collapse(ft)); + AbridgedTopics ret; + ret.reserve(_topics.size()); + for (auto const& t : _topics) + ret.push_back(abridge(t)); return ret; } -CollapsedTopic BuildTopic::toTopic() const +AbridgedTopics BuildTopic::toAbridgedTopics() const { - CollapsedTopic ret; + AbridgedTopics ret; ret.reserve(m_parts.size()); for (auto const& h: m_parts) - ret.push_back(collapse(h)); + ret.push_back(abridge(h)); return ret; } @@ -71,7 +71,7 @@ bool TopicFilter::matches(Envelope const& _e) const for (unsigned i = 0; i < t.size(); ++i) { for (auto et: _e.topic()) - if (((t[i].first ^ et) & t[i].second) == CollapsedTopicPart()) + if (((t[i].first ^ et) & t[i].second) == AbridgedTopic()) goto NEXT_TOPICPART; // failed to match topicmask against any topics: move on to next mask goto NEXT_TOPICMASK; @@ -89,7 +89,7 @@ TopicMask BuildTopicMask::toTopicMask() const TopicMask ret; ret.reserve(m_parts.size()); for (auto const& h: m_parts) - ret.push_back(make_pair(collapse(h), ~CollapsedTopicPart())); + ret.push_back(make_pair(abridge(h), ~AbridgedTopic())); return ret; } diff --git a/libwhisper/Common.h b/libwhisper/Common.h index 480b79350..b575166b4 100644 --- a/libwhisper/Common.h +++ b/libwhisper/Common.h @@ -60,14 +60,14 @@ enum WhisperPacket PacketCount }; -using CollapsedTopicPart = FixedHash<4>; -using FullTopicPart = h256; +using AbridgedTopic = FixedHash<4>; +using Topic = h256; -using CollapsedTopic = std::vector; -using FullTopic = h256s; +using AbridgedTopics = std::vector; +using Topics = h256s; -CollapsedTopicPart collapse(FullTopicPart const& _fullTopicPart); -CollapsedTopic collapse(FullTopic const& _fullTopic); +AbridgedTopic abridge(Topic const& _topic); +AbridgedTopics abridge(Topics const& _topics); class BuildTopic { @@ -80,10 +80,10 @@ public: BuildTopic& shiftRaw(h256 const& _part) { m_parts.push_back(_part); return *this; } - operator CollapsedTopic() const { return toTopic(); } - operator FullTopic() const { return toFullTopic(); } - CollapsedTopic toTopic() const; - FullTopic toFullTopic() const { return m_parts; } + operator AbridgedTopics() const { return toAbridgedTopics(); } + operator Topics() const { return toTopics(); } + AbridgedTopics toAbridgedTopics() const; + Topics toTopics() const { return m_parts; } protected: BuildTopic& shiftBytes(bytes const& _b); @@ -91,14 +91,14 @@ protected: h256s m_parts; }; -using TopicMask = std::vector>; +using TopicMask = std::vector>; using TopicMasks = std::vector; class TopicFilter { public: TopicFilter() {} - TopicFilter(FullTopic const& _m) { m_topicMasks.push_back(TopicMask()); for (auto const& h: _m) m_topicMasks.back().push_back(std::make_pair(collapse(h), h ? ~CollapsedTopicPart() : CollapsedTopicPart())); } + TopicFilter(Topics const& _m) { m_topicMasks.push_back(TopicMask()); for (auto const& h: _m) m_topicMasks.back().push_back(std::make_pair(abridge(h), h ? ~AbridgedTopic() : AbridgedTopic())); } TopicFilter(TopicMask const& _m): m_topicMasks(1, _m) {} TopicFilter(TopicMasks const& _m): m_topicMasks(_m) {} TopicFilter(RLP const& _r)//: m_topicMasks(_r.toVector>()) @@ -132,9 +132,9 @@ public: template BuildTopicMask& operator()(T const& _t) { shift(_t); return *this; } operator TopicMask() const { return toTopicMask(); } - operator FullTopic() const { return toFullTopic(); } + operator Topics() const { return toTopics(); } TopicMask toTopicMask() const; - FullTopic toFullTopic() const { return m_parts; } + Topics toTopics() const { return m_parts; } }; } diff --git a/libwhisper/Interface.h b/libwhisper/Interface.h index 8b84193aa..8015957fc 100644 --- a/libwhisper/Interface.h +++ b/libwhisper/Interface.h @@ -38,19 +38,13 @@ namespace dev namespace shh { -/*struct TopicMask -{ - Topic data; - Topic mask; -};*/ - class Watch; struct InstalledFilter { - InstalledFilter(FullTopic const& _f): full(_f), filter(_f) {} + InstalledFilter(Topics const& _t): full(_t), filter(_t) {} - FullTopic full; + Topics full; TopicFilter filter; unsigned refCount = 1; }; @@ -71,8 +65,8 @@ public: virtual void inject(Envelope const& _m, WhisperPeer* _from = nullptr) = 0; - virtual FullTopic const& fullTopic(unsigned _id) const = 0; - virtual unsigned installWatch(FullTopic const& _mask) = 0; + virtual Topics const& fullTopics(unsigned _id) const = 0; + virtual unsigned installWatch(Topics const& _filter) = 0; virtual unsigned installWatchOnId(h256 _filterId) = 0; virtual void uninstallWatch(unsigned _watchId) = 0; virtual h256s peekWatch(unsigned _watchId) const = 0; @@ -81,10 +75,10 @@ public: virtual Envelope envelope(h256 _m) const = 0; - void post(bytes const& _payload, FullTopic _topic, unsigned _ttl = 50, unsigned _workToProve = 50) { inject(Message(_payload).seal(_topic, _ttl, _workToProve)); } - void post(Public _to, bytes const& _payload, FullTopic _topic, unsigned _ttl = 50, unsigned _workToProve = 50) { inject(Message(_payload).sealTo(_to, _topic, _ttl, _workToProve)); } - void post(Secret _from, bytes const& _payload, FullTopic _topic, unsigned _ttl = 50, unsigned _workToProve = 50) { inject(Message(_payload).seal(_from, _topic, _ttl, _workToProve)); } - void post(Secret _from, Public _to, bytes const& _payload, FullTopic _topic, unsigned _ttl = 50, unsigned _workToProve = 50) { inject(Message(_payload).sealTo(_from, _to, _topic, _ttl, _workToProve)); } + void post(bytes const& _payload, Topics _topics, unsigned _ttl = 50, unsigned _workToProve = 50) { inject(Message(_payload).seal(_topics, _ttl, _workToProve)); } + void post(Public _to, bytes const& _payload, Topics _topics, unsigned _ttl = 50, unsigned _workToProve = 50) { inject(Message(_payload).sealTo(_to, _topics, _ttl, _workToProve)); } + void post(Secret _from, bytes const& _payload, Topics _topics, unsigned _ttl = 50, unsigned _workToProve = 50) { inject(Message(_payload).seal(_from, _topics, _ttl, _workToProve)); } + void post(Secret _from, Public _to, bytes const& _payload, Topics _topics, unsigned _ttl = 50, unsigned _workToProve = 50) { inject(Message(_payload).sealTo(_from, _to, _topics, _ttl, _workToProve)); } }; struct WatshhChannel: public dev::LogChannel { static const char* name() { return "shh"; } static const int verbosity = 1; }; @@ -106,7 +100,7 @@ class Watch: public boost::noncopyable public: Watch() {} - Watch(Interface& _c, FullTopic const& _f): m_c(&_c), m_id(_c.installWatch(_f)) {} + Watch(Interface& _c, Topics const& _t) : m_c(&_c), m_id(_c.installWatch(_t)) {} ~Watch() { if (m_c) m_c->uninstallWatch(m_id); } h256s check() { return m_c ? m_c->checkWatch(m_id) : h256s(); } diff --git a/libwhisper/Message.cpp b/libwhisper/Message.cpp index 9ba67ff9c..50020b783 100644 --- a/libwhisper/Message.cpp +++ b/libwhisper/Message.cpp @@ -26,7 +26,7 @@ using namespace dev; using namespace dev::p2p; using namespace dev::shh; -Message::Message(Envelope const& _e, FullTopic const& _fk, Secret const& _s) +Message::Message(Envelope const& _e, Topics const& _t, Secret const& _s) { try { @@ -35,7 +35,7 @@ Message::Message(Envelope const& _e, FullTopic const& _fk, Secret const& _s) if (!decrypt(_s, &(_e.data()), b)) return; else{} - else if (!openBroadcastEnvelope(_e, _fk, b)) + else if (!openBroadcastEnvelope(_e, _t, b)) return; if (populate(b)) @@ -47,14 +47,14 @@ Message::Message(Envelope const& _e, FullTopic const& _fk, Secret const& _s) } } -bool Message::openBroadcastEnvelope(Envelope const& _e, FullTopic const& _fk, bytes& o_b) +bool Message::openBroadcastEnvelope(Envelope const& _e, Topics const& _fk, bytes& o_b) { // retrieve the key using the known topic and topicIndex. unsigned topicIndex = 0; Secret topicSecret; // determine topicSecret/topicIndex from knowledge of the collapsed topics (which give the order) and our full-size filter topic. - CollapsedTopic knownTopic = collapse(_fk); + AbridgedTopics knownTopic = abridge(_fk); for (unsigned ti = 0; ti < _fk.size() && !topicSecret; ++ti) for (unsigned i = 0; i < _e.topic().size(); ++i) if (_e.topic()[i] == knownTopic[ti]) @@ -96,10 +96,10 @@ bool Message::populate(bytes const& _data) return true; } -Envelope Message::seal(Secret _from, FullTopic const& _fullTopic, unsigned _ttl, unsigned _workToProve) const +Envelope Message::seal(Secret _from, Topics const& _fullTopics, unsigned _ttl, unsigned _workToProve) const { - CollapsedTopic topic = collapse(_fullTopic); - Envelope ret(time(0) + _ttl, _ttl, topic); + AbridgedTopics topics = abridge(_fullTopics); + Envelope ret(time(0) + _ttl, _ttl, topics); bytes input(1 + m_payload.size()); input[0] = 0; @@ -121,7 +121,7 @@ Envelope Message::seal(Secret _from, FullTopic const& _fullTopic, unsigned _ttl, // this message is for broadcast (could be read by anyone who knows at least one of the topics) // create the shared secret for encrypting the payload, then encrypt the shared secret with each topic Secret s = Secret::random(); - for (h256 const& t : _fullTopic) + for (h256 const& t : _fullTopics) { h256 salt = h256::random(); ret.m_data += (generateGamma(t, salt) ^ s).asBytes(); @@ -146,9 +146,9 @@ Envelope::Envelope(RLP const& _m) m_nonce = _m[4].toInt(); } -Message Envelope::open(FullTopic const& _ft, Secret const& _s) const +Message Envelope::open(Topics const& _t, Secret const& _s) const { - return Message(*this, _ft, _s); + return Message(*this, _t, _s); } unsigned Envelope::workProved() const diff --git a/libwhisper/Message.h b/libwhisper/Message.h index 5b069f57b..3529054e0 100644 --- a/libwhisper/Message.h +++ b/libwhisper/Message.h @@ -72,22 +72,22 @@ public: unsigned sent() const { return m_expiry - m_ttl; } unsigned expiry() const { return m_expiry; } unsigned ttl() const { return m_ttl; } - CollapsedTopic const& topic() const { return m_topic; } + AbridgedTopics const& topic() const { return m_topic; } bytes const& data() const { return m_data; } - Message open(FullTopic const& _ft, Secret const& _s = Secret()) const; + Message open(Topics const& _t, Secret const& _s = Secret()) const; unsigned workProved() const; void proveWork(unsigned _ms); private: - Envelope(unsigned _exp, unsigned _ttl, CollapsedTopic const& _topic): m_expiry(_exp), m_ttl(_ttl), m_topic(_topic) {} + Envelope(unsigned _exp, unsigned _ttl, AbridgedTopics const& _topic): m_expiry(_exp), m_ttl(_ttl), m_topic(_topic) {} unsigned m_expiry = 0; unsigned m_ttl = 0; u256 m_nonce; - CollapsedTopic m_topic; + AbridgedTopics m_topic; bytes m_data; }; @@ -102,7 +102,7 @@ class Message { public: Message() {} - Message(Envelope const& _e, FullTopic const& _ft, Secret const& _s = Secret()); + Message(Envelope const& _e, Topics const& _t, Secret const& _s = Secret()); Message(bytes const& _payload): m_payload(_payload) {} Message(bytesConstRef _payload): m_payload(_payload.toBytes()) {} Message(bytes&& _payload) { std::swap(_payload, m_payload); } @@ -119,15 +119,15 @@ public: operator bool() const { return !!m_payload.size() || m_from || m_to; } /// Turn this message into a ditributable Envelope. - Envelope seal(Secret _from, FullTopic const& _topic, unsigned _ttl = 50, unsigned _workToProve = 50) const; + Envelope seal(Secret _from, Topics const& _topics, unsigned _ttl = 50, unsigned _workToProve = 50) const; // Overloads for skipping _from or specifying _to. - Envelope seal(FullTopic const& _topic, unsigned _ttl = 50, unsigned _workToProve = 50) const { return seal(Secret(), _topic, _ttl, _workToProve); } - Envelope sealTo(Public _to, FullTopic const& _topic, unsigned _ttl = 50, unsigned _workToProve = 50) { m_to = _to; return seal(Secret(), _topic, _ttl, _workToProve); } - Envelope sealTo(Secret _from, Public _to, FullTopic const& _topic, unsigned _ttl = 50, unsigned _workToProve = 50) { m_to = _to; return seal(_from, _topic, _ttl, _workToProve); } + Envelope seal(Topics const& _topics, unsigned _ttl = 50, unsigned _workToProve = 50) const { return seal(Secret(), _topics, _ttl, _workToProve); } + Envelope sealTo(Public _to, Topics const& _topics, unsigned _ttl = 50, unsigned _workToProve = 50) { m_to = _to; return seal(Secret(), _topics, _ttl, _workToProve); } + Envelope sealTo(Secret _from, Public _to, Topics const& _topics, unsigned _ttl = 50, unsigned _workToProve = 50) { m_to = _to; return seal(_from, _topics, _ttl, _workToProve); } private: bool populate(bytes const& _data); - bool openBroadcastEnvelope(Envelope const& _e, FullTopic const& _fk, bytes& o_b); + bool openBroadcastEnvelope(Envelope const& _e, Topics const& _t, bytes& o_b); h256 generateGamma(h256 const& _key, h256 const& _salt) const { return sha3(_key ^ _salt); } Public m_from; diff --git a/libwhisper/WhisperHost.cpp b/libwhisper/WhisperHost.cpp index ab3576292..366bb92e4 100644 --- a/libwhisper/WhisperHost.cpp +++ b/libwhisper/WhisperHost.cpp @@ -103,11 +103,11 @@ unsigned WhisperHost::installWatchOnId(h256 _h) return ret; } -unsigned WhisperHost::installWatch(shh::FullTopic const& _ft) +unsigned WhisperHost::installWatch(shh::Topics const& _t) { Guard l(m_filterLock); - InstalledFilter f(_ft); + InstalledFilter f(_t); h256 h = f.filter.sha3(); if (!m_filters.count(h)) diff --git a/libwhisper/WhisperHost.h b/libwhisper/WhisperHost.h index cd427da35..c5ec5867b 100644 --- a/libwhisper/WhisperHost.h +++ b/libwhisper/WhisperHost.h @@ -40,7 +40,7 @@ namespace dev namespace shh { -static const FullTopic EmptyFullTopic; +static const Topics EmptyTopics; class WhisperHost: public HostCapability, public Interface, public Worker { @@ -54,8 +54,8 @@ public: virtual void inject(Envelope const& _e, WhisperPeer* _from = nullptr) override; - virtual FullTopic const& fullTopic(unsigned _id) const { try { return m_filters.at(m_watches.at(_id).id).full; } catch (...) { return EmptyFullTopic; } } - virtual unsigned installWatch(FullTopic const& _filter) override; + virtual Topics const& fullTopics(unsigned _id) const { try { return m_filters.at(m_watches.at(_id).id).full; } catch (...) { return EmptyTopics; } } + virtual unsigned installWatch(Topics const& _filter) override; virtual unsigned installWatchOnId(h256 _filterId) override; virtual void uninstallWatch(unsigned _watchId) override; virtual h256s peekWatch(unsigned _watchId) const override { dev::Guard l(m_filterLock); try { return m_watches.at(_watchId).changes; } catch (...) { return h256s(); } } diff --git a/test/libwhisper/whisperMessage.cpp b/test/libwhisper/whisperMessage.cpp index 5e4dff725..b3cda581b 100644 --- a/test/libwhisper/whisperMessage.cpp +++ b/test/libwhisper/whisperMessage.cpp @@ -34,9 +34,9 @@ struct VerbosityHolder int oldLogVerbosity; }; -FullTopic createRandomTopics(unsigned int i) +Topics createRandomTopics(unsigned int i) { - FullTopic ret; + Topics ret; h256 t(i); for (int j = 0; j < 8; ++j) @@ -72,14 +72,14 @@ void comparePayloads(Message const& m1, Message const& m2) void sealAndOpenSingleMessage(unsigned int i) { Secret zero; - FullTopic topics = createRandomTopics(i); + Topics topics = createRandomTopics(i); bytes const payload = createRandomPayload(i); Message m1(payload); Envelope e = m1.seal(zero, topics, 1, 1); for (auto const& t: topics) { - FullTopic singleTopic; + Topics singleTopic; singleTopic.push_back(t); Message m2(e, singleTopic, zero); comparePayloads(m1, m2); From f0ec754e46cbc9232a57853593ed40ddb782fac2 Mon Sep 17 00:00:00 2001 From: gluk256 Date: Wed, 3 Jun 2015 22:23:17 +0200 Subject: [PATCH 2/2] Update Interface.h --- libwhisper/Interface.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libwhisper/Interface.h b/libwhisper/Interface.h index 8015957fc..ff16c7e53 100644 --- a/libwhisper/Interface.h +++ b/libwhisper/Interface.h @@ -100,7 +100,7 @@ class Watch: public boost::noncopyable public: Watch() {} - Watch(Interface& _c, Topics const& _t) : m_c(&_c), m_id(_c.installWatch(_t)) {} + Watch(Interface& _c, Topics const& _t): m_c(&_c), m_id(_c.installWatch(_t)) {} ~Watch() { if (m_c) m_c->uninstallWatch(m_id); } h256s check() { return m_c ? m_c->checkWatch(m_id) : h256s(); }