Skip to content

Commit

Permalink
Merge pull request #16399 from MinaProtocol/georgeee/remove-constrain…
Browse files Browse the repository at this point in the history
…t_constants-from-currency_consumed

Remove constraint_constants from currency_consumed
  • Loading branch information
mrmr1993 authored Dec 29, 2024
2 parents d3f9e39 + 9937554 commit 390fb30
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 57 deletions.
67 changes: 23 additions & 44 deletions src/lib/network_pool/indexed_pool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,8 @@ let config t = t.config
(* Compute the total currency required from the sender to execute a command.
Returns None in case of overflow.
*)
let currency_consumed_unchecked :
constraint_constants:Genesis_constants.Constraint_constants.t
-> User_command.t
-> Currency.Amount.t option =
fun ~constraint_constants:_ cmd ->
let currency_consumed_unchecked : User_command.t -> Currency.Amount.t option =
fun cmd ->
let fee_amt = Currency.Amount.of_fee @@ User_command.fee cmd in
let open Currency.Amount in
let amt =
Expand All @@ -88,17 +85,14 @@ let currency_consumed_unchecked :
in
fee_amt + amt

let currency_consumed ~constraint_constants cmd =
currency_consumed_unchecked ~constraint_constants
let currency_consumed cmd =
currency_consumed_unchecked
(Transaction_hash.User_command_with_valid_signature.command cmd)

let currency_consumed' :
constraint_constants:Genesis_constants.Constraint_constants.t
-> User_command.t
-> (Currency.Amount.t, Command_error.t) Result.t =
fun ~constraint_constants cmd ->
cmd
|> currency_consumed_unchecked ~constraint_constants
User_command.t -> (Currency.Amount.t, Command_error.t) Result.t =
fun cmd ->
cmd |> currency_consumed_unchecked
|> Result.of_option ~error:Command_error.Overflow

module For_tests = struct
Expand Down Expand Up @@ -180,7 +174,6 @@ module For_tests = struct
nonce ;
let consumed =
currency_consumed_unchecked
~constraint_constants:pool.config.constraint_constants
(Transaction_hash.User_command_with_valid_signature.command
cmd )
|> Option.value_exn
Expand Down Expand Up @@ -568,14 +561,13 @@ let run :
it. Called from revalidate and remove_lowest_fee, and when replacing
transactions. *)
let remove_with_dependents_exn :
constraint_constants:_
-> Transaction_hash.User_command_with_valid_signature.t
Transaction_hash.User_command_with_valid_signature.t
-> Sender_local_state.t ref
-> ( Transaction_hash.User_command_with_valid_signature.t Sequence.t
, Update.single
, _ )
Writer_result.t =
fun ~constraint_constants (* ({ constraint_constants; _ } as t) *) cmd state ->
fun cmd state ->
let unchecked =
Transaction_hash.User_command_with_valid_signature.command cmd
in
Expand Down Expand Up @@ -604,7 +596,7 @@ let remove_with_dependents_exn :
Option.value_exn
(* safe because we check for overflow when we add commands. *)
(let open Option.Let_syntax in
let%bind consumed = currency_consumed ~constraint_constants cmd' in
let%bind consumed = currency_consumed cmd' in
Currency.Amount.(consumed + acc)) )
Currency.Amount.zero drop_queue
in
Expand Down Expand Up @@ -642,11 +634,7 @@ let run' t cmd x =
x

