From 68d6aa89062594312336e2f58b14fd3fde94284d Mon Sep 17 00:00:00 2001 From: Vlad Gluhovsky Date: Fri, 24 Jul 2015 14:50:27 +0200 Subject: [PATCH] moved some fucntions to WhsiperDB class --- libwhisper/WhisperDB.cpp | 54 +++++++++++++++++++++-------------- libwhisper/WhisperDB.h | 3 +- libwhisper/WhisperHost.cpp | 24 ++++------------ libwhisper/WhisperHost.h | 7 +++-- test/libwhisper/whisperDB.cpp | 14 +++++---- 5 files changed, 53 insertions(+), 49 deletions(-) diff --git a/libwhisper/WhisperDB.cpp b/libwhisper/WhisperDB.cpp index 118c1cb12..4e8f7cf92 100644 --- a/libwhisper/WhisperDB.cpp +++ b/libwhisper/WhisperDB.cpp @@ -158,36 +158,48 @@ void WhisperMessagesDB::saveSingleMessage(h256 const& _key, Envelope const& _e) } } -vector WhisperFiltersDB::restoreTopicsFromDB(WhisperHost* _host, string const& _app) +vector WhisperFiltersDB::restoreTopicsFromDB(WhisperHost* _host, string const& _password) { vector ret; - h256 s = sha3(_app); + h256 s = sha3(_password); h256 h = sha3(s); string raw = lookup(h); - - bytes plain; - - decryptSym(s, raw, plain); - - RLP rlp(plain); - auto sz = rlp.itemCountStrict(); - - for (unsigned i = 0; i < sz; ++i) + if (!raw.empty()) { - RLP r = rlp[i]; - bytesConstRef ref(r.toBytesConstRef()); - Topics topics; - unsigned num = ref.size() / h256::size; - for (unsigned j = 0; j < num; ++j) + bytes plain; + decryptSym(s, raw, plain); + RLP rlp(plain); + auto sz = rlp.itemCountStrict(); + + for (unsigned i = 0; i < sz; ++i) { - h256 topic(ref.data() + j * h256::size, h256::ConstructFromPointerType()); - topics.push_back(topic); - } + RLP r = rlp[i]; + bytesConstRef ref(r.toBytesConstRef()); + Topics topics; + unsigned num = ref.size() / h256::size; + for (unsigned j = 0; j < num; ++j) + { + h256 topic(ref.data() + j * h256::size, h256::ConstructFromPointerType()); + topics.push_back(topic); + } - unsigned w = _host->installWatch(topics); - ret.push_back(w); + unsigned w = _host->installWatch(topics); + ret.push_back(w); + } } return ret; } +void WhisperFiltersDB::saveTopicsToDB(WhisperHost const& _host, std::string const& _password) +{ + bytes plain; + RLPStream rlp; + _host.exportFilters(rlp); + rlp.swapOut(plain); + h256 s = sha3(_password); + h256 h = sha3(s); + bytes encrypted; + encryptSym(s, &plain, encrypted); + insert(h, encrypted); +} diff --git a/libwhisper/WhisperDB.h b/libwhisper/WhisperDB.h index 1ea29d5a3..6ca79666f 100644 --- a/libwhisper/WhisperDB.h +++ b/libwhisper/WhisperDB.h @@ -69,7 +69,8 @@ class WhisperFiltersDB: public WhisperDB public: WhisperFiltersDB(): WhisperDB("filters") {} virtual ~WhisperFiltersDB() {} - std::vector restoreTopicsFromDB(WhisperHost* _host, std::string const& _app); + std::vector restoreTopicsFromDB(WhisperHost* _host, std::string const& _password); + void saveTopicsToDB(WhisperHost const& _host, std::string const& _password); }; } diff --git a/libwhisper/WhisperHost.cpp b/libwhisper/WhisperHost.cpp index 71fafe365..1892c3acd 100644 --- a/libwhisper/WhisperHost.cpp +++ b/libwhisper/WhisperHost.cpp @@ -30,7 +30,7 @@ using namespace dev; using namespace dev::p2p; using namespace dev::shh; -WhisperHost::WhisperHost(bool _useDB): Worker("shh"), m_useDB(_useDB) +WhisperHost::WhisperHost(bool _storeMessagesInDB): Worker("shh"), m_storeMessagesInDB(_storeMessagesInDB) { loadMessagesFromBD(); } @@ -236,7 +236,7 @@ bool WhisperHost::isWatched(Envelope const& _e) const void WhisperHost::saveMessagesToBD() { - if (!m_useDB) + if (!m_storeMessagesInDB) return; try @@ -265,7 +265,7 @@ void WhisperHost::saveMessagesToBD() void WhisperHost::loadMessagesFromBD() { - if (!m_useDB) + if (!m_storeMessagesInDB) return; try @@ -288,14 +288,11 @@ void WhisperHost::loadMessagesFromBD() } } -void WhisperHost::saveTopicsToDB(string const& _app, string const& _password) +void WhisperHost::exportFilters(RLPStream& o_dst) const { - bytes plain; - RLPStream rlp; - DEV_GUARDED(m_filterLock) { - rlp.appendList(m_filters.size()); + o_dst.appendList(m_filters.size()); for (auto const& x: m_filters) { @@ -308,16 +305,7 @@ void WhisperHost::saveTopicsToDB(string const& _app, string const& _password) memcpy(p.get() + h256::size * i++, t.data(), h256::size); bytesConstRef ref(p.get(), RawDataSize); - rlp.append(ref); + o_dst.append(ref); } } - - rlp.swapOut(plain); - h256 s = sha3(_app); - h256 h = sha3(s); - bytes encrypted; - encryptSym(s, &plain, encrypted); - - WhisperFiltersDB db; - db.insert(h, encrypted); } diff --git a/libwhisper/WhisperHost.h b/libwhisper/WhisperHost.h index 6357043e9..db2f60bfd 100644 --- a/libwhisper/WhisperHost.h +++ b/libwhisper/WhisperHost.h @@ -48,7 +48,7 @@ class WhisperHost: public HostCapability, public Interface, public friend class WhisperPeer; public: - WhisperHost(bool _useDB = false); + WhisperHost(bool _storeMessagesInDB = false); virtual ~WhisperHost(); unsigned protocolVersion() const { return WhisperProtocolVersion; } void cleanup(); ///< remove old messages @@ -63,7 +63,8 @@ public: virtual h256s checkWatch(unsigned _watchId) override; virtual h256s watchMessages(unsigned _watchId) override; ///< returns IDs of messages, which match specific watch criteria virtual Envelope envelope(h256 _m) const override { try { dev::ReadGuard l(x_messages); return m_messages.at(_m); } catch (...) { return Envelope(); } } - virtual void saveTopicsToDB(std::string const& _app, std::string const& _password); + + void exportFilters(dev::RLPStream& o_dst) const; protected: virtual void doWork() override; @@ -86,7 +87,7 @@ private: std::map m_watches; TopicBloomFilter m_bloom; - bool m_useDB; ///< needed for tests and other special cases + bool m_storeMessagesInDB; ///< needed for tests and other special cases }; } diff --git a/test/libwhisper/whisperDB.cpp b/test/libwhisper/whisperDB.cpp index b6fabfa07..aca6f6914 100644 --- a/test/libwhisper/whisperDB.cpp +++ b/test/libwhisper/whisperDB.cpp @@ -213,15 +213,15 @@ BOOST_AUTO_TEST_CASE(filters) { cnote << "Testing filters saving..."; VerbosityHolder setTemporaryLevel(2); - string const app("test suite whisperDB/filters"); - string const password("some pseudorandom stuff"); // todo: delete + string const password("some pseudorandom string"); { + WhisperFiltersDB db; p2p::Host h("Test"); auto wh = h.registerCapability(new WhisperHost()); wh->installWatch(BuildTopic("t1")); wh->installWatch(BuildTopic("t2")); - wh->saveTopicsToDB(app, password); + db.saveTopicsToDB(*wh, password); } short unsigned port1 = 30313; @@ -235,9 +235,11 @@ BOOST_AUTO_TEST_CASE(filters) host1.setIdealPeerCount(1); auto whost1 = host1.registerCapability(new WhisperHost()); host1.start(); - //auto ids = whost1->restoreTopicsFromDB(app, password); // todo: delete WhisperFiltersDB db; - auto ids = db.restoreTopicsFromDB(whost1.get(), app); + auto watches = db.restoreTopicsFromDB(whost1.get(), password); + auto zero = db.restoreTopicsFromDB(whost1.get(), password + "qwer"); + BOOST_REQUIRE(!watches.empty()); + BOOST_REQUIRE(zero.empty()); std::thread listener([&]() { @@ -249,7 +251,7 @@ BOOST_AUTO_TEST_CASE(filters) for (unsigned j = 0; j < 200 && messageCount < 2; ++j) { - for (unsigned id: ids) + for (unsigned id: watches) for (auto const& e: whost1->checkWatch(id)) { Message msg = whost1->envelope(e).open(whost1->fullTopics(id));