@@ -678,27 +678,27 @@ let revalidate :
678
678
-> [ `Entire_pool | `Subset of Account_id.Set. t ]
679
679
-> (Account_id. t -> Account. t )
680
680
-> t * Transaction_hash.User_command_with_valid_signature. t Sequence. t =
681
- fun t ~logger scope f ->
681
+ fun t_initial ~logger scope f ->
682
682
let requires_revalidation =
683
683
match scope with
684
684
| `Entire_pool ->
685
685
Fn. const true
686
686
| `Subset subset ->
687
687
Set. mem subset
688
688
in
689
- Map. fold t .all_by_sender ~init: (t , Sequence. empty)
689
+ Map. fold t_initial .all_by_sender ~init: (t_initial , Sequence. empty)
690
690
~f: (fun
691
691
~key :sender
692
692
~data :(queue , currency_reserved )
693
- ((t' , dropped_acc ) as acc )
693
+ ((t , dropped_acc ) as acc )
694
694
->
695
695
if not (requires_revalidation sender) then acc
696
696
else
697
697
let account : Account.t = f sender in
698
698
let current_balance =
699
699
Currency.Balance. to_amount
700
700
(Account. liquid_balance_at_slot
701
- ~global_slot: (global_slot_since_genesis t .config)
701
+ ~global_slot: (global_slot_since_genesis t_initial .config)
702
702
account )
703
703
in
704
704
[% log debug]
@@ -724,14 +724,14 @@ let revalidate :
724
724
then (
725
725
[% log debug]
726
726
" Account no longer has permission to send; dropping queue" ;
727
- let dropped, t'' = remove_with_dependents_exn' t first_cmd in
728
- (t'' , Sequence. append dropped_acc dropped) )
727
+ let dropped, t_updated = remove_with_dependents_exn' t first_cmd in
728
+ (t_updated , Sequence. append dropped_acc dropped) )
729
729
else if Account_nonce. (account.nonce < first_nonce) then (
730
730
[% log debug]
731
731
" Current account nonce precedes first nonce in queue; dropping \
732
732
queue" ;
733
- let dropped, t'' = remove_with_dependents_exn' t first_cmd in
734
- (t'' , Sequence. append dropped_acc dropped) )
733
+ let dropped, t_updated = remove_with_dependents_exn' t first_cmd in
734
+ (t_updated , Sequence. append dropped_acc dropped) )
735
735
else
736
736
(* current_nonce >= first_nonce *)
737
737
let first_applicable_nonce_index =
@@ -748,62 +748,73 @@ let revalidate :
748
748
" Current account nonce succeeds first nonce in queue; splitting \
749
749
queue at $index"
750
750
~metadata: [ (" index" , `Int first_applicable_nonce_index) ] ;
751
- let drop_queue, keep_queue =
751
+ let dropped_for_nonce, retained_for_nonce =
752
752
F_sequence. split_at queue first_applicable_nonce_index
753
753
in
754
- let currency_reserved' =
754
+ let currency_reserved_partially_updated =
755
755
F_sequence. foldl
756
756
(fun c cmd ->
757
757
Option. value_exn
758
758
Currency.Amount. (c - Option. value_exn (currency_consumed cmd))
759
759
)
760
- currency_reserved drop_queue
760
+ currency_reserved dropped_for_nonce
761
761
in
762
- let keep_queue', currency_reserved'' , dropped_for_balance =
762
+ let keep_queue, currency_reserved_updated , dropped_for_balance =
763
763
drop_until_sufficient_balance
764
- (keep_queue, currency_reserved' )
764
+ (retained_for_nonce, currency_reserved_partially_updated )
765
765
current_balance
766
766
in
767
767
let to_drop =
768
- Sequence. append (F_sequence. to_seq drop_queue) dropped_for_balance
768
+ Sequence. append
769
+ (F_sequence. to_seq dropped_for_nonce)
770
+ dropped_for_balance
769
771
in
770
- match Sequence. next to_drop with
771
- | None ->
772
- acc
773
- | Some (head , tail ) ->
774
- let t'' =
775
- Sequence. fold tail
776
- ~init:
777
- (remove_all_by_fee_and_hash_and_expiration_exn
778
- (remove_applicable_exn t' head)
779
- head )
780
- ~f: remove_all_by_fee_and_hash_and_expiration_exn
781
- in
782
- let t''' =
783
- match F_sequence. uncons keep_queue' with
784
- | None ->
785
- { t'' with
786
- all_by_sender = Map. remove t''.all_by_sender sender
787
- }
788
- | Some (first_kept , _ ) ->
789
- let first_kept_unchecked =
790
- Transaction_hash.User_command_with_valid_signature. command
791
- first_kept
792
- in
793
- { t'' with
794
- all_by_sender =
795
- Map. set t''.all_by_sender ~key: sender
796
- ~data: (keep_queue', currency_reserved'')
797
- ; applicable_by_fee =
798
- Map_set. insert
799
- ( module Transaction_hash
800
- .User_command_with_valid_signature )
801
- t''.applicable_by_fee
802
- (User_command. fee_per_wu first_kept_unchecked)
803
- first_kept
804
- }
805
- in
806
- (t''' , Sequence. append dropped_acc to_drop) )
772
+ let keeping_prefix = F_sequence. is_empty dropped_for_nonce in
773
+ let keeping_suffix = Sequence. is_empty dropped_for_balance in
774
+ (* t with all_by_sender and applicable_by_fee fields updated *)
775
+ let t_partially_updated =
776
+ match F_sequence. uncons keep_queue with
777
+ | _ when keeping_prefix && keeping_suffix ->
778
+ (* Nothing dropped, nothing needs to be updated *)
779
+ t
780
+ | None ->
781
+ (* We drop the entire queue, first element needs to be removed from
782
+ applicable_by_fee *)
783
+ let t' = remove_applicable_exn t first_cmd in
784
+ { t' with all_by_sender = Map. remove t'.all_by_sender sender }
785
+ | Some _ when keeping_prefix ->
786
+ (* We drop only transactions from the end of queue, keeping
787
+ the head untouched, no need to update applicable_by_fee *)
788
+ { t with
789
+ all_by_sender =
790
+ Map. set t.all_by_sender ~key: sender
791
+ ~data: (keep_queue, currency_reserved_updated)
792
+ }
793
+ | Some (first_kept , _ ) ->
794
+ (* We need to replace old queue head with the new queue head
795
+ in applicable_by_fee *)
796
+ let first_kept_unchecked =
797
+ Transaction_hash.User_command_with_valid_signature. command
798
+ first_kept
799
+ in
800
+ let t' = remove_applicable_exn t first_cmd in
801
+ { t' with
802
+ all_by_sender =
803
+ Map. set t'.all_by_sender ~key: sender
804
+ ~data: (keep_queue, currency_reserved_updated)
805
+ ; applicable_by_fee =
806
+ Map_set. insert
807
+ (module Transaction_hash. User_command_with_valid_signature )
808
+ t'.applicable_by_fee
809
+ (User_command. fee_per_wu first_kept_unchecked)
810
+ first_kept
811
+ }
812
+ in
813
+ let t_updated =
814
+ Sequence. fold ~init: t_partially_updated
815
+ ~f: remove_all_by_fee_and_hash_and_expiration_exn to_drop
816
+ in
817
+ (t_updated, Sequence. append dropped_acc to_drop) )
807
818
808
819
let expired_by_global_slot (t : t ) :
809
820
Transaction_hash.User_command_with_valid_signature. t Sequence. t =
0 commit comments