Browse Source

Moved to endian-neutral 256/160-bit datatypes.

cl-refactor
Gav Wood 11 years ago
parent
commit
140fc42e09
  1. 2
      libethereum/AddressState.cpp
  2. 4
      libethereum/AddressState.h
  3. 8
      libethereum/BlockChain.cpp
  4. 22
      libethereum/BlockChain.h
  5. 12
      libethereum/BlockInfo.cpp
  6. 15
      libethereum/BlockInfo.h
  7. 9
      libethereum/Common.cpp
  8. 89
      libethereum/Common.h
  9. 2
      libethereum/Dagger.cpp
  10. 2
      libethereum/Dagger.h
  11. 40
      libethereum/RLP.h
  12. 22
      libethereum/State.cpp
  13. 6
      libethereum/State.h
  14. 22
      libethereum/Transaction.cpp
  15. 8
      libethereum/Transaction.h
  16. 2
      libethereum/TransactionQueue.cpp
  17. 6
      libethereum/TransactionQueue.h
  18. 16
      libethereum/Trie.cpp
  19. 6
      libethereum/Trie.h
  20. 4
      test/main.cpp

2
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);
}

4
libethereum/AddressState.h

@ -46,14 +46,14 @@ public:
std::map<u256, u256>& memory() { assert(m_type == AddressType::Contract); return m_memory; }
std::map<u256, u256> 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 "";
}

8
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())

22
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<u256, BlockDetails> m_details;
mutable std::multimap<u256, u256> m_children;
mutable std::map<h256, BlockDetails> m_details;
mutable std::multimap<h256, h256> m_children;
mutable std::map<u256, std::string> m_cache;
mutable std::map<h256, std::string> 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;

12
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<u256>();
sha3Uncles = header[1].toInt<u256>();
coinbaseAddress = header[2].toInt<u160>();
stateRoot = header[3].toInt<u256>();
sha3Transactions = header[4].toInt<u256>();
parentHash = header[0].toHash<h256>();
sha3Uncles = header[1].toHash<h256>();
coinbaseAddress = header[2].toHash<Address>();
stateRoot = header[3].toHash<h256>();
sha3Transactions = header[4].toHash<h256>();
difficulty = header[5].toInt<u256>();
timestamp = header[6].toInt<u256>();
nonce = header[7].toInt<u256>();

15
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();

9
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;
}

89
libethereum/Common.h

@ -23,6 +23,7 @@
#pragma once
#include <array>
#include <map>
#include <set>
#include <string>
@ -55,6 +56,45 @@ using u160s = std::vector<u160>;
using u256Set = std::set<u256>;
using u160Set = std::set<u160>;
template <class _T, class _Out> inline void toBigEndian(_T _val, _Out& o_out);
template <class _T, class _In> inline _T fromBigEndian(_In const& _bytes);
template <unsigned _N>
class FixedHash
{
using Arith = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<_N * 8, _N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
public:
static const unsigned size = _N;
FixedHash() { m_data.fill(0); }
FixedHash(Arith const& _arith) { toBigEndian(_arith, m_data); }
operator Arith() const { return fromBigEndian<Arith>(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<byte, _N> m_data;
};
using h256 = FixedHash<32>;
using h160 = FixedHash<20>;
using h256s = std::vector<h256>;
using h160s = std::vector<h160>;
using h256Set = std::set<h256>;
using h160Set = std::set<h160>;
// Map types.
using StringMap = std::map<std::string, std::string>;
using u256Map = std::map<u256, u256>;
@ -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 <class _T>
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 <class _T>
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 <class _T>
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 <class _T>
inline std::vector<_T>& operator+=(std::vector<typename std::enable_if<std::is_pod<_T>::value, _T>::type>& _a, std::vector<_T> const& _b)
@ -235,18 +287,13 @@ inline std::vector<_T> operator+(std::vector<typename std::enable_if<std::is_pod
return ret += _b;
}
/// Calculate RIPEMD-160 hash of the given message.
u160 ripemd160(bytesConstRef _message);
/// SHA-3 convenience routines.
void sha3(bytesConstRef _input, bytesRef _output);
std::string sha3(std::string const& _input, bool _hex);
bytes sha3Bytes(bytesConstRef _input);
bytes sha3Bytes(bytesConstRef _input);
inline bytes sha3Bytes(std::string const& _input) { return sha3Bytes((std::string*)&_input); }
inline bytes sha3Bytes(bytes const& _input) { return sha3Bytes((bytes*)&_input); }
u256 sha3(bytesConstRef _input);
inline u256 sha3(bytes const& _input) { return sha3(bytesConstRef((bytes*)&_input)); }
h256 sha3(bytesConstRef _input);
inline h256 sha3(bytes const& _input) { return sha3(bytesConstRef((bytes*)&_input)); }
}

2
libethereum/Dagger.cpp

@ -9,7 +9,7 @@ using namespace std;
using namespace std::chrono;
using namespace eth;
Dagger::Dagger(u256 _hash): m_hash(_hash)
Dagger::Dagger(h256 _hash): m_hash(_hash)
{
}

2
libethereum/Dagger.h

@ -9,7 +9,7 @@ namespace eth
class Dagger
{
public:
Dagger(u256 _hash);
Dagger(h256 _hash);
~Dagger();
u256 node(uint_fast32_t _L, uint_fast32_t _i) const;

40
libethereum/RLP.h

@ -110,6 +110,8 @@ public:
bool operator!=(char const* _s) const { return isString() && toString() != _s; }
bool operator==(std::string const& _s) const { return isString() && toString() == _s; }
bool operator!=(std::string const& _s) const { return isString() && toString() != _s; }
template <unsigned _N> bool operator==(FixedHash<_N> const& _h) const { return isString() && toHash<_N>() == _h; }
template <unsigned _N> 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 <unsigned _N> 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 <class _N> _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<unsigned>(_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<uint>(_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 <unsigned _N>
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 <class _T> static uint bytesRequired(_T _i)
{

22
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<u256> l = _bc.blockChain(u256Set());
std::vector<h256> 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);

6
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<Address> and then move to unordered_map.
// Will need to sort on hash construction.
std::map<Address, AddressState> m_current; ///< The current state. We work with a C++ hash map rather than a Trie.
std::map<u256, Transaction> m_transactions; ///< The current list of transactions that we've included in the state.
std::map<h256, Transaction> 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.

22
libethereum/Transaction.cpp

@ -29,7 +29,7 @@ Transaction::Transaction(bytesConstRef _rlpData)
{
RLP rlp(_rlpData);
nonce = rlp[0].toInt<u256>(RLP::StrictlyInt);
receiveAddress = rlp[1].toInt<u160>(RLP::StrictlyString);
receiveAddress = rlp[1].toHash<Address>();
value = rlp[2].toInt<u256>(RLP::StrictlyInt);
fee = rlp[3].toInt<u256>(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();
}

8
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()); }
};

2
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;

6
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<u256, bytes> const& transactions() const { return m_data; }
std::map<h256, bytes> const& transactions() const { return m_data; }
private:
std::map<u256, bytes> m_data; ///< the queue.
std::map<h256, bytes> m_data; ///< the queue.
};
}

16
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);
}

6
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();

4
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<milliseconds>(steady_clock::now() - s).count() << " ms" << endl;
@ -46,7 +46,7 @@ int main()
cout << " " << dec << duration_cast<milliseconds>(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<milliseconds>(steady_clock::now() - s).count() << " ms" << endl;

Loading…
Cancel
Save