Browse Source

Correct namespace for a few things in devcrypto.

Added lower_bound to TrieDB.
Added nextActiveAddress to State.
cl-refactor
Gav Wood 10 years ago
parent
commit
c7634c27b0
  1. 2
      alethzero/MainWin.cpp
  2. 2
      eth/main.cpp
  3. 15
      exp/main.cpp
  4. 5
      libdevcrypto/Common.cpp
  5. 4
      libdevcrypto/MemoryDB.cpp
  6. 3
      libdevcrypto/MemoryDB.h
  7. 4
      libdevcrypto/OverlayDB.cpp
  8. 3
      libdevcrypto/OverlayDB.h
  9. 4
      libdevcrypto/SHA3.cpp
  10. 3
      libdevcrypto/SHA3.h
  11. 3
      libdevcrypto/TrieCommon.cpp
  12. 22
      libdevcrypto/TrieCommon.h
  13. 3
      libdevcrypto/TrieDB.cpp
  14. 168
      libdevcrypto/TrieDB.h
  15. 2
      libethcore/BlockInfo.cpp
  16. 2
      libethcore/CommonEth.cpp
  17. 4
      libethereum/BlockChain.h
  18. 5
      libethereum/CommonNet.h
  19. 2
      libethereum/MessageFilter.cpp
  20. 15
      libethereum/State.cpp
  21. 5
      libethereum/State.h
  22. 2
      libethereum/Transaction.cpp
  23. 4
      libethereum/Transaction.h
  24. 8
      libqethereum/QEthereum.cpp
  25. 2
      libwhisper/Common.h
  26. 13
      libwhisper/Interface.cpp
  27. 19
      libwhisper/Interface.h
  28. 8
      libwhisper/Message.h
  29. 2
      libwhisper/WhisperHost.h
  30. 2
      neth/main.cpp
  31. 4
      test/MemTrie.cpp
  32. 2
      test/crypto.cpp
  33. 1
      test/hexPrefix.cpp
  34. 46
      test/trie.cpp

2
alethzero/MainWin.cpp

@ -1327,7 +1327,7 @@ void Main::on_contracts_currentItemChanged()
s << "<h4>Body Code</h4>" << disassemble(ethereum()->codeAt(address));
ui->contractInfo->appendHtml(QString::fromStdString(s.str()));
}
catch (dev::eth::InvalidTrie)
catch (dev::InvalidTrie)
{
ui->contractInfo->appendHtml("Corrupted trie.");
}

2
eth/main.cpp

@ -700,7 +700,7 @@ int main(int argc, char** argv)
cnote << "Saved" << rechex << "to" << outFile;
}
catch (dev::eth::InvalidTrie)
catch (dev::InvalidTrie)
{
cwarn << "Corrupted trie.";
}

15
exp/main.cpp

@ -29,12 +29,14 @@
#include <libdevcore/RangeMask.h>
#include <libethereum/DownloadMan.h>
#include <libwhisper/WhisperPeer.h>
#include <libwhisper/WhisperHost.h>
using namespace std;
using namespace dev;
using namespace dev::eth;
using namespace dev::p2p;
using namespace dev::shh;
#if 0
int main()
{
DownloadMan man;
@ -69,8 +71,8 @@ int main()
cnote << i;*/
return 0;
}
#endif
/*
int main(int argc, char** argv)
{
g_logVerbosity = 20;
@ -102,15 +104,16 @@ int main(int argc, char** argv)
ph.connect(remoteHost, remotePort);
/// Only interested in the packet if the lowest bit is 1
auto w = wh->installWatch(MessageFilter(std::vector<std::pair<bytes, bytes> >({{fromHex("0000000000000000000000000000000000000000000000000000000000000001"), fromHex("0000000000000000000000000000000000000000000000000000000000000001")}})));
auto w = wh->installWatch(MessageFilter(TopicMasks({{Topic("0000000000000000000000000000000000000000000000000000000000000001"), Topic("0000000000000000000000000000000000000000000000000000000000000001")}})));
for (int i = 0; ; ++i)
{
wh->sendRaw(h256(u256(i * i)).asBytes(), h256(u256(i)).asBytes(), 1000);
wh->sendRaw(RLPStream().append(i * i).out(), Topic(u256(i)), 1000);
for (auto i: wh->checkWatch(w))
cnote << "New message:" << (u256)h256(wh->message(i).payload);
{
auto p = wh->message(i).payload;
cnote << "New message:" << RLP(p).toInt<unsigned>();
}
}
return 0;
}
*/

5
libdevcrypto/Common.cpp

@ -25,7 +25,6 @@
#include "SHA3.h"
using namespace std;
using namespace dev;
using namespace dev::eth;
//#define ETH_ADDRESS_DEBUG 1
@ -46,7 +45,7 @@ Address dev::toAddress(Secret _private)
ok = secp256k1_ecdsa_pubkey_verify(pubkey, 65);
if (!ok)
return Address();
auto ret = right160(dev::eth::sha3(bytesConstRef(&(pubkey[1]), 64)));
auto ret = right160(dev::sha3(bytesConstRef(&(pubkey[1]), 64)));
#if ETH_ADDRESS_DEBUG
cout << "---- ADDRESS -------------------------------" << endl;
cout << "SEC: " << _private << endl;
@ -94,7 +93,7 @@ KeyPair::KeyPair(h256 _sec):
m_secret = m_secret;
memcpy(m_public.data(), &(pubkey[1]), 64);
m_address = right160(dev::eth::sha3(bytesConstRef(&(pubkey[1]), 64)));
m_address = right160(dev::sha3(bytesConstRef(&(pubkey[1]), 64)));
#if ETH_ADDRESS_DEBUG
cout << "---- ADDRESS -------------------------------" << endl;

4
libdevcrypto/MemoryDB.cpp

@ -23,12 +23,9 @@
#include "MemoryDB.h"
using namespace std;
using namespace dev;
using namespace dev::eth;
namespace dev
{
namespace eth
{
std::map<h256, std::string> MemoryDB::get() const
{
@ -116,4 +113,3 @@ set<h256> MemoryDB::keys() const
}
}
}

3
libdevcrypto/MemoryDB.h

@ -29,8 +29,6 @@
namespace dev
{
namespace eth
{
struct DBChannel: public LogChannel { static const char* name() { return "TDB"; } static const int verbosity = 18; };
@ -85,4 +83,3 @@ inline std::ostream& operator<<(std::ostream& _out, MemoryDB const& _m)
}
}
}

4
libdevcrypto/OverlayDB.cpp

@ -23,12 +23,9 @@
#include "OverlayDB.h"
using namespace std;
using namespace dev;
using namespace dev::eth;
namespace dev
{
namespace eth
{
OverlayDB::~OverlayDB()
{
@ -100,4 +97,3 @@ void OverlayDB::kill(h256 _h)
}
}
}

3
libdevcrypto/OverlayDB.h

@ -34,8 +34,6 @@ namespace ldb = leveldb;
namespace dev
{
namespace eth
{
class OverlayDB: public MemoryDB
{
@ -63,4 +61,3 @@ private:
};
}
}

4
libdevcrypto/SHA3.cpp

@ -24,12 +24,9 @@
using namespace std;
using namespace dev;
using namespace dev::eth;
namespace dev
{
namespace eth
{
h256 EmptySHA3 = sha3(bytesConstRef());
@ -118,4 +115,3 @@ bytes aesDecrypt(bytesConstRef _ivCipher, std::string const& _password, unsigned
}
}
}

3
libdevcrypto/SHA3.h

@ -29,8 +29,6 @@
namespace dev
{
namespace eth
{
// SHA-3 convenience routines.
@ -69,4 +67,3 @@ void sha256(bytesConstRef _input, bytesRef _output);
void ripemd160(bytesConstRef _input, bytesRef _output);
}
}

3
libdevcrypto/TrieCommon.cpp

