123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)typeargument=(* If you're considering adding [--endpoint] and [--rpc-addr],
beware that, for the moment, they are automatically computed in
{!create} below *)|Data_dir|Symbolic_block_caching_timeofintmoduleParameters=structtypepersistent_state={arguments:stringlist;mutablepending_ready:unitoptionLwt.ulist;rpc_port:int;runner:Runner.toption;}typesession_state={mutableready:bool}letbase_default_name="proxy_server"letdefault_colors=Log.Color.[|FG.gray;BG.gray|]endopenParametersincludeDaemon.Make(Parameters)lettrigger_readytvalue=letpending=t.persistent_state.pending_readyint.persistent_state.pending_ready<-[];List.iter(funpending->Lwt.wakeup_laterpendingvalue)pendingletset_readyt=(matcht.statuswith|Not_running->()|Runningstatus->status.session_state.ready<-true);trigger_readyt(Some())lethandle_eventt({name;_}:event)=matchnamewith"starting_proxy_rpc_server.v0"->set_readyt|_->()(** Returns the [--endpoint] and [--rpc-addr] arguments corresponding to [?rpc_port]
and [node]. Also returns the chosen rpc port, in case it was omitted
and got generated. *)letconnection_arguments_and_port?rpc_portnode=letrpc_port=matchrpc_portwithNone->Port.fresh()|Someport->portin(["--endpoint";"http://localhost:"^string_of_int(Node.rpc_portnode);(* "-l"; <- to debug RPC delegations to the node
Note that if you want to debug the proxy server's RPC server,
set TEZOS_LOG to "rpc->debug", just like you would do with a node.
*)"--rpc-addr";"http://127.0.0.1:"^string_of_intrpc_port;],rpc_port)letspawn?rpc_port?(args=[])node=letargs=(connection_arguments_and_port?rpc_portnode|>fst)@argsinProcess.spawn~name:Parameters.base_default_name~color:Parameters.default_colors.(0)Constant.tezos_proxy_serverargsletcreate?runner?name?rpc_port?(args=[])node=letpath=Constant.tezos_proxy_serverinletuser_arguments=List.map(function|Data_dir->["--data-dir";Node.data_dirnode]|Symbolic_block_caching_time s->["--sym-block-caching-time";Int.to_strings])args|>List.concatinletarguments,rpc_port=connection_arguments_and_port?rpc_portnode|>fun(args,rpc_port)->(args@user_arguments,rpc_port)inlett=create?runner~path?name{arguments;pending_ready=[];rpc_port;runner}inon_eventt(handle_eventt);returntletrpc_port({persistent_state;_}:t)=persistent_state.rpc_portletrunnernode=node.persistent_state.runnerletrun?(on_terminate=fun_->())?event_level?event_sections_levelsendpointarguments=letarguments=endpoint.persistent_state.arguments@argumentsinleton_terminatestatus=on_terminatestatus;unitinrun?event_level?event_sections_levelsendpoint{ready=false}arguments~on_terminateletcheck_event?wherenodenamepromise=let*result=promiseinmatchresultwith|None->raise(Terminated_before_event{daemon=node.name;event=name;where})|Somex->returnxletwait_for_readyt=matcht.statuswith|Running{session_state={ready=true};_}->unit|Not_running|Running{session_state={ready=false};_}->letpromise,resolver=Lwt.task()int.persistent_state.pending_ready<-resolver::t.persistent_state.pending_ready;check_eventt"starting_proxy_rpc_server.v0"promiseletinit?runner?name?rpc_port?event_level?event_sections_levels?argsnode=let*endpoint=create?runner?name?rpc_port?argsnodeinlet*()=run?event_level?event_sections_levelsendpoint[]inlet*()=wait_for_readyendpointinreturnendpoint