From 8dfabff96c707bc56d8b8082c750011b656b5447 Mon Sep 17 00:00:00 2001 From: Vlad Gluhovsky Date: Fri, 3 Jul 2015 01:14:56 +0200 Subject: [PATCH] proof of work bugfix --- libwhisper/Message.cpp | 10 +++++----- libwhisper/Message.h | 1 + test/libwhisper/whisperMessage.cpp | 18 ++++++++++++------ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/libwhisper/Message.cpp b/libwhisper/Message.cpp index 0b235b984..2d15ee315 100644 --- a/libwhisper/Message.cpp +++ b/libwhisper/Message.cpp @@ -161,24 +161,24 @@ unsigned Envelope::workProved() const void Envelope::proveWork(unsigned _ms) { - // PoW h256 d[2]; d[0] = sha3(WithoutNonce); - uint32_t& n = *(uint32_t*)&(d[1][28]); unsigned bestBitSet = 0; bytesConstRef chuck(d[0].data(), 64); chrono::high_resolution_clock::time_point then = chrono::high_resolution_clock::now() + chrono::milliseconds(_ms); - for (n = 0; chrono::high_resolution_clock::now() < then; ) + while (chrono::high_resolution_clock::now() < then) // do it rounds of 1024 for efficiency - for (unsigned i = 0; i < 1024; ++i, ++n) + for (unsigned i = 0; i < 1024; ++i) { auto fbs = dev::sha3(chuck).firstBitSet(); if (fbs > bestBitSet) { bestBitSet = fbs; - m_nonce = n; + m_nonce = (h256::Arith)d[1]; } + + incrementHash(d[1]); } } diff --git a/libwhisper/Message.h b/libwhisper/Message.h index ae61f210a..e382a328b 100644 --- a/libwhisper/Message.h +++ b/libwhisper/Message.h @@ -81,6 +81,7 @@ public: void proveWork(unsigned _ms); bool matchesBloomFilter(TopicBloomFilterHash const& f) const; + static void incrementHash(h256& _h) { for (unsigned i = h256::size; i > 0 && ++_h[--i] > 0; ) {} } private: Envelope(unsigned _exp, unsigned _ttl, AbridgedTopics const& _topic): m_expiry(_exp), m_ttl(_ttl), m_topic(_topic) {} diff --git a/test/libwhisper/whisperMessage.cpp b/test/libwhisper/whisperMessage.cpp index 1db3352ac..8d9a29579 100644 --- a/test/libwhisper/whisperMessage.cpp +++ b/test/libwhisper/whisperMessage.cpp @@ -95,12 +95,18 @@ BOOST_AUTO_TEST_CASE(work) cnote << "Testing proof of work..."; Secret zero; - Topics topics = createRandomTopics(0xDEAD); - bytes const payload = createRandomPayload(0xFFFFF); - Message m(payload); - Envelope e = m.seal(zero, topics, 1, 50); - unsigned x = e.workProved(); - BOOST_REQUIRE(x > 0); + unsigned r = 0xC0DEFEED; + + for (int i = 0; i < 20; ++i) + { + Topics topics = createRandomTopics(++r); + bytes const payload = createRandomPayload(++r); + Message m(payload); + Envelope e = m.seal(zero, topics, 1, 50); + unsigned x = e.workProved(); + cnote << x; + BOOST_REQUIRE(x > 4); + } } BOOST_AUTO_TEST_SUITE_END()