diff --git a/src/proto_alpha/lib_benchmarks_proto/cache_benchmarks.ml b/src/proto_alpha/lib_benchmarks_proto/cache_benchmarks.ml index 875d80b6897aba48905450a924e4387d9a19f420..e6cdd5e17d3b6ac10d1adabe6509bf25b52f28d8 100644 --- a/src/proto_alpha/lib_benchmarks_proto/cache_benchmarks.ml +++ b/src/proto_alpha/lib_benchmarks_proto/cache_benchmarks.ml @@ -95,14 +95,12 @@ module Cache_shared_config = struct end (* We can't produce a Script_cache.identifier without calling [Script_cache.find]. *) -let identifier_of_contract (c : Alpha_context.Contract.t) : Cache.identifier = +let identifier_of_contract (c : Contract_hash.t) : Cache.identifier = let _, id, _ = Cache.find throwaway_context c |> assert_ok_lwt in id -let contract_of_int i : Alpha_context.Contract.t = - Alpha_context.Contract.of_b58check - Contract_hash.(to_b58check (hash_string [string_of_int i])) - |> assert_ok +let contract_of_int i : Contract_hash.t = + Contract_hash.(of_b58check_exn (to_b58check (hash_string [string_of_int i]))) let identifier_of_int i = identifier_of_contract @@ contract_of_int i diff --git a/src/proto_alpha/lib_client/client_proto_context.mli b/src/proto_alpha/lib_client/client_proto_context.mli index dac109654d597a833354f59a749a5527ad0f8318..abcfb614768d112267e5054efa937eff6b6b00d5 100644 --- a/src/proto_alpha/lib_client/client_proto_context.mli +++ b/src/proto_alpha/lib_client/client_proto_context.mli @@ -399,13 +399,13 @@ val cached_contracts : #Protocol_client_context.full -> chain:Shell_services.chain -> block:Shell_services.block -> - (Contract.t * int) list tzresult Lwt.t + (Contract_hash.t * int) list tzresult Lwt.t val contract_rank : #Protocol_client_context.full -> chain:Shell_services.chain -> block:Shell_services.block -> - Contract.t -> + Contract_hash.t -> int option tzresult Lwt.t val contract_cache_size : diff --git a/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml b/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml index 7496e2e7c9982855b898870bef19852cf5348772..36a849a75bd63f9ee55d5623dc86358355882d88 100644 --- a/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml +++ b/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml @@ -201,8 +201,7 @@ let commands_ro () = cached_contracts cctxt ~chain:cctxt#chain ~block:cctxt#block >>=? fun keys -> List.iter_s - (fun (key, size) -> - cctxt#message "%a %d" Alpha_context.Contract.pp key size) + (fun (key, size) -> cctxt#message "%a %d" Contract_hash.pp key size) keys >>= fun () -> return_unit); command @@ -213,15 +212,11 @@ let commands_ro () = @@ OriginatedContractAlias.destination_param ~name:"src" ~desc:"contract" @@ stop) (fun () contract (cctxt : Protocol_client_context.full) -> - let contract = Contract.Originated contract in contract_rank cctxt ~chain:cctxt#chain ~block:cctxt#block contract >>=? fun rank -> match rank with | None -> - cctxt#error - "Invalid contract: %a" - Alpha_context.Contract.pp - contract + cctxt#error "Invalid contract: %a" Contract_hash.pp contract >>= fun () -> return_unit | Some rank -> cctxt#message "%d" rank >>= fun () -> return_unit); command diff --git a/src/proto_alpha/lib_protocol/alpha_services.ml b/src/proto_alpha/lib_protocol/alpha_services.ml index 6c8161d4cae1a9cdffedded66adff639f7125df7..ad6eccf851a5488604fdb8cdb2abf85975acc173 100644 --- a/src/proto_alpha/lib_protocol/alpha_services.ml +++ b/src/proto_alpha/lib_protocol/alpha_services.ml @@ -197,8 +197,7 @@ module Cache = struct RPC_service.get_service ~description:"Return the list of cached contracts" ~query:RPC_query.empty - ~output: - Data_encoding.(list @@ tup2 Alpha_context.Contract.encoding int31) + ~output:Data_encoding.(list @@ tup2 Contract_hash.encoding int31) RPC_path.(custom_root / "context" / "cache" / "contracts" / "all") let contract_cache_size = @@ -222,7 +221,7 @@ module Cache = struct "Return the number of cached contracts older than the provided \ contract" ~query:RPC_query.empty - ~input:Alpha_context.Contract.encoding + ~input:Alpha_context.Contract.originated_encoding ~output:Data_encoding.(option int31) RPC_path.(custom_root / "context" / "cache" / "contracts" / "rank") end diff --git a/src/proto_alpha/lib_protocol/alpha_services.mli b/src/proto_alpha/lib_protocol/alpha_services.mli index 25a22c348773649c62ff33eee177f7e832d19971..5bfc4054c68bb5259cbbae65200a5d0537f44e6c 100644 --- a/src/proto_alpha/lib_protocol/alpha_services.mli +++ b/src/proto_alpha/lib_protocol/alpha_services.mli @@ -70,7 +70,7 @@ module Cache : sig val cached_contracts : 'a #RPC_context.simple -> 'a -> - (Alpha_context.Contract.t * int) list shell_tzresult Lwt.t + (Contract_hash.t * int) list shell_tzresult Lwt.t val contract_cache_size : 'a #RPC_context.simple -> 'a -> int shell_tzresult Lwt.t @@ -81,7 +81,7 @@ module Cache : sig val contract_rank : 'a #RPC_context.simple -> 'a -> - Alpha_context.Contract.t -> + Contract_hash.t -> int option shell_tzresult Lwt.t end diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 42694c1fbcf1b93a238992fbb77f6f3c4d37cc50..50c8602820daf7cce3ca7188051a81e0b54fc6d9 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -938,7 +938,7 @@ let apply_transaction_to_smart_contract ~ctxt ~source ~contract ~amount let apply_transaction ~ctxt ~parameter ~source ~(contract : Contract.t) ~amount ~entrypoint ~before_operation ~payer ~chain_id ~mode ~internal = match contract with - | Originated _ -> ( + | Originated contract_hash -> ( (* Since the contract is originated, nothing will be allocated or this transfer of tokens will fail. Calls to non-existing contracts are detected by [Script_cache.find] @@ -947,7 +947,7 @@ let apply_transaction ~ctxt ~parameter ~source ~(contract : Contract.t) ~amount *) Token.transfer ctxt (`Contract source) (`Contract contract) amount >>=? fun (ctxt, balance_updates) -> - Script_cache.find ctxt contract >>=? fun (ctxt, cache_key, script) -> + Script_cache.find ctxt contract_hash >>=? fun (ctxt, cache_key, script) -> match script with | None -> fail (Contract.Non_existing_contract contract) | Some (script, script_ir) -> @@ -3131,7 +3131,7 @@ let apply_liquidity_baking_subsidy ctxt ~toggle_vote = (`Contract liquidity_baking_cpmm_contract) liquidity_baking_subsidy >>=? fun (ctxt, balance_updates) -> - Script_cache.find ctxt liquidity_baking_cpmm_contract + Script_cache.find ctxt liquidity_baking_cpmm_contract_hash >>=? fun (ctxt, cache_key, script) -> match script with | None -> fail (Script_tc_errors.No_such_entrypoint Entrypoint.default) diff --git a/src/proto_alpha/lib_protocol/script_cache.ml b/src/proto_alpha/lib_protocol/script_cache.ml index 74ecd7973811e0f07e4fa8dcc8f0b339ce13e54f..6750a99d20616a4ed0a0568175b8f2476e355046 100644 --- a/src/proto_alpha/lib_protocol/script_cache.ml +++ b/src/proto_alpha/lib_protocol/script_cache.ml @@ -27,13 +27,17 @@ open Alpha_context type identifier = string -let identifier_of_contract addr = Contract.to_b58check addr +let identifier_of_contract addr = Contract_hash.to_b58check addr -let contract_of_identifier identifier = Contract.of_b58check identifier +let contract_of_identifier identifier = + match Contract_hash.of_b58check_opt identifier with + | Some addr -> Ok addr + | None -> error (Contract_repr.Invalid_contract_notation identifier) type cached_contract = Script.t * Script_ir_translator.ex_script let load_and_elaborate ctxt addr = + let addr = Contract.Originated addr in Contract.get_script ctxt addr >>=? fun (ctxt, script) -> match script with | None -> return (ctxt, None) diff --git a/src/proto_alpha/lib_protocol/script_cache.mli b/src/proto_alpha/lib_protocol/script_cache.mli index 09ebdb26b17d06872a3b622a9cfa7979b5715146..03a856926c3875cd27c5063e89989c797c6ad44d 100644 --- a/src/proto_alpha/lib_protocol/script_cache.mli +++ b/src/proto_alpha/lib_protocol/script_cache.mli @@ -54,7 +54,7 @@ type cached_contract = Script.t * Script_ir_translator.ex_script and [Script_ir_translator.parse_script]. *) val find : context -> - Contract.t -> + Contract_hash.t -> (context * identifier * cached_contract option) tzresult Lwt.t (** [update ctxt identifier unparsed_script ir_script size] refreshes the @@ -65,13 +65,13 @@ val update : context -> identifier -> cached_contract -> int -> context tzresult (** [entries ctxt] returns the contracts in the cache as well as their respective size. The list is sorted by date of last modification: the least recently updated entry comes first. *) -val entries : context -> (Contract.t * int) list tzresult +val entries : context -> (Contract_hash.t * int) list tzresult (** [contract_rank ctxt contract] returns the number of contracts older than [contract] in the cache of [ctxt]. This function returns [None] if [contract] does not exist in the cache of [ctxt]. *) -val contract_rank : context -> Contract.t -> int option +val contract_rank : context -> Contract_hash.t -> int option (** [size ctxt] is an overapproximation of the cache size in memory (in bytes). *) @@ -81,5 +81,6 @@ val size : context -> int val size_limit : context -> int (** [insert] is a variant of [update] which identifies the contract to update - by its address (of type [Contract.t]) instead of its cache identifier. *) -val insert : context -> Contract.t -> cached_contract -> int -> context tzresult + by its address (of type [Contract_hash.t]) instead of its cache identifier. *) +val insert : + context -> Contract_hash.t -> cached_contract -> int -> context tzresult diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_script_cache.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_script_cache.ml index a02bda8b2508cada24fad547c4a5bea2e5154596..13962d62487faf53810f33240b02a0e9bd161c61 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_script_cache.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_script_cache.ml @@ -49,9 +49,7 @@ let err x = Exn (Script_cache_test_error x) let liquidity_baking_contract_size = 267304 let liquidity_baking_contract = - Contract.of_b58check "KT1TxqZ8QtKvLu3V3JH7Gx58n7Co8pgtpQU5" |> function - | Ok x -> x - | _ -> assert false + Contract_hash.of_b58check_exn "KT1TxqZ8QtKvLu3V3JH7Gx58n7Co8pgtpQU5" let make_block block f = Incremental.begin_construction block >>=? fun incr -> @@ -96,7 +94,7 @@ let add_some_contracts k src block baker = >>=? fun (liquidity_baking_contract_id, block) -> List.fold_left_es (fun (rev_contracts, block) _ -> - originate_contract "contracts/int-store.tz" "31" src block baker + originate_contract_hash "contracts/int-store.tz" "31" src block baker >>=? fun (addr, block) -> Block.bake block >>=? fun block -> make_block block @@ fun ctxt -> @@ -143,7 +141,7 @@ let test_size_of_liquidity_baking_contract () = let test_size_of_int_store_contract () = init () >>=? fun (block, baker, src, _) -> - originate_contract "contracts/int-store.tz" "31" src block baker + originate_contract_hash "contracts/int-store.tz" "31" src block baker >>=? fun (addr, block) -> ( make_block block @! fun ctxt -> Script_cache.find ctxt addr >|= Environment.wrap_tzresult @@ -160,7 +158,7 @@ let test_size_of_int_store_contract () = *) let test_find_correctly_looks_up () = init () >>=? fun (block, baker, src, _) -> - originate_contract "contracts/sapling_contract.tz" "{ }" src block baker + originate_contract_hash "contracts/sapling_contract.tz" "{ }" src block baker >>=? fun (addr, block) -> ( make_block block @! fun ctxt -> (* @@ -168,7 +166,8 @@ let test_find_correctly_looks_up () = *) Script_cache.find ctxt addr >|= Environment.wrap_tzresult >>=? fun (_, _, result) -> - Contract.get_script ctxt addr >|= Environment.wrap_tzresult + Contract.get_script ctxt (Contract.Originated addr) + >|= Environment.wrap_tzresult >>=? fun (ctxt, script) -> (match (result, script) with | None, _ -> ok false @@ -185,7 +184,6 @@ let test_find_correctly_looks_up () = *) >>=? fun () -> let addr = Contract_helpers.fake_KT1 in - let addr = Contract.Originated addr in Script_cache.find ctxt addr >|= Environment.wrap_tzresult >>=? fun (_, _, cached_contract) -> fail_unless @@ -200,7 +198,7 @@ let test_find_correctly_looks_up () = *) let test_update_modifies_cached_contract () = init () >>=? fun (block, baker, src, _) -> - originate_contract "contracts/int-store.tz" "36" src block baker + originate_contract_hash "contracts/int-store.tz" "36" src block baker >>=? fun (addr, block) -> ( make_block block @! fun ctxt -> find ctxt addr >>=? fun (ctxt, identifier, script, Ex_script (Script ir)) -> diff --git a/src/proto_alpha/lib_protocol/ticket_operations_diff.ml b/src/proto_alpha/lib_protocol/ticket_operations_diff.ml index 22a4adde45df6e0ebcdd91bca0bd0b23a220fd58..737deb72c42a844249a6014931c2a134adb379d0 100644 --- a/src/proto_alpha/lib_protocol/ticket_operations_diff.ml +++ b/src/proto_alpha/lib_protocol/ticket_operations_diff.ml @@ -178,7 +178,7 @@ let tickets_of_transaction ctxt ~(destination : Contract.t) ~entrypoint ~location ~parameters_ty ~parameters = match destination with | Implicit _ -> return (None, ctxt) - | Originated _ -> + | Originated contract_hash -> (* TODO: #2653 Avoid having to load the script from the cache. This is currently in place to avoid regressions for type-checking @@ -186,7 +186,7 @@ let tickets_of_transaction ctxt ~(destination : Contract.t) ~entrypoint *) parse_and_cache_script ctxt - ~destination + ~destination:contract_hash ~get_non_cached_script:(fun ctxt -> (* Look up the script from the context. *) Contract.get_script ctxt destination >>=? fun (ctxt, script_opt) ->