From 6cf03d0f6bc660b7b6550c03151398e0e249d16e Mon Sep 17 00:00:00 2001 From: Benjamin Canou Date: Sat, 30 Jun 2018 14:52:20 +0200 Subject: [PATCH] Client: select commands depending on the network --- src/bin_client/main_client.ml | 16 +-- src/lib_client_commands/client_commands.ml | 6 +- src/lib_client_commands/client_commands.mli | 7 +- .../client_keys_commands.ml | 50 +++++---- .../bin_accuser/main_accuser_alpha.ml | 2 +- src/proto_alpha/bin_baker/main_baker_alpha.ml | 2 +- .../bin_endorser/main_endorser_alpha.ml | 2 +- .../alpha_commands_registration.ml | 4 +- .../client_proto_context_commands.ml | 103 +++++++++--------- .../delegate_commands_registration.ml | 2 +- .../lib_client/client_proto_main.ml | 2 +- 11 files changed, 102 insertions(+), 94 deletions(-) diff --git a/src/bin_client/main_client.ml b/src/bin_client/main_client.ml index 0fe80e6e8..d27778e3f 100644 --- a/src/bin_client/main_client.ml +++ b/src/bin_client/main_client.ml @@ -75,7 +75,7 @@ let sandbox () = \ Do @{NOT@} use your fundraiser keys on this network.@,\ You should not see this message if you are not a developer.@]@\n@." -let check_version ctxt = +let check_network ctxt = Shell_services.P2p.versions ctxt >>= function | Error _ -> default () ; @@ -98,12 +98,12 @@ let check_version ctxt = default () ; Lwt.return_none -let get_commands_for_version ctxt block protocol = +let get_commands_for_version ctxt network block protocol = Shell_services.Blocks.protocols ctxt ~block () >>= function | Ok { next_protocol = version } -> begin match protocol with | None -> - return (Some version, Client_commands.commands_for_version version) + return (Some version, Client_commands.commands_for_version version network) | Some given_version -> begin if not (Protocol_hash.equal version given_version) then Format.eprintf @@ -112,7 +112,7 @@ let get_commands_for_version ctxt block protocol = is not the one retrieved from the node (%a).@]@\n@." Protocol_hash.pp_short given_version Protocol_hash.pp_short version ; - return (Some version, Client_commands.commands_for_version given_version) + return (Some version, Client_commands.commands_for_version given_version network) end end | Error errs -> begin @@ -125,18 +125,18 @@ let get_commands_for_version ctxt block protocol = return (None, []) end | Some version -> - return (Some version, Client_commands.commands_for_version version) + return (Some version, Client_commands.commands_for_version version network) end let select_commands ctxt { block ; protocol } = - check_version ctxt >>= fun version -> - get_commands_for_version ctxt block protocol >>|? fun (_, commands_for_version) -> + check_network ctxt >>= fun network -> + get_commands_for_version ctxt network block protocol >>|? fun (_, commands_for_version) -> Client_rpc_commands.commands @ List.map (Clic.map_command (fun (o : Client_context.full) -> (o :> Client_context.io_wallet))) (Tezos_signer_backends.Ledger.commands () @ - Client_keys_commands.commands version) @ + Client_keys_commands.commands network) @ Client_helpers_commands.commands () @ commands_for_version diff --git a/src/lib_client_commands/client_commands.ml b/src/lib_client_commands/client_commands.ml index 2f417bc87..b2018e2b8 100644 --- a/src/lib_client_commands/client_commands.ml +++ b/src/lib_client_commands/client_commands.ml @@ -10,6 +10,7 @@ open Client_context type command = full Clic.command +type network = [ `Betanet | `Alphanet | `Zeronet | `Sandbox ] exception Version_not_found @@ -24,8 +25,9 @@ let get_versions () = let register name commands = let previous = try Protocol_hash.Table.find versions name - with Not_found -> [] in - Protocol_hash.Table.replace versions name (commands @ previous) + with Not_found -> (fun (_network : network option) -> ([] : command list)) in + Protocol_hash.Table.replace versions name + (fun (network : network option) -> (commands network @ previous network)) let commands_for_version version = try Protocol_hash.Table.find versions version diff --git a/src/lib_client_commands/client_commands.mli b/src/lib_client_commands/client_commands.mli index eb2fadd94..fcb1f238e 100644 --- a/src/lib_client_commands/client_commands.mli +++ b/src/lib_client_commands/client_commands.mli @@ -10,9 +10,10 @@ open Client_context type command = full Clic.command +type network = [ `Betanet | `Alphanet | `Zeronet | `Sandbox ] exception Version_not_found -val register: Protocol_hash.t -> command list -> unit -val commands_for_version: Protocol_hash.t -> command list -val get_versions: unit -> (Protocol_hash.t * (command list)) list +val register: Protocol_hash.t -> (network option -> command list) -> unit +val commands_for_version: Protocol_hash.t -> network option -> command list +val get_versions: unit -> (Protocol_hash.t * (network option -> command list)) list diff --git a/src/lib_client_commands/client_keys_commands.ml b/src/lib_client_commands/client_keys_commands.ml index ef7b7da45..861b46041 100644 --- a/src/lib_client_commands/client_keys_commands.ml +++ b/src/lib_client_commands/client_keys_commands.ml @@ -279,30 +279,32 @@ let commands version : Client_context.io_wallet Clic.command list = "Tezos address added: %a" Signature.Public_key_hash.pp pkh >>= fun () -> register_key cctxt ~force (pkh, pk_uri, sk_uri) ?public_key name) ; - - command ~group ~desc: "Add a fundraiser secret key to the wallet." - (args1 (Secret_key.force_switch ())) - (prefix "import" - @@ prefixes [ "fundraiser" ; "secret" ; "key" ] - @@ Secret_key.fresh_alias_param - @@ stop) - (fun force name (cctxt : Client_context.io_wallet) -> - Secret_key.of_fresh cctxt force name >>=? fun name -> - input_fundraiser_params cctxt >>=? fun sk -> - Tezos_signer_backends.Encrypted.encrypt cctxt sk >>=? fun sk_uri -> - Client_keys.neuterize sk_uri >>=? fun pk_uri -> - begin - Public_key.find_opt cctxt name >>=? function - | None -> return_unit - | Some (pk_uri_found, _) -> - fail_unless (pk_uri = pk_uri_found || force) - (failure - "public and secret keys '%s' don't correspond, \ - please don't use --force" name) - end >>=? fun () -> - Client_keys.public_key_hash pk_uri >>=? fun (pkh, _public_key) -> - register_key cctxt ~force (pkh, pk_uri, sk_uri) name) ; - + ] @ + (if version <> (Some `Betanet) then [] else [ + command ~group ~desc: "Add a fundraiser secret key to the wallet." + (args1 (Secret_key.force_switch ())) + (prefix "import" + @@ prefixes [ "fundraiser" ; "secret" ; "key" ] + @@ Secret_key.fresh_alias_param + @@ stop) + (fun force name (cctxt : Client_context.io_wallet) -> + Secret_key.of_fresh cctxt force name >>=? fun name -> + input_fundraiser_params cctxt >>=? fun sk -> + Tezos_signer_backends.Encrypted.encrypt cctxt sk >>=? fun sk_uri -> + Client_keys.neuterize sk_uri >>=? fun pk_uri -> + begin + Public_key.find_opt cctxt name >>=? function + | None -> return_unit + | Some (pk_uri_found, _) -> + fail_unless (pk_uri = pk_uri_found || force) + (failure + "public and secret keys '%s' don't correspond, \ + please don't use --force" name) + end >>=? fun () -> + Client_keys.public_key_hash pk_uri >>=? fun (pkh, _public_key) -> + register_key cctxt ~force (pkh, pk_uri, sk_uri) name) ; + ]) @ + [ command ~group ~desc: "Add a public key to the wallet." (args1 (Public_key.force_switch ())) (prefix "import" diff --git a/src/proto_alpha/bin_accuser/main_accuser_alpha.ml b/src/proto_alpha/bin_accuser/main_accuser_alpha.ml index 26e5c4ea3..af2f366eb 100644 --- a/src/proto_alpha/bin_accuser/main_accuser_alpha.ml +++ b/src/proto_alpha/bin_accuser/main_accuser_alpha.ml @@ -8,7 +8,7 @@ (**************************************************************************) let () = - Client_commands.register Proto_alpha.hash @@ + Client_commands.register Proto_alpha.hash @@ fun _network -> List.map (Clic.map_command (new Proto_alpha.wrap_full)) @@ Delegate_commands.accuser_commands () diff --git a/src/proto_alpha/bin_baker/main_baker_alpha.ml b/src/proto_alpha/bin_baker/main_baker_alpha.ml index 0b835f475..96636740a 100644 --- a/src/proto_alpha/bin_baker/main_baker_alpha.ml +++ b/src/proto_alpha/bin_baker/main_baker_alpha.ml @@ -8,7 +8,7 @@ (**************************************************************************) let () = - Client_commands.register Proto_alpha.hash @@ + Client_commands.register Proto_alpha.hash @@ fun _network -> List.map (Clic.map_command (new Proto_alpha.wrap_full)) @@ Delegate_commands.delegate_commands () diff --git a/src/proto_alpha/bin_endorser/main_endorser_alpha.ml b/src/proto_alpha/bin_endorser/main_endorser_alpha.ml index 3afdce630..c00c7ad08 100644 --- a/src/proto_alpha/bin_endorser/main_endorser_alpha.ml +++ b/src/proto_alpha/bin_endorser/main_endorser_alpha.ml @@ -8,7 +8,7 @@ (**************************************************************************) let () = - Client_commands.register Proto_alpha.hash @@ + Client_commands.register Proto_alpha.hash @@ fun _network -> List.map (Clic.map_command (new Proto_alpha.wrap_full)) @@ Delegate_commands.delegate_commands () diff --git a/src/proto_alpha/lib_client_commands/alpha_commands_registration.ml b/src/proto_alpha/lib_client_commands/alpha_commands_registration.ml index 3da4bb378..6416c1244 100644 --- a/src/proto_alpha/lib_client_commands/alpha_commands_registration.ml +++ b/src/proto_alpha/lib_client_commands/alpha_commands_registration.ml @@ -8,8 +8,8 @@ (**************************************************************************) let () = - Client_commands.register Proto_alpha.hash @@ + Client_commands.register Proto_alpha.hash @@ fun network -> List.map (Clic.map_command (new Proto_alpha.wrap_full)) @@ Client_proto_programs_commands.commands () @ Client_proto_contracts_commands.commands () @ - Client_proto_context_commands.commands () + Client_proto_context_commands.commands network () 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 65640542c..709940bbf 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 @@ -58,7 +58,7 @@ let binary_description = { Clic.name = "description" ; title = "Binary Description" } -let commands () = +let commands version () = let open Clic in [ command ~group ~desc: "Access the timestamp of the block." @@ -342,55 +342,58 @@ let commands () = ~fee ~manager_sk:src_sk src_pk >>=? fun _res -> return_unit end; - - command ~group ~desc:"Register and activate an Alphanet/Zeronet faucet account." - (args2 - (Secret_key.force_switch ()) - encrypted_switch) - (prefixes [ "activate" ; "account" ] - @@ Secret_key.fresh_alias_param - @@ prefixes [ "with" ] - @@ param ~name:"activation_key" - ~desc:"Activate an Alphanet/Zeronet faucet account from the doanloaded JSON file." - file_parameter - @@ stop) - (fun (force, encrypted) name activation_key_file cctxt -> - Secret_key.of_fresh cctxt force name >>=? fun name -> - Lwt_utils_unix.Json.read_file activation_key_file >>=? fun json -> - match Data_encoding.Json.destruct - Client_proto_context.activation_key_encoding - json with - | exception (Data_encoding.Json.Cannot_destruct _ as exn) -> - Format.kasprintf (fun s -> failwith "%s" s) - "Invalid activation file: %a %a" - (fun ppf -> Data_encoding.Json.print_error ppf) exn - Data_encoding.Json.pp json - | key -> - activate_account cctxt - ~chain:`Main ~block:cctxt#block ?confirmations:cctxt#confirmations - ~encrypted ~force key name >>=? fun _res -> - return_unit - ); - - command ~group ~desc:"Activate a fundraiser account." - (args1 dry_run_switch) - (prefixes [ "activate" ; "fundraiser" ; "account" ] - @@ Public_key_hash.alias_param - @@ prefixes [ "with" ] - @@ param ~name:"code" - (Clic.parameter (fun _ctx code -> - protect (fun () -> - return (Blinded_public_key_hash.activation_code_of_hex code)))) - ~desc:"Activation code obtained from the Tezos foundation." - @@ stop) - (fun dry_run (name, _pkh) code cctxt -> - activate_existing_account cctxt ~chain:`Main - ~block:cctxt#block ?confirmations:cctxt#confirmations - ~dry_run - name code >>=? fun _res -> - return_unit - ); - + ] @ + (if version = (Some `Betanet) then [] else [ + command ~group ~desc:"Register and activate an Alphanet/Zeronet faucet account." + (args2 + (Secret_key.force_switch ()) + encrypted_switch) + (prefixes [ "activate" ; "account" ] + @@ Secret_key.fresh_alias_param + @@ prefixes [ "with" ] + @@ param ~name:"activation_key" + ~desc:"Activate an Alphanet/Zeronet faucet account from the downloaded JSON file." + file_parameter + @@ stop) + (fun (force, encrypted) name activation_key_file cctxt -> + Secret_key.of_fresh cctxt force name >>=? fun name -> + Lwt_utils_unix.Json.read_file activation_key_file >>=? fun json -> + match Data_encoding.Json.destruct + Client_proto_context.activation_key_encoding + json with + | exception (Data_encoding.Json.Cannot_destruct _ as exn) -> + Format.kasprintf (fun s -> failwith "%s" s) + "Invalid activation file: %a %a" + (fun ppf -> Data_encoding.Json.print_error ppf) exn + Data_encoding.Json.pp json + | key -> + activate_account cctxt + ~chain:`Main ~block:cctxt#block ?confirmations:cctxt#confirmations + ~encrypted ~force key name >>=? fun _res -> + return_unit + ); + ]) @ + (if version <> Some `Betanet then [] else [ + command ~group ~desc:"Activate a fundraiser account." + (args1 dry_run_switch) + (prefixes [ "activate" ; "fundraiser" ; "account" ] + @@ Public_key_hash.alias_param + @@ prefixes [ "with" ] + @@ param ~name:"code" + (Clic.parameter (fun _ctx code -> + protect (fun () -> + return (Blinded_public_key_hash.activation_code_of_hex code)))) + ~desc:"Activation code obtained from the Tezos foundation." + @@ stop) + (fun dry_run (name, _pkh) code cctxt -> + activate_existing_account cctxt ~chain:`Main + ~block:cctxt#block ?confirmations:cctxt#confirmations + ~dry_run + name code >>=? fun _res -> + return_unit + ); + ]) @ + [ command ~desc:"Wait until an operation is included in a block" (let int_param = parameter diff --git a/src/proto_alpha/lib_delegate/delegate_commands_registration.ml b/src/proto_alpha/lib_delegate/delegate_commands_registration.ml index efea65321..295fa8a4f 100644 --- a/src/proto_alpha/lib_delegate/delegate_commands_registration.ml +++ b/src/proto_alpha/lib_delegate/delegate_commands_registration.ml @@ -8,6 +8,6 @@ (**************************************************************************) let () = - Client_commands.register Proto_alpha.hash @@ + Client_commands.register Proto_alpha.hash @@ fun _network -> List.map (Clic.map_command (new Proto_alpha.wrap_full)) @@ Delegate_commands.delegate_commands () diff --git a/src/proto_genesis/lib_client/client_proto_main.ml b/src/proto_genesis/lib_client/client_proto_main.ml index 63c01a22e..e08d00f5b 100644 --- a/src/proto_genesis/lib_client/client_proto_main.ml +++ b/src/proto_genesis/lib_client/client_proto_main.ml @@ -101,5 +101,5 @@ let commands () = ] let () = - Client_commands.register protocol @@ + Client_commands.register protocol @@ fun _network -> commands ()