From ebc00b64636481e21fb1624a6ae56b00713bbb46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Sun, 22 Apr 2018 12:53:35 +0200 Subject: [PATCH] Client/Alpha: display the "real" metadata when `--wait` is used --- src/lib_client_base/client_confirmations.ml | 78 ++++++++++++-------- src/lib_client_base/client_confirmations.mli | 9 ++- src/proto_alpha/lib_client/injection.ml | 10 ++- 3 files changed, 61 insertions(+), 36 deletions(-) diff --git a/src/lib_client_base/client_confirmations.ml b/src/lib_client_base/client_confirmations.ml index f6d3097d9..ed0134bad 100644 --- a/src/lib_client_base/client_confirmations.ml +++ b/src/lib_client_base/client_confirmations.ml @@ -16,9 +16,11 @@ let wait_for_operation_inclusion (* Table of known blocks: - None: if neither the block or its predecessors contains the operation - - (Some n): if the `n-th` predecessors of the block contains the operation *) + - (Some ((hash, i, j), n)): + if the `hash` contains the operation in list `i` at position `j` + and if `hash` denotes the `n-th` predecessors of the block. *) - let blocks : int option Block_hash.Table.t = + let blocks : ((Block_hash.t * int * int) * int) option Block_hash.Table.t = Block_hash.Table.create confirmations in (* Fetch _all_ the 'unknown' predecessors af a block. *) @@ -49,37 +51,44 @@ let wait_for_operation_inclusion Shell_services.Blocks.Header.Shell.predecessor ctxt ~chain ~block () >>=? fun predecessor -> match Block_hash.Table.find blocks predecessor with - | Some n -> + | Some (block_with_op, n) -> ctxt#answer "Operation received %d confirmations as of block: %a" (n+1) Block_hash.pp hash >>= fun () -> + Block_hash.Table.add blocks hash (Some (block_with_op, n+1)) ; if n+1 < confirmations then begin - Block_hash.Table.add blocks hash (Some (n+1)) ; - return false + return None end else - return true + return (Some block_with_op) | None -> Shell_services.Blocks.Operation_hash.operation_hashes ctxt ~chain ~block () >>=? fun operations -> let in_block = - List.exists - (List.exists - (Operation_hash.equal operation_hash)) - operations in - if not in_block then begin - Block_hash.Table.add blocks hash None ; - return false - end else begin - ctxt#answer - "Operation found in block: %a" - Block_hash.pp hash >>= fun () -> - if confirmations <= 0 then - return true - else begin - Block_hash.Table.add blocks hash (Some 0) ; - return false - end - end in + let exception Found of int * int in + try + List.iteri + (fun i ops -> + List.iteri (fun j op -> + if Operation_hash.equal operation_hash op then + raise (Found (i,j))) ops) + operations ; + None + with Found (i,j) -> Some (i, j) in + match in_block with + | None -> + Block_hash.Table.add blocks hash None ; + return None + | Some (i, j) -> begin + ctxt#answer + "Operation found in block: %a (pass: %d, offset: %d)" + Block_hash.pp hash i j >>= fun () -> + Block_hash.Table.add blocks hash (Some ((hash, i, j), 0)) ; + if confirmations <= 0 then + return (Some (hash, i, j)) + else begin + return None + end + end in Shell_services.Monitor.heads ctxt chain >>=? fun (stream, stop) -> Lwt_stream.get stream >>= function @@ -88,10 +97,10 @@ let wait_for_operation_inclusion let rec loop n = if n >= 0 then process (`Hash (head, n)) >>=? function - | true -> + | Some block -> stop () ; - return () - | false -> + return block + | None -> loop (n-1) else let exception WrapError of error list in @@ -101,14 +110,21 @@ let wait_for_operation_inclusion Lwt_stream.find_s (fun block -> process (`Hash (block, 0)) >>= function - | Ok b -> Lwt.return b + | Ok None -> Lwt.return false + | Ok (Some _) -> Lwt.return true | Error err -> Lwt.fail (WrapError err)) stream >>= return) (function | WrapError e -> Lwt.return (Error e) - | exn -> Lwt.fail exn) >>=? fun _ -> - stop () ; - return () in + | exn -> Lwt.fail exn) >>=? function + | None -> + failwith "..." + | Some hash -> + stop () ; + match Block_hash.Table.find_opt blocks hash with + | None | Some None -> assert false + | Some (Some (hash, _)) -> + return hash in Block_services.Empty.hash ctxt ~block:(`Hash (head, predecessors+1)) () >>=? fun oldest -> Block_hash.Table.add blocks oldest None ; diff --git a/src/lib_client_base/client_confirmations.mli b/src/lib_client_base/client_confirmations.mli index bc5d1580b..6e1cc39ad 100644 --- a/src/lib_client_base/client_confirmations.mli +++ b/src/lib_client_base/client_confirmations.mli @@ -7,10 +7,17 @@ (* *) (**************************************************************************) +(** [wait_for_operation_inclusion chain ~predecessors ~confirmations + oph] waits for `oph` to appears in the main chain with at least + `confirmations`. It returns the hash of the block that contains + the operation and the operation position in the block. + + This functions also looks for the operations in the `predecessors` + of the intial chain head. *) val wait_for_operation_inclusion: #Client_context.full -> chain:Chain_services.chain -> ?predecessors:int -> ?confirmations:int -> Operation_hash.t -> - unit tzresult Lwt.t + (Block_hash.t * int * int) tzresult Lwt.t diff --git a/src/proto_alpha/lib_client/injection.ml b/src/proto_alpha/lib_client/injection.ml index dc4980f6a..8ddc2f2a9 100644 --- a/src/proto_alpha/lib_client/injection.ml +++ b/src/proto_alpha/lib_client/injection.ml @@ -181,13 +181,15 @@ let inject_operation cctxt#message "Operation hash is '%a'." Operation_hash.pp oph >>= fun () -> begin match confirmations with - | None -> return () + | None -> return result | Some confirmations -> cctxt#message "Waiting for the operation to be included..." >>= fun () -> Client_confirmations.wait_for_operation_inclusion - ~confirmations cctxt ~chain oph >>=? fun _ -> - return () - end >>=? fun () -> + ~confirmations cctxt ~chain oph >>=? fun (h, i , j) -> + Alpha_block_services.Operation.operation + cctxt ~block:(`Hash (h, 0)) i j >>=? fun op -> + return op.metadata + end >>=? fun result -> cctxt#message "@[This sequence of operations was run:@,%a@]" Operation_result.pp_operation_result (op, result) >>= fun () ->