text stringlengths 0 601k |
|---|
let forge_timestamp ( ? delay = 0 ) ( ) = let time = Time . System . to_protocol @@ Systime_os . now ( ) in Time . Protocol . add time ( Int64 . of_int delay ) |
let forge_value ? delay ? timestamp ? peer ( ) = let peer = match peer with Some peer -> peer | None -> forge_peer_id ( ) in let timestamp = match timestamp with | Some timestamp -> timestamp | None -> forge_timestamp ? delay ( ) in ( timestamp , peer ) |
let test_threshold_negative ( ) = let heuristic = create ~ threshold ( :- 1 ) ~ latency : 120 in Assert . equal ~ prn ( get_status heuristic ) Not_synchronised ; update heuristic @@ forge_value ( ) ; Assert . equal ~ prn ( get_status heuristic ) Not_synchronised ; update heuristic @@ forge_value ( ) ; update heuristic ... |
let test_threshold_is_zero ( ) = let heuristic = create ~ threshold : 0 ~ latency : 120 in Assert . equal ~ prn ( get_status heuristic ) ( Synchronised { is_chain_stuck = false } ) ; update heuristic @@ forge_value ( ) ; Assert . equal ~ prn ( get_status heuristic ) ( Synchronised { is_chain_stuck = false } ) ; update ... |
let test_threshold_is_one ( ) = let heuristic = create ~ threshold : 1 ~ latency : 120 in Assert . equal ~ prn Not_synchronised ( get_status heuristic ) ; update heuristic @@ forge_value ( ) ; Assert . equal ~ prn ( Synchronised { is_chain_stuck = false } ) ( get_status heuristic ) ; update heuristic @@ forge_value ( )... |
let test_threshold_is_one_update_in_the_past ( ) = let latency = 120 in let heuristic = create ~ threshold : 1 ~ latency in Assert . equal ~ prn Not_synchronised ( get_status heuristic ) ; let peer = forge_peer_id ( ) in update heuristic @@ forge_value ~ peer ( ) ; Assert . equal ~ prn ( Synchronised { is_chain_stuck =... |
let test_threshold_is_one_value_in_the_past ( ) = let latency = 120 in let heuristic = create ~ threshold : 1 ~ latency in Assert . equal ~ prn Not_synchronised ( get_status heuristic ) ; update heuristic @@ forge_value ~ delay ( : latency * - 2 ) ( ) ; Assert . equal ~ prn Not_synchronised ( get_status heuristic ) ; u... |
let test_threshold_is_one_always_takes_best_timestamp ( ) = let latency = 120 in let heuristic = create ~ threshold : 1 ~ latency in Assert . equal ~ prn Not_synchronised ( get_status heuristic ) ; update heuristic @@ forge_value ( ) ; Assert . equal ~ prn ( Synchronised { is_chain_stuck = false } ) ( get_status heuris... |
let test_threshold_is_two ( ) = let latency = 120 in let heuristic = create ~ threshold : 2 ~ latency in Assert . equal ~ prn Not_synchronised ( get_status heuristic ) ; let peer = forge_peer_id ( ) in update heuristic @@ forge_value ~ peer ( ) ; Assert . equal ~ prn Not_synchronised ( get_status heuristic ) ; update h... |
let test_threshold_is_two_one_in_the_past ( ) = let latency = 120 in let heuristic = create ~ threshold : 2 ~ latency in Assert . equal ~ prn Not_synchronised ( get_status heuristic ) ; update heuristic @@ forge_value ( ) ; Assert . equal ~ prn Not_synchronised ( get_status heuristic ) ; let peer = forge_peer_id ( ) in... |
let test_threshold_is_two_one_in_the_past_and_one_more ( ) = let latency = 120 in let heuristic = create ~ threshold : 2 ~ latency in Assert . equal ~ prn Not_synchronised ( get_status heuristic ) ; let peer = forge_peer_id ( ) in update heuristic @@ forge_value ~ peer ~ delay ( :- 3 ) ( ) ; Assert . equal ~ prn Not_sy... |
let test_threshold_is_two_two_in_the_past ( ) = let latency = 120 in let heuristic = create ~ threshold : 2 ~ latency in Assert . equal ~ prn Not_synchronised ( get_status heuristic ) ; let timestamp = forge_timestamp ~ delay ( : latency * - 3 ) ( ) in update heuristic @@ forge_value ~ timestamp ( ) ; Assert . equal ~ ... |
let test_threshold_is_three ( ) = let latency = 120 in let heuristic = create ~ threshold : 3 ~ latency in Assert . equal ~ prn Not_synchronised ( get_status heuristic ) ; update heuristic @@ forge_value ( ) ; Assert . equal ~ prn Not_synchronised ( get_status heuristic ) ; update heuristic @@ forge_value ( ) ; Assert ... |
let test_threshold_is_three_and_stuck ( ) = let latency = 120 in let heuristic = create ~ threshold : 3 ~ latency in Assert . equal ~ prn Not_synchronised ( get_status heuristic ) ; let peer1 = forge_peer_id ( ) in let timestamp = forge_timestamp ~ delay ( : latency * - 2 ) ( ) in update heuristic @@ forge_value ~ peer... |
let test_counterexample_1 ( ) = let latency = 100 in let heuristic = create ~ threshold : 3 ~ latency in let p9 = forge_peer_id ( ) in let fresh = forge_peer_id ( ) in let p2 = forge_peer_id ( ) in let p7 = forge_peer_id ( ) in let p8 = forge_peer_id ( ) in let delay = - 279 in let peer = p9 in update heuristic @@ forg... |
let tests_raw : ( string * ( unit -> unit ) ) list = [ ( " Threshold negative " , test_threshold_negative ) ; ( " Threshold is zero " , test_threshold_is_zero ) ; ( " Threshold is one , update in the present " , test_threshold_is_one ) ; ( " Threshold is one , update in the past " , test_threshold_is_one_update_in_the_... |
let tests = List . map ( fun ( s , f ) -> Alcotest_lwt . test_case_sync s ` Quick f ) tests_raw |
let test_bp_create ( ) = let latency = 100 in let called = ref false in let when_status_changes _ = called := true ; Lwt . return_unit in let _heuristic = create ~ when_status_changes ~ threshold : 3 ~ latency ( ) in Assert . equal ! called false ; Lwt . return_unit |
let test_bp_activate ( ) = let latency = 100 in let called = ref false in let when_status_changes _ = called := true ; Lwt . return_unit in let heuristic = create ~ when_status_changes ~ threshold : 3 ~ latency ( ) in activate heuristic >>= fun ( ) -> Assert . equal ! called true ; Lwt . return_unit |
let test_bp_create_2 ( ) = let latency = 100 in let status_changes_called = ref false in let bootstrapped_changes_called = ref false in let when_status_changes _ = status_changes_called := true ; Lwt . return_unit in let when_bootstrapped_changes b = Assert . equal b true ; bootstrapped_changes_called := true ; Lwt . r... |
let test_bp_activate_2 ( ) = let latency = 100 in let status_changes_called = ref false in let bootstrapped_changes_called = ref false in let when_status_changes _ = status_changes_called := true ; Lwt . return_unit in let when_bootstrapped_changes b = Assert . equal b true ; bootstrapped_changes_called := true ; Lwt .... |
let test_force_bootstrapped ( ) = let latency = 100 in let bootstrapped_changes_called = ref 0 in let when_bootstrapped_changes b = let expected_value = if ! bootstrapped_changes_called = 0 then false else true in Assert . equal b expected_value ; incr bootstrapped_changes_called ; Lwt . return_unit in let heuristic = ... |
let test_force_bootstrapped_2 ( ) = let latency = 100 in let bootstrapped_changes_called = ref 0 in let when_bootstrapped_changes b = Assert . equal b false ; incr bootstrapped_changes_called ; Lwt . return_unit in let heuristic = create ~ when_bootstrapped_changes ~ threshold : 1 ~ latency ( ) in activate heuristic >>... |
let test_force_bootstrapped_3 ( ) = let latency = 100 in let bootstrapped_changes_called = ref 0 in let when_bootstrapped_changes _b = incr bootstrapped_changes_called ; Lwt . return_unit in let heuristic = create ~ when_bootstrapped_changes ~ threshold : 1 ~ latency ( ) in activate heuristic >>= fun ( ) -> force_boots... |
let test_is_bootstrapped ( ) = let latency = 100 in let heuristic = create ~ threshold : 1 ~ latency ( ) in activate heuristic >>= fun ( ) -> Assert . equal ( is_bootstrapped heuristic ) false ; Lwt . return_unit |
let test_is_bootstrapped_2 ( ) = let latency = 100 in let heuristic = create ~ threshold : 0 ~ latency ( ) in activate heuristic >>= fun ( ) -> Assert . equal ( is_bootstrapped heuristic ) true ; Lwt . return_unit |
let test_is_bootstrapped_3 ( ) = let latency = 100 in let heuristic = create ~ threshold : 1 ~ latency ( ) in activate heuristic >>= fun ( ) -> force_bootstrapped heuristic true >>= fun ( ) -> Assert . equal ( is_bootstrapped heuristic ) true ; Lwt . return_unit |
let test_bootstrapped ( ) = let latency = 100 in let heuristic = create ~ threshold : 0 ~ latency ( ) in activate heuristic >>= fun ( ) -> bootstrapped heuristic |
let test_bootstrapped_2 ( ) = let latency = 100 in let heuristic = create ~ threshold : 1 ~ latency ( ) in activate heuristic >>= fun ( ) -> let p = bootstrapped heuristic in Assert . equal ( Lwt . state p ) Lwt . Sleep ; force_bootstrapped heuristic true >>= fun ( ) -> p |
let test_threshold_is_three_and_stuck_with_callbacks ( ) = let latency = 120 in let status_changes = ref 0 in let when_status_changes status = let expected_value = if ! status_changes = 0 then Not_synchronised else if ! status_changes = 1 then Synchronised { is_chain_stuck = true } else if ! status_changes = 2 then Not... |
let wrap f _switch ( ) = f ( ) |
let tests_lwt_raw : ( string * ( Lwt_switch . t -> unit -> unit Lwt . t ) ) list = [ ( " test bp create " , wrap test_bp_create ) ; ( " test bp create 2 " , wrap test_bp_create_2 ) ; ( " test bp activate " , wrap test_bp_activate ) ; ( " test bp activate 2 " , wrap test_bp_activate_2 ) ; ( " test force bootstrapped " ,... |
let tests_lwt = List . map ( fun ( s , f ) -> Alcotest_lwt . test_case s ` Quick f ) tests_lwt_raw |
module type S = sig type status = Chain_validator_worker_state . Event . synchronisation_status = | Synchronised of { is_chain_stuck : bool } | Not_synchronised type t val update : t -> Time . Protocol . t * P2p_peer . Id . t -> unit val get_status : t -> status val create : threshold : int -> latency : int -> t end |
module Reference : S = struct type status = Chain_validator_worker_state . Event . synchronisation_status = | Synchronised of { is_chain_stuck : bool } | Not_synchronised type t = { threshold : int ; latency : int ; mutable candidates : ( Time . Protocol . t * P2p_peer . Id . t ) list ; } let rec replace_or_add ( ts , ... |
let forge_peer_id ( ) = let identity = P2p_identity . generate_with_pow_target_0 ( ) in identity . peer_id |
let peer_id = let open QCheck2 . Gen in let static = [ " P1 " ; " P2 " ; " P3 " ; " P4 " ; " P5 " ; " P6 " ; " P7 " ; " P8 " ; " P9 " ] |> List . map ( fun name -> pure ( forge_peer_id ( ) , name ) ) in delay ( fun ( ) -> oneof ( pure ( forge_peer_id ( ) , " fresh " ) :: static ) ) |
let now = Time . System . to_protocol @@ Systime_os . now ( ) |
let forge_timestamp ~ delay = Time . Protocol . add now ( Int64 . of_int delay ) |
let timestamp_pp n = let delay = Time . Protocol . diff n now in Format . asprintf " delay : % Ld " delay |
let timestamp = let open QCheck2 in Gen . map ( fun pre_delay -> let delay = ( pre_delay * 20 ) - 300 in forge_timestamp ~ delay ) Gen . ( oneof [ pure 5 ; 0 -- 20 ] ) |
let value = let open QCheck2 . Gen in pair timestamp peer_id |
let print_value ( time_stamp , ( _ , peer_id_str ) ) = Printf . sprintf " ( % s , % s ) " ( timestamp_pp time_stamp ) peer_id_str |
let values = let open QCheck2 . Gen in list value |
let print_values = QCheck2 . Print . list print_value |
let pp fmt = let open Reference in function | Synchronised { is_chain_stuck = true } -> Format . fprintf fmt " Synchronised ( stuck ) " | Not_synchronised -> Format . fprintf fmt " Not synchronised " | Synchronised { is_chain_stuck = false } -> Format . fprintf fmt " Synchronised ( not stuck ) " |
let make_tests check_update lcreate rcreate threshold latency = let open QCheck2 in let threshold_1 = Test . make ~ name : ( Format . asprintf " Shell . synchronisation_heuristic . equivalence - with - reference - implementation \ ( threshold % d ) ( latency % d ) " 1 latency ) ~ print : Print . ( pair print_value prin... |
let tests = let module L = Synchronisation_heuristic . Core in let module R = Reference in let check_update state_left state_right ( time_stamp , ( peer_id , _ ) ) = let value = ( time_stamp , peer_id ) in L . update state_left value ; R . update state_right value ; qcheck_eq ' ~ pp ~ expected ( : R . get_status state_... |
let ( ) = Alcotest . run " synchronisation heuristic fuzzy " [ ( " synchronisation heuristic fuzzy " , qcheck_wrap tests ) ] |
let assert_roundtrip printer encoder decoder str value = let e = Protobuf . Encoder . create ( ) in encoder value e ; assert_equal ~ printer ( : Printf . sprintf " % S " ) str ( Protobuf . Encoder . to_string e ) ; let d = Protobuf . Decoder . of_string str in assert_equal ~ printer value ( decoder d ) |
type b = bool [ @@ deriving protobuf ] |
let test_bool ctxt = assert_roundtrip string_of_bool b_to_protobuf b_from_protobuf " \ x08 \ x01 " true |
type i1 = int [ @@ deriving protobuf ] |
type i2 = int [ @ encoding ` zigzag ] [ @@ deriving protobuf ] |
type i3 = int [ @ encoding ` bits32 ] [ @@ deriving protobuf ] |
type i4 = int [ @ encoding ` bits64 ] [ @@ deriving protobuf ] |
type il1 = int32 [ @ encoding ` varint ] [ @@ deriving protobuf ] |
type il2 = int32 [ @ encoding ` zigzag ] [ @@ deriving protobuf ] |
type il3 = Int32 . t [ @@ deriving protobuf ] |
type il4 = int32 [ @ encoding ` bits64 ] [ @@ deriving protobuf ] |
type iL1 = int64 [ @ encoding ` varint ] [ @@ deriving protobuf ] |
type iL2 = int64 [ @ encoding ` zigzag ] [ @@ deriving protobuf ] |
type iL3 = int64 [ @ encoding ` bits32 ] [ @@ deriving protobuf ] |
type iL4 = Int64 . t [ @@ deriving protobuf ] |
let test_ints ctxt = assert_roundtrip string_of_int i1_to_protobuf i1_from_protobuf " \ x08 \ xac \ x02 " 300 ; assert_roundtrip string_of_int i2_to_protobuf i2_from_protobuf " \ x08 \ xac \ x02 " 150 ; assert_roundtrip string_of_int i3_to_protobuf i3_from_protobuf " \ x0d \ x2c \ x01 \ x00 \ x00 " 300 ; assert_roundtr... |
type ul1 = uint32 [ @ encoding ` varint ] [ @@ deriving protobuf ] |
type ul2 = uint32 [ @ encoding ` zigzag ] [ @@ deriving protobuf ] |
type ul3 = Uint32 . t [ @@ deriving protobuf ] |
type ul4 = uint32 [ @ encoding ` bits64 ] [ @@ deriving protobuf ] |
type uL1 = uint64 [ @ encoding ` varint ] [ @@ deriving protobuf ] |
type uL2 = uint64 [ @ encoding ` zigzag ] [ @@ deriving protobuf ] |
type uL3 = uint64 [ @ encoding ` bits32 ] [ @@ deriving protobuf ] |
type uL4 = Uint64 . t [ @@ deriving protobuf ] |
let test_uints ctxt = assert_roundtrip Uint32 . to_string ul1_to_protobuf ul1_from_protobuf " \ x08 \ xac \ x02 " ( Uint32 . of_int 300 ) ; assert_roundtrip Uint32 . to_string ul2_to_protobuf ul2_from_protobuf " \ x08 \ xac \ x02 " ( Uint32 . of_int 150 ) ; assert_roundtrip Uint32 . to_string ul3_to_protobuf ul3_from_p... |
type f1 = float [ @ encoding ` bits32 ] [ @@ deriving protobuf ] |
type f2 = float [ @@ deriving protobuf ] |
let test_floats ctxt = assert_roundtrip string_of_float f1_to_protobuf f1_from_protobuf " \ x0d \ x00 \ x00 \ xC0 \ x3f " 1 . 5 ; assert_roundtrip string_of_float f2_to_protobuf f2_from_protobuf " \ x09 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ xF8 \ x3f " 1 . 5 |
type s = string [ @@ deriving protobuf ] |
let test_string ctxt = assert_roundtrip ( fun x -> x ) s_to_protobuf s_from_protobuf " \ x0a \ x03abc " " abc " |
type by = bytes [ @@ deriving protobuf ] |
let test_string ctxt = assert_roundtrip ( fun x -> Bytes . to_string x ) by_to_protobuf by_from_protobuf " \ x0a \ x03abc " ( Bytes . of_string " abc " ) |
type o = int option [ @@ deriving protobuf ] |
let test_option ctxt = let printer x = match x with None -> " None " | Some v -> " Some " ^ ( string_of_int v ) in assert_roundtrip printer o_to_protobuf o_from_protobuf " " None ; assert_roundtrip printer o_to_protobuf o_from_protobuf " \ x08 \ xac \ x02 " ( Some 300 ) |
type l = int list [ @@ deriving protobuf ] |
let test_list ctxt = let printer x = x |> List . map string_of_int |> String . concat " , " in assert_roundtrip printer l_to_protobuf l_from_protobuf " " [ ] ; assert_roundtrip printer l_to_protobuf l_from_protobuf " \ x08 \ xac \ x02 \ x08 \ x2a " [ 300 ; 42 ] |
type a = int array [ @@ deriving protobuf ] |
let test_array ctxt = let printer x = Array . to_list x |> List . map string_of_int |> String . concat " , " in assert_roundtrip printer a_to_protobuf a_from_protobuf " " [ ] ; || assert_roundtrip printer a_to_protobuf a_from_protobuf " \ x08 \ xac \ x02 \ x08 \ x2a " [ | 300 ; 42 ] | |
type ts = int * string [ @@ deriving protobuf ] |
let test_tuple ctxt = let printer ( x , y ) = Printf . sprintf " % d , % s " x y in assert_roundtrip printer ts_to_protobuf ts_from_protobuf " \ x08 \ xac \ x02 \ x12 \ x08spartans " ( 300 , " spartans " ) |
type r1 = { r1a : int [ @ key 1 ] ; r1b : string [ @ key 2 ] ; |
let test_record ctxt = let printer r = Printf . sprintf " { r1a = % d , r1b = % s } " r . r1a r . r1b in assert_roundtrip printer r1_to_protobuf r1_from_protobuf " \ x08 \ xac \ x02 \ x12 \ x08spartans " { r1a = 300 ; r1b = " spartans " } |
type r2 = { r2a : r1 [ @ key 1 ] ; |
let test_nested ctxt = let printer r = Printf . sprintf " { r2a = { r1a = % d , r1b = % s } } " r . r2a . r1a r . r2a . r1b in assert_roundtrip printer r2_to_protobuf r2_from_protobuf " \ x0a \ x0d \ x08 \ xac \ x02 \ x12 \ x08spartans " { r2a = { r1a = 300 ; r1b = " spartans " } } |
type r3 = { r3a : ( int [ @ encoding ` bits32 ] ) * string [ @ key 1 ] ; |
let test_imm_tuple ctxt = let printer { r3a = a , b } = Printf . sprintf " { r3a = % d , % s } } " a b in assert_roundtrip printer r3_to_protobuf r3_from_protobuf " \ x0a \ x0f \ x0d \ x2c \ x01 \ x00 \ x00 \ x12 \ x08spartans " { r3a = 300 , " spartans " } |
let test_variant ctxt = let printer v = match v with | V1A -> " V1A " | V1B -> " V1B " | V1C i -> Printf . sprintf " V1C ( % d ) " i | V1D ( s1 , s2 ) -> Printf . sprintf " V1D ( % S , % S ) " s1 s2 in assert_roundtrip printer v1_to_protobuf v1_from_protobuf " \ x08 \ x02 " V1B ; assert_roundtrip printer v1_to_protobuf... |
type v2 = r4a : v2 [ @ key 1 ] [ @ bare ] |
let test_variant_bare ctxt = let printer { r4a } = match r4a with V2A -> " { r4a = V2A } " | V2B -> " { r4a = V2B } " in assert_roundtrip printer r4_to_protobuf r4_from_protobuf " \ x08 \ x02 " { r4a = V2B } |
type ' a r5 = { r5a : ' a [ @ key 1 ] |
let test_tvar ctxt = let printer f { r5a } = Printf . sprintf " { r5a = % s } " ( f r5a ) in assert_roundtrip ( printer string_of_int ) ( r5_to_protobuf i1_to_protobuf ) ( r5_from_protobuf i1_from_protobuf ) " \ x0a \ x02 \ x08 \ x01 " { r5a = 1 } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.