text
stringlengths
0
601k
let ( ) = let open Data_encoding in register_error_kind ` Branch ~ id " : RPC_context . Not_found " ~ title " : RPC lookup failed " ~ description : " RPC lookup failed . No RPC exists at the URL or the RPC tried to access \ non - existent data . " ( obj2 ( req " method " RPC_service . meth_encoding ) ( req " uri " RPC_...
let ( ) = let open Data_encoding in register_error_kind ` Branch ~ id " : RPC_context . Gone " ~ title " : RPC lookup failed because of deleted data " ~ description : " RPC lookup failed . Block has been pruned and requested data deleted . " ( obj2 ( req " method " RPC_service . meth_encoding ) ( req " uri " RPC_encodi...
let gen_register dir service handler = register dir service ( fun p q i -> Lwt . catch ( fun ( ) -> handler p q i ) ( function | Not_found -> RPC_answer . not_found | exn -> RPC_answer . fail [ Exn exn ] ) )
let gen_register = ( gen_register : _ -> _ -> ( _ -> _ -> _ -> _ RPC_answer . t Lwt . t ) -> _ :> _ -> _ -> ( _ -> _ -> _ -> [ < _ RPC_answer . t ] Lwt . t ) -> _ )
let register dir service handler = gen_register dir service ( fun p q i -> let open Lwt_syntax in let * r = handler p q i in match r with Ok o -> RPC_answer . return o | Error e -> RPC_answer . fail e )
let register_chunked dir service handler = gen_register dir service ( fun p q i -> let open Lwt_syntax in let * r = handler p q i in match r with | Ok o -> RPC_answer . return_chunked o | Error e -> RPC_answer . fail e )
let opt_register dir service handler = gen_register dir service ( fun p q i -> let open Lwt_syntax in let * ro = handler p q i in match ro with | Ok ( Some o ) -> RPC_answer . return o | Ok None -> RPC_answer . not_found | Error e -> RPC_answer . fail e )
let lwt_register dir service handler = gen_register dir service ( fun p q i -> let open Lwt_syntax in let * o = handler p q i in RPC_answer . return o )
let register0 root s f = register root s ( curry Z f )
let register0_chunked root s f = register_chunked root s ( curry Z f )
let register1 root s f = register root s ( curry ( S Z ) f )
let register2 root s f = register root s ( curry ( S ( S Z ) ) f )
let register3 root s f = register root s ( curry ( S ( S ( S Z ) ) ) f )
let register4 root s f = register root s ( curry ( S ( S ( S ( S Z ) ) ) ) f )
let register5 root s f = register root s ( curry ( S ( S ( S ( S ( S Z ) ) ) ) ) f )
let opt_register0 root s f = opt_register root s ( curry Z f )
let opt_register1 root s f = opt_register root s ( curry ( S Z ) f )
let opt_register2 root s f = opt_register root s ( curry ( S ( S Z ) ) f )
let opt_register3 root s f = opt_register root s ( curry ( S ( S ( S Z ) ) ) f )
let opt_register4 root s f = opt_register root s ( curry ( S ( S ( S ( S Z ) ) ) ) f )
let opt_register5 root s f = opt_register root s ( curry ( S ( S ( S ( S ( S Z ) ) ) ) ) f )
let gen_register0 root s f = gen_register root s ( curry Z f )
let gen_register1 root s f = gen_register root s ( curry ( S Z ) f )
let gen_register2 root s f = gen_register root s ( curry ( S ( S Z ) ) f )
let gen_register3 root s f = gen_register root s ( curry ( S ( S ( S Z ) ) ) f )
let gen_register4 root s f = gen_register root s ( curry ( S ( S ( S ( S Z ) ) ) ) f )
let gen_register5 root s f = gen_register root s ( curry ( S ( S ( S ( S ( S Z ) ) ) ) ) f )
let lwt_register0 root s f = lwt_register root s ( curry Z f )
let lwt_register1 root s f = lwt_register root s ( curry ( S Z ) f )
let lwt_register2 root s f = lwt_register root s ( curry ( S ( S Z ) ) f )
let lwt_register3 root s f = lwt_register root s ( curry ( S ( S ( S Z ) ) ) f )
let lwt_register4 root s f = lwt_register root s ( curry ( S ( S ( S ( S Z ) ) ) ) f )
let lwt_register5 root s f = lwt_register root s ( curry ( S ( S ( S ( S ( S Z ) ) ) ) ) f )
let protocols = [ ( " alpha " , " Alpha " , Some " / include / rpc_introduction . rst . inc " , " ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK " ) ; ( " " , " 011 Hangzhou " , Some " / include / rpc_introduction . rst . inc " , " PtHangz2aRngywmSRGGvrcTyMbbdpWdpFKuS4uMWxg2RaH9i1qx " ) ; ( " 012 " , " 012 Ithaca ...
let pp_name ppf = function | [ ] | [ " " ] -> Format . pp_print_string ppf " " / | prefix -> Format . pp_print_string ppf ( String . concat " " / prefix )
let ref_of_service ( prefix , meth ) = Format . asprintf " % s_ % s " ( Resto . string_of_meth meth ) ( Re . Str . global_replace ( Re . Str . regexp " ( [ ] ) " ) <\\^>*\\> " \\ 1 " ( String . concat " " -- prefix ) )
module Index = struct let rec pp prefix ppf dir = let open Resto . Description in match dir with | Empty -> Format . fprintf ppf " Empty " | Static { services ; subdirs = None } -> pp_services prefix ppf services | Static { services ; subdirs = Some ( Suffixes map ) } -> Format . fprintf ppf " [ @< v 2 >% a @ @ % a ] "...
module Description = struct module Query = struct let pp_arg fmt = let open RPC_arg in function { name ; _ } -> Format . fprintf fmt " <% s " > name let pp_title_item ppf = let open RPC_description in function | { name ; kind ; _ } -> ( match kind with | Single arg | Optional arg -> Format . fprintf ppf " [ % s =% a ] ...
let pp_document ppf descriptions version = Format . fprintf ppf " % a . " @ Rst . pp_raw_html Rst . style ; Format . fprintf ppf " % a . " @ Rst . pp_raw_html Rst . script ; Format . pp_set_margin ppf 10000 ; Format . pp_set_max_indent ppf 9000 ; let version_suffix = if version = " " then " " else " _ " ^ version in Fo...
let make_index node required_version = let shell_dir = Node . build_rpc_directory node in let protocol_dirs = List . map ( fun ( version , name , intro , hash ) -> let hash = Protocol_hash . of_b58check_exn hash in let ( module Proto ) = match Registered_protocol . get hash with | None -> Format . eprintf " Hash not fo...
let make_default_acl _node = let addr_of_string addr = P2p_point . Id . { addr ; port = None ; peer_id = None } in let policy = let open Tezos_rpc_http_server . RPC_server . Acl in put_policy ( addr_of_string " 127 . 0 . 0 . 1 " , allow_all ) empty_policy |> put_policy ( addr_of_string " any . public . address " , secu...
let main node = let cmd = Sys . argv . ( 1 ) in match cmd with | " index " -> make_index node Sys . argv . ( 2 ) | " acl " -> make_default_acl node | _ -> raise ( Invalid_argument cmd )
let ( ) = Lwt_main . run ( Node_helpers . with_node main )
type ' a t = ' a Data_encoding . t
type schema = Data_encoding . json_schema * Data_encoding . Binary_schema . t
let untyped = Data_encoding . ( obj1 ( req " untyped " string ) )
let conv f g t = Data_encoding . conv ~ schema ( : Data_encoding . Json . schema t ) f g t
let schema ? definitions_path t = ( Data_encoding . Json . schema ? definitions_path t , Data_encoding . Binary . describe t )
let schema_encoding = let open Data_encoding in obj2 ( req " json_schema " json_schema ) ( req " binary_schema " Data_encoding . Binary_schema . encoding )
let arg_encoding = let open Data_encoding in conv ( fun { Resto . Arg . name ; descr } -> ( ( ) , name , descr ) ) ( fun ( ( ) , name , descr ) -> { name ; descr } ) ( obj3 ( req " id " ( constant " single " ) ) ( req " name " string ) ( opt " descr " string ) )
let multi_arg_encoding = let open Data_encoding in conv ( fun { Resto . Arg . name ; descr } -> ( ( ) , name , descr ) ) ( fun ( ( ) , name , descr ) -> { name ; descr } ) ( obj3 ( req " id " ( constant " multiple " ) ) ( req " name " string ) ( opt " descr " string ) )
let meth_encoding = Data_encoding . string_enum [ ( " GET " , ` GET ) ; ( " POST " , ` POST ) ; ( " DELETE " , ` DELETE ) ; ( " PUT " , ` PUT ) ; ( " PATCH " , ` PATCH ) ; ]
let path_item_encoding = let open Data_encoding in union [ case ( Tag 0 ) string ~ title " : PStatic " ( function PStatic s -> Some s | _ -> None ) ( fun s -> PStatic s ) ; case ( Tag 1 ) arg_encoding ~ title " : PDynamic " ( function PDynamic s -> Some s | _ -> None ) ( fun s -> PDynamic s ) ; case ( Tag 2 ) multi_arg...
let query_kind_encoding = let open Data_encoding in union [ case ( Tag 0 ) ~ title " : Single " ( obj1 ( req " single " arg_encoding ) ) ( function Single s -> Some s | _ -> None ) ( fun s -> Single s ) ; case ( Tag 1 ) ~ title " : Optional " ( obj1 ( req " optional " arg_encoding ) ) ( function Optional s -> Some s | ...
let query_item_encoding = let open Data_encoding in conv ( fun { name ; description ; kind } -> ( name , description , kind ) ) ( fun ( name , description , kind ) -> { name ; description ; kind } ) ( obj3 ( req " name " string ) ( opt " description " string ) ( req " kind " query_kind_encoding ) )
let service_descr_encoding = let open Data_encoding in conv ( fun { meth ; path ; description ; query ; input ; output ; error } -> ( meth , path , description , query , Option . map Lazy . force input , Lazy . force output , Lazy . force error ) ) ( fun ( meth , path , description , query , input , output , error ) ->...
let directory_descr_encoding = let open Data_encoding in mu " service_tree " @@ fun directory_descr_encoding -> let static_subdirectories_descr_encoding = union [ case ( Tag 0 ) ~ title " : Suffixes " ( obj1 ( req " suffixes " ( list ( obj2 ( req " name " string ) ( req " tree " directory_descr_encoding ) ) ) ) ) ( fun...
let description_request_encoding = let open Data_encoding in conv ( fun { recurse } -> recurse ) ( function recurse -> { recurse } ) ( obj1 ( dft " recursive " bool false ) )
let uri_encoding = let open Data_encoding in conv Uri . to_string Uri . of_string string
let list ctxt = RPC_context . make_call RPC_service . error_service ctxt ( ) ( ) ( )
type Error_monad . error += Bad_version of { given : int ; supported : int list }
let ( ) = let open Error_monad in register_error_kind ` Permanent ~ id " : RPC_error . bad_version " ~ title " : Unknown RPC version " ~ description " : The RPC was called with a bad version number . " ~ pp ( : fun ppf ( given , supported ) -> Format . fprintf ppf " [ @< v 2 > The RPC was called with version number ' %...
let bad_version ~ given ~ supported : RPC_service . error = [ Bad_version { given ; supported } ]
module Server = struct type t = | Tcp of ( Socket . Address . Inet . t , int ) Tcp . Server . t | Netkit of Netkit . Network . Tcp_endpoint . Server . t let bound_on = function | Tcp s -> Tcp . Server . listening_on s | Netkit s -> Netkit . Network . Tcp_endpoint . Server . bound s |> Sockaddr . port ; ; let close = fu...
type make_transport = Unix . Fd . t -> max_message_size : int -> Async_rpc . Rpc . Transport . t
type t = | Async of make_transport | Netkit of Netkit . Network . t
let make_client ? heartbeat_config t host port = match t with | Async make_transport -> Connection . client ? heartbeat_config ~ make_transport ( Tcp . Where_to_connect . of_host_and_port { host ; port } ) | Netkit network -> Netkit_rpc . Rpc . Connection . client ? heartbeat_config network ( Sockaddr . create ~ addr (...
let with_client ? heartbeat_config t host port f = match % bind make_client ? heartbeat_config t host port with | Ok conn -> let % bind result = f conn in let % bind ( ) = Connection . close conn in return ( Ok result ) | Error _ as err -> return err ; ;
let make_server ? heartbeat_config ? port ~ implementations ~ initial_connection_state t = match t with | Async make_transport -> let where_to_listen = match port with | None -> Tcp . Where_to_listen . of_port_chosen_by_os | Some port -> Tcp . Where_to_listen . of_port port in Connection . serve ? heartbeat_config ~ im...
let spec = let open Command . Spec in let standard_mt fd ~ max_message_size = Transport . of_fd fd ~ max_message_size in let low_latency_mt fd ~ max_message_size = Low_latency_transport . create fd ~ max_message_size in let standard _netkit_ifname = Async standard_mt in let low_latency _netkit_ifname = Async low_latenc...
let message_stream global_state = let bus_read = Bus . read_only global_state . Global_state . message_bus in let f _user_state ( ) = Async_bus . pipe1_exn bus_read [ % here ] |> Deferred . Result . return in Rpc . Pipe_rpc . implement Protocol . Message_stream . t f ; ;
let message_request global_state = let messages = global_state . Global_state . messages in let f _user_state room = match Room . Table . find messages room with | None -> return [ ] | Some q -> return ( Queue . to_list q ) in Rpc . Rpc . implement Protocol . Messages_request . t f ; ;
let send_message global_state = let messages = global_state . Global_state . messages in let bus = global_state . Global_state . message_bus in let f user_state message = let author = Krb . Principal . Name . to_string user_state . User_state . user in let message = { message with Message . author } in let room = Messa...
let create_room global_state = let messages = global_state . Global_state . messages in let f _user_state room = match Room . Table . add messages ~ key : room ~ data ( : Queue . create ( ) ) with | ` Ok -> Deferred . Or_error . return ( ) | ` Duplicate -> Deferred . Or_error . error_s [ % message " duplicate room " ( ...
let list_rooms global_state = let messages = global_state . Global_state . messages in let f _user_state ( ) = return ( Room . Table . keys messages ) in Rpc . Rpc . implement Protocol . List_rooms . t f ; ;
let implementations global_state = let implementations = [ message_stream global_state ; message_request global_state ; send_message global_state ; create_room global_state ; list_rooms global_state ] in Rpc . Implementations . create_exn ~ implementations ~ on_unknown_rpc ` : Continue ; ;
type ( ' query , ' response ) ' response rpc_fn = state -> version : int -> ' query -> ' response Deferred . t
type ' r rpc_response = | Failed_to_connect of Error . t | Connected of ' r Or_error . t Envelope . Incoming . t
module type Rpc_implementation_intf = sig type query type response val name : string val versions : unit -> Int . Set . t val sent_counter : Mina_metrics . Counter . t * Mina_metrics . Gauge . t val received_counter : Mina_metrics . Counter . t * Mina_metrics . Gauge . t val failed_request_counter : Mina_metrics . Coun...
type ( ' query , ' response ) ' response rpc_implementation = ( module Rpc_implementation_intf with type query = ' query and type response = ' response )
module type Rpc_interface_intf = sig type ( ' query , ' response ) ' response rpc type rpc_handler = | Rpc_handler : { rpc : ( ' q , ' r ) ' r rpc ; f : ( ' q , ' r ) ' r rpc_fn ; cost : ' q -> int ; budget : int * [ ` Per of Time . Span . t ] } -> rpc_handler val implementation_of_rpc : ( ' q , ' r ) ' r rpc -> ( ' q ...
let ( ) = if Time . Span . ( > ) ( Scheduler . event_precision ( ) ) ( Time . Span . of_us 1 . ) then ( Core . eprintf { |% s : you need to run this program with : ASYNC_CONFIG ' ( ( = timing_wheel_config ( ( alarm_precision 1us ) ) ) ) ' } | ( Filename . basename Sys . executable_name ) ; Caml . exit 2 ) ; ;
module Spec = struct open Command . Spec let port ( ) = step ( fun main port -> main ~ port ) +> flag " - port " ( optional_with_default 12345 int ) ~ doc " : RPC server port " ; ; let host ( ) = step ( fun main host -> main ~ host ) +> flag " - host " ( required string ) ~ doc " : RPC server hostname " ; ; end
module Stats = struct type t = { mutable count : int ; mutable total : Time_ns . Span . t ; mutable max : Time_ns . Span . t ; mutable min : Time_ns . Span . t ; mutable count_by_us : int array } let create ( ) = { count = 0 ; total = Time_ns . Span . zero ; max = Time_ns . Span . zero ; min = Time_ns . Span . max_valu...
module Protocol = struct type query = string [ @@ deriving bin_io ] type response = string [ @@ deriving bin_io ] let rpc = Rpc . create ~ name " : test - rpc - latency " ~ version : 1 ~ bin_query ~ bin_response module Quit = struct type query = unit [ @@ deriving bin_io ] type response = unit [ @@ deriving bin_io ] le...
module Client = struct let one_call ~ connection ~ stats ~ record ~ start ~ payload = Rpc . dispatch_exn Protocol . rpc connection payload >>> fun _ -> let stop = Time_ns . now ( ) in let span = Time_ns . diff stop start in if ! record then Stats . add stats ~ span ; ; let rec loop ~ connection ~ stats ~ record ~ span_...
module Client_long = struct let pending = ref 0 let running = ref false let record = ref false let one_call ~ connection ~ stats ~ start ~ payload = incr pending ; Rpc . dispatch_exn Protocol . rpc connection payload >>> fun _ -> decr pending ; let stop = Time_ns . now ( ) in let span = Time_ns . diff stop start in let...
module Server = struct let implementations = [ Rpc . implement ' Protocol . rpc ( fun ( ) s -> s ) ; Rpc . implement ' Protocol . Quit . rpc ( fun ( ) ( ) -> Clock . after ( sec 1 . 0 ) >>> fun ( ) -> shutdown 0 ) ] ; ; let main ~ port ~ rpc_impl ( ) = let implementations = Implementations . create_exn ~ implementation...
module Quit = struct let main ~ host ~ port ~ rpc_impl ( ) = Rpc_impl . make_client rpc_impl host port >>| Result . ok_exn >>= fun connection -> Rpc . dispatch_exn Protocol . Quit . rpc connection ( ) >>| fun ( ) -> shutdown 0 ; ; end
let server_command = Command . async_spec ~ summary " : test server " Command . Spec . ( empty ++ Spec . port ( ) ++ Rpc_impl . spec ( ) ) Server . main ; ;
let client_command = Command . async_spec ~ summary " : test client " Command . Spec . ( empty +> flag " msg - per - sec " ( required int ) ~ doc " " : +> flag " msg - size " ( optional_with_default 0 int ) ~ doc " : message size " ++ Spec . host ( ) ++ Spec . port ( ) ++ Rpc_impl . spec ( ) ) Client . main ; ;
let client_long_command = Command . async_spec ~ summary " : long test client " Command . Spec . ( empty +> flag " csv - prefix " ( required string ) ~ doc " " : +> flag " msg - size " ( optional_with_default 0 int ) ~ doc " : message size " ++ Spec . host ( ) ++ Spec . port ( ) ++ Rpc_impl . spec ( ) ) Client_long . m...
let quit_command = Command . async_spec ~ summary " : test quit " Command . Spec . ( empty ++ Spec . host ( ) ++ Spec . port ( ) ++ Rpc_impl . spec ( ) ) Quit . main ; ;
let ( ) = Command . run ( Command . group ~ summary " : Async RPC latency test " [ " server " , server_command ; " client " , client_command ; " client - long " , client_long_command ; " quit - server " , quit_command ] ) ; ;
let section = Internal_event . Section . make_sanitized [ " rpc " ]
module Event_def = struct type t = { level : Internal_event . level ; message : string } let name = " rpc_http_event " let doc = " A logging event emitted by the RPC HTTP server . " let pp ~ short : _ fmt { message ; _ } = Format . fprintf fmt " % s " message let encoding = let open Data_encoding in let enc : ( Interna...
module Event = Internal_event . Make ( Event_def )
let emit level message = let open Lwt_syntax in let * r = Event . emit ~ section ( fun ( ) -> { level ; message } ) in match r with | Ok ( ) -> return_unit | Error e -> Format . kasprintf Lwt . fail_with " % a " pp_print_trace e
let wrap_lwt f a = Lwt . dont_wait ( fun ( ) -> f a ) raise
let if_level_appropriate_or_else ~ level if_so if_not fmt = let lwt_level = Lwt_log_core . Section . level @@ Internal_event . Section . to_lwt_log section in if Internal_event . Level . to_lwt_log level >= lwt_level then if_so fmt else if_not fmt