From 3795a6e9ff84a1c0bcb9dda33846d1443641962d Mon Sep 17 00:00:00 2001 From: lin Date: Tue, 4 Oct 2022 14:28:00 +0900 Subject: [PATCH 1/5] Client: Add ticket_balance functions Will use these new functions to create client commands --- src/proto_alpha/lib_client/client_proto_context.ml | 4 ++++ src/proto_alpha/lib_client/client_proto_context.mli | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/proto_alpha/lib_client/client_proto_context.ml b/src/proto_alpha/lib_client/client_proto_context.ml index fcdcb7aaba9f..71713c022991 100644 --- a/src/proto_alpha/lib_client/client_proto_context.ml +++ b/src/proto_alpha/lib_client/client_proto_context.ml @@ -85,6 +85,10 @@ let get_script_hash (rpc : #rpc_context) ~chain ~block contract = ok hash) script_opt +let get_contract_ticket_balance (rpc : #rpc_context) ~chain ~block contract key + = + Plugin.RPC.Contract.ticket_balance rpc (chain, block) contract key + let get_frozen_deposits_limit (rpc : #rpc_context) ~chain ~block delegate = Alpha_services.Delegate.frozen_deposits_limit rpc (chain, block) delegate diff --git a/src/proto_alpha/lib_client/client_proto_context.mli b/src/proto_alpha/lib_client/client_proto_context.mli index cf0a45469f83..e25b0a0aa97a 100644 --- a/src/proto_alpha/lib_client/client_proto_context.mli +++ b/src/proto_alpha/lib_client/client_proto_context.mli @@ -126,6 +126,15 @@ val get_balance : Contract.t -> Tez.t tzresult Lwt.t +(** Calls {!Tezos_protocol_alpha.Protocol.Ticket_services.ticket_balance}. *) +val get_contract_ticket_balance : + #Protocol_client_context.rpc_context -> + chain:Shell_services.chain -> + block:Shell_services.block -> + Contract.t -> + Ticket_token.unparsed_token -> + Z.t tzresult Lwt.t + (** Calls {!Tezos_protocol_alpha.Protocol.Delegate_services.frozen_deposits_limit}. *) val get_frozen_deposits_limit : #Protocol_client_context.rpc_context -> -- GitLab From 8c270e04004da56db8704cf8f2ea2db1b40daada Mon Sep 17 00:00:00 2001 From: lin Date: Tue, 4 Oct 2022 16:28:39 +0900 Subject: [PATCH 2/5] Client: Add ticket_balance client command --- .../client_proto_context_commands.ml | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) 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 56eed796ce9f..faf6eab6d320 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 @@ -515,6 +515,46 @@ let commands_ro () = (match mn with None -> "unknown" | Some n -> "known as " ^ n) in return_unit); + command + ~group + ~desc: + "Get contract's balance of ticket with specified ticketer, content \ + type, and content." + no_options + (prefixes ["get"; "ticket"; "balance"; "for"] + @@ ContractAlias.destination_param ~name:"src" ~desc:"Source contract." + @@ prefixes ["with"; "ticketer"] + @@ ContractAlias.destination_param + ~name:"ticketer" + ~desc:"Ticketer contract of the ticket." + @@ prefixes ["and"; "type"] + @@ Tezos_clic.param + ~name:"ticket content type" + ~desc:"Type of the content of the ticket." + data_parameter + @@ prefixes ["and"; "content"] + @@ Tezos_clic.param + ~name:"ticket content" + ~desc:"Content of the ticket." + data_parameter + @@ stop) + (fun () contract ticketer content_type content cctxt -> + let open Lwt_result_syntax in + let* balance = + get_contract_ticket_balance + cctxt + ~chain:cctxt#chain + ~block:cctxt#block + contract + Ticket_token. + { + ticketer; + contents_type = content_type.expanded; + contents = content.expanded; + } + in + let*! () = cctxt#answer "%a" Z.pp_print balance in + return_unit); command ~desc:"Get receipt for past operation" (args1 -- GitLab From 9cbf25d5bfa2383e6ae3aee4dd7a5cee18204b79 Mon Sep 17 00:00:00 2001 From: lin Date: Tue, 4 Oct 2022 19:11:24 +0900 Subject: [PATCH 3/5] Tezt/Client: Add ticket_balance commands --- tezt/lib_tezos/client.ml | 26 ++++++++++++++++++++++++++ tezt/lib_tezos/client.mli | 24 ++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/tezt/lib_tezos/client.ml b/tezt/lib_tezos/client.ml index 313296f8f421..aed3c3a7f053 100644 --- a/tezt/lib_tezos/client.ml +++ b/tezt/lib_tezos/client.ml @@ -973,6 +973,32 @@ let get_balance_for ?endpoint ~account client = in return @@ Tez.parse_floating output +let spawn_ticket_balance ?hooks ~contract ~ticketer ~content_type ~content + client = + spawn_command + ?hooks + client + [ + "get"; + "ticket"; + "balance"; + "for"; + contract; + "with"; + "ticketer"; + ticketer; + "and"; + "type"; + content_type; + "and"; + "content"; + content; + ] + +let ticket_balance ?hooks ~contract ~ticketer ~content_type ~content client = + spawn_ticket_balance ?hooks ~contract ~ticketer ~content_type ~content client + |> Process.check_and_read_stdout + let spawn_create_mockup ?(sync_mode = Synchronous) ?parameter_file ?bootstrap_accounts_file ~protocol client = let cmd = diff --git a/tezt/lib_tezos/client.mli b/tezt/lib_tezos/client.mli index c1cdb3a87231..ae16f99f73ec 100644 --- a/tezt/lib_tezos/client.mli +++ b/tezt/lib_tezos/client.mli @@ -757,6 +757,26 @@ val get_balance_for : ?endpoint:endpoint -> account:string -> t -> Tez.t Lwt.t val spawn_get_balance_for : ?endpoint:endpoint -> account:string -> t -> Process.t +(** Run [octez-client get ticket balance for contract with ticketer and type and content ]. *) +val ticket_balance : + ?hooks:Process.hooks -> + contract:string -> + ticketer:string -> + content_type:string -> + content:string -> + t -> + string Lwt.t + +(** Same as [ticket_balance], but do not wait for the process to exit. *) +val spawn_ticket_balance : + ?hooks:Process.hooks -> + contract:string -> + ticketer:string -> + content_type:string -> + content:string -> + t -> + Process.t + (** Run [octez-client create mockup]. *) val create_mockup : ?sync_mode:mockup_sync_mode -> @@ -1207,8 +1227,8 @@ val spawn_run_tzip4_view : t -> Process.t -(** Run [tezos-client run tzip4 view .. on contract .. with input .. ] - +(** Run [tezos-client run tzip4 view .. on contract .. with input .. ] + Returns the value returned by a view as a string. Fails if the view or the contract does not exist. If [input] is [None], -- GitLab From d74635eaded0778a394eef877816a1181e51b8ce Mon Sep 17 00:00:00 2001 From: lin Date: Wed, 5 Oct 2022 00:38:56 +0900 Subject: [PATCH 4/5] Tezt/Client: Test CLI for ticket_balance --- .../Alpha- Ticket updates in receipt.out | 15 +++ tezt/tests/ticket_receipt_and_rpc.ml | 99 ++++++++++--------- 2 files changed, 65 insertions(+), 49 deletions(-) diff --git a/tezt/tests/expected/ticket_receipt_and_rpc.ml/Alpha- Ticket updates in receipt.out b/tezt/tests/expected/ticket_receipt_and_rpc.ml/Alpha- Ticket updates in receipt.out index 9891a1ba0246..25ecb11eb542 100644 --- a/tezt/tests/expected/ticket_receipt_and_rpc.ml/Alpha- Ticket updates in receipt.out +++ b/tezt/tests/expected/ticket_receipt_and_rpc.ml/Alpha- Ticket updates in receipt.out @@ -90,3 +90,18 @@ This sequence of operations was run: Account updates: [CONTRACT_HASH] ... +1 + +./octez-client --mode mockup get ticket balance for '[CONTRACT_HASH]' with ticketer '[CONTRACT_HASH]' and type string and content '"red"' +1 + +./octez-client --mode mockup get ticket balance for '[CONTRACT_HASH]' with ticketer '[CONTRACT_HASH]' and type string and content '"green"' +2 + +./octez-client --mode mockup get ticket balance for '[CONTRACT_HASH]' with ticketer '[CONTRACT_HASH]' and type string and content '"blue"' +0 + +./octez-client --mode mockup get ticket balance for '[CONTRACT_HASH]' with ticketer '[CONTRACT_HASH]' and type string and content '"blue"' +1 + +./octez-client --mode mockup get ticket balance for '[CONTRACT_HASH]' with ticketer '[CONTRACT_HASH]' and type string and content '"blue"' +1 diff --git a/tezt/tests/ticket_receipt_and_rpc.ml b/tezt/tests/ticket_receipt_and_rpc.ml index 492acdf18997..1ab2bd7efc5d 100644 --- a/tezt/tests/ticket_receipt_and_rpc.ml +++ b/tezt/tests/ticket_receipt_and_rpc.ml @@ -27,12 +27,12 @@ ------- Component: Michelson Invocation: dune exec tezt/tests/main.exe -- --file ticket_receipt_and_rpc.ml - Subject: Regression tests for ticket receipt and ticket RPC + Subject: Regression tests for ticket receipt/RPC/CLI *) let hooks = Tezos_regression.hooks -let check_ticket_balance client ~contract ~ticketer ~content_type ~content +let rpc_check_ticket_balance client ~contract ~ticketer ~content_type ~content ~expected = let post_body = Ezjsonm.value_from_string @@ -56,6 +56,24 @@ let check_ticket_balance client ~contract ~ticketer ~content_type ~content ~__LOC__ ~error_msg:"expected ticket amount %R, got %L") +let cli_check_ticket_balance client ~hooks ~contract ~ticketer ~content_type + ~content ~expected = + let* actual = + Client.ticket_balance + client + ~hooks + ~contract + ~ticketer + ~content_type + ~content:(sf {|"%s"|} content) + in + return + @@ Check.( + (String.trim actual = string_of_int expected) + string + ~__LOC__ + ~error_msg:"expected ticket amount %R, got %L") + (* Checks how the ticket updates in receipt look like in various cases, such as: - Contract stores multiple tickets of different types. - Contract stores multiple tickets of the same type. @@ -124,52 +142,35 @@ let test_ticket_receipt_and_rpc = ~hooks client in - (* Check that the ticket balance are expected via [ticket_balance] RPC. *) - let* () = - check_ticket_balance - client - ~contract:kt_a - ~ticketer:kt_a - ~content_type:"string" - ~content:"red" - ~expected:1 - in - let* () = - check_ticket_balance - client - ~contract:kt_a - ~ticketer:kt_a - ~content_type:"string" - ~content:"green" - ~expected:2 - in - let* () = - check_ticket_balance - client - ~contract:kt_a - ~ticketer:kt_a - ~content_type:"string" - ~content:"blue" - ~expected:0 - in - let* () = - check_ticket_balance - client - ~contract:kt_b - ~ticketer:kt_a - ~content_type:"string" - ~content:"blue" - ~expected:1 - in - let* () = - check_ticket_balance - client - ~contract:kt_c - ~ticketer:kt_a - ~content_type:"string" - ~content:"blue" - ~expected:1 - in - unit + (* Check that the ticket balance are expected via both RPC and CLI. *) + [ + (kt_a, kt_a, "string", "red", 1); + (kt_a, kt_a, "string", "green", 2); + (kt_a, kt_a, "string", "blue", 0); + (kt_b, kt_a, "string", "blue", 1); + (kt_c, kt_a, "string", "blue", 1); + ] + |> Lwt_list.iter_s + @@ fun (contract, ticketer, content_type, content, expected) -> + let* () = + rpc_check_ticket_balance + client + ~contract + ~ticketer + ~content_type + ~content + ~expected + in + let* () = + cli_check_ticket_balance + client + ~hooks + ~contract + ~ticketer + ~content_type + ~content + ~expected + in + unit let register ~protocols = test_ticket_receipt_and_rpc protocols -- GitLab From 18d095e4aceb916c8199899cc5fa8edb3ef6c4df Mon Sep 17 00:00:00 2001 From: lin Date: Mon, 17 Oct 2022 15:40:00 +0900 Subject: [PATCH 5/5] Doc: Update change log for ticket balance CLI --- CHANGES.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 9674eaefe0f7..6e8324389b6d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -46,6 +46,9 @@ Node Client ------ +- Added command to get contract's balance of ticket with specified ticketer, content type, and content: + ``octez-client get ticket balance for with ticketer '' and type and content ``. (MR :gl:`!6491`) + Baker ----- -- GitLab