From 3a49d78fc79b1fc9284d2e8bf4a1848cef61126e Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 31 Aug 2022 11:56:35 +0200 Subject: [PATCH 01/11] Proto/Apply: must_exist always succeeds for implicit accounts --- src/proto_alpha/lib_protocol/apply.ml | 1 - 1 file changed, 1 deletion(-) diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 95e8eb0ea389..2a721d533a63 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -707,7 +707,6 @@ let apply_manager_operation : Lwt.t = fun ctxt_before_op ~source ~chain_id operation -> let source_contract = Contract.Implicit source in - Contract.must_exist ctxt_before_op source_contract >>=? fun () -> Gas.consume ctxt_before_op Michelson_v1_gas.Cost_of.manager_operation >>?= fun ctxt -> (* Note that [ctxt_before_op] will be used again later to compute -- GitLab From 61555238d0ace65fe0317d3f7b5102a15c3ddd6f Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 31 Aug 2022 12:05:51 +0200 Subject: [PATCH 02/11] Proto: Contract.get_script_code works for originated contracts only --- src/proto_alpha/lib_protocol/alpha_context.mli | 4 +++- src/proto_alpha/lib_protocol/contract_services.ml | 4 ++-- src/proto_alpha/lib_protocol/contract_storage.ml | 4 +++- src/proto_alpha/lib_protocol/contract_storage.mli | 2 +- src/proto_alpha/lib_protocol/script_ir_translator.ml | 3 ++- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 1c9a7ed52b97..c40c34f5fb5a 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -1642,7 +1642,9 @@ module Contract : sig context tzresult Lwt.t val get_script_code : - context -> t -> (context * Script.lazy_expr option) tzresult Lwt.t + context -> + Contract_hash.t -> + (context * Script.lazy_expr option) tzresult Lwt.t val get_script : context -> Contract_hash.t -> (context * Script.t option) tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/contract_services.ml b/src/proto_alpha/lib_protocol/contract_services.ml index 5f5b18efb532..b55fa2740e7b 100644 --- a/src/proto_alpha/lib_protocol/contract_services.ml +++ b/src/proto_alpha/lib_protocol/contract_services.ml @@ -429,7 +429,7 @@ let register () = (fun ctxt v entrypoint {normalize_types} () -> match (v : Contract.t) with | Implicit _ -> return_none - | Originated _ -> ( + | Originated v -> ( Contract.get_script_code ctxt v >>=? fun (_, expr) -> match expr with | None -> return_none @@ -469,7 +469,7 @@ let register () = (fun ctxt v {normalize_types} () -> match (v : Contract.t) with | Implicit _ -> return_none - | Originated _ -> ( + | Originated v -> ( Contract.get_script_code ctxt v >>=? fun (_, expr) -> match expr with | None -> return_none diff --git a/src/proto_alpha/lib_protocol/contract_storage.ml b/src/proto_alpha/lib_protocol/contract_storage.ml index ff38861ab5e9..052007553ca9 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.ml +++ b/src/proto_alpha/lib_protocol/contract_storage.ml @@ -501,7 +501,9 @@ let increment_counter c manager = Storage.Contract.Counter.get c contract >>=? fun contract_counter -> Storage.Contract.Counter.update c contract (Z.succ contract_counter) -let get_script_code c contract = Storage.Contract.Code.find c contract +let get_script_code c contract_hash = + let contract = Contract_repr.Originated contract_hash in + Storage.Contract.Code.find c contract let get_script c contract_hash = let contract = Contract_repr.Originated contract_hash in diff --git a/src/proto_alpha/lib_protocol/contract_storage.mli b/src/proto_alpha/lib_protocol/contract_storage.mli index c08e7b45fb57..7166af4c21c5 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.mli +++ b/src/proto_alpha/lib_protocol/contract_storage.mli @@ -103,7 +103,7 @@ val get_counter : val get_script_code : Raw_context.t -> - Contract_repr.t -> + Contract_hash.t -> (Raw_context.t * Script_repr.lazy_expr option) tzresult Lwt.t val get_script : diff --git a/src/proto_alpha/lib_protocol/script_ir_translator.ml b/src/proto_alpha/lib_protocol/script_ir_translator.ml index 323b16ae293f..41453d7ec67e 100644 --- a/src/proto_alpha/lib_protocol/script_ir_translator.ml +++ b/src/proto_alpha/lib_protocol/script_ir_translator.ml @@ -4514,7 +4514,8 @@ and parse_contract : | Originated contract_hash -> trace (Invalid_contract (loc, contract)) - ( Contract.get_script_code ctxt contract >>=? fun (ctxt, code) -> + ( Contract.get_script_code ctxt contract_hash + >>=? fun (ctxt, code) -> Lwt.return (match code with | None -> -- GitLab From da6250cc5257ddd5920b959ad1840915b3488353 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 31 Aug 2022 12:14:37 +0200 Subject: [PATCH 03/11] Proto/Tests/Tx_rollup: the withdraw contract is originated --- .../integration/operations/test_tx_rollup.ml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml index 1455ce742ca0..0a1d44037768 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml @@ -4081,7 +4081,7 @@ module Withdraw = struct Nat_ticket.init_deposit amount block tx_rollup account1 >>=? fun (operation, block, deposit_contract) -> Block.bake ~operation block >>=? fun block -> - Contract_helpers.originate_contract_from_string + Contract_helpers.originate_contract_from_string_hash ~script: (Format.sprintf {| parameter (ticket %s); @@ -4282,7 +4282,7 @@ module Withdraw = struct ~loc:__LOC__ block token_one - (Contract withdraw_contract) + (Contract (Originated withdraw_contract)) None >>=? fun () -> assert_ticket_balance ~loc:__LOC__ block token_one (Contract account1) None @@ -4380,7 +4380,7 @@ module Withdraw = struct ~loc:__LOC__ block token_one - (Contract withdraw_contract) + (Contract (Originated withdraw_contract)) None >>=? fun () -> assert_ticket_balance @@ -4417,7 +4417,7 @@ module Withdraw = struct (WithExceptions.Option.get ~loc:__LOC__ @@ Ticket_amount.of_z @@ Script_int.of_int64 int64_half_amount) - ~destination:withdraw_contract + ~destination:(Originated withdraw_contract) ~entrypoint >>=? fun operation -> Block.bake ~operation block >>=? fun block -> @@ -4432,7 +4432,7 @@ module Withdraw = struct assert (extra_storage_space = Z.zero) ; Incremental.begin_construction block >>=? fun i -> let ctxt = Incremental.alpha_ctxt i in - Contract.get_storage ctxt withdraw_contract + Contract.get_storage ctxt (Originated withdraw_contract) >>=?? fun (_ctxt, found_storage) -> let expected_storage = Format.sprintf @@ -4462,7 +4462,7 @@ module Withdraw = struct ~loc:__LOC__ block token_one - (Contract withdraw_contract) + (Contract (Originated withdraw_contract)) (Some (Int64.to_int int64_half_amount)) >>=? fun () -> assert_ticket_balance ~loc:__LOC__ block token_one (Contract account1) None @@ -4517,7 +4517,7 @@ module Withdraw = struct ~loc:__LOC__ block token_one - (Contract withdraw_contract) + (Contract (Originated withdraw_contract)) (Some (Int64.to_int int64_half_amount)) >>=? fun () -> assert_ticket_balance ~loc:__LOC__ block token_one (Contract account1) None @@ -4737,7 +4737,7 @@ module Withdraw = struct tx_rollup committed_level context_hash - [{ticket_info with ticketer = withdraw_contract}] + [{ticket_info with ticketer = Originated withdraw_contract}] >>=? fun operation -> Incremental.add_operation ~expect_apply_failure: @@ -4904,7 +4904,7 @@ module Withdraw = struct ~ty:(Script.lazy_expr Nat_ticket.ty) ~ticketer:deposit_contract ~amount:qty - ~destination:withdraw_contract + ~destination:(Originated withdraw_contract) ~entrypoint:Entrypoint.default in (* Execute withdraw with half amount *) -- GitLab From 4b454f3f6cc94423005cc32a5d6f2446b392b358 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 31 Aug 2022 12:16:20 +0200 Subject: [PATCH 04/11] Proto/Tests/Tx_rollup: the withdraw dropping contract is originated --- .../test/integration/operations/test_tx_rollup.ml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml index 0a1d44037768..e6d7704af382 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml @@ -4256,7 +4256,7 @@ module Withdraw = struct deposit_contract, withdraw_contract, block ) -> - Contract_helpers.originate_contract_from_string + Contract_helpers.originate_contract_from_string_hash ~script: (Format.sprintf {| parameter (ticket %s); @@ -4492,7 +4492,7 @@ module Withdraw = struct (WithExceptions.Option.get ~loc:__LOC__ @@ Ticket_amount.of_z @@ Script_int.of_int64 int64_half_amount) - ~destination:withdraw_dropping_contract + ~destination:(Originated withdraw_dropping_contract) ~entrypoint >>=? fun operation -> Block.bake ~operation block >>=? fun block -> @@ -4500,7 +4500,7 @@ module Withdraw = struct expected *) Incremental.begin_construction block >>=? fun i -> let ctxt = Incremental.alpha_ctxt i in - Contract.get_storage ctxt withdraw_dropping_contract + Contract.get_storage ctxt (Originated withdraw_dropping_contract) >>=?? fun (_ctxt, found_storage) -> let expected_storage = "Unit" |> Expr.from_string |> Option.some in (if expected_storage = found_storage then return_unit -- GitLab From bdfe858c41abfa4428dd3440d6a1d60f9448e3cd Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 31 Aug 2022 12:18:47 +0200 Subject: [PATCH 05/11] Proto/Tests/Tx_rollup: the forge withdraw deposit contract is originated --- .../integration/operations/test_tx_rollup.ml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml index e6d7704af382..abdc34688e6a 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml @@ -4123,7 +4123,7 @@ module Withdraw = struct (account1, account2, tx_rollup, deposit_contract, withdraw_contract, b) let originate_forge_withdraw_deposit_contract account block = - Contract_helpers.originate_contract_from_string + Contract_helpers.originate_contract_from_string_hash ~script: {| parameter (or (pair %default nat nat) (or (ticket %withdraw nat) @@ -5460,7 +5460,7 @@ module Withdraw = struct |> Tezos_micheline.Micheline.strip_locations |> Script.lazy_expr) ~fee:Tez.one account - forge_withdraw_deposit_contract + (Originated forge_withdraw_deposit_contract) (Tez.of_mutez_exn 0L) >>=? fun operation -> Block.bake ~operation block in @@ -5478,14 +5478,14 @@ module Withdraw = struct |> Tezos_micheline.Micheline.strip_locations |> Script.lazy_expr) ~fee:Tez.one account - forge_withdraw_deposit_contract + (Originated forge_withdraw_deposit_contract) (Tez.of_mutez_exn 0L) >>=? fun operation -> Block.bake ~operation block in let dispatch_ticket block = Nat_ticket.withdrawal (B block) - ~ticketer:forge_withdraw_deposit_contract + ~ticketer:(Originated forge_withdraw_deposit_contract) ~claimer:account tx_rollup >>=? fun (withdraw, ticket_info) -> @@ -5522,24 +5522,24 @@ module Withdraw = struct ~source:account ~contents:(Script.lazy_expr Nat_ticket.contents) ~ty:(Script.lazy_expr Nat_ticket.ty) - ~ticketer:forge_withdraw_deposit_contract + ~ticketer:(Originated forge_withdraw_deposit_contract) ~amount: (WithExceptions.Option.get ~loc:__LOC__ @@ Ticket_amount.of_zint @@ Tx_rollup_l2_qty.to_z Nat_ticket.amount) - ~destination:forge_withdraw_deposit_contract + ~destination:(Originated forge_withdraw_deposit_contract) ~entrypoint:(Entrypoint.of_string_strict_exn "withdraw") >>=? fun operation -> Block.bake ~operation block in let token_one = - Nat_ticket.ex_token ~ticketer:forge_withdraw_deposit_contract + Nat_ticket.ex_token ~ticketer:(Originated forge_withdraw_deposit_contract) in let assert_contract_ticket_balance ~__LOC__ block balance = assert_ticket_balance ~loc:__LOC__ block token_one - (Contract forge_withdraw_deposit_contract) + (Contract (Originated forge_withdraw_deposit_contract)) balance in let assert_account_ticket_balance ~__LOC__ block balance = -- GitLab From 181628b1341a3eac33b2d92ba9a46b84620ed231 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 31 Aug 2022 12:09:16 +0200 Subject: [PATCH 06/11] Proto: Contract.get_storage works for originated contracts only --- src/proto_alpha/lib_protocol/alpha_context.mli | 2 +- src/proto_alpha/lib_protocol/contract_storage.ml | 3 ++- src/proto_alpha/lib_protocol/contract_storage.mli | 2 +- .../lib_protocol/test/integration/michelson/test_timelock.ml | 4 ++-- .../test/integration/operations/test_tx_rollup.ml | 4 ++-- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index c40c34f5fb5a..c6574b79d7d9 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -1650,7 +1650,7 @@ module Contract : sig context -> Contract_hash.t -> (context * Script.t option) tzresult Lwt.t val get_storage : - context -> t -> (context * Script.expr option) tzresult Lwt.t + context -> Contract_hash.t -> (context * Script.expr option) tzresult Lwt.t val get_counter : context -> public_key_hash -> Z.t tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/contract_storage.ml b/src/proto_alpha/lib_protocol/contract_storage.ml index 052007553ca9..92c6f0037733 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.ml +++ b/src/proto_alpha/lib_protocol/contract_storage.ml @@ -514,7 +514,8 @@ let get_script c contract_hash = | Some code, Some storage -> return (c, Some {Script_repr.code; storage}) | None, Some _ | Some _, None -> failwith "get_script" -let get_storage ctxt contract = +let get_storage ctxt contract_hash = + let contract = Contract_repr.Originated contract_hash in Storage.Contract.Storage.find ctxt contract >>=? function | ctxt, None -> return (ctxt, None) | ctxt, Some storage -> diff --git a/src/proto_alpha/lib_protocol/contract_storage.mli b/src/proto_alpha/lib_protocol/contract_storage.mli index 7166af4c21c5..87249b206064 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.mli +++ b/src/proto_alpha/lib_protocol/contract_storage.mli @@ -113,7 +113,7 @@ val get_script : val get_storage : Raw_context.t -> - Contract_repr.t -> + Contract_hash.t -> (Raw_context.t * Script_repr.expr option) tzresult Lwt.t module Legacy_big_map_diff : sig diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml index 7e548766f55d..bfb059e6f9b4 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_timelock.ml @@ -86,7 +86,7 @@ let contract_test () = let storage = "0xdeadbeef" in let script = Contract_helpers.read_file "./contracts/timelock.tz" in let* dst, _script, block = - Contract_helpers.originate_contract_from_string + Contract_helpers.originate_contract_from_string_hash ~script ~storage ~source_contract @@ -129,7 +129,7 @@ let contract_test () = ~fee (B block) source_contract - dst + (Originated dst) (Test_tez.of_int 3) ~parameters >>=? fun operation -> diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml index abdc34688e6a..7544d1caac1f 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_tx_rollup.ml @@ -4432,7 +4432,7 @@ module Withdraw = struct assert (extra_storage_space = Z.zero) ; Incremental.begin_construction block >>=? fun i -> let ctxt = Incremental.alpha_ctxt i in - Contract.get_storage ctxt (Originated withdraw_contract) + Contract.get_storage ctxt withdraw_contract >>=?? fun (_ctxt, found_storage) -> let expected_storage = Format.sprintf @@ -4500,7 +4500,7 @@ module Withdraw = struct expected *) Incremental.begin_construction block >>=? fun i -> let ctxt = Incremental.alpha_ctxt i in - Contract.get_storage ctxt (Originated withdraw_dropping_contract) + Contract.get_storage ctxt withdraw_dropping_contract >>=?? fun (_ctxt, found_storage) -> let expected_storage = "Unit" |> Expr.from_string |> Option.some in (if expected_storage = found_storage then return_unit -- GitLab From 691082ea06097e31a58a48d61d3d61ffadc0e126 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 31 Aug 2022 12:22:49 +0200 Subject: [PATCH 07/11] Proto: Contract.increase_paid_storage works for originated contracts only --- src/proto_alpha/lib_protocol/alpha_context.mli | 2 +- src/proto_alpha/lib_protocol/apply.ml | 3 +-- src/proto_alpha/lib_protocol/contract_storage.ml | 3 ++- src/proto_alpha/lib_protocol/contract_storage.mli | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index c6574b79d7d9..09550bfa32f9 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -1664,7 +1664,7 @@ module Contract : sig context -> public_key_hash -> Tez.t tzresult Lwt.t val increase_paid_storage : - context -> t -> amount_in_bytes:Z.t -> context tzresult Lwt.t + context -> Contract_hash.t -> amount_in_bytes:Z.t -> context tzresult Lwt.t val fresh_contract_from_current_nonce : context -> (context * Contract_hash.t) tzresult diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 2a721d533a63..f002e1193f86 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -1034,8 +1034,7 @@ let apply_manager_operation : {consumed_gas = Gas.consumed ~since:ctxt_before_op ~until:ctxt}, [] ) | Increase_paid_storage {amount_in_bytes; destination} -> - let contract = Contract.Originated destination in - Contract.increase_paid_storage ctxt contract ~amount_in_bytes + Contract.increase_paid_storage ctxt destination ~amount_in_bytes >>=? fun ctxt -> let payer = `Contract (Contract.Implicit source) in Fees.burn_storage_increase_fees ctxt ~payer amount_in_bytes diff --git a/src/proto_alpha/lib_protocol/contract_storage.ml b/src/proto_alpha/lib_protocol/contract_storage.ml index 92c6f0037733..e2f24de06f85 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.ml +++ b/src/proto_alpha/lib_protocol/contract_storage.ml @@ -637,7 +637,8 @@ let set_paid_storage_space_and_return_fees_to_pay c contract new_storage_space = Storage.Contract.Paid_storage_space.update c contract new_storage_space >|=? fun c -> (to_pay, c) -let increase_paid_storage c contract ~amount_in_bytes:storage_incr = +let increase_paid_storage c contract_hash ~amount_in_bytes:storage_incr = + let contract = Contract_repr.Originated contract_hash in Storage.Contract.Paid_storage_space.get c contract >>=? fun already_paid_space -> let new_storage_space = Z.add already_paid_space storage_incr in diff --git a/src/proto_alpha/lib_protocol/contract_storage.mli b/src/proto_alpha/lib_protocol/contract_storage.mli index 87249b206064..ea3d8f1ad50d 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.mli +++ b/src/proto_alpha/lib_protocol/contract_storage.mli @@ -192,7 +192,7 @@ val set_paid_storage_space_and_return_fees_to_pay : (** Enable a payer to increase the paid storage of a contract by some amount. *) val increase_paid_storage : Raw_context.t -> - Contract_repr.t -> + Contract_hash.t -> amount_in_bytes:Z.t -> Raw_context.t tzresult Lwt.t -- GitLab From 5ceac2ba38d8caee5c37f639f0a5290ad38c67a7 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 31 Aug 2022 12:28:05 +0200 Subject: [PATCH 08/11] Proto/Apply: update_script_storage_and_ticket_balances works for originated contracts only --- src/proto_alpha/lib_protocol/apply.ml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index f002e1193f86..614107e926dc 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -284,6 +284,7 @@ let assert_sc_rollup_feature_enabled ctxt = let update_script_storage_and_ticket_balances ctxt ~self_contract storage lazy_storage_diff ticket_diffs operations = + let self_contract = Contract.Originated self_contract in Contract.update_script_storage ctxt self_contract storage lazy_storage_diff >>=? fun ctxt -> Ticket_accounting.update_ticket_balances @@ -391,7 +392,7 @@ let apply_transaction_to_smart_contract ~ctxt ~source ~contract_hash ~amount ctxt ) -> update_script_storage_and_ticket_balances ctxt - ~self_contract:contract + ~self_contract:contract_hash storage lazy_storage_diff ticket_diffs @@ -2408,7 +2409,7 @@ let apply_liquidity_baking_subsidy ctxt ~toggle_vote = (* update CPMM storage *) update_script_storage_and_ticket_balances ctxt - ~self_contract:liquidity_baking_cpmm_contract + ~self_contract:liquidity_baking_cpmm_contract_hash storage lazy_storage_diff ticket_diffs -- GitLab From 54c57d826298356215c4ace57a917ea8e69504eb Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 31 Aug 2022 12:32:36 +0200 Subject: [PATCH 09/11] Proto: Contract.update_script_storage works for originated contracts only --- src/proto_alpha/lib_protocol/alpha_context.mli | 2 +- src/proto_alpha/lib_protocol/apply.ml | 2 +- src/proto_alpha/lib_protocol/contract_storage.ml | 3 ++- src/proto_alpha/lib_protocol/contract_storage.mli | 2 +- .../lib_protocol/test/helpers/big_map_helpers.ml | 2 +- .../michelson/test_ticket_accounting.ml | 15 +++++++++++++-- .../michelson/test_ticket_lazy_storage_diff.ml | 2 +- .../michelson/test_ticket_operations_diff.ml | 7 +++++-- .../integration/michelson/test_ticket_scanner.ml | 2 +- .../integration/michelson/test_typechecking.ml | 2 +- 10 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 09550bfa32f9..84cf1f0a383c 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -1699,7 +1699,7 @@ module Contract : sig val update_script_storage : context -> - t -> + Contract_hash.t -> Script.expr -> Lazy_storage.diffs option -> context tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 614107e926dc..9a35c749dd91 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -284,9 +284,9 @@ let assert_sc_rollup_feature_enabled ctxt = let update_script_storage_and_ticket_balances ctxt ~self_contract storage lazy_storage_diff ticket_diffs operations = - let self_contract = Contract.Originated self_contract in Contract.update_script_storage ctxt self_contract storage lazy_storage_diff >>=? fun ctxt -> + let self_contract = Contract.Originated self_contract in Ticket_accounting.update_ticket_balances ctxt ~self_contract diff --git a/src/proto_alpha/lib_protocol/contract_storage.ml b/src/proto_alpha/lib_protocol/contract_storage.ml index e2f24de06f85..4f11a9171a4d 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.ml +++ b/src/proto_alpha/lib_protocol/contract_storage.ml @@ -558,7 +558,8 @@ let check_allocated_and_get_balance c pkh = | None -> fail (Empty_implicit_contract pkh) | Some balance -> return balance -let update_script_storage c contract storage lazy_storage_diff = +let update_script_storage c contract_hash storage lazy_storage_diff = + let contract = Contract_repr.Originated contract_hash in let storage = Script_repr.lazy_expr storage in update_script_lazy_storage c lazy_storage_diff >>=? fun (c, lazy_storage_size_diff) -> diff --git a/src/proto_alpha/lib_protocol/contract_storage.mli b/src/proto_alpha/lib_protocol/contract_storage.mli index ea3d8f1ad50d..2f97c357c19e 100644 --- a/src/proto_alpha/lib_protocol/contract_storage.mli +++ b/src/proto_alpha/lib_protocol/contract_storage.mli @@ -143,7 +143,7 @@ end val update_script_storage : Raw_context.t -> - Contract_repr.t -> + Contract_hash.t -> Script_repr.expr -> Lazy_storage_diff.diffs option -> Raw_context.t tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/test/helpers/big_map_helpers.ml b/src/proto_alpha/lib_protocol/test/helpers/big_map_helpers.ml index 45d025ea31dc..23a2b146034c 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/big_map_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/big_map_helpers.ml @@ -33,7 +33,7 @@ let make_big_map block ~source ~key_type ~value_type key_values = let key_type = Expr.from_string key_type in let value_type = Expr.from_string value_type in let* operation, originated = - Op.contract_origination (B block) source ~script:Op.dummy_script + Op.contract_origination_hash (B block) source ~script:Op.dummy_script in let* block = Block.bake ~operation block in let* incr = Incremental.begin_construction block in diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_accounting.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_accounting.ml index 724e401da45d..2a9b1945703d 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_accounting.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_accounting.ml @@ -182,7 +182,7 @@ let init () = let open Lwt_result_syntax in let* block, source = Context.init1 () in let* operation, originated = - Op.contract_origination (B block) source ~script:Op.dummy_script + Op.contract_origination_hash (B block) source ~script:Op.dummy_script in let* block = Block.bake ~operation block in let* inc = Incremental.begin_construction block in @@ -452,7 +452,7 @@ let assert_ticket_diffs ctxt ~loc ~self_contract ~arg_type ~storage_type ~arg wrap (Ticket_accounting.ticket_diffs ctxt - ~self_contract + ~self_contract:(Originated self_contract) ~arg_type_has_tickets ~storage_type_has_tickets ~arg @@ -464,6 +464,17 @@ let assert_ticket_diffs ctxt ~loc ~self_contract ~arg_type ~storage_type ~arg Environment.wrap_tzresult @@ Ticket_token_map.to_list ctxt ticket_diff in let* () = assert_equal_ticket_diffs ~loc ctxt ticket_diffs expected_diff in + let expected_receipt = + List.map + (fun (contract, contents, amounts) -> + let amounts = + List.map + (fun (contract, amount) -> (Contract.Originated contract, amount)) + amounts + in + (contract, contents, amounts)) + expected_receipt + in assert_equal_ticket_receipt ~loc ticket_receipt expected_receipt let assert_balance = Ticket_helpers.assert_balance diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_lazy_storage_diff.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_lazy_storage_diff.ml index 475856ed4327..754a4079cf2b 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_lazy_storage_diff.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_lazy_storage_diff.ml @@ -134,7 +134,7 @@ let make_alloc big_map_id alloc updates = let init () = let* block, source = Context.init1 () in let* operation, originated = - Op.contract_origination (B block) source ~script:Op.dummy_script + Op.contract_origination_hash (B block) source ~script:Op.dummy_script in let* block = Block.bake ~operation block in let* inc = Incremental.begin_construction block in diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_operations_diff.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_operations_diff.ml index 4589cab69389..81c853bff6c0 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_operations_diff.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_operations_diff.ml @@ -1040,7 +1040,7 @@ let test_originate_and_transfer () = let test_originate_big_map_with_tickets () = let* baker, ticketer, block = init () in let* operation, originated = - Op.contract_origination (B block) ticketer ~script:Op.dummy_script + Op.contract_origination_hash (B block) ticketer ~script:Op.dummy_script in let* block = Block.bake ~operation block in let* incr = Incremental.begin_construction block in @@ -1098,7 +1098,10 @@ let test_originate_big_map_with_tickets () = let test_transfer_big_map_with_tickets () = let* baker, ticketer_contract, block = init () in let* operation, originated = - Op.contract_origination (B block) ticketer_contract ~script:Op.dummy_script + Op.contract_origination_hash + (B block) + ticketer_contract + ~script:Op.dummy_script in let* block = Block.bake ~operation block in let* incr = Incremental.begin_construction block in diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_scanner.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_scanner.ml index 642318c089c3..f8e662e07c8e 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_scanner.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_ticket_scanner.ml @@ -186,7 +186,7 @@ let make_string_tickets ctxt ticketer_amounts = let tickets_from_big_map_ref ~pre_populated value_exp = let* block, source = Context.init1 () in let* operation, originated = - Op.contract_origination (B block) source ~script:Op.dummy_script + Op.contract_origination_hash (B block) source ~script:Op.dummy_script in let* block = Block.bake ~operation block in let* inc = Incremental.begin_construction block in diff --git a/src/proto_alpha/lib_protocol/test/integration/michelson/test_typechecking.ml b/src/proto_alpha/lib_protocol/test/integration/michelson/test_typechecking.ml index 723b75343c83..eacf459e588f 100644 --- a/src/proto_alpha/lib_protocol/test/integration/michelson/test_typechecking.ml +++ b/src/proto_alpha/lib_protocol/test/integration/michelson/test_typechecking.ml @@ -107,7 +107,7 @@ let test_context_with_nat_nat_big_map ?(sc_rollup_enable = false) () = }; } >>=? fun (b, source) -> - Op.contract_origination (B b) source ~script:Op.dummy_script + Op.contract_origination_hash (B b) source ~script:Op.dummy_script >>=? fun (operation, originated) -> Block.bake ~operation b >>=? fun b -> Incremental.begin_construction b >>=? fun v -> -- GitLab From bd8f3ecdb2582b278c70cadc927faa6f83182bb0 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 31 Aug 2022 12:48:22 +0200 Subject: [PATCH 10/11] Proto: Fees.record_paid_storage_space works for originated contracts only --- src/proto_alpha/lib_protocol/alpha_context.mli | 2 +- src/proto_alpha/lib_protocol/apply.ml | 6 +++--- src/proto_alpha/lib_protocol/fees_storage.ml | 3 ++- src/proto_alpha/lib_protocol/fees_storage.mli | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 84cf1f0a383c..6e8eeaf372e3 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -5003,7 +5003,7 @@ end (** This module re-exports definitions from {!Fees_storage}. *) module Fees : sig val record_paid_storage_space : - context -> Contract.t -> (context * Z.t * Z.t) tzresult Lwt.t + context -> Contract_hash.t -> (context * Z.t * Z.t) tzresult Lwt.t val record_global_constant_storage_space : context -> Z.t -> context * Z.t diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 9a35c749dd91..71cc2bda7be3 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -402,7 +402,7 @@ let apply_transaction_to_smart_contract ~ctxt ~source ~contract_hash ~amount ctxt ~storage_diff:ticket_table_size_diff >>=? fun (ticket_paid_storage_diff, ctxt) -> - Fees.record_paid_storage_space ctxt contract + Fees.record_paid_storage_space ctxt contract_hash >>=? fun (ctxt, new_size, contract_paid_storage_size_diff) -> Contract.originated_from_current_nonce ~since:before_operation ~until:ctxt >>=? fun originated_contracts -> @@ -531,7 +531,7 @@ let apply_origination ~ctxt ~storage_type ~storage ~unparsed_code >>=? fun ctxt -> Token.transfer ctxt (`Contract source) (`Contract contract) credit >>=? fun (ctxt, balance_updates) -> - Fees.record_paid_storage_space ctxt contract + Fees.record_paid_storage_space ctxt contract_hash >|=? fun (ctxt, size, paid_storage_size_diff) -> let result = { @@ -2417,7 +2417,7 @@ let apply_liquidity_baking_subsidy ctxt ~toggle_vote = >>=? fun (ticket_table_size_diff, ctxt) -> Fees.record_paid_storage_space ctxt - liquidity_baking_cpmm_contract + liquidity_baking_cpmm_contract_hash >>=? fun (ctxt, new_size, paid_storage_size_diff) -> Ticket_balance.adjust_storage_space ctxt diff --git a/src/proto_alpha/lib_protocol/fees_storage.ml b/src/proto_alpha/lib_protocol/fees_storage.ml index 364ba8d59ab0..d09f06b5ed9e 100644 --- a/src/proto_alpha/lib_protocol/fees_storage.ml +++ b/src/proto_alpha/lib_protocol/fees_storage.ml @@ -78,7 +78,8 @@ let record_global_constant_storage_space context size = let to_be_paid = Z.add size cost_of_key in (context, to_be_paid) -let record_paid_storage_space ctxt contract = +let record_paid_storage_space ctxt contract_hash = + let contract = Contract_repr.Originated contract_hash in (* Get the new size of the contract's storage. *) Contract_storage.used_storage_space ctxt contract >>=? fun new_storage_size -> Contract_storage.set_paid_storage_space_and_return_fees_to_pay diff --git a/src/proto_alpha/lib_protocol/fees_storage.mli b/src/proto_alpha/lib_protocol/fees_storage.mli index e66babbd7f9f..78a13395d977 100644 --- a/src/proto_alpha/lib_protocol/fees_storage.mli +++ b/src/proto_alpha/lib_protocol/fees_storage.mli @@ -47,7 +47,7 @@ val record_global_constant_storage_space : and the additional (and unpaid) space consumed since the last call of this function on this [contract]. *) val record_paid_storage_space : - Raw_context.t -> Contract_repr.t -> (Raw_context.t * Z.t * Z.t) tzresult Lwt.t + Raw_context.t -> Contract_hash.t -> (Raw_context.t * Z.t * Z.t) tzresult Lwt.t (** [check_storage_limit ctxt ~storage_limit] raises the [Storage_limit_too_high] error iff [storage_limit] is negative or greater the constant -- GitLab From 6ed542eb2dc5344849b267ee5c7b030f938db12b Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 31 Aug 2022 12:53:03 +0200 Subject: [PATCH 11/11] Proto/Alpha_context: classify functions in Contract --- .../lib_protocol/alpha_context.mli | 94 ++++++++++--------- 1 file changed, 52 insertions(+), 42 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 6e8eeaf372e3..2573191328e3 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -1596,6 +1596,8 @@ end module Contract : sig type t = Implicit of public_key_hash | Originated of Contract_hash.t + (** Functions related to contracts address. *) + type error += Non_existing_contract of t include BASIC_DATA with type t := t @@ -1612,6 +1614,8 @@ module Contract : sig val of_b58check : string -> t tzresult + (** Functions related to contracts existence. *) + val exists : context -> t -> bool Lwt.t val must_exist : context -> t -> unit tzresult Lwt.t @@ -1622,6 +1626,19 @@ module Contract : sig val list : context -> t list Lwt.t + (** Functions related to both implicit accounts and originated contracts. *) + + (** See {!Contract_storage.get_balance}. *) + val get_balance : context -> t -> Tez.t tzresult Lwt.t + + val get_balance_carbonated : context -> t -> (context * Tez.t) tzresult Lwt.t + + val get_frozen_bonds : context -> t -> Tez.t tzresult Lwt.t + + val get_balance_and_frozen_bonds : context -> t -> Tez.t tzresult Lwt.t + + (** Functions related to implicit accounts. *) + (** See {!Contract_manager_storage.get_manager_key}. *) val get_manager_key : ?error:error -> context -> public_key_hash -> public_key tzresult Lwt.t @@ -1641,6 +1658,27 @@ module Contract : sig public_key -> context tzresult Lwt.t + val get_counter : context -> public_key_hash -> Z.t tzresult Lwt.t + + val increment_counter : context -> public_key_hash -> context tzresult Lwt.t + + val check_counter_increment : + context -> public_key_hash -> Z.t -> unit tzresult Lwt.t + + (** See {!Contract_storage.check_allocated_and_get_balance}. *) + val check_allocated_and_get_balance : + context -> public_key_hash -> Tez.t tzresult Lwt.t + + (** See {!Contract_storage.simulate_spending}. *) + val simulate_spending : + context -> + balance:Tez.t -> + amount:Tez.t -> + public_key_hash -> + (Tez.t * bool) tzresult Lwt.t + + (** Functions related to smart contracts. *) + val get_script_code : context -> Contract_hash.t -> @@ -1652,16 +1690,9 @@ module Contract : sig val get_storage : context -> Contract_hash.t -> (context * Script.expr option) tzresult Lwt.t - val get_counter : context -> public_key_hash -> Z.t tzresult Lwt.t - - (** See {Contract_storage.get_balance}. *) - val get_balance : context -> t -> Tez.t tzresult Lwt.t - - val get_balance_carbonated : context -> t -> (context * Tez.t) tzresult Lwt.t + val used_storage_space : context -> t -> Z.t tzresult Lwt.t - (** See {Contract_storage.check_allocated_and_get_balance}. *) - val check_allocated_and_get_balance : - context -> public_key_hash -> Tez.t tzresult Lwt.t + val paid_storage_space : context -> t -> Z.t tzresult Lwt.t val increase_paid_storage : context -> Contract_hash.t -> amount_in_bytes:Z.t -> context tzresult Lwt.t @@ -1672,9 +1703,19 @@ module Contract : sig val originated_from_current_nonce : since:context -> until:context -> Contract_hash.t list tzresult Lwt.t - val get_frozen_bonds : context -> t -> Tez.t tzresult Lwt.t + val update_script_storage : + context -> + Contract_hash.t -> + Script.expr -> + Lazy_storage.diffs option -> + context tzresult Lwt.t - val get_balance_and_frozen_bonds : context -> t -> Tez.t tzresult Lwt.t + val raw_originate : + context -> + prepaid_bootstrap_storage:bool -> + Contract_hash.t -> + script:Script.t * Lazy_storage.diffs option -> + context tzresult Lwt.t module Legacy_big_map_diff : sig type item = private @@ -1697,37 +1738,6 @@ module Contract : sig val of_lazy_storage_diff : Lazy_storage.diffs -> t end - val update_script_storage : - context -> - Contract_hash.t -> - Script.expr -> - Lazy_storage.diffs option -> - context tzresult Lwt.t - - val used_storage_space : context -> t -> Z.t tzresult Lwt.t - - val paid_storage_space : context -> t -> Z.t tzresult Lwt.t - - val increment_counter : context -> public_key_hash -> context tzresult Lwt.t - - val check_counter_increment : - context -> public_key_hash -> Z.t -> unit tzresult Lwt.t - - (** See {Contract_storage.simulate_spending}. *) - val simulate_spending : - context -> - balance:Tez.t -> - amount:Tez.t -> - public_key_hash -> - (Tez.t * bool) tzresult Lwt.t - - val raw_originate : - context -> - prepaid_bootstrap_storage:bool -> - Contract_hash.t -> - script:Script.t * Lazy_storage.diffs option -> - context tzresult Lwt.t - (** Functions for handling the delegate of a contract.*) module Delegate : sig (** See {!Contract_delegate_storage.find}. *) -- GitLab