Browse Source

proof of work bugfix

cl-refactor
Vlad Gluhovsky 10 years ago
parent
commit
8dfabff96c
  1. 10
      libwhisper/Message.cpp
  2. 1
      libwhisper/Message.h
  3. 12
      test/libwhisper/whisperMessage.cpp

10
libwhisper/Message.cpp

@ -161,24 +161,24 @@ unsigned Envelope::workProved() const
void Envelope::proveWork(unsigned _ms) void Envelope::proveWork(unsigned _ms)
{ {
// PoW
h256 d[2]; h256 d[2];
d[0] = sha3(WithoutNonce); d[0] = sha3(WithoutNonce);
uint32_t& n = *(uint32_t*)&(d[1][28]);
unsigned bestBitSet = 0; unsigned bestBitSet = 0;
bytesConstRef chuck(d[0].data(), 64); bytesConstRef chuck(d[0].data(), 64);
chrono::high_resolution_clock::time_point then = chrono::high_resolution_clock::now() + chrono::milliseconds(_ms); 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 // 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(); auto fbs = dev::sha3(chuck).firstBitSet();
if (fbs > bestBitSet) if (fbs > bestBitSet)
{ {
bestBitSet = fbs; bestBitSet = fbs;
m_nonce = n; m_nonce = (h256::Arith)d[1];
} }
incrementHash(d[1]);
} }
} }

1
libwhisper/Message.h

@ -81,6 +81,7 @@ public:
void proveWork(unsigned _ms); void proveWork(unsigned _ms);
bool matchesBloomFilter(TopicBloomFilterHash const& f) const; bool matchesBloomFilter(TopicBloomFilterHash const& f) const;
static void incrementHash(h256& _h) { for (unsigned i = h256::size; i > 0 && ++_h[--i] > 0; ) {} }
private: private:
Envelope(unsigned _exp, unsigned _ttl, AbridgedTopics const& _topic): m_expiry(_exp), m_ttl(_ttl), m_topic(_topic) {} Envelope(unsigned _exp, unsigned _ttl, AbridgedTopics const& _topic): m_expiry(_exp), m_ttl(_ttl), m_topic(_topic) {}

12
test/libwhisper/whisperMessage.cpp

@ -95,12 +95,18 @@ BOOST_AUTO_TEST_CASE(work)
cnote << "Testing proof of work..."; cnote << "Testing proof of work...";
Secret zero; Secret zero;
Topics topics = createRandomTopics(0xDEAD); unsigned r = 0xC0DEFEED;
bytes const payload = createRandomPayload(0xFFFFF);
for (int i = 0; i < 20; ++i)
{
Topics topics = createRandomTopics(++r);
bytes const payload = createRandomPayload(++r);
Message m(payload); Message m(payload);
Envelope e = m.seal(zero, topics, 1, 50); Envelope e = m.seal(zero, topics, 1, 50);
unsigned x = e.workProved(); unsigned x = e.workProved();
BOOST_REQUIRE(x > 0); cnote << x;
BOOST_REQUIRE(x > 4);
}
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

Loading…
Cancel
Save