Browse Source

moved some fucntions to WhsiperDB class

cl-refactor
Vlad Gluhovsky 10 years ago
parent
commit
68d6aa8906
  1. 54
      libwhisper/WhisperDB.cpp
  2. 3
      libwhisper/WhisperDB.h
  3. 24
      libwhisper/WhisperHost.cpp
  4. 7
      libwhisper/WhisperHost.h
  5. 14
      test/libwhisper/whisperDB.cpp

54
libwhisper/WhisperDB.cpp

@ -158,36 +158,48 @@ void WhisperMessagesDB::saveSingleMessage(h256 const& _key, Envelope const& _e)
} }
} }
vector<unsigned> WhisperFiltersDB::restoreTopicsFromDB(WhisperHost* _host, string const& _app) vector<unsigned> WhisperFiltersDB::restoreTopicsFromDB(WhisperHost* _host, string const& _password)
{ {
vector<unsigned> ret; vector<unsigned> ret;
h256 s = sha3(_app); h256 s = sha3(_password);
h256 h = sha3(s); h256 h = sha3(s);
string raw = lookup(h); string raw = lookup(h);
if (!raw.empty())
bytes plain;
decryptSym(s, raw, plain);
RLP rlp(plain);
auto sz = rlp.itemCountStrict();
for (unsigned i = 0; i < sz; ++i)
{ {
RLP r = rlp[i]; bytes plain;
bytesConstRef ref(r.toBytesConstRef()); decryptSym(s, raw, plain);
Topics topics; RLP rlp(plain);
unsigned num = ref.size() / h256::size; auto sz = rlp.itemCountStrict();
for (unsigned j = 0; j < num; ++j)
for (unsigned i = 0; i < sz; ++i)
{ {
h256 topic(ref.data() + j * h256::size, h256::ConstructFromPointerType()); RLP r = rlp[i];
topics.push_back(topic); 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); unsigned w = _host->installWatch(topics);
ret.push_back(w); ret.push_back(w);
}
} }
return ret; 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);
}

3
libwhisper/WhisperDB.h

@ -69,7 +69,8 @@ class WhisperFiltersDB: public WhisperDB
public: public:
WhisperFiltersDB(): WhisperDB("filters") {} WhisperFiltersDB(): WhisperDB("filters") {}
virtual ~WhisperFiltersDB() {} virtual ~WhisperFiltersDB() {}
std::vector<unsigned> restoreTopicsFromDB(WhisperHost* _host, std::string const& _app); std::vector<unsigned> restoreTopicsFromDB(WhisperHost* _host, std::string const& _password);
void saveTopicsToDB(WhisperHost const& _host, std::string const& _password);
}; };
} }

24
libwhisper/WhisperHost.cpp

@ -30,7 +30,7 @@ using namespace dev;
using namespace dev::p2p; using namespace dev::p2p;
using namespace dev::shh; using namespace dev::shh;
WhisperHost::WhisperHost(bool _useDB): Worker("shh"), m_useDB(_useDB) WhisperHost::WhisperHost(bool _storeMessagesInDB): Worker("shh"), m_storeMessagesInDB(_storeMessagesInDB)
{ {
loadMessagesFromBD(); loadMessagesFromBD();
} }
@ -236,7 +236,7 @@ bool WhisperHost::isWatched(Envelope const& _e) const
void WhisperHost::saveMessagesToBD() void WhisperHost::saveMessagesToBD()
{ {
if (!m_useDB) if (!m_storeMessagesInDB)
return; return;
try try
@ -265,7 +265,7 @@ void WhisperHost::saveMessagesToBD()
void WhisperHost::loadMessagesFromBD() void WhisperHost::loadMessagesFromBD()
{ {
if (!m_useDB) if (!m_storeMessagesInDB)
return; return;
try 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) DEV_GUARDED(m_filterLock)
{ {
rlp.appendList(m_filters.size()); o_dst.appendList(m_filters.size());
for (auto const& x: m_filters) 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); memcpy(p.get() + h256::size * i++, t.data(), h256::size);
bytesConstRef ref(p.get(), RawDataSize); 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);
} }

7
libwhisper/WhisperHost.h

@ -48,7 +48,7 @@ class WhisperHost: public HostCapability<WhisperPeer>, public Interface, public
friend class WhisperPeer; friend class WhisperPeer;
public: public:
WhisperHost(bool _useDB = false); WhisperHost(bool _storeMessagesInDB = false);
virtual ~WhisperHost(); virtual ~WhisperHost();
unsigned protocolVersion() const { return WhisperProtocolVersion; } unsigned protocolVersion() const { return WhisperProtocolVersion; }
void cleanup(); ///< remove old messages void cleanup(); ///< remove old messages
@ -63,7 +63,8 @@ public:
virtual h256s checkWatch(unsigned _watchId) override; virtual h256s checkWatch(unsigned _watchId) override;
virtual h256s watchMessages(unsigned _watchId) override; ///< returns IDs of messages, which match specific watch criteria 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 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: protected:
virtual void doWork() override; virtual void doWork() override;
@ -86,7 +87,7 @@ private:
std::map<unsigned, ClientWatch> m_watches; std::map<unsigned, ClientWatch> m_watches;
TopicBloomFilter m_bloom; TopicBloomFilter m_bloom;
bool m_useDB; ///< needed for tests and other special cases bool m_storeMessagesInDB; ///< needed for tests and other special cases
}; };
} }

14
test/libwhisper/whisperDB.cpp

@ -213,15 +213,15 @@ BOOST_AUTO_TEST_CASE(filters)
{ {
cnote << "Testing filters saving..."; cnote << "Testing filters saving...";
VerbosityHolder setTemporaryLevel(2); VerbosityHolder setTemporaryLevel(2);
string const app("test suite whisperDB/filters"); string const password("some pseudorandom string");
string const password("some pseudorandom stuff"); // todo: delete
{ {
WhisperFiltersDB db;
p2p::Host h("Test"); p2p::Host h("Test");
auto wh = h.registerCapability(new WhisperHost()); auto wh = h.registerCapability(new WhisperHost());
wh->installWatch(BuildTopic("t1")); wh->installWatch(BuildTopic("t1"));
wh->installWatch(BuildTopic("t2")); wh->installWatch(BuildTopic("t2"));
wh->saveTopicsToDB(app, password); db.saveTopicsToDB(*wh, password);
} }
short unsigned port1 = 30313; short unsigned port1 = 30313;
@ -235,9 +235,11 @@ BOOST_AUTO_TEST_CASE(filters)
host1.setIdealPeerCount(1); host1.setIdealPeerCount(1);
auto whost1 = host1.registerCapability(new WhisperHost()); auto whost1 = host1.registerCapability(new WhisperHost());
host1.start(); host1.start();
//auto ids = whost1->restoreTopicsFromDB(app, password); // todo: delete
WhisperFiltersDB db; 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([&]() std::thread listener([&]()
{ {
@ -249,7 +251,7 @@ BOOST_AUTO_TEST_CASE(filters)
for (unsigned j = 0; j < 200 && messageCount < 2; ++j) for (unsigned j = 0; j < 200 && messageCount < 2; ++j)
{ {
for (unsigned id: ids) for (unsigned id: watches)
for (auto const& e: whost1->checkWatch(id)) for (auto const& e: whost1->checkWatch(id))
{ {
Message msg = whost1->envelope(e).open(whost1->fullTopics(id)); Message msg = whost1->envelope(e).open(whost1->fullTopics(id));

Loading…
Cancel
Save