text
stringlengths
0
601k
module Michelson_signature : Signature . S with type t = Michelson_v1_primitives . prim = struct type t = Michelson_v1_primitives . prim let compare ( x : t ) ( y : t ) = Stdlib . compare x y let hash ( x : t ) = Hashtbl . hash x let pp fmtr prim = Format . fprintf fmtr " % s " ( Michelson_v1_primitives . string_of_pri...
module Michelson = Micheline_with_hash_consing . Make ( Michelson_signature ) ( struct let initial_size = None end )
module Michelson_path = Path . With_hash_consing ( struct let initial_size = None end )
module Michelson_pattern = Pattern . Make ( Michelson_signature ) ( Michelson ) ( Michelson_path )
module Michelson_rewriter = Rewrite . Make ( Michelson_signature ) ( Michelson ) ( Michelson_path ) ( Michelson_pattern )
let pattern = let open Michelson_pattern in seq ( any . @ any . @ any . @ focus ( prim Michelson_v1_primitives . I_ADDRESS list_empty ) . @ list_any )
let replacement = let open Michelson in seq [ prim Michelson_v1_primitives . I_ADDRESS [ ] [ ] ; prim Michelson_v1_primitives . I_CHAIN_ID [ ] [ ] ; prim Michelson_v1_primitives . I_PAIR [ ] [ ] ; ]
let rewrite_contract : Script_repr . expr -> Script_repr . expr = fun script -> let node = Micheline . root script in let node = Micheline . map_node ( fun _ -> Michelson . default_label ) ( fun h -> h ) node in let focuses = Michelson_rewriter . all_matches pattern node in match focuses with | [ ] -> assert false | pa...
let multisig_script_string = " parameter ( pair \ n \ \ ( pair : payload \ n \ \ ( nat % counter ) # counter , used to prevent replay attacks \ n \ \ ( or : action # payload to sign , represents the requested \ action \ n \ \ ( pair : transfer # transfer tokens \ n \ \ ( mutez % amount ) # amount to transfer \ n \ \ ( ...
let multisig_script : Script_repr . expr = let result = Tezos_micheline . Micheline_parser . no_parsing_error @@ Michelson_v1_parser . parse_toplevel ? check ( : Some true ) multisig_script_string >>? fun parsing_result -> ok parsing_result . Michelson_v1_parser . expanded in match result with | Ok script -> script | E...
let original_script_oracle = " { parameter \ n \ \ ( pair ( pair : payload \ n \ \ ( nat % counter ) \ n \ \ ( or : action \ n \ \ ( pair : transfer ( mutez % amount ) ( contract % dest unit ) ) \ n \ \ ( or ( option % delegate key_hash ) \ n \ \ ( pair % change_keys ( nat % threshold ) ( list % keys key ) ) ) ) ) \ n ...
let original_script = let script = Micheline_printer . printable Michelson_v1_primitives . string_of_prim multisig_script in Format . asprintf " % a " Micheline_printer . print_expr_unwrapped script
let ( ) = assert ( original_script_oracle = original_script )
let pattern_oracle = " Seq ( _ :: _ :: _ :: [ > [ ADDRESS ] ( [ ] ) ] < :: _ ) "
let pattern = Format . asprintf " % a " Michelson_pattern . pp pattern
let ( ) = assert ( pattern_oracle = pattern )
let rewritten_new = rewrite_contract multisig_script
let rewritten_script_oracle = " { parameter \ n \ \ ( pair ( pair : payload \ n \ \ ( nat % counter ) \ n \ \ ( or : action \ n \ \ ( pair : transfer ( mutez % amount ) ( contract % dest unit ) ) \ n \ \ ( or ( option % delegate key_hash ) \ n \ \ ( pair % change_keys ( nat % threshold ) ( list % keys key ) ) ) ) ) \ n...
let rewritten_script = let script = Micheline_printer . printable Michelson_v1_primitives . string_of_prim rewritten_new in Format . asprintf " % a " Micheline_printer . print_expr_unwrapped script
let ( ) = assert ( rewritten_script_oracle = rewritten_script )
let pp_result ppf r = let pp_ok ppf ( t , tz , count ) = Format . fprintf ppf " ( % a , % a , % d ) " stamp t ( pp_opt ~ pp_v : pp_int ) tz count in let pp_error ppf = function ` RFC3339 ( ( s , e ) , err ) -> Format . fprintf ppf " [ @< 1 >% d -% d :@ [ @% a ] ] " @@ s e Ptime . pp_rfc3339_error err in match r with | ...
let eq_result = let ok ( t , tz , c ) ( t ' , tz ' , c ' ) = Ptime . equal t t ' && tz = tz ' && c = c ' in let r_equal r r ' = match r , r ' with | Ok v , Ok v ' -> ok v v ' | Error e , Error e ' -> e = e ' | _ -> false in eq ~ eq : r_equal ~ pp : pp_result
let stamp_of_rfc3339 = let p s = Ptime . of_rfc3339 s in ( p $ pp_str @-> ret_get_result pp_result )
let stamp_of_date_time = Ptime . of_date_time $ raw_date_time @-> ret_get_option stamp
let stamp_of_s = ( Ptime . of_float_s $ pp_float @-> ret_get_option stamp )
let stamp_conversions = test " Stamp to RFC 3339 conversions " @@ fun ( ) -> let dt ? space ? frac_s ? tz_offset_s dt = Ptime . to_rfc3339 ? space ? frac_s ? tz_offset_s ( stamp_of_date_time dt ) in let stamp ? space ? frac_s ? tz_offset_s s = Ptime . to_rfc3339 ? space ? frac_s ? tz_offset_s s in let dt0 = ( 1999 , 01...
let parse = test " RFC 3339 to stamp conversions " @@ fun ( ) -> let edigit = ` Exp_chars [ ' 0 ' ; ' 1 ' ; ' 2 ' ; ' 3 ' ; ' 4 ' ; ' 5 ' ; ' 6 ' ; ' 7 ' ; ' 8 ' ; ' 9 ' ] in let etz = ` Exp_chars [ ' ' ; + ' ' ; - ' Z ' ; ' z ' ] in let etz_strict = ` Exp_chars [ ' ' ; + ' ' ; - ' Z ' ] in let edtsep = ` Exp_chars [ '...
let stamp_trips = test " Random stamps to RFC 3339 round trips " @@ fun ( ) -> let stamp_of_posix_s = Ptime . of_float_s $ pp_float @-> ( ret_get_option stamp ) in let trip ? tz_offset_s t = let back = stamp_of_posix_s ( floor ( Ptime . to_float_s t ) ) in let trip , tz , _ = stamp_of_rfc3339 ( Ptime . to_rfc3339 ? tz_...
let suite = suite " Ptime RFC 3339 support " [ stamp_conversions ; parse ; stamp_trips ]
module Node = P2p . Node . Make ( Node_id )
module View = P2p . View . Make ( Node_id ) ( Node )
module Ringcast = P2p_ringcast . Make ( Node_id ) ( Node ) ( View ) ( Msg_id )
let me = Node . init ( u64 16L )
let my_view = View . add ( Node . init ( u64 7L ) ) ( View . add ( Node . init ( u64 11L ) ) ( View . add ( Node . init ( u64 13L ) ) ( View . add ( Node . init ( u64 17L ) ) ( View . add ( Node . init ( u64 19L ) ) ( View . add ( Node . init ( u64 23L ) ) ( View . add ( Node . init ( u64 41L ) ) ( View . add ( Node . ...
let my_view_str = View . add ( Node . init ( u64 5L ) ) ( View . add ( Node . init ( u64 10L ) ) ( View . add ( Node . init ( u64 20L ) ) ( View . add ( Node . init ( u64 30L ) ) ( View . add ( Node . init ( u64 40L ) ) ( View . add ( Node . init ( u64 45L ) ) ( View . add ( Node . init ( u64 50L ) ) ( View . add ( Nod...
let my_recvd = View . add ( Node . init ( u64 5L ) ) ( View . add ( Node . init ( u64 10L ) ) ( View . add ( Node . init ( u64 20L ) ) ( View . add ( Node . init ( u64 30L ) ) View . empty ) ) )
let test_gossip _ctx = printf " \ nRINGCAST GOSSIP \ n " ; let view = my_view in let xview = my_view_str in let ( dst , sent , view ) = Ringcast . initiate ~ me ~ view ~ xview ~ view_len ~ xchg_len in let recvd = my_recvd in let dst = match dst with | Some dst -> dst | None -> assert_failure " No gossip target " in pf ...
let suite = " suite " >::: [ " gossip " >:: test_gossip ; ]
let ( ) = Nocrypto_entropy_unix . initialize ( ) ; run_test_tt_main suite
let sent_msgs = Hashtbl . create 3
module Node = P2p . Node . Make ( Node_id )
module View = P2p . View . Make ( Node_id ) ( Node )
module Ringcast = P2p_ringcast . Make ( Node_id ) ( Node ) ( View ) ( Msg_id )
module Io = struct type t = { node_id : Node_id . t ; in_chan : Lwt_io . input_channel ; out_chan : Lwt_io . output_channel ; } let init node_id in_chan out_chan = { node_id ; in_chan ; out_chan } let initiate_gossip t dst xchg = pf out " % a # INITIATE_GOSSIP to node % a \ n " Node_id . pp t . node_id Node . pp dst ; ...
module Ringcast_lwt = P2p_ringcast_lwt . Make ( Node_id ) ( Node ) ( View ) ( Msg_id ) ( Ringcast ) ( Io )
let rec read_chan ch rc node rnode = let % lwt recvd = Lwt_io . read_value ch in pf out " % a # READ_CHAN \ n " Node_id . pp ( Node . id node ) ; pf out " recvd :\ n % a \ n " View . pp recvd ; flush stdout ; let % lwt view = Ringcast_lwt . respond rc rnode recvd in pf out " recvd :\ n % a \ n " View . pp recvd ; pf ou...
let forward delay rc src msgid msg = let % lwt _ = Lwt_unix . sleep delay in Ringcast_lwt . forward rc src msgid ( Cstruct . of_string msg )
let check_sent_msgs delay = let % lwt _ = Lwt_unix . sleep delay in let s1 , s2 , s3 = Hashtbl . find sent_msgs ( Uint64 . of_int 1 ) , Hashtbl . find sent_msgs ( Uint64 . of_int 2 ) , Hashtbl . find sent_msgs ( Uint64 . of_int 3 ) in let % lwt _ = Lwt_io . printf " check_recvd_msgs : % d % d % d \ n " s1 s2 s3 in asse...
let _ = Nocrypto_entropy_lwt . initialize ( )
let ( ) = let view_len = 8 in let xchg_len = 4 in let period = 1 . 0 in let fanout = 5 in let seen_len = 10 in let ( in_ch1 , out_ch2 ) = Lwt_io . pipe ( ) in let ( in_ch2 , out_ch1 ) = Lwt_io . pipe ( ) in let node1 = Node . init ( u64 100L ) in let io1 = Io . init ( Node . id node1 ) in_ch1 out_ch1 in let view1 = Vie...
let account_pair = function [ a1 ; a2 ] -> ( a1 , a2 ) | _ -> assert false
let wrap e = Lwt . return ( Environment . wrap_tzresult e )
let check_rolls ( b : Block . t ) ( account : Account . t ) = Context . get_constants ( B b ) >>=? fun constants -> Context . Delegate . info ( B b ) account . pkh >>=? fun { staking_balance ; _ } -> let token_per_roll = constants . parametric . tokens_per_roll in let expected_rolls = Int64 . div ( Tez . to_mutez staki...
let check_no_rolls ( b : Block . t ) ( account : Account . t ) = Raw_context . prepare b . context ~ level : b . header . shell . level ~ predecessor_timestamp : b . header . shell . timestamp ~ timestamp : b . header . shell . timestamp ~ fitness : b . header . shell . fitness >>= wrap >>=? fun ctxt -> Roll_storage . ...
let test_simple_staking_rights ( ) = Context . init 2 >>=? fun ( b , accounts ) -> let ( a1 , _a2 ) = account_pair accounts in Context . Contract . balance ( B b ) a1 >>=? fun balance -> Context . Contract . manager ( B b ) a1 >>=? fun m1 -> Context . Delegate . info ( B b ) m1 . pkh >>=? fun info -> Assert . equal_tez...
let test_simple_staking_rights_after_baking ( ) = Context . init 2 >>=? fun ( b , accounts ) -> let ( a1 , a2 ) = account_pair accounts in Context . Contract . balance ( B b ) a1 >>=? fun balance -> Context . Contract . manager ( B b ) a1 >>=? fun m1 -> Context . Contract . manager ( B b ) a2 >>=? fun m2 -> Block . bak...
let frozen_deposit ( info : Context . Delegate . info ) = Cycle . Map . fold ( fun _ { Delegate . deposit ; _ } acc -> Test_tez . Tez . ( deposit + acc ) ) info . frozen_balance_by_cycle Tez . zero
let check_activate_staking_balance ~ loc ~ deactivated b ( a , ( m : Account . t ) ) = Context . Delegate . info ( B b ) m . pkh >>=? fun info -> Assert . equal_bool ~ loc info . deactivated deactivated >>=? fun ( ) -> Context . Contract . balance ( B b ) a >>=? fun balance -> let deposit = frozen_deposit info in Asser...
let run_until_deactivation ( ) = Context . init 2 >>=? fun ( b , accounts ) -> let ( a1 , a2 ) = account_pair accounts in Context . Contract . balance ( B b ) a1 >>=? fun balance_start -> Context . Contract . manager ( B b ) a1 >>=? fun m1 -> Context . Contract . manager ( B b ) a2 >>=? fun m2 -> check_activate_staking...
let test_deactivation_then_bake ( ) = run_until_deactivation ( ) >>=? fun ( b , ( ( ( _deactivated_contract , deactivated_account ) as deactivated ) , _start_balance ) , ( _a2 , _m2 ) ) -> Block . bake ~ policy ( : By_account deactivated_account . pkh ) b >>=? fun b -> check_activate_staking_balance ~ loc : __LOC__ ~ d...
let test_deactivation_then_self_delegation ( ) = run_until_deactivation ( ) >>=? fun ( b , ( ( ( deactivated_contract , deactivated_account ) as deactivated ) , start_balance ) , ( _a2 , m2 ) ) -> Op . delegation ( B b ) deactivated_contract ( Some deactivated_account . pkh ) >>=? fun self_delegation -> Block . bake ~ ...
let test_deactivation_then_empty_then_self_delegation ( ) = run_until_deactivation ( ) >>=? fun ( b , ( ( ( deactivated_contract , deactivated_account ) as deactivated ) , _start_balance ) , ( _a2 , m2 ) ) -> Context . Contract . balance ( B b ) deactivated_contract >>=? fun balance -> let sink_account = Account . new_...
let test_deactivation_then_empty_then_self_delegation_then_recredit ( ) = run_until_deactivation ( ) >>=? fun ( b , ( ( ( deactivated_contract , deactivated_account ) as deactivated ) , balance ) , ( _a2 , m2 ) ) -> let sink_account = Account . new_account ( ) in let sink_contract = Contract . implicit_contract sink_ac...
let test_delegation ( ) = Context . init 2 >>=? fun ( b , accounts ) -> let ( a1 , a2 ) = account_pair accounts in let m3 = Account . new_account ( ) in Account . add_account m3 ; Context . Contract . manager ( B b ) a1 >>=? fun m1 -> Context . Contract . manager ( B b ) a2 >>=? fun m2 -> let a3 = Contract . implicit_c...
let tests = [ Tztest . tztest " simple staking rights " ` Quick test_simple_staking_rights ; Tztest . tztest " simple staking rights after baking " ` Quick test_simple_staking_rights_after_baking ; Tztest . tztest " deactivation then bake " ` Quick test_deactivation_then_bake ; Tztest . tztest " deactivation then self ...
let testlib = Dl . ( dlopen ~ filename " : clib / libtest_functions . so " ~ flags [ : RTLD_NOW ] )
let test_root_lifetime _ = let alive = ref true in let ( ) = let v = [ | 1 ; 2 ; 3 ] | in Gc . finalise ( fun _ -> alive := false ) v ; in Gc . compact ( ) ; assert_equal false ! alive ~ msg " : values not registered as roots are collected " ; let alive = ref true in let _r = let v = [ | 1 ; 2 ; 3 ] | in Gc . finalise ...
let test_passing_roots _ = let save = foreign ~ from : testlib " save_ocaml_value " ( ptr void @-> returning void ) and retrieve = foreign ~ from : testlib " retrieve_ocaml_value " ( void @-> returning ( ptr void ) ) in let r = Root . create [ | ( + ) 1 ; ( * ) 2 ] | in begin save r ; Gc . compact ( ) ; let fs : ( int ...
let suite = " Root tests " >::: [ " root lifetime " >:: test_root_lifetime ; " passing roots " >:: test_passing_roots ; ]
let _ = run_test_tt_main suite
let x = of_string " x " in Exn . does_raise ( fun ( ) -> Fn . apply_n_times ~ n : Int . num_bits ( fun x -> x ^ x ) x ) ; ; [ % test_result : string ] ~ expect " " : ( concat ~ sep ( : of_string " , " ) [ ] |> to_string ) ; [ % test_result : string ] ~ expect " : one , two , three " ( concat ~ sep ( : of_string " , " )...
let a2 = of_string " aa "
let a3 = of_string " aaa "
let aba = of_string " aba "
let ab = of_string " ab "
let ba = of_string " ba " let test a b = require_equal [ % here ] ( module Bool ) ( String . is_prefix ( to_string a ) ~ prefix ( : to_string b ) ) ( is_prefix a ~ prefix : b ) in test a2 a3 ; test a3 a2 ; test ( a2 ^ a2 ) a2 ; test ( a2 ^ a2 ) a3 ; test ( a3 ^ a3 ) ( a2 ^ a2 ) ; test ( a3 ^ a3 ) ( a2 ^ a2 ^ a2 ) ; tes...
let ( >>>=? ) v f = v >|= Environment . wrap_tzresult >>=? f
let ( >>>?= ) v f = v |> Environment . wrap_tzresult >>?= f
let ( >>>? ) v f = v |> Environment . wrap_tzresult >>? f
type round_test = { round_duration : ( int * int ) list ; round_and_offset : ( int * ( int * int ) ) list ; timestamp_of_round : ( ( int * int * int ) * int ) list ; round_of_timestamp : ( ( int * int * int ) * int ) list ; }
let case_3_4 = { round_duration = [ ( 0 , 3 ) ; ( 1 , 4 ) ; ( 2 , 5 ) ; ( 3 , 6 ) ] ; round_and_offset = [ ( 0 , ( 0 , 0 ) ) ; ( 1 , ( 0 , 1 ) ) ; ( 2 , ( 0 , 2 ) ) ; ( 3 , ( 1 , 0 ) ) ; ( 4 , ( 1 , 1 ) ) ; ( 5 , ( 1 , 2 ) ) ; ( 6 , ( 1 , 3 ) ) ; ( 7 , ( 2 , 0 ) ) ; ( 8 , ( 2 , 1 ) ) ; ] ; timestamp_of_round = [ ( ( 10...
let case_3_6 = { round_duration = [ ( 0 , 3 ) ; ( 1 , 6 ) ; ( 2 , 9 ) ; ( 3 , 12 ) ; ( 4 , 15 ) ; ( 5 , 18 ) ; ( 6 , 21 ) ; ( 7 , 24 ) ; ( 8 , 27 ) ; ] ; round_and_offset = [ ( 0 , ( 0 , 0 ) ) ; ( 1 , ( 0 , 1 ) ) ; ( 2 , ( 0 , 2 ) ) ; ( 3 , ( 1 , 0 ) ) ; ( 4 , ( 1 , 1 ) ) ; ( 5 , ( 1 , 2 ) ) ; ( 6 , ( 1 , 3 ) ) ; ( 7 ,...
let test_cases = [ ( ( 3 , 1 ) , case_3_4 , " case_3_4 " ) ; ( ( 3 , 3 ) , case_3_6 , " case_3_6 " ) ; ]
let round_of_int i = Round_repr . of_int i |> Environment . wrap_tzresult
let mk_round_durations first_round_duration delay_increment_per_round = let first_round_duration = Period_repr . of_seconds_exn @@ Int64 . of_int first_round_duration in let delay_increment_per_round = Period_repr . of_seconds_exn @@ Int64 . of_int delay_increment_per_round in Stdlib . Option . get @@ Round_repr . Dura...
let process_test_case ( round_durations , ios , _ ) = let open Round_repr in List . iter_es ( fun ( i , o ) -> round_of_int i >>?= fun round -> let dur = Durations . round_duration round_durations round in Assert . equal_int64 ~ loc : __LOC__ ( Int64 . of_int o ) ( Period_repr . to_seconds dur ) ) ios . round_duration ...
let test_round ( ) = let final_test_cases = List . map ( fun ( ( first_round_duration , delay_increment_per_round ) , ios , name ) -> ( mk_round_durations first_round_duration delay_increment_per_round , ios , name ) ) test_cases in List . iter_es process_test_case final_test_cases
let ts_add ts period = match Timestamp . ( ts +? period ) with | Ok ts ' -> ts ' | Error _ -> Environment . Pervasives . failwith " timestamp add "
let test_round_of_timestamp ( ) = let duration0 = Period . of_seconds_exn 1L in Environment . wrap_tzresult @@ Round . Durations . create ~ first_round_duration : duration0 ~ delay_increment_per_round : Period . one_second >>?= fun round_durations -> let predecessor_timestamp = Time . Protocol . epoch in let level_star...
let round_of_timestamp_perf ( duration0_int64 , dipr ) = let duration0 = Period . of_seconds_exn duration0_int64 in let delay_increment_per_round = Period . of_seconds_exn dipr in let round_durations = Stdlib . Option . get @@ Round . Durations . create_opt ~ first_round_duration : duration0 ~ delay_increment_per_round...
let default_round_durations_list = [ ( 1L , 1L ) ; ( 1L , 2L ) ; ( 1L , 3L ) ; ( 2L , 3L ) ; ( 2L , 4L ) ]
let test_round_of_timestamp_perf ( ) = List . iter_es round_of_timestamp_perf default_round_durations_list
let timestamp_of_round_perf ( duration0_int64 , dipr ) = let duration0 = Period . of_seconds_exn duration0_int64 in let delay_increment_per_round = Period . of_seconds_exn dipr in let round_durations = Stdlib . Option . get @@ Round . Durations . create_opt ~ first_round_duration : duration0 ~ delay_increment_per_round...
let test_timestamp_of_round_perf ( ) = List . iter_es timestamp_of_round_perf default_round_durations_list
let test_error_is_triggered_for_too_high_timestamp ( ) = let round_durations = Stdlib . Option . get @@ Round . Durations . create_opt ~ first_round_duration : Period . one_second ~ delay_increment_per_round : Period . one_second in let predecessor_timestamp = Time . Protocol . epoch in let res = Round . round_of_times...
let rec ( --> ) i j = if Compare . Int . ( i > j ) then [ ] else i :: ( succ i --> j )
let ts_of_round_inverse ( duration0_int64 , dipr ) round_int = let first_round_duration = Period . of_seconds_exn duration0_int64 in let delay_increment_per_round = Period . of_seconds_exn dipr in let round_durations = Stdlib . Option . get @@ Round . Durations . create_opt ~ first_round_duration ~ delay_increment_per_...
let test_ts_of_round_inverse ( ) = List . iter_es ( fun durations -> List . iter_es ( ts_of_round_inverse durations ) ( ( 0 --> 20 ) @ ( 60000 --> 60010 ) ) ) default_round_durations_list >>=? fun ( ) -> List . iter_es ( ts_of_round_inverse ( 1L , 1L ) ) ( List . map ( fun i -> Int32 . to_int 134_217_727l - i ) ( 1 -->...
let round_of_ts_inverse ~ first_round_duration ~ delay_increment_per_round ts = Format . printf " ts = % Ld . " @ ts ; let first_round_duration = Period . of_seconds_exn first_round_duration in let delay_increment_per_round = Period . of_seconds_exn delay_increment_per_round in Round . Durations . create ~ first_round_...
let test_round_of_ts_inverse ( ) = List . iter_es ( fun ( first_round_duration , delay_increment_per_round ) -> List . iter_es ( fun ts -> round_of_ts_inverse ~ first_round_duration ~ delay_increment_per_round ( Int64 . of_int ts ) ) ( ( 0 --> 20 ) @ ( 60000 --> 60010 ) ) ) default_round_durations_list >>=? fun ( ) -> ...