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

Wallet handle txs with conflicting account nonces #1864

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

azarovh
Copy link
Member

@azarovh azarovh commented Jan 15, 2025

Unconfirmed txs in the wallet which uses outdated nonce are marked as conflicted now if confirmed tx is encountered on mainchain and the state of OutputCache is updated accordingly.

The behaviour is similar to abandoning tx.

Besides tests I had a wallet with conflicting orders which I couldn't open previously because of InconsistentOrderDuplicateNonce which is now resolved.

@azarovh azarovh marked this pull request as draft January 15, 2025 14:33
wallet/src/account/output_cache/mod.rs Outdated Show resolved Hide resolved
wallet/src/wallet/tests.rs Show resolved Hide resolved
@azarovh azarovh force-pushed the fix/wallet_remove_conflicting_txs branch from bfbe582 to 3a2fd08 Compare January 21, 2025 08:54
@azarovh azarovh force-pushed the fix/wallet_remove_conflicting_txs branch from 3a2fd08 to 6c83ac1 Compare January 21, 2025 09:11
@azarovh azarovh marked this pull request as ready for review January 21, 2025 09:17
@azarovh azarovh requested a review from OBorce January 21, 2025 09:17
wallet/src/account/output_cache/mod.rs Outdated Show resolved Hide resolved
wallet/src/account/output_cache/mod.rs Outdated Show resolved Hide resolved

conflicting_txs.push(unconfirmed);
if let Some((confirmed_account, confirmed_account_nonce)) = confirmed_account_nonce
Copy link
Contributor

Choose a reason for hiding this comment

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

this can probably be an else if, as the transactions that use a token id will be a superset of the transactions that use the token id and have a specific nonce.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that it'd make the code more confusing especially when modifying in the future. I replaced the vec with set to avoid duplicates.

wallet/src/account/output_cache/mod.rs Outdated Show resolved Hide resolved
wallet/src/account/output_cache/mod.rs Outdated Show resolved Hide resolved
wallet/src/wallet/tests.rs Outdated Show resolved Hide resolved
wallet/src/wallet/tests.rs Outdated Show resolved Hide resolved
wallet/src/wallet/tests.rs Outdated Show resolved Hide resolved
wallet/src/wallet/tests.rs Outdated Show resolved Hide resolved
wallet/src/wallet/tests.rs Outdated Show resolved Hide resolved
wallet/src/wallet/tests.rs Show resolved Hide resolved
wallet/src/account/output_cache/mod.rs Outdated Show resolved Hide resolved
wallet/src/account/output_cache/mod.rs Outdated Show resolved Hide resolved
wallet/src/account/output_cache/mod.rs Show resolved Hide resolved
@azarovh azarovh force-pushed the fix/wallet_remove_conflicting_txs branch from 8be3bb5 to 3327b22 Compare January 29, 2025 09:56
@azarovh
Copy link
Member Author

azarovh commented Jan 30, 2025

Fixed the problem when abandoning a tx that is already marked as conflicted leads to invalid state of internal fields of OutputCache (like account nonces).

wallet/src/wallet/tests.rs Outdated Show resolved Hide resolved
wallet/src/wallet/tests.rs Outdated Show resolved Hide resolved
wallet/src/account/output_cache/mod.rs Outdated Show resolved Hide resolved
wallet/src/account/output_cache/mod.rs Outdated Show resolved Hide resolved
wallet/src/account/output_cache/mod.rs Outdated Show resolved Hide resolved
wallet/src/account/output_cache/mod.rs Outdated Show resolved Hide resolved
}
},
},
Entry::Vacant(_) => Err(WalletError::CannotFindTransactionWithId(tx_id)),
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this also an invariant? If so, it deserves its own error.

But I also see that remove_tx removes the tx from txs unconditionally, possibly leaving it inside unconfirmed_descendants. If this situation is legitimate, we probably shouldn't return an error here.

Copy link
Member Author

Choose a reason for hiding this comment

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

To me it looks like an invariant. Because removing a tx removes its descendants as well.

Comment on lines 784 to 792
TxState::Abandoned
| TxState::Confirmed(..)
| TxState::InMempool(..)
| TxState::Conflicted(..) => {
Err(WalletError::CannotChangeTransactionState(
*tx.state(),
TxState::Conflicted(block_id),
))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't look nice. This whole function is called during block processing, so if this situation happens, the wallet will stop working, right?

Abandoned, Confirmed and Conflicted probably can't happen here and are invariant violations, am I right? If so, a separate error would be nice.
But what about InMempool? It sounds like it should be possible. If so, we must handle it somehow. Would it be wrong to change the state from InMempool to Conflicted?

Copy link
Member Author

@azarovh azarovh Feb 3, 2025

Choose a reason for hiding this comment

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

Looks like InMempool -> Conflicted is theoretically possible. Changed the logic to mark it as conflicted.

wallet/src/account/output_cache/mod.rs Show resolved Hide resolved
wallet/src/wallet/tests.rs Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants