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. Dec 14, 2018 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. Dec 14, 2018 at 19:56

2 Answers 2


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. Dec 14, 2018 at 16:58
  • 1
    @TheQuantumPhysicist For example, Serialize(Stream& s, uint16_t a) calls ser_writedata16, which calls htole16(obj);. htole16 does the bswap there.
    – MCCCS
    Dec 14, 2018 at 17:03

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 and acknowledge you have read our privacy policy.

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