Skip to content

Commit 914fe20

Browse files
committed
Merge branch 'dkijania/transaction_pool_tests' into mrmr1993/transaction_pool_tests
2 parents caa9b60 + 1d3a4f4 commit 914fe20

32 files changed

+197
-182
lines changed

src/lib/crypto/kimchi_backend/kimchi_backend.mli

+2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ module Pasta : sig
164164

165165
val field_size : Pasta_bindings.BigInt256.t
166166

167+
module Cvar = Kimchi_pasta.Pallas_based_plonk.Cvar
167168
module Verification_key = Kimchi_pasta.Pallas_based_plonk.Verification_key
168169

169170
module R1CS_constraint_system =
@@ -197,6 +198,7 @@ module Pasta : sig
197198

198199
val field_size : Pasta_bindings.BigInt256.t
199200

201+
module Cvar = Kimchi_pasta.Vesta_based_plonk.Cvar
200202
module Verification_key = Kimchi_pasta.Vesta_based_plonk.Verification_key
201203
module R1CS_constraint_system =
202204
Kimchi_pasta.Vesta_based_plonk.R1CS_constraint_system

src/lib/crypto/kimchi_backend/pasta/kimchi_pasta.ml

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Pallas_based_plonk = struct
77

88
let field_size = Pallas_based_plonk.field_size
99

10+
module Cvar = Pallas_based_plonk.Cvar
1011
module Verification_key = Pallas_based_plonk.Verification_key
1112
module R1CS_constraint_system = Pallas_based_plonk.R1CS_constraint_system
1213
module Constraint = R1CS_constraint_system.Constraint
@@ -26,6 +27,7 @@ module Vesta_based_plonk = struct
2627

2728
let field_size = Vesta_based_plonk.field_size
2829

30+
module Cvar = Vesta_based_plonk.Cvar
2931
module Verification_key = Vesta_based_plonk.Verification_key
3032
module R1CS_constraint_system = Vesta_based_plonk.R1CS_constraint_system
3133
module Constraint = R1CS_constraint_system.Constraint

src/lib/crypto/kimchi_backend/pasta/kimchi_pasta.mli

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Pallas_based_plonk : sig
77

88
val field_size : Pasta_bindings.BigInt256.t
99

10+
module Cvar = Pallas_based_plonk.Cvar
1011
module Verification_key = Pallas_based_plonk.Verification_key
1112
module R1CS_constraint_system = Pallas_based_plonk.R1CS_constraint_system
1213
module Constraint = R1CS_constraint_system.Constraint
@@ -26,6 +27,7 @@ module Vesta_based_plonk : sig
2627

2728
val field_size : Pasta_bindings.BigInt256.t
2829

30+
module Cvar = Vesta_based_plonk.Cvar
2931
module Verification_key = Vesta_based_plonk.Verification_key
3032
module R1CS_constraint_system = Vesta_based_plonk.R1CS_constraint_system
3133
module Constraint = R1CS_constraint_system.Constraint

src/lib/crypto/kimchi_backend/pasta/pallas_based_plonk.ml

+2
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,6 @@ module Oracles = Plonk_dlog_oracles.Make (struct
181181
end
182182
end)
183183

184+
module Cvar = Kimchi_pasta_snarky_backend.Pallas_based_plonk.Cvar
185+
184186
module Run_state = Kimchi_pasta_snarky_backend.Pallas_based_plonk.Run_state

src/lib/crypto/kimchi_backend/pasta/vesta_based_plonk.ml

+2
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,6 @@ module Oracles = Plonk_dlog_oracles.Make (struct
179179
end
180180
end)
181181

182+
module Cvar = Kimchi_pasta_snarky_backend.Vesta_based_plonk.Cvar
183+
182184
module Run_state = Kimchi_pasta_snarky_backend.Vesta_based_plonk.Run_state

src/lib/crypto/kimchi_pasta_snarky_backend/kimchi_pasta_snarky_backend.ml

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ module Vesta_based_plonk = struct
3535

3636
let field_size = Field.size
3737

38+
module Cvar = Snarky_backendless.Cvar.Make (Field)
39+
3840
module R1CS_constraint_system =
3941
Plonk_constraint_system.Make
4042
(Field)
@@ -73,6 +75,8 @@ module Pallas_based_plonk = struct
7375

7476
let field_size = Field.size
7577

78+
module Cvar = Snarky_backendless.Cvar.Make (Field)
79+
7680
module R1CS_constraint_system =
7781
Plonk_constraint_system.Make
7882
(Field)

src/lib/crypto/kimchi_pasta_snarky_backend/plonk_constraint_system.ml

+1
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ module type Snark_intf = sig
748748
include
749749
Snarky_backendless.Snark_intf.Run
750750
with type field := field
751+
and type field_var = field Snarky_backendless.Cvar.t
751752
and type Constraint.t =
752753
(field Snarky_backendless.Cvar.t, field) Plonk_constraint.basic
753754
end

src/lib/network_pool/indexed_pool.ml

+62-51
Original file line numberDiff line numberDiff line change
@@ -678,27 +678,27 @@ let revalidate :
678678
-> [ `Entire_pool | `Subset of Account_id.Set.t ]
679679
-> (Account_id.t -> Account.t)
680680
-> t * Transaction_hash.User_command_with_valid_signature.t Sequence.t =
681-
fun t ~logger scope f ->
681+
fun t_initial ~logger scope f ->
682682
let requires_revalidation =
683683
match scope with
684684
| `Entire_pool ->
685685
Fn.const true
686686
| `Subset subset ->
687687
Set.mem subset
688688
in
689-
Map.fold t.all_by_sender ~init:(t, Sequence.empty)
689+
Map.fold t_initial.all_by_sender ~init:(t_initial, Sequence.empty)
690690
~f:(fun
691691
~key:sender
692692
~data:(queue, currency_reserved)
693-
((t', dropped_acc) as acc)
693+
((t, dropped_acc) as acc)
694694
->
695695
if not (requires_revalidation sender) then acc
696696
else
697697
let account : Account.t = f sender in
698698
let current_balance =
699699
Currency.Balance.to_amount
700700
(Account.liquid_balance_at_slot
701-
~global_slot:(global_slot_since_genesis t.config)
701+
~global_slot:(global_slot_since_genesis t_initial.config)
702702
account )
703703
in
704704
[%log debug]
@@ -724,14 +724,14 @@ let revalidate :
724724
then (
725725
[%log debug]
726726
"Account no longer has permission to send; dropping queue" ;
727-
let dropped, t'' = remove_with_dependents_exn' t first_cmd in
728-
(t'', Sequence.append dropped_acc dropped) )
727+
let dropped, t_updated = remove_with_dependents_exn' t first_cmd in
728+
(t_updated, Sequence.append dropped_acc dropped) )
729729
else if Account_nonce.(account.nonce < first_nonce) then (
730730
[%log debug]
731731
"Current account nonce precedes first nonce in queue; dropping \
732732
queue" ;
733-
let dropped, t'' = remove_with_dependents_exn' t first_cmd in
734-
(t'', Sequence.append dropped_acc dropped) )
733+
let dropped, t_updated = remove_with_dependents_exn' t first_cmd in
734+
(t_updated, Sequence.append dropped_acc dropped) )
735735
else
736736
(* current_nonce >= first_nonce *)
737737
let first_applicable_nonce_index =
@@ -748,62 +748,73 @@ let revalidate :
748748
"Current account nonce succeeds first nonce in queue; splitting \
749749
queue at $index"
750750
~metadata:[ ("index", `Int first_applicable_nonce_index) ] ;
751-
let drop_queue, keep_queue =
751+
let dropped_for_nonce, retained_for_nonce =
752752
F_sequence.split_at queue first_applicable_nonce_index
753753
in
754-
let currency_reserved' =
754+
let currency_reserved_partially_updated =
755755
F_sequence.foldl
756756
(fun c cmd ->
757757
Option.value_exn
758758
Currency.Amount.(c - Option.value_exn (currency_consumed cmd))
759759
)
760-
currency_reserved drop_queue
760+
currency_reserved dropped_for_nonce
761761
in
762-
let keep_queue', currency_reserved'', dropped_for_balance =
762+
let keep_queue, currency_reserved_updated, dropped_for_balance =
763763
drop_until_sufficient_balance
764-
(keep_queue, currency_reserved')
764+
(retained_for_nonce, currency_reserved_partially_updated)
765765
current_balance
766766
in
767767
let to_drop =
768-
Sequence.append (F_sequence.to_seq drop_queue) dropped_for_balance
768+
Sequence.append
769+
(F_sequence.to_seq dropped_for_nonce)
770+
dropped_for_balance
769771
in
770-
match Sequence.next to_drop with
771-
| None ->
772-
acc
773-
| Some (head, tail) ->
774-
let t'' =
775-
Sequence.fold tail
776-
~init:
777-
(remove_all_by_fee_and_hash_and_expiration_exn
778-
(remove_applicable_exn t' head)
779-
head )
780-
~f:remove_all_by_fee_and_hash_and_expiration_exn
781-
in
782-
let t''' =
783-
match F_sequence.uncons keep_queue' with
784-
| None ->
785-
{ t'' with
786-
all_by_sender = Map.remove t''.all_by_sender sender
787-
}
788-
| Some (first_kept, _) ->
789-
let first_kept_unchecked =
790-
Transaction_hash.User_command_with_valid_signature.command
791-
first_kept
792-
in
793-
{ t'' with
794-
all_by_sender =
795-
Map.set t''.all_by_sender ~key:sender
796-
~data:(keep_queue', currency_reserved'')
797-
; applicable_by_fee =
798-
Map_set.insert
799-
( module Transaction_hash
800-
.User_command_with_valid_signature )
801-
t''.applicable_by_fee
802-
(User_command.fee_per_wu first_kept_unchecked)
803-
first_kept
804-
}
805-
in
806-
(t''', Sequence.append dropped_acc to_drop) )
772+
let keeping_prefix = F_sequence.is_empty dropped_for_nonce in
773+
let keeping_suffix = Sequence.is_empty dropped_for_balance in
774+
(* t with all_by_sender and applicable_by_fee fields updated *)
775+
let t_partially_updated =
776+
match F_sequence.uncons keep_queue with
777+
| _ when keeping_prefix && keeping_suffix ->
778+
(* Nothing dropped, nothing needs to be updated *)
779+
t
780+
| None ->
781+
(* We drop the entire queue, first element needs to be removed from
782+
applicable_by_fee *)
783+
let t' = remove_applicable_exn t first_cmd in
784+
{ t' with all_by_sender = Map.remove t'.all_by_sender sender }
785+
| Some _ when keeping_prefix ->
786+
(* We drop only transactions from the end of queue, keeping
787+
the head untouched, no need to update applicable_by_fee *)
788+
{ t with
789+
all_by_sender =
790+
Map.set t.all_by_sender ~key:sender
791+
~data:(keep_queue, currency_reserved_updated)
792+
}
793+
| Some (first_kept, _) ->
794+
(* We need to replace old queue head with the new queue head
795+
in applicable_by_fee *)
796+
let first_kept_unchecked =
797+
Transaction_hash.User_command_with_valid_signature.command
798+
first_kept
799+
in
800+
let t' = remove_applicable_exn t first_cmd in
801+
{ t' with
802+
all_by_sender =
803+
Map.set t'.all_by_sender ~key:sender
804+
~data:(keep_queue, currency_reserved_updated)
805+
; applicable_by_fee =
806+
Map_set.insert
807+
(module Transaction_hash.User_command_with_valid_signature)
808+
t'.applicable_by_fee
809+
(User_command.fee_per_wu first_kept_unchecked)
810+
first_kept
811+
}
812+
in
813+
let t_updated =
814+
Sequence.fold ~init:t_partially_updated
815+
~f:remove_all_by_fee_and_hash_and_expiration_exn to_drop
816+
in
817+
(t_updated, Sequence.append dropped_acc to_drop) )
807818

808819
let expired_by_global_slot (t : t) :
809820
Transaction_hash.User_command_with_valid_signature.t Sequence.t =

src/lib/pickles/common.ml

+5-3
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,11 @@ let ft_comm ~add:( + ) ~scale ~negate
250250
f_comm + chunked_t_comm
251251
+ negate (scale chunked_t_comm plonk.zeta_to_domain_size)
252252

253-
let combined_evaluation (type f)
254-
(module Impl : Snarky_backendless.Snark_intf.Run with type field = f)
255-
~(xi : Impl.Field.t) (without_degree_bound : _ list) =
253+
let combined_evaluation (type f v)
254+
(module Impl : Snarky_backendless.Snark_intf.Run
255+
with type field = f
256+
and type field_var = v ) ~(xi : Impl.Field.t)
257+
(without_degree_bound : _ list) =
256258
let open Impl in
257259
let open Field in
258260
let mul_and_add ~(acc : Field.t) ~(xi : Field.t)

src/lib/pickles/common.mli

+6-8
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@ val dlog_pcs_batch :
5757
-> ('a, 'total, Pickles_types.Nat.z) Pickles_types.Pcs_batch.t
5858

5959
val combined_evaluation :
60-
(module Snarky_backendless.Snark_intf.Run with type field = 'f)
61-
-> xi:'f Snarky_backendless.Cvar.t
62-
-> ( 'f Snarky_backendless.Cvar.t
63-
, 'f Snarky_backendless.Cvar.t Snarky_backendless.Snark_intf.Boolean0.t )
64-
Pickles_types.Opt.t
65-
array
66-
list
67-
-> 'f Snarky_backendless.Cvar.t
60+
(module Snarky_backendless.Snark_intf.Run
61+
with type field = 'f
62+
and type field_var = 'v )
63+
-> xi:'v
64+
-> ('v, 'v Snarky_backendless.Boolean.t) Pickles_types.Opt.t array list
65+
-> 'v
6866

6967
module Max_degree : sig
7068
val wrap_log2 : int

src/lib/pickles/composition_types/composition_types.mli

+7-13
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ module Wrap : sig
814814
(** A layout of the raw data in a statement, which is needed for
815815
representing it inside the circuit. *)
816816
val spec :
817-
'a Spec.impl
817+
('f, 'v) Spec.impl
818818
-> ('challenge1, 'challenge2, 'field1, 'field2) Lookup_parameters.t
819819
-> Opt.Flag.t Plonk_types.Features.t
820820
-> ( ( 'field1
@@ -1190,13 +1190,10 @@ module Step : sig
11901190
val typ :
11911191
('b, 'c) Step_impl.Typ.t
11921192
-> assert_16_bits:(Step_impl.Field.t -> unit)
1193-
-> ( ( Step_impl.Field.Constant.t Limb_vector.Challenge.t
1194-
, Step_impl.Field.Constant.t Limb_vector.Challenge.t
1195-
Scalar_challenge.t
1193+
-> ( ( Step_impl.Field.t
1194+
, Step_impl.Field.t Scalar_challenge.t
11961195
, 'b
1197-
, ( Step_impl.Field.Constant.t Limb_vector.Challenge.t
1198-
Scalar_challenge.t
1199-
Bulletproof_challenge.t
1196+
, ( Step_impl.Field.t Scalar_challenge.t Bulletproof_challenge.t
12001197
, Backend.Tock.Rounds.n )
12011198
Vector.t
12021199
, Step_impl.Field.t
@@ -1237,13 +1234,10 @@ module Step : sig
12371234
assert_16_bits:(Wrap_impl.Field.t -> unit)
12381235
-> (Opt.Flag.t Plonk_types.Features.t, 'n) Vector.t
12391236
-> ('b, 'a) Wrap_impl.Typ.t
1240-
-> ( ( ( ( Wrap_impl.Field.Constant.t Limb_vector.Challenge.t
1241-
, Wrap_impl.Field.Constant.t Limb_vector.Challenge.t
1242-
Scalar_challenge.t
1237+
-> ( ( ( ( Wrap_impl.Field.t
1238+
, Wrap_impl.Field.t Scalar_challenge.t
12431239
, 'b
1244-
, ( Wrap_impl.Field.Constant.t Limb_vector.Challenge.t
1245-
Scalar_challenge.t
1246-
Bulletproof_challenge.t
1240+
, ( Wrap_impl.Field.t Scalar_challenge.t Bulletproof_challenge.t
12471241
, Backend.Tock.Rounds.n )
12481242
Vector.t
12491243
, Wrap_impl.Field.t

src/lib/pickles/composition_types/spec.ml

+10-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ open Pickles_types
44
open Hlist
55
module Sc = Kimchi_backend_common.Scalar_challenge
66

7-
type 'f impl = (module Snarky_backendless.Snark_intf.Run with type field = 'f)
7+
type ('f, 'v) impl =
8+
(module Snarky_backendless.Snark_intf.Run
9+
with type field = 'f
10+
and type field_var = 'v )
811

912
module type Branch_data_checked = sig
1013
type field_var
@@ -199,8 +202,9 @@ struct
199202
end
200203
end
201204

202-
let pack_basic (type field other_field other_field_var branch_data_var)
203-
((module Impl) : field impl)
205+
let pack_basic
206+
(type field field_var other_field other_field_var branch_data_var)
207+
((module Impl) : (field, field_var) impl)
204208
((module Branch_data_checked) : (branch_data_var, Impl.Field.t) branch_data)
205209
=
206210
let open Impl in
@@ -235,7 +239,7 @@ let pack_basic (type field other_field other_field_var branch_data_var)
235239
in
236240
{ pack }
237241

238-
let pack (type f) ((module Impl) as impl : f impl) branch_data t =
242+
let pack (type f v) ((module Impl) as impl : (f, v) impl) branch_data t =
239243
let open Impl in
240244
pack
241245
(pack_basic impl branch_data)
@@ -381,10 +385,7 @@ struct
381385
let (Typ typ) = typ t is_boolean spec in
382386
let constant_var =
383387
let fields, aux = typ.value_to_fields x in
384-
let fields =
385-
Array.map fields ~f:(fun x ->
386-
Snarky_backendless.Cvar.Constant x )
387-
in
388+
let fields = Array.map ~f:Impl.Field.constant fields in
388389
typ.var_of_fields (fields, aux)
389390
in
390391
let open Impl.Typ in
@@ -477,10 +478,7 @@ struct
477478
let (T (Typ typ, f, f')) = etyp e is_boolean spec in
478479
let constant_var =
479480
let fields, aux = typ.value_to_fields x in
480-
let fields =
481-
Array.map fields ~f:(fun x ->
482-
Snarky_backendless.Cvar.Constant x )
483-
in
481+
let fields = Array.map ~f:Impl.Field.constant fields in
484482
typ.var_of_fields (fields, aux)
485483
in
486484
(* We skip any constraints that would be added here, but we *do* use

0 commit comments

Comments
 (0)