Skip to content

Commit 3956165

Browse files
committed
Merge #17775: DecodeHexTx: Try case where txn has inputs first
27fc6a3 DecodeHexTx: Break out transaction decoding logic into own function (Gregory Sanders) 6020ce3 DecodeHexTx: Try case where txn has inputs first (Gregory Sanders) Pull request description: Alternative/complementary to bitcoin/bitcoin#17773 to avoid random `decoderawtransaction` failures. Most cases this is used now is on complete transactions, especially with the uptake of PSBT. ACKs for top commit: ajtowns: ACK 27fc6a3 achow101: ACK 27fc6a3 Tree-SHA512: 0a836d7c9951bf7d2764507788dbcc871d520f1ea9b77d6b22f051f4d6224ed779aba0e4f28c5c165040095ee0c70b67080c39164d82de61b19158f7ae6fddb2
2 parents 3caee16 + 27fc6a3 commit 3956165

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/core_read.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,28 +117,23 @@ static bool CheckTxScriptsSanity(const CMutableTransaction& tx)
117117
return true;
118118
}
119119

120-
bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness, bool try_witness)
120+
static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>& tx_data, bool try_no_witness, bool try_witness)
121121
{
122-
if (!IsHex(hex_tx)) {
123-
return false;
124-
}
125-
126-
std::vector<unsigned char> txData(ParseHex(hex_tx));
127-
128-
if (try_no_witness) {
129-
CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
122+
if (try_witness) {
123+
CDataStream ssData(tx_data, SER_NETWORK, PROTOCOL_VERSION);
130124
try {
131125
ssData >> tx;
132-
if (ssData.eof() && (!try_witness || CheckTxScriptsSanity(tx))) {
126+
// If transaction looks sane, we don't try other mode even if requested
127+
if (ssData.empty() && (!try_no_witness || CheckTxScriptsSanity(tx))) {
133128
return true;
134129
}
135130
} catch (const std::exception&) {
136131
// Fall through.
137132
}
138133
}
139134

140-
if (try_witness) {
141-
CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
135+
if (try_no_witness) {
136+
CDataStream ssData(tx_data, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
142137
try {
143138
ssData >> tx;
144139
if (ssData.empty()) {
@@ -152,6 +147,16 @@ bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no
152147
return false;
153148
}
154149

150+
bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness, bool try_witness)
151+
{
152+
if (!IsHex(hex_tx)) {
153+
return false;
154+
}
155+
156+
std::vector<unsigned char> txData(ParseHex(hex_tx));
157+
return DecodeTx(tx, txData, try_no_witness, try_witness);
158+
}
159+
155160
bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header)
156161
{
157162
if (!IsHex(hex_header)) return false;

0 commit comments

Comments
 (0)