Skip to content

Commit eb53c03

Browse files
committed
Merge #20595: Improve heuristic hex transaction decoding
0f949cd Add regression test for incorrect decoding (Pieter Wuille) 39c42c4 Improve heuristic hex transaction decoding (Pieter Wuille) Pull request description: The current hex tx decoding logic will refuse to decode valid extended-encoded transactions if the result fails the heuristic sanity check, even when the legacy-encoding fails. Fix this. Fixes #20579 ACKs for top commit: achow101: Code review ACK 0f949cd jonatack: Tested ACK 0f949cd laanwj: Code review ACK 0f949cd Tree-SHA512: bd6dc80d824eb9a87026a623be910cac92173f8ce1c8b040c2246348c3cf0c6d64bcc40127b859e5e4da1efe88cf02a6945f7ebb91079799395145cb09d9c7a5
2 parents c8fdbb4 + 0f949cd commit eb53c03

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

src/core_read.cpp

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,31 +126,72 @@ static bool CheckTxScriptsSanity(const CMutableTransaction& tx)
126126

127127
static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>& tx_data, bool try_no_witness, bool try_witness)
128128
{
129+
// General strategy:
130+
// - Decode both with extended serialization (which interprets the 0x0001 tag as a marker for
131+
// the presense of witnesses) and with legacy serialization (which interprets the tag as a
132+
// 0-input 1-output incomplete transaction).
133+
// - Restricted by try_no_witness (which disables legacy if false) and try_witness (which
134+
// disables extended if false).
135+
// - Ignore serializations that do not fully consume the hex string.
136+
// - If neither succeeds, fail.
137+
// - If only one succeeds, return that one.
138+
// - If both decode attempts succeed:
139+
// - If only one passes the CheckTxScriptsSanity check, return that one.
140+
// - If neither or both pass CheckTxScriptsSanity, return the extended one.
141+
142+
CMutableTransaction tx_extended, tx_legacy;
143+
bool ok_extended = false, ok_legacy = false;
144+
145+
// Try decoding with extended serialization support, and remember if the result successfully
146+
// consumes the entire input.
129147
if (try_witness) {
130148
CDataStream ssData(tx_data, SER_NETWORK, PROTOCOL_VERSION);
131149
try {
132-
ssData >> tx;
133-
// If transaction looks sane, we don't try other mode even if requested
134-
if (ssData.empty() && (!try_no_witness || CheckTxScriptsSanity(tx))) {
135-
return true;
136-
}
150+
ssData >> tx_extended;
151+
if (ssData.empty()) ok_extended = true;
137152
} catch (const std::exception&) {
138153
// Fall through.
139154
}
140155
}
141156

157+
// Optimization: if extended decoding succeeded and the result passes CheckTxScriptsSanity,
158+
// don't bother decoding the other way.
159+
if (ok_extended && CheckTxScriptsSanity(tx_extended)) {
160+
tx = std::move(tx_extended);
161+
return true;
162+
}
163+
164+
// Try decoding with legacy serialization, and remember if the result successfully consumes the entire input.
142165
if (try_no_witness) {
143166
CDataStream ssData(tx_data, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
144167
try {
145-
ssData >> tx;
146-
if (ssData.empty()) {
147-
return true;
148-
}
168+
ssData >> tx_legacy;
169+
if (ssData.empty()) ok_legacy = true;
149170
} catch (const std::exception&) {
150171
// Fall through.
151172
}
152173
}
153174

175+
// If legacy decoding succeeded and passes CheckTxScriptsSanity, that's our answer, as we know
176+
// at this point that extended decoding either failed or doesn't pass the sanity check.
177+
if (ok_legacy && CheckTxScriptsSanity(tx_legacy)) {
178+
tx = std::move(tx_legacy);
179+
return true;
180+
}
181+
182+
// If extended decoding succeeded, and neither decoding passes sanity, return the extended one.
183+
if (ok_extended) {
184+
tx = std::move(tx_extended);
185+
return true;
186+
}
187+
188+
// If legacy decoding succeeded and extended didn't, return the legacy one.
189+
if (ok_legacy) {
190+
tx = std::move(tx_legacy);
191+
return true;
192+
}
193+
194+
// If none succeeded, we failed.
154195
return false;
155196
}
156197

test/functional/rpc_rawtransaction.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,13 @@ def run_test(self):
372372
encrawtx = "01000000010000000000000072c1a6a246ae63f74f931e8365e15a089c68d61900000000000000000000ffffffff0100e1f505000000000000000000"
373373
decrawtx = self.nodes[0].decoderawtransaction(encrawtx, False) # decode as non-witness transaction
374374
assert_equal(decrawtx['vout'][0]['value'], Decimal('1.00000000'))
375+
# known ambiguous transaction in the chain (see https://github.com/bitcoin/bitcoin/issues/20579)
376+
encrawtx = "020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff4b03c68708046ff8415c622f4254432e434f4d2ffabe6d6de1965d02c68f928e5b244ab1965115a36f56eb997633c7f690124bbf43644e23080000000ca3d3af6d005a65ff0200fd00000000ffffffff03f4c1fb4b0000000016001497cfc76442fe717f2a3f0cc9c175f7561b6619970000000000000000266a24aa21a9ed957d1036a80343e0d1b659497e1b48a38ebe876a056d45965fac4a85cda84e1900000000000000002952534b424c4f434b3a8e092581ab01986cbadc84f4b43f4fa4bb9e7a2e2a0caf9b7cf64d939028e22c0120000000000000000000000000000000000000000000000000000000000000000000000000"
377+
decrawtx = self.nodes[0].decoderawtransaction(encrawtx)
378+
decrawtx_wit = self.nodes[0].decoderawtransaction(encrawtx, True)
379+
assert_raises_rpc_error(-22, 'TX decode failed', self.nodes[0].decoderawtransaction, encrawtx, False) # fails to decode as non-witness transaction
380+
assert_equal(decrawtx, decrawtx_wit) # the witness interpretation should be chosen
381+
assert_equal(decrawtx['vin'][0]['coinbase'], "03c68708046ff8415c622f4254432e434f4d2ffabe6d6de1965d02c68f928e5b244ab1965115a36f56eb997633c7f690124bbf43644e23080000000ca3d3af6d005a65ff0200fd00000000")
375382

376383
# Basic signrawtransaction test
377384
addr = self.nodes[1].getnewaddress()

0 commit comments

Comments
 (0)