Browse Source

merkle tree hash works.

cl-refactor
Gav Wood 11 years ago
parent
commit
928ab1dcee
  1. 19
      PatriciaTree.h
  2. 14
      RLP.h
  3. 10
      VirtualMachine.cpp
  4. 4
      main.cpp
  5. 1
      sha256.h

19
PatriciaTree.h

@ -35,7 +35,7 @@ inline std::string hexPrefixEncode(bytes const& _hexVector, bool _terminated = f
std::string ret(1, ((termed ? 2 : 0) | (odd ? 1 : 0)) * 16);
if (odd)
{
ret[0] |= _hexVector[0];
ret[0] |= _hexVector[begin];
++begin;
}
for (uint i = begin; i < end; i += 2)
@ -55,6 +55,15 @@ inline bytes toHex(std::string const& _s)
return ret;
}
inline std::string toBigEndianString(u256 _val)
{
std::string ret;
ret.resize(32);
for (int i = 0; i <32; ++i, _val >>= 8)
ret[31 - i] = (char)(uint8_t)_val;
return ret;
}
inline u256 hash256aux(HexMap const& _s, HexMap::const_iterator _begin, HexMap::const_iterator _end, unsigned _preLen)
{
RLPStream rlp;
@ -83,7 +92,7 @@ inline u256 hash256aux(HexMap const& _s, HexMap::const_iterator _begin, HexMap::
if (sharedPre > _preLen)
{
// if they all have the same next nibble, we also want a pair.
rlp << RLPList(2) << hexPrefixEncode(_begin->first, false, _preLen, sharedPre) << hash256aux(_s, _begin, _end, sharedPre);
rlp << RLPList(2) << hexPrefixEncode(_begin->first, false, _preLen, sharedPre) << toBigEndianString(hash256aux(_s, _begin, _end, sharedPre));
}
else
{
@ -99,14 +108,16 @@ inline u256 hash256aux(HexMap const& _s, HexMap::const_iterator _begin, HexMap::
if (b == n)
rlp << "";
else
rlp << hash256aux(_s, b, n, _preLen + 1);
rlp << toBigEndianString(hash256aux(_s, b, n, _preLen + 1));
b = n;
}
if (_preLen == _begin->first.size())
rlp << _begin->second;
else
rlp << "";
}
}
std::cout << std::hex << sha256(rlp.out()) << ": " << RLP(rlp.out()) << std::endl;
// std::cout << std::hex << sha256(rlp.out()) << ": " << asHex(rlp.out()) << ": " << RLP(rlp.out()) << std::endl;
return sha256(rlp.out());
}

14
RLP.h

@ -295,21 +295,21 @@ private:
}
inline std::string escaped(std::string const& _s)
inline std::string escaped(std::string const& _s, bool _all = true)
{
std::string ret;
ret.reserve(_s.size());
ret.push_back('"');
for (auto i: _s)
if (i == '"')
if (i == '"' && !_all)
ret += "\\\"";
else if (i == '\\')
else if (i == '\\' && !_all)
ret += "\\\\";
else if (i < ' ' || i > 127)
else if (i < ' ' || i > 127 || _all)
{
ret += "\\x";
ret.push_back("0123456789abcdef"[i / 16]);
ret.push_back("0123456789abcdef"[i % 16]);
ret.push_back("0123456789abcdef"[(uint8_t)i / 16]);
ret.push_back("0123456789abcdef"[(uint8_t)i % 16]);
}
else
ret.push_back(i);
@ -324,7 +324,7 @@ inline std::ostream& operator<<(std::ostream& _out, eth::RLP _d)
else if (_d.isInt())
_out << std::showbase << std::hex << std::nouppercase << _d.toBigInt();
else if (_d.isString())
_out << escaped(_d.toString());
_out << escaped(_d.toString(), true);
else if (_d.isList())
{
_out << "[";

10
VirtualMachine.cpp

@ -1,4 +1,3 @@
#include "uint256_t.h"
#include "VirtualMachine.h"
using namespace std;
using namespace eth;
@ -53,8 +52,17 @@ void VirtualMachine::go()
(s256&)m_stack[m_stack.size() - 2] = (s256&)m_stack.back() / (s256&)m_stack[m_stack.size() - 2];
m_stack.pop_back();
case Instruction::MOD:
require(2);
m_stack[m_stack.size() - 2] = m_stack.back() % m_stack[m_stack.size() - 2];
m_stack.pop_back();
case Instruction::SMOD:
require(2);
(s256&)m_stack[m_stack.size() - 2] = (s256&)m_stack.back() % (s256&)m_stack[m_stack.size() - 2];
m_stack.pop_back();
case Instruction::EXP:
require(2);
// (s256&)m_stack[m_stack.size() - 2] = pow(m_stack.back(), m_stack[m_stack.size() - 2]);
// m_stack.pop_back();
case Instruction::NEG:
case Instruction::LT:
case Instruction::LE:

4
main.cpp

@ -34,8 +34,8 @@ template <class ... _Ts> std::string rlpList(_Ts ... _ts)
int main()
{
cout << hex << hash256({{"dog", "puppy"}}) << endl;
cout << hex << hash256({{"dog", "puppy"}, {"horse", "stallion"}}) << endl;
cout << hex << hash256({{"dog", "puppy"}, {"doge", "coin"}}) << endl;
cout << hex << hash256({{"dog", "puppy"}, {"doe", "reindeer"}}) << endl;
cout << hex << hash256({{"doe", "reindeer"}, {"dog", "puppy"}, {"dogglesworth", "cat"}}) << endl;
cout << hex << hash256({{"dog", "puppy"}, {"horse", "stallion"}, {"do", "verb"}, {"doge", "coin"}}) << endl;
// int of value 15

1
sha256.h

@ -2,7 +2,6 @@
#include <string>
#include "Common.h"
#include "uint256_t.h"
namespace eth
{

Loading…
Cancel
Save