From 377d216af1869b54b011de4bed6256d708572581 Mon Sep 17 00:00:00 2001 From: Intel Date: Sun, 22 Dec 2013 17:03:25 +0200 Subject: [PATCH] Deserialization --- src/server/poolserver/Server/Server.cpp | 18 +++--- src/server/shared/Bitcoin/Script.h | 1 + src/server/shared/Bitcoin/Serialization.cpp | 68 +++++++++++++++++++++ src/server/shared/Bitcoin/Transaction.h | 8 +++ src/server/shared/Bitcoin/VarInt.h | 6 +- src/server/shared/ByteBuffer.h | 30 +++++++++ 6 files changed, 121 insertions(+), 10 deletions(-) diff --git a/src/server/poolserver/Server/Server.cpp b/src/server/poolserver/Server/Server.cpp index 78a06d3..00a3637 100644 --- a/src/server/poolserver/Server/Server.cpp +++ b/src/server/poolserver/Server/Server.cpp @@ -35,17 +35,17 @@ int Server::Run() //InitDatabase(); - Bitcoin::OutPoint outpoint; - outpoint.hash = Util::ASCIIToBin("6DBDDB085B1D8AF75184F0BC01FAD58D1266E9B63B50881990E4B40D6AEE3629"); - outpoint.n = 0; + ByteBuffer buf(Util::ASCIIToBin("01000000010c432f4fb3e871a8bda638350b3d5c698cf431db8d6031b53e3fb5159e59d4a90000000000ffffffff0100f2052a010000001976a9143744841e13b90b4aca16fe793a7f88da3a23cc7188ac00000000")); + + Bitcoin::Transaction trans; + buf >> trans; + + ByteBuffer buf2; + buf2 << trans; + + sLog.Info(LOG_SERVER, "Trans: %s", Util::BinToASCII(buf2.vec).c_str()); - Bitcoin::TxOut txout; - txout.value = 5000000; - txout.scriptPubKey = Bitcoin::Script(Util::ASCIIToBin("76A9141AA0CD1CBEA6E7458A7ABAD512A9D9EA1AFB225E88AC")); - ByteBuffer buf; - buf << txout; - sLog.Info(LOG_SERVER, "Serialized: %s", Util::BinToASCII(buf.vec).c_str()); // Main io service asio::io_service io_service; diff --git a/src/server/shared/Bitcoin/Script.h b/src/server/shared/Bitcoin/Script.h index 5e65a06..17e8c75 100644 --- a/src/server/shared/Bitcoin/Script.h +++ b/src/server/shared/Bitcoin/Script.h @@ -64,6 +64,7 @@ namespace Bitcoin }; ByteBuffer& operator<<(ByteBuffer& a, Script& b); + ByteBuffer& operator>>(ByteBuffer& a, Script& b); } #endif diff --git a/src/server/shared/Bitcoin/Serialization.cpp b/src/server/shared/Bitcoin/Serialization.cpp index 086d448..440ba79 100644 --- a/src/server/shared/Bitcoin/Serialization.cpp +++ b/src/server/shared/Bitcoin/Serialization.cpp @@ -4,6 +4,7 @@ using namespace Bitcoin; +// VarInt ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, VarInt& b) { if (b.value < 0xfd) { @@ -20,7 +21,21 @@ ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, VarInt& b) } return a; } +ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, VarInt& b) +{ + uint8 size = a.Read(); + + if (size < 0xfd) + b.value = size; + else if (size == 0xfd) + b.value = a.Read(); + else if (size == 0xfe) + b.value = a.Read(); + else + b.value = a.Read(); +} +// Script ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, Script& b) { VarInt size(b.script.size()); @@ -28,14 +43,29 @@ ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, Script& b) a << b.script; return a; } +ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, Script& b) +{ + VarInt size; + a >> size; + b.script = a.ReadBytes(size); + return a; +} +// OutPoint ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, OutPoint& b) { a << b.hash; a << b.n; return a; } +ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, OutPoint& b) +{ + b.hash = a.ReadBytes(32); + b.n = a.Read(); + return a; +} +// TxIn ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, TxIn& b) { a << b.prevout; @@ -43,14 +73,29 @@ ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, TxIn& b) a << b.n; return a; } +ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, TxIn& b) +{ + a >> b.prevout; + a >> b.script; + b.n = a.Read(); + return a; +} +// TxOut ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, TxOut& b) { a << b.value; a << b.scriptPubKey; return a; } +ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, TxOut& b) +{ + b.value = a.Read(); + a >> b.scriptPubKey; + return a; +} +// Transaction ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, Transaction& b) { a << b.version; @@ -70,3 +115,26 @@ ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, Transaction& b) a << b.lockTime; return a; } +ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, Transaction& b) +{ + b.version = a.Read(); + + // Inputs + VarInt insize; + a >> insize; + + b.in.resize(insize); + for (uint32 i = 0; i < insize; ++i) + a >> b.in[i]; + + // Outputs + VarInt outsize; + a >> outsize; + + b.out.resize(outsize); + for (uint32 i = 0; i < outsize; ++i) + a >> b.out[i]; + + b.lockTime = a.Read(); + return a; +} diff --git a/src/server/shared/Bitcoin/Transaction.h b/src/server/shared/Bitcoin/Transaction.h index d4d0b8b..7a8e80c 100644 --- a/src/server/shared/Bitcoin/Transaction.h +++ b/src/server/shared/Bitcoin/Transaction.h @@ -15,7 +15,9 @@ namespace Bitcoin uint32 n; }; + // OutPoint Serialization (Implementation in Serialization.cpp) ByteBuffer& operator<<(ByteBuffer& a, OutPoint& b); + ByteBuffer& operator>>(ByteBuffer& a, OutPoint& b); class TxIn { @@ -25,7 +27,9 @@ namespace Bitcoin uint32 n; }; + // TxIn Serialization (Implementation in Serialization.cpp) ByteBuffer& operator<<(ByteBuffer& a, TxIn& b); + ByteBuffer& operator>>(ByteBuffer& a, TxIn& b); class TxOut { @@ -34,7 +38,9 @@ namespace Bitcoin Script scriptPubKey; }; + // TxOut Serialization (Implementation in Serialization.cpp) ByteBuffer& operator<<(ByteBuffer& a, TxOut& b); + ByteBuffer& operator>>(ByteBuffer& a, TxOut& b); class Transaction { @@ -45,7 +51,9 @@ namespace Bitcoin uint32 lockTime; }; + // Transaction Serialization (Implementation in Serialization.cpp) ByteBuffer& operator<<(ByteBuffer& a, Transaction& b); + ByteBuffer& operator>>(ByteBuffer& a, Transaction& b); } #endif diff --git a/src/server/shared/Bitcoin/VarInt.h b/src/server/shared/Bitcoin/VarInt.h index 1e97f0c..801132e 100644 --- a/src/server/shared/Bitcoin/VarInt.h +++ b/src/server/shared/Bitcoin/VarInt.h @@ -9,13 +9,17 @@ namespace Bitcoin class VarInt { public: - VarInt(uint64 data) : value(data) {} + VarInt(): value(0) {} + VarInt(uint64 data): value(data) {} uint64 value; + operator uint64() const { return value; } + friend ByteBuffer& operator<<(ByteBuffer& a, VarInt& b); }; ByteBuffer& operator<<(ByteBuffer& a, VarInt& b); + ByteBuffer& operator>>(ByteBuffer& a, VarInt& b); } #endif diff --git a/src/server/shared/ByteBuffer.h b/src/server/shared/ByteBuffer.h index 1b03b2d..2bc7661 100644 --- a/src/server/shared/ByteBuffer.h +++ b/src/server/shared/ByteBuffer.h @@ -7,6 +7,9 @@ class ByteBuffer { public: + ByteBuffer(): pointer(0) {} + ByteBuffer(std::vector data): pointer(0), vec(data) {} + ByteBuffer& operator<<(ByteBuffer& b) { Append(b.vec); @@ -69,6 +72,33 @@ public: vec.insert(vec.end(), data.begin(), data.end()); } + std::vector ReadBytes(size_t size) + { + if (vec.size() < pointer+size) + return std::vector(); + + pointer += size; + return std::vector(vec.begin()+pointer-size, vec.begin()+pointer); + } + + template + T Read() + { + size_t size = sizeof(T); + + if (vec.size() < pointer+size) + return NULL; + + T data = 0; + for (uint32 i = 0; i < size; ++i) + data += vec[pointer+i]<<(i*8); + + pointer += size; + + return data; + } + + uint64 pointer; std::vector vec; };