123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.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. *)(* *)(*****************************************************************************)openProtocolopenAlpha_contextopenMichelineopenMichelson_v1_primitivesletcreate_context()=letopenLwt_result_syntaxinlet*?accounts=Account.generate_accounts2inBlock.alpha_context(Account.make_bootstrap_accountsaccounts)letexpr_to_hashexpr=letlexpr=Script_repr.lazy_exprexprinScript_repr.force_byteslexpr>|?funb->Script_expr_hash.hash_bytes[b]letassert_expr_equalloc=Assert.equal~loc(=)"Michelson Expressions Not Equal"Michelson_v1_printer.print_exprletassert_proto_error_idlocidresult=lettesterr=(Error_monad.find_info_of_errorerr).id="proto."^Protocol.name^"."^idinAssert.error~locresulttestletassert_ok_lwtx=matchLwt_main.runxwith|Okx->x|Error_->raise@@Failure"Called assert_ok_lwt on Error"letassert_ok=function|Okx->x|Error_->raise@@Failure"Called assert_ok on Error"(** Filters out values that would cause [register] *)letassume_expr_not_too_largeexpr=letnode=rootexprinQCheck2.assume@@not@@Global_constants_storage.Internal_for_tests.node_too_largenodemoduleGenerators=structletcontext_gen()=QCheck2.Gen.return(create_context()|>assert_ok_lwt)letprims=[K_parameter;K_storage;K_code;D_False;D_Elt;D_Left;D_None;D_Pair;D_Right;D_Some;D_True;D_Unit;I_PACK;I_UNPACK;I_BLAKE2B;I_SHA256;I_SHA512;I_ABS;I_ADD;I_AMOUNT;I_AND;I_BALANCE;I_CAR;I_CDR;I_CHAIN_ID;I_CHECK_SIGNATURE;I_COMPARE;I_CONCAT;I_CONS;I_CREATE_ACCOUNT;I_CREATE_CONTRACT;I_IMPLICIT_ACCOUNT;I_DIP;I_DROP;I_DUP;I_EDIV;I_EMPTY_BIG_MAP;I_EMPTY_MAP;I_EMPTY_SET;I_EQ;I_EXEC;I_APPLY;I_FAILWITH;I_GE;I_GET;I_GET_AND_UPDATE;I_GT;I_HASH_KEY;I_IF;I_IF_CONS;I_IF_LEFT;I_IF_NONE;I_INT;I_LAMBDA;I_LE;I_LEFT;I_LEVEL;I_LOOP;I_LSL;I_LSR;I_LT;I_MAP;I_MEM;I_MUL;I_NEG;I_NEQ;I_NIL;I_NONE;I_NOT;I_NOW;I_OR;I_PAIR;I_UNPAIR;I_PUSH;I_RIGHT;I_SIZE;I_SOME;I_SOURCE;I_SENDER;I_SELF;I_SELF_ADDRESS;I_SLICE;I_STEPS_TO_QUOTA;I_SUB;I_SWAP;I_TRANSFER_TOKENS;I_SET_DELEGATE;I_UNIT;I_UPDATE;I_XOR;I_ITER;I_LOOP_LEFT;I_ADDRESS;I_CONTRACT;I_ISNAT;I_CAST;I_RENAME;I_SAPLING_EMPTY_STATE;I_SAPLING_VERIFY_UPDATE;I_DIG;I_DUG;I_NEVER;I_VOTING_POWER;I_TOTAL_VOTING_POWER;I_KECCAK;I_SHA3;I_PAIRING_CHECK;I_TICKET;I_READ_TICKET;I_SPLIT_TICKET;I_JOIN_TICKETS;T_bool;T_contract;T_int;T_key;T_key_hash;T_lambda;T_list;T_map;T_big_map;T_nat;T_option;T_or;T_pair;T_set;T_signature;T_string;T_bytes;T_mutez;T_timestamp;T_unit;T_operation;T_address;T_sapling_transaction_deprecated;T_sapling_state;T_chain_id;T_never;T_bls12_381_g1;T_bls12_381_g2;T_bls12_381_fr;T_ticket;H_constant;]letprim_gen=QCheck2.Gen.oneoflprimsletprims_without_constants_gen=QCheck2.Gen.oneofl(List.filter(funx->x!=H_constant)prims)letz_gen=QCheck2.Gen.mapZ.of_intQCheck2.Gen.intletmicheline_node_genl_genp_genannot_gen:('l,'p)Micheline.nodeQCheck2.Gen.t=letopenMichelineinletopenQCheck2.Geninfix(funself()->frequency[(3,map(fun(l,x)->Int(l,x))(pairl_genz_gen));(3,map(fun(l,x)->String(l,x))(pairl_genstring));(3,map(fun(l,x)->Bytes(l,Bytes.of_stringx))(pairl_genstring));(1,map(fun(l,p,args,annot)->Prim(l,p,args,annot))(quadl_genp_gen(list_size(int_bound10)(self()))annot_gen));(1,map(fun(l,args)->Seq(l,args))(pairl_gen(list_size(int_bound10)(self()))));])()letrecreplace_with_constant:Script.node->Script.location->Script.node*Script.nodeoption=funnodeloc->letopenMichelson_v1_primitivesinletopenMichelineinletrecloop:Script.nodelist->Script.nodelist*Script.nodeoption=function|[]->([],None)|hd::tl->(matchreplace_with_constanthdlocwith|node,Somex->(node::tl,Somex)|_,None->letl,x=looptlin(hd::l,x))inmatchnodewith|(Int(l,_)|String(l,_)|Bytes(l,_))asnode->ifl=locthenlethash=node|>strip_locations|>expr_to_hash|>assert_ok|>Script_expr_hash.to_b58checkin(Prim(-1,H_constant,[String(-1,hash)],[]),Somenode)else(node,None)|Prim(l,prim,args,annot)asnode->ifl=locthenlethash=node|>strip_locations|>expr_to_hash|>assert_ok|>Script_expr_hash.to_b58checkin(Prim(-1,H_constant,[String(-1,hash)],[]),Somenode)elseletresult,x=loopargsin(Prim(l,prim,result,annot),x)|Seq(l,args)asnode->ifl=locthenlethash=node|>strip_locations|>expr_to_hash|>assert_ok|>Script_expr_hash.to_b58checkin(Prim(-1,H_constant,[String(-1,hash)],[]),Somenode)elseletresult,x=loopargsin(Seq(l,result),x)letmicheline_genp_genannot_gen=QCheck2.Gen.mapMicheline.strip_locations(micheline_node_gen(QCheck2.Gen.return(-1))p_genannot_gen)letcanonical_without_constant_gen()=QCheck2.Gen.mapstrip_locations(micheline_node_gen(QCheck2.Gen.return(-1))prims_without_constants_gen(QCheck2.Gen.return[]))letcanonical_with_constant_gen()=letopenQCheck2.Genincanonical_without_constant_gen()>>=funexpr->letsize=Script_repr.micheline_nodes(rootexpr)in0--(size-1)>|=funloc->matchreplace_with_constant(rootexpr)locwith|_,None->assertfalse|node,Somereplaced_node->(expr,strip_locationsnode,strip_locationsreplaced_node)end