What is the endianness of the hex strings returned by getblockhash and getblockheader?
What is the endianness of the values returned by bitcoin-cli RPC commands getblockhash and getblockheader (verbose = false)?
Bitcoin Wiki's "Block hashing algorithm" page says the header is comprised of "little-endian values in hex notation". It continues (§ "Endianess"):
Note that the hash, which is a 256-bit number, has lots of leading zero bytes when stored or printed as a big-endian hexadecimal constant, but it has trailing zero bytes when stored or printed in little-endian.
Is this correct?
So does getblockheader (verbose = false) return little-endian hex:
$ bitcoin-cli getblockheader 00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d false
0100000081cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122bc7f5d74df2b9441a42a14695
and getblockhash:
$ bitcoin-cli getblockhash 125552
00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d
return big-endian hex, ∵ it has lots of leading zeros?
Why isn't everything the same endianness?
Top Answer/Comment:
is this correct?
Yes, the observation about leading and trailing zeroes corresponding to endianness looks correct to me.
So does getblockheader (verbose = false) return little-endian hex:
I think it returns the header in Bitcoin's network serialization, which has most integer values in little-endian byte-order. This conflicts with Internet standards but that's the way Satoshi Nakamoto wanted it.
and getblockhash [...] return big-endian hex,
Yes. I believe Satoshi Nakamoto and other developers preferred to present individual integers in big-endian as that makes most sense to conventionally educated people where fifteen is written as 15 not 51 because ten is bigger than five.
Why isn't everything the same endianness?
I doubt anyone really knows Satoshi Nakamoto's intent here. He was aware of endianness problems in porting Bitcoin code between architectures (and I think discouraged use of PPC for this reason).
I suspect some of this springs from characteristics of C and C++ programming and habits widespread in that community. These issues might not arise in network applications written in other programming languages and written to conform to Internet RFC standards.
RFC1700 by Reynolds and Postel in 1994 says
When a multi-octet quantity is transmitted the most significant octet is transmitted first.
Bitcoin violates that rule.
The consequence is that, in Bitcoin, I find you have to pay special attention to endianness and carefully code around it. I also find that many people are tripped up by this.
상단 광고의 [X] 버튼을 누르면 내용이 보입니다