Skip to content

Commit 3888a96

Browse files
authored
Merge pull request #16677 from MinaProtocol/dkijania/use_zkapp_vk_in_transaction_pool_dev
[DEV] Introduce zkapp_vk_cache_tag project for caching zkapp verification keys
2 parents 1e996f9 + e3d4bd0 commit 3888a96

File tree

10 files changed

+96
-15
lines changed

10 files changed

+96
-15
lines changed

src/dune-project

+2-1
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,5 @@
231231
(package (name work_selector))
232232
(package (name zkapp_command_builder))
233233
(package (name zkapps_examples))
234-
(package (name zkapp_limits))
234+
(package (name zkapp_limits))
235+
(package (name zkapp_vk_cache_tag))

src/lib/mina_lib/mina_lib.ml

+7-1
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,11 @@ let initialize_proof_cache_db (config : Config.t) =
16711671
(config.conf_dir ^/ "proof_cache")
16721672
>>| function Error e -> raise_on_initialization_error e | Ok db -> db
16731673

1674+
let initialize_zkapp_vk_cache_db (config : Config.t) =
1675+
Zkapp_vk_cache_tag.create_db ~logger:config.logger
1676+
(config.conf_dir ^/ "zkapp_vk_cache")
1677+
>>| function Error e -> raise_on_initialization_error e | Ok db -> db
1678+
16741679
let create ~commit_id ?wallets (config : Config.t) =
16751680
let commit_id_short = String.sub ~pos:0 ~len:8 commit_id in
16761681
let catchup_mode = if config.super_catchup then `Super else `Normal in
@@ -1705,6 +1710,7 @@ let create ~commit_id ?wallets (config : Config.t) =
17051710
in_memory_reverse_structured_log_messages_for_integration_test
17061711
config.start_filtered_logs ;
17071712
let%bind proof_cache_db = initialize_proof_cache_db config in
1713+
let%bind zkapp_vk_cache_db = initialize_zkapp_vk_cache_db config in
17081714
let module Context =
17091715
(val context ~proof_cache_db ~commit_id config)
17101716
in
@@ -2001,7 +2007,7 @@ let create ~commit_id ?wallets (config : Config.t) =
20012007
~pool_max_size:
20022008
config.precomputed_values.genesis_constants.txpool_max_size
20032009
~genesis_constants:config.precomputed_values.genesis_constants
2004-
~slot_tx_end
2010+
~slot_tx_end ~vk_cache_db:zkapp_vk_cache_db
20052011
in
20062012
let first_received_message_signal = Ivar.create () in
20072013
let online_status, notify_online_impl =

src/lib/mina_lib/tests/tests.ml

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ let%test_module "Epoch ledger sync tests" =
211211
~pool_max_size:precomputed_values.genesis_constants.txpool_max_size
212212
~genesis_constants:precomputed_values.genesis_constants
213213
~slot_tx_end:None
214+
~vk_cache_db:(Zkapp_vk_cache_tag.For_tests.create_db ())
214215
in
215216
Network_pool.Transaction_pool.create ~config ~constraint_constants
216217
~consensus_constants ~time_controller ~logger

src/lib/network_pool/dune

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
kimchi_pasta.basic
7878
coda_genesis_ledger
7979
staged_ledger_diff
80-
mina_wire_types)
80+
mina_wire_types
81+
zkapp_vk_cache_tag)
8182
(instrumentation
8283
(backend bisect_ppx))
8384
(preprocess

src/lib/network_pool/intf.ml

+1
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ module type Transaction_resource_pool_intf = sig
387387
-> verifier:Verifier.t
388388
-> genesis_constants:Genesis_constants.t
389389
-> slot_tx_end:Mina_numbers.Global_slot_since_hard_fork.t option
390+
-> vk_cache_db:Zkapp_vk_cache_tag.cache_db
390391
-> Config.t
391392

392393
val member : t -> Transaction_hash.User_command_with_valid_signature.t -> bool

src/lib/network_pool/transaction_pool.ml

+21-12
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,19 @@ struct
287287
; verifier : (Verifier.t[@sexp.opaque])
288288
; genesis_constants : Genesis_constants.t
289289
; slot_tx_end : Mina_numbers.Global_slot_since_hard_fork.t option
290+
; vk_cache_db : Zkapp_vk_cache_tag.cache_db
290291
}
291-
[@@deriving sexp_of]
292292

293293
(* remove next line if there's a way to force [@@deriving make] write a
294294
named parameter instead of an optional parameter *)
295295
let make ~trust_system ~pool_max_size ~verifier ~genesis_constants
296-
~slot_tx_end =
296+
~slot_tx_end ~vk_cache_db =
297297
{ trust_system
298298
; pool_max_size
299299
; verifier
300300
; genesis_constants
301301
; slot_tx_end
302+
; vk_cache_db
302303
}
303304
end
304305

@@ -331,15 +332,17 @@ struct
331332
module Vk_refcount_table = struct
332333
type t =
333334
{ verification_keys :
334-
(int * Verification_key_wire.t) Zkapp_basic.F_map.Table.t
335+
(int * Zkapp_vk_cache_tag.t) Zkapp_basic.F_map.Table.t
335336
; account_id_to_vks : int Zkapp_basic.F_map.Map.t Account_id.Table.t
336337
; vk_to_account_ids : int Account_id.Map.t Zkapp_basic.F_map.Table.t
338+
; vk_cache_db : Zkapp_vk_cache_tag.cache_db
337339
}
338340

339-
let create () =
341+
let create vk_cache_db () =
340342
{ verification_keys = Zkapp_basic.F_map.Table.create ()
341343
; account_id_to_vks = Account_id.Table.create ()
342344
; vk_to_account_ids = Zkapp_basic.F_map.Table.create ()
345+
; vk_cache_db
343346
}
344347

345348
let find_vk (t : t) = Hashtbl.find t.verification_keys
@@ -365,7 +368,7 @@ struct
365368
in
366369
Hashtbl.update t.verification_keys vk.hash ~f:(function
367370
| None ->
368-
(1, vk)
371+
(1, Zkapp_vk_cache_tag.write_key_to_disk t.vk_cache_db vk)
369372
| Some (count, vk) ->
370373
(count + 1, vk) ) ;
371374
Hashtbl.update t.account_id_to_vks account_id
@@ -425,13 +428,12 @@ struct
425428
; mutable current_batch : int
426429
; mutable remaining_in_batch : int
427430
; config : Config.t
428-
; logger : (Logger.t[@sexp.opaque])
431+
; logger : Logger.t
429432
; batcher : Batcher.t
430-
; mutable best_tip_diff_relay : (unit Deferred.t[@sexp.opaque]) Option.t
431-
; mutable best_tip_ledger : (Base_ledger.t[@sexp.opaque]) Option.t
432-
; verification_key_table : (Vk_refcount_table.t[@sexp.opaque])
433+
; mutable best_tip_diff_relay : unit Deferred.t Option.t
434+
; mutable best_tip_ledger : Base_ledger.t Option.t
435+
; verification_key_table : Vk_refcount_table.t
433436
}
434-
[@@deriving sexp_of]
435437

436438
let member t x =
437439
Indexed_pool.member t.pool (Transaction_hash.User_command.of_checked x)
@@ -837,7 +839,8 @@ struct
837839
; batcher = Batcher.create ~logger config.verifier
838840
; best_tip_diff_relay = None
839841
; best_tip_ledger = None
840-
; verification_key_table = Vk_refcount_table.create ()
842+
; verification_key_table =
843+
Vk_refcount_table.create config.vk_cache_db ()
841844
}
842845
in
843846
don't_wait_for
@@ -1169,7 +1172,12 @@ struct
11691172
in
11701173
let vks =
11711174
vks
1172-
|> List.map ~f:(fun vk -> (vk.hash, vk))
1175+
|> List.map ~f:(fun vk_cached ->
1176+
let vk =
1177+
Zkapp_vk_cache_tag.read_key_from_disk
1178+
vk_cached
1179+
in
1180+
(vk.hash, vk) )
11731181
|> Zkapp_basic.F_map.Map.of_alist_exn
11741182
in
11751183
(account_id, vks) )
@@ -1930,6 +1938,7 @@ let%test_module _ =
19301938
let config =
19311939
Test.Resource_pool.make_config ~trust_system ~pool_max_size ~verifier
19321940
~genesis_constants ~slot_tx_end
1941+
~vk_cache_db:(Zkapp_vk_cache_tag.For_tests.create_db ())
19331942
in
19341943
let pool_, _, _ =
19351944
Test.create ~config ~logger ~constraint_constants ~consensus_constants

src/lib/transaction_inclusion_status/transaction_inclusion_status.ml

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ let%test_module "transaction_status" =
131131
Transaction_pool.Resource_pool.make_config ~trust_system ~pool_max_size
132132
~verifier ~genesis_constants:precomputed_values.genesis_constants
133133
~slot_tx_end:None
134+
~vk_cache_db:(Zkapp_vk_cache_tag.For_tests.create_db ())
134135
in
135136
let transaction_pool, _, local_sink =
136137
Transaction_pool.create ~config

src/lib/zkapp_vk_cache_tag/dune

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(library
2+
(public_name zkapp_vk_cache_tag)
3+
(libraries
4+
;; opam libraries
5+
core_kernel
6+
async
7+
;; local libraries
8+
logger
9+
disk_cache
10+
mina_base
11+
)
12+
(preprocess
13+
(pps ppx_mina ppx_version))
14+
(instrumentation (backend bisect_ppx)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Cache = Disk_cache.Make (Mina_base.Verification_key_wire.Stable.Latest)
2+
3+
type cache_db = Lmdb_cache of Cache.t | Identity_cache
4+
5+
type t =
6+
| Lmdb of { cache_id : Cache.id; cache_db : Cache.t }
7+
| Identity of Mina_base.Verification_key_wire.t
8+
9+
let read_key_from_disk = function
10+
| Lmdb t ->
11+
Cache.get t.cache_db t.cache_id
12+
| Identity key ->
13+
key
14+
15+
let write_key_to_disk db key =
16+
match db with
17+
| Lmdb_cache cache_db ->
18+
Lmdb { cache_id = Cache.put cache_db key; cache_db }
19+
| Identity_cache ->
20+
Identity key
21+
22+
let create_db path ~logger =
23+
Cache.initialize ~logger path
24+
|> Async.Deferred.Result.map ~f:(fun cache -> Lmdb_cache cache)
25+
26+
module For_tests = struct
27+
let create_db () = Identity_cache
28+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
open Core
2+
open Async
3+
4+
type t
5+
6+
type cache_db
7+
8+
val create_db :
9+
string
10+
-> logger:Logger.t
11+
-> (cache_db, [> `Initialization_error of Error.t ]) Deferred.Result.t
12+
13+
val read_key_from_disk : t -> Mina_base.Verification_key_wire.t
14+
15+
val write_key_to_disk : cache_db -> Mina_base.Verification_key_wire.t -> t
16+
17+
module For_tests : sig
18+
val create_db : unit -> cache_db
19+
end

0 commit comments

Comments
 (0)