let remove_with_dependents_exn' t cmd =
match
run' t cmd
(remove_with_dependents_exn
~constraint_constants:t.config.constraint_constants cmd )
with
match run' t cmd (remove_with_dependents_exn cmd) with
| Ok x ->
x
| Error _ ->
Expand All @@ -655,14 +643,13 @@ let remove_with_dependents_exn' t cmd =
(** Drop commands from the end of the queue until the total currency consumed is
<= the current balance. *)
let drop_until_sufficient_balance :
constraint_constants:Genesis_constants.Constraint_constants.t
-> Transaction_hash.User_command_with_valid_signature.t F_sequence.t
Transaction_hash.User_command_with_valid_signature.t F_sequence.t
* Currency.Amount.t
-> Currency.Amount.t
-> Transaction_hash.User_command_with_valid_signature.t F_sequence.t
* Currency.Amount.t
* Transaction_hash.User_command_with_valid_signature.t Sequence.t =
fun ~constraint_constants (queue, currency_reserved) current_balance ->
fun (queue, currency_reserved) current_balance ->
let rec go queue' currency_reserved' dropped_so_far =
if Currency.Amount.(currency_reserved' <= current_balance) then
(queue', currency_reserved', dropped_so_far)
Expand All @@ -674,9 +661,7 @@ let drop_until_sufficient_balance :
sufficient balance"
(F_sequence.unsnoc queue')
in
let consumed =
Option.value_exn (currency_consumed ~constraint_constants liat)
in
let consumed = Option.value_exn (currency_consumed liat) in
go daeh
(Option.value_exn Currency.Amount.(currency_reserved' - consumed))
(Sequence.append dropped_so_far @@ Sequence.singleton liat)
Expand All @@ -693,7 +678,7 @@ let revalidate :
-> [ `Entire_pool | `Subset of Account_id.Set.t ]
-> (Account_id.t -> Account.t)
-> t * Transaction_hash.User_command_with_valid_signature.t Sequence.t =
fun ({ config = { constraint_constants; _ }; _ } as t) ~logger scope f ->
fun t ~logger scope f ->
let requires_revalidation =
match scope with
| `Entire_pool ->
Expand Down Expand Up @@ -770,14 +755,12 @@ let revalidate :
F_sequence.foldl
(fun c cmd ->
Option.value_exn
Currency.Amount.(
c
- Option.value_exn
(currency_consumed ~constraint_constants cmd)) )
Currency.Amount.(c - Option.value_exn (currency_consumed cmd))
)
currency_reserved drop_queue
in
let keep_queue', currency_reserved'', dropped_for_balance =
drop_until_sufficient_balance ~constraint_constants
drop_until_sufficient_balance
(keep_queue, currency_reserved')
current_balance
in
Expand Down Expand Up @@ -898,7 +881,7 @@ module Add_from_gossip_exn (M : Writer_result.S) = struct
, Command_error.t )
M.t =
fun ~config:
( { constraint_constants
( { constraint_constants = _
; consensus_constants
; time_controller
; slot_tx_end
Expand Down Expand Up @@ -929,9 +912,7 @@ module Add_from_gossip_exn (M : Writer_result.S) = struct
Result.Let_syntax.(
(* C5 *)
let%bind () = check_expiry config unchecked in
let%bind consumed =
currency_consumed' ~constraint_constants unchecked
in
let%bind consumed = currency_consumed' unchecked in
let%map () =
(* TODO: Proper exchange rate mechanism. *)
let fee_token = User_command.fee_token unchecked in
Expand Down Expand Up @@ -1058,7 +1039,7 @@ module Add_from_gossip_exn (M : Writer_result.S) = struct
(* C3 *)
in
let%bind dropped =
remove_with_dependents_exn ~constraint_constants
remove_with_dependents_exn
(F_sequence.head_exn drop_queue)
by_sender
|> M.lift
Expand Down Expand Up @@ -1159,7 +1140,7 @@ let add_from_backtrack :
-> Transaction_hash.User_command_with_valid_signature.t
-> (t, Command_error.t) Result.t =
fun ( { config =
{ constraint_constants
{ constraint_constants = _
; consensus_constants
; time_controller
; slot_tx_end
Expand All @@ -1186,9 +1167,7 @@ let add_from_backtrack :
let fee_payer = User_command.fee_payer unchecked in
let fee_per_wu = User_command.fee_per_wu unchecked in
let cmd_hash = Transaction_hash.User_command_with_valid_signature.hash cmd in
let consumed =
Option.value_exn (currency_consumed ~constraint_constants cmd)
in
let consumed = Option.value_exn (currency_consumed cmd) in
match Map.find t.all_by_sender fee_payer with
| None ->
{ all_by_sender =
Expand Down
3 changes: 1 addition & 2 deletions src/lib/network_pool/indexed_pool.mli
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ module For_tests : sig
Account_id.Map.t

val currency_consumed :
constraint_constants:Genesis_constants.Constraint_constants.t
-> Transaction_hash.User_command_with_valid_signature.t
Transaction_hash.User_command_with_valid_signature.t
-> Currency.Amount.t option
end
16 changes: 5 additions & 11 deletions src/lib/network_pool/test/indexed_pool_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ let singleton_properties () =
(Amount.of_nanomina_int_exn 500)
in
if
Option.value_exn (currency_consumed ~constraint_constants cmd)
|> Amount.to_nanomina_int > 500
Option.value_exn (currency_consumed cmd) |> Amount.to_nanomina_int > 500
then
match add_res with
| Error (Insufficient_funds _) ->
Expand Down Expand Up @@ -214,9 +213,7 @@ let replacement () =
~common:(fun c -> { c with fee = Amount.to_fee fee })
~body:(fun b -> { b with amount })
in
let consumed =
Option.value_exn (currency_consumed ~constraint_constants cmd')
in
let consumed = Option.value_exn (currency_consumed cmd') in
let%map rest =
go
(Account_nonce.succ current_nonce)
Expand Down Expand Up @@ -293,20 +290,17 @@ let replacement () =
~f:(fun consumed_so_far cmd ->
Option.value_exn
Option.(
currency_consumed ~constraint_constants cmd
currency_consumed cmd
>>= fun consumed -> Amount.(consumed + consumed_so_far)) )
in
assert (Amount.(currency_consumed_pre_replace <= init_balance)) ;
let currency_consumed_post_replace =
Option.value_exn
(let open Option.Let_syntax in
let%bind replaced_currency_consumed =
currency_consumed ~constraint_constants
@@ List.nth_exn setup_cmds replaced_idx
in
let%bind replacer_currency_consumed =
currency_consumed ~constraint_constants replace_cmd
currency_consumed @@ List.nth_exn setup_cmds replaced_idx
in
let%bind replacer_currency_consumed = currency_consumed replace_cmd in
let%bind a =
Amount.(currency_consumed_pre_replace - replaced_currency_consumed)
in
Expand Down

0 comments on commit 390fb30

Please sign in to comment.