text
stringlengths
12
786k
module T = struct type temporal_locality = P . temporal_locality = | None | Low | Moderate | High [ @@ deriving sexp , enumerate ] type operation = P . operation = | Read | Write [ @@ deriving sexp , enumerate ] end
let ints = List . init len ~ f ( : fun _ -> Random . int Int . max_value )
let floats = List . init len ~ f ( : fun _ -> Random . float Float . max_value )
let int_refs = List . map ints ~ f : create_untagged_int_ref
let float_refs = List . map floats ~ f : create_unboxed_float_ref
let int_refs_as_native_pointer = List . map ints ~ f : create_untagged_int_ref_as_native_pointer ; ;
let float_refs_as_native_pointer = List . map floats ~ f : create_unboxed_float_ref_as_native_pointer ; ;
let bigstring_of_string s = let a = Array1 . create char c_layout ( String . length s ) in for i = 0 to String . length s - 1 do a . { i } <- s . [ i ] done ; a ; ;
let bigstring = bigstring_of_string ( String . make 500 ' \ x00 ' )
let positions = [ 0 ; 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ; 8 ; 9 ; 10 ; 16 ; 32 ; 64 ; 256 ] List . iter all_of_operation ~ f ( : fun operation -> List . iter all_of_temporal_locality ~ f ( : fun temporal_locality -> printf " { !% sexp : operation } { % se...
let ocaml_keywords = [ " and " ; " as " ; " assert " ; " asr " ; " begin " ; " class " ; " constraint " ; " do " ; " done " ; " downto " ; " else " ; " end " ; " exception " ; " external " ; " false " ; " for " ; " fun ...
let ocaml_spec_chars = [ ' ' ; ! ' ' ; $ ' ' ; % ' ' ; & ' ' ; * ' ' ; + ' ' ; - ' . ' ; ' ' ; / ' ' ; : ' ; ' ; ' ' ; < ' ' ; = ' ' ; > ' ' ; ? ' ' ; @ ' ' ; ^ ' ' ; | ' ' ; ~ ' '...
let ocaml_comments_info = { single_comment = " " ; multi_comment_start = " " ; nest = true ; strings = true ; }
let java_keywords = [ " abstract " ; " continue " ; " for " ; " new " ; " switch " ; " assert " ; " default " ; " package " ; " synchronized " ; " boolean " ; " do " ; " if " ; " private " ; " this " ; " break " ; " double " ...
let java_spec_chars = [ ' ' ; ! ' ' ; $ ' ' ; % ' ' ; & ' ' ; * ' ' ; + ' ' ; - ' . ' ; ' ' ; / ' ' ; : ' ; ' ; ' ' ; < ' ' ; = ' ' ; > ' ' ; ? ' ' ; ^ ' ' ; | ' " ' ; \ ' ' ' ; ...
let java_comments_info = { single_comment = " " ; // multi_comment_start = " " ; /* multi_comment_end = " " ; */ nest = false ; strings = true ; }
let test_fun_str = " ) * quite the comment ) * let split_and_keep_on_spec_chars spec_chars str = let char_array = str_to_chr_arr str in ( List . fold_left ( fun acc_arr chr -> let str_of_chr = String . make 1 chr in if List . mem chr spec_chars then List . cons " " \\ ( List . cons st...
let expected_res_str = String . concat " " [ " letvvv = letv = vvin ( List . v ( funvv -> letv = String . v1vinifList . vvvthenList . v " ; " ( List . vvv ) elsematchvwith | v :: v ( -> String . v [ v ; v ] ) :: v [ ] |-> failwithv " ; " ) [ ] v ) ...
let test_fun_str4 = " wow " " \ ocaml_keywords ocaml_spec_chars false ) " letv = vinletv = 5inv " ) ; " remove_noise_9 " >:: ( fun _ -> assert_equal ( remove_noise ocaml_comments_info " let x = ' y ' " ocaml_keywords ocaml_spec_chars false ) " letv = v " ) ; ]
let normalized_sexp t = let rec of_t = function | Normalize . Sexp ( sexp , _ ) -> Some ( of_sexp sexp ) | Normalize . Comment _ -> None and of_sexp = function | Normalize . Atom str -> Sexp . Atom str | Normalize . List ts -> Sexp . List ( of_t_list ts ) and of_t_list ts = List ...
let normalize conf sexp = sexp |> sexp_to_sexp_or_comment |> Normalize . of_sexp_or_comment conf |> normalized_sexp ; ;
let conf = { Config . default with Config . atom_printing = Interpreted }
let test ~ input : input_sexp sexp = [ % test_result : Sexp . t ] ~ expect : sexp ( normalize conf input_sexp ) ; ; let atom = Sexp . Atom " Not connected to oculus monitor " in test ~ input : atom atom ; ; test ~ input ( : Sexp . Atom " Not connected to oculus monitor ( conn...
let conf = { Config . default with Config . atom_printing = Escaped }
let test ~ input : input_sexp sexp = [ % test_result : Sexp . t ] ~ expect : sexp ( normalize conf input_sexp ) ; ; let atom = Sexp . Atom " Not connected to oculus monitor " in test ~ input : atom atom ; ; let sexp = Sexp . Atom " ( this is a ( bona fide ) sexp ) " in t...
let test ( ? sticky_comments = Sexp_pretty . Config . default . sticky_comments ) ( ? atom_printing = Sexp_pretty . Config . default . atom_printing ) string = let lexbuf = Lexing . from_string string in let next ( ) = Option . try_with ( fun ( ) -> Sexp . With_layout . Par...
let conf = { ( Config . create ~ color : false ( ) ) with Config . atom_printing = Minimal_escaping } ; ; let sexp = Sexp . Atom ( String . concat ~ sep " :\ n " ( List . init 10 ~ f : Int . to_string ) ) in let pretty = pretty_string conf sexp in print_endline pretty ...
let test_safe_decode ( ) = let exception Custom_exception of string in let broken_encoding = Data_encoding . conv Fun . id ( fun _ -> raise ( Custom_exception " Should not leave the function scope " ) ) Data_encoding . unit in let actual = Prevalidation . Internal_for_tests . safe_binary_...
module Parameters : Requester_impl . PARAMETERS with type key = Operation_hash . t and type value = int = struct type key = Operation_hash . t type value = int end
module Hash : Requester . HASH with type t = Operation_hash . t = struct type t = Parameters . key let name = " test_with_key_Operation_hash_dot_t " let encoding = Operation_hash . encoding let pp = Operation_hash . pp end
module Test_request = Requester_impl . Simple_request ( Parameters )
module Test_disk_table = Requester_impl . Disk_memory_table ( Parameters )
module Test_Requester = Requester_impl . Make_memory_full_requester ( Hash ) ( Parameters ) ( Test_request )
let init_full_requester_disk ? global_input ( ) : Test_Requester . t * Test_Requester . store = let ( st : Test_Requester . store ) = Test_disk_table . create 16 in let requester = Test_Requester . create ? global_input ( ) st in ( requester , st )
let init_full_requester ? global_input ( ) : Test_Requester . t = fst ( init_full_requester_disk ? global_input ( ) )
let mk_operation n : Operation . t = let base = " BLuxtLkkNKWgV8xTzBuGcHJRPukgk4nY " in let base_len = String . length base in let n_string = Int . to_string n in let n_string_len = String . length n_string in assert ( 0 <= n_string_len && n_string_len <= String . length base ) ; let base_...
let test_db_leak f ( nb_ops : int ) ( _ : unit ) = let requester = init_full_requester ( ) in let max_table_size = 32 in assert ( nb_ops >= max_table_size ) ; let parameters = Classification . { map_size_limit = max_table_size ; on_discarded_operation = Test_Requester . clear_or_can...
let test_in_mempool_leak f ( nb_ops : int ) ( _ : unit ) = let requester = init_full_requester ( ) in let max_table_size = 32 in assert ( nb_ops >= max_table_size ) ; let parameters = Classification . { map_size_limit = max_table_size ; on_discarded_operation = Test_Requester . clea...
let test_db_do_not_clear_right_away f ( nb_ops : int ) ( _ : unit ) = let requester = init_full_requester ( ) in let max_table_size = 32 in assert ( nb_ops >= max_table_size ) ; let parameters = Classification . { map_size_limit = max_table_size ; on_discarded_operation = Test_Request...
let ( ) = let nb_ops = [ 64 ; 128 ] in let handle_refused_pair = [ ( ( fun tztrace -> Classification . add ( ` Refused tztrace ) ) , " handle_refused " ) ; ( ( fun tztrace -> Classification . add ( ` Outdated tztrace ) ) , " handle_outdated " ) ; ] in...
module Mock_protocol : Tezos_protocol_environment . PROTOCOL with type operation_data = unit and type operation_receipt = unit and type validation_state = unit = struct include Environment_protocol_T_test . Internal_for_tests . Mock_all_unit let begin_construction ~ chain_id : _ ~ predecessor_context : ...
module Init = struct let genesis_protocol = Protocol_hash . of_b58check_exn " ProtoDemoNoopsDemoNoopsDemoNoopsDemoNoopsDemo6XBoYp " let chain_id = Chain_id . zero let genesis_time = Time . Protocol . of_seconds 0L let wrap_tzresult_lwt ( f : Context . t -> unit tzresult Lwt . t ) ( ) : u...
let create_prevalidation ( module Mock_protocol : Tezos_protocol_environment . PROTOCOL with type operation_data = unit and type operation_receipt = unit and type validation_state = unit ) ctxt = let module Chain_store : Internal_for_tests . CHAIN_STORE with type chain_store = unit = struct type chain_s...
let now ( ) = Time . System . to_protocol ( Tezos_stdlib_unix . Systime_os . now ( ) )
let test_create ctxt = let live_operations = Operation_hash . Set . empty in let timestamp : Time . Protocol . t = now ( ) in let ( module Prevalidation ) = create_prevalidation ( module Mock_protocol ) ctxt in let predecessor : Store . Block . t = Init . genesis_block @@ Context . h...
let prevalidation_operations_gen ( type a ) ( module P : Prevalidation . T with type protocol_operation = a ) ( ~ n : int ) : a Prevalidation . operation list QCheck2 . Gen . t = let mk_operation ( hash , ( raw : Operation . t ) ) : P . protocol_operation Prevalidation . oper...
let mk_ops ( type a ) ( module P : Prevalidation . T with type protocol_operation = a ) : a Prevalidation . operation list = let ops = QCheck2 . Gen . generate1 ( prevalidation_operations_gen ( module P ) ~ n : nb_ops ) in assert ( Compare . List_length_with . ( ops = nb_ops ) ...
let test_apply_operation_crash ctxt = let live_operations = Operation_hash . Set . empty in let timestamp : Time . Protocol . t = now ( ) in let ( module P ) = create_prevalidation ( module Mock_protocol ) ctxt in let ops : P . protocol_operation Prevalidation . operation list = mk_ops ( ...
let ( ==> ) a b = ( not a ) || b
let mk_rand ( ) = QCheck2 . Gen . generate ~ n : 8 QCheck2 . Gen . int |> Array . of_list |> Random . State . make
let mk_live_operations ( type a ) rand ( ops : a Prevalidation . operation list ) = List . fold_left ( fun acc ( op : _ Prevalidation . operation ) -> if Random . State . bool rand then Operation_hash . Set . add ( Internal_for_tests . to_raw op |> Operation . hash ) acc else a...
let test_apply_operation_live_operations ctxt = let timestamp : Time . Protocol . t = now ( ) in let rand : Random . State . t = mk_rand ( ) in let ( module Protocol : Tezos_protocol_environment . PROTOCOL with type operation_data = unit and type operation_receipt = unit and type validation_...
let test_apply_operation_applied ctxt = let timestamp : Time . Protocol . t = now ( ) in let rand : Random . State . t = mk_rand ( ) in let ( module Protocol : Tezos_protocol_environment . PROTOCOL with type operation_data = unit and type operation_receipt = unit and type validation_state =...
let ( ) = Alcotest_lwt . run " mempool - prevalidation " [ ( " create " , [ Tztest . tztest " [ create ] returns Ok " ` Quick ( Init . wrap_tzresult_lwt test_create ) ; ] ) ; ( " apply_operation " , [ Tztest . tztest " [ apply_operation ] returns [ B...
let is_in_mempool oph t = Classification . is_in_mempool oph t <> None
module Operation_map = struct let pp_with_trace ppf map = Format . fprintf ppf " [ % a ] " ( Format . pp_print_list ( fun ppf ( oph , ( op , _tztrace ) ) -> Format . fprintf ppf " ( % a : ( % a , < tztrace ) ) " > Operation_hash . pp oph Operation . pp op . Pr...
type classification_event = | Add_if_not_present of Classification . classification * unit Prevalidation . operation | Remove of Operation_hash . t | Flush of bool
let drop oph t = let open Classification in let ( _ : ( unit Prevalidation . operation * classification ) option ) = remove oph t in ( )
let play_event event t = let open Classification in match event with | Add_if_not_present ( classification , op ) -> Generators . add_if_not_present classification op t | Remove oph -> drop oph t | Flush handle_branch_refused -> Internal_for_tests . flush ~ handle_branch_refused t
module Extra_generators = struct open Classification let event_gen t = let open QCheck2 . Gen in let add_gen = let + ( classification , op ) = pair Generators . classification_gen ( Generators . operation_with_hash_gen ( ) ) in Add_if_not_present ( classification , op ) in let remove_gen ...
let qcheck_eq_true ~ actual = let _ = qcheck_eq ' ~ pp : Format . pp_print_bool ~ expected : true ~ actual ( ) in ( )
let qcheck_eq_false ~ actual = let _ = qcheck_eq ' ~ pp : Format . pp_print_bool ~ expected : false ~ actual ( ) in ( )
let qcheck_bounded_map_is_empty bounded_map = let actual = bounded_map |> Classification . map |> Operation_hash . Map . is_empty in qcheck_eq_true ~ actual
let disjoint_union_classified_fields ? fail_msg ( t : unit Classification . t ) = let ( +> ) acc next_set = if not ( Operation_hash . Set . disjoint acc next_set ) then QCheck2 . Test . fail_reportf " Invariant ' The fields : [ refused ; outdated ; branch_refused ; \ branch_dela...
let check_invariants ? fail_msg ( t : unit Classification . t ) = let to_set map = Operation_hash . Map . to_seq map |> Seq . map fst |> Operation_hash . Set . of_seq in let expected_in_mempool = disjoint_union_classified_fields ? fail_msg t in let mempool_as_set = to_set t . in_mempool in if...
let classification_pp pp classification = Format . fprintf pp ( match classification with | ` Applied -> " Applied " | ` Prechecked -> " Prechecked " | ` Branch_delayed _ -> " Branch_delayed " | ` Branch_refused _ -> " Branch_refused " | ` Refused _ -> " Refused " | ` ...
let event_pp pp = function | Add_if_not_present ( classification , op ) -> Format . fprintf pp " Add_if_not_present % a % a " classification_pp classification Operation_hash . pp op . Prevalidation . hash | Remove oph -> Format . fprintf pp " Remove % a " Operation_hash . pp oph | Flu...
let test_flush_empties_all_except_refused_and_outdated = let open QCheck2 in Test . make ~ name : " [ flush ~ handle_branch_refused : true ] empties everything except [ refused ] \ and [ outdated ] " ( Generators . t_gen ( ) ) @@ fun t -> let refused_before = t . refused |> Class...
let test_flush_empties_all_except_refused_and_branch_refused = let open QCheck2 in Test . make ~ name : " [ flush ~ handle_branch_refused : false ] empties everything except \ [ refused ] , [ outdated ] and [ branch_refused ] " ( Generators . t_gen ( ) ) @@ fun t -> let refused...
let test_is_in_mempool_remove = let open QCheck2 in Test . make ~ name " [ : is_in_mempool ] and [ remove_ ] * are well - behaved " Generators . ( Gen . pair ( t_with_operation_gen ( ) ) unrefused_classification_gen ) @@ fun ( ( t , op ) , unrefused_classification ) -> C...
let test_is_applied = let open QCheck2 in Test . make ~ name " [ : is_applied ] is well - behaved " Generators . ( Gen . pair ( t_gen ( ) ) ( operation_with_hash_gen ( ) ) ) @@ fun ( t , op ) -> Classification . add ` Applied op t ; let oph = op . Prevalidation . ...
let test_invariants = let open QCheck2 in Test . make ~ name : " invariants are preserved through any sequence of events ( provided we do \ not [ add ] already present operations ) " Extra_generators . t_with_event_sequence_gen @@ fun ( t , events ) -> let _ = List . fold_left ( fun ( ...
module Unparsable = struct let test_add_is_known = let open QCheck2 in Test . make ~ name " [ : is_known_unparsable oph ( add_unparsable oph t ) ] holds " Generators . ( t_with_operation_gen ( ) ) @@ fun ( t , op ) -> let oph = op . Prevalidation . hash in Classification . add...
module Bounded = struct type binding = unit Prevalidation . operation type custom = unit Classification . t * [ ` Branch_delayed of tztrace | ` Branch_refused of tztrace | ` Refused of tztrace | ` Outdated of tztrace ] * binding list * binding list let custom_print : custom QCheck2 . Print ....
module To_map = struct let map_pp fmt x = let map_to_list m = Operation_hash . Map . to_seq m |> Seq . map ( fun ( _ , op ) -> op ) |> List . of_seq in let pp_pair fmt op = Format . fprintf fmt " % a :% a " Operation_hash . pp op . Prevalidation . hash Operation . pp op . raw...
let test_create_is_empty = let open QCheck2 in Test . make ~ name " [ : is_empty ( create params ) ] holds " Generators . parameters_gen @@ fun parameters -> let t = Classification . create parameters in qcheck_eq ' ~ expected : true ~ actual ( : Classification . is_empty t ) ( )
let test_create_add_not_empty = let open QCheck2 in Test . make ~ name " [ : not ( is_empty ( add _ _ _ t ) ) ] holds " ( Gen . pair ( Generators . t_with_operation_gen ( ) ) Generators . classification_gen ) @@ fun ( ( t , op ) , classification ) -> Classificatio...
let ( ) = let mk_tests label tests = ( label , qcheck_wrap tests ) in Alcotest . run " Prevalidator_classification " [ mk_tests " flush " [ test_flush_empties_all_except_refused_and_outdated ; test_flush_empties_all_except_refused_and_branch_refused ; ] ; mk_tests " is_in_mempool " ...
let force_opt ~ loc = function | Some x -> x | None -> QCheck2 . Test . fail_reportf " Unexpected None at % s " loc
let values_from_to ( ~ equal : ' a -> ' a -> bool ) ( tree : ' a Tree . tree ) ( start : ' a ) ( ancestor : ' a ) : ' a list = Tree . predecessors ~ equal tree start |> List_extra . take_until_if_found ~ pred ( ( : = ) ancestor ) |> force_opt ~ loc : __LOC__ ...
let op_set_pp fmt x = let set_to_list m = Operation_hash . Set . to_seq m |> List . of_seq in Format . fprintf fmt " % a " ( Format . pp_print_list Operation_hash . pp ) ( set_to_list x )
let op_map_pp fmt x = let pp_pair fmt ( hash , op ) = Format . fprintf fmt " % a :% a " Operation_hash . pp hash Operation . pp op . Prevalidation . raw in Format . fprintf fmt " % a " ( Format . pp_print_list pp_pair ) ( Operation_hash . Map . bindings x )
let qcheck_cond ? pp ~ cond e1 e2 ( ) = if cond e1 e2 then true else match pp with | None -> QCheck2 . Test . fail_reportf " [ @< h 0 > The condition check failed , but no pretty printer was \ provided . ] " @ | Some pp -> QCheck2 . Test . fail_reportf " [ @< v 2 > The conditi...
let blocks_to_oph_set ( blocks : Operation_hash . t list list list ) : Operation_hash . Set . t = List . concat blocks |> List . concat |> Operation_hash . Set . of_list
let is_subset ( m1 : unit Prevalidation . operation Op_map . t ) ( m2 : unit Prevalidation . operation Op_map . t ) = let rec go ( m1_seq : ( Operation_hash . t * unit Prevalidation . operation ) Seq . t ) = match m1_seq ( ) with | Seq . Nil -> true | Seq . Cons ( ( m1...
module Handle_operations = struct let dummy_classes = Classification . ( create { map_size_limit = 1 ; on_discarded_operation = ( fun _oph -> ( ) ) } ) let parse raw hash = Some ( Prevalidation . Internal_for_tests . make_operation hash raw ( ) ) let test_handle_live_operations_i...
module Recyle_operations = struct let classification_of_ops_gen ( ops : unit Prevalidation . operation Op_map . t ) : unit Classification . t QCheck2 . Gen . t = let open QCheck2 . Gen in let ops = Operation_hash . Map . bindings ops |> List . map snd in let length = List . length ops in ...
let ( ) = Alcotest . run " Prevalidator " [ ( " handle_operations " , qcheck_wrap Handle_operations . [ test_handle_live_operations_is_branch_alive ; test_handle_live_operations_path_spec ; test_handle_live_operations_clear ; test_handle_live_operations_inject ; ] ) ; ( " recy...
module CompareListQ = Compare . List ( Q )
let pending_of_list = List . fold_left ( fun pendings ( op , priority ) -> Pending_ops . add op priority pendings ) Pending_ops . empty
let test_iterators_ordering ~ name ~ iterator return_value = let open QCheck2 in Test . make ~ name : ( Format . sprintf " Ensure that % s returns operations in their priority ordering " name ) ( Gen . small_list ( Generators . operation_with_hash_and_priority_gen ( ) ) ) @@ fun ops ...
let test_iter_ordering = test_iterators_ordering ~ name " : iter " ~ iterator ( : fun f ops _acc -> Pending_ops . iter ( fun p h o -> f p h o ( ) ) ops ) ( )
let test_fold_ordering = test_iterators_ordering ~ name " : fold " ~ iterator : Pending_ops . fold ( )
let test_fold_es_ordering = test_iterators_ordering ~ name " : fold_es " ~ iterator : Pending_ops . fold_es return_unit
let test_partial_fold_es = let open QCheck2 in Test . make ~ name " : Test cardinal after partial iteration with fold_es " ( Gen . pair ( Gen . small_list ( Generators . operation_with_hash_and_priority_gen ( ) ) ) Gen . small_nat ) @@ fun ( ops , to_process ) -> let pending = ...
let ( ) = let mk_tests label tests = ( label , qcheck_wrap tests ) in Alcotest . run " Prevalidator_pending_operations " [ mk_tests " iter ordering " [ test_iter_ordering ] ; mk_tests " fold ordering " [ test_fold_ordering ] ; mk_tests " fold_es ordering " [ test_fold_es_or...
type int ' = int [ @@ deriving qcheck ] qcheck
let test_int ( ) = test_compare ~ msg " : Gen . int <=> deriving int " ~ eq : Alcotest . int Gen . int gen_int '
type unit ' = unit [ @@ deriving qcheck ] qcheck
let test_unit ( ) = test_compare ~ msg " : Gen . unit <=> deriving unit " ~ eq : Alcotest . unit Gen . unit gen_unit '