Browse Source

Various fixes.

cl-refactor
Gav Wood 10 years ago
parent
commit
cec504d1a2
  1. 4
      alethzero/MainWin.cpp
  2. 5
      libweb3jsonrpc/WebThreeStubServerBase.cpp
  3. 9
      libwhisper/Interface.h
  4. 17
      libwhisper/Message.cpp
  5. 16
      libwhisper/Message.h
  6. 2
      libwhisper/WhisperHost.cpp
  7. 18
      test/whisperTopic.cpp

4
alethzero/MainWin.cpp

@ -2414,10 +2414,10 @@ void Main::refreshWhispers()
shh::Envelope const& e = w.second; shh::Envelope const& e = w.second;
shh::Message m; shh::Message m;
for (pair<Public, Secret> const& i: m_server->ids()) for (pair<Public, Secret> const& i: m_server->ids())
if (!!(m = e.open(i.second))) if (!!(m = e.open(shh::FilterKey(shh::Undefined, i.second))))
break; break;
if (!m) if (!m)
m = e.open(); m = e.open(shh::FilterKey());
QString msg; QString msg;
if (m.from()) if (m.from())

5
libweb3jsonrpc/WebThreeStubServerBase.cpp

@ -576,15 +576,12 @@ Json::Value WebThreeStubServerBase::shh_changed(int const& _id)
if (pub) if (pub)
{ {
cwarn << "Silently decrypting message from identity" << pub.abridged() << ": User validation hook goes here."; cwarn << "Silently decrypting message from identity" << pub.abridged() << ": User validation hook goes here.";
m = e.open(m_ids[pub], shh::NotPublic); m = e.open(shh::FilterKey(shh::Undefined, m_ids[pub]));
if (!m) if (!m)
continue; continue;
} }
else else
{
unsigned i = 0;
m = e.open(face()->filterKey(_id)); m = e.open(face()->filterKey(_id));
}
ret.append(toJson(h, e, m)); ret.append(toJson(h, e, m));
} }

9
libwhisper/Interface.h

@ -45,17 +45,11 @@ namespace shh
class Watch; class Watch;
struct FilterKey
{
unsigned topicIndex = (unsigned)-1;
Secret key;
};
struct InstalledFilter struct InstalledFilter
{ {
InstalledFilter(FullTopic const& _f): full(_f), filter(_f) {} InstalledFilter(FullTopic const& _f): full(_f), filter(_f) {}
FilterKey filterKey() const { unsigned i; for (i = 0; i < full.size() && !full[i]; ++i) {} return i < full.size() ? FilterKey{i, full[i]} : FilterKey(); } FilterKey filterKey() const { unsigned i; for (i = 0; i < full.size() && !full[i]; ++i) {} return i < full.size() ? FilterKey(i, full[i]) : FilterKey(); }
FullTopic full; FullTopic full;
TopicFilter filter; TopicFilter filter;
@ -116,7 +110,6 @@ public:
Watch(Interface& _c, FullTopic const& _f): m_c(&_c), m_id(_c.installWatch(_f)) {} Watch(Interface& _c, FullTopic const& _f): m_c(&_c), m_id(_c.installWatch(_f)) {}
~Watch() { if (m_c) m_c->uninstallWatch(m_id); } ~Watch() { if (m_c) m_c->uninstallWatch(m_id); }
FullTopic fullTopic() const { return m_c ? m_c->getFilter(m_id) : FullTopic(); }
h256s check() { return m_c ? m_c->checkWatch(m_id) : h256s(); } h256s check() { return m_c ? m_c->checkWatch(m_id) : h256s(); }
h256s peek() { return m_c ? m_c->peekWatch(m_id) : h256s(); } h256s peek() { return m_c ? m_c->peekWatch(m_id) : h256s(); }

17
libwhisper/Message.cpp

@ -26,27 +26,28 @@ using namespace dev;
using namespace dev::p2p; using namespace dev::p2p;
using namespace dev::shh; using namespace dev::shh;
Message::Message(Envelope const& _e, Secret const& _s, unsigned _topicIndex) Message::Message(Envelope const& _e, FilterKey const& _fk)
{ {
try try
{ {
bytes b; bytes b;
if (_s) if (_fk.topicIndex == Undefined)
if (!decrypt(_s, &(_e.data()), b)) if (!_fk.key || !decrypt(_fk.key, &(_e.data()), b))
return; return;
else{} else{}
else if (_topicIndex != (unsigned)-1) else
{ {
// public - need to get the key through combining with the topic/topicIndex we know. // public - need to get the key through combining with the topic/topicIndex we know.
if (_e.data().size() < _e.topics().size() * 32) if (_e.data().size() < _e.topics().size() * 32)
return; return;
// get key from decrypted topic key: just xor // get key from decrypted topic key: just xor
if (!decryptSym(_s ^ h256(bytesConstRef(&(_e.data())).cropped(32 * _topicIndex, 32)), bytesConstRef(&(_e.data())).cropped(32 * _e.topics().size()), b)) if (!decryptSym(_fk.key ^ h256(bytesConstRef(&(_e.data())).cropped(32 * _fk.topicIndex, 32)), bytesConstRef(&(_e.data())).cropped(32 * _e.topics().size()), b))
return; return;
} }
if (populate(b)) if (populate(b))
m_to = KeyPair(_s).pub(); if (_fk.key && _fk.topicIndex == Undefined)
m_to = KeyPair(_fk.key).pub();
} }
catch (...) // Invalid secret? TODO: replace ... with InvalidSecret catch (...) // Invalid secret? TODO: replace ... with InvalidSecret
{ {
@ -120,9 +121,9 @@ Envelope::Envelope(RLP const& _m)
m_nonce = _m[4].toInt<u256>(); m_nonce = _m[4].toInt<u256>();
} }
Message Envelope::open(Secret const& _s, unsigned _topicIndex) const Message Envelope::open(FilterKey const& _filterKey) const
{ {
return Message(*this, _s, _topicIndex); return Message(*this, _filterKey);
} }
unsigned Envelope::workProved() const unsigned Envelope::workProved() const

16
libwhisper/Message.h

@ -39,14 +39,22 @@ namespace shh
class Message; class Message;
static const unsigned Undefined = (unsigned)-1;
struct FilterKey
{
FilterKey() {}
FilterKey(unsigned _tI, Secret const& _k): topicIndex(_tI), key(_k) {}
unsigned topicIndex = Undefined;
Secret key;
};
enum IncludeNonce enum IncludeNonce
{ {
WithoutNonce = 0, WithoutNonce = 0,
WithNonce = 1 WithNonce = 1
}; };
static const unsigned NotPublic = (unsigned)-1;
class Envelope class Envelope
{ {
friend class Message; friend class Message;
@ -66,7 +74,7 @@ public:
Topic const& topic() const { return m_topic; } Topic const& topic() const { return m_topic; }
bytes const& data() const { return m_data; } bytes const& data() const { return m_data; }
Message open(Secret const& _s, unsigned _topicIndex = NotPublic) const; Message open(FilterKey const& _fk) const;
unsigned workProved() const; unsigned workProved() const;
void proveWork(unsigned _ms); void proveWork(unsigned _ms);
@ -93,7 +101,7 @@ class Message
{ {
public: public:
Message() {} Message() {}
Message(Envelope const& _e, Secret const& _s, unsigned _topicIndex); Message(Envelope const& _e, FilterKey const& _fk);
Message(bytes const& _payload): m_payload(_payload) {} Message(bytes const& _payload): m_payload(_payload) {}
Message(bytesConstRef _payload): m_payload(_payload.toBytes()) {} Message(bytesConstRef _payload): m_payload(_payload.toBytes()) {}
Message(bytes&& _payload) { std::swap(_payload, m_payload); } Message(bytes&& _payload) { std::swap(_payload, m_payload); }

2
libwhisper/WhisperHost.cpp

@ -56,7 +56,7 @@ void WhisperHost::streamMessage(h256 _m, RLPStream& _s) const
void WhisperHost::inject(Envelope const& _m, WhisperPeer* _p) void WhisperHost::inject(Envelope const& _m, WhisperPeer* _p)
{ {
cnote << "inject: " << _m.expiry() << _m.ttl() << _m.topic() << toHex(_m.data()); cnote << this << ": inject: " << _m.expiry() << _m.ttl() << _m.topic() << toHex(_m.data());
if (_m.expiry() <= time(0)) if (_m.expiry() <= time(0))
return; return;

18
test/whisperTopic.cpp

@ -34,7 +34,7 @@ BOOST_AUTO_TEST_CASE(topic)
{ {
cnote << "Testing Whisper..."; cnote << "Testing Whisper...";
auto oldLogVerbosity = g_logVerbosity; auto oldLogVerbosity = g_logVerbosity;
g_logVerbosity = 0; g_logVerbosity = 4;
bool started = false; bool started = false;
unsigned result = 0; unsigned result = 0;
@ -46,16 +46,16 @@ BOOST_AUTO_TEST_CASE(topic)
auto wh = ph.registerCapability(new WhisperHost()); auto wh = ph.registerCapability(new WhisperHost());
ph.start(); ph.start();
started = true;
/// Only interested in odd packets /// Only interested in odd packets
auto w = wh->installWatch(BuildTopicMask("odd")); auto w = wh->installWatch(BuildTopicMask("odd"));
for (int i = 0, last = 0; i < 200 && last < 81; ++i) started = true;
for (int iterout = 0, last = 0; iterout < 200 && last < 81; ++iterout)
{ {
for (auto i: wh->checkWatch(w)) for (auto i: wh->checkWatch(w))
{ {
Message msg = wh->envelope(i).open(wh->filterKey()); Message msg = wh->envelope(i).open(wh->filterKey(w));
last = RLP(msg.payload()).toInt<unsigned>(); last = RLP(msg.payload()).toInt<unsigned>();
cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>(); cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>();
result += last; result += last;
@ -116,7 +116,7 @@ BOOST_AUTO_TEST_CASE(forwarding)
{ {
for (auto i: wh->checkWatch(w)) for (auto i: wh->checkWatch(w))
{ {
Message msg = wh->envelope(i).open(); Message msg = wh->envelope(i).open(wh->filterKey(w));
unsigned last = RLP(msg.payload()).toInt<unsigned>(); unsigned last = RLP(msg.payload()).toInt<unsigned>();
cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>(); cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>();
result = last; result = last;
@ -152,7 +152,7 @@ BOOST_AUTO_TEST_CASE(forwarding)
{ {
for (auto i: wh->checkWatch(w)) for (auto i: wh->checkWatch(w))
{ {
Message msg = wh->envelope(i).open(); Message msg = wh->envelope(i).open(wh->filterKey(w));
cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>(); cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>();
} }
this_thread::sleep_for(chrono::milliseconds(50)); this_thread::sleep_for(chrono::milliseconds(50));
@ -215,7 +215,7 @@ BOOST_AUTO_TEST_CASE(asyncforwarding)
{ {
for (auto i: wh->checkWatch(w)) for (auto i: wh->checkWatch(w))
{ {
Message msg = wh->envelope(i).open(); Message msg = wh->envelope(i).open(wh->filterKey(w));
cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>(); cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>();
} }
this_thread::sleep_for(chrono::milliseconds(50)); this_thread::sleep_for(chrono::milliseconds(50));
@ -255,7 +255,7 @@ BOOST_AUTO_TEST_CASE(asyncforwarding)
{ {
for (auto i: wh->checkWatch(w)) for (auto i: wh->checkWatch(w))
{ {
Message msg = wh->envelope(i).open(); Message msg = wh->envelope(i).open(wh->filterKey(w));
unsigned last = RLP(msg.payload()).toInt<unsigned>(); unsigned last = RLP(msg.payload()).toInt<unsigned>();
cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>(); cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>();
result = last; result = last;

Loading…
Cancel
Save