@ -23,8 +23,6 @@
namespace dev
{
namespace eth
{
/*
* Hex-prefix Notation. First nibble has flags: oddness = 2^0 & termination = 2^1
@ -127,4 +125,3 @@ byte uniqueInUse(RLP const& _orig, byte except)
}
}
}

22
libdevcrypto/TrieCommon.h

@ -26,8 +26,6 @@
namespace dev
{
namespace eth
{
inline byte nibble(bytesConstRef _data, unsigned _i)
{
@ -49,10 +47,29 @@ struct NibbleSlice
NibbleSlice(bytesConstRef _d = bytesConstRef(), unsigned _o = 0): data(_d), offset(_o) {}
byte operator[](unsigned _index) const { return nibble(data, offset + _index); }
unsigned size() const { return data.size() * 2 - offset; }
bool empty() const { return !size(); }
NibbleSlice mid(unsigned _index) const { return NibbleSlice(data, offset + _index); }
void clear() { data.reset(); offset = 0; }
bool contains(NibbleSlice _k) const { return shared(_k) == _k.size(); }
unsigned shared(NibbleSlice _k) const { return sharedNibbles(data, offset, offset + size(), _k.data, _k.offset, _k.offset + _k.size()); }
/**
* @brief Determine if we, a full key, are situated prior to a particular key-prefix.
* @param _k The prefix.
* @return true if we are strictly prior to the prefix.
*/
bool isEarlierThan(NibbleSlice _k) const
{
unsigned i;
for (i = 0; i < _k.size() && i < size(); ++i)
if (operator[](i) < _k[i]) // Byte is lower - we're earlier..
return true;
else if (operator[](i) > _k[i]) // Byte is higher - we're not earlier.
return false;
if (i >= _k.size()) // Ran past the end of the prefix - we're == for the entire prefix - we're not earlier.
return false;
return true; // Ran out before the prefix had finished - we're earlier.
}
bool operator==(NibbleSlice _k) const { return _k.size() == size() && shared(_k) == _k.size(); }
bool operator!=(NibbleSlice _s) const { return !operator==(_s); }
};
@ -102,4 +119,3 @@ inline std::string hexPrefixEncode(NibbleSlice _s1, NibbleSlice _s2, bool _leaf)
}
}
}

3
libdevcrypto/TrieDB.cpp

@ -23,10 +23,9 @@
#include "TrieDB.h"
using namespace std;
using namespace dev;
using namespace dev::eth;
#if !ETH_LANGUAGES
const h256 dev::eth::c_shaNull = sha3(rlp(""));
const h256 dev::c_shaNull = sha3(rlp(""));
#endif

168
libdevcrypto/TrieDB.h

@ -39,8 +39,6 @@ namespace ldb = leveldb;
namespace dev
{
namespace eth
{
struct TrieDBChannel: public LogChannel { static const char* name() { return "-T-"; } static const int verbosity = 17; };
#define tdebug clog(TrieDBChannel)
@ -171,6 +169,7 @@ public:
iterator() {}
iterator(GenericTrieDB const* _db);
iterator(GenericTrieDB const* _db, bytesConstRef _key);
iterator& operator++() { next(); return *this; }
@ -184,13 +183,17 @@ public:
private:
void next();
void next(NibbleSlice _key);
struct Node
{
std::string rlp;
std::string key; // as hexPrefixEncoding.
byte child; // 255 -> entering
byte child; // 255 -> entering, 16 -> actually at the node, 17 -> exiting, 0-15 -> actual children.
// 255 -> 16 -> 0 -> 1 -> ... -> 15 -> 17
void setChild(unsigned _i) { child = _i; }
void setFirstChild() { child = 16; }
void incrementChild() { child = child == 16 ? 0 : child == 15 ? 17 : (child + 1); }
@ -205,6 +208,8 @@ public:
iterator begin() const { return this; }
iterator end() const { return iterator(); }
iterator lower_bound(bytesConstRef _key) const { return iterator(this, _key); }
private:
RLPStream& streamNode(RLPStream& _s, bytes const& _b);
@ -298,6 +303,7 @@ public:
iterator() {}
iterator(TrieDB const* _db): Super(_db) {}
iterator(TrieDB const* _db, bytesConstRef _k): Super(_db, _k) {}
value_type operator*() const { return at(); }
value_type operator->() const { return at(); }
@ -307,6 +313,7 @@ public:
iterator begin() const { return this; }
iterator end() const { return iterator(); }
iterator lower_bound(KeyType _k) const { return iterator(this, bytesConstRef((byte const*)&_k, sizeof(KeyType))); }
};
template <class KeyType, class DB>
@ -317,14 +324,11 @@ std::ostream& operator<<(std::ostream& _out, TrieDB<KeyType, DB> const& _db)
return _out;
}
}
}
// Template implementations...
namespace dev
{
namespace eth
{
template <class DB> GenericTrieDB<DB>::iterator::iterator(GenericTrieDB const* _db)
{
@ -333,6 +337,13 @@ template <class DB> GenericTrieDB<DB>::iterator::iterator(GenericTrieDB const* _
next();
}
template <class DB> GenericTrieDB<DB>::iterator::iterator(GenericTrieDB const* _db, bytesConstRef _fullKey)
{
m_that = _db;
m_trail.push_back({_db->node(_db->m_root), std::string(1, '\0'), 255}); // one null byte is the HPE for the empty key.
next(_fullKey);
}
template <class DB> typename GenericTrieDB<DB>::iterator::value_type GenericTrieDB<DB>::iterator::at() const
{
assert(m_trail.size());
@ -341,10 +352,145 @@ template <class DB> typename GenericTrieDB<DB>::iterator::value_type GenericTrie
assert(!(b.key[0] & 0x10)); // should be an integer number of bytes (i.e. not an odd number of nibbles).
RLP rlp(b.rlp);
if (rlp.itemCount() == 2)
return std::make_pair(bytesConstRef(b.key).cropped(1), rlp[1].payload());
else
return std::make_pair(bytesConstRef(b.key).cropped(1), rlp[16].payload());
return std::make_pair(bytesConstRef(b.key).cropped(1), rlp[rlp.itemCount() == 2 ? 1 : 16].payload());
}
template <class DB> void GenericTrieDB<DB>::iterator::next(NibbleSlice _key)
{
NibbleSlice k = _key;
while (true)
{
if (m_trail.empty())
{
m_that = nullptr;
return;
}
Node const& b = m_trail.back();
RLP rlp(b.rlp);
if (m_trail.back().child == 255)
{
// Entering. Look for first...
if (rlp.isEmpty())
{
// Kill our search as soon as we hit an empty node.
k.clear();
m_trail.pop_back();
continue;
}
if (!rlp.isList() || (rlp.itemCount() != 2 && rlp.itemCount() != 17))
{
#if ETH_PARANOIA
cwarn << "BIG FAT ERROR. STATE TRIE CORRUPTED!!!!!";
cwarn << b.rlp.size() << toHex(b.rlp);
cwarn << rlp;
auto c = rlp.itemCount();
cwarn << c;
BOOST_THROW_EXCEPTION(InvalidTrie());
#else
m_that = nullptr;
return;
#endif
}
if (rlp.itemCount() == 2)
{
// Just turn it into a valid Branch
auto keyOfRLP = keyOf(rlp);
// TODO: do something different depending on how keyOfRLP compares to k.mid(0, std::min(k.size(), keyOfRLP.size()));
// if == all is good - continue descent.
// if > discard key and continue descent.
// if < discard key and skip node.
if (!k.contains(keyOfRLP))
{
if (!k.isEarlierThan(keyOfRLP))
{
k.clear();
m_trail.pop_back();
continue;
}
k.clear();
}
k = k.mid(std::min(k.size(), keyOfRLP.size()));
m_trail.back().key = hexPrefixEncode(keyOf(m_trail.back().key), keyOfRLP, false);
if (isLeaf(rlp))
{
// leaf - exit now.
if (k.empty())
{
m_trail.back().child = 0;
return;
}
// Still data in key we're supposed to be looking for when we're at a leaf. Go for next one.
k.clear();
m_trail.pop_back();
continue;
}
// enter child.
m_trail.back().rlp = m_that->deref(rlp[1]);
// no need to set .child as 255 - it's already done.
continue;
}
else
{
// Already a branch - look for first valid.
if (k.size())
{
m_trail.back().setChild(k[0]);
k = k.mid(1);
}
else
m_trail.back().setChild(16);
// run through to...
}
}
else
{
// Continuing/exiting. Look for next...
if (!(rlp.isList() && rlp.itemCount() == 17))
{
k.clear();
m_trail.pop_back();
continue;
}
// else run through to...
m_trail.back().incrementChild();
}
// ...here. should only get here if we're a list.
assert(rlp.isList() && rlp.itemCount() == 17);
for (;; m_trail.back().incrementChild())
if (m_trail.back().child == 17)
{
// finished here.
k.clear();
m_trail.pop_back();
break;
}
else if (!rlp[m_trail.back().child].isEmpty())
{
if (m_trail.back().child == 16)
return; // have a value at this node - exit now.
else
{
// lead-on to another node - enter child.
// fixed so that Node passed into push_back is constructed *before* m_trail is potentially resized (which invalidates back and rlp)
Node const& back = m_trail.back();
m_trail.push_back(Node{
m_that->deref(rlp[back.child]),
hexPrefixEncode(keyOf(back.key), NibbleSlice(bytesConstRef(&back.child, 1), 1), false),
255
});
break;
}
}
else
k.clear();
}
}
template <class DB> void GenericTrieDB<DB>::iterator::next()
@ -923,5 +1069,3 @@ template <class DB> bytes GenericTrieDB<DB>::branch(RLP const& _orig)
}
}
}

2
libethcore/BlockInfo.cpp

@ -72,7 +72,7 @@ h256 BlockInfo::headerHash(bytesConstRef _block)
void BlockInfo::populateFromHeader(RLP const& _header, bool _checkNonce)
{
hash = dev::eth::sha3(_header.data());
hash = dev::sha3(_header.data());
int field = 0;
try

2
libethcore/CommonEth.cpp

@ -100,7 +100,7 @@ Address toAddress(Secret _private)
ok = secp256k1_ecdsa_pubkey_verify(pubkey, 65);
if (!ok)
return Address();
auto ret = right160(dev::eth::sha3(bytesConstRef(&(pubkey[1]), 64)));
auto ret = right160(dev::sha3(bytesConstRef(&(pubkey[1]), 64)));
#if ETH_ADDRESS_DEBUG
cout << "---- ADDRESS -------------------------------" << endl;
cout << "SEC: " << _private << endl;

4
libethereum/BlockChain.h

@ -39,13 +39,15 @@ namespace ldb = leveldb;
namespace dev
{
class OverlayDB;
namespace eth
{
static const h256s NullH256s;
class State;
class OverlayDB;
struct AlreadyHaveBlock: virtual Exception {};
struct UnknownParent: virtual Exception {};

5
libethereum/CommonNet.h

@ -30,6 +30,9 @@
namespace dev
{
class OverlayDB;
namespace eth
{
@ -44,7 +47,7 @@ static const unsigned c_maxHashesAsk = 256; ///< Maximum number of hashes GetBl
static const unsigned c_maxBlocks = 128; ///< Maximum number of blocks Blocks will ever send.
static const unsigned c_maxBlocksAsk = 128; ///< Maximum number of blocks we ask to receive in Blocks (when using GetChain).
#endif
class OverlayDB;
class BlockChain;
class TransactionQueue;
class EthereumHost;

2
libethereum/MessageFilter.cpp

@ -36,7 +36,7 @@ h256 MessageFilter::sha3() const
{
RLPStream s;
fillStream(s);
return dev::eth::sha3(s.out());
return dev::sha3(s.out());
}
bool MessageFilter::matches(h256 _bloom) const

15
libethereum/State.cpp

@ -58,7 +58,7 @@ void ecrecoverCode(bytesConstRef _in, bytesRef _out)
int pubkeylen = 65;
secp256k1_start();
if (secp256k1_ecdsa_recover_compact(in.hash.data(), 32, in.r.data(), pubkey, &pubkeylen, 0, (int)(u256)in.v - 27))
ret = dev::eth::sha3(bytesConstRef(&(pubkey[1]), 64));
ret = dev::sha3(bytesConstRef(&(pubkey[1]), 64));
memcpy(_out.data(), &ret, min(_out.size(), sizeof(ret)));
}
@ -207,6 +207,19 @@ State::~State()
{
}
Address State::nextActiveAddress(Address _a) const
{
auto it = m_state.lower_bound(_a);
if ((*it).first == _a)
++it;
if (it == m_state.end())
// exchange comments if we want to wraparound
// it = m_state.begin();
return Address();
return (*it).first;
}
// TODO: repot
struct CachedAddressState
{
CachedAddressState(std::string const& _rlp, AddressState const* _s, OverlayDB const* _o): rS(_rlp), r(rS), s(_s), o(_o) {}

5
libethereum/State.h

@ -40,7 +40,8 @@
namespace dev
{
namespace test{ class FakeExtVM; class FakeState;}
namespace test { class FakeExtVM; class FakeState; }
namespace eth
{
@ -116,6 +117,8 @@ public:
/// @returns the set containing all addresses currently in use in Ethereum.
std::map<Address, u256> addresses() const;
Address nextActiveAddress(Address _a) const;
BlockInfo const& info() const { return m_currentBlock; }
/// @brief Checks that mining the current object will result in a valid block.

2
libethereum/Transaction.cpp

@ -81,7 +81,7 @@ Address Transaction::sender() const
BOOST_THROW_EXCEPTION(InvalidSignature());
// TODO: check right160 is correct and shouldn't be left160.
m_sender = right160(dev::eth::sha3(bytesConstRef(&(pubkey[1]), 64)));
m_sender = right160(dev::sha3(bytesConstRef(&(pubkey[1]), 64)));
#if ETH_ADDRESS_DEBUG
cout << "---- RECOVER -------------------------------" << endl;

4
libethereum/Transaction.h

@ -67,8 +67,8 @@ struct Transaction
void fillStream(RLPStream& _s, bool _sig = true) const;
bytes rlp(bool _sig = true) const { RLPStream s; fillStream(s, _sig); return s.out(); }
std::string rlpString(bool _sig = true) const { return asString(rlp(_sig)); }
h256 sha3(bool _sig = true) const { RLPStream s; fillStream(s, _sig); return dev::eth::sha3(s.out()); }
bytes sha3Bytes(bool _sig = true) const { RLPStream s; fillStream(s, _sig); return dev::eth::sha3Bytes(s.out()); }
h256 sha3(bool _sig = true) const { RLPStream s; fillStream(s, _sig); return dev::sha3(s.out()); }
bytes sha3Bytes(bool _sig = true) const { RLPStream s; fillStream(s, _sig); return dev::sha3Bytes(s.out()); }
private:
mutable Address m_sender;

8
libqethereum/QEthereum.cpp

@ -98,22 +98,22 @@ QString QEthereum::lll(QString _s) const
QString QEthereum::sha3(QString _s) const
{
return toQJS(dev::eth::sha3(toBytes(_s)));
return toQJS(dev::sha3(toBytes(_s)));
}
QString QEthereum::sha3(QString _s1, QString _s2) const
{
return toQJS(dev::eth::sha3(asBytes(padded(_s1, 32)) + asBytes(padded(_s2, 32))));
return toQJS(dev::sha3(asBytes(padded(_s1, 32)) + asBytes(padded(_s2, 32))));
}
QString QEthereum::sha3(QString _s1, QString _s2, QString _s3) const
{
return toQJS(dev::eth::sha3(asBytes(padded(_s1, 32)) + asBytes(padded(_s2, 32)) + asBytes(padded(_s3, 32))));
return toQJS(dev::sha3(asBytes(padded(_s1, 32)) + asBytes(padded(_s2, 32)) + asBytes(padded(_s3, 32))));
}
QString QEthereum::sha3old(QString _s) const
{
return toQJS(dev::eth::sha3(asBytes(_s)));
return toQJS(dev::sha3(asBytes(_s)));
}
QString QEthereum::offset(QString _s, int _i) const

2
libwhisper/Common.h

@ -57,5 +57,7 @@ enum WhisperPacket
PacketCount
};
using Topic = h256;
}
}

13
libwhisper/Interface.cpp

@ -36,15 +36,8 @@ using namespace dev::shh;
bool MessageFilter::matches(Message const& _m) const
{
for (auto const& t: m_topicMasks)
{
if (t.first.size() != t.second.size() || _m.topic.size() < t.first.size())
continue;
for (unsigned i = 0; i < t.first.size(); ++i)
if (((t.first[i] ^ _m.topic[i]) & t.second[i]) != 0)
goto NEXT;
return true;
NEXT:;
}
for (TopicMask const& t: m_topicMasks)
if (((t.first ^ _m.topic) & t.second) == 0)
return true;
return false;
}

19
libwhisper/Interface.h

@ -37,20 +37,29 @@ namespace dev
namespace shh
{
/*struct TopicMask
{
Topic data;
Topic mask;
};*/
using TopicMask = std::pair<Topic, Topic>;
using TopicMasks = std::vector<TopicMask>;
class MessageFilter
{
public:
MessageFilter() {}
MessageFilter(std::vector<std::pair<bytes, bytes> > const& _m): m_topicMasks(_m) {}
MessageFilter(RLP const& _r): m_topicMasks((std::vector<std::pair<bytes, bytes>>)_r) {}
MessageFilter(TopicMasks const& _m): m_topicMasks(_m) {}
MessageFilter(RLP const& _r): m_topicMasks((TopicMasks)_r) {}
void fillStream(RLPStream& _s) const { _s << m_topicMasks; }
h256 sha3() const { RLPStream s; fillStream(s); return dev::eth::sha3(s.out()); }
h256 sha3() const { RLPStream s; fillStream(s); return dev::sha3(s.out()); }
bool matches(Message const& _m) const;
private:
std::vector<std::pair<bytes, bytes> > m_topicMasks;
TopicMasks m_topicMasks;
};
struct InstalledFilter
@ -85,7 +94,7 @@ public:
virtual Message message(h256 _m) const = 0;
virtual void sendRaw(bytes const& _payload, bytes const& _topic, unsigned _ttl) = 0;
virtual void sendRaw(bytes const& _payload, h256 _topic, unsigned _ttl) = 0;
};
struct WatshhChannel: public dev::LogChannel { static const char* name() { return "shh"; } static const int verbosity = 1; };

8
libwhisper/Message.h

@ -40,23 +40,23 @@ struct Message
{
unsigned expiry = 0;
unsigned ttl = 0;
bytes topic; // TODO: change to h256
Topic topic; // TODO: change to h256
bytes payload;
Message() {}
Message(unsigned _exp, unsigned _ttl, bytes const& _topic, bytes const& _payload): expiry(_exp), ttl(_ttl), topic(_topic), payload(_payload) {}
Message(unsigned _exp, unsigned _ttl, Topic const& _topic, bytes const& _payload): expiry(_exp), ttl(_ttl), topic(_topic), payload(_payload) {}
Message(RLP const& _m)
{
expiry = _m[0].toInt<unsigned>();
ttl = _m[1].toInt<unsigned>();
topic = _m[2].toBytes();
topic = (Topic)_m[2];
payload = _m[3].toBytes();
}
operator bool () const { return !!expiry; }
void streamOut(RLPStream& _s) const { _s.appendList(4) << expiry << ttl << topic << payload; }
h256 sha3() const { RLPStream s; streamOut(s); return dev::eth::sha3(s.out()); }
h256 sha3() const { RLPStream s; streamOut(s); return dev::sha3(s.out()); }
};
}

2
libwhisper/WhisperHost.h

@ -58,7 +58,7 @@ public:
virtual Message message(h256 _m) const { try { dev::ReadGuard l(x_messages); return m_messages.at(_m); } catch (...) { return Message(); } }
virtual void sendRaw(bytes const& _payload, bytes const& _topic, unsigned _ttl) { inject(Message(time(0) + _ttl, _ttl, _topic, _payload)); }
virtual void sendRaw(bytes const& _payload, Topic _topic, unsigned _ttl) { inject(Message(time(0) + _ttl, _ttl, _topic, _payload)); }
private:
void streamMessage(h256 _m, RLPStream& _s) const;

2
neth/main.cpp

@ -805,7 +805,7 @@ int main(int argc, char** argv)
cnote << "Saved" << rechex << "to" << outFile;
}
catch (dev::eth::InvalidTrie const& _e)
catch (dev::InvalidTrie const& _e)
{
cwarn << "Corrupted trie.\n" << diagnostic_information(_e);
}

4
test/MemTrie.cpp

@ -55,7 +55,7 @@ public:
#endif
/// 256-bit hash of the node - this is a SHA-3/256 hash of the RLP of the node.
h256 hash256() const { RLPStream s; makeRLP(s); return dev::eth::sha3(s.out()); }
h256 hash256() const { RLPStream s; makeRLP(s); return dev::sha3(s.out()); }
bytes rlp() const { RLPStream s; makeRLP(s); return s.out(); }
void mark() { m_hash256 = h256(); }
@ -200,7 +200,7 @@ void MemTrieNode::putRLP(RLPStream& _parentStream) const
if (s.out().size() < 32)
_parentStream.APPEND_CHILD(s.out());
else
_parentStream << dev::eth::sha3(s.out());
_parentStream << dev::sha3(s.out());
}
void TrieBranchNode::makeRLP(RLPStream& _intoStream) const

2
test/crypto.cpp

@ -150,7 +150,7 @@ int cryptoTest()
int ret = secp256k1_ecdsa_recover_compact((byte const*)hmsg.data(), (int)hmsg.size(), (byte const*)sig64.data(), pubkey.data(), &pubkeylen, 0, (int)t.vrs.v - 27);
pubkey.resize(pubkeylen);
cout << "RECPUB: " << dec << ret << " " << pubkeylen << " " << toHex(pubkey) << endl;
cout << "SENDER: " << hex << toAddress(dev::eth::sha3(bytesConstRef(&pubkey).cropped(1))) << dec << endl;
cout << "SENDER: " << hex << toAddress(dev::sha3(bytesConstRef(&pubkey).cropped(1))) << dec << endl;
}
#endif
return 0;

1
test/hexPrefix.cpp

@ -29,7 +29,6 @@
using namespace std;
using namespace dev;
using namespace dev::eth;
namespace js = json_spirit;
BOOST_AUTO_TEST_CASE(hexPrefix_test)

46
test/trie.cpp

@ -31,7 +31,6 @@
using namespace std;
using namespace dev;
using namespace dev::eth;
namespace js = json_spirit;
@ -236,6 +235,51 @@ BOOST_AUTO_TEST_CASE(moreTrieTests)
}
}
BOOST_AUTO_TEST_CASE(trieLowerBound)
{
cnote << "Stress-testing Trie.lower_bound...";
{
MemoryDB dm;
EnforceRefs e(dm, true);
GenericTrieDB<MemoryDB> d(&dm);
d.init(); // initialise as empty tree.
for (int a = 0; a < 20; ++a)
{
StringMap m;
for (int i = 0; i < 50; ++i)
{
auto k = randomWord();
auto v = toString(i);
m[k] = v;
d.insert(k, v);
}
for (auto i: d)
{
auto it = d.lower_bound(i.first);
for (auto iit = d.begin(); iit != d.end(); ++iit)
if ((*iit).first.toString() >= i.first.toString())
{
BOOST_REQUIRE(it == iit);
break;
}
}
for (unsigned i = 0; i < 100; ++i)
{
auto k = randomWord();
auto it = d.lower_bound(k);
for (auto iit = d.begin(); iit != d.end(); ++iit)
if ((*iit).first.toString() >= k)
{
BOOST_REQUIRE(it == iit);
break;
}
}
}
}
}
BOOST_AUTO_TEST_CASE(trieStess)
{
cnote << "Stress-testing Trie...";

Loading…
Cancel
Save