Looking at the source code of bitcoin core, the serialization part, I see that there's no serialization abstraction layer that accounts for a possible different endianness or different binary layouts on different machines.

This is not only for disk serialization, but also for network serialization. If bitcoin core was used on a big-endian machine, or a machine with int size not equal to 32-bit, it'll fail to connect to the network because all sizes will be incorrectly interpreted and values will be different.

Am I missing something or did I arrive at the correct conclusion?

  • The byteswap has been addressed in MCCCS's answer (it's done by htole16 and the like). For the word size, can you explain specifically what you think is wrong? That code appears, at first glance, to make proper use of the uintNN_t types. – Nate Eldredge Dec 14 '18 at 17:27
  • @NateEldredge Many of the data structures being serialized use int/long/short/..., and serialize those directly. The serialization code per se does the right thing, but the data being serialized wouldn't. – Pieter Wuille Dec 14 '18 at 19:56

ser_writedata8 does not care about endianness, in contrast to ser_writedata16, ser_writedata32, ser_writedata64. Single bytes don't need to swapped, as 8 bits have one address and there's no way to access specific bits without using BMI or arithmetic operations. Therefore, bit endianness is not important except in bitfields.

  • I know 1 byte doesn't need endianness flips. I just couldn't see how the flipping is being done. – The Quantum Physicist Dec 14 '18 at 16:58
  • @TheQuantumPhysicist For example, Serialize(Stream& s, uint16_t a) calls ser_writedata16, which calls htole16(obj);. htole16 does the bswap there. – MCCCS Dec 14 '18 at 17:03
  • Oh, OK. I see that now. Thanks for the answer. – The Quantum Physicist Dec 14 '18 at 17:05

It should work on big-endian systems. The serialization code for integers will byteswap on such systems. This was certainly tested at some point, but I'm not sure how recently someone has.

I don't think it will work on systems where int is different than 32-bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.