text
stringlengths
0
601k
let log level fmt = if_level_appropriate_or_else ~ level ( Format . kasprintf ( wrap_lwt @@ emit level ) ) ( Format . ifprintf Format . std_formatter ) fmt
let log_lwt level fmt = if_level_appropriate_or_else ~ level ( Format . kasprintf ( emit level ) ) ( Format . ikfprintf ( fun _ -> Lwt . return_unit ) Format . std_formatter ) fmt
let debug fmt = log Debug fmt
let log_info fmt = log Info fmt
let log_notice fmt = log Notice fmt
let warn fmt = log Warning fmt
let log_error fmt = log Error fmt
let lwt_debug fmt = log_lwt Debug fmt
let lwt_log_info fmt = log_lwt Info fmt
let lwt_log_notice fmt = log_lwt Notice fmt
let lwt_warn fmt = log_lwt Warning fmt
let lwt_log_error fmt = log_lwt Error fmt
let main ( ) = let ( version , filename ) = if Array . length Sys . argv <> 3 then ( prerr_endline " Usage : rpc_openapi < VERSION > < API . json >\ n \ n \ Version is the version of the API , to be put in the " \ version " \ field \ of the output . \ n \ n \ Multiple input files are not supported . " ; exit ( if Array...
let ( ) = Printexc . record_backtrace true ; try main ( ) with exn -> Printexc . print_backtrace stderr ; prerr_endline ( Printexc . to_string exn ) ; exit 1
type ' a t = ( ' a , Rpc_error . t ) Result . t [ @@ deriving bin_io ]
type located_error = { location : string ; exn : Exn . t }
let uncaught_exn ~ location exn = Error ( Rpc_error . Uncaught_exn ( sexp_of_located_error { location ; exn } ) ) ; ;
let bin_io_exn ~ location exn = Error ( Rpc_error . Bin_io_exn ( sexp_of_located_error { location ; exn } ) ) ; ;
let try_with ? run ~ location f = let x = Monitor . try_with ? run f in let join = function | Ok x -> x | Error exn -> uncaught_exn ~ location exn in match Deferred . peek x with | None -> x >>| join | Some x -> return ( join x ) ; ;
let or_error ~ rpc_tag ~ rpc_version ~ connection_description ~ connection_close_started = function | Ok x -> Ok x | Error ( rpc_error : Rpc_error . t ) -> let rpc_error = Rpc_error . sexp_of_t rpc_error ~ get_connection_close_reason ( : fun ( ) -> let close_reason = Deferred . peek connection_close_started in [ % sexp...
let register_sc_rollup_address configuration dir = RPC_directory . register0 dir ( Sc_rollup_services . sc_rollup_address ( ) ) ( fun ( ) ( ) -> return @@ configuration . Configuration . sc_rollup_address )
let register configuration = RPC_directory . empty |> register_sc_rollup_address configuration
let start configuration = let Configuration . { rpc_addr ; rpc_port ; _ } = configuration in let rpc_addr = P2p_addr . of_string_exn rpc_addr in let host = Ipaddr . V6 . to_string rpc_addr in let dir = register configuration in let node = ` TCP ( ` Port rpc_port ) in let acl = RPC_server . Acl . default rpc_addr in Lwt...
type meth = [ ` GET | ` POST | ` DELETE | ` PUT | ` PATCH ]
let meth_encoding = let open Data_encoding in conv string_of_meth ( fun m -> match meth_of_string m with | None -> Stdlib . failwith " Cannot parse methods " | Some s -> s ) string
type ( ' + m , ' pr , ' p , ' q , ' i , ' o , ' e ) raw = ( ' m , ' pr , ' p , ' q , ' i , ' o , ' e ) Resto . MakeService ( RPC_encoding ) . t constraint ' meth = [ < meth ]
type error = Error_monad . error list
type ( ' + meth , ' prefix , ' params , ' query , ' input , ' output ) t = ( ' meth , ' prefix , ' params , ' query , ' input , ' output , error ) raw constraint ' meth = [ < meth ]
type ( ' + meth , ' prefix , ' params , ' query , ' input , ' output ) service = ( ' meth , ' prefix , ' params , ' query , ' input , ' output , error ) raw constraint ' meth = [ < meth ] Resto . MakeService ( RPC_encoding ) : module type of struct include Resto . MakeService ( RPC_encoding ) end with type ( ' + m , ' ...
let error_path = ref None
type Error_monad . error += Unparsable_RPC_error of Data_encoding . json
let ( ) = let open Error_monad in register_error_kind ` Branch ~ id " : RPC . Unexpected_error_encoding " ~ title " : RPC fails with an unparsable error message " ~ description : " The RPC returned with an error code , and the associated body was not a \ valid error trace . It is likely that the answer does not comes d...
let ( ) = let open Error_monad in register_error_kind ` Branch ~ id " : RPC . Empty_error_list " ~ title " : RPC returned an empty list of errors " ~ description " : The RPC returned with an error code but no associated error . " ~ pp ( : fun ppf ( ) -> Format . fprintf ppf " [ @< v 2 > The RPC returned with an error c...
let error_encoding = let open Data_encoding in delayed ( fun ( ) -> let { meth ; uri ; _ } = match ! error_path with None -> assert false | Some p -> p in def " error " ~ description : ( Printf . sprintf " The full list of errors is available with the global RPC ` % s % s ` " ( string_of_meth meth ) ( Uri . path_and_qu...
let error_opt_encoding = let open Data_encoding in delayed ( fun ( ) -> let { meth ; uri ; _ } = match ! error_path with None -> assert false | Some p -> p in def " error_opt " ~ description : ( Printf . sprintf " An optional error - trace ( None indicates no error ) . The full list \ of errors is available with the gl...
let get_service = get_service ~ error : error_encoding
let post_service = post_service ~ error : error_encoding
let delete_service = delete_service ~ error : error_encoding
let patch_service = patch_service ~ error : error_encoding
let put_service = put_service ~ error : error_encoding
let error_service = get_service ~ description " : Schema for all the RPC errors from the shell " ~ query : RPC_query . empty ~ output : Data_encoding . json_schema RPC_path . ( root / " errors " )
let ( ) = error_path := Some ( forge_request error_service ( ) ( ) )
let description_service = description_service ~ description " : RPCs documentation and input / output schema " error_encoding RPC_path . ( root / " describe " )
let node_wait_for_level level client = match Client . get_mode client with | Light ( _min_agreement , nodes ) -> Lwt_list . iter_p ( function | Client . Node node -> Lwt . map ( Fun . const ( ) ) @@ Node . wait_for_level node level | _ -> Lwt . return_unit ) nodes | Mockup -> assert false | Client ( Some ( Client . Nod...
let make_client_bake_for ( ) = let current_level = ref 1 in fun client -> let * ( ) = Client . bake_for client in current_level := ! current_level + 1 ; node_wait_for_level ! current_level client
let check_rpc ~ group_name ~ protocols ~ test_mode_tag ( ~ rpcs : ( string * ( ? endpoint : Client . endpoint -> Client . t -> unit Lwt . t ) * Protocol . parameter_overrides option * Node . argument list option ) list ) ( ) = let ( client_mode_tag , title_tag ) = match test_mode_tag with | ` Client -> ( ` Client , " c...
let test_contracts ? endpoint client = let client_bake_for = make_client_bake_for ( ) in let test_implicit_contract contract_id = let * _ = RPC . Contracts . get ? endpoint ~ hooks ~ contract_id client in let * _ = RPC . Contracts . get_balance ? endpoint ~ hooks ~ contract_id client in let * _ = RPC . Contracts . get_...
let test_delegates_on_registered_alpha ~ contracts ? endpoint client = Log . info " Test implicit baker contract " ; let bootstrap = List . hd contracts in let * _ = RPC . Delegates . get ? endpoint ~ hooks ~ pkh : bootstrap client in let * _ = RPC . Delegates . get_full_balance ? endpoint ~ hooks ~ pkh : bootstrap cli...
let test_delegates_on_registered_hangzhou ~ contracts ? endpoint client = Log . info " Test implicit baker contract " ; let bootstrap = List . hd contracts in let * _ = RPC . Delegates . get ? endpoint ~ hooks ~ pkh : bootstrap client in let * _ = RPC . Delegates . get_balance ? endpoint ~ hooks ~ pkh : bootstrap clien...
let test_delegates_on_unregistered_alpha ~ contracts ? endpoint client = Log . info " Test with a PKH that is not a registered baker contract " ; let unregistered_baker = " tz1c5BVkpwCiaPHJBzyjg7UHpJEMPTYA1bHG " in assert ( not @@ List . mem unregistered_baker contracts ) ; let * _ = RPC . Delegates . spawn_get ? endpo...
let test_delegates_on_unregistered_hangzhou ~ contracts ? endpoint client = Log . info " Test with a PKH that is not a registered baker contract " ; let unregistered_baker = " tz1c5BVkpwCiaPHJBzyjg7UHpJEMPTYA1bHG " in assert ( not @@ List . mem unregistered_baker contracts ) ; let * _ = RPC . Delegates . spawn_get ? en...
let get_contracts ? endpoint client = let * _ = RPC . Contracts . get_all ? endpoint ~ hooks client in let * contracts = RPC . Contracts . get_all_delegates ? endpoint ~ hooks client in Lwt . return contracts
let test_delegates protocol ? endpoint client = let * contracts = get_contracts ? endpoint client in let * ( ) = match protocol with | Protocol . Ithaca | Protocol . Alpha -> let * ( ) = test_delegates_on_registered_alpha ~ contracts ? endpoint client in test_delegates_on_unregistered_alpha ~ contracts ? endpoint clien...
let test_votes ? endpoint client = let client_bake_for = make_client_bake_for ( ) in let proto_hash = " ProtoDemoNoopsDemoNoopsDemoNoopsDemoNoopsDemo6XBoYp " in let * ( ) = Client . submit_proposals ~ proto_hash client in let * ( ) = client_bake_for client in let * _ = RPC . Votes . get_ballot_list ? endpoint ~ hooks c...
let test_tx_rollup ? endpoint client = let client_bake_for = make_client_bake_for ( ) in let * tx_rollup = Client . originate_tx_rollup ~ burn_cap : Tez . ( of_int 9999999 ) ~ storage_limit : 60_000 ~ src : Constant . bootstrap1 . public_key_hash client in let * ( ) = client_bake_for client in let * _ = RPC . Tx_rollup...
let test_others ? endpoint client = let * _ = RPC . get_constants ? endpoint ~ hooks client in let * _ = RPC . get_baking_rights ? endpoint ~ hooks client in let * _ = RPC . get_current_level ? endpoint ~ hooks client in let * _ = RPC . get_endorsing_rights ? endpoint ~ hooks client in let * _ = RPC . get_levels_in_cur...
let mempool_hooks = let replace_variable string = let replacements = [ ( " edsig \\ w { 94 } " , " [ SIGNATURE ] " ) ; ( " sig \\ w { 93 } " , " [ SIGNATURE ] " ) ; ( " o \\ w { 50 } " , " [ OPERATION_HASH ] " ) ; ( " B \\ w { 50 } " , " [ BRANCH_HASH ] " ) ; ( " vh \\ w { 50 } " , " [ BLOCK_PAYLOAD_HASH ] " ) ; ] in L...
let get_client_port client = match Client . get_mode client |> Client . mode_to_endpoint |> Option . map Client . rpc_port with | Some port -> port | None -> Test . fail " Client for mempool rpc tests should be initialized with a node or a \ proxy server only . Both have an endpoint and hence a RPC port so this \ shoul...
let mempool_node_flags = Node . [ Synchronisation_threshold 0 ; Connections 1 ; ]
let bake_empty_block ? endpoint client = let * mempool = Client . empty_mempool_file ( ) in Client . spawn_command ? endpoint client ( [ " bake " ; " for " ; Constant . bootstrap1 . alias ; " -- minimal - timestamp " ] @ let option_name = " -- operations - pool " in [ option_name ; mempool ] ) |> Process . check
let test_mempool protocol ? endpoint client = let * node = Node . init mempool_node_flags in let * ( ) = Client . Admin . trust_address ? endpoint client ~ peer : node in let * ( ) = Client . Admin . connect_address ? endpoint client ~ peer : node in let level = 1 in let * _ = Node . wait_for_level node level in let * ...
let start_with_acl address acl = let node = Node . create ~ rpc_host : address [ ] in let endpoint = Client . ( Node node ) in let * ( ) = Node . config_init node [ ] in Node . Config_file . update node ( JSON . update " rpc " ( JSON . put ( " acl " , acl ) ) ) ; let * ( ) = Node . identity_generate node in let * ( ) =...
let test_whitelist address ( ) = let whitelist = JSON . annotate ~ origin " : whitelist " @@ ` A [ ` O [ ( " address " , ` String address ) ; ( " whitelist " , ` A [ ` String " GET / describe " ; /** ` String " GET / chains " ] /** ) ; ] ; ] in let * client = start_with_acl address whitelist in let * success_resp = Cli...
let test_blacklist address ( ) = let blacklist = JSON . annotate ~ origin " : blacklist " @@ ` A [ ` O [ ( " address " , ` String address ) ; ( " blacklist " , ` A [ ` String " GET / chains " ] ) ; /** ] ; ] in let * client = start_with_acl address blacklist in let * ( ) = Client . spawn_rpc GET [ " chains " ; " main "...
let binary_regression_test ( ) = let node = Node . create ~ rpc_host " : 127 . 0 . 0 . 1 " [ ] in let endpoint = Client . ( Node node ) in let * ( ) = Node . config_init node [ ] in let * ( ) = Node . identity_generate node in let * ( ) = Node . run node [ ] in let * json_client = Client . init ~ endpoint ~ media_type ...
let test_node_binary_mode address ( ) = let node = Node . create ~ media_type : Binary ~ rpc_host : address [ ] in let endpoint = Client . ( Node node ) in let * ( ) = Node . config_init node [ ] in let * ( ) = Node . identity_generate node in let * ( ) = Node . run node [ ] in let * client = Client . init ~ endpoint ~...
let test_no_service_at_valid_prefix address ( ) = let node = Node . create ~ rpc_host : address [ ] in let endpoint = Client . ( Node node ) in let * ( ) = Node . config_init node [ ] in let * ( ) = Node . identity_generate node in let * ( ) = Node . run node [ ] in let * client = Client . init ~ endpoint ( ) in let * ...
let register ( ) = Regression . register ~ __FILE__ ~ title " : Binary RPC regression tests " ~ tags [ " : rpc " ; " regression " ; " binary " ] ~ output_file " : binary_rpc " ~ regression_output_path " : tezt / _regressions / rpc " / binary_regression_test ; let alpha_consensus_threshold = [ ( [ " consensus_threshold ...
module Result = struct module Infix = struct let ( >>= ) r f = match r with Ok x -> f x | Error _ as e -> e let ( >>| ) r f = match r with Ok x -> Ok ( f x ) | Error _ as e -> e end let map_error ~ f = function Ok x -> Ok x | Error x -> Error ( f x ) end
module IO = struct type ' a t = ' a type ic = in_channel type oc = out_channel let ( >>= ) x f = f x let return x = x let read ic = match Csexp . input ic with | Ok x -> return ( Some x ) | Error _ -> return None let write oc lx = List . iter ( Csexp . to_channel oc ) lx ; Stdlib . flush oc ; return ( ) end
module Ocf = Make ( IO )
let supported_versions = List . map Version . to_string [ V2 ; V1 ]
type close = unit -> unit
type state = Uninitialized | Running of Ocf . client * close | Errored
let state : state ref = ref Uninitialized
let start ( ) = let prog = Sys . argv . ( 1 ) in let argv = [ " | ocamlformat - rpc " ] | in ( match let input , output = Unix . open_process_args prog argv in let pid = Unix . process_pid ( input , output ) in Ocf . pick_client ~ pid input output supported_versions >>| fun client -> let close = match client with | ` V...
let get_client ( ) = match ! state with | Uninitialized -> start ( ) | Running ( cl , _ ) -> let i , _ = Unix . waitpid [ WNOHANG ] ( Ocf . pid cl ) in if i = 0 then Ok cl else start ( ) | Errored -> Error ` No_process
let close_client ( ) = match ! state with | Uninitialized -> ( ) | Running ( cl , close ) -> let i , _ = Unix . waitpid [ WNOHANG ] ( Ocf . pid cl ) in if i = 0 then close ( ) else ( ) | Errored -> ( )
let config c = get_client ( ) >>= fun cl -> log " [ ocf ] Config \ n " %! ; Ocf . config c cl
let format x = get_client ( ) >>= fun cl -> log " [ ocf ] Format ' % s ' \ n " %! x ; Ocf . format x cl
let halt ( ) = get_client ( ) >>= fun cl -> log " [ ocf ] Halt \ n " %! ; Ocf . halt cl >>| fun ( ) -> close_client ( ) ; state := Uninitialized
let protect_unit x = match x with | Ok ( ) -> ( ) | Error ( ` Msg e ) -> log " Error : % s \ n " %! e | Error ` No_process -> log " No process \ n " %!
let protect_string x = match x with | Ok s -> log " [ @< hv > Output ; :@% s ] @\ n " %! s | Error ( ` Msg e ) -> log " Error : % s \ n " %! e | Error ` No_process -> log " No process \ n " %!
let ( ) = log " Starting then doing nothing \ n " %! ; protect_unit @@ halt ( )
let ( ) = log " Sending requests \ n " %! ; protect_unit @@ config [ ( " profile " , " janestreet " ) ] ; protect_string @@ format " char -> string " ; protect_unit @@ halt ( )
module With_limit : sig type ' a t = private { t : ' a ; max_message_size : int } [ @@ deriving sexp_of ] val create : ' a -> max_message_size : int -> ' a t val message_size_ok : _ t -> payload_len : int -> bool val check_message_size : _ t -> payload_len : int -> unit type ' a t = { t : ' a ; max_message_size : int }...
module Unix_reader = struct open With_limit type t = Reader . t With_limit . t [ @@ deriving sexp_of ] let create ~ reader ~ max_message_size = With_limit . create reader ~ max_message_size let close t = Reader . close t . t let is_closed t = Reader . is_closed t . t let all_unit_then_return l ret_val = match l with | ...
module Unix_writer = struct open With_limit type t = Writer . t With_limit . t [ @@ deriving sexp_of ] let create ~ writer ~ max_message_size = Writer . set_raise_when_consumer_leaves writer false ; With_limit . create writer ~ max_message_size ; ; let close t = Writer . close t . t let is_closed t = Writer . is_closed...
module Reader = struct include Kernel_transport . Reader let of_reader ~ max_message_size reader = pack ( module Unix_reader ) ( Unix_reader . create ~ reader ~ max_message_size ) ; ; end
module Writer = struct include Kernel_transport . Writer let of_writer ~ max_message_size writer = pack ( module Unix_writer ) ( Unix_writer . create ~ writer ~ max_message_size ) ; ; end
type t = Kernel_transport . t = { reader : Reader . t ; writer : Writer . t }
let of_reader_writer ~ max_message_size reader writer = { reader = Reader . of_reader reader ~ max_message_size ; writer = Writer . of_writer writer ~ max_message_size } ; ;
let of_fd ? buffer_age_limit ? reader_buffer_size ~ max_message_size fd = of_reader_writer ~ max_message_size ( Async_unix . Reader . create ? buf_len : reader_buffer_size fd ) ( Async_unix . Writer . create ? buffer_age_limit fd ) ; ;
module Send_result = Kernel_transport . Send_result : Core . Unix . File_descr . t -> buf1 : Bigstring . t -> pos1 : int -> len1 : int -> buf2 : Bigstring . t -> pos2 : int -> len2 : int -> Unix . Syscall_result . Int . t = " async_extra_rpc_writev2_byte " " async_extra_rpc_writev2 " [ @@ noalloc ]
module Config = struct let default_write_timeout = Time_ns . Span . of_min 2 . let default_max_message_size = Int . max_value let default_max_buffer_size = Int . max_value let default_start_batching_after_num_messages = 2 let default_initial_buffer_size = 64 * 1024 let default_buffering_threshold_in_bytes = 32 * 1024 t...
let set_nonblocking fd = Fd . with_file_descr_exn fd ignore ~ nonblocking : true
module Reader_internal = struct type t = { fd : Fd . t ; config : Config . t ; mutable reading : bool ; mutable closed : bool ; close_finished : unit Ivar . t ; mutable buf : ( Bigstring . t [ @ sexp . opaque ] ) ; mutable pos : int ; mutable max : int } [ @@ deriving sexp_of , fields ] let create fd config = set_nonbl...
module Writer_internal = struct type flush = { pos : Int63 . t ; ivar : unit Ivar . t } [ @@ deriving sexp_of ] let get_job_number ( ) = Scheduler . num_jobs_run ( ) module Connection_state : sig type t [ @@ deriving sexp_of ] val create : unit -> t val is_currently_accepting_writes : t -> bool val is_able_to_send_data...
let make_create f ( ? config = Config . default ) ~ max_message_size fd = let max_message_size = min config . max_message_size max_message_size in let config = Config . validate { config with max_message_size } in f fd config ; ;
module Reader = struct include Kernel_transport . Reader let create_internal fd config = pack ( module Reader_internal ) ( Reader_internal . create fd config ) ; ; let create = make_create create_internal end