From 8ba143e564501f48db0801d298ccc55e2d593c5d Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 27 Dec 2013 16:19:09 +0000 Subject: [PATCH] Docs. --- Common.h | 1 + RLP.h | 26 ++++++++++++++++++----- main.cpp | 64 ++++++++++++++++++++++++-------------------------------- 3 files changed, 49 insertions(+), 42 deletions(-) diff --git a/Common.h b/Common.h index e4bcd22d7..c3736872a 100644 --- a/Common.h +++ b/Common.h @@ -8,6 +8,7 @@ namespace eth typedef uint8_t byte; typedef foreign Bytes; +typedef foreign ConstBytes; template std::string toString(_T const& _t) { std::ostringstream o; o << _t; return o.str(); } diff --git a/RLP.h b/RLP.h index e4b83baa9..76fd86e2c 100644 --- a/RLP.h +++ b/RLP.h @@ -16,12 +16,28 @@ using bigint = boost::multiprecision::cpp_int; using uint = uint64_t; using sint = int64_t; +/** + * @brief Recursive Linear-Packed data structure. + * @by Gav Wood, 2013 + * + * Class for reading byte arrays of data in RLP format. + */ class RLP { public: + /// Construct a null node. RLP() {} - RLP(Bytes _d): m_data(_d) {} + /// Construct a node of value given in the bytes. + explicit RLP(ConstBytes _d): m_data(_d) {} + + /// Construct a node to read RLP data in the bytes given. + RLP(byte const* _b, uint _s): m_data(ConstBytes(_b, _s)) {} + + /// Construct a node to read RLP data in the string. + explicit RLP(std::string const& _s): m_data(ConstBytes((byte const*)_s.data(), _s.size())) {} + + /// @returns true if the RLP is non-null. explicit operator bool() const { return !isNull(); } /// No value. @@ -82,7 +98,7 @@ public: if (!isList()) return ret; uint64_t c = items(); - Bytes d = payload(); + ConstBytes d = payload(); for (uint64_t i = 0; i < c; ++i, d = d.cropped(RLP(d).size())) ret.push_back(RLP(d)); return ret; @@ -112,7 +128,7 @@ private: return payload().data() - m_data.data() + items(); if (isList()) { - Bytes d = payload(); + ConstBytes d = payload(); uint64_t c = items(); for (uint64_t i = 0; i < c; ++i, d = d.cropped(RLP(d).size())) {} return d.data() - m_data.data(); @@ -134,14 +150,14 @@ private: return ret; } - Bytes payload() const + ConstBytes payload() const { assert(isString() || isList()); auto n = (m_data[0] & 0x3f); return m_data.cropped(1 + (n < 0x38 ? 0 : (n - 0x37))); } - Bytes m_data; + ConstBytes m_data; }; } diff --git a/main.cpp b/main.cpp index 7a8dcdb4a..a15b059b0 100644 --- a/main.cpp +++ b/main.cpp @@ -4,43 +4,33 @@ using namespace eth; int main() { - { - string t = "\x0f"; - assert(toString(RLP(Bytes((byte*)t.data(), t.size()))) == "15"); - } - { - string t = "\x43""dog"; - assert(toString(RLP(Bytes((byte*)t.data(), t.size()))) == "\"dog\""); - } - { - string t = "\x82\x0f\x43""dog"; - assert(toString(RLP(Bytes((byte*)t.data(), t.size()))) == "[ 15, \"dog\" ]"); - } - { - string t = "\x18\x45"; - assert(toString(RLP(Bytes((byte*)t.data(), t.size()))) == "69"); - } - { - string t = "\x19\x01\x01"; - assert(toString(RLP(Bytes((byte*)t.data(), t.size()))) == "257"); - } - { - string t = "\x37\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"; - ostringstream o; - o << hex << RLP(Bytes((byte*)t.data(), t.size())); - assert(o.str() == "100102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"); - } - { - // 33-byte int - string t = "\x38\x21\x20\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"; - ostringstream o; - o << hex << RLP(Bytes((byte*)t.data(), t.size())); - assert(o.str() == "20100102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"); - } - { - string t = "\x78\x38""Lorem ipsum dolor sit amet, consectetur adipisicing elit"; - assert(toString(RLP(Bytes((byte*)t.data(), t.size()))) == "\"Lorem ipsum dolor sit amet, consectetur adipisicing elit\""); - } + // int of value 15 + assert(toString(RLP("\x0f")) == "15"); + + // 2-item list + assert(toString(RLP("\x43""dog")) == "\"dog\""); + + // 3-character string + assert(toString(RLP("\x82\x0f\x43""dog")) == "[ 15, \"dog\" ]"); + + // 1-byte (8-bit) int + assert(toString(RLP("\x18\x45")) == "69"); + + // 2-byte (16-bit) int + assert(toString(RLP("\x19\x01\x01")) == "257"); + + // 32-byte (256-bit) int + ostringstream o1; + o1 << hex << RLP("\x37\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"); + assert(o1.str() == "100102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"); + + // 33-byte (264-bit) int + ostringstream o2; + o2 << hex << RLP("\x38\x21\x20\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"); + assert(o2.str() == "20100102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"); + + // 56-character string. + assert(toString(RLP("\x78\x38""Lorem ipsum dolor sit amet, consectetur adipisicing elit")) == "\"Lorem ipsum dolor sit amet, consectetur adipisicing elit\""); return 0; }