123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2020 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. *)(* *)(*****************************************************************************)moduleProtoRpc:Tezos_proxy.Proxy_proto.PROTO_RPC=struct(** Split done only when the mode is [Tezos_proxy.Proxy.server]. Getting
an entire big map at once is useful for dapp developers that
iterate a lot on big maps and that use proxy servers in their
internal infra. *)letsplit_serverkey=matchkeywith(* matches paths like:
big_maps/index/i/contents/tail *)|"big_maps"::"index"::i::"contents"::tail->Some(["big_maps";"index";i;"contents"],tail)|_->None(** Split that is always done, no matter the mode *)letsplit_alwayskey=matchkeywith(* matches paths like:
contracts/index/000002298c03ed7d454a101eb7022bc95f7e5f41ac78/tail *)|"contracts"::"index"::i::tail->Some(["contracts";"index";i],tail)|"cycle"::i::tail->Some(["cycle";i],tail)(* matches paths like:
rolls/owner/snapshot/19/1/tail *)|"rolls"::"owner"::"snapshot"::i::j::tail->Some(["rolls";"owner";"snapshot";i;j],tail)|"v1"::tail->Some(["v1"],tail)|_->Noneletsplit_key(mode:Tezos_proxy.Proxy.mode)(key:Tezos_protocol_environment.Proxy_context.M.key):(Tezos_protocol_environment.Proxy_context.M.key*Tezos_protocol_environment.Proxy_context.M.key)option=matchsplit_alwayskeywith|Some_asres->res(* No need to inspect the mode, this split is always done *)|None->(matchmodewith|Client->(* There are strictly less splits in Client mode: return immediately *)None|Server->split_serverkey)letfailure_is_permanent=function|["pending_migration_balance_updates"]|["pending_migration_operation_results"]->true|_->falseletdo_rpc(pgi:Tezos_proxy.Proxy.proxy_getter_input)(key:Tezos_protocol_environment.Proxy_context.M.key)=letopenLwt_result_syntaxinletchain=pgi.chaininletblock=pgi.blockinlet*!()=Tezos_proxy.Logger.emitTezos_proxy.Logger.proxy_block_rpc(Tezos_shell_services.Block_services.chain_to_stringchain,Tezos_shell_services.Block_services.to_stringblock,key)inlet*(raw_context:Tezos_context_sigs.Context.Proof_types.raw_context)=Protocol_client_context.Alpha_block_services.Context.readpgi.rpc_context~chain~blockkeyinlet*!()=Tezos_proxy.Logger.emitTezos_proxy.Logger.tree_received@@Int64.of_int(Tezos_proxy.Proxy_getter.raw_context_sizeraw_context)inreturnraw_contextendletinitial_context(ctx:Tezos_proxy.Proxy_getter.rpc_context_args)(hash:Context_hash.t):Tezos_protocol_environment.Context.ttzresultLwt.t=letopenLwt_result_syntaxinlet*!()=Tezos_proxy.Logger.emitTezos_proxy.Logger.proxy_getter_created(Tezos_shell_services.Block_services.chain_to_stringctx.chain,Tezos_shell_services.Block_services.to_stringctx.block)inletp_rpc=(moduleProtoRpc:Tezos_proxy.Proxy_proto.PROTO_RPC)inlet*(moduleProxyDelegation)=Tezos_proxy.Proxy_getter.make_delegatectxp_rpchashinletempty=Tezos_protocol_environment.Proxy_context.empty@@Some(moduleProxyDelegation)inletversion_value="alpha_current"inlet*!ctxt=Tezos_protocol_environment.Context.addempty["version"](Bytes.of_stringversion_value)in(* There is something fundamentally strange here. The purpose of the
proxy mode is to fetch pieces of data that are missing. Hence,
there is no need to initialize the context properly. It is
sufficient that the proxy client is connected to a node with a
valid context. However, this makes the assumption that any
interaction with the context is pure: Only the Irmin context can
be modified. The cache breaks this since initializing the cache
changes the Irmin context but also loads values into memory. If
the cache is not initialized, then any cache access will fail.
Hence, the initialization is done here. This means that the
caller needs to maintain a cache on its own. But I suspect this
is not wanted: The cache of the proxied node should be used
instead. *)letcache_layout=(* The order matters, be careful to maintain it correctly. *)Default_parameters.[constants_mainnet.cache_script_size;constants_mainnet.cache_stake_distribution_cycles;constants_mainnet.cache_sampler_state_cycles;]inLwt_result.ok(Tezos_protocol_environment.Context.Cache.set_cache_layoutctxtcache_layout)letround_durations(rpc_context:Tezos_rpc.Context.generic)(chain:Tezos_shell_services.Block_services.chain)(block:Tezos_shell_services.Block_services.block)=letopenProtocolinletopenLwt_result_syntaxinletrpc_context=newProtocol_client_context.wrap_rpc_contextrpc_contextinlet*constants=Constants_services.allrpc_context(chain,block)in(* Return the duration of block 0 *)return@@Some(Alpha_context.Period.to_secondsconstants.parametric.minimal_block_delay)let()=letopenTezos_proxy.RegistrationinletmoduleM:Proxy_sig=structmoduleProtocol=Lifted_protocolletprotocol_hash=Protocol.hashletdirectory=Plugin.RPC.rpc_servicesletinitial_context=initial_contextlettime_between_blocks=round_durationsincludeLight.Mendinregister_proxy_context(moduleM)