123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2021 Nomadic Labs <contact@nomadic-labs.com> *)(* Copyright (c) 2022 Trili Tech, <contact@trili.tech> *)(* *)(* 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. *)(* *)(*****************************************************************************)moduleAddress=structletprefix="scr1"letencoded_size=37letdecoded_prefix="\001\118\132\217"(* "scr1(37)" decoded from base 58. *)moduleH=Blake2B.Make(Base58)(structletname="Sc_rollup_hash"lettitle="A smart contract rollup address"letb58check_prefix=decoded_prefixletsize=Some20end)includeHlet()=Base58.check_encoded_prefixb58check_encodingprefixencoded_sizeincludePath_encoding.Make_hex(H)typeerror+=(* `Permanent *)Error_sc_rollup_address_generationlet()=letopenData_encodinginletmsg="Error while generating rollup address"inregister_error_kind`Permanent~id:"rollup.error_smart_contract_rollup_address_generation"~title:msg~pp:(funppf()->Format.fprintfppf"%s"msg)~description:msgunit(functionError_sc_rollup_address_generation->Some()|_->None)(fun()->Error_sc_rollup_address_generation)letfrom_noncenonce=Data_encoding.Binary.to_bytes_optOrigination_nonce.encodingnonce|>function|None->errorError_sc_rollup_address_generation|Somenonce->ok@@hash_bytes[nonce]endmoduleInternal_for_tests=structletoriginated_sc_rollupnonce=letdata=Data_encoding.Binary.to_bytes_exnOrigination_nonce.encodingnonceinAddress.hash_bytes[data]end(* 32 *)letcommitment_hash_prefix="\017\144\021\100"(* scc1(54) *)moduleCommitment_hash=structletprefix="scc1"letencoded_size=54moduleH=Blake2B.Make(Base58)(structletname="commitment_hash"lettitle="The hash of a commitment of a smart contract rollup"letb58check_prefix=commitment_hash_prefix(* defaults to 32 *)letsize=Noneend)includeHlet()=Base58.check_encoded_prefixb58check_encodingprefixencoded_sizeincludePath_encoding.Make_hex(H)end(* 32 *)letstate_hash_prefix="\017\144\122\202"(* scs1(54) *)moduleState_hash=structletprefix="scs1"letencoded_size=54moduleH=Blake2B.Make(Base58)(structletname="state_hash"lettitle="The hash of the VM state of a smart contract rollup"letb58check_prefix=state_hash_prefix(* defaults to 32 *)letsize=Noneend)includeHlet()=Base58.check_encoded_prefixb58check_encodingprefixencoded_sizeincludePath_encoding.Make_hex(H)endtypet=Address.tmoduleStaker=Signature.Public_key_hashletdescription="A smart contract rollup is identified by a base58 address starting with "^Address.prefixtypeerror+=(* `Permanent *)Invalid_sc_rollup_addressofstringleterror_description=Format.sprintf"A smart contract rollup address must be a valid hash starting with '%s'."Address.prefixlet()=letopenData_encodinginregister_error_kind`Permanent~id:"rollup.invalid_smart_contract_rollup_address"~title:"Invalid smart contract rollup address"~pp:(funppfx->Format.fprintfppf"Invalid smart contract rollup address %S"x)~description:error_description(obj1(req"address"string))(functionInvalid_sc_rollup_addressloc->Someloc|_->None)(funloc->Invalid_sc_rollup_addressloc)letof_b58checks=matchBase58.decodeswith|Some(Address.Datahash)->okhash|_->Error(Format.sprintf"Invalid_sc_rollup_address %s"s)letpp=Address.ppletencoding=letopenData_encodingindef"rollup_address"~title:"A smart contract rollup address"~description(conv_with_guardAddress.to_b58checkof_b58checkstring)letrpc_arg=letconstruct=Address.to_b58checkinletdestructhash=Result.map_error(fun_->error_description)(of_b58checkhash)inRPC_arg.make~descr:"A smart contract rollup address."~name:"sc_rollup_address"~construct~destruct()moduleIndex=structtypet=Address.tletpath_length=1letto_pathcl=letraw_key=Data_encoding.Binary.to_bytes_exnencodingcinlet(`Hexkey)=Hex.of_bytesraw_keyinkey::lletof_path=function|[key]->Option.bind(Hex.to_bytes(`Hexkey))(Data_encoding.Binary.of_bytes_optencoding)|_->Noneletrpc_arg=rpc_argletencoding=encodingletcompare=Address.compareendmoduleCommitment_hash_index=structincludeCommitment_hashendmoduleNumber_of_messages=Bounded.Int32.Make(structletmin_int=1lletmax_int=4096l(* TODO: check this is reasonable.
See https://gitlab.com/tezos/tezos/-/issues/2373
*)end)moduleNumber_of_ticks=Bounded.Int32.Make(structletmin_int=1lletmax_int=Int32.max_intend)moduleCommitment=structtypet={compressed_state:State_hash.t;inbox_level:Raw_level_repr.t;predecessor:Commitment_hash.t;number_of_messages:Number_of_messages.t;number_of_ticks:Number_of_ticks.t;}letppfmt{compressed_state;inbox_level;predecessor;number_of_messages;number_of_ticks;}=Format.fprintffmt"@[<v 2>SCORU Commitment:@ compressed_state: %a@ inbox_level: %a@ \
predecessor: %a@ number_of_messages: %d@ number_of_ticks: %d@]"State_hash.ppcompressed_stateRaw_level_repr.ppinbox_levelCommitment_hash.pppredecessor(Int32.to_int(Number_of_messages.to_int32number_of_messages))(Int32.to_int(Number_of_ticks.to_int32number_of_ticks))letencoding=letopenData_encodinginconv(fun{compressed_state;inbox_level;predecessor;number_of_messages;number_of_ticks;}->(compressed_state,inbox_level,predecessor,number_of_messages,number_of_ticks))(fun(compressed_state,inbox_level,predecessor,number_of_messages,number_of_ticks)->{compressed_state;inbox_level;predecessor;number_of_messages;number_of_ticks;})(obj5(req"compressed_state"State_hash.encoding)(req"inbox_level"Raw_level_repr.encoding)(req"predecessor"Commitment_hash.encoding)(req"number_of_messages"Number_of_messages.encoding)(req"number_of_ticks"Number_of_ticks.encoding))lethashcommitment=letcommitment_bytes=Data_encoding.Binary.to_bytes_exnencodingcommitmentinCommitment_hash.hash_bytes[commitment_bytes]endmoduleKind=struct(*
Each time we add a data constructor to [t], we also need:
- to extend [Sc_rollups.all] with this new constructor ;
- to update [Sc_rollups.kind_of_string] and [encoding].
*)typet=Example_arithletexample_arith_case=Data_encoding.(case~title:"Example_arith smart contract rollup kind"(Tag0)unit(functionExample_arith->Some())(fun()->Example_arith))letencoding=Data_encoding.union~tag_size:`Uint16[example_arith_case]letequalxy=match(x,y)with(Example_arith,Example_arith)->trueletppfmtkind=matchkindwithExample_arith->Format.fprintffmt"Example_arith"end