Skip to content

Commit 3080c5e

Browse files
committed
Map based snark pool tables
1 parent 3bfc796 commit 3080c5e

File tree

3 files changed

+52
-32
lines changed

3 files changed

+52
-32
lines changed

src/lib/network_pool/snark_pool.ml

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ open Core_kernel
22
open Async
33
open Pipe_lib
44
open Network_peer
5-
module Statement_table = Transaction_snark_work.Statement.Table
65

76
module Snark_tables = struct
87
type t =
98
{ all :
109
Ledger_proof.t One_or_two.t Priced_proof.t
11-
Transaction_snark_work.Statement.Table.t
10+
Transaction_snark_work.Statement.Map.t
1211
; rebroadcastable :
1312
(Ledger_proof.t One_or_two.t Priced_proof.t * Core.Time.t)
14-
Transaction_snark_work.Statement.Table.t
13+
Transaction_snark_work.Statement.Map.t
1514
}
1615
[@@deriving sexp]
1716
end
@@ -107,7 +106,7 @@ struct
107106
| `New_best_tip of Base_ledger.t ]
108107

109108
type t =
110-
{ snark_tables : Snark_tables.t
109+
{ snark_tables : Snark_tables.t ref
111110
; snark_table_lock : (unit Throttle.Sequencer.t[@sexp.opaque])
112111
; frontier : (unit -> Transition_frontier.t option[@sexp.opaque])
113112
; config : Config.t
@@ -136,7 +135,7 @@ struct
136135

137136
let snark_pool_json t : Yojson.Safe.t =
138137
`List
139-
(Statement_table.fold ~init:[] t.snark_tables.all
138+
(Map.fold ~init:[] !(t.snark_tables).all
140139
~f:(fun ~key ~data:{ proof = _; fee = { fee; prover } } acc ->
141140
let work_ids =
142141
Transaction_snark_work.Statement.compact_json key
@@ -151,7 +150,7 @@ struct
151150
:: acc ) )
152151

153152
let all_completed_work (t : t) : Transaction_snark_work.Info.t list =
154-
Statement_table.fold ~init:[] t.snark_tables.all
153+
Map.fold ~init:[] !(t.snark_tables).all
155154
~f:(fun ~key ~data:{ proof = _; fee = { fee; prover } } acc ->
156155
let work_ids = Transaction_snark_work.Statement.work_ids key in
157156
{ Transaction_snark_work.Info.statements = key
@@ -177,7 +176,7 @@ struct
177176
in
178177
let%bind prover_account_ids =
179178
let account_ids =
180-
t.snark_tables.all |> Statement_table.data
179+
!(t.snark_tables).all |> Map.data
181180
|> List.map
182181
~f:(fun { Priced_proof.fee = { prover; _ }; _ } ->
183182
prover )
@@ -202,7 +201,7 @@ struct
202201
let yield = Staged.unstage (Scheduler.yield_every ~n:50) in
203202
let%map () =
204203
let open Deferred.Let_syntax in
205-
Statement_table.fold ~init:Deferred.unit t.snark_tables.all
204+
Map.fold ~init:Deferred.unit !(t.snark_tables).all
206205
~f:(fun ~key ~data:{ fee = { fee; prover }; _ } acc ->
207206
let%bind () = acc in
208207
let%map () = yield () in
@@ -216,9 +215,12 @@ struct
216215
fee_is_sufficient t ~fee
217216
~account_exists:prover_account_exists
218217
in
219-
if not keep then (
220-
Hashtbl.remove t.snark_tables.all key ;
221-
Hashtbl.remove t.snark_tables.rebroadcastable key ) )
218+
if not keep then
219+
t.snark_tables :=
220+
{ all = Map.remove !(t.snark_tables).all key
221+
; rebroadcastable =
222+
Map.remove !(t.snark_tables).rebroadcastable key
223+
} )
222224
in
223225
() )
224226
in
@@ -229,12 +231,16 @@ struct
229231
[%log' trace t.logger]
230232
"Handling snark refcount table: $num_removed works were removed"
231233
~metadata:[ ("num_removed", `Int (List.length removed_work)) ] ;
232-
List.iter removed_work ~f:(fun work ->
233-
Hashtbl.remove t.snark_tables.rebroadcastable work ;
234-
Hashtbl.remove t.snark_tables.all work ) ;
234+
t.snark_tables :=
235+
{ all =
236+
List.fold ~init:!(t.snark_tables).all ~f:Map.remove removed_work
237+
; rebroadcastable =
238+
List.fold ~init:!(t.snark_tables).rebroadcastable ~f:Map.remove
239+
removed_work
240+
} ;
235241
Mina_metrics.(
236242
Gauge.set Snark_work.snark_pool_size
237-
(Float.of_int @@ Hashtbl.length t.snark_tables.all))
243+
(Float.of_int @@ Map.length !(t.snark_tables).all))
238244

239245
let handle_transition_frontier_diff u t =
240246
match u with
@@ -276,9 +282,10 @@ struct
276282
~frontier_broadcast_pipe ~config ~logger ~tf_diff_writer =
277283
let t =
278284
{ snark_tables =
279-
{ all = Statement_table.create ()
280-
; rebroadcastable = Statement_table.create ()
281-
}
285+
ref
286+
{ Snark_tables.all = Transaction_snark_work.Statement.Map.empty
287+
; rebroadcastable = Transaction_snark_work.Statement.Map.empty
288+
}
282289
; snark_table_lock = Throttle.Sequencer.create ()
283290
; frontier =
284291
(fun () -> Broadcast_pipe.Reader.peek frontier_broadcast_pipe)
@@ -296,7 +303,7 @@ struct
296303

297304
let get_logger t = t.logger
298305

299-
let request_proof t = Statement_table.find t.snark_tables.all
306+
let request_proof t = Map.find !(t.snark_tables).all
300307

301308
let add_snark ?(is_local = false) t ~work
302309
~(proof : Ledger_proof.t One_or_two.t) ~fee =
@@ -305,23 +312,28 @@ struct
305312
( if work_is_referenced t work then (
306313
(*Note: fee against existing proofs and the new proofs are checked in
307314
Diff.unsafe_apply which calls this function*)
308-
Hashtbl.set t.snark_tables.all ~key:work ~data:{ proof; fee } ;
309-
if is_local then
310-
Hashtbl.set t.snark_tables.rebroadcastable ~key:work
311-
~data:({ proof; fee }, Time.now ())
312-
else
313-
(* Stop rebroadcasting locally generated snarks if they are
314-
overwritten. No-op if there is no rebroadcastable SNARK with that
315-
statement. *)
316-
Hashtbl.remove t.snark_tables.rebroadcastable work ;
315+
t.snark_tables :=
316+
{ all =
317+
Map.set !(t.snark_tables).all ~key:work
318+
~data:{ proof; fee }
319+
; rebroadcastable =
320+
( if is_local then
321+
Map.set !(t.snark_tables).rebroadcastable ~key:work
322+
~data:({ proof; fee }, Time.now ())
323+
else
324+
(* Stop rebroadcasting locally generated snarks if they are
325+
overwritten. No-op if there is no rebroadcastable SNARK with that
326+
statement. *)
327+
Map.remove !(t.snark_tables).rebroadcastable work )
328+
} ;
317329
(*when snark work is added to the pool*)
318330
Mina_metrics.(
319331
Gauge.set Snark_work.useful_snark_work_received_time_sec
320332
Time.(
321333
let x = now () |> to_span_since_epoch |> Span.to_sec in
322334
x -. Mina_metrics.time_offset_sec) ;
323335
Gauge.set Snark_work.snark_pool_size
324-
(Float.of_int @@ Hashtbl.length t.snark_tables.all) ;
336+
(Float.of_int @@ Map.length !(t.snark_tables).all) ;
325337
Snark_work.Snark_fee_histogram.observe Snark_work.snark_fee
326338
( fee.Mina_base.Fee_with_prover.fee |> Currency.Fee.to_int
327339
|> Float.of_int )) ;
@@ -486,15 +498,17 @@ struct
486498
| None ->
487499
[]
488500
| Some best_tips ->
489-
Hashtbl.to_alist t.snark_tables.rebroadcastable
501+
Map.to_alist !(t.snark_tables).rebroadcastable
490502
|> List.filter_map ~f:(fun (stmt, (snark, _time)) ->
491503
if Hash_set.mem best_tips stmt then
492504
Some (Diff.Add_solved_work (stmt, snark))
493505
else None )
494506

495507
let remove_solved_work t work =
496-
Statement_table.remove t.snark_tables.all work ;
497-
Statement_table.remove t.snark_tables.rebroadcastable work
508+
t.snark_tables :=
509+
{ all = Map.remove !(t.snark_tables).all work
510+
; rebroadcastable = Map.remove !(t.snark_tables).rebroadcastable work
511+
}
498512
end
499513

500514
include Network_pool_base.Make (Transition_frontier) (Resource_pool)

src/lib/transaction_snark_work/transaction_snark_work.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ module Statement = struct
2727

2828
type _unused = unit constraint t = Arg.Stable.V1.t
2929

30+
include Comparable.Make_binable (Arg.Stable.V1)
3031
include Hashable.Make_binable (Arg.Stable.V1)
3132
end
3233
end]
3334

3435
type t = Stable.Latest.t [@@deriving sexp, hash, compare, yojson, equal]
3536

37+
include Comparable.Make_binable (Stable.Latest)
3638
include Hashable.Make (Stable.Latest)
3739

3840
let gen = One_or_two.gen Transaction_snark.Statement.gen

src/lib/transaction_snark_work/transaction_snark_work.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ module Statement : sig
66
type t = Transaction_snark.Statement.t One_or_two.t
77
[@@deriving compare, sexp, yojson, equal]
88

9+
include Comparable.S with type t := t
10+
911
include Hashable.S with type t := t
1012

1113
module Stable : sig
1214
module V1 : sig
1315
type t [@@deriving bin_io, compare, sexp, version, yojson, equal]
1416

17+
include Comparable.S with type t := t
18+
1519
include Hashable.S_binable with type t := t
1620
end
1721
end

0 commit comments

Comments
 (0)