Skip to content

Commit

Permalink
Remove Generator.filter call;
Browse files Browse the repository at this point in the history
Add ?min_vesting_periods param
  • Loading branch information
gltrost committed Apr 4, 2023
1 parent ab1490a commit 41842f4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
10 changes: 6 additions & 4 deletions src/lib/mina_base/account.ml
Original file line number Diff line number Diff line change
Expand Up @@ -878,11 +878,13 @@ let gen_any_vesting_range =
let%map cliff_time = gen_incl (of_int 1) vesting_end in
(cliff_time, vesting_end, vesting_period)

let gen_at_least_one_vesting_period =
let gen_at_least_one_vesting_period ?(min_vesting_periods = 1) =
let open Quickcheck.Generator.Let_syntax in
let open Global_slot in
(* vesting period must be at least oe to avoid division by zero *)
let%bind vesting_period = Int.gen_incl 1 1000 >>| Global_slot.of_int in
(* vesting period must be at least one to avoid division by zero *)
let%bind vesting_period =
min_vesting_periods |> return >>| Global_slot.of_int
in
let min_vesting_end = succ vesting_period in
let%bind vesting_end = gen_incl min_vesting_end max_value in
let max_cliff_time = Option.value_exn @@ sub vesting_end vesting_period in
Expand Down Expand Up @@ -947,7 +949,7 @@ let gen_timing_at_least_one_vesting_period (account_balance : Balance.t) =
let open Quickcheck.Generator.Let_syntax in
let%bind initial_minimum_balance = Balance.(gen_incl one account_balance)
and cliff_time, vesting_end, vesting_period =
gen_at_least_one_vesting_period
gen_at_least_one_vesting_period ~min_vesting_periods:2
in
gen_vesting_details ~vesting_period ~cliff_time ~vesting_end
initial_minimum_balance
Expand Down
49 changes: 24 additions & 25 deletions src/lib/mina_base/test/account/account.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ open Unsigned

let gen_timing = Account.gen_timing Balance.max_int


let%test_module "Test account's timing." =
( module struct
open Account.Timing.As_record
Expand All @@ -29,26 +28,26 @@ let%test_module "Test account's timing." =
let%test_unit "Test fine-tuning of the account generation." =
Quickcheck.test Account.gen_timed ~f:(fun account ->
try
[%test_eq: Balance.t option] (Some Balance.zero)
( match account.timing with
| Untimed ->
None
| Timed
{ initial_minimum_balance
; cliff_time
; cliff_amount
; vesting_period
; vesting_increment
} ->
let global_slot = Global_slot.max_value in
Option.some
@@ Account.min_balance_at_slot ~global_slot ~cliff_time
~cliff_amount ~vesting_period ~vesting_increment
~initial_minimum_balance )
with
| e ->
Printf.printf "%s" (Sexp.to_string @@ Account.Timing.sexp_of_t account.timing);
raise e)
[%test_eq: Balance.t option] (Some Balance.zero)
( match account.timing with
| Untimed ->
None
| Timed
{ initial_minimum_balance
; cliff_time
; cliff_amount
; vesting_period
; vesting_increment
} ->
let global_slot = Global_slot.max_value in
Option.some
@@ Account.min_balance_at_slot ~global_slot ~cliff_time
~cliff_amount ~vesting_period ~vesting_increment
~initial_minimum_balance )
with e ->
Printf.printf "%s"
(Sexp.to_string @@ Account.Timing.sexp_of_t account.timing) ;
raise e )

let%test_unit "Minimum balance never changes before the cliff time." =
Quickcheck.test
Expand Down Expand Up @@ -105,7 +104,9 @@ let%test_module "Test account's timing." =
let open Generator.Let_syntax in
(* The amount to be vested after the cliff time must be greater than zero
or else there is no vesting at all. *)
let%bind timing_rec = Account.gen_timing_at_least_one_vesting_period Balance.max_int in
let%bind timing_rec =
Account.gen_timing_at_least_one_vesting_period Balance.max_int
in
let timing = Account.Timing.of_record timing_rec in
let vesting_end = Account.timing_final_vesting_slot timing in
(* Global_slot addition may overflow, so we need to make sure it won't happen. *)
Expand Down Expand Up @@ -178,9 +179,7 @@ let%test_module "Test account's timing." =
(let open Quickcheck in
let open Generator.Let_syntax in
let%bind timing =
Generator.filter
(Account.gen_timing_at_least_one_vesting_period Balance.max_int)
~f:(fun t -> Global_slot.(t.vesting_period > of_int 1) )
Account.gen_timing_at_least_one_vesting_period Balance.max_int
in
let min_slot = Global_slot.(add timing.cliff_time (of_int 1)) in
let max_slot =
Expand Down

0 comments on commit 41842f4

Please sign in to comment.