Browse Source

refactoring

cl-refactor
Vlad Gluhovsky 10 years ago
parent
commit
432e1988f9
  1. 2
      libethereum/BlockChain.cpp
  2. 20
      libp2p/Common.h
  3. 22
      libwhisper/WhisperDB.cpp
  4. 4
      libwhisper/WhisperDB.h
  5. 8
      test/libwhisper/whisperDB.cpp

2
libethereum/BlockChain.cpp

@ -386,6 +386,7 @@ tuple<ImportRoute, bool, unsigned> BlockChain::sync(BlockQueue& _bq, OverlayDB c
if (!badBlocks.empty()) if (!badBlocks.empty())
badBlocks.push_back(block.verified.info.hash()); badBlocks.push_back(block.verified.info.hash());
else else
{
do { do {
try try
{ {
@ -427,6 +428,7 @@ tuple<ImportRoute, bool, unsigned> BlockChain::sync(BlockQueue& _bq, OverlayDB c
badBlocks.push_back(block.verified.info.hash()); badBlocks.push_back(block.verified.info.hash());
} }
} while (false); } while (false);
}
return make_tuple(ImportRoute{dead, fresh, goodTransactions}, _bq.doneDrain(badBlocks), count); return make_tuple(ImportRoute{dead, fresh, goodTransactions}, _bq.doneDrain(badBlocks), count);
} }

20
libp2p/Common.h

@ -243,25 +243,7 @@ public:
void stop() { m_stopped = true; DEV_GUARDED(x_timers) m_timers.clear(); } void stop() { m_stopped = true; DEV_GUARDED(x_timers) m_timers.clear(); }
protected: protected:
void reap() void reap();
{
Guard l(x_timers);
std::vector<DeadlineOp>::iterator t = m_timers.begin();
while (t != m_timers.end())
if (t->expired())
{
t->wait();
t = m_timers.erase(t);
}
else
t++;
m_timers.emplace_back(m_io, m_reapIntervalMs, [this](boost::system::error_code const& ec)
{
if (!ec)
reap();
});
}
private: private:
ba::io_service& m_io; ba::io_service& m_io;

22
libwhisper/WhisperDB.cpp

@ -158,17 +158,13 @@ void WhisperMessagesDB::saveSingleMessage(h256 const& _key, Envelope const& _e)
} }
} }
vector<unsigned> WhisperFiltersDB::restoreTopicsFromDB(WhisperHost* _host, string const& _password) vector<unsigned> WhisperFiltersDB::restoreTopicsFromDB(WhisperHost* _host, h256 const& _id)
{ {
vector<unsigned> ret; vector<unsigned> ret;
h256 s = sha3(_password); string raw = lookup(_id);
h256 h = sha3(s);
string raw = lookup(h);
if (!raw.empty()) if (!raw.empty())
{ {
bytes plain; RLP rlp(raw);
decryptSym(s, raw, plain);
RLP rlp(plain);
auto sz = rlp.itemCountStrict(); auto sz = rlp.itemCountStrict();
for (unsigned i = 0; i < sz; ++i) for (unsigned i = 0; i < sz; ++i)
@ -191,15 +187,11 @@ vector<unsigned> WhisperFiltersDB::restoreTopicsFromDB(WhisperHost* _host, strin
return ret; return ret;
} }
void WhisperFiltersDB::saveTopicsToDB(WhisperHost const& _host, std::string const& _password) void WhisperFiltersDB::saveTopicsToDB(WhisperHost const& _host, h256 const& _id)
{ {
bytes plain; bytes b;
RLPStream rlp; RLPStream rlp;
_host.exportFilters(rlp); _host.exportFilters(rlp);
rlp.swapOut(plain); rlp.swapOut(b);
h256 s = sha3(_password); insert(_id, b);
h256 h = sha3(s);
bytes encrypted;
encryptSym(s, &plain, encrypted);
insert(h, encrypted);
} }

4
libwhisper/WhisperDB.h

@ -69,8 +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& _password); std::vector<unsigned> restoreTopicsFromDB(WhisperHost* _host, h256 const& _id);
void saveTopicsToDB(WhisperHost const& _host, std::string const& _password); void saveTopicsToDB(WhisperHost const& _host, h256 const& _id);
}; };
} }

8
test/libwhisper/whisperDB.cpp

@ -213,7 +213,7 @@ BOOST_AUTO_TEST_CASE(filters)
{ {
cnote << "Testing filters saving..."; cnote << "Testing filters saving...";
VerbosityHolder setTemporaryLevel(2); VerbosityHolder setTemporaryLevel(2);
string const password("some pseudorandom string"); h256 persistID(0xC0FFEE);
{ {
WhisperFiltersDB db; WhisperFiltersDB db;
@ -221,7 +221,7 @@ BOOST_AUTO_TEST_CASE(filters)
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"));
db.saveTopicsToDB(*wh, password); db.saveTopicsToDB(*wh, persistID);
} }
short unsigned port1 = 30313; short unsigned port1 = 30313;
@ -236,8 +236,8 @@ BOOST_AUTO_TEST_CASE(filters)
auto whost1 = host1.registerCapability(new WhisperHost()); auto whost1 = host1.registerCapability(new WhisperHost());
host1.start(); host1.start();
WhisperFiltersDB db; WhisperFiltersDB db;
auto watches = db.restoreTopicsFromDB(whost1.get(), password); auto watches = db.restoreTopicsFromDB(whost1.get(), persistID);
auto zero = db.restoreTopicsFromDB(whost1.get(), password + "qwer"); auto zero = db.restoreTopicsFromDB(whost1.get(), ++persistID);
BOOST_REQUIRE(!watches.empty()); BOOST_REQUIRE(!watches.empty());
BOOST_REQUIRE(zero.empty()); BOOST_REQUIRE(zero.empty());

Loading…
Cancel
Save