mirror of
https://github.com/GOSTSec/poolserver
synced 2025-01-19 19:20:16 +00:00
Deserialization
This commit is contained in:
parent
68af0a221f
commit
377d216af1
@ -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;
|
||||
|
@ -64,6 +64,7 @@ namespace Bitcoin
|
||||
};
|
||||
|
||||
ByteBuffer& operator<<(ByteBuffer& a, Script& b);
|
||||
ByteBuffer& operator>>(ByteBuffer& a, Script& b);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -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<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)
|
||||
{
|
||||
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<uint32>();
|
||||
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<uint32>();
|
||||
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<uint64>();
|
||||
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<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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -7,6 +7,9 @@
|
||||
class ByteBuffer
|
||||
{
|
||||
public:
|
||||
ByteBuffer(): pointer(0) {}
|
||||
ByteBuffer(std::vector<byte> 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<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;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user