Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utxo): avoid index out of bounds panics in tx_details_by_hash #1915

Merged
merged 2 commits into from
Jul 18, 2023

Conversation

shamardy
Copy link
Collaborator

@shamardy shamardy commented Jul 12, 2023

Partially fixes #1906

This PR addresses index out of bounds errors in the tx_details_by_hash functions. The changes replace direct array access with safer methods, avoiding potential panics due to out-of-range indices. In addition, error handling around the function was improved to log and skip transactions in case of errors.
Please note that while this PR doesn't fully resolve the issue outlined in https://github.com/KomodoPlatform/komodo-wallet-desktop/issues/2317, as the transactions causing panics during history fetching will be skipped instead. The PR will facilitate the pinpointing of the actual problem by logging the transactions that cause these issues.

@shamardy shamardy added in progress Changes will be made from the author under review 1.0.6-beta and removed in progress Changes will be made from the author labels Jul 12, 2023
@rozhkovdmitrii
Copy link

There are few places where transaction outputs are got: 1, 2, 3 etc...

I'm not sure, but it presumably can have a sense to make all these places safe if we're getting this error to prevent such class of errors in the future.

Copy link

@caglaryucekaya caglaryucekaya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I couldn't find test coverage for transaction_details_with_token_transfers for BCH. Is the test_cashaddresses_in_tx_details_by_hash test intended for it? It calls the tx_details_by_hash in utxo_common. Otherwise, I don't see any problem that could cause the panic, we should wait for it to happen again and use the tx hash that you logged now as you wrote.

@shamardy
Copy link
Collaborator Author

There are few places where transaction outputs are got: 1, 2, 3 etc...

I am aware of these places but thanks @rozhkovdmitrii for pointing it out :)
I wanted to do this fix quickly to include it in the release then work on those later since I also check where each function containing those are used and try to fix any other problems I find.

@shamardy
Copy link
Collaborator Author

but I couldn't find test coverage for transaction_details_with_token_transfers for BCH. Is the test_cashaddresses_in_tx_details_by_hash test intended for it? It calls the tx_details_by_hash in utxo_common.

it also calls UtxoTxHistoryOps::tx_details_by_hash

let tx_details = get_tx_details_eq_for_both_versions(&coin, TX_HASH);

let tx_details_v2 = get_tx_details_by_hash_v2(coin, tx_hash, tx_details_v1.block_height, tx_details_v1.timestamp);

block_on(UtxoTxHistoryOps::tx_details_by_hash(coin, params)).unwrap()

Otherwise, I don't see any problem that could cause the panic, we should wait for it to happen again and use the tx hash that you logged now as you wrote.

Yeah, that is the intention of this PR, to prevent panic and to see what transactions were causing that in the future so that we can fix their deserializations if it were the problem.

@caglaryucekaya
Copy link

it also calls UtxoTxHistoryOps::tx_details_by_hash

Yes, but the coin created in that test is UtxoStandardCoin, not BchCoin, so it does not call the transaction_details_with_token_transfers. It calls the tx_details_by_hash in utxo_common.

@shamardy
Copy link
Collaborator Author

Yes, but the coin created in that test is UtxoStandardCoin, not BchCoin, so it does not call the transaction_details_with_token_transfers. It calls the tx_details_by_hash in utxo_common.

Yeah, I missed that. Thanks for pointing it out. The changes here don't change anything of how the logic works though, it only returns errors where it's necessary, so no need to cover that in this PR.

@caglaryucekaya
Copy link

caglaryucekaya commented Jul 14, 2023

Yeah, I missed that. Thanks for pointing it out. The changes here don't change anything of how the logic works though, it only returns errors where it's necessary, so no need to cover that in this PR.

Sure, I approved the PR and opened an issue for this instead

caglaryucekaya
caglaryucekaya previously approved these changes Jul 14, 2023
rozhkovdmitrii
rozhkovdmitrii previously approved these changes Jul 17, 2023
Alrighttt
Alrighttt previously approved these changes Jul 17, 2023
Copy link
Member

@Alrighttt Alrighttt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only feedback is that we might want to consider logging the full tx hex instead of the txid. I didn't trace where this is actually used, but if it's possible we are dealing with unconfirmed transactions, we may end up logging a txid that we can't retrieve the full hex for at a later time.

@shamardy
Copy link
Collaborator Author

My only feedback is that we might want to consider logging the full tx hex instead of the txid.

I will add hex logging in addition to txid. Thanks for the feedback.

@shamardy shamardy merged commit 0dcaeea into dev Jul 18, 2023
@shamardy shamardy deleted the fix-tx-history-index-panic branch July 18, 2023 00:43
@smk762
Copy link

smk762 commented Aug 16, 2023

Another user reported same issue, details below

Reported by user jommy99 via discord on behalf of a Brazilian member of the PND community.

User experiences loss of connection to mm2 when activating PND. Issue persists in all current releases for all GUIs using 1.0.5 1d8bebd15 SDK.

mm2 log file ends with


· 2023-08-15 07:54:39 -0700 [tx_history coin=PND] utxo_common:2913] Error "utxo_common:3112] utxo_common:3241] UnexpectedEnd, tx: 0f64cb2d216fb1be996a83283a2ae07ca236578fe45f7e61be0dce390f192211" on getting the details of 904b5d62dad92563b7ecd7c8fdf97c0e0d622e5cf2afbe5a658bad580ae4dff8, skipping the tx
15 14:54:39, common:500] panicked at 'index out of bounds: the len is 1 but the index is 1', mm2src\coins\utxo\utxo_common.rs:3119:29
15 14:54:39, common:501] backtrace
  ??:0] BaseThreadInitThunk 0x7fff0a4784d3
  ??:0] RtlUserThreadStart 0x7fff0cdc1790

Transaction mentioned in logs do not appear to be "exotic"
https://chainz.cryptoid.info/pnd/api.dws?q=txinfo&t=904b5d62dad92563b7ecd7c8fdf97c0e0d622e5cf2afbe5a658bad580ae4dff8
https://chainz.cryptoid.info/pnd/api.dws?q=txinfo&t=0f64cb2d216fb1be996a83283a2ae07ca236578fe45f7e61be0dce390f192211

** Full Logs:**
2023-08-15-14-53-48.log
2023-08-15-14-54-34.mm2.log

A test branch in legacy desktop is building at https://github.com/KomodoPlatform/komodo-wallet-desktop/actions/runs/5876114420 to confirm the fix

@shamardy
Copy link
Collaborator Author

Transaction mentioned in logs do not appear to be "exotic"
https://chainz.cryptoid.info/pnd/api.dws?q=txinfo&t=904b5d62dad92563b7ecd7c8fdf97c0e0d622e5cf2afbe5a658bad580ae4dff8
https://chainz.cryptoid.info/pnd/api.dws?q=txinfo&t=0f64cb2d216fb1be996a83283a2ae07ca236578fe45f7e61be0dce390f192211

These transactions are not the one that caused index out of bounds but the one after them, 1.0.6 will log the one that caused the panic though without causing panics, it will also not be included in history same as the above transactions. But this is still good info since now I know one of the coins that have problems with transaction deserialization and some examples of transactions that couldn't be deserialized properly.
P.S. the one that caused the panic will not be exotic too, due to deserialization problems the output index is getting messed up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Panic happens with index out of bounds in utxo_common::tx_details_by_hash
5 participants