Skip to content

Commit aa3e06a

Browse files
committed
Fix comments
1 parent 3a3e4f4 commit aa3e06a

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
@@ -750,6 +750,7 @@ impl OutputCache {
750750
match input {
751751
TxInput::Utxo(_) => {
752752
//TODO: check conflicting utxo spends
753+
// See https://github.com/mintlayer/mintlayer-core/issues/1875
753754
None
754755
}
755756
TxInput::Account(outpoint) => Some(Conflict {
@@ -783,26 +784,24 @@ impl OutputCache {
783784
for unconfirmed in self.unconfirmed_descendants.keys() {
784785
let unconfirmed_tx = self.txs.get(unconfirmed).expect("must be present");
785786

786-
if let Some(frozen_token_id) = conflict.frozen_token_id {
787-
if self.uses_token(unconfirmed_tx, &frozen_token_id) {
788-
if let WalletTx::Tx(tx) = unconfirmed_tx {
787+
if let WalletTx::Tx(tx) = unconfirmed_tx {
788+
if let Some(frozen_token_id) = conflict.frozen_token_id {
789+
if self.uses_token(unconfirmed_tx, &frozen_token_id) {
789790
conflicting_txs.insert(tx.get_transaction().get_id());
790791
}
791792
}
792-
}
793793

794-
if let Some((confirmed_account, confirmed_account_nonce)) =
795-
conflict.confirmed_account_nonce
796-
{
797-
if uses_conflicting_nonce(
798-
unconfirmed_tx,
799-
confirmed_account,
800-
confirmed_account_nonce,
801-
) {
802-
if let WalletTx::Tx(tx) = unconfirmed_tx {
803-
if confirmed_tx.get_id() != tx.get_transaction().get_id() {
804-
conflicting_txs.insert(tx.get_transaction().get_id());
805-
}
794+
if let Some((confirmed_account, confirmed_account_nonce)) =
795+
conflict.confirmed_account_nonce
796+
{
797+
if confirmed_tx.get_id() != tx.get_transaction().get_id()
798+
&& uses_conflicting_nonce(
799+
unconfirmed_tx,
800+
confirmed_account,
801+
confirmed_account_nonce,
802+
)
803+
{
804+
conflicting_txs.insert(tx.get_transaction().get_id());
806805
}
807806
}
808807
}
@@ -813,15 +812,17 @@ impl OutputCache {
813812
let mut conflicting_txs_with_descendants = vec![];
814813

815814
for conflicting_tx in conflicting_txs {
816-
let descendants = self.remove_descendants(conflicting_tx);
815+
let txs_to_rollback = self.remove_from_unconfirmed_descendants(conflicting_tx);
817816

818817
// Mark conflicting tx and its descendants as Conflicting and update OutputCache data accordingly
819-
for tx_id in descendants.iter().rev().copied() {
818+
for tx_id in txs_to_rollback.iter().rev().copied() {
820819
match self.txs.entry(tx_id.into()) {
821820
Entry::Occupied(mut entry) => match entry.get_mut() {
822-
WalletTx::Block(_) => Err(WalletError::CannotFindTransactionWithId(tx_id)),
821+
WalletTx::Block(_) => {
822+
Err(WalletError::TransactionIdCannotMapToBlock(tx_id))
823+
}
823824
WalletTx::Tx(tx) => match tx.state() {
824-
TxState::Inactive(_) => {
825+
TxState::Inactive(_) | TxState::InMempool(_) => {
825826
tx.set_state(TxState::Conflicted(block_id));
826827
OutputCache::rollback_tx_data(
827828
tx,
@@ -835,20 +836,18 @@ impl OutputCache {
835836
}
836837
TxState::Abandoned
837838
| TxState::Confirmed(..)
838-
| TxState::InMempool(..)
839839
| TxState::Conflicted(..) => {
840-
Err(WalletError::CannotChangeTransactionState(
841-
*tx.state(),
842-
TxState::Conflicted(block_id),
843-
))
840+
Err(WalletError::CannotMarkTxAsConflictedIfInState(*tx.state()))
844841
}
845842
},
846843
},
847-
Entry::Vacant(_) => Err(WalletError::CannotFindTransactionWithId(tx_id)),
844+
Entry::Vacant(_) => {
845+
Err(WalletError::CannotFindDescendantTransactionWithId(tx_id))
846+
}
848847
}?;
849848
}
850849

851-
conflicting_txs_with_descendants.extend(descendants.into_iter());
850+
conflicting_txs_with_descendants.extend(txs_to_rollback.into_iter());
852851
}
853852

854853
Ok(conflicting_txs_with_descendants)
@@ -1478,7 +1477,12 @@ impl OutputCache {
14781477
})
14791478
}
14801479

1481-
fn remove_descendants(&mut self, tx_id: Id<Transaction>) -> Vec<Id<Transaction>> {
1480+
// Removes a tx from unconfirmed descendant.
1481+
// Returns provided tx and all the descendants.
1482+
fn remove_from_unconfirmed_descendants(
1483+
&mut self,
1484+
tx_id: Id<Transaction>,
1485+
) -> Vec<Id<Transaction>> {
14821486
let mut all_txs = Vec::new();
14831487
let mut to_update = BTreeSet::from_iter([OutPointSourceId::from(tx_id)]);
14841488

@@ -1548,7 +1552,7 @@ impl OutputCache {
15481552
&mut self,
15491553
tx_id: Id<Transaction>,
15501554
) -> WalletResult<Vec<Id<Transaction>>> {
1551-
let all_abandoned = self.remove_descendants(tx_id);
1555+
let all_abandoned = self.remove_from_unconfirmed_descendants(tx_id);
15521556

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

wallet/src/wallet/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,14 @@ pub enum WalletError {
180180
CoinSelectionError(#[from] UtxoSelectorError),
181181
#[error("Cannot change a transaction's state from {0} to {1}")]
182182
CannotChangeTransactionState(TxState, TxState),
183+
#[error("Cannot mark as conflicted transaction in {0} state")]
184+
CannotMarkTxAsConflictedIfInState(TxState),
183185
#[error("Transaction with Id {0} not found")]
184186
CannotFindTransactionWithId(Id<Transaction>),
187+
#[error("Transaction with Id {0} cannot map to a block")]
188+
TransactionIdCannotMapToBlock(Id<Transaction>),
189+
#[error("Descendant transaction with Id {0} not found")]
190+
CannotFindDescendantTransactionWithId(Id<Transaction>),
185191
#[error("Address error: {0}")]
186192
AddressError(#[from] AddressError),
187193
#[error("Unknown pool id {0}")]

wallet/src/wallet/tests.rs

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

39373937
// check balance
3938-
let balance_without_locked_transer =
3938+
let balance_without_locked_transfer =
39393939
((block1_amount * 2).unwrap() - amount_to_lock_then_transfer).unwrap();
39403940

39413941
let coin_balance = get_coin_balance(&wallet);
3942-
assert_eq!(coin_balance, balance_without_locked_transer);
3942+
assert_eq!(coin_balance, balance_without_locked_transfer);
39433943

39443944
// check that for block_count_lock, the amount is not included
39453945
for idx in 0..block_count_lock {
39463946
let coin_balance = get_coin_balance(&wallet);
39473947
// check that the amount is still not unlocked
3948-
assert_eq!(coin_balance, balance_without_locked_transer);
3948+
assert_eq!(coin_balance, balance_without_locked_transfer);
39493949

39503950
let currency_balances = wallet
39513951
.get_balance(
@@ -3978,7 +3978,7 @@ fn lock_then_transfer(#[case] seed: Seed) {
39783978
let coin_balance = get_coin_balance(&wallet);
39793979
assert_eq!(
39803980
coin_balance,
3981-
(balance_without_locked_transer + amount_to_lock_then_transfer).unwrap()
3981+
(balance_without_locked_transfer + amount_to_lock_then_transfer).unwrap()
39823982
);
39833983
}
39843984

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

43284328
assert_eq!(abandonable_transactions.len(), transactions.len());
43294329

4330-
let txs_to_abandone = rng.gen_range(0..total_num_transactions) as usize;
4331-
let (txs_to_keep, txs_to_abandone) = transactions.split_at(txs_to_abandone);
4330+
let txs_to_abandon = rng.gen_range(0..total_num_transactions) as usize;
4331+
let (txs_to_keep, txs_to_abandon) = transactions.split_at(txs_to_abandon);
43324332

4333-
assert!(!txs_to_abandone.is_empty());
4333+
assert!(!txs_to_abandon.is_empty());
43344334

4335-
let transaction_id = txs_to_abandone.first().unwrap().0.transaction().get_id();
4335+
let transaction_id = txs_to_abandon.first().unwrap().0.transaction().get_id();
43364336
wallet.abandon_transaction(DEFAULT_ACCOUNT_INDEX, transaction_id).unwrap();
43374337

4338-
let coins_after_abandon = txs_to_abandone.first().unwrap().1;
4338+
let coins_after_abandon = txs_to_abandon.first().unwrap().1;
43394339

43404340
let coin_balance = get_coin_balance_with_inactive(&wallet);
43414341
assert_eq!(coin_balance, coins_after_abandon);
@@ -4345,7 +4345,7 @@ fn wallet_abandone_transactions(#[case] seed: Seed) {
43454345
let coin_balance = get_coin_balance_with_inactive(&wallet);
43464346
assert_eq!(coin_balance, coins_after_abandon);
43474347

4348-
// Abandone the same tx again
4348+
// Abandon the same tx again
43494349
let result = wallet.abandon_transaction(DEFAULT_ACCOUNT_INDEX, transaction_id);
43504350
assert_eq!(
43514351
result.unwrap_err(),
@@ -6728,7 +6728,7 @@ fn conflicting_delegation_account_nonce(#[case] seed: Seed) {
67286728
)
67296729
);
67306730

6731-
// Abandone conflicting txs
6731+
// Abandon conflicting txs
67326732
wallet
67336733
.abandon_transaction(DEFAULT_ACCOUNT_INDEX, spend_from_delegation_tx_1_id)
67346734
.unwrap();
@@ -6991,9 +6991,9 @@ fn conflicting_delegation_account_nonce_same_tx(#[case] seed: Seed) {
69916991
);
69926992
}
69936993

6994-
// Issue and mint some tokens
6995-
// Create an order selling tokens for coins
6996-
// Create 2 fill order txs and add them to a wallet as unconfirmed
6994+
// Issue and mint some tokens.
6995+
// Create an order selling tokens for coins.
6996+
// Create 2 fill order txs and add them to a wallet as unconfirmed.
69976997
// Confirm the first tx in a block and check that it is accounted in confirmed balance
69986998
// and also that unconfirmed balance has second tx.
69996999
#[rstest]
@@ -7126,7 +7126,7 @@ fn conflicting_order_account_nonce(#[case] seed: Seed) {
71267126
};
71277127

71287128
let spend_coins_1 = Amount::from_atoms(10);
7129-
let recieved_tokens_1 = spend_coins_1;
7129+
let received_tokens_1 = spend_coins_1;
71307130
let fill_order_tx_1 = wallet
71317131
.create_fill_order_tx(
71327132
DEFAULT_ACCOUNT_INDEX,
@@ -7163,7 +7163,7 @@ fn conflicting_order_account_nonce(#[case] seed: Seed) {
71637163
};
71647164

71657165
let spend_coins_2 = Amount::from_atoms(3);
7166-
let recieved_tokens_2 = spend_coins_2;
7166+
let received_tokens_2 = spend_coins_2;
71677167
let fill_order_tx_2 = wallet
71687168
.create_fill_order_tx(
71697169
DEFAULT_ACCOUNT_INDEX,
@@ -7237,7 +7237,7 @@ fn conflicting_order_account_nonce(#[case] seed: Seed) {
72377237
UtxoState::Confirmed.into(),
72387238
WithLocked::Any,
72397239
);
7240-
assert_eq!(token_balance_confirmed, recieved_tokens_1);
7240+
assert_eq!(token_balance_confirmed, received_tokens_1);
72417241

72427242
let token_balance_unconfirmed = get_balance_with(
72437243
&wallet,
@@ -7247,6 +7247,6 @@ fn conflicting_order_account_nonce(#[case] seed: Seed) {
72477247
);
72487248
assert_eq!(
72497249
token_balance_unconfirmed,
7250-
(recieved_tokens_1 + recieved_tokens_2).unwrap()
7250+
(received_tokens_1 + received_tokens_2).unwrap()
72517251
);
72527252
}

0 commit comments

Comments
 (0)