@@ -741,60 +741,66 @@ impl OutputCache {
741
741
confirmed_tx : & Transaction ,
742
742
block_id : Id < GenBlock > ,
743
743
) -> 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
+ }
746
748
747
- for input in confirmed_tx. inputs ( ) {
749
+ let conflict = confirmed_tx. inputs ( ) . iter ( ) . find_map ( |input| {
748
750
match input {
749
751
TxInput :: Utxo ( _) => {
750
752
//TODO: check conflicting utxo spends
753
+ None
751
754
}
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
+ } ,
771
776
}
772
- }
777
+ } ) ;
773
778
774
779
// Collect all conflicting txs
775
- let mut conflicting_txs = vec ! [ ] ;
780
+ let mut conflicting_txs = BTreeSet :: new ( ) ;
776
781
777
- if frozen_token_id . is_some ( ) || confirmed_account_nonce . is_some ( ) {
782
+ if let Some ( conflict ) = conflict {
778
783
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 {
781
787
if self . uses_token ( unconfirmed_tx, & frozen_token_id) {
782
788
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 ( ) ) ;
784
790
}
785
791
}
786
792
}
787
793
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
789
796
{
790
- let unconfirmed_tx = self . txs . get ( unconfirmed) . expect ( "must be present" ) ;
791
797
if uses_conflicting_nonce (
792
798
unconfirmed_tx,
793
799
confirmed_account,
794
800
confirmed_account_nonce,
795
801
) {
796
802
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 ( ) ) ;
798
804
}
799
805
}
800
806
}
@@ -884,7 +890,7 @@ impl OutputCache {
884
890
885
891
if is_unconfirmed && !already_present {
886
892
self . unconfirmed_descendants . insert ( tx_id. clone ( ) , BTreeSet :: new ( ) ) ;
887
- } else {
893
+ } else if !is_unconfirmed {
888
894
self . unconfirmed_descendants . remove ( & tx_id) ;
889
895
}
890
896
@@ -1003,11 +1009,11 @@ impl OutputCache {
1003
1009
if let Some ( descendants) =
1004
1010
self . unconfirmed_descendants . get_mut ( & outpoint. source_id ( ) )
1005
1011
{
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 ( ) ) ;
1011
1017
}
1012
1018
}
1013
1019
TxInput :: Account ( outpoint) => match outpoint. account ( ) {
0 commit comments