• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MinaProtocol / mina / 2995

25 Nov 2024 08:02PM UTC coverage: 32.818% (-27.9%) from 60.697%
2995

push

buildkite

web-flow
Merge pull request #16378 from MinaProtocol/dkijania/implement_test_ledger_apply_dev

implement test ledger apply dev

23246 of 70834 relevant lines covered (32.82%)

17178.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

14.29
/src/lib/work_selector/random_offset.ml
1
open Core_kernel
6✔
2

3
(** The work selection method implemented here is the following. We distinguish
4
two cases:
5
    1. The work pool is updated: a random [offset] is generated. The selected
6
    work is the [offset]-th one of the expensive work list.
7
    2. As long as the pool stays the same, the works are selected sequentially
8
    from the offset. If the end of the work list is reached, the next works are
9
    selected sequentially from the beginning of the list. *)
10
module Make (Lib : Intf.Lib_intf) = struct
11
  module Offset = struct
12
    (* The initialization for the first offset can be 0; as the first
13
       [previous_length] 0, any non-empty list will trigger an update of the offset. *)
14
    let offset = ref 0
15

16
    (* The previous length of expensive works [work] got through *)
17
    let prev_length = ref 0
18

19
    (* This function maintains [prev_length] & [offset] up do date.
20
       When the pool is not updated, we consider that its length should
21
       decrease by 1 ([new_length = !prev_length - 1]). If an other behavior
22
       is observed, the pool is considered updated and a new [offset] is
23
       generated randomly, uniformly chosen between 0 and [new_length]. *)
24
    let update ~new_length =
25
      let () =
×
26
        if new_length = !prev_length - 1 then ()
×
27
        else offset := Random.int new_length
×
28
      in
29
      prev_length := new_length
30

31
    (* Because of the [offset] being constant and the [expensive_work] list
32
       reducing in size, a case where [offset] ends up greater than the list
33
       length may happen. In that case, we go back to the beginning of the list,
34
       by resetting [offset] to 0.
35
       /!\ fails if [expensive_work] is empty! *)
36
    let get_nth expensive_work =
37
      try List.nth_exn expensive_work !offset
×
38
      with _ ->
×
39
        (* Raising an error here means that offset is now too large for the
40
           list -> back to the beginning with offset at 0 *)
41
        offset := 0 ;
42
        List.nth_exn expensive_work !offset
43
  end
44

45
  let work ~snark_pool ~fee ~logger (state : Lib.State.t) =
46
    Lib.State.remove_old_assignments state ~logger ;
×
47
    let unseen_jobs = Lib.State.all_unseen_works state in
×
48
    match Lib.get_expensive_work ~snark_pool ~fee unseen_jobs with
×
49
    | [] ->
×
50
        None
51
    | expensive_work ->
×
52
        Offset.update ~new_length:(List.length expensive_work) ;
×
53
        let x = Offset.get_nth expensive_work in
54
        Lib.State.set state x ; Some x
×
55
end
56

57
let%test_module "test" =
58
  ( module struct
59
    module Test = Test.Make_test (Make)
60
  end )
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc