@ -2,6 +2,7 @@
# include <map>
# include "RLP.h"
# include "sha256.h"
namespace eth
{
@ -24,137 +25,49 @@ using HexMap = std::map<bytes, std::string>;
* [ 1 , 2 , 3 , 4 , T ] 0x201234
*/
std : : string fromHex ( bytes const & _hexVector , bool _forceT erminated = false )
inline std : : string fromHex ( bytes const & _hexVector , bool _t erminated = false , int _begin = 0 , int _end = - 1 )
{
uint begin = 0 ;
uint end = _hexVector . size ( ) ;
bool termed = _forceT erminated ;
bool odd = _hexVector . size ( ) % 2 ;
uint begin = _begin ;
uint end = _end < 0 ? _ hexVector . size ( ) + 1 + _end : _end ;
bool termed = _t erminated ;
bool odd = ( end - begin ) % 2 ;
std : : string ret ( ( ( termed ? 2 : 0 ) | ( odd ? 1 : 0 ) ) * 16 , 1 ) ;
std : : string ret ( 1 , ( ( termed ? 2 : 0 ) | ( odd ? 1 : 0 ) ) * 16 ) ;
if ( odd )
{
ret [ 0 ] | = _hexVector [ 0 ] ;
begin = 1 ;
+ + begin ;
}
else if ( leadingZero )
for ( uint i = begin ; i < end ; i + = 2 )
ret + = _hexVector [ i ] * 16 + _hexVector [ i + 1 ] ;
return ret ;
}
template < class _T , class . . . _Ts > bytes encodeRLP ( _T _t , _Ts . . . _ts )
inline u256 hash256aux ( HexMap const & _s , HexMap : : const_iterator _begin , HexMap : : const_iterator _end , unsigned _preLen )
{
unsigned c = 0 ;
for ( auto i = _begin ; i ! = _end ; + + i , + + c ) { }
}
struct rlplist { rlplist ( uint _count ) : count ( _count ) { } uint count ; } ;
class RLPStream
{
public :
RLPStream ( ) { }
void append ( uint _s ) { appendNumeric ( _s ) ; }
void append ( u256 _s ) { appendNumeric ( _s ) ; }
void append ( bigint _s ) { appendNumeric ( _s ) ; }
void append ( std : : string const & _s )
{
if ( _s . size ( ) < 0x38 )
m_out . push_back ( _count | 0x40 ) ;
else
pushCount ( _count , 0x40 ) ;
uint os = m_out . size ( ) ;
m_out . resize ( m_out . size ( ) + _s . size ( ) ) ;
memcpy ( m_out . data ( ) + os , _s . data ( ) , _s . size ( ) ) ;
}
void appendList ( uint _count )
{
if ( _s . size ( ) < 0x38 )
m_out . push_back ( _count | 0x80 ) ;
else
pushCount ( _count , 0x80 ) ;
}
RLPStream operator < < ( uint _t ) { append ( _i ) ; }
RLPStream operator < < ( u256 _t ) { append ( _i ) ; }
RLPStream operator < < ( bigint _t ) { append ( _i ) ; }
RLPStream operator < < ( std : : string const & _s ) { append ( _s ) ; }
RLPStream operator < < ( rlplist _l ) { m_lists . push_back ( _l . count ) ; appendList ( _l . count ) ; }
private :
void appendNumeric ( uint _i )
assert ( c > 0 ) ;
RLPStream rlp ;
if ( c = = 1 )
{
if ( _i < 0x18 )
m_out . push_back ( _i ) ;
else
m_out . push_back ( bytesRequired ( _i ) + 0x17 ) ; // max 8 bytes.
// only one left - terminate with the pair.
rlp < < RLPList ( 2 ) < < fromHex ( _begin - > first , true , _preLen ) < < _begin - > second ;
}
void appendNumeric ( u256 _i )
else
{
if ( _i < 0x18 )
m_out . push_back ( _i ) ;
else
m_out . push_back ( bytesRequired ( _i ) + 0x17 ) ; // max 32 bytes.
}
// if they all have the same next nibble, we also want a pair.
void appendNumeric ( bigint _i )
{
if ( _i < 0x18 )
m_out . push_back ( _i ) ;
else
// otherwise enumerate all 16+1 entries.
for ( auto i = 0 ; i < 16 ; + + i )
{
uint br = bytesRequired ( _i ) ;
if ( br < = 32 )
m_out . push_back ( bytesRequired ( _i ) + 0x17 ) ; // max 32 bytes.
else
m_out . push_back ( 0x37 + bytesRequired ( br ) ) ;
}
for ( uint i = 0 ; i < )
m_out . push_back ( )
}
void pushCount ( uint _count , byte _base )
{
m_out . push_back ( bytesRequired ( _i ) + 0x17 ) ; // max 8 bytes.
}
template < class _T > static uint bytesRequired ( _T _i )
{
_i > > = 8 ;
uint i = 1 ;
for ( ; _i ! = 0 ; + + i , _i > > = 8 ) { }
return i ;
}
bytes m_out ;
} ;
template < > bytes encodeRLP ( _T _t )
{
}
u256 hash256aux ( HexMap const & _s , HexMap : : const_iterator _begin , HexMap : : const_iterator _end , unsigned _preLen )
{
unsigned c = 0 ;
for ( auto i = _begin ; i ! = _end ; + + i , + + c ) { }
assert ( c > 0 ) ;
if ( c = = 1 )
return sha256 ( encodeRLP ( ) ) ;
for ( auto i = 0 ; i < 16 ; + + i )
{
}
return sha256 ( rlp . out ( ) ) ;
}
bytes toHex ( std : : string const & _s )
inline bytes toHex ( std : : string const & _s )
{
std : : vector < uint8_t > ret ( _s . size ( ) * 2 + 1 ) ;
for ( auto i : _s )
@ -166,7 +79,7 @@ bytes toHex(std::string const& _s)
return ret ;
}
u256 hash256 ( StringMap const & _s )
inline u256 hash256 ( StringMap const & _s )
{
// build patricia tree.
if ( _s . empty ( ) )