diff --git a/docs/protocols/alpha.rst b/docs/protocols/alpha.rst index 09a1d38a74cf42cacad4514dea889661080a26af..63d822e2411be316503b807bc2f87969a6ca9623 100644 --- a/docs/protocols/alpha.rst +++ b/docs/protocols/alpha.rst @@ -49,3 +49,13 @@ Michelson In their output encodings, the objects containing the fields ``item`` and ``annot`` are replaced with the contents of the field ``item``. (MR :gl:`!4139`) + +Internal +-------- + +The following changes are not visible to the users but reflect +improvements of the codebase. + +- ``BALANCE`` is now passed to the Michelson interpreter as a step constant + instead of being read from the context each time this instruction is + executed. (MR :gl:`!3871`) diff --git a/src/proto_alpha/lib_benchmark/execution_context.ml b/src/proto_alpha/lib_benchmark/execution_context.ml index 725e24204db8f1f27266e9804aa1a43a14bc3768..f9e6b1a1fd271848b73e5bfd4a82463950fc4cf0 100644 --- a/src/proto_alpha/lib_benchmark/execution_context.ml +++ b/src/proto_alpha/lib_benchmark/execution_context.ml @@ -28,16 +28,18 @@ open Error_monad type context = Alpha_context.context * Script_interpreter.step_constants +let initial_balance = 4_000_000_000_000L + let context_init_memory ~rng_state = Context.init ~rng_state ~initial_balances: [ - 4_000_000_000_000L; - 4_000_000_000_000L; - 4_000_000_000_000L; - 4_000_000_000_000L; - 4_000_000_000_000L; + initial_balance; + initial_balance; + initial_balance; + initial_balance; + initial_balance; ] 5 >>=? fun (block, accounts) -> @@ -61,12 +63,30 @@ let make ~rng_state = let payer = bs2 in let self = bs3 in let step_constants = - {source; payer; self; amount; chain_id; now; level} + { + source; + payer; + self; + amount; + balance = Alpha_context.Tez.of_mutez_exn initial_balance; + chain_id; + now; + level; + } in return (block, step_constants) | `Disk_block (block, source) -> let step_constants = - {source; payer = source; self = source; amount; chain_id; now; level} + { + source; + payer = source; + self = source; + amount; + balance = Alpha_context.Tez.of_mutez_exn initial_balance; + chain_id; + now; + level; + } in return (block, step_constants)) >>=? fun (block, step_constants) -> diff --git a/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml b/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml index b7570a634a91d7297eae5e0447c189b337dc12a2..3ec52df528f61cc3cbc9551c80d4c016cb79a3d3 100644 --- a/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml +++ b/src/proto_alpha/lib_benchmarks_proto/interpreter_benchmarks.ml @@ -2899,6 +2899,7 @@ module Registration_section = struct payer = zero; self = zero; amount = Tez.zero; + balance = Tez.zero; chain_id = Chain_id.zero; now = Script_timestamp.of_zint Z.zero; level = Script_int.zero_n; diff --git a/src/proto_alpha/lib_plugin/plugin.ml b/src/proto_alpha/lib_plugin/plugin.ml index 0360e939d21748ff23aeddbb679719896c4b4775..7cc34ca3d518cea451665b9636dc3522abc2e306 100644 --- a/src/proto_alpha/lib_plugin/plugin.ml +++ b/src/proto_alpha/lib_plugin/plugin.ml @@ -1967,7 +1967,16 @@ module RPC = struct in let step_constants = let open Script_interpreter in - {source; payer; self = dummy_contract; amount; chain_id; now; level} + { + source; + payer; + self = dummy_contract; + amount; + balance; + chain_id; + now; + level; + } in Script_interpreter.execute ctxt @@ -2033,7 +2042,16 @@ module RPC = struct in let step_constants = let open Script_interpreter in - {source; payer; self = dummy_contract; amount; chain_id; now; level} + { + source; + payer; + self = dummy_contract; + amount; + balance; + chain_id; + now; + level; + } in let module Unparsing_mode = struct let unparsing_mode = unparsing_mode @@ -2080,6 +2098,7 @@ module RPC = struct >>=? fun view_ty -> View_helpers.extract_view_output_type entrypoint view_ty >>?= fun ty -> + Contract.get_balance ctxt contract >>=? fun balance -> Error_monad.trace View_helpers.View_callback_origination_failed @@ originate_dummy_contract ctxt @@ -2116,6 +2135,7 @@ module RPC = struct payer; self = contract; amount = Tez.zero; + balance; chain_id; now; level; diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index f4e07926e0f36a13b48da7775c59561ae165e43c..ef57bcc9c97bee903a2edbb5c51a5cce098a6b8d 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -931,6 +931,9 @@ let apply_manager_operation_content : in (ctxt, result, []) ) | Some (script, script_ir) -> + (* Token.transfer which is being called above already loads this + value into the Irmin cache, so no need to burn gas for it. *) + Contract.get_balance ctxt destination >>=? fun balance -> let now = Script_timestamp.now ctxt in let level = (Level.current ctxt).level |> Raw_level.to_int32 @@ -938,7 +941,16 @@ let apply_manager_operation_content : in let step_constants = let open Script_interpreter in - {source; payer; self = destination; amount; chain_id; now; level} + { + source; + payer; + self = destination; + amount; + chain_id; + balance; + now; + level; + } in Script_interpreter.execute ctxt @@ -2389,6 +2401,10 @@ let apply_liquidity_baking_subsidy ctxt ~escape_vote = match script with | None -> fail (Script_tc_errors.No_such_entrypoint Entrypoint.default) | Some (script, script_ir) -> ( + (* Token.transfer which is being called above already loads this + value into the Irmin cache, so no need to burn gas for it. *) + Contract.get_balance ctxt liquidity_baking_cpmm_contract + >>=? fun balance -> let now = Script_timestamp.now ctxt in let level = (Level.current ctxt).level |> Raw_level.to_int32 @@ -2404,6 +2420,7 @@ let apply_liquidity_baking_subsidy ctxt ~escape_vote = payer = liquidity_baking_cpmm_contract; self = liquidity_baking_cpmm_contract; amount = liquidity_baking_subsidy; + balance; chain_id = Chain_id.zero; now; level; diff --git a/src/proto_alpha/lib_protocol/script_interpreter.ml b/src/proto_alpha/lib_protocol/script_interpreter.ml index 4486c4705b4f727b7f1fdf8692f2e0588104a836..f979279bd2b53fe5dd52a9ee029ea66f6e6f6358 100644 --- a/src/proto_alpha/lib_protocol/script_interpreter.ml +++ b/src/proto_alpha/lib_protocol/script_interpreter.ml @@ -95,6 +95,7 @@ type step_constants = Script_typed_ir.step_constants = { payer : Contract.t; self : Contract.t; amount : Tez.t; + balance : Tez.t; chain_id : Chain_id.t; now : Script_timestamp.t; level : Script_int.n Script_int.num; @@ -1102,6 +1103,8 @@ and step : type a s b t r f. (a, s, b, t, r, f) step_type = let kstack_ty = Item_t (output_ty, s) in let kkinfo = {kkinfo with kstack_ty} in let ks = KCons (ICons_some (kkinfo, k), ks) in + Contract.get_balance_carbonated ctxt c + >>=? fun (ctxt, balance) -> (step [@ocaml.tailcall]) ( outdated ctxt, { @@ -1109,6 +1112,7 @@ and step : type a s b t r f. (a, s, b, t, r, f) step_type = source = sc.self; self = c; amount = Tez.zero; + balance; } ) (update_local_gas_counter ctxt) kinstr @@ -1155,12 +1159,10 @@ and step : type a s b t r f. (a, s, b, t, r, f) step_type = (step [@ocaml.tailcall]) (ctxt, sc) gas k ks res stack | IBalance (_, k) -> let ctxt = update_context gas ctxt in - Contract.get_balance_carbonated ctxt sc.self - >>=? fun (ctxt, balance) -> let gas = update_local_gas_counter ctxt in let ctxt = outdated ctxt in let g = (ctxt, sc) in - (step [@ocaml.tailcall]) g gas k ks balance (accu, stack) + (step [@ocaml.tailcall]) g gas k ks sc.balance (accu, stack) | ILevel (_, k) -> (step [@ocaml.tailcall]) g gas k ks sc.level (accu, stack) | INow (_, k) -> (step [@ocaml.tailcall]) g gas k ks sc.now (accu, stack) diff --git a/src/proto_alpha/lib_protocol/script_interpreter.mli b/src/proto_alpha/lib_protocol/script_interpreter.mli index 0fce503243a42b375fe810862abeaa9ecb61499a..1d03e3cb796abbc54f808b0599d737ae720206ff 100644 --- a/src/proto_alpha/lib_protocol/script_interpreter.mli +++ b/src/proto_alpha/lib_protocol/script_interpreter.mli @@ -62,6 +62,7 @@ type step_constants = Script_typed_ir.step_constants = { payer : Contract.t; self : Contract.t; amount : Tez.t; + balance : Tez.t; chain_id : Chain_id.t; now : Script_timestamp.t; level : Script_int.n Script_int.num; diff --git a/src/proto_alpha/lib_protocol/script_typed_ir.ml b/src/proto_alpha/lib_protocol/script_typed_ir.ml index 1b795de98d01b707f0eeb91ddd5669ce95df9a5f..5d9fe73d0af7d87f3b5c45640cbdeecfb8bbe834 100644 --- a/src/proto_alpha/lib_protocol/script_typed_ir.ml +++ b/src/proto_alpha/lib_protocol/script_typed_ir.ml @@ -49,6 +49,7 @@ type step_constants = { - as caller in VIEW, TRANSFER_TOKENS, and CREATE_CONTRACT *) amount : Tez.t; (** The amount of the current transaction, as returned by AMOUNT. *) + balance : Tez.t; (** The balance of the contract as returned by BALANCE. *) chain_id : Chain_id.t; (** The chain id of the chain, as returned by CHAIN_ID. *) now : Script_timestamp.t; diff --git a/src/proto_alpha/lib_protocol/script_typed_ir.mli b/src/proto_alpha/lib_protocol/script_typed_ir.mli index 7c586021b6324af0bc74023f863fffd7ebe40cfb..4991feaedf3e57f1f520acfc067dcad9895bd270 100644 --- a/src/proto_alpha/lib_protocol/script_typed_ir.mli +++ b/src/proto_alpha/lib_protocol/script_typed_ir.mli @@ -33,6 +33,7 @@ type step_constants = { payer : Contract.t; self : Contract.t; amount : Tez.t; + balance : Tez.t; chain_id : Chain_id.t; now : Script_timestamp.t; level : Script_int.n Script_int.num; diff --git a/src/proto_alpha/lib_protocol/test/helpers/contract_helpers.ml b/src/proto_alpha/lib_protocol/test/helpers/contract_helpers.ml index ad37f1aa039d91bdd08ad150c9cabcf27fa6b77d..2570b95a18e5decabe6597818f83ebb08f68babe 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/contract_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/contract_helpers.ml @@ -73,6 +73,7 @@ let default_step_constants = payer = default_source; self = default_source; amount = Tez.zero; + balance = Tez.zero; chain_id = Chain_id.zero; now = Script_timestamp.of_zint Z.zero; level = Script_int.zero_n; diff --git a/tests_python/tests_alpha/_regtest_outputs/test_contract.TestSelfAddressTransfer::test_send_self_address.out b/tests_python/tests_alpha/_regtest_outputs/test_contract.TestSelfAddressTransfer::test_send_self_address.out index a7b397372f01bd7119070d0bd1bb6963d2c8211d..67a858252965fb3f52d8463964afdbe186b2cd4f 100644 --- a/tests_python/tests_alpha/_regtest_outputs/test_contract.TestSelfAddressTransfer::test_send_self_address.out +++ b/tests_python/tests_alpha/_regtest_outputs/test_contract.TestSelfAddressTransfer::test_send_self_address.out @@ -1,7 +1,7 @@ tests_alpha/test_contract.py::TestSelfAddressTransfer::test_send_self_address Node is bootstrapped. -Estimated gas: 5413.006 units (will add 100 for safety) +Estimated gas: 5192.990 units (will add 100 for safety) Estimated storage: no bytes added Operation successfully injected in the node. Operation hash is '[BLOCK_HASH]' @@ -12,13 +12,13 @@ and/or an external block explorer to make sure that it has been included. This sequence of operations was run: Manager signed operations: From: [CONTRACT_HASH] - Fee to the baker: ꜩ0.000846 + Fee to the baker: ꜩ0.000824 Expected counter: [EXPECTED_COUNTER] - Gas limit: 5514 + Gas limit: 5293 Storage limit: 0 bytes Balance updates: - [CONTRACT_HASH] ... -ꜩ0.000846 - payload fees(the block proposer) ....... +ꜩ0.000846 + [CONTRACT_HASH] ... -ꜩ0.000824 + payload fees(the block proposer) ....... +ꜩ0.000824 Transaction: Amount: ꜩ0 From: [CONTRACT_HASH] @@ -27,7 +27,7 @@ This sequence of operations was run: This transaction was successfully applied Updated storage: Unit Storage size: 82 bytes - Consumed gas: 3353.748 + Consumed gas: 3133.732 Internal operations: Transaction: Amount: ꜩ0 diff --git a/tests_python/tests_alpha/_regtest_outputs/test_contract_onchain_opcodes.TestContractOnchainOpcodes::test_slice_success[(Pair 0xe009ab79e8b84ef0e55c43a9a857214d8761e67b.7da5c9014e.out b/tests_python/tests_alpha/_regtest_outputs/test_contract_onchain_opcodes.TestContractOnchainOpcodes::test_slice_success[(Pair 0xe009ab79e8b84ef0e55c43a9a857214d8761e67b.7da5c9014e.out index 4f437d5c943178b8e78f58afdb8c792fc1f6a1a5..05e09c848dbf50838ccafe38825719e03b0f2ea1 100644 --- a/tests_python/tests_alpha/_regtest_outputs/test_contract_onchain_opcodes.TestContractOnchainOpcodes::test_slice_success[(Pair 0xe009ab79e8b84ef0e55c43a9a857214d8761e67b.7da5c9014e.out +++ b/tests_python/tests_alpha/_regtest_outputs/test_contract_onchain_opcodes.TestContractOnchainOpcodes::test_slice_success[(Pair 0xe009ab79e8b84ef0e55c43a9a857214d8761e67b.7da5c9014e.out @@ -1,7 +1,7 @@ tests_alpha/test_contract_onchain_opcodes.py::TestContractOnchainOpcodes::test_slice_success[(Pair 0xe009ab79e8b84ef0e55c43a9a857214d8761e67b75ba63500a5694fb2ffe174acc2de22d01ccb7259342437f05e1987949f0ad82e9f32e9a0b79cb252d7f7b8236ad728893f4e7150742eefdbeda254970f9fcd92c6228c178e1a923e5600758eb83f2a05edd0be7625657901f2ba81eaf145d003dbef78e33f43a32a3788bdf0501000000085341554349535345 "spsig1PPUFZucuAQybs5wsqsNQ68QNgFaBnVKMFaoZZfi1BtNnuCAWnmL9wVy5HfHkR6AeodjVGxpBVVSYcJKyMURn6K1yknYLm")] Node is bootstrapped. -Estimated gas: 4184.720 units (will add 100 for safety) +Estimated gas: 3964.704 units (will add 100 for safety) Estimated storage: 257 bytes added (will add 20 for safety) Operation successfully injected in the node. Operation hash is '[BLOCK_HASH]' @@ -12,13 +12,13 @@ and/or an external block explorer to make sure that it has been included. This sequence of operations was run: Manager signed operations: From: [CONTRACT_HASH] - Fee to the baker: ꜩ0.000936 + Fee to the baker: ꜩ0.000914 Expected counter: [EXPECTED_COUNTER] - Gas limit: 4285 + Gas limit: 4065 Storage limit: 277 bytes Balance updates: - [CONTRACT_HASH] ... -ꜩ0.000936 - payload fees(the block proposer) ....... +ꜩ0.000936 + [CONTRACT_HASH] ... -ꜩ0.000914 + payload fees(the block proposer) ....... +ꜩ0.000914 Transaction: Amount: ꜩ0 From: [CONTRACT_HASH] @@ -29,7 +29,7 @@ This sequence of operations was run: Updated storage: [OPERATION_HASH]48f709699019725ba Storage size: 578 bytes - Consumed gas: 2764.720 + Consumed gas: 2544.704 Internal operations: Transaction: Amount: ꜩ1000 diff --git a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[0.5].out b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[0.5].out index 49508b6ba64acd2ed79f82a33ffa6027290f31f6..ddd26b5b18aa835ff98f2bf2a77303583a21dbaf 100644 --- a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[0.5].out +++ b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[0.5].out @@ -11,11 +11,11 @@ trace [ (Pair Unit 0) ] - location: 7 (remaining gas: 1039994.938 units remaining) [ ] - - location: 8 (remaining gas: 1039774.922 units remaining) + - location: 8 (remaining gas: 1039994.938 units remaining) [ 500000 ] - - location: 9 (remaining gas: 1039774.907 units remaining) + - location: 9 (remaining gas: 1039994.923 units remaining) [ {} 500000 ] - - location: 11 (remaining gas: 1039774.892 units remaining) + - location: 11 (remaining gas: 1039994.908 units remaining) [ (Pair {} 500000) ] diff --git a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[0].out b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[0].out index 5e66595481d6ab2177dbf9d611e5cbcffa5e104c..c7f286bb44c4d33274d3288fdd84707d34bae336 100644 --- a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[0].out +++ b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[0].out @@ -11,11 +11,11 @@ trace [ (Pair Unit 0) ] - location: 7 (remaining gas: 1039994.938 units remaining) [ ] - - location: 8 (remaining gas: 1039774.922 units remaining) + - location: 8 (remaining gas: 1039994.938 units remaining) [ 0 ] - - location: 9 (remaining gas: 1039774.907 units remaining) + - location: 9 (remaining gas: 1039994.923 units remaining) [ {} 0 ] - - location: 11 (remaining gas: 1039774.892 units remaining) + - location: 11 (remaining gas: 1039994.908 units remaining) [ (Pair {} 0) ] diff --git a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[1000].out b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[1000].out index a579be096adce79080c2a552e5d98305d8a07c1b..a1ed7938b6e1a6537469d1a2b75fc486f814e4b6 100644 --- a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[1000].out +++ b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[1000].out @@ -11,11 +11,11 @@ trace [ (Pair Unit 0) ] - location: 7 (remaining gas: 1039994.938 units remaining) [ ] - - location: 8 (remaining gas: 1039774.922 units remaining) + - location: 8 (remaining gas: 1039994.938 units remaining) [ 1000000000 ] - - location: 9 (remaining gas: 1039774.907 units remaining) + - location: 9 (remaining gas: 1039994.923 units remaining) [ {} 1000000000 ] - - location: 11 (remaining gas: 1039774.892 units remaining) + - location: 11 (remaining gas: 1039994.908 units remaining) [ (Pair {} 1000000000) ] diff --git a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[1].out b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[1].out index bf19a152b9eb1de5f1faf4db5cafbdeb371c6f5c..e27ed8107fed2e14be09d802598f700a37fbde11 100644 --- a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[1].out +++ b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[1].out @@ -11,11 +11,11 @@ trace [ (Pair Unit 0) ] - location: 7 (remaining gas: 1039994.938 units remaining) [ ] - - location: 8 (remaining gas: 1039774.922 units remaining) + - location: 8 (remaining gas: 1039994.938 units remaining) [ 1000000 ] - - location: 9 (remaining gas: 1039774.907 units remaining) + - location: 9 (remaining gas: 1039994.923 units remaining) [ {} 1000000 ] - - location: 11 (remaining gas: 1039774.892 units remaining) + - location: 11 (remaining gas: 1039994.908 units remaining) [ (Pair {} 1000000) ] diff --git a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[1e-06].out b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[1e-06].out index 9ac426c714a7bc90bb456fb69cc0e29cb2201561..f7e51d2d2d9c045f8fd83d978850ffc405ba6b91 100644 --- a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[1e-06].out +++ b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[1e-06].out @@ -11,11 +11,11 @@ trace [ (Pair Unit 0) ] - location: 7 (remaining gas: 1039994.938 units remaining) [ ] - - location: 8 (remaining gas: 1039774.922 units remaining) + - location: 8 (remaining gas: 1039994.938 units remaining) [ 1 ] - - location: 9 (remaining gas: 1039774.907 units remaining) + - location: 9 (remaining gas: 1039994.923 units remaining) [ {} 1 ] - - location: 11 (remaining gas: 1039774.892 units remaining) + - location: 11 (remaining gas: 1039994.908 units remaining) [ (Pair {} 1) ] diff --git a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[5].out b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[5].out index ce8cb0601025378533884e42c3b7d1a7dd413bb2..ee4e1500051e964e7181055b6c0c01338154241c 100644 --- a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[5].out +++ b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[5].out @@ -11,11 +11,11 @@ trace [ (Pair Unit 0) ] - location: 7 (remaining gas: 1039994.938 units remaining) [ ] - - location: 8 (remaining gas: 1039774.922 units remaining) + - location: 8 (remaining gas: 1039994.938 units remaining) [ 5000000 ] - - location: 9 (remaining gas: 1039774.907 units remaining) + - location: 9 (remaining gas: 1039994.923 units remaining) [ {} 5000000 ] - - location: 11 (remaining gas: 1039774.892 units remaining) + - location: 11 (remaining gas: 1039994.908 units remaining) [ (Pair {} 5000000) ] diff --git a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[8000000000000.0].out b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[8000000000000.0].out index 47b7efaaaf6e16474f584d11a432507edbdba64f..fb69d27649c8547c6860203e7ba94de68dab7315 100644 --- a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[8000000000000.0].out +++ b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_balance[8000000000000.0].out @@ -11,11 +11,11 @@ trace [ (Pair Unit 0) ] - location: 7 (remaining gas: 1039994.938 units remaining) [ ] - - location: 8 (remaining gas: 1039774.922 units remaining) + - location: 8 (remaining gas: 1039994.938 units remaining) [ 8000000000000000000 ] - - location: 9 (remaining gas: 1039774.907 units remaining) + - location: 9 (remaining gas: 1039994.923 units remaining) [ {} 8000000000000000000 ] - - location: 11 (remaining gas: 1039774.892 units remaining) + - location: 11 (remaining gas: 1039994.908 units remaining) [ (Pair {} 8000000000000000000) ] diff --git a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_contract_input_output[balance.tz-111-Unit-4000000000000].out b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_contract_input_output[balance.tz-111-Unit-4000000000000].out index 78cc5b82534cd6c9dbdc7eee8ebfd367f07b3737..8bc1c350c188ae191061f6665a4612abce1e23e2 100644 --- a/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_contract_input_output[balance.tz-111-Unit-4000000000000].out +++ b/tests_python/tests_alpha/_regtest_outputs/test_contract_opcodes.TestContractOpcodes::test_contract_input_output[balance.tz-111-Unit-4000000000000].out @@ -11,11 +11,11 @@ trace [ (Pair Unit 111) ] - location: 7 (remaining gas: 1039994.938 units remaining) [ ] - - location: 8 (remaining gas: 1039774.922 units remaining) + - location: 8 (remaining gas: 1039994.938 units remaining) [ 4000000000000 ] - - location: 9 (remaining gas: 1039774.907 units remaining) + - location: 9 (remaining gas: 1039994.923 units remaining) [ {} 4000000000000 ] - - location: 11 (remaining gas: 1039774.892 units remaining) + - location: 11 (remaining gas: 1039994.908 units remaining) [ (Pair {} 4000000000000) ] diff --git a/tezt/_regressions/run_views.out b/tezt/_regressions/run_views.out new file mode 100644 index 0000000000000000000000000000000000000000..2f1c0b721fe68c22fc2b9d34f9dfed121b5c6eef --- /dev/null +++ b/tezt/_regressions/run_views.out @@ -0,0 +1,252 @@ +tezt/_regressions/run_views.out + +./tezos-client --mode mockup --base-dir '' --wait none originate contract register_calls transferring 0 from bootstrap1 running ' +parameter unit; +storage (list address); +code { + CDR ; + SOURCE ; + CONS ; + NIL operation ; + PAIR ; + }; +view "calls_count" unit nat { CDR ; SIZE }; +view "last_caller" unit (option address) { CDR ; IF_CONS { DIP { DROP } ; SOME } { NONE address } }; +' --init '{}' --burn-cap 1 +Node is bootstrapped. +Estimated gas: 1417.213 units (will add 100 for safety) +Estimated storage: 409 bytes added (will add 20 for safety) +Operation successfully injected in the node. +Operation hash is 'oo5VNxswL6HtEs7vaoTekiDz9QE5ZXW2DV6H4h5DnCuEtyUfhmt' +NOT waiting for the operation to be included. +Use command + tezos-client wait for oo5VNxswL6HtEs7vaoTekiDz9QE5ZXW2DV6H4h5DnCuEtyUfhmt to be included --confirmations 1 --branch BLockGenesisGenesisGenesisGenesisGenesisCCCCCeZiLHU +and/or an external block explorer to make sure that it has been included. +This sequence of operations was run: + Manager signed operations: + From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + Fee to the baker: ꜩ0.000531 + Expected counter: 1 + Gas limit: 1518 + Storage limit: 429 bytes + Balance updates: + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.000531 + payload fees(the block proposer) ....... +ꜩ0.000531 + Origination: + From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + Credit: ꜩ0 + Script: + { parameter unit ; + storage (list address) ; + code { CDR ; SOURCE ; CONS ; NIL operation ; PAIR } ; + view "calls_count" unit nat { CDR ; SIZE } ; + view "last_caller" + unit + (option address) + { CDR ; IF_CONS { DIP { DROP } ; SOME } { NONE address } } } + Initial storage: {} + No delegate for this contract + This origination was successfully applied + Originated contracts: + KT19P9vLfrXCEwUMpiURQ5vY8Ncp5idkyKTD + Storage size: 152 bytes + Paid storage size diff: 152 bytes + Consumed gas: 1417.213 + Balance updates: + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.038 + storage fees ........................... +ꜩ0.038 + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.06425 + storage fees ........................... +ꜩ0.06425 + +New contract KT19P9vLfrXCEwUMpiURQ5vY8Ncp5idkyKTD originated. +Contract memorized as register_calls. + +./tezos-client --mode mockup --base-dir '' --wait none originate contract check_caller transferring 0 from bootstrap1 running ' +parameter address ; +storage (option address) ; +code { + CAR ; + DUP ; + UNIT ; + VIEW "calls_count" nat ; + IF_NONE { UNIT ; FAILWITH } {} ; + DIP { + UNIT ; + VIEW "last_caller" (option address) ; + } ; + PUSH nat 0 ; + /* Check if the caller address is consistent with given calls count. */ + IFCMPEQ { + IF_NONE { UNIT ; FAILWITH } { IF_NONE {} { UNIT ; FAILWITH }} ; + NONE address ; + } + { + IF_NONE { UNIT ; FAILWITH } { IF_NONE { UNIT ; FAILWITH } {}} ; + SOME ; + } ; + NIL operation ; + PAIR ; + } + ' --init None --burn-cap 1 +Node is bootstrapped. +Estimated gas: 1432.729 units (will add 100 for safety) +Estimated storage: 465 bytes added (will add 20 for safety) +Operation successfully injected in the node. +Operation hash is 'ooYj2QbiRiwv8K5dGfGVn6RhN29WLZsUBSkPHpTWJ68iZhCxBcw' +NOT waiting for the operation to be included. +Use command + tezos-client wait for ooYj2QbiRiwv8K5dGfGVn6RhN29WLZsUBSkPHpTWJ68iZhCxBcw to be included --confirmations 1 --branch BLockGenesisGenesisGenesisGenesisGenesisCCCCCeZiLHU +and/or an external block explorer to make sure that it has been included. +This sequence of operations was run: + Manager signed operations: + From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + Fee to the baker: ꜩ0.000589 + Expected counter: 2 + Gas limit: 1533 + Storage limit: 485 bytes + Balance updates: + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.000589 + payload fees(the block proposer) ....... +ꜩ0.000589 + Origination: + From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + Credit: ꜩ0 + Script: + { parameter address ; + storage (option address) ; + code { CAR ; + DUP ; + UNIT ; + VIEW "calls_count" nat ; + IF_NONE { UNIT ; FAILWITH } {} ; + DIP { UNIT ; VIEW "last_caller" (option address) } ; + PUSH nat 0 ; + IFCMPEQ + { IF_NONE { UNIT ; FAILWITH } { IF_NONE {} { UNIT ; FAILWITH } } ; + NONE address } + { IF_NONE { UNIT ; FAILWITH } { IF_NONE { UNIT ; FAILWITH } {} } ; + SOME } ; + NIL operation ; + PAIR } } + Initial storage: None + No delegate for this contract + This origination was successfully applied + Originated contracts: + KT1EYwqeAzgkynYXJnHJ7ZxuXk5bNBBy6R6L + Storage size: 208 bytes + Paid storage size diff: 208 bytes + Consumed gas: 1432.729 + Balance updates: + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.052 + storage fees ........................... +ꜩ0.052 + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.06425 + storage fees ........................... +ꜩ0.06425 + +New contract KT1EYwqeAzgkynYXJnHJ7ZxuXk5bNBBy6R6L originated. +Contract memorized as check_caller. + +./tezos-client --mode mockup --base-dir '' --wait none transfer 1 from bootstrap1 to KT1EYwqeAzgkynYXJnHJ7ZxuXk5bNBBy6R6L --burn-cap 1 --arg '"KT19P9vLfrXCEwUMpiURQ5vY8Ncp5idkyKTD"' +Node is bootstrapped. +Estimated gas: 4888.980 units (will add 100 for safety) +Estimated storage: no bytes added +Operation successfully injected in the node. +Operation hash is 'op4wu7aQT9ofgup46yvygx2td15DfJ2o8CLgamEkaZkRnqyPMvL' +NOT waiting for the operation to be included. +Use command + tezos-client wait for op4wu7aQT9ofgup46yvygx2td15DfJ2o8CLgamEkaZkRnqyPMvL to be included --confirmations 1 --branch BLockGenesisGenesisGenesisGenesisGenesisCCCCCeZiLHU +and/or an external block explorer to make sure that it has been included. +This sequence of operations was run: + Manager signed operations: + From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + Fee to the baker: ꜩ0.000795 + Expected counter: 3 + Gas limit: 4989 + Storage limit: 0 bytes + Balance updates: + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.000795 + payload fees(the block proposer) ....... +ꜩ0.000795 + Transaction: + Amount: ꜩ1 + From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + To: KT1EYwqeAzgkynYXJnHJ7ZxuXk5bNBBy6R6L + Parameter: "KT19P9vLfrXCEwUMpiURQ5vY8Ncp5idkyKTD" + This transaction was successfully applied + Updated storage: None + Storage size: 208 bytes + Consumed gas: 4889.778 + Balance updates: + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ1 + KT1EYwqeAzgkynYXJnHJ7ZxuXk5bNBBy6R6L ... +ꜩ1 + + +./tezos-client --mode mockup --base-dir '' --wait none transfer 1 from bootstrap1 to KT19P9vLfrXCEwUMpiURQ5vY8Ncp5idkyKTD --burn-cap 1 +Node is bootstrapped. +Estimated gas: 2067.348 units (will add 100 for safety) +Estimated storage: 27 bytes added (will add 20 for safety) +Operation successfully injected in the node. +Operation hash is 'ooqdCJ1FcbQQtnEg3bJA2CWxvv1To8aVBGgnBT7nYpU7zFM33pW' +NOT waiting for the operation to be included. +Use command + tezos-client wait for ooqdCJ1FcbQQtnEg3bJA2CWxvv1To8aVBGgnBT7nYpU7zFM33pW to be included --confirmations 1 --branch BLockGenesisGenesisGenesisGenesisGenesisCCCCCeZiLHU +and/or an external block explorer to make sure that it has been included. +This sequence of operations was run: + Manager signed operations: + From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + Fee to the baker: ꜩ0.000467 + Expected counter: 4 + Gas limit: 2168 + Storage limit: 47 bytes + Balance updates: + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.000467 + payload fees(the block proposer) ....... +ꜩ0.000467 + Transaction: + Amount: ꜩ1 + From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + To: KT19P9vLfrXCEwUMpiURQ5vY8Ncp5idkyKTD + This transaction was successfully applied + Updated storage: { 0x000002298c03ed7d454a101eb7022bc95f7e5f41ac78 } + Storage size: 179 bytes + Paid storage size diff: 27 bytes + Consumed gas: 2068.260 + Balance updates: + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.00675 + storage fees ........................... +ꜩ0.00675 + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ1 + KT19P9vLfrXCEwUMpiURQ5vY8Ncp5idkyKTD ... +ꜩ1 + + +./tezos-client --mode mockup --base-dir '' --wait none transfer 1 from bootstrap1 to KT1EYwqeAzgkynYXJnHJ7ZxuXk5bNBBy6R6L --burn-cap 1 --arg '"KT19P9vLfrXCEwUMpiURQ5vY8Ncp5idkyKTD"' +Node is bootstrapped. +Estimated gas: 6201.987 units (will add 100 for safety) +Estimated storage: 27 bytes added (will add 20 for safety) +Operation successfully injected in the node. +Operation hash is 'oo6wNGAK8aSyy6XkpBd1Eij4DvdV6te4V91uYzSdtZCPcRNPnAG' +NOT waiting for the operation to be included. +Use command + tezos-client wait for oo6wNGAK8aSyy6XkpBd1Eij4DvdV6te4V91uYzSdtZCPcRNPnAG to be included --confirmations 1 --branch BLockGenesisGenesisGenesisGenesisGenesisCCCCCeZiLHU +and/or an external block explorer to make sure that it has been included. +This sequence of operations was run: + Manager signed operations: + From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + Fee to the baker: ꜩ0.000927 + Expected counter: 5 + Gas limit: 6302 + Storage limit: 47 bytes + Balance updates: + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.000927 + payload fees(the block proposer) ....... +ꜩ0.000927 + Transaction: + Amount: ꜩ1 + From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + To: KT1EYwqeAzgkynYXJnHJ7ZxuXk5bNBBy6R6L + Parameter: "KT19P9vLfrXCEwUMpiURQ5vY8Ncp5idkyKTD" + This transaction was successfully applied + Updated storage: (Some 0x000002298c03ed7d454a101eb7022bc95f7e5f41ac78) + Storage size: 235 bytes + Paid storage size diff: 27 bytes + Consumed gas: 6202.785 + Balance updates: + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.00675 + storage fees ........................... +ꜩ0.00675 + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ1 + KT1EYwqeAzgkynYXJnHJ7ZxuXk5bNBBy6R6L ... +ꜩ1 + diff --git a/tezt/lib_tezos/client.ml b/tezt/lib_tezos/client.ml index 73a39678f9b58020915a290ea11046c5db0bc795..b90b0a930e2259194b3ec7d715294a6c9c4ee515 100644 --- a/tezt/lib_tezos/client.ml +++ b/tezt/lib_tezos/client.ml @@ -567,10 +567,11 @@ let gen_and_show_keys ?alias client = let* alias = gen_keys ?alias client in show_address ~alias client -let spawn_transfer ?endpoint ?(wait = "none") ?burn_cap ?fee ?gas_limit +let spawn_transfer ?hooks ?endpoint ?(wait = "none") ?burn_cap ?fee ?gas_limit ?storage_limit ?counter ?arg ~amount ~giver ~receiver client = spawn_command ?endpoint + ?hooks client (["--wait"; wait] @ ["transfer"; Tez.to_string amount; "from"; giver; "to"; receiver] @@ -596,10 +597,11 @@ let spawn_transfer ?endpoint ?(wait = "none") ?burn_cap ?fee ?gas_limit counter @ Option.fold ~none:[] ~some:(fun p -> ["--arg"; p]) arg) -let transfer ?endpoint ?wait ?burn_cap ?fee ?gas_limit ?storage_limit ?counter - ?arg ~amount ~giver ~receiver client = +let transfer ?hooks ?endpoint ?wait ?burn_cap ?fee ?gas_limit ?storage_limit + ?counter ?arg ~amount ~giver ~receiver client = spawn_transfer ?endpoint + ?hooks ?wait ?burn_cap ?fee @@ -743,9 +745,10 @@ let spawn_submit_ballot ?(key = Constant.bootstrap1.alias) ?(wait = "none") let submit_ballot ?key ?wait ~proto_hash vote client = spawn_submit_ballot ?key ?wait ~proto_hash vote client |> Process.check -let spawn_originate_contract ?endpoint ?(wait = "none") ?init ?burn_cap ~alias - ~amount ~src ~prg client = +let spawn_originate_contract ?hooks ?endpoint ?(wait = "none") ?init ?burn_cap + ~alias ~amount ~src ~prg client = spawn_command + ?hooks ?endpoint client (["--wait"; wait] @@ -782,11 +785,12 @@ let convert_script_to_json ?endpoint ~script client = let convert_data_to_json ?endpoint ~data client = convert_michelson_to_json ~kind:"data" ?endpoint ~input:data client -let originate_contract ?endpoint ?wait ?init ?burn_cap ~alias ~amount ~src ~prg - client = +let originate_contract ?hooks ?endpoint ?wait ?init ?burn_cap ~alias ~amount + ~src ~prg client = let* client_output = spawn_originate_contract ?endpoint + ?hooks ?wait ?init ?burn_cap @@ -885,14 +889,15 @@ let stresstest ?endpoint ?source_aliases ?source_pkhs ?source_accounts ?seed client |> Process.check -let spawn_run_script ~src ~storage ~input client = +let spawn_run_script ?hooks ~src ~storage ~input client = spawn_command + ?hooks client ["run"; "script"; src; "on"; "storage"; storage; "and"; "input"; input] -let run_script ~src ~storage ~input client = +let run_script ?hooks ~src ~storage ~input client = let* client_output = - spawn_run_script ~src ~storage ~input client + spawn_run_script ?hooks ~src ~storage ~input client |> Process.check_and_read_stdout in match client_output =~* rex "storage\n(.*)" with diff --git a/tezt/lib_tezos/client.mli b/tezt/lib_tezos/client.mli index b86a8e5015894ef261f385dd121b7c817e02723e..a96e74c045f9a6bc4b6630df567c69e720a20314 100644 --- a/tezt/lib_tezos/client.mli +++ b/tezt/lib_tezos/client.mli @@ -413,6 +413,7 @@ val gen_and_show_keys : ?alias:string -> t -> Account.key Lwt.t (** Run [tezos-client transfer amount from giver to receiver]. *) val transfer : + ?hooks:Process.hooks -> ?endpoint:endpoint -> ?wait:string -> ?burn_cap:Tez.t -> @@ -429,6 +430,7 @@ val transfer : (** Same as [transfer], but do not wait for the process to exit. *) val spawn_transfer : + ?hooks:Process.hooks -> ?endpoint:endpoint -> ?wait:string -> ?burn_cap:Tez.t -> @@ -563,6 +565,7 @@ val spawn_submit_ballot : (** Run [tezos-client originate contract alias transferring amount from src running prg]. Returns the originated contract hash *) val originate_contract : + ?hooks:Process.hooks -> ?endpoint:endpoint -> ?wait:string -> ?init:string -> @@ -576,6 +579,7 @@ val originate_contract : (** Same as [originate_contract], but do not wait for the process to exit. *) val spawn_originate_contract : + ?hooks:Process.hooks -> ?endpoint:endpoint -> ?wait:string -> ?init:string -> @@ -653,11 +657,21 @@ val spawn_stresstest : Fails if the new storage cannot be extracted from the output. *) val run_script : - src:string -> storage:string -> input:string -> t -> string Lwt.t + ?hooks:Process.hooks -> + src:string -> + storage:string -> + input:string -> + t -> + string Lwt.t (** Same as [run_script] but do not wait for the process to exit. *) val spawn_run_script : - src:string -> storage:string -> input:string -> t -> Process.t + ?hooks:Process.hooks -> + src:string -> + storage:string -> + input:string -> + t -> + Process.t (** Run [tezos-client register global constant value from src]. Returns the address hash of the new constant. *) diff --git a/tezt/tests/main.ml b/tezt/tests/main.ml index 7e706555d8f48e506583f9f06029bdaf34abd07f..792fd66ccb455fec24ca2b2a65668e003ebf3997 100644 --- a/tezt/tests/main.ml +++ b/tezt/tests/main.ml @@ -108,5 +108,6 @@ let () = Manager_operations.register ~protocols ; Replace_by_fees.register ~protocols:[Alpha] ; Sc_rollup.register ~protocols:[Alpha] ; + Views.register ~protocols:[Alpha] () ; (* Test.run () should be the last statement, don't register afterwards! *) Test.run () diff --git a/tezt/tests/views.ml b/tezt/tests/views.ml new file mode 100644 index 0000000000000000000000000000000000000000..96ff3770cee10cd96d8f874437c457995a736ca6 --- /dev/null +++ b/tezt/tests/views.ml @@ -0,0 +1,158 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2021 Nomadic Labs *) +(* *) +(* 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. *) +(* *) +(*****************************************************************************) + +(* Testing + ------- + Component: Michelson + Invocation: dune exec tezt/tests/main.exe -- --file views.ml + Subject: Call smart contract views to catch performance regressions. + *) + +(* This contract registers all SOURCE addresses that ever call it. It has views + that return registered callers count and the last caller address respectively. *) +let register_callers_src = + {| +parameter unit; +storage (list address); +code { + CDR ; + SOURCE ; + CONS ; + NIL operation ; + PAIR ; + }; +view "calls_count" unit nat { CDR ; SIZE }; +view "last_caller" unit (option address) { CDR ; IF_CONS { DIP { DROP } ; SOME } { NONE address } }; +|} + +(* This script calls views on register_callers contract and verifies whether + its responses are consistent, i.e. if the view calls_count returned 0, then + last caller is None, otherwise – it's Some address. *) +let check_caller_src = + {| +parameter address ; +storage (option address) ; +code { + CAR ; + DUP ; + UNIT ; + VIEW "calls_count" nat ; + IF_NONE { UNIT ; FAILWITH } {} ; + DIP { + UNIT ; + VIEW "last_caller" (option address) ; + } ; + PUSH nat 0 ; + /* Check if the caller address is consistent with given calls count. */ + IFCMPEQ { + IF_NONE { UNIT ; FAILWITH } { IF_NONE {} { UNIT ; FAILWITH }} ; + NONE address ; + } + { + IF_NONE { UNIT ; FAILWITH } { IF_NONE { UNIT ; FAILWITH } {}} ; + SOME ; + } ; + NIL operation ; + PAIR ; + } + |} + +(* Normally "--base-dir" would appear in regression logs. However, since + it is a different dir on every run, we need to mask it in regression + logs so that it doesn't cause false differences. *) +let hooks = + let rec mask_temp_dir = function + | [] -> [] + | "--base-dir" :: _ :: rest -> "--base-dir" :: "" :: rest + | arg :: args -> arg :: mask_temp_dir args + in + { + Regression.hooks with + on_spawn = + (fun cmd args -> mask_temp_dir args |> Regression.hooks.on_spawn cmd); + } + +let register ~protocols () = + Protocol.register_regression_test + ~__FILE__ + ~title:"Run views" + ~tags:["client"; "michelson"] + ~output_file:"run_views" + ~protocols + @@ fun protocol -> + let* client = Client.init_mockup ~protocol () in + let* register_callers = + Client.originate_contract + ~hooks + ~burn_cap:Tez.one + ~alias:"register_calls" + ~amount:Tez.zero + ~src:"bootstrap1" + ~prg:register_callers_src + ~init:"{}" + client + in + let arg = Format.sprintf "\"%s\"" register_callers in + let* check_caller = + Client.originate_contract + ~hooks + ~burn_cap:Tez.one + ~alias:"check_caller" + ~amount:Tez.zero + ~src:"bootstrap1" + ~prg:check_caller_src + ~init:"None" + client + in + let* () = + Client.transfer + ~hooks + ~burn_cap:Tez.one + ~amount:Tez.one + ~giver:"bootstrap1" + ~receiver:check_caller + ~arg + client + in + let* () = + Client.transfer + ~hooks + ~burn_cap:Tez.one + ~amount:Tez.one + ~giver:"bootstrap1" + ~receiver:register_callers + client + in + let* () = + Client.transfer + ~hooks + ~burn_cap:Tez.one + ~amount:Tez.one + ~giver:"bootstrap1" + ~receiver:check_caller + ~arg + client + in + return ()