From 140fc42e09163e68409ff8ea09822da10c8b67d5 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sun, 12 Jan 2014 20:30:15 +0000 Subject: [PATCH] Moved to endian-neutral 256/160-bit datatypes. --- libethereum/AddressState.cpp | 2 +- libethereum/AddressState.h | 4 +- libethereum/BlockChain.cpp | 8 +-- libethereum/BlockChain.h | 22 ++++---- libethereum/BlockInfo.cpp | 12 ++--- libethereum/BlockInfo.h | 15 +++--- libethereum/Common.cpp | 9 ++-- libethereum/Common.h | 89 ++++++++++++++++++++++++-------- libethereum/Dagger.cpp | 2 +- libethereum/Dagger.h | 2 +- libethereum/RLP.h | 40 ++++++++++++++ libethereum/State.cpp | 22 ++++---- libethereum/State.h | 6 +-- libethereum/Transaction.cpp | 22 ++++---- libethereum/Transaction.h | 8 +-- libethereum/TransactionQueue.cpp | 2 +- libethereum/TransactionQueue.h | 6 +-- libethereum/Trie.cpp | 16 +++--- libethereum/Trie.h | 6 +-- test/main.cpp | 4 +- 20 files changed, 192 insertions(+), 105 deletions(-) diff --git a/libethereum/AddressState.cpp b/libethereum/AddressState.cpp index 1e0408c13..b54836956 100644 --- a/libethereum/AddressState.cpp +++ b/libethereum/AddressState.cpp @@ -24,7 +24,7 @@ using namespace std; using namespace eth; -u256 AddressState::memoryHash() const +h256 AddressState::memoryHash() const { return hash256(m_memory); } diff --git a/libethereum/AddressState.h b/libethereum/AddressState.h index b8c826227..c84a6ac40 100644 --- a/libethereum/AddressState.h +++ b/libethereum/AddressState.h @@ -46,14 +46,14 @@ public: std::map& memory() { assert(m_type == AddressType::Contract); return m_memory; } std::map const& memory() const { assert(m_type == AddressType::Contract); return m_memory; } - u256 memoryHash() const; + h256 memoryHash() const; std::string toString() const { if (m_type == AddressType::Normal) return asString(rlpList(m_balance, toCompactBigEndianString(m_nonce))); if (m_type == AddressType::Contract) - return asString(rlpList(m_balance, toCompactBigEndianString(m_nonce), toCompactBigEndianString(memoryHash()))); + return asString(rlpList(m_balance, toCompactBigEndianString(m_nonce), memoryHash())); return ""; } diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index 26934beef..794ecdec6 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -43,11 +43,11 @@ BlockChain::~BlockChain() { } -u256s BlockChain::blockChain(u256Set const& _earlyExit) const +h256s BlockChain::blockChain(h256Set const& _earlyExit) const { // TODO: return the current valid block chain from most recent to genesis. // TODO: arguments for specifying a set of early-ends - u256s ret; + h256s ret; ret.reserve(m_details[m_lastBlockHash].number + 1); auto i = m_lastBlockHash; for (; i != m_genesisHash && !_earlyExit.count(i); i = m_details[i].parent) @@ -122,7 +122,7 @@ void BlockChain::import(bytes const& _block) } } -bytesConstRef BlockChain::block(u256 _hash) const +bytesConstRef BlockChain::block(h256 _hash) const { if (_hash == m_genesisHash) return &m_genesisBlock; @@ -131,7 +131,7 @@ bytesConstRef BlockChain::block(u256 _hash) const return bytesConstRef(&m_cache[_hash]); } -BlockDetails const& BlockChain::details(u256 _h) const +BlockDetails const& BlockChain::details(h256 _h) const { auto it = m_details.find(_h); if (it == m_details.end()) diff --git a/libethereum/BlockChain.h b/libethereum/BlockChain.h index 6ed49eba1..3f72d68ac 100644 --- a/libethereum/BlockChain.h +++ b/libethereum/BlockChain.h @@ -32,10 +32,10 @@ struct BlockDetails { uint number; u256 totalDifficulty; - u256 parent; + h256 parent; }; -static const BlockDetails NullBlockDetails({0, 0, 0}); +static const BlockDetails NullBlockDetails({0, 0, h256()}); /** * @brief Implements the blockchain database. All data this gives is disk-backed. @@ -57,29 +57,29 @@ public: void import(bytes const& _block); /// Get the full block chain, according to the GHOST algo and the blocks available in the db. - u256s blockChain(u256Set const& _earlyExit) const; + h256s blockChain(h256Set const& _earlyExit) const; /// Get the number of the last block of the longest chain. - BlockDetails const& details(u256 _hash) const; + BlockDetails const& details(h256 _hash) const; /// Get a given block (RLP format). - bytesConstRef block(u256 _hash) const; + bytesConstRef block(h256 _hash) const; /// Get a given block (RLP format). - u256 currentHash() const { return m_lastBlockHash; } + h256 currentHash() const { return m_lastBlockHash; } private: /// Get fully populated from disk DB. - mutable std::map m_details; - mutable std::multimap m_children; + mutable std::map m_details; + mutable std::multimap m_children; - mutable std::map m_cache; + mutable std::map m_cache; ldb::DB* m_db; /// Hash of the last (valid) block on the longest chain. - u256 m_lastBlockHash; - u256 m_genesisHash; + h256 m_lastBlockHash; + h256 m_genesisHash; bytes m_genesisBlock; ldb::ReadOptions m_readOptions; diff --git a/libethereum/BlockInfo.cpp b/libethereum/BlockInfo.cpp index 958e03a95..9c169cc77 100644 --- a/libethereum/BlockInfo.cpp +++ b/libethereum/BlockInfo.cpp @@ -48,7 +48,7 @@ bytes BlockInfo::createGenesisBlock() return block.out(); } -u256 BlockInfo::headerHashWithoutNonce() const +h256 BlockInfo::headerHashWithoutNonce() const { return sha3((RLPStream(7) << toBigEndianString(parentHash) << toBigEndianString(sha3Uncles) << coinbaseAddress << toBigEndianString(stateRoot) << toBigEndianString(sha3Transactions) << difficulty << timestamp).out()); } @@ -66,11 +66,11 @@ void BlockInfo::populate(bytesConstRef _block) { RLP header = root[0]; hash = eth::sha3(_block); - parentHash = header[0].toInt(); - sha3Uncles = header[1].toInt(); - coinbaseAddress = header[2].toInt(); - stateRoot = header[3].toInt(); - sha3Transactions = header[4].toInt(); + parentHash = header[0].toHash(); + sha3Uncles = header[1].toHash(); + coinbaseAddress = header[2].toHash
(); + stateRoot = header[3].toHash(); + sha3Transactions = header[4].toHash(); difficulty = header[5].toInt(); timestamp = header[6].toInt(); nonce = header[7].toInt(); diff --git a/libethereum/BlockInfo.h b/libethereum/BlockInfo.h index 97e4dfc9f..bd6269335 100644 --- a/libethereum/BlockInfo.h +++ b/libethereum/BlockInfo.h @@ -22,6 +22,7 @@ #pragma once #include "Common.h" +#include "Transaction.h" namespace eth { @@ -29,12 +30,12 @@ namespace eth struct BlockInfo { public: - u256 hash; - u256 parentHash; - u256 sha3Uncles; - u160 coinbaseAddress; - u256 stateRoot; - u256 sha3Transactions; + h256 hash; + h256 parentHash; + h256 sha3Uncles; + Address coinbaseAddress; + h256 stateRoot; + h256 sha3Transactions; u256 difficulty; u256 timestamp; u256 nonce; @@ -54,7 +55,7 @@ public: u256 calculateDifficulty(BlockInfo const& _bi) const; /// No-nonce sha3 of the header only. - u256 headerHashWithoutNonce() const; + h256 headerHashWithoutNonce() const; static bytes createGenesisBlock(); diff --git a/libethereum/Common.cpp b/libethereum/Common.cpp index a0d75849b..5804ea911 100644 --- a/libethereum/Common.cpp +++ b/libethereum/Common.cpp @@ -127,12 +127,9 @@ bytes eth::sha3Bytes(bytesConstRef _input) return ret; } -u256 eth::sha3(bytesConstRef _input) +h256 eth::sha3(bytesConstRef _input) { - uint8_t buf[32]; - sha3(_input, bytesRef(&buf[0], 32)); - u256 ret = 0; - for (unsigned i = 0; i < 32; ++i) - ret = (ret << 8) | buf[i]; + h256 ret; + sha3(_input, bytesRef(&ret[0], 32)); return ret; } diff --git a/libethereum/Common.h b/libethereum/Common.h index 7ed6bd3ba..7e10bcdcc 100644 --- a/libethereum/Common.h +++ b/libethereum/Common.h @@ -23,6 +23,7 @@ #pragma once +#include #include #include #include @@ -55,6 +56,45 @@ using u160s = std::vector; using u256Set = std::set; using u160Set = std::set; +template inline void toBigEndian(_T _val, _Out& o_out); +template inline _T fromBigEndian(_In const& _bytes); + +template +class FixedHash +{ + using Arith = boost::multiprecision::number>; + +public: + static const unsigned size = _N; + + FixedHash() { m_data.fill(0); } + FixedHash(Arith const& _arith) { toBigEndian(_arith, m_data); } + + operator Arith() const { return fromBigEndian(m_data); } + + operator bool() const { return ((Arith)*this) != 0; } + + bool operator==(FixedHash const& _c) const { return m_data == _c.m_data; } + bool operator!=(FixedHash const& _c) const { return m_data != _c.m_data; } + bool operator<(FixedHash const& _c) const { return m_data < _c.m_data; } + + byte& operator[](unsigned _i) { return m_data[_i]; } + byte operator[](unsigned _i) const { return m_data[_i]; } + + byte* data() { return m_data.data(); } + byte const* data() const { return m_data.data(); } + +private: + std::array m_data; +}; + +using h256 = FixedHash<32>; +using h160 = FixedHash<20>; +using h256s = std::vector; +using h160s = std::vector; +using h256Set = std::set; +using h160Set = std::set; + // Map types. using StringMap = std::map; using u256Map = std::map; @@ -194,28 +234,40 @@ uint commonPrefix(_T const& _t, _U const& _u) return s; } -/// Convert the given value into u160 (160-bit unsigned integer) by taking the lowest order 160-bits and discarding the rest. -template -inline u160 low160(_T const& _t) +/// Convert the given value into h160 (160-bit unsigned integer) using the right 20 bytes. +inline h160 right160(h256 const& _t) +{ + h160 ret; + memcpy(ret.data(), _t.data() + 10, 20); + return ret; +} + +/// Convert the given value into h160 (160-bit unsigned integer) using the left 20 bytes. +inline h160 left160(h256 const& _t) { - return (u160)(_t & ((((_T)1) << 160) - 1)); + h160 ret; + memcpy(&ret[0], _t.data(), 20); + return ret; } /// Convert the given value into u160 (160-bit unsigned integer) by taking the lowest order 160-bits and discarding the rest. -template -inline u160 high160(_T const& _t) +inline u160 low160(u256 const& _t) { - return (u160)(_t >> 96); + return (u160)(_t & ((((u256)1) << 160) - 1)); } -/// Convert the given value safely into u160 (160-bit unsigned integer). -/// @note Currently unsafe. -template -inline u160 as160(_T const& _t) +inline u160 low160(bigint const& _t) +{ + return (u160)(_t & ((((bigint)1) << 160) - 1)); +} + +/// Convert the given value into u160 (160-bit unsigned integer) by taking the lowest order 160-bits and discarding the rest. +inline u160 high160(u256 const& _t) { - return low160(_t); + return (u160)(_t >> 96); } + /// Concatenate two vectors of elements. _T must be POD. template inline std::vector<_T>& operator+=(std::vector::value, _T>::type>& _a, std::vector<_T> const& _b) @@ -235,18 +287,13 @@ inline std::vector<_T> operator+(std::vector bool operator==(FixedHash<_N> const& _h) const { return isString() && toHash<_N>() == _h; } + template bool operator!=(FixedHash<_N> const& _s) const { return isString() && toHash<_N>() != _s; } bool operator==(uint const& _i) const { return (isInt() || isString()) && toSlimInt() == _i; } bool operator!=(uint const& _i) const { return (isInt() || isString()) && toSlimInt() != _i; } bool operator==(u256 const& _i) const { return (isInt() || isString()) && toFatInt() == _i; } @@ -159,6 +161,7 @@ public: explicit operator uint() const { return toSlimInt(); } explicit operator u256() const { return toFatInt(); } explicit operator bigint() const { return toBigInt(); } + template explicit operator FixedHash<_N>() const { return toHash<_N>(); } /// Converts to string. @returns the empty string if not a string. std::string toString() const { if (!isString()) return std::string(); return payload().cropped(0, items()).toString(); } @@ -206,6 +209,21 @@ public: return ret; } + template _N toHash(int _flags = Strict) const + { + if (!isString() || (items() >= _N::size && (_flags & FailIfTooBig))) + if (_flags & ThrowOnFail) + throw BadCast(); + else + return _N(); + else{} + + _N ret; + unsigned s = std::min(_N::size, items()); + memcpy(ret.data() + _N::size - s, payload().data(), s); + return ret; + } + /// Converts to eth::uint. @see toInt() uint toSlimInt(int _flags = Strict) const { return toInt(_flags); } @@ -275,6 +293,8 @@ public: RLPStream& append(uint _s); RLPStream& append(u160 _s); RLPStream& append(u256 _s); + RLPStream& append(h160 _s, bool _compact = false) { return appendFixed(_s, _compact); } + RLPStream& append(h256 _s, bool _compact = false) { return appendFixed(_s, _compact); } RLPStream& append(bigint _s); RLPStream& append(std::string const& _s); RLPStream& appendList(uint _count); @@ -285,6 +305,8 @@ public: RLPStream& operator<<(uint _i) { return append(_i); } RLPStream& operator<<(u160 _i) { return append(_i); } RLPStream& operator<<(u256 _i) { return append(_i); } + RLPStream& operator<<(h160 _i) { return append(_i); } + RLPStream& operator<<(h256 _i) { return append(_i); } RLPStream& operator<<(bigint _i) { return append(_i); } RLPStream& operator<<(char const* _s) { return append(std::string(_s)); } RLPStream& operator<<(std::string const& _s) { return append(_s); } @@ -307,6 +329,24 @@ private: *(b--) = (byte)_i; } + template + RLPStream& appendFixed(FixedHash<_N> const& _s, bool _compact) + { + uint s = _N; + byte const* d = _s.data(); + if (_compact) + for (unsigned i = 0; i < _N && !*d; ++i, --s, ++d) {} + + if (s < 0x38) + m_out.push_back(s | 0x40); + else + pushCount(s, 0x40); + uint os = m_out.size(); + m_out.resize(os + s); + memcpy(m_out.data() + os, d, s); + return *this; + } + /// Determine bytes required to encode the given integer value. @returns 0 if @a _i is zero. template static uint bytesRequired(_T _i) { diff --git a/libethereum/State.cpp b/libethereum/State.cpp index 7f328731e..7188b60ee 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -51,7 +51,7 @@ void State::sync(BlockChain const& _bc) sync(_bc, _bc.currentHash()); } -void State::sync(BlockChain const& _bc, u256 _block) +void State::sync(BlockChain const& _bc, h256 _block) { // BLOCK BlockInfo bi; @@ -88,7 +88,7 @@ void State::sync(BlockChain const& _bc, u256 _block) // New blocks available, or we've switched to a different branch. All change. // Find most recent state dump and replay what's left. // (Most recent state dump might end up being genesis.) - std::vector l = _bc.blockChain(u256Set()); + std::vector l = _bc.blockChain(h256Set()); if (l.back() == BlockInfo::genesis().hash) { @@ -165,10 +165,10 @@ void State::playback(bytesConstRef _block) } } -u256 State::rootHash() const +h256 State::rootHash() const { // TODO. - return 0; + return h256(); } bool State::mine(uint _msTimeout) const @@ -466,10 +466,10 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256 _ stack.pop_back(); break; case Instruction::MYADDRESS: - stack.push_back(_myAddress); + stack.push_back((u160)_myAddress); break; case Instruction::TXSENDER: - stack.push_back(_txSender); + stack.push_back((u160)_txSender); break; case Instruction::TXVALUE: stack.push_back(_txValue); @@ -488,7 +488,7 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256 _ stack.push_back(m_previousBlock.hash); break; case Instruction::BLK_COINBASE: - stack.push_back(m_currentBlock.coinbaseAddress); + stack.push_back((u160)m_currentBlock.coinbaseAddress); break; case Instruction::BLK_TIMESTAMP: stack.push_back(m_currentBlock.timestamp); @@ -745,14 +745,14 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256 _ require(2); auto memoryAddress = stack.back(); stack.pop_back(); - Address contractAddress = as160(stack.back()); + Address contractAddress = left160(stack.back()); stack.back() = contractMemory(contractAddress, memoryAddress); break; } case Instruction::BALANCE: { require(1); - stack.back() = balance(as160(stack.back())); + stack.back() = balance(low160(stack.back())); break; } case Instruction::MKTX: @@ -760,7 +760,7 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256 _ require(4); Transaction t; - t.receiveAddress = as160(stack.back()); + t.receiveAddress = left160(stack.back()); stack.pop_back(); t.value = stack.back(); stack.pop_back(); @@ -786,7 +786,7 @@ void State::execute(Address _myAddress, Address _txSender, u256 _txValue, u256 _ case Instruction::SUICIDE: { require(1); - Address dest = as160(stack.back()); + Address dest = left160(stack.back()); u256 minusVoidFee = m_current[_myAddress].memory().size() * c_memoryFee; addBalance(dest, balance(_myAddress) + minusVoidFee); m_current.erase(_myAddress); diff --git a/libethereum/State.h b/libethereum/State.h index 886fabe9a..9e43cad0f 100644 --- a/libethereum/State.h +++ b/libethereum/State.h @@ -65,7 +65,7 @@ public: void sync(BlockChain const& _bc); /// Sync with the block chain, but rather than synching to the latest block sync to the given block. - void sync(BlockChain const& _bc, u256 _blockHash); + void sync(BlockChain const& _bc, h256 _blockHash); /// Sync our transactions, killing those from the queue that we have and assimilating those that we don't. void sync(TransactionQueue& _tq); @@ -103,7 +103,7 @@ public: u256 transactionsFrom(Address _address) const; /// The hash of the root of our state tree. - u256 rootHash() const; + h256 rootHash() const; private: /// Fee-adder on destruction RAII class. @@ -127,7 +127,7 @@ private: // TODO: std::hash
and then move to unordered_map. // Will need to sort on hash construction. std::map m_current; ///< The current state. We work with a C++ hash map rather than a Trie. - std::map m_transactions; ///< The current list of transactions that we've included in the state. + std::map m_transactions; ///< The current list of transactions that we've included in the state. BlockInfo m_previousBlock; ///< The previous block's information. BlockInfo m_currentBlock; ///< The current block's information. diff --git a/libethereum/Transaction.cpp b/libethereum/Transaction.cpp index 5659d42f9..23e84cb83 100644 --- a/libethereum/Transaction.cpp +++ b/libethereum/Transaction.cpp @@ -29,7 +29,7 @@ Transaction::Transaction(bytesConstRef _rlpData) { RLP rlp(_rlpData); nonce = rlp[0].toInt(RLP::StrictlyInt); - receiveAddress = rlp[1].toInt(RLP::StrictlyString); + receiveAddress = rlp[1].toHash
(); value = rlp[2].toInt(RLP::StrictlyInt); fee = rlp[3].toInt(RLP::StrictlyInt); data.reserve(rlp[4].itemCountStrict()); @@ -44,22 +44,23 @@ Address Transaction::sender() const bytes sig = toBigEndian(vrs.r) + toBigEndian(vrs.s); assert(sig.size() == 64); - bytes msg = sha3Bytes(false); + h256 msg = sha3(false); byte pubkey[65]; int pubkeylen = 65; - if (!secp256k1_ecdsa_recover_compact(msg.data(), msg.size(), sig.data(), pubkey, &pubkeylen, 0, (int)vrs.v - 27)) + if (!secp256k1_ecdsa_recover_compact(msg.data(), 32, sig.data(), pubkey, &pubkeylen, 0, (int)vrs.v - 27)) throw InvalidSignature(); - return low160(eth::sha3(bytesConstRef(&(pubkey[1]), 64))); + // TODO: check right160 is correct and shouldn't be left160. + return right160(eth::sha3(bytesConstRef(&(pubkey[1]), 64))); } void Transaction::sign(PrivateKey _priv) { int v = 0; - u256 msg = sha3(false); + h256 msg = sha3(false); byte sig[64]; - if (!secp256k1_ecdsa_sign_compact(toBigEndian(msg).data(), 32, sig, toBigEndian(_priv).data(), toBigEndian(kFromMessage(msg, _priv)).data(), &v)) + if (!secp256k1_ecdsa_sign_compact(msg.data(), 32, sig, _priv.data(), kFromMessage(msg, _priv).data(), &v)) throw InvalidSignature(); vrs.v = v + 27; @@ -70,12 +71,13 @@ void Transaction::sign(PrivateKey _priv) void Transaction::fillStream(RLPStream& _s, bool _sig) const { _s.appendList(_sig ? 8 : 5); - _s << nonce << toCompactBigEndianString(receiveAddress) << value << fee << data; + _s << nonce << receiveAddress << value << fee << data; if (_sig) - _s << toCompactBigEndianString(vrs.v) << toCompactBigEndianString(vrs.r) << toCompactBigEndianString(vrs.s); + _s << vrs.v << vrs.r << vrs.s; } -u256 Transaction::kFromMessage(u256 _msg, u256 _priv) +// If the h256 return is an integer, store it in bigendian (i.e. u256 ret; ... return (h256)ret; ) +h256 Transaction::kFromMessage(h256 _msg, h256 _priv) { // TODO! /* @@ -89,6 +91,6 @@ u256 Transaction::kFromMessage(u256 _msg, u256 _priv) v = hmac.new(k, v, hashlib.sha256).digest() return decode(hmac.new(k, v, hashlib.sha256).digest(),256) */ - return 0; + return h256(); } diff --git a/libethereum/Transaction.h b/libethereum/Transaction.h index c9989fb22..183d95d2d 100644 --- a/libethereum/Transaction.h +++ b/libethereum/Transaction.h @@ -27,8 +27,8 @@ namespace eth { -using PrivateKey = u256; -using Address = u160; +using PrivateKey = h256; +using Address = h160; struct Signature { @@ -54,12 +54,12 @@ struct Transaction Address sender() const; void sign(PrivateKey _priv); - static u256 kFromMessage(u256 _msg, u256 _priv); + static h256 kFromMessage(h256 _msg, h256 _priv); 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()); } - u256 sha3(bool _sig = true) const { RLPStream s; fillStream(s, _sig); return eth::sha3(s.out()); } + h256 sha3(bool _sig = true) const { RLPStream s; fillStream(s, _sig); return eth::sha3(s.out()); } bytes sha3Bytes(bool _sig = true) const { RLPStream s; fillStream(s, _sig); return eth::sha3Bytes(s.out()); } }; diff --git a/libethereum/TransactionQueue.cpp b/libethereum/TransactionQueue.cpp index 5afc7a5e6..77abec707 100644 --- a/libethereum/TransactionQueue.cpp +++ b/libethereum/TransactionQueue.cpp @@ -27,7 +27,7 @@ using namespace eth; void TransactionQueue::import(bytes const& _block) { // Check if we already know this transaction. - u256 h = sha3(_block); + h256 h = sha3(_block); if (m_data.count(h)) return; diff --git a/libethereum/TransactionQueue.h b/libethereum/TransactionQueue.h index 1c5b9300a..1ab2cec41 100644 --- a/libethereum/TransactionQueue.h +++ b/libethereum/TransactionQueue.h @@ -38,12 +38,12 @@ public: void import(bytes const& _block); - void drop(u256 _txHash) { m_data.erase(_txHash); } + void drop(h256 _txHash) { m_data.erase(_txHash); } - std::map const& transactions() const { return m_data; } + std::map const& transactions() const { return m_data; } private: - std::map m_data; ///< the queue. + std::map m_data; ///< the queue. }; } diff --git a/libethereum/Trie.cpp b/libethereum/Trie.cpp index ceb37ae78..d64ca3d30 100644 --- a/libethereum/Trie.cpp +++ b/libethereum/Trie.cpp @@ -183,11 +183,11 @@ void hash256aux(HexMap const& _s, HexMap::const_iterator _begin, HexMap::const_i #if ENABLE_DEBUG_PRINT cerr << "[HASH: " << dec << rlp.out().size() << " >= 32]" << endl; #endif - _rlp << toCompactBigEndianString(sha3(rlp.out())); + _rlp << sha3(rlp.out()); } } -u256 hash256(StringMap const& _s) +h256 hash256(StringMap const& _s) { // build patricia tree. if (_s.empty()) @@ -200,7 +200,7 @@ u256 hash256(StringMap const& _s) return sha3(s.out()); } -u256 hash256(u256Map const& _s) +h256 hash256(u256Map const& _s) { // build patricia tree. if (_s.empty()) @@ -229,9 +229,9 @@ public: #endif /// 256-bit hash of the node - this is a SHA-3/256 hash of the RLP of the node. - u256 hash256() const { RLPStream s; makeRLP(s); return eth::sha3(s.out()); } + h256 hash256() const { RLPStream s; makeRLP(s); return eth::sha3(s.out()); } bytes rlp() const { RLPStream s; makeRLP(s); return s.out(); } - void mark() { m_hash256 = 0; } + void mark() { m_hash256 = h256(); } protected: virtual void makeRLP(RLPStream& _intoStream) const = 0; @@ -243,7 +243,7 @@ protected: static TrieNode* newBranch(bytesConstRef _k1, std::string const& _v1, bytesConstRef _k2, std::string const& _v2); private: - mutable u256 m_hash256 = 0; + mutable h256 m_hash256; }; static const std::string c_nullString; @@ -374,7 +374,7 @@ void TrieNode::putRLP(RLPStream& _parentStream) const if (s.out().size() < 32) _parentStream.APPEND_CHILD(s.out()); else - _parentStream << toCompactBigEndianString(eth::sha3(s.out())); + _parentStream << eth::sha3(s.out()); } void TrieBranchNode::makeRLP(RLPStream& _intoStream) const @@ -609,7 +609,7 @@ Trie::~Trie() delete m_root; } -u256 Trie::hash256() const +h256 Trie::hash256() const { return m_root ? m_root->hash256() : eth::sha3(RLPNull); } diff --git a/libethereum/Trie.h b/libethereum/Trie.h index b1e05e248..1a6bdc73b 100644 --- a/libethereum/Trie.h +++ b/libethereum/Trie.h @@ -27,8 +27,8 @@ namespace eth { -u256 hash256(StringMap const& _s); -u256 hash256(u256Map const& _s); +h256 hash256(StringMap const& _s); +h256 hash256(u256Map const& _s); std::string hexPrefixEncode(bytes const& _hexVector, bool _terminated = false, int _begin = 0, int _end = -1); class TrieNode; @@ -42,7 +42,7 @@ public: Trie(): m_root(nullptr) {} ~Trie(); - u256 hash256() const; + h256 hash256() const; bytes rlp() const; void debugPrint(); diff --git a/test/main.cpp b/test/main.cpp index ecf3645ce..3587a1ee6 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -38,7 +38,7 @@ int main() { // Test dagger { - Dagger d(0); + Dagger d((h256)0); auto s = steady_clock::now(); cout << hex << d.eval(0); cout << " " << dec << duration_cast(steady_clock::now() - s).count() << " ms" << endl; @@ -46,7 +46,7 @@ int main() cout << " " << dec << duration_cast(steady_clock::now() - s).count() << " ms" << endl; } { - Dagger d(1); + Dagger d((h256)1); auto s = steady_clock::now(); cout << hex << d.eval(0); cout << " " << dec << duration_cast(steady_clock::now() - s).count() << " ms" << endl;