I have an API endpoint that takes a binary struct on a server written in C++. The endpoint must accept hundreds of these structs per second so using JSON or something similar has too much overhead.
The struct looks something like this:
struct TX {
int blockId;
char signature[64];
char signingKey[32];
time_t timestamp;
char nonce[8];
char to[25];
char from[25];
long amount;
long fee;
bool isTransactionFee;
};
I am writing a client in Python that must submit data in the above format. When I do sizeof on the struct in C++ it gives me 200 bytes, the sum of the individual fields sizeof's is somewhere closer to 182 bytes.
I am not sure where to start in terms of being able to serialize the structs in python accurately -- Do I assume that the padding to the struct is always added to the end? How do I know there isn't padding in between one of the two fields?
I wrote something along these lines to get started and plan to copy the data into these bytearrays:
txBinary = bytearray([0]*200)
blockId = bytearray([0]*4)
signature = bytearray([0]*64)
signingKey = bytearray([0]*32)
timestamp = bytearray([0]*8)
nonce = bytearray([0]*8)
toWallet = bytearray([0]*25)
fromWallet = bytearray([0]*25)
amount = bytearray([0]*8)
fee = bytearray([0]*8)
isFee = bytearray([0])
Is this is a sane approach? Is there some other easier way to serialize a datastructure like this in Python?
So after a lot of finagling, I decided the most robust way to do this is to use exec from within Python to call a C++ cli app that takes in the transaction parameters and outputs a base64 encoded version of the struct.
The most difficult challenge was that I needed to compute a cryptographic signature over the bytes in the transaction and getting the C++ and python versions of the cryptography libraries to work in unison became a pain, this led me to just use the C++ codebase and call it from within Python.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.