diff --git a/src/proto_alpha/lib_plugin/plugin.ml b/src/proto_alpha/lib_plugin/plugin.ml index eee1d35ef3d20ba8cc7bfe12ecd4566711ad458f..e6ec2daf2091413e58122e643f78d497a16485ba 100644 --- a/src/proto_alpha/lib_plugin/plugin.ml +++ b/src/proto_alpha/lib_plugin/plugin.ml @@ -2427,8 +2427,9 @@ module RPC = struct let register () = let originate_dummy_contract ctxt script balance = let ctxt = Origination_nonce.init ctxt Operation_hash.zero in - Lwt.return (Contract.fresh_contract_from_current_nonce ctxt) - >>=? fun (ctxt, dummy_contract) -> + Contract.fresh_contract_from_current_nonce ctxt + >>?= fun (ctxt, dummy_contract_hash) -> + let dummy_contract = Contract.Originated dummy_contract_hash in Contract.raw_originate ctxt ~prepaid_bootstrap_storage:false diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index eb644ef194d07a8b2fff2022e2638549584c9205..72597091ad41491601db20196737c5c9470b5a9f 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -9,10 +9,10 @@ "State_hash", "Nonce_hash", "Script_expr_hash", + "Origination_nonce", "Contract_hash", "Blinded_public_key_hash", "Block_payload_hash", - "Origination_nonce", "Tx_rollup_prefixes", "Merkle_list", "Bitset", diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index eb9ca6c4b6c3ce23f37779183b3e2e562b187ee2..8256832fb6ddac4574c5f04ce0a755aa02a97e6a 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -1479,6 +1479,8 @@ module Contract : sig include BASIC_DATA with type t := t + val originated_encoding : Contract_hash.t Data_encoding.t + val in_memory_size : t -> Cache_memory_helpers.sint val rpc_arg : t RPC_arg.arg @@ -1520,7 +1522,8 @@ module Contract : sig val get_balance_carbonated : context -> t -> (context * Tez.t) tzresult Lwt.t - val fresh_contract_from_current_nonce : context -> (context * t) tzresult + val fresh_contract_from_current_nonce : + context -> (context * Contract_hash.t) tzresult val originated_from_current_nonce : since:context -> until:context -> t list tzresult Lwt.t @@ -3500,7 +3503,7 @@ module Liquidity_baking : sig val liquidity_baking_toggle_vote_encoding : liquidity_baking_toggle_vote Data_encoding.encoding - val get_cpmm_address : context -> Contract.t tzresult Lwt.t + val get_cpmm_address : context -> Contract_hash.t tzresult Lwt.t module Toggle_EMA : sig type t @@ -3515,7 +3518,7 @@ module Liquidity_baking : sig val on_subsidy_allowed : context -> toggle_vote:liquidity_baking_toggle_vote -> - (context -> Contract.t -> (context * 'a list) tzresult Lwt.t) -> + (context -> Contract_hash.t -> (context * 'a list) tzresult Lwt.t) -> (context * 'a list * Toggle_EMA.t) tzresult Lwt.t end diff --git a/src/proto_alpha/lib_protocol/alpha_services.ml b/src/proto_alpha/lib_protocol/alpha_services.ml index a75da8eb8b12ad10ce606ade960acb84a8806487..6c8161d4cae1a9cdffedded66adff639f7125df7 100644 --- a/src/proto_alpha/lib_protocol/alpha_services.ml +++ b/src/proto_alpha/lib_protocol/alpha_services.ml @@ -178,7 +178,7 @@ module Liquidity_baking = struct RPC_service.get_service ~description:"Liquidity baking CPMM address" ~query:RPC_query.empty - ~output:Alpha_context.Contract.encoding + ~output:Alpha_context.Contract.originated_encoding RPC_path.(custom_root / "context" / "liquidity_baking" / "cpmm_address") end diff --git a/src/proto_alpha/lib_protocol/alpha_services.mli b/src/proto_alpha/lib_protocol/alpha_services.mli index 468c0241e44e56ad75efe8779ab05c372b1167d6..25a22c348773649c62ff33eee177f7e832d19971 100644 --- a/src/proto_alpha/lib_protocol/alpha_services.mli +++ b/src/proto_alpha/lib_protocol/alpha_services.mli @@ -63,9 +63,7 @@ module Sapling = Sapling_services module Liquidity_baking : sig val get_cpmm_address : - 'a #RPC_context.simple -> - 'a -> - Alpha_context.Contract.t shell_tzresult Lwt.t + 'a #RPC_context.simple -> 'a -> Contract_hash.t shell_tzresult Lwt.t end module Cache : sig diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 6265f3a43bd4ff4cd6aac1452628a3713af693e8..cb7a1cd95d455e24ce4938ba70c7c24b36c88b2c 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -1414,7 +1414,8 @@ let apply_external_manager_operation_content : so that the script can use it immediately. The address of external originations is generated here. *) Contract.fresh_contract_from_current_nonce ctxt - >>?= fun (ctxt, contract) -> + >>?= fun (ctxt, contract_hash) -> + let contract = Contract.Originated contract_hash in Script.force_decode_in_context ~consume_deserialization_gas ctxt @@ -3107,7 +3108,10 @@ let apply_liquidity_baking_subsidy ctxt ~toggle_vote = Liquidity_baking.on_subsidy_allowed ctxt ~toggle_vote - (fun ctxt liquidity_baking_cpmm_contract -> + (fun ctxt liquidity_baking_cpmm_contract_hash -> + let liquidity_baking_cpmm_contract = + Contract.Originated liquidity_baking_cpmm_contract_hash + in let ctxt = (* We set a gas limit of 1/20th the block limit, which is ~10x actual usage here in Granada. Gas consumed is reported in diff --git a/src/proto_alpha/lib_protocol/bootstrap_storage.ml b/src/proto_alpha/lib_protocol/bootstrap_storage.ml index df398aca79c9eac5209b10b3a0ac8ac06d1170f5..a5f9715e5a6b26d78e0988ca8cae670fa511a958 100644 --- a/src/proto_alpha/lib_protocol/bootstrap_storage.ml +++ b/src/proto_alpha/lib_protocol/bootstrap_storage.ml @@ -47,7 +47,8 @@ let init_account (ctxt, balance_updates) let init_contract ~typecheck (ctxt, balance_updates) ({delegate; amount; script} : Parameters_repr.bootstrap_contract) = Contract_storage.fresh_contract_from_current_nonce ctxt - >>?= fun (ctxt, contract) -> + >>?= fun (ctxt, contract_hash) -> + let contract = Contract_repr.Originated contract_hash in typecheck ctxt script >>=? fun (script, ctxt) -> Contract_storage.raw_originate ctxt diff --git a/src/proto_alpha/lib_protocol/contract_hash.ml b/src/proto_alpha/lib_protocol/contract_hash.ml index c232a80278df99e91c14f1829e6559c19984d36b..8583c1839303e52408b80c5016076488d8d0e0f9 100644 --- a/src/proto_alpha/lib_protocol/contract_hash.ml +++ b/src/proto_alpha/lib_protocol/contract_hash.ml @@ -43,3 +43,9 @@ include H include Path_encoding.Make_hex (H) let () = Base58.check_encoded_prefix b58check_encoding "KT1" 36 + +let of_nonce nonce = + let data = + Data_encoding.Binary.to_bytes_exn Origination_nonce.encoding nonce + in + hash_bytes [data] diff --git a/src/proto_alpha/lib_protocol/contract_hash.mli b/src/proto_alpha/lib_protocol/contract_hash.mli index 1cbc424150af082413b8b9cac6143377400edc3a..45a9f23cdf32c2f8ec0020c837db67ac80c6dbf2 100644 --- a/src/proto_alpha/lib_protocol/contract_hash.mli +++ b/src/proto_alpha/lib_protocol/contract_hash.mli @@ -27,3 +27,6 @@ (** A specialized Blake2B implementation for hashing contract identifiers. *) include S.HASH + +(** [of_nonce nonce] is the contract address originated from [nonce]. *) +val of_nonce : Origination_nonce.t -> t diff --git a/src/proto_alpha/lib_protocol/contract_repr.ml b/src/proto_alpha/lib_protocol/contract_repr.ml index 3f4dc97444d614f83c42aa62893bbea93f0bde16..b1cfe042a292ded324a08901333dbd1c60aabd57 100644 --- a/src/proto_alpha/lib_protocol/contract_repr.ml +++ b/src/proto_alpha/lib_protocol/contract_repr.ml @@ -181,11 +181,7 @@ let () = (function Invalid_contract_notation loc -> Some loc | _ -> None) (fun loc -> Invalid_contract_notation loc) -let originated_contract nonce = - let data = - Data_encoding.Binary.to_bytes_exn Origination_nonce.encoding nonce - in - Originated (Contract_hash.hash_bytes [data]) +let originated_contract nonce = Originated (Contract_hash.of_nonce nonce) let originated_contracts ~since: diff --git a/src/proto_alpha/lib_protocol/contract_storage.ml b/src/proto_alpha/lib_protocol/contract_storage.ml index ac00a29e514f6294bd5edf3f1e302f324354647c..6e06ef64ad23d69a3a67e20ad59a0c03bf449f69 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.ml +++ b/src/proto_alpha/lib_protocol/contract_storage.ml @@ -502,7 +502,7 @@ let list c = Storage.Contract.list c let fresh_contract_from_current_nonce c = Raw_context.increment_origination_nonce c >|? fun (c, nonce) -> - (c, Contract_repr.originated_contract nonce) + (c, Contract_hash.of_nonce nonce) let originated_from_current_nonce ~since:ctxt_since ~until:ctxt_until = Raw_context.get_origination_nonce ctxt_since >>?= fun since -> diff --git a/src/proto_alpha/lib_protocol/contract_storage.mli b/src/proto_alpha/lib_protocol/contract_storage.mli index 48421c4a1619272efa56a7186e427989220a30f0..344e1409caa4092abdcf44df56ad91060e06d798 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.mli +++ b/src/proto_alpha/lib_protocol/contract_storage.mli @@ -150,7 +150,7 @@ val raw_originate : Raw_context.t tzresult Lwt.t val fresh_contract_from_current_nonce : - Raw_context.t -> (Raw_context.t * Contract_repr.t) tzresult + Raw_context.t -> (Raw_context.t * Contract_hash.t) tzresult val originated_from_current_nonce : since:Raw_context.t -> diff --git a/src/proto_alpha/lib_protocol/dune.inc b/src/proto_alpha/lib_protocol/dune.inc index b2cb4c95ff94aa8b582c1fe129b270396a9bfd8f..c2a5f6a6ff2fbf1ec5390b78f9e4124ebf02a344 100644 --- a/src/proto_alpha/lib_protocol/dune.inc +++ b/src/proto_alpha/lib_protocol/dune.inc @@ -35,10 +35,10 @@ module CamlinternalFormatBasics = struct include CamlinternalFormatBasics end state_hash.mli state_hash.ml nonce_hash.mli nonce_hash.ml script_expr_hash.mli script_expr_hash.ml + origination_nonce.mli origination_nonce.ml contract_hash.mli contract_hash.ml blinded_public_key_hash.mli blinded_public_key_hash.ml block_payload_hash.mli block_payload_hash.ml - origination_nonce.mli origination_nonce.ml tx_rollup_prefixes.mli tx_rollup_prefixes.ml merkle_list.mli merkle_list.ml bitset.mli bitset.ml @@ -225,10 +225,10 @@ module CamlinternalFormatBasics = struct include CamlinternalFormatBasics end state_hash.mli state_hash.ml nonce_hash.mli nonce_hash.ml script_expr_hash.mli script_expr_hash.ml + origination_nonce.mli origination_nonce.ml contract_hash.mli contract_hash.ml blinded_public_key_hash.mli blinded_public_key_hash.ml block_payload_hash.mli block_payload_hash.ml - origination_nonce.mli origination_nonce.ml tx_rollup_prefixes.mli tx_rollup_prefixes.ml merkle_list.mli merkle_list.ml bitset.mli bitset.ml @@ -415,10 +415,10 @@ module CamlinternalFormatBasics = struct include CamlinternalFormatBasics end state_hash.mli state_hash.ml nonce_hash.mli nonce_hash.ml script_expr_hash.mli script_expr_hash.ml + origination_nonce.mli origination_nonce.ml contract_hash.mli contract_hash.ml blinded_public_key_hash.mli blinded_public_key_hash.ml block_payload_hash.mli block_payload_hash.ml - origination_nonce.mli origination_nonce.ml tx_rollup_prefixes.mli tx_rollup_prefixes.ml merkle_list.mli merkle_list.ml bitset.mli bitset.ml @@ -627,10 +627,10 @@ include Tezos_raw_protocol_alpha.Main State_hash Nonce_hash Script_expr_hash + Origination_nonce Contract_hash Blinded_public_key_hash Block_payload_hash - Origination_nonce Tx_rollup_prefixes Merkle_list Bitset @@ -858,10 +858,10 @@ include Tezos_raw_protocol_alpha.Main state_hash.mli state_hash.ml nonce_hash.mli nonce_hash.ml script_expr_hash.mli script_expr_hash.ml + origination_nonce.mli origination_nonce.ml contract_hash.mli contract_hash.ml blinded_public_key_hash.mli blinded_public_key_hash.ml block_payload_hash.mli block_payload_hash.ml - origination_nonce.mli origination_nonce.ml tx_rollup_prefixes.mli tx_rollup_prefixes.ml merkle_list.mli merkle_list.ml bitset.mli bitset.ml diff --git a/src/proto_alpha/lib_protocol/liquidity_baking_migration.ml b/src/proto_alpha/lib_protocol/liquidity_baking_migration.ml index ad3717117a3c6ecc640bcf583d45bc643fa1269b..8e773910ffb294657d95aca7d6fc146edf0f36f9 100644 --- a/src/proto_alpha/lib_protocol/liquidity_baking_migration.ml +++ b/src/proto_alpha/lib_protocol/liquidity_baking_migration.ml @@ -53,7 +53,8 @@ let null_address = Bytes.of_string "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" -let mainnet_tzBTC_address = "KT1PWx2mnDueood7fEmfbBDKx1D9BAnnXitn" +let mainnet_tzBTC_address = + Contract_hash.of_b58check_exn "KT1PWx2mnDueood7fEmfbBDKx1D9BAnnXitn" (** If token_pool, xtz_pool, or lqt_total are ever zero the CPMM will be permanently broken. Therefore, we initialize it with the null address @@ -110,7 +111,8 @@ let test_fa12_init_storage manager = ], [] ))) -let originate ctxt address ~balance script = +let originate ctxt address_hash ~balance script = + let address = Contract_repr.Originated address_hash in Contract_storage.raw_originate ctxt ~prepaid_bootstrap_storage:true @@ -174,11 +176,11 @@ let first_bootstrap_account = "edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav") let check_tzBTC ~typecheck current_level ctxt f = - Contract_repr.of_b58check mainnet_tzBTC_address >>?= fun tzBTC -> - Contract_storage.exists ctxt tzBTC >>= function + Contract_storage.exists ctxt (Contract_repr.Originated mainnet_tzBTC_address) + >>= function | true -> (* If tzBTC exists, we're on mainnet and we use it as the token address in the CPMM. *) - f ctxt tzBTC [] + f ctxt mainnet_tzBTC_address [] | false -> (* If the tzBTC contract does not exist, we originate a test FA1.2 contract using the same script as the LQT. This is so that we can test the contracts after performing the same protocol migration that will be done on mainnet. @@ -216,8 +218,8 @@ let init ctxt ~typecheck = code = Script_repr.lazy_expr Liquidity_baking_cpmm.script; storage = cpmm_init_storage - ~token_address:(Contract_repr.to_b58check token_address) - ~lqt_address:(Contract_repr.to_b58check lqt_address); + ~token_address:(Contract_hash.to_b58check token_address) + ~lqt_address:(Contract_hash.to_b58check lqt_address); } in typecheck ctxt cpmm_script >>=? fun (cpmm_script, ctxt) -> @@ -225,7 +227,7 @@ let init ctxt ~typecheck = Script_repr. { code = Script_repr.lazy_expr Liquidity_baking_lqt.script; - storage = lqt_init_storage (Contract_repr.to_b58check cpmm_address); + storage = lqt_init_storage (Contract_hash.to_b58check cpmm_address); } in typecheck ctxt lqt_script >>=? fun (lqt_script, ctxt) -> diff --git a/src/proto_alpha/lib_protocol/liquidity_baking_storage.ml b/src/proto_alpha/lib_protocol/liquidity_baking_storage.ml index 2c78115b223aa72152c19e169f8827bab8013106..0202241484b9650957134154000c3d8f6a4524e8 100644 --- a/src/proto_alpha/lib_protocol/liquidity_baking_storage.ml +++ b/src/proto_alpha/lib_protocol/liquidity_baking_storage.ml @@ -34,7 +34,8 @@ let get_toggle_ema ctxt = let on_cpmm_exists ctxt f = get_cpmm_address ctxt >>=? fun cpmm_contract -> - Contract_storage.exists ctxt cpmm_contract >>= function + Contract_storage.exists ctxt (Contract_repr.Originated cpmm_contract) + >>= function | false -> (* do nothing if the cpmm is not found *) return (ctxt, []) diff --git a/src/proto_alpha/lib_protocol/liquidity_baking_storage.mli b/src/proto_alpha/lib_protocol/liquidity_baking_storage.mli index a22a03927bfaf55a6bba03677b3523b6f05f44d9..4db50c0a25365f6276ea7bc0e8b826773f1729db 100644 --- a/src/proto_alpha/lib_protocol/liquidity_baking_storage.mli +++ b/src/proto_alpha/lib_protocol/liquidity_baking_storage.mli @@ -26,7 +26,7 @@ (** Get the address of the Constant-Product Market Maker receiving the Liquidity Baking subsidy *) -val get_cpmm_address : Raw_context.t -> Contract_repr.t tzresult Lwt.t +val get_cpmm_address : Raw_context.t -> Contract_hash.t tzresult Lwt.t (** [on_subsidy_allowed ctxt ~toggle_vote f] updates the toggle EMA according to [toggle_vote]. Then the callback function [f] is called if the following @@ -40,5 +40,5 @@ val get_cpmm_address : Raw_context.t -> Contract_repr.t tzresult Lwt.t val on_subsidy_allowed : Raw_context.t -> toggle_vote:Liquidity_baking_repr.liquidity_baking_toggle_vote -> - (Raw_context.t -> Contract_repr.t -> (Raw_context.t * 'a list) tzresult Lwt.t) -> + (Raw_context.t -> Contract_hash.t -> (Raw_context.t * 'a list) tzresult Lwt.t) -> (Raw_context.t * 'a list * Liquidity_baking_repr.Toggle_EMA.t) tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/script_interpreter_defs.ml b/src/proto_alpha/lib_protocol/script_interpreter_defs.ml index 44c0f255b918e1aad301bce392fd5685f1a272b7..91aca747c5ca9ad8c500b1ead80ee2de587ae260 100644 --- a/src/proto_alpha/lib_protocol/script_interpreter_defs.ml +++ b/src/proto_alpha/lib_protocol/script_interpreter_defs.ml @@ -646,7 +646,9 @@ let create_contract (ctxt, sc) gas storage_type code delegate credit init = unparse_data ctxt Optimized storage_type init >>=? fun (storage, ctxt) -> Gas.consume ctxt (Script.strip_locations_cost storage) >>?= fun ctxt -> let storage = Micheline.strip_locations storage in - Contract.fresh_contract_from_current_nonce ctxt >>?= fun (ctxt, contract) -> + Contract.fresh_contract_from_current_nonce ctxt + >>?= fun (ctxt, contract_hash) -> + let contract = Contract.Originated contract_hash in let origination = { credit; diff --git a/src/proto_alpha/lib_protocol/storage.ml b/src/proto_alpha/lib_protocol/storage.ml index 7d178652fa40da600012dfad6719762cbdff2be1..d4b0e00d6e3963d635932054eebf2a46e19d0fbc 100644 --- a/src/proto_alpha/lib_protocol/storage.ml +++ b/src/proto_alpha/lib_protocol/storage.ml @@ -1366,7 +1366,12 @@ module Liquidity_baking = struct (struct let name = ["liquidity_baking_cpmm_address"] end) - (Contract_repr) + (struct + type t = Contract_hash.t + + (* Keeping contract-compatible encoding to avoid migrating this. *) + let encoding = Contract_repr.originated_encoding + end) end module Ticket_balance = struct diff --git a/src/proto_alpha/lib_protocol/storage.mli b/src/proto_alpha/lib_protocol/storage.mli index 21565d0e3f31eb7fa830d24288b2eae059adb552..e97d724f49b9f0b839d0896f37bc411e08ee02d6 100644 --- a/src/proto_alpha/lib_protocol/storage.mli +++ b/src/proto_alpha/lib_protocol/storage.mli @@ -548,7 +548,7 @@ module Liquidity_baking : sig module Cpmm_address : Single_data_storage with type t := Raw_context.t - and type value = Contract_repr.t + and type value = Contract_hash.t end (** A map of [Script_repr.expr] values, indexed by their hash ([Script_expr_hash.t]). diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index e08f29d45aa3833f7a11a5b68f405f51412b61db..d4649b4d09a01b42b8a143861b6a3b33ef12124d 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -215,6 +215,7 @@ let get_liquidity_baking_subsidy ctxt = let get_liquidity_baking_cpmm_address ctxt = Alpha_services.Liquidity_baking.get_cpmm_address rpc_ctxt ctxt + >|=? fun hash -> Contract.Originated hash (* Voting *)