|
|
@ -22,39 +22,30 @@ |
|
|
|
#include "WhisperDB.h" |
|
|
|
#include <boost/filesystem.hpp> |
|
|
|
#include <libdevcore/FileSystem.h> |
|
|
|
#include "WhisperHost.h" |
|
|
|
|
|
|
|
using namespace std; |
|
|
|
using namespace dev; |
|
|
|
using namespace dev::shh; |
|
|
|
namespace fs = boost::filesystem; |
|
|
|
|
|
|
|
WhisperDB::WhisperDB(DBType _t): m_type(_t) |
|
|
|
WhisperDB::WhisperDB(string const& _type) |
|
|
|
{ |
|
|
|
m_readOptions.verify_checksums = true; |
|
|
|
string path = dev::getDataDir("shh"); |
|
|
|
fs::create_directories(path); |
|
|
|
fs::permissions(path, fs::owner_all); |
|
|
|
path.append("\\").append(_type); |
|
|
|
leveldb::Options op; |
|
|
|
op.create_if_missing = true; |
|
|
|
op.max_open_files = 256; |
|
|
|
string suffix = getTypeSuffix(); |
|
|
|
leveldb::DB* p = nullptr; |
|
|
|
leveldb::Status status = leveldb::DB::Open(op, path + suffix, &p); |
|
|
|
leveldb::Status status = leveldb::DB::Open(op, path, &p); |
|
|
|
m_db.reset(p); |
|
|
|
if (!status.ok()) |
|
|
|
BOOST_THROW_EXCEPTION(FailedToOpenLevelDB(status.ToString())); |
|
|
|
} |
|
|
|
|
|
|
|
string WhisperDB::getTypeSuffix() |
|
|
|
{ |
|
|
|
switch(m_type) |
|
|
|
{ |
|
|
|
case Messages: return "\\messages"; |
|
|
|
case Filters: return "\\filters"; |
|
|
|
} |
|
|
|
|
|
|
|
return "\\misc"; |
|
|
|
} |
|
|
|
|
|
|
|
string WhisperDB::lookup(dev::h256 const& _key) const |
|
|
|
{ |
|
|
|
string ret; |
|
|
@ -91,11 +82,8 @@ void WhisperDB::kill(dev::h256 const& _key) |
|
|
|
BOOST_THROW_EXCEPTION(FailedDeleteInLevelDB(status.ToString())); |
|
|
|
} |
|
|
|
|
|
|
|
void WhisperDB::loadAllMessages(std::map<h256, Envelope>& o_dst) |
|
|
|
void WhisperMessagesDB::loadAllMessages(std::map<h256, Envelope>& o_dst) |
|
|
|
{ |
|
|
|
if (m_type != Messages) |
|
|
|
BOOST_THROW_EXCEPTION(WrongTypeLevelDB()); |
|
|
|
|
|
|
|
leveldb::ReadOptions op; |
|
|
|
op.fill_cache = false; |
|
|
|
op.verify_checksums = true; |
|
|
@ -150,11 +138,8 @@ void WhisperDB::loadAllMessages(std::map<h256, Envelope>& o_dst) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void WhisperDB::save(h256 const& _key, Envelope const& _e) |
|
|
|
void WhisperMessagesDB::saveSingleMessage(h256 const& _key, Envelope const& _e) |
|
|
|
{ |
|
|
|
if (m_type != Messages) |
|
|
|
BOOST_THROW_EXCEPTION(WrongTypeLevelDB()); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
RLPStream rlp; |
|
|
@ -172,3 +157,37 @@ void WhisperDB::save(h256 const& _key, Envelope const& _e) |
|
|
|
cwarn << boost::diagnostic_information(ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
vector<unsigned> WhisperFiltersDB::restoreTopicsFromDB(WhisperHost* _host, string const& _app) |
|
|
|
{ |
|
|
|
vector<unsigned> ret; |
|
|
|
h256 s = sha3(_app); |
|
|
|
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) |
|
|
|
{ |
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
return ret; |
|
|
|
} |
|
|
|
|
|
|
|