Skip to content

Commit 433412f

Browse files
committed
Merge bitcoin#31596: doc: Clarify comments about endianness after bitcoin#30526
3e0a992 doc: Clarify comments about endianness after bitcoin#30526 (Ryan Ofsky) Pull request description: This is a documentation-only change following up on suggestions made in the bitcoin#30526 review. Motivation for this change is that I was recently reviewing bitcoin#31583, which reminded me how confusing the arithmetic blob code was and made me want to write better comments. ACKs for top commit: achow101: ACK 3e0a992 TheCharlatan: ACK 3e0a992 Sjors: ACK 3e0a992 BrandonOdiwuor: LGTM ACK 3e0a992 Tree-SHA512: 90d5582a25a51fc406d83ca6b8c4e5e4d3aee828a0497f4b226b2024ff89e29b9b50d0ae8ddeac6abf2757fe78548d58cf3dd54df4b6d623f634a2106048091d
2 parents c506f2c + 3e0a992 commit 433412f

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

src/arith_uint256.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class base_uint
2626
protected:
2727
static_assert(BITS / 32 > 0 && BITS % 32 == 0, "Template parameter BITS must be a positive multiple of 32.");
2828
static constexpr int WIDTH = BITS / 32;
29+
/** Big integer represented with 32-bit digits, least-significant first. */
2930
uint32_t pn[WIDTH];
3031
public:
3132

src/policy/packages.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ bool IsChildWithParents(const Package& package);
8989
*/
9090
bool IsChildWithParentsTree(const Package& package);
9191

92-
/** Get the hash of these transactions' wtxids, concatenated in lexicographical order (treating the
93-
* wtxids as little endian encoded uint256, smallest to largest). */
92+
/** Get the hash of the concatenated wtxids of transactions, with wtxids
93+
* treated as a little-endian numbers and sorted in ascending numeric order.
94+
*/
9495
uint256 GetPackageHash(const std::vector<CTransactionRef>& transactions);
9596

9697
#endif // BITCOIN_POLICY_PACKAGES_H

src/rpc/mining.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,8 @@ static RPCHelpMan getblocktemplate()
633633
{RPCResult::Type::OBJ, "", "",
634634
{
635635
{RPCResult::Type::STR_HEX, "data", "transaction data encoded in hexadecimal (byte-for-byte)"},
636-
{RPCResult::Type::STR_HEX, "txid", "transaction id encoded in little-endian hexadecimal"},
637-
{RPCResult::Type::STR_HEX, "hash", "hash encoded in little-endian hexadecimal (including witness data)"},
636+
{RPCResult::Type::STR_HEX, "txid", "transaction hash excluding witness data, shown in byte-reversed hex"},
637+
{RPCResult::Type::STR_HEX, "hash", "transaction hash including witness data, shown in byte-reversed hex"},
638638
{RPCResult::Type::ARR, "depends", "array of numbers",
639639
{
640640
{RPCResult::Type::NUM, "", "transactions before this one (by 1-based index in 'transactions' list) that must be present in the final block if this one is"},

src/uint256.h

+20-9
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,27 @@ class base_blob
6969

7070
/** @name Hex representation
7171
*
72-
* The reverse-byte hex representation is a convenient way to view the blob
73-
* as a number, because it is consistent with the way the base_uint class
74-
* converts blobs to numbers.
72+
* The hex representation used by GetHex(), ToString(), FromHex() and
73+
* SetHexDeprecated() is unusual, since it shows bytes of the base_blob in
74+
* reverse order. For example, a 4-byte blob {0x12, 0x34, 0x56, 0x78} is
75+
* represented as "78563412" instead of the more typical "12345678"
76+
* representation that would be shown in a hex editor or used by typical
77+
* byte-array / hex conversion functions like python's bytes.hex() and
78+
* bytes.fromhex().
79+
*
80+
* The nice thing about the reverse-byte representation, even though it is
81+
* unusual, is that if a blob contains an arithmetic number in little endian
82+
* format (with least significant bytes first, and most significant bytes
83+
* last), the GetHex() output will match the way the number would normally
84+
* be written in base-16 (with most significant digits first and least
85+
* significant digits last).
86+
*
87+
* This means, for example, that ArithToUint256(num).GetHex() can be used to
88+
* display an arith_uint256 num value as a number, because
89+
* ArithToUint256() converts the number to a blob in little-endian format,
90+
* so the arith_uint256 class doesn't need to have its own number parsing
91+
* and formatting functions.
7592
*
76-
* @note base_uint treats the blob as an array of bytes with the numerically
77-
* least significant byte first and the most significant byte last. Because
78-
* numbers are typically written with the most significant digit first and
79-
* the least significant digit last, the reverse hex display of the blob
80-
* corresponds to the same numeric value that base_uint interprets from the
81-
* blob.
8293
* @{*/
8394
std::string GetHex() const;
8495
/** Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!

0 commit comments

Comments
 (0)