Skip to content

Commit 83b515a

Browse files
committed
Fix comments
1 parent 529d6bf commit 83b515a

File tree

3 files changed

+57
-47
lines changed

3 files changed

+57
-47
lines changed

wallet/src/account/output_cache/mod.rs

+33-29
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ impl OutputCache {
698698
match input {
699699
TxInput::Utxo(_) => {
700700
//TODO: check conflicting utxo spends
701+
// See https://github.com/mintlayer/mintlayer-core/issues/1875
701702
None
702703
}
703704
TxInput::Account(outpoint) => Some(Conflict {
@@ -731,26 +732,24 @@ impl OutputCache {
731732
for unconfirmed in self.unconfirmed_descendants.keys() {
732733
let unconfirmed_tx = self.txs.get(unconfirmed).expect("must be present");
733734

734-
if let Some(frozen_token_id) = conflict.frozen_token_id {
735-
if self.uses_token(unconfirmed_tx, &frozen_token_id) {
736-
if let WalletTx::Tx(tx) = unconfirmed_tx {
735+
if let WalletTx::Tx(tx) = unconfirmed_tx {
736+
if let Some(frozen_token_id) = conflict.frozen_token_id {
737+
if self.uses_token(unconfirmed_tx, &frozen_token_id) {
737738
conflicting_txs.insert(tx.get_transaction().get_id());
738739
}
739740
}
740-
}
741741

742-
if let Some((confirmed_account, confirmed_account_nonce)) =
743-
conflict.confirmed_account_nonce
744-
{
745-
if uses_conflicting_nonce(
746-
unconfirmed_tx,
747-
confirmed_account,
748-
confirmed_account_nonce,
749-
) {
750-
if let WalletTx::Tx(tx) = unconfirmed_tx {
751-
if confirmed_tx.get_id() != tx.get_transaction().get_id() {
752-
conflicting_txs.insert(tx.get_transaction().get_id());
753-
}
742+
if let Some((confirmed_account, confirmed_account_nonce)) =
743+
conflict.confirmed_account_nonce
744+
{
745+
if confirmed_tx.get_id() != tx.get_transaction().get_id()
746+
&& uses_conflicting_nonce(
747+
unconfirmed_tx,
748+
confirmed_account,
749+
confirmed_account_nonce,
750+
)
751+
{
752+
conflicting_txs.insert(tx.get_transaction().get_id());
754753
}
755754
}
756755
}
@@ -761,15 +760,17 @@ impl OutputCache {
761760
let mut conflicting_txs_with_descendants = vec![];
762761

763762
for conflicting_tx in conflicting_txs {
764-
let descendants = self.remove_descendants(conflicting_tx);
763+
let txs_to_rollback = self.remove_from_unconfirmed_descendants(conflicting_tx);
765764

766765
// Mark conflicting tx and its descendants as Conflicting and update OutputCache data accordingly
767-
for tx_id in descendants.iter().rev().copied() {
766+
for tx_id in txs_to_rollback.iter().rev().copied() {
768767
match self.txs.entry(tx_id.into()) {
769768
Entry::Occupied(mut entry) => match entry.get_mut() {
770-
WalletTx::Block(_) => Err(WalletError::CannotFindTransactionWithId(tx_id)),
769+
WalletTx::Block(_) => {
770+
Err(WalletError::TransactionIdCannotMapToBlock(tx_id))
771+
}
771772
WalletTx::Tx(tx) => match tx.state() {
772-
TxState::Inactive(_) => {
773+
TxState::Inactive(_) | TxState::InMempool(_) => {
773774
tx.set_state(TxState::Conflicted(block_id));
774775
OutputCache::rollback_tx_data(
775776
tx,
@@ -783,20 +784,18 @@ impl OutputCache {
783784
}
784785
TxState::Abandoned
785786
| TxState::Confirmed(..)
786-
| TxState::InMempool(..)
787787
| TxState::Conflicted(..) => {
788-
Err(WalletError::CannotChangeTransactionState(
789-
*tx.state(),
790-
TxState::Conflicted(block_id),
791-
))
788+
Err(WalletError::CannotMarkTxAsConflictedIfInState(*tx.state()))
792789
}
793790
},
794791
},
795-
Entry::Vacant(_) => Err(WalletError::CannotFindTransactionWithId(tx_id)),
792+
Entry::Vacant(_) => {
793+
Err(WalletError::CannotFindDescendantTransactionWithId(tx_id))
794+
}
796795
}?;
797796
}
798797

799-
conflicting_txs_with_descendants.extend(descendants.into_iter());
798+
conflicting_txs_with_descendants.extend(txs_to_rollback.into_iter());
800799
}
801800

802801
Ok(conflicting_txs_with_descendants)
@@ -1441,7 +1440,12 @@ impl OutputCache {
14411440
})
14421441
}
14431442

1444-
fn remove_descendants(&mut self, tx_id: Id<Transaction>) -> Vec<Id<Transaction>> {
1443+
// Removes a tx from unconfirmed descendant.
1444+
// Returns provided tx and all the descendants.
1445+
fn remove_from_unconfirmed_descendants(
1446+
&mut self,
1447+
tx_id: Id<Transaction>,
1448+
) -> Vec<Id<Transaction>> {
14451449
let mut all_txs = Vec::new();
14461450
let mut to_update = BTreeSet::from_iter([OutPointSourceId::from(tx_id)]);
14471451

@@ -1511,7 +1515,7 @@ impl OutputCache {
15111515
&mut self,
15121516
tx_id: Id<Transaction>,
15131517
) -> WalletResult<Vec<Id<Transaction>>> {
1514-
let all_abandoned = self.remove_descendants(tx_id);
1518+
let all_abandoned = self.remove_from_unconfirmed_descendants(tx_id);
15151519

15161520
for tx_id in all_abandoned.iter().rev().copied() {
15171521
match self.txs.entry(tx_id.into()) {

wallet/src/wallet/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,14 @@ pub enum WalletError {
177177
CoinSelectionError(#[from] UtxoSelectorError),
178178
#[error("Cannot change a transaction's state from {0} to {1}")]
179179
CannotChangeTransactionState(TxState, TxState),
180+
#[error("Cannot mark as conflicted transaction in {0} state")]
181+
CannotMarkTxAsConflictedIfInState(TxState),
180182
#[error("Transaction with Id {0} not found")]
181183
CannotFindTransactionWithId(Id<Transaction>),
184+
#[error("Transaction with Id {0} cannot map to a block")]
185+
TransactionIdCannotMapToBlock(Id<Transaction>),
186+
#[error("Descendant transaction with Id {0} not found")]
187+
CannotFindDescendantTransactionWithId(Id<Transaction>),
182188
#[error("Address error: {0}")]
183189
AddressError(#[from] AddressError),
184190
#[error("Unknown pool id {0}")]

wallet/src/wallet/tests.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -3481,17 +3481,17 @@ fn lock_then_transfer(#[case] seed: Seed) {
34813481
scan_wallet(&mut wallet, BlockHeight::new(1), vec![block2]);
34823482

34833483
// check balance
3484-
let balance_without_locked_transer =
3484+
let balance_without_locked_transfer =
34853485
((block1_amount * 2).unwrap() - amount_to_lock_then_transfer).unwrap();
34863486

34873487
let coin_balance = get_coin_balance(&wallet);
3488-
assert_eq!(coin_balance, balance_without_locked_transer);
3488+
assert_eq!(coin_balance, balance_without_locked_transfer);
34893489

34903490
// check that for block_count_lock, the amount is not included
34913491
for idx in 0..block_count_lock {
34923492
let coin_balance = get_coin_balance(&wallet);
34933493
// check that the amount is still not unlocked
3494-
assert_eq!(coin_balance, balance_without_locked_transer);
3494+
assert_eq!(coin_balance, balance_without_locked_transfer);
34953495

34963496
let currency_balances = wallet
34973497
.get_balance(
@@ -3524,7 +3524,7 @@ fn lock_then_transfer(#[case] seed: Seed) {
35243524
let coin_balance = get_coin_balance(&wallet);
35253525
assert_eq!(
35263526
coin_balance,
3527-
(balance_without_locked_transer + amount_to_lock_then_transfer).unwrap()
3527+
(balance_without_locked_transfer + amount_to_lock_then_transfer).unwrap()
35283528
);
35293529
}
35303530

@@ -3882,15 +3882,15 @@ fn wallet_abandone_transactions(#[case] seed: Seed) {
38823882

38833883
assert_eq!(abandonable_transactions.len(), transactions.len());
38843884

3885-
let txs_to_abandone = rng.gen_range(0..total_num_transactions) as usize;
3886-
let (txs_to_keep, txs_to_abandone) = transactions.split_at(txs_to_abandone);
3885+
let txs_to_abandon = rng.gen_range(0..total_num_transactions) as usize;
3886+
let (txs_to_keep, txs_to_abandon) = transactions.split_at(txs_to_abandon);
38873887

3888-
assert!(!txs_to_abandone.is_empty());
3888+
assert!(!txs_to_abandon.is_empty());
38893889

3890-
let transaction_id = txs_to_abandone.first().unwrap().0.transaction().get_id();
3890+
let transaction_id = txs_to_abandon.first().unwrap().0.transaction().get_id();
38913891
wallet.abandon_transaction(DEFAULT_ACCOUNT_INDEX, transaction_id).unwrap();
38923892

3893-
let coins_after_abandon = txs_to_abandone.first().unwrap().1;
3893+
let coins_after_abandon = txs_to_abandon.first().unwrap().1;
38943894

38953895
let coin_balance = get_coin_balance_with_inactive(&wallet);
38963896
assert_eq!(coin_balance, coins_after_abandon);
@@ -3900,7 +3900,7 @@ fn wallet_abandone_transactions(#[case] seed: Seed) {
39003900
let coin_balance = get_coin_balance_with_inactive(&wallet);
39013901
assert_eq!(coin_balance, coins_after_abandon);
39023902

3903-
// Abandone the same tx again
3903+
// Abandon the same tx again
39043904
let result = wallet.abandon_transaction(DEFAULT_ACCOUNT_INDEX, transaction_id);
39053905
assert_eq!(
39063906
result.unwrap_err(),
@@ -6195,7 +6195,7 @@ fn conflicting_delegation_account_nonce(#[case] seed: Seed) {
61956195
)
61966196
);
61976197

6198-
// Abandone conflicting txs
6198+
// Abandon conflicting txs
61996199
wallet
62006200
.abandon_transaction(DEFAULT_ACCOUNT_INDEX, spend_from_delegation_tx_1_id)
62016201
.unwrap();
@@ -6455,9 +6455,9 @@ fn conflicting_delegation_account_nonce_same_tx(#[case] seed: Seed) {
64556455
);
64566456
}
64576457

6458-
// Issue and mint some tokens
6459-
// Create an order selling tokens for coins
6460-
// Create 2 fill order txs and add them to a wallet as unconfirmed
6458+
// Issue and mint some tokens.
6459+
// Create an order selling tokens for coins.
6460+
// Create 2 fill order txs and add them to a wallet as unconfirmed.
64616461
// Confirm the first tx in a block and check that it is accounted in confirmed balance
64626462
// and also that unconfirmed balance has second tx.
64636463
#[rstest]
@@ -6589,7 +6589,7 @@ fn conflicting_order_account_nonce(#[case] seed: Seed) {
65896589
};
65906590

65916591
let spend_coins_1 = Amount::from_atoms(10);
6592-
let recieved_tokens_1 = spend_coins_1;
6592+
let received_tokens_1 = spend_coins_1;
65936593
let fill_order_tx_1 = wallet
65946594
.create_fill_order_tx(
65956595
DEFAULT_ACCOUNT_INDEX,
@@ -6625,7 +6625,7 @@ fn conflicting_order_account_nonce(#[case] seed: Seed) {
66256625
};
66266626

66276627
let spend_coins_2 = Amount::from_atoms(3);
6628-
let recieved_tokens_2 = spend_coins_2;
6628+
let received_tokens_2 = spend_coins_2;
66296629
let fill_order_tx_2 = wallet
66306630
.create_fill_order_tx(
66316631
DEFAULT_ACCOUNT_INDEX,
@@ -6698,7 +6698,7 @@ fn conflicting_order_account_nonce(#[case] seed: Seed) {
66986698
UtxoState::Confirmed.into(),
66996699
WithLocked::Any,
67006700
);
6701-
assert_eq!(token_balance_confirmed, recieved_tokens_1);
6701+
assert_eq!(token_balance_confirmed, received_tokens_1);
67026702

67036703
let token_balance_unconfirmed = get_balance_with(
67046704
&wallet,
@@ -6708,6 +6708,6 @@ fn conflicting_order_account_nonce(#[case] seed: Seed) {
67086708
);
67096709
assert_eq!(
67106710
token_balance_unconfirmed,
6711-
(recieved_tokens_1 + recieved_tokens_2).unwrap()
6711+
(received_tokens_1 + received_tokens_2).unwrap()
67126712
);
67136713
}

0 commit comments

Comments
 (0)