Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file proxy_server_config.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2021 Nomadic Labs. <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)typet={endpoint:Uri.toption;rpc_addr:Uri.toption;rpc_tls:stringoption;sym_block_caching_time:Ptime.spanoption;data_dir:stringoption;}letppppf{endpoint;rpc_addr;rpc_tls;sym_block_caching_time;data_dir}=letpp_uri_opt=Format.pp_print_optionUri.ppinFormat.fprintfppf"@[<v>endpoint=%a@,\
rpc_addr=%a@,\
rpc_tls=%a@,\
sym_block_caching_time=%a@,\
data_dir=%a@]"pp_uri_optendpointpp_uri_optrpc_addr(Format.pp_print_optionFormat.pp_print_string)rpc_tls(Format.pp_print_optionPtime.Span.pp)sym_block_caching_time(Format.pp_print_optionFormat.pp_print_string)data_dirletexample_config={|{"endpoint": "http://127.0.0.1:18731", "rpc_addr": "http://127.0.0.1:18732", "sym_block_caching_time": 60}|}letencoding:tData_encoding.t=letopenData_encodinginletof_spans=Option.bindsPtime.Span.to_int_sinconv(funt->(Option.mapUri.to_stringt.endpoint,Option.mapUri.to_stringt.rpc_addr,t.rpc_tls,Option.mapInt32.of_int@@of_spant.sym_block_caching_time,t.data_dir))(fun(endpoint,rpc_addr,rpc_tls,sym_block_caching_time,data_dir)->{endpoint=Option.mapUri.of_stringendpoint;rpc_addr=Option.mapUri.of_stringrpc_addr;rpc_tls;sym_block_caching_time=Option.map(funx->Ptime.Span.of_int_s@@Int32.to_intx)sym_block_caching_time;data_dir;})(obj5(opt"endpoint"string)(opt"rpc_addr"string)(opt"rpc_tls"string)(opt"sym_block_caching_time"int32)(opt"data_dir"string))letmake~endpoint~rpc_addr~rpc_tls~sym_block_caching_time~data_dir:t={endpoint;rpc_addr;rpc_tls;sym_block_caching_time;data_dir}letsym_block_caching_time_errorsym_block_caching_time=matchsym_block_caching_timewith|Somesym_block_caching_timewhenPtime.Span.(comparesym_block_caching_timezero<=0)->Some(Format.asprintf{|--sym-block-caching-time argument and sym_block_caching_time field must be strictly positive, but found %a|}Ptime.Span.ppsym_block_caching_time)|_->Nonetype'adestructed=Validof'a|Invalidofstring|CannotDeserializeletdestruct_configjson=matchData_encoding.Json.destructencodingjsonwith|cfg->(matchsym_block_caching_time_errorcfg.sym_block_caching_timewith|Someerr->Invaliderr|None->Validcfg)|exception_->CannotDeserializeletunion_right_bias(t1:t)(t2:t)={endpoint=Option.eithert2.endpointt1.endpoint;rpc_addr=Option.eithert2.rpc_addrt1.rpc_addr;rpc_tls=Option.eithert2.rpc_tlst1.rpc_tls;sym_block_caching_time=Option.eithert2.sym_block_caching_timet1.sym_block_caching_time;data_dir=Option.eithert2.data_dirt1.data_dir;}typeruntime={endpoint:Uri.t;rpc_server_address:P2p_addr.t;rpc_server_port:int;rpc_server_tls:(string*string)option;sym_block_caching_time:Ptime.spanoption;data_dir:stringoption;}(** Given the value of the [--rpc-addr] argument (or the [rpc_addr] CONFIG field),
return the address and the port of the server that should be spawned. *)letaddress_and_port_for_runtimerpc_addr=letopenResultinletwrong_rpc_addrlooked_for=error@@Format.asprintf{|Wrong "--rpc-addr" argument or "rpc_addr" field: %a. %s cannot be determined|}Uri.pprpc_addrlooked_forinmatch(Uri.hostrpc_addr,Uri.portrpc_addr)with|None,_->wrong_rpc_addr"Hostname"|_,None->wrong_rpc_addr"Port"|Somerpc_server_address,Somerpc_server_port->(matchP2p_addr.of_string_optrpc_server_addresswith|Somerpc_server_address->Ok(rpc_server_address,rpc_server_port)|None->error@@Format.asprintf{|Cannot convert hostname of "--rpc-addr" argument or "rpc_addr" field to P2p_addr: %s|}rpc_server_address)(** Given the value of the [--rpc-tls] argument (or the [rpc_tls] CONFIG field),
return the paths to the certificate and the key for use by TLS *)lettls_for_runtime=letopenResultinletregexp_str="(.*),(.*)"inletregexp=Re.compile(Re.Perl.reregexp_str)infunrpc_tls->matchRe.exec_optregexprpc_tlswith|None->error@@Format.asprintf{|Value of "--rpc-tls" argument or "rpc_tls" field cannot be parsed: %s doesn't match regexp %s|}rpc_tlsregexp_str|Somegroup->ok(Re.Group.getgroup1,Re.Group.getgroup2)(** Helper to lift a validation function [f : 'a -> ('b * _) result] over
an optional value. *)letopt_res_to_res_opt=function|None->(* No data to validate: no error, no data. *)OkNone|Some(Okx)->(* Data was successfully validated: no error, some data. *)Ok(Somex)|Some(Errorx)->(* Data could not be successfully validated: an error and no data *)Errorxletto_runtime({endpoint;rpc_addr;rpc_tls;sym_block_caching_time;data_dir}:t):(runtime,string)result=(* Validating sym_block_caching_time is required if it was specified
on the command line. In this case it wasn't validated yet. *)letopenResult_syntaxinmatch(endpoint,rpc_addr,sym_block_caching_time_errorsym_block_caching_time)with|None,_,_->fail{|Endpoint not specified: pass argument --endpoint or specify "endpoint" field in CONFIG file|}|_,None,_->fail{|RPC address not specified: pass argument --rpc-addr or specify "rpc_addr" field in CONFIG file|}|_,_,Someerr->failerr|Someendpoint,Somerpc_addr,None->let*rpc_server_address,rpc_server_port=address_and_port_for_runtimerpc_addrinlet*rpc_server_tls=Option.maptls_for_runtimerpc_tls|>opt_res_to_res_optinOk{endpoint;rpc_server_address;rpc_server_port;rpc_server_tls;sym_block_caching_time;data_dir;}