From e5ff4e416ecc8a51367022eb8a7a291f8cbc0c65 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Wed, 19 Feb 2025 09:46:06 -0500 Subject: [PATCH] qa: use a clearer and documented amount error in malleated snapshot In the assumeutxo functional tests, the final test case with alternated UTxO data tests the error raised when deserializing a snapshot that contains a coin with an amount not in range (<0 or >MAX_MONEY). The current malleation uses an undocumented byte string and offset which makes it hard to maintain. In addition, the undocumented offset is set surprisingly high (39 bytes is well into the serialization of the amount which starts at offset 36). Similarly the value is surprisingly small, presumably one was adjusted for the other. But there is no comment explaining how they were chosen, why not in a clearer manner and what they are supposed to represent. Instead replace this seemingly magic value with a clear one, MAX_MONEY + 1, serialize the whole value for the amount field at the correct offset, and document the whole thing for the next person around. --- test/functional/feature_assumeutxo.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/test/functional/feature_assumeutxo.py b/test/functional/feature_assumeutxo.py index 2d4e33e5d87..41c6bda3569 100755 --- a/test/functional/feature_assumeutxo.py +++ b/test/functional/feature_assumeutxo.py @@ -16,11 +16,16 @@ from test_framework.blocktools import ( create_block, create_coinbase ) +from test_framework.compressor import ( + compress_amount, +) from test_framework.messages import ( CBlockHeader, from_hex, msg_headers, - tx_from_hex + tx_from_hex, + ser_varint, + MAX_MONEY, ) from test_framework.p2p import ( P2PInterface, @@ -139,7 +144,14 @@ class AssumeutxoTest(BitcoinTestFramework): [b"\x81", 34, "3da966ba9826fb6d2604260e01607b55ba44e1a5de298606b08704bc62570ea8", None], # wrong coin code VARINT [b"\x80", 34, "091e893b3ccb4334378709578025356c8bcb0a623f37c7c4e493133c988648e5", None], # another wrong coin code [b"\x84\x58", 34, None, "Bad snapshot data after deserializing 0 coins"], # wrong coin case with height 364 and coinbase 0 - [b"\xCA\xD2\x8F\x5A", 39, None, "Bad snapshot data after deserializing 0 coins - bad tx out value"], # Amount exceeds MAX_MONEY + [ + # compressed txout value + scriptpubkey + ser_varint(compress_amount(MAX_MONEY + 1)) + ser_varint(0), + # txid + coins per txid + vout + coin height + 32 + 1 + 1 + 2, + None, + "Bad snapshot data after deserializing 0 coins - bad tx out value" + ], # Amount exceeds MAX_MONEY ] for content, offset, wrong_hash, custom_message in cases: