@@ -693,27 +693,27 @@ let revalidate :
693693 -> [ `Entire_pool | `Subset of Account_id.Set. t ]
694694 -> (Account_id. t -> Account. t )
695695 -> 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 ->
697697 let requires_revalidation =
698698 match scope with
699699 | `Entire_pool ->
700700 Fn. const true
701701 | `Subset subset ->
702702 Set. mem subset
703703 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)
705705 ~f: (fun
706706 ~key :sender
707707 ~data :(queue , currency_reserved )
708- ((t' , dropped_acc ) as acc )
708+ ((t , dropped_acc ) as acc )
709709 ->
710710 if not (requires_revalidation sender) then acc
711711 else
712712 let account : Account.t = f sender in
713713 let current_balance =
714714 Currency.Balance. to_amount
715715 (Account. liquid_balance_at_slot
716- ~global_slot: (global_slot_since_genesis t .config)
716+ ~global_slot: (global_slot_since_genesis t_initial .config)
717717 account )
718718 in
719719 [% log debug]
@@ -739,14 +739,14 @@ let revalidate :
739739 then (
740740 [% log debug]
741741 " 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) )
744744 else if Account_nonce. (account.nonce < first_nonce) then (
745745 [% log debug]
746746 " Current account nonce precedes first nonce in queue; dropping \
747747 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) )
750750 else
751751 (* current_nonce >= first_nonce *)
752752 let first_applicable_nonce_index =
@@ -763,64 +763,75 @@ let revalidate :
763763 " Current account nonce succeeds first nonce in queue; splitting \
764764 queue at $index"
765765 ~metadata: [ (" index" , `Int first_applicable_nonce_index) ] ;
766- let drop_queue, keep_queue =
766+ let dropped_for_nonce, retained_for_nonce =
767767 F_sequence. split_at queue first_applicable_nonce_index
768768 in
769- let currency_reserved' =
769+ let currency_reserved_partially_updated =
770770 F_sequence. foldl
771771 (fun c cmd ->
772772 Option. value_exn
773773 Currency.Amount. (
774774 c
775775 - Option. value_exn
776776 (currency_consumed ~constraint_constants cmd)) )
777- currency_reserved drop_queue
777+ currency_reserved dropped_for_nonce
778778 in
779- let keep_queue', currency_reserved'' , dropped_for_balance =
779+ let keep_queue, currency_reserved_updated , dropped_for_balance =
780780 drop_until_sufficient_balance ~constraint_constants
781- (keep_queue, currency_reserved' )
781+ (retained_for_nonce, currency_reserved_partially_updated )
782782 current_balance
783783 in
784784 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
786788 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) )
824835
825836let expired_by_global_slot (t : t ) :
826837 Transaction_hash.User_command_with_valid_signature. t Sequence. t =
0 commit comments