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;
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);
}

3
libwhisper/WhisperDB.h

@ -69,7 +69,8 @@ class WhisperFiltersDB: public WhisperDB
public:
WhisperFiltersDB(): WhisperDB("filters") {}
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::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);
}

7
libwhisper/WhisperHost.h

@ -48,7 +48,7 @@ class WhisperHost: public HostCapability<WhisperPeer>, 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<unsigned, ClientWatch> 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
};
}

14
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));

Loading…
Cancel
Save