123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2023 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. *)(* *)(*****************************************************************************)moduleParameters=structtypepersistent_state={arguments:stringlist;mutablepending_ready:unitoptionLwt.ulist;rpc_addr:string;rpc_port:int;rollup_node:Sc_rollup_node.t;runner:Runner.toption;}typesession_state={mutableready:bool}letbase_default_name="evm_proxy_server"letdefault_colors=Log.Color.[|FG.magenta|]endopenParametersincludeDaemon.Make(Parameters)letpath="./octez-evm-proxy-server"letconnection_arguments?rpc_addr?rpc_portrollup_node_endpoint=letopenCli_arginletrpc_port=matchrpc_portwithNone->Port.fresh()|Someport->portin(["--rollup-node-endpoint";rollup_node_endpoint;"--rpc-port";string_of_intrpc_port;]@optional_arg"--rpc-addr"Fun.idrpc_addr,Option.value~default:"127.0.0.1"rpc_addr,rpc_port)lettrigger_readysc_nodevalue=letpending=sc_node.persistent_state.pending_readyinsc_node.persistent_state.pending_ready<-[];List.iter(funpending->Lwt.wakeup_laterpendingvalue)pendingletset_readyproxy_server=(matchproxy_server.statuswith|Not_running->()|Runningstatus->status.session_state.ready<-true);trigger_readyproxy_server(Some())letevent_ready_name="evm_proxy_server_is_ready.v0"lethandle_event(proxy_server:t){name;value=_;timestamp=_}=ifname=event_ready_namethenset_readyproxy_serverelse()letcheck_eventproxy_servernamepromise=let*result=promiseinmatchresultwith|None->raise(Terminated_before_event{daemon=proxy_server.name;event=name;where=None})|Somex->returnxletwait_for_readyproxy_server=matchproxy_server.statuswith|Running{session_state={ready=true;_};_}->unit|Not_running|Running{session_state={ready=false;_};_}->letpromise,resolver=Lwt.task()inproxy_server.persistent_state.pending_ready<-resolver::proxy_server.persistent_state.pending_ready;check_eventproxy_serverevent_ready_namepromiseletcreate?runner?rpc_addr?rpc_portrollup_node=letrollup_node_endpoint=Sc_rollup_node.endpointrollup_nodeinletarguments,rpc_addr,rpc_port=connection_arguments?rpc_addr?rpc_portrollup_node_endpointinletproxy_server=create~path{arguments;pending_ready=[];rpc_addr;rpc_port;rollup_node;runner}inon_eventproxy_server(handle_eventproxy_server);proxy_serverletrunproxy_server=let*()=runproxy_server{ready=false}(["run"]@proxy_server.persistent_state.arguments)inlet*()=wait_for_readyproxy_serverinunitletspawn_commandproxy_serverargs=Process.spawn?runner:proxy_server.persistent_state.runnerpath@@argsletspawn_runproxy_server=spawn_commandproxy_server(["run"]@proxy_server.persistent_state.arguments)letendpoint(proxy_server:t)=Format.sprintf"http://%s:%d"proxy_server.persistent_state.rpc_addrproxy_server.persistent_state.rpc_portletinit?runner?rpc_addr?rpc_portrollup_node=letproxy_server=create?runner?rpc_addr?rpc_portrollup_nodeinlet*()=runproxy_serverinreturnproxy_serverletrequestmethod_parameters:JSON.t=`O[("jsonrpc",`String"2.0");("method",`Stringmethod_);("params",parameters);("id",`String"0");]|>JSON.annotate~origin:"evm_proxy_server"letcall_evm_rpcproxy_server~method_~parameters=letendpoint=endpointproxy_serverinRPC.Curl.postendpoint(requestmethod_parameters)|>Runnable.run