123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2021 Nomadic Labs <contact@nomadic-labs.com> *)(* Copyright (c) 2022 Trili Tech, <contact@trili.tech> *)(* Copyright (c) 2022 Marigold, <contact@marigold.dev> *)(* *)(* 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)letof_b58data=functionH.Datah->Someh|_->NoneendmoduleInternal_for_tests=structletoriginated_sc_rollupnonce=letdata=Data_encoding.Binary.to_bytes_exnOrigination_nonce.encodingnonceinAddress.hash_bytes[data]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)letcontext_hash_to_state_hash=(* Both State_hash and Context_hash's hashes are supposed to have the
same size. This top-level check enforces this invariant, in which case,
no exception could be thrown by [of_bytes_exn] below *)let()=assert(Compare.Int.equalsizeContext_hash.size)infunh->of_bytes_exn@@Context_hash.to_bytesh(* Hackish way to disable hash_bytes and hash_string to force people to use
context_hash_to_state_hash (without changing content of HASH.S) *)typeunreachable=|lethash_bytes=function(_:unreachable)->.lethash_string=function(_:unreachable)->.endtypet=Address.tletdescription="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()letin_memory_size(_:t)=letopenCache_memory_helpersinh1w+!string_size_genAddress.sizemoduleStaker=Signature.Public_key_hashmoduleIndex=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.compareendmoduleNumber_of_ticks=structincludeBounded.Int64(structletmin_value=0Lletmax_value=Int64.max_intend)letzero=matchof_value0Lwith|Somezero->zero|None->assertfalse(* unreachable case, since [min_int = 0l] *)end