Client/Alpha: display the "real" metadata when --wait is used

This commit is contained in:
Grégoire Henry 2018-04-22 12:53:35 +02:00 committed by Benjamin Canou
parent 89cbe0f8fa
commit ebc00b6463
3 changed files with 61 additions and 36 deletions

View File

@ -16,9 +16,11 @@ let wait_for_operation_inclusion
(* Table of known blocks: (* Table of known blocks:
- None: if neither the block or its predecessors contains the operation - 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 Block_hash.Table.create confirmations in
(* Fetch _all_ the 'unknown' predecessors af a block. *) (* Fetch _all_ the 'unknown' predecessors af a block. *)
@ -49,35 +51,42 @@ let wait_for_operation_inclusion
Shell_services.Blocks.Header.Shell.predecessor Shell_services.Blocks.Header.Shell.predecessor
ctxt ~chain ~block () >>=? fun predecessor -> ctxt ~chain ~block () >>=? fun predecessor ->
match Block_hash.Table.find blocks predecessor with match Block_hash.Table.find blocks predecessor with
| Some n -> | Some (block_with_op, n) ->
ctxt#answer ctxt#answer
"Operation received %d confirmations as of block: %a" "Operation received %d confirmations as of block: %a"
(n+1) Block_hash.pp hash >>= fun () -> (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 if n+1 < confirmations then begin
Block_hash.Table.add blocks hash (Some (n+1)) ; return None
return false
end else end else
return true return (Some block_with_op)
| None -> | None ->
Shell_services.Blocks.Operation_hash.operation_hashes Shell_services.Blocks.Operation_hash.operation_hashes
ctxt ~chain ~block () >>=? fun operations -> ctxt ~chain ~block () >>=? fun operations ->
let in_block = let in_block =
List.exists let exception Found of int * int in
(List.exists try
(Operation_hash.equal operation_hash)) List.iteri
operations in (fun i ops ->
if not in_block then begin 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 ; Block_hash.Table.add blocks hash None ;
return false return None
end else begin | Some (i, j) -> begin
ctxt#answer ctxt#answer
"Operation found in block: %a" "Operation found in block: %a (pass: %d, offset: %d)"
Block_hash.pp hash >>= fun () -> Block_hash.pp hash i j >>= fun () ->
Block_hash.Table.add blocks hash (Some ((hash, i, j), 0)) ;
if confirmations <= 0 then if confirmations <= 0 then
return true return (Some (hash, i, j))
else begin else begin
Block_hash.Table.add blocks hash (Some 0) ; return None
return false
end end
end in end in
@ -88,10 +97,10 @@ let wait_for_operation_inclusion
let rec loop n = let rec loop n =
if n >= 0 then if n >= 0 then
process (`Hash (head, n)) >>=? function process (`Hash (head, n)) >>=? function
| true -> | Some block ->
stop () ; stop () ;
return () return block
| false -> | None ->
loop (n-1) loop (n-1)
else else
let exception WrapError of error list in let exception WrapError of error list in
@ -101,14 +110,21 @@ let wait_for_operation_inclusion
Lwt_stream.find_s Lwt_stream.find_s
(fun block -> (fun block ->
process (`Hash (block, 0)) >>= function process (`Hash (block, 0)) >>= function
| Ok b -> Lwt.return b | Ok None -> Lwt.return false
| Ok (Some _) -> Lwt.return true
| Error err -> | Error err ->
Lwt.fail (WrapError err)) stream >>= return) Lwt.fail (WrapError err)) stream >>= return)
(function (function
| WrapError e -> Lwt.return (Error e) | WrapError e -> Lwt.return (Error e)
| exn -> Lwt.fail exn) >>=? fun _ -> | exn -> Lwt.fail exn) >>=? function
| None ->
failwith "..."
| Some hash ->
stop () ; stop () ;
return () in match Block_hash.Table.find_opt blocks hash with
| None | Some None -> assert false
| Some (Some (hash, _)) ->
return hash in
Block_services.Empty.hash Block_services.Empty.hash
ctxt ~block:(`Hash (head, predecessors+1)) () >>=? fun oldest -> ctxt ~block:(`Hash (head, predecessors+1)) () >>=? fun oldest ->
Block_hash.Table.add blocks oldest None ; Block_hash.Table.add blocks oldest None ;

View File

@ -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: val wait_for_operation_inclusion:
#Client_context.full -> #Client_context.full ->
chain:Chain_services.chain -> chain:Chain_services.chain ->
?predecessors:int -> ?predecessors:int ->
?confirmations:int -> ?confirmations:int ->
Operation_hash.t -> Operation_hash.t ->
unit tzresult Lwt.t (Block_hash.t * int * int) tzresult Lwt.t

View File

@ -181,13 +181,15 @@ let inject_operation
cctxt#message "Operation hash is '%a'." Operation_hash.pp oph >>= fun () -> cctxt#message "Operation hash is '%a'." Operation_hash.pp oph >>= fun () ->
begin begin
match confirmations with match confirmations with
| None -> return () | None -> return result
| Some confirmations -> | Some confirmations ->
cctxt#message "Waiting for the operation to be included..." >>= fun () -> cctxt#message "Waiting for the operation to be included..." >>= fun () ->
Client_confirmations.wait_for_operation_inclusion Client_confirmations.wait_for_operation_inclusion
~confirmations cctxt ~chain oph >>=? fun _ -> ~confirmations cctxt ~chain oph >>=? fun (h, i , j) ->
return () Alpha_block_services.Operation.operation
end >>=? fun () -> cctxt ~block:(`Hash (h, 0)) i j >>=? fun op ->
return op.metadata
end >>=? fun result ->
cctxt#message cctxt#message
"@[<v 2>This sequence of operations was run:@,%a@]" "@[<v 2>This sequence of operations was run:@,%a@]"
Operation_result.pp_operation_result (op, result) >>= fun () -> Operation_result.pp_operation_result (op, result) >>= fun () ->