Browse Source

Deserialization

peercoin
Intel 11 years ago
parent
commit
377d216af1
  1. 18
      src/server/poolserver/Server/Server.cpp
  2. 1
      src/server/shared/Bitcoin/Script.h
  3. 68
      src/server/shared/Bitcoin/Serialization.cpp
  4. 8
      src/server/shared/Bitcoin/Transaction.h
  5. 4
      src/server/shared/Bitcoin/VarInt.h
  6. 30
      src/server/shared/ByteBuffer.h

18
src/server/poolserver/Server/Server.cpp

@ -35,17 +35,17 @@ int Server::Run()
//InitDatabase(); //InitDatabase();
Bitcoin::OutPoint outpoint; ByteBuffer buf(Util::ASCIIToBin("01000000010c432f4fb3e871a8bda638350b3d5c698cf431db8d6031b53e3fb5159e59d4a90000000000ffffffff0100f2052a010000001976a9143744841e13b90b4aca16fe793a7f88da3a23cc7188ac00000000"));
outpoint.hash = Util::ASCIIToBin("6DBDDB085B1D8AF75184F0BC01FAD58D1266E9B63B50881990E4B40D6AEE3629");
outpoint.n = 0; 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 // Main io service
asio::io_service io_service; asio::io_service io_service;

1
src/server/shared/Bitcoin/Script.h

@ -64,6 +64,7 @@ namespace Bitcoin
}; };
ByteBuffer& operator<<(ByteBuffer& a, Script& b); ByteBuffer& operator<<(ByteBuffer& a, Script& b);
ByteBuffer& operator>>(ByteBuffer& a, Script& b);
} }
#endif #endif

68
src/server/shared/Bitcoin/Serialization.cpp

@ -4,6 +4,7 @@
using namespace Bitcoin; using namespace Bitcoin;
// VarInt
ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, VarInt& b) ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, VarInt& b)
{ {
if (b.value < 0xfd) { if (b.value < 0xfd) {
@ -20,7 +21,21 @@ ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, VarInt& b)
} }
return a; return a;
} }
ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, VarInt& b)
{
uint8 size = a.Read<uint8>();
if (size < 0xfd)
b.value = size;
else if (size == 0xfd)
b.value = a.Read<uint16>();
else if (size == 0xfe)
b.value = a.Read<uint32>();
else
b.value = a.Read<uint64>();
}
// Script
ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, Script& b) ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, Script& b)
{ {
VarInt size(b.script.size()); VarInt size(b.script.size());
@ -28,14 +43,29 @@ ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, Script& b)
a << b.script; a << b.script;
return a; 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) ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, OutPoint& b)
{ {
a << b.hash; a << b.hash;
a << b.n; a << b.n;
return a; return a;
} }
ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, OutPoint& b)
{
b.hash = a.ReadBytes(32);
b.n = a.Read<uint32>();
return a;
}
// TxIn
ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, TxIn& b) ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, TxIn& b)
{ {
a << b.prevout; a << b.prevout;
@ -43,14 +73,29 @@ ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, TxIn& b)
a << b.n; a << b.n;
return a; return a;
} }
ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, TxIn& b)
{
a >> b.prevout;
a >> b.script;
b.n = a.Read<uint32>();
return a;
}
// TxOut
ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, TxOut& b) ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, TxOut& b)
{ {
a << b.value; a << b.value;
a << b.scriptPubKey; a << b.scriptPubKey;
return a; return a;
} }
ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, TxOut& b)
{
b.value = a.Read<uint64>();
a >> b.scriptPubKey;
return a;
}
// Transaction
ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, Transaction& b) ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, Transaction& b)
{ {
a << b.version; a << b.version;
@ -70,3 +115,26 @@ ByteBuffer& Bitcoin::operator<<(ByteBuffer& a, Transaction& b)
a << b.lockTime; a << b.lockTime;
return a; return a;
} }
ByteBuffer& Bitcoin::operator>>(ByteBuffer& a, Transaction& b)
{
b.version = a.Read<uint32>();
// 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<uint32>();
return a;
}

8
src/server/shared/Bitcoin/Transaction.h

@ -15,7 +15,9 @@ namespace Bitcoin
uint32 n; uint32 n;
}; };
// OutPoint Serialization (Implementation in Serialization.cpp)
ByteBuffer& operator<<(ByteBuffer& a, OutPoint& b); ByteBuffer& operator<<(ByteBuffer& a, OutPoint& b);
ByteBuffer& operator>>(ByteBuffer& a, OutPoint& b);
class TxIn class TxIn
{ {
@ -25,7 +27,9 @@ namespace Bitcoin
uint32 n; uint32 n;
}; };
// TxIn Serialization (Implementation in Serialization.cpp)
ByteBuffer& operator<<(ByteBuffer& a, TxIn& b); ByteBuffer& operator<<(ByteBuffer& a, TxIn& b);
ByteBuffer& operator>>(ByteBuffer& a, TxIn& b);
class TxOut class TxOut
{ {
@ -34,7 +38,9 @@ namespace Bitcoin
Script scriptPubKey; Script scriptPubKey;
}; };
// TxOut Serialization (Implementation in Serialization.cpp)
ByteBuffer& operator<<(ByteBuffer& a, TxOut& b); ByteBuffer& operator<<(ByteBuffer& a, TxOut& b);
ByteBuffer& operator>>(ByteBuffer& a, TxOut& b);
class Transaction class Transaction
{ {
@ -45,7 +51,9 @@ namespace Bitcoin
uint32 lockTime; uint32 lockTime;
}; };
// Transaction Serialization (Implementation in Serialization.cpp)
ByteBuffer& operator<<(ByteBuffer& a, Transaction& b); ByteBuffer& operator<<(ByteBuffer& a, Transaction& b);
ByteBuffer& operator>>(ByteBuffer& a, Transaction& b);
} }
#endif #endif

4
src/server/shared/Bitcoin/VarInt.h

@ -9,13 +9,17 @@ namespace Bitcoin
class VarInt class VarInt
{ {
public: public:
VarInt(): value(0) {}
VarInt(uint64 data): value(data) {} VarInt(uint64 data): value(data) {}
uint64 value; uint64 value;
operator uint64() const { return value; }
friend ByteBuffer& operator<<(ByteBuffer& a, VarInt& b); friend ByteBuffer& operator<<(ByteBuffer& a, VarInt& b);
}; };
ByteBuffer& operator<<(ByteBuffer& a, VarInt& b); ByteBuffer& operator<<(ByteBuffer& a, VarInt& b);
ByteBuffer& operator>>(ByteBuffer& a, VarInt& b);
} }
#endif #endif

30
src/server/shared/ByteBuffer.h

@ -7,6 +7,9 @@
class ByteBuffer class ByteBuffer
{ {
public: public:
ByteBuffer(): pointer(0) {}
ByteBuffer(std::vector<byte> data): pointer(0), vec(data) {}
ByteBuffer& operator<<(ByteBuffer& b) ByteBuffer& operator<<(ByteBuffer& b)
{ {
Append(b.vec); Append(b.vec);
@ -69,6 +72,33 @@ public:
vec.insert(vec.end(), data.begin(), data.end()); vec.insert(vec.end(), data.begin(), data.end());
} }
std::vector<byte> ReadBytes(size_t size)
{
if (vec.size() < pointer+size)
return std::vector<byte>();
pointer += size;
return std::vector<byte>(vec.begin()+pointer-size, vec.begin()+pointer);
}
template<typename T>
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<byte> vec; std::vector<byte> vec;
}; };

Loading…
Cancel
Save