 |
Ethereum
PoC-8
The C++ Implementation of Ethereum
|
Go to the documentation of this file.
33 static const byte c_rlpMaxLengthBytes = 8;
34 static const byte c_rlpDataImmLenStart = 0x80;
35 static const byte c_rlpListStart = 0xc0;
37 static const byte c_rlpDataImmLenCount = c_rlpListStart - c_rlpDataImmLenStart - c_rlpMaxLengthBytes;
38 static const byte c_rlpDataIndLenZero = c_rlpDataImmLenStart + c_rlpDataImmLenCount - 1;
39 static const byte c_rlpListImmLenCount = 256 - c_rlpListStart - c_rlpMaxLengthBytes;
40 static const byte c_rlpListIndLenZero = c_rlpListStart + c_rlpListImmLenCount - 1;
42 template <
class T>
struct Converter {
static T
convert(
RLP const&,
int) { BOOST_THROW_EXCEPTION(BadCast()); } };
83 explicit operator bool()
const {
return !
isNull(); }
89 bool isEmpty()
const {
return !
isNull() && (m_data[0] == c_rlpDataImmLenStart || m_data[0] == c_rlpListStart); }
92 bool isData()
const {
return !
isNull() && m_data[0] < c_rlpListStart; }
95 bool isList()
const {
return !
isNull() && m_data[0] >= c_rlpListStart; }
106 size_t sizeStrict()
const {
if (!
isData()) BOOST_THROW_EXCEPTION(BadCast());
return length(); }
115 bool operator==(
unsigned const& _i)
const {
return isInt() && toInt<unsigned>() == _i; }
116 bool operator!=(
unsigned const& _i)
const {
return isInt() && toInt<unsigned>() != _i; }
146 iterator(
RLP const& _parent,
bool _begin);
148 size_t m_remaining = 0;
158 template <
class T>
inline T
convert(
int _flags)
const;
161 explicit operator std::string()
const {
return toString(); }
163 explicit operator uint8_t()
const {
return toInt<uint8_t>(); }
164 explicit operator uint16_t()
const {
return toInt<uint16_t>(); }
165 explicit operator uint32_t()
const {
return toInt<uint32_t>(); }
166 explicit operator uint64_t()
const {
return toInt<uint64_t>(); }
167 explicit operator u160()
const {
return toInt<u160>(); }
168 explicit operator u256()
const {
return toInt<u256>(); }
169 explicit operator bigint()
const {
return toInt<bigint>(); }
170 template <
unsigned N>
explicit operator FixedHash<N>()
const {
return toHash<FixedHash<N>>(); }
171 template <
class T,
class U>
explicit operator std::pair<T, U>()
const {
return toPair<T, U>(); }
172 template <
class T>
explicit operator std::vector<T>()
const {
return toVector<T>(); }
173 template <
class T>
explicit operator std::set<T>()
const {
return toSet<T>(); }
174 template <
class T,
size_t N>
explicit operator std::array<T, N>()
const {
return toArray<T, N>(); }
192 for (
auto const& i: *
this)
193 ret.push_back(i.convert<T>(_flags));
196 BOOST_THROW_EXCEPTION(BadCast());
205 for (
auto const& i: *
this)
206 ret.insert(i.convert<T>(_flags));
208 BOOST_THROW_EXCEPTION(BadCast());
215 std::unordered_set<T> ret;
217 for (
auto const& i: *
this)
218 ret.insert(i.convert<T>(_flags));
220 BOOST_THROW_EXCEPTION(BadCast());
224 template <
class T,
class U>
231 BOOST_THROW_EXCEPTION(BadCast());
235 ret.first = (*this)[0].convert<T>(_flags);
236 ret.second = (*this)[1].convert<U>(_flags);
240 template <
class T,
size_t N>
246 BOOST_THROW_EXCEPTION(BadCast());
248 return std::array<T, N>();
250 std::array<T, N> ret;
251 for (
size_t i = 0; i < N; ++i)
252 ret[i] =
operator[](i).convert<T>(_flags);
257 template <
class _T =
unsigned> _T
toInt(
int _flags =
Strict)
const
263 BOOST_THROW_EXCEPTION(BadCast());
272 BOOST_THROW_EXCEPTION(BadCast());
277 return fromBigEndian<_T>(p);
282 int64_t i = toInt<int64_t>(_flags);
284 BOOST_THROW_EXCEPTION(BadCast());
296 BOOST_THROW_EXCEPTION(BadCast());
302 size_t s = std::min<size_t>(_N::size, l);
303 memcpy(ret.data() + _N::size - s, p.data(), s);
319 void requireGood()
const;
322 bool isSingleByte()
const {
return !
isNull() && m_data[0] < c_rlpDataImmLenStart; }
325 unsigned lengthSize()
const {
if (
isData() && m_data[0] > c_rlpDataIndLenZero)
return m_data[0] - c_rlpDataIndLenZero;
if (
isList() && m_data[0] > c_rlpListIndLenZero)
return m_data[0] - c_rlpListIndLenZero;
return 0; }
328 size_t length()
const;
331 size_t payloadOffset()
const {
return isSingleByte() ? 0 : (1 + lengthSize()); }
334 size_t items()
const;
343 mutable size_t m_lastIndex = (size_t)-1;
344 mutable size_t m_lastEnd = 0;
351 template <>
struct Converter<uint16_t> {
static uint16_t
convert(
RLP const& _r,
int _flags) {
return _r.
toInt<uint16_t>(_flags); } };
352 template <>
struct Converter<uint32_t> {
static uint32_t
convert(
RLP const& _r,
int _flags) {
return _r.
toInt<uint32_t>(_flags); } };
353 template <>
struct Converter<uint64_t> {
static uint64_t
convert(
RLP const& _r,
int _flags) {
return _r.
toInt<uint64_t>(_flags); } };
358 template <
class T,
class U>
struct Converter<
std::pair<T, U>> {
static std::pair<T, U>
convert(
RLP const& _r,
int _flags) {
return _r.
toPair<T, U>(_flags); } };
362 template <
class T,
size_t N>
struct Converter<
std::array<T, N>> {
static std::array<T, N>
convert(
RLP const& _r,
int _flags) {
return _r.
toArray<T, N>(_flags); } };
416 void clear() { m_out.clear(); m_listStack.clear(); }
428 void noteAppended(
size_t _itemCount = 1);
432 void pushCount(
size_t _count,
byte _offset);
435 template <
class _T>
void pushInt(_T _i,
size_t _br)
437 m_out.resize(m_out.size() + _br);
438 byte* b = &m_out.back();
440 *(b--) = (
byte)(_i & 0xff);
446 std::vector<std::pair<size_t, size_t>> m_listStack;
RLPStream & appendList(RLPStream const &_s)
static FixedHash< N > convert(RLP const &_r, int _flags)
bool operator==(std::string const &_s) const
std::pair< T, U > toPair(int _flags=Strict) const
RLPStream & append(u160 _s)
RLPStream & append(std::pair< T, U > const &_s)
bool operator!=(unsigned const &_i) const
static bigint convert(RLP const &_r, int _flags)
static uint32_t convert(RLP const &_r, int _flags)
static std::string convert(RLP const &_r, int _flags)
static u256 convert(RLP const &_r, int _flags)
RLP(byte const *_b, unsigned _s, Strictness _st=VeryStrict)
Construct a node to read RLP data in the bytes given.
RLPStream & append(RLP const &_rlp, size_t _itemCount=1)
Appends an arbitrary RLP fragment - this must be a single item unless _itemCount is given.
RLPStream(size_t _listItems)
Initializes the RLPStream as a list of _listItems items.
bool operator!=(std::string const &_s) const
void rlpListAux(RLPStream &_out, _T _t)
void swap(dev::eth::Watch &_a, dev::eth::Watch &_b)
RLPStream & append(std::array< _T, S > const &_s)
static std::set< T > convert(RLP const &_r, int _flags)
bytes const & out() const
Read the byte stream.
static const unsigned maxSize
bool isData() const
String value.
Base class for all RLP exceptions.
size_t actualSize() const
iterator begin() const
Iterator into beginning of sub-item list (valid only if we are a list).
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
bool isList() const
List value.
static std::array< T, N > convert(RLP const &_r, int _flags)
static bytes convert(RLP const &_r, int _flags)
bool isNull() const
No value.
size_t sizeStrict() const
std::ostream & operator<<(std::ostream &_out, bytes const &_e)
bool operator==(bigint const &_i) const
bool operator==(iterator const &_cmp) const
std::vector< T > toVector(int _flags=LaissezFaire) const
static uint16_t convert(RLP const &_r, int _flags)
RLPStream & appendVector(std::vector< _T > const &_s)
RLPStream & append(std::set< _T > const &_s)
bool operator!=(char const *_s) const
void clear()
Clear the output stream so far.
static T convert(RLP const &, int)
bytesConstRef toBytesConstRef(int _flags=LaissezFaire) const
Converts to bytearray.
RLPStream & append(bytes const &_s)
bool operator!=(bigint const &_i) const
bool operator==(char const *_s) const
Equality operators; does best-effort conversion and checks for equality.
RLPStream & appendRaw(bytes const &_rlp, size_t _itemCount=1)
vector_ref< _T > cropped(size_t _begin, size_t _count) const
RLP(bytes const &_d, Strictness _s=VeryStrict)
Construct a node of value given in the bytes.
RLPStream & append(std::unordered_set< _T > const &_s)
bool isInt() const
Integer value. Must not have a leading zero.
std::vector< byte > bytes
RLP()
Construct a null node.
std::string toString() const
RLPStream & append(FixedHash< N > _s, bool _compact=false, bool _allOrNothing=false)
vector_ref< byte const > bytesConstRef
static u160 convert(RLP const &_r, int _flags)
std::string toString(int _flags=LaissezFaire) const
Converts to string.
bool operator==(FixedHash< _N > const &_h) const
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 160, 160, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u160
iterator end() const
Iterator into end of sub-item list (valid only if we are a list).
static std::vector< T > convert(RLP const &_r, int _flags)
bool operator!=(FixedHash< _N > const &_s) const
RLPStream & appendList(bytes const &_rlp)
std::array< T, N > toArray(int _flags=LaissezFaire) const
std::unordered_set< T > toUnorderedSet(int _flags=LaissezFaire) const
bytesConstRef data() const
The bare data of the RLP.
RLPStream & append(std::string const &_s)
RLPStream & append(char const *_s)
bytesConstRef payload() const
Class for writing to an RLP bytestream.
static uint8_t convert(RLP const &_r, int _flags)
RLPStream & append(unsigned _s)
Append given datum to the byte stream.
bytes RLPNull
The empty string in RLP format.
std::set< T > toSet(int _flags=LaissezFaire) const
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<> > bigint
T convert(int _flags) const
bytes && invalidate()
Invalidate the object and steal the output byte stream.
bool operator!=(u256 const &_i) const
RLP operator[](size_t _i) const
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
_T toInt(int _flags=Strict) const
Converts to int of type given; if isData(), decodes as big-endian bytestream.
bool operator!=(iterator const &_cmp) const
_N toHash(int _flags=Strict) const
Iterator class for iterating through items of RLP list.
size_t itemCountStrict() const
void swapOut(bytes &_dest)
Swap the contents of the output stream out for some other byte array.
std::string toStringStrict() const
Converts to string.
bytes rlpList()
Export a list of items in RLP format, returning a byte array.
RLPStream & operator<<(T _data)
Shift operators for appending data items.
bool operator==(u256 const &_i) const
bool operator==(unsigned const &_i) const
int64_t toPositiveInt64(int _flags=Strict) const
RLPStream & appendRaw(bytesConstRef _rlp, size_t _itemCount=1)
Appends raw (pre-serialised) RLP data. Use with caution.
static std::unordered_set< T > convert(RLP const &_r, int _flags)
RLPStream & append(std::vector< _T > const &_s)
Appends a sequence of data to the stream as a list.
RLP(std::string const &_s, Strictness _st=VeryStrict)
Construct a node to read RLP data in the string.
RLPStream & append(u256 _s)
static std::pair< T, U > convert(RLP const &_r, int _flags)
bytes RLPEmptyList
The empty list in RLP format.
bool isEmpty() const
Contains a zero-length string or zero-length list.
RLPStream & appendList(size_t _items)
Appends a list.
static uint64_t convert(RLP const &_r, int _flags)
boost::error_info< struct tag_comment, std::string > errinfo_comment
bytes toBytes(int _flags=LaissezFaire) const
Converts to bytearray.
RLPStream()
Initializes empty RLPStream.