123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2022 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. *)(* *)(*****************************************************************************)openTezos_protocol_alphaopenProtocolopenAlpha_context(* Encoding reading string as Michelson data, of the form "Pair Unit...". It can
only be used for decoding however, not encoding. *)letparsed_string_encoding=Data_encoding.conv(fun_->Stdlib.failwith"This value is not supposed to be encoded")(funs->letparsed,_=Tezos_client_alpha.Michelson_v1_parser.parse_expressionsinparsed.Tezos_client_alpha.Michelson_v1_parser.expanded)Data_encoding.string(* [input_encoding default_sender default_source default_destination] is an
alternative encoding for {Sc_rollup_inbox_message_repr.t} that only encodes
`Internal Transfer` and `External`. In the case of `Internal Transfer`, only
the Micheline payload is mandatory, the other field are taken from the
default one if they are missing.
It can also take already serialized messages, if the input does not belong
to the two cases above. This allows to inject arbitrary messages in
the rollup. *)letinput_encodingdefault_senderdefault_sourcedefault_destination:[<`Inbox_messageofSc_rollup.Inbox_message.t|`Serializedofstring]Data_encoding.encoding=letopenData_encodinginunion[case(Tag0)~title:"Transfer"(obj4(req"payload"parsed_string_encoding)(dft"sender"Contract_hash.encodingdefault_sender)(dft"source"Tezos_crypto.Signature.Public_key_hash.encodingdefault_source)(dft"destination"Sc_rollup_repr.Address.encodingdefault_destination))(function|`Inbox_messageSc_rollup.Inbox_message.(Internal(Transfer{payload;sender;source;destination}))->Some(payload,sender,source,destination)|_->None)(fun(payload,sender,source,destination)->`Inbox_message(Internal(Transfer{payload;sender;source;destination})));case(Tag1)~title:"External"(obj1(req"external"string))(function|`Inbox_message(Sc_rollup.Inbox_message.Externalmsg)->let(`Hexmsg)=Hex.of_stringmsginSomemsg|_->None)(funmsg->`Inbox_message(External(Hex.to_string(`Hexmsg)|>Option.value~default:"")));case(Tag2)~title:"Serialized"(obj1(req"serialized"string))(function|`Serializedmsg->let(`Hexmsg)=Hex.of_stringmsginSomemsg|_->None)(funmsg->`Serialized(Hex.to_string(`Hexmsg)|>Option.value~default:""));](* Represent a set of inboxes, i.e. a set of set of inputs. The position of an
inbox in the list represents its level. *)typeinboxes=Sc_rollup.Inbox_message.tlistlistletinboxes_encodingdefault_senderdefault_sourcedefault_destination=Data_encoding.(list(list(input_encodingdefault_senderdefault_sourcedefault_destination)))(* [parse_inboxes inputs config] parses an inbox from raw string. *)letparse_inboxesinputsConfig.{sender;source;destination;_}=letopenLwt_result_syntaxinmatchData_encoding.Json.from_stringinputswith|Okjson->let*full_inputs=(fun()->Lwt.return(Data_encoding.Json.destruct(inboxes_encodingsendersourcedestination)json))|>Repl_helpers.trap_exninList.map_es(funinputs->List.map_es(function|`Inbox_messageinput->Protocol.Alpha_context.Sc_rollup.Inbox_message.(serializeinput|>Result.mapunsafe_to_string|>Environment.wrap_tzresult|>Lwt.return)|`Serializedinput->Lwt.return_okinput)inputs)full_inputs|Errore->Error_monad.failwith"%s"e(* [pp_input ppf bytes] is a pretty printer for Sc_rollup.Inbox messages that
also handles deserialization from raw bytes. If the message cannot be
decoded, it prints its hexadecimal representation. *)letpp_inputppfbytes=letppppf=function|Sc_rollup.Inbox_message.(Internal(Transfer_)asmsg)->letjson=Data_encoding.Json.constructSc_rollup.Inbox_message.encodingmsginData_encoding.Json.ppppfjson|InternalStart_of_level->Format.fprintfppf"Start_of_level"|InternalEnd_of_level->Format.fprintfppf"End_of_level"|Internal(Protocol_migrationproto)->Format.fprintfppf"Protocol_migration %s"proto|Internal(Info_per_level{predecessor_timestamp;predecessor})->Format.fprintfppf"Info_per_level {predecessor_timestamp = %a; predecessor = %a}"Timestamp.pppredecessor_timestampBlock_hash.pppredecessor|Externalmsg->Format.fprintfppf"%a"Hex.pp(Hex.of_stringmsg)inmatchSc_rollup.Inbox_message.(deserialize(unsafe_of_string(Bytes.to_stringbytes)))with|Error_->Format.fprintfppf"Unknown encoding: %a"Hex.pp(Hex.of_bytesbytes)|Okmsg->ppppfmsgletcompare_input_buffer_messagesi1i2=letcmp_lvl=Int32.comparei1.Tezos_webassembly_interpreter.Input_buffer.raw_leveli2.Tezos_webassembly_interpreter.Input_buffer.raw_levelinifcmp_lvl=0thenZ.comparei1.message_counteri2.message_counterelsecmp_lvl(* [pp_output ppf bytes] is a pretty printer for Sc_rollup.Outbox messages that
also handles deserialization from raw bytes. If the message cannot be
decoded, it prints its hexadecimal representation. *)letpp_outputppfbytes=matchSc_rollup.Outbox.Message.(deserialize(unsafe_of_string(Bytes.to_stringbytes)))with|Error_->Format.fprintfppf"Unknown encoding: %a"Hex.pp(Hex.of_bytesbytes)|Okmsgs->Sc_rollup.Outbox.Message.ppppfmsgs