From 9b3cde5df9dc6bfc3507b02f831f0aedfd876c8a Mon Sep 17 00:00:00 2001 From: Guillem Rieu Date: Tue, 21 Mar 2017 15:19:35 +0100 Subject: [PATCH] Client: add Client_proto_context.delegate_contract function --- .../embedded/alpha/client_proto_context.ml | 29 +++++++++++++++++++ .../embedded/alpha/client_proto_context.mli | 11 +++++++ 2 files changed, 40 insertions(+) diff --git a/src/client/embedded/alpha/client_proto_context.ml b/src/client/embedded/alpha/client_proto_context.ml index 6a36fbf23..0966d53c5 100644 --- a/src/client/embedded/alpha/client_proto_context.ml +++ b/src/client/embedded/alpha/client_proto_context.ml @@ -169,6 +169,35 @@ let faucet cctxt block ?force ~manager_pkh () = ~net ~id:manager_pkh () >>=? fun bytes -> originate cctxt ?force ~block bytes +let delegate_contract cctxt + block ?force + ~source ?src_pk ~manager_sk + ~fee delegate_opt = + Client_node_rpcs.Blocks.net cctxt block >>= fun net -> + Client_proto_rpcs.Context.Contract.counter cctxt block source + >>=? fun pcounter -> + let counter = Int32.succ pcounter in + cctxt.message "Acquired the source's sequence counter (%ld -> %ld)." + pcounter counter >>= fun () -> + Client_proto_rpcs.Helpers.Forge.Manager.delegation cctxt block + ~net ~source ?sourcePubKey:src_pk ~counter ~fee delegate_opt + >>=? fun bytes -> + cctxt.Client_commands.message "Forged the raw origination frame." >>= fun () -> + Client_node_rpcs.Blocks.predecessor cctxt block >>= fun predecessor -> + let signature = Environment.Ed25519.sign manager_sk bytes in + let signed_bytes = MBytes.concat bytes signature in + let oph = Operation_hash.hash_bytes [ signed_bytes ] in + Client_proto_rpcs.Helpers.apply_operation cctxt block + predecessor oph bytes (Some signature) >>=? function + | [] -> + Client_node_rpcs.inject_operation cctxt ?force signed_bytes >>=? fun injected_oph -> + assert (Operation_hash.equal oph injected_oph) ; + cctxt.message "Operation successfully injected in the node." >>= fun () -> + cctxt.message "Operation hash is '%a'." Operation_hash.pp oph >>= fun () -> + return () + | contracts -> + cctxt.error "The origination introduced %d contracts instead of one." (List.length contracts) + let dictate cctxt block command seckey = Client_node_rpcs.Blocks.net cctxt block >>= fun net -> Client_proto_rpcs.Helpers.Forge.Dictator.operation diff --git a/src/client/embedded/alpha/client_proto_context.mli b/src/client/embedded/alpha/client_proto_context.mli index 8f1720d26..b17b74112 100644 --- a/src/client/embedded/alpha/client_proto_context.mli +++ b/src/client/embedded/alpha/client_proto_context.mli @@ -63,4 +63,15 @@ val originate_contract: fee:Tez.t -> unit -> Contract.t tzresult Lwt.t +val delegate_contract: + Client_commands.context -> + Client_proto_rpcs.block -> + ?force:bool -> + source:Contract.t -> + ?src_pk:public_key -> + manager_sk:secret_key -> + fee:Tez.t -> + public_key_hash option -> + unit tzresult Lwt.t + val commands: unit -> Client_commands.command list