@@ -693,27 +693,27 @@ let revalidate :
693
693
-> [ `Entire_pool | `Subset of Account_id.Set. t ]
694
694
-> (Account_id. t -> Account. t )
695
695
-> t * Transaction_hash.User_command_with_valid_signature. t Sequence. t =
696
- fun ({ config = { constraint_constants; _ } ; _ } as t ) ~logger scope f ->
696
+ fun ({ config = { constraint_constants; _ } ; _ } as t_initial ) ~logger scope f ->
697
697
let requires_revalidation =
698
698
match scope with
699
699
| `Entire_pool ->
700
700
Fn. const true
701
701
| `Subset subset ->
702
702
Set. mem subset
703
703
in
704
- Map. fold t .all_by_sender ~init: (t , Sequence. empty)
704
+ Map. fold t_initial .all_by_sender ~init: (t_initial , Sequence. empty)
705
705
~f: (fun
706
706
~key :sender
707
707
~data :(queue , currency_reserved )
708
- ((t' , dropped_acc ) as acc )
708
+ ((t , dropped_acc ) as acc )
709
709
->
710
710
if not (requires_revalidation sender) then acc
711
711
else
712
712
let account : Account.t = f sender in
713
713
let current_balance =
714
714
Currency.Balance. to_amount
715
715
(Account. liquid_balance_at_slot
716
- ~global_slot: (global_slot_since_genesis t .config)
716
+ ~global_slot: (global_slot_since_genesis t_initial .config)
717
717
account )
718
718
in
719
719
[% log debug]
@@ -739,14 +739,14 @@ let revalidate :
739
739
then (
740
740
[% log debug]
741
741
" Account no longer has permission to send; dropping queue" ;
742
- let dropped, t'' = remove_with_dependents_exn' t first_cmd in
743
- (t'' , Sequence. append dropped_acc dropped) )
742
+ let dropped, t_updated = remove_with_dependents_exn' t first_cmd in
743
+ (t_updated , Sequence. append dropped_acc dropped) )
744
744
else if Account_nonce. (account.nonce < first_nonce) then (
745
745
[% log debug]
746
746
" Current account nonce precedes first nonce in queue; dropping \
747
747
queue" ;
748
- let dropped, t'' = remove_with_dependents_exn' t first_cmd in
749
- (t'' , Sequence. append dropped_acc dropped) )
748
+ let dropped, t_updated = remove_with_dependents_exn' t first_cmd in
749
+ (t_updated , Sequence. append dropped_acc dropped) )
750
750
else
751
751
(* current_nonce >= first_nonce *)
752
752
let first_applicable_nonce_index =
@@ -763,64 +763,75 @@ let revalidate :
763
763
" Current account nonce succeeds first nonce in queue; splitting \
764
764
queue at $index"
765
765
~metadata: [ (" index" , `Int first_applicable_nonce_index) ] ;
766
- let drop_queue, keep_queue =
766
+ let dropped_for_nonce, retained_for_nonce =
767
767
F_sequence. split_at queue first_applicable_nonce_index
768
768
in
769
- let currency_reserved' =
769
+ let currency_reserved_partially_updated =
770
770
F_sequence. foldl
771
771
(fun c cmd ->
772
772
Option. value_exn
773
773
Currency.Amount. (
774
774
c
775
775
- Option. value_exn
776
776
(currency_consumed ~constraint_constants cmd)) )
777
- currency_reserved drop_queue
777
+ currency_reserved dropped_for_nonce
778
778
in
779
- let keep_queue', currency_reserved'' , dropped_for_balance =
779
+ let keep_queue, currency_reserved_updated , dropped_for_balance =
780
780
drop_until_sufficient_balance ~constraint_constants
781
- (keep_queue, currency_reserved' )
781
+ (retained_for_nonce, currency_reserved_partially_updated )
782
782
current_balance
783
783
in
784
784
let to_drop =
785
- Sequence. append (F_sequence. to_seq drop_queue) dropped_for_balance
785
+ Sequence. append
786
+ (F_sequence. to_seq dropped_for_nonce)
787
+ dropped_for_balance
786
788
in
787
- match Sequence. next to_drop with
788
- | None ->
789
- acc
790
- | Some (head , tail ) ->
791
- let t'' =
792
- Sequence. fold tail
793
- ~init:
794
- (remove_all_by_fee_and_hash_and_expiration_exn
795
- (remove_applicable_exn t' head)
796
- head )
797
- ~f: remove_all_by_fee_and_hash_and_expiration_exn
798
- in
799
- let t''' =
800
- match F_sequence. uncons keep_queue' with
801
- | None ->
802
- { t'' with
803
- all_by_sender = Map. remove t''.all_by_sender sender
804
- }
805
- | Some (first_kept , _ ) ->
806
- let first_kept_unchecked =
807
- Transaction_hash.User_command_with_valid_signature. command
808
- first_kept
809
- in
810
- { t'' with
811
- all_by_sender =
812
- Map. set t''.all_by_sender ~key: sender
813
- ~data: (keep_queue', currency_reserved'')
814
- ; applicable_by_fee =
815
- Map_set. insert
816
- ( module Transaction_hash
817
- .User_command_with_valid_signature )
818
- t''.applicable_by_fee
819
- (User_command. fee_per_wu first_kept_unchecked)
820
- first_kept
821
- }
822
- in
823
- (t''' , Sequence. append dropped_acc to_drop) )
789
+ let keeping_prefix = F_sequence. is_empty dropped_for_nonce in
790
+ let keeping_suffix = Sequence. is_empty dropped_for_balance in
791
+ (* t with all_by_sender and applicable_by_fee fields updated *)
792
+ let t_partially_updated =
793
+ match F_sequence. uncons keep_queue with
794
+ | _ when keeping_prefix && keeping_suffix ->
795
+ (* Nothing dropped, nothing needs to be updated *)
796
+ t
797
+ | None ->
798
+ (* We drop the entire queue, first element needs to be removed from
799
+ applicable_by_fee *)
800
+ let t' = remove_applicable_exn t first_cmd in
801
+ { t' with all_by_sender = Map. remove t'.all_by_sender sender }
802
+ | Some _ when keeping_prefix ->
803
+ (* We drop only transactions from the end of queue, keeping
804
+ the head untouched, no need to update applicable_by_fee *)
805
+ { t with
806
+ all_by_sender =
807
+ Map. set t.all_by_sender ~key: sender
808
+ ~data: (keep_queue, currency_reserved_updated)
809
+ }
810
+ | Some (first_kept , _ ) ->
811
+ (* We need to replace old queue head with the new queue head
812
+ in applicable_by_fee *)
813
+ let first_kept_unchecked =
814
+ Transaction_hash.User_command_with_valid_signature. command
815
+ first_kept
816
+ in
817
+ let t' = remove_applicable_exn t first_cmd in
818
+ { t' with
819
+ all_by_sender =
820
+ Map. set t'.all_by_sender ~key: sender
821
+ ~data: (keep_queue, currency_reserved_updated)
822
+ ; applicable_by_fee =
823
+ Map_set. insert
824
+ (module Transaction_hash. User_command_with_valid_signature )
825
+ t'.applicable_by_fee
826
+ (User_command. fee_per_wu first_kept_unchecked)
827
+ first_kept
828
+ }
829
+ in
830
+ let t_updated =
831
+ Sequence. fold ~init: t_partially_updated
832
+ ~f: remove_all_by_fee_and_hash_and_expiration_exn to_drop
833
+ in
834
+ (t_updated, Sequence. append dropped_acc to_drop) )
824
835
825
836
let expired_by_global_slot (t : t ) :
826
837
Transaction_hash.User_command_with_valid_signature. t Sequence. t =
0 commit comments