Skip to content

Commit 7ed77e0

Browse files
committed
Fix comments
1 parent e33b836 commit 7ed77e0

File tree

2 files changed

+45
-37
lines changed

2 files changed

+45
-37
lines changed

wallet/src/account/output_cache/mod.rs

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -741,60 +741,66 @@ impl OutputCache {
741741
confirmed_tx: &Transaction,
742742
block_id: Id<GenBlock>,
743743
) -> WalletResult<Vec<Id<Transaction>>> {
744-
let mut frozen_token_id: Option<TokenId> = None;
745-
let mut confirmed_account_nonce: Option<(AccountType, AccountNonce)> = None;
744+
struct Conflict {
745+
frozen_token_id: Option<TokenId>,
746+
confirmed_account_nonce: Option<(AccountType, AccountNonce)>,
747+
}
746748

747-
for input in confirmed_tx.inputs() {
749+
let conflict = confirmed_tx.inputs().iter().find_map(|input| {
748750
match input {
749751
TxInput::Utxo(_) => {
750752
//TODO: check conflicting utxo spends
753+
None
751754
}
752-
TxInput::Account(outpoint) => {
753-
confirmed_account_nonce = Some((outpoint.account().into(), outpoint.nonce()));
754-
}
755-
TxInput::AccountCommand(nonce, cmd) => {
756-
confirmed_account_nonce = Some((cmd.into(), *nonce));
757-
match cmd {
758-
AccountCommand::MintTokens(_, _)
759-
| AccountCommand::UnmintTokens(_)
760-
| AccountCommand::LockTokenSupply(_)
761-
| AccountCommand::ChangeTokenMetadataUri(_, _)
762-
| AccountCommand::ChangeTokenAuthority(_, _)
763-
| AccountCommand::UnfreezeToken(_)
764-
| AccountCommand::ConcludeOrder(_)
765-
| AccountCommand::FillOrder(_, _, _) => { /* do nothing */ }
766-
| AccountCommand::FreezeToken(token_id, _) => {
767-
frozen_token_id = Some(*token_id);
768-
}
769-
}
770-
}
755+
TxInput::Account(outpoint) => Some(Conflict {
756+
frozen_token_id: None,
757+
confirmed_account_nonce: Some((outpoint.account().into(), outpoint.nonce())),
758+
}),
759+
TxInput::AccountCommand(nonce, cmd) => match cmd {
760+
AccountCommand::MintTokens(_, _)
761+
| AccountCommand::UnmintTokens(_)
762+
| AccountCommand::LockTokenSupply(_)
763+
| AccountCommand::ChangeTokenMetadataUri(_, _)
764+
| AccountCommand::ChangeTokenAuthority(_, _)
765+
| AccountCommand::UnfreezeToken(_)
766+
| AccountCommand::ConcludeOrder(_)
767+
| AccountCommand::FillOrder(_, _, _) => Some(Conflict {
768+
frozen_token_id: None,
769+
confirmed_account_nonce: Some((cmd.into(), *nonce)),
770+
}),
771+
| AccountCommand::FreezeToken(token_id, _) => Some(Conflict {
772+
frozen_token_id: Some(*token_id),
773+
confirmed_account_nonce: Some((cmd.into(), *nonce)),
774+
}),
775+
},
771776
}
772-
}
777+
});
773778

774779
// Collect all conflicting txs
775-
let mut conflicting_txs = vec![];
780+
let mut conflicting_txs = BTreeSet::new();
776781

777-
if frozen_token_id.is_some() || confirmed_account_nonce.is_some() {
782+
if let Some(conflict) = conflict {
778783
for unconfirmed in self.unconfirmed_descendants.keys() {
779-
if let Some(frozen_token_id) = frozen_token_id {
780-
let unconfirmed_tx = self.txs.get(unconfirmed).expect("must be present");
784+
let unconfirmed_tx = self.txs.get(unconfirmed).expect("must be present");
785+
786+
if let Some(frozen_token_id) = conflict.frozen_token_id {
781787
if self.uses_token(unconfirmed_tx, &frozen_token_id) {
782788
if let WalletTx::Tx(tx) = unconfirmed_tx {
783-
conflicting_txs.push(tx.get_transaction().get_id());
789+
conflicting_txs.insert(tx.get_transaction().get_id());
784790
}
785791
}
786792
}
787793

788-
if let Some((confirmed_account, confirmed_account_nonce)) = confirmed_account_nonce
794+
if let Some((confirmed_account, confirmed_account_nonce)) =
795+
conflict.confirmed_account_nonce
789796
{
790-
let unconfirmed_tx = self.txs.get(unconfirmed).expect("must be present");
791797
if uses_conflicting_nonce(
792798
unconfirmed_tx,
793799
confirmed_account,
794800
confirmed_account_nonce,
795801
) {
796802
if let WalletTx::Tx(tx) = unconfirmed_tx {
797-
conflicting_txs.push(tx.get_transaction().get_id());
803+
conflicting_txs.insert(tx.get_transaction().get_id());
798804
}
799805
}
800806
}
@@ -884,7 +890,7 @@ impl OutputCache {
884890

885891
if is_unconfirmed && !already_present {
886892
self.unconfirmed_descendants.insert(tx_id.clone(), BTreeSet::new());
887-
} else {
893+
} else if !is_unconfirmed {
888894
self.unconfirmed_descendants.remove(&tx_id);
889895
}
890896

@@ -1003,11 +1009,11 @@ impl OutputCache {
10031009
if let Some(descendants) =
10041010
self.unconfirmed_descendants.get_mut(&outpoint.source_id())
10051011
{
1006-
if is_unconfirmed {
1007-
descendants.insert(tx_id.clone());
1008-
} else {
1009-
descendants.remove(tx_id);
1010-
}
1012+
ensure!(
1013+
is_unconfirmed,
1014+
WalletError::ConfirmedTxAmongUnconfirmedDescendants(tx_id.clone())
1015+
);
1016+
descendants.insert(tx_id.clone());
10111017
}
10121018
}
10131019
TxInput::Account(outpoint) => match outpoint.account() {

wallet/src/wallet/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ pub enum WalletError {
281281
MismatchedTokenAdditionalData(TokenId),
282282
#[error("Unsupported operation for a Hardware wallet")]
283283
UnsupportedHardwareWalletOperation,
284+
#[error("Transaction from {0:?} is confirmed and among unconfirmed descendants")]
285+
ConfirmedTxAmongUnconfirmedDescendants(OutPointSourceId),
284286
}
285287

286288
/// Result type used for the wallet

0 commit comments

Comments
 (0)