2018-04-22 12:50:34 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2018-06-15 15:16:09 +04:00
|
|
|
let wait_for_bootstrapped (ctxt : #Client_context.full) =
|
|
|
|
let display = ref false in
|
|
|
|
Lwt.async begin fun () ->
|
|
|
|
Lwt_unix.sleep 0.3 >>= fun () ->
|
|
|
|
if not !display then
|
|
|
|
ctxt#answer "Waiting for the node to be bootstrapped before injection..." >>= fun () ->
|
|
|
|
display := true ;
|
2018-06-26 13:07:12 +04:00
|
|
|
Lwt.return_unit
|
2018-06-15 15:16:09 +04:00
|
|
|
else
|
2018-06-26 13:07:12 +04:00
|
|
|
Lwt.return_unit
|
2018-06-15 15:16:09 +04:00
|
|
|
end ;
|
|
|
|
Monitor_services.bootstrapped ctxt >>=? fun (stream, _stop) ->
|
|
|
|
Lwt_stream.iter_s
|
|
|
|
(fun (hash, time) ->
|
|
|
|
if !display then
|
|
|
|
ctxt#message "Current head: %a (timestamp: %a, validation: %a)"
|
|
|
|
Block_hash.pp_short hash
|
|
|
|
Time.pp_hum time
|
|
|
|
Time.pp_hum (Time.now ())
|
2018-06-26 13:07:12 +04:00
|
|
|
else Lwt.return_unit) stream >>= fun () ->
|
2018-06-15 15:16:09 +04:00
|
|
|
display := true ;
|
|
|
|
ctxt#answer "Node is bootstrapped, ready for injecting operations." >>= fun () ->
|
2018-06-26 13:07:12 +04:00
|
|
|
return_unit
|
2018-06-15 15:16:09 +04:00
|
|
|
|
2018-04-22 12:50:34 +04:00
|
|
|
let wait_for_operation_inclusion
|
|
|
|
(ctxt : #Client_context.full)
|
2018-04-16 02:44:24 +04:00
|
|
|
~chain
|
2018-04-22 12:50:34 +04:00
|
|
|
?(predecessors = 10)
|
|
|
|
?(confirmations = 1)
|
|
|
|
operation_hash =
|
2018-04-16 02:44:24 +04:00
|
|
|
|
|
|
|
(* Table of known blocks:
|
|
|
|
- None: if neither the block or its predecessors contains the operation
|
2018-04-22 14:53:35 +04:00
|
|
|
- (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. *)
|
2018-04-16 02:44:24 +04:00
|
|
|
|
2018-04-22 14:53:35 +04:00
|
|
|
let blocks : ((Block_hash.t * int * int) * int) option Block_hash.Table.t =
|
2018-04-16 02:44:24 +04:00
|
|
|
Block_hash.Table.create confirmations in
|
|
|
|
|
|
|
|
(* Fetch _all_ the 'unknown' predecessors af a block. *)
|
|
|
|
|
2018-06-06 17:21:56 +04:00
|
|
|
let fetch_predecessors (hash, header) =
|
|
|
|
let rec loop acc (_hash, header) =
|
|
|
|
let predecessor = header.Block_header.predecessor in
|
2018-04-16 02:44:24 +04:00
|
|
|
if Block_hash.Table.mem blocks predecessor then
|
|
|
|
return acc
|
|
|
|
else
|
2018-06-06 17:21:56 +04:00
|
|
|
Chain_services.Blocks.Header.shell_header
|
|
|
|
ctxt ~chain ~block:(`Hash (predecessor, 0)) () >>=? fun shell ->
|
|
|
|
let block = (predecessor, shell) in
|
|
|
|
loop (block :: acc) block in
|
|
|
|
loop [hash, header.Block_header.shell] (hash, header.shell) >>= function
|
2018-04-16 02:44:24 +04:00
|
|
|
| Ok blocks -> Lwt.return blocks
|
|
|
|
| Error err ->
|
|
|
|
ctxt#warning
|
|
|
|
"Error while fetching block (ignored): %a"
|
|
|
|
pp_print_error err >>= fun () ->
|
|
|
|
(* Will be retried when a new head arrives *)
|
|
|
|
Lwt.return [] in
|
|
|
|
|
|
|
|
(* Check whether a block as enough confirmations. This function
|
|
|
|
assumes that the block predecessor has been processed already. *)
|
|
|
|
|
2018-06-06 17:21:56 +04:00
|
|
|
let process hash header =
|
|
|
|
let block = `Hash (hash, 0) in
|
|
|
|
let predecessor = header.Tezos_base.Block_header.predecessor in
|
2018-04-16 02:44:24 +04:00
|
|
|
match Block_hash.Table.find blocks predecessor with
|
2018-04-22 14:53:35 +04:00
|
|
|
| Some (block_with_op, n) ->
|
2018-04-22 12:50:34 +04:00
|
|
|
ctxt#answer
|
|
|
|
"Operation received %d confirmations as of block: %a"
|
|
|
|
(n+1) Block_hash.pp hash >>= fun () ->
|
2018-04-22 14:53:35 +04:00
|
|
|
Block_hash.Table.add blocks hash (Some (block_with_op, n+1)) ;
|
2018-04-22 12:50:34 +04:00
|
|
|
if n+1 < confirmations then begin
|
2018-04-22 14:53:35 +04:00
|
|
|
return None
|
2018-04-22 12:50:34 +04:00
|
|
|
end else
|
2018-04-22 14:53:35 +04:00
|
|
|
return (Some block_with_op)
|
2018-04-22 12:50:34 +04:00
|
|
|
| None ->
|
2018-06-11 16:25:47 +04:00
|
|
|
Shell_services.Blocks.Operation_hashes.operation_hashes
|
2018-04-16 02:44:24 +04:00
|
|
|
ctxt ~chain ~block () >>=? fun operations ->
|
2018-04-22 12:50:34 +04:00
|
|
|
let in_block =
|
2018-04-22 14:53:35 +04:00
|
|
|
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
|
2018-04-16 02:44:24 +04:00
|
|
|
|
2018-04-22 16:40:44 +04:00
|
|
|
Shell_services.Monitor.heads ctxt chain >>=? fun (stream, stop) ->
|
2018-04-16 02:44:24 +04:00
|
|
|
Lwt_stream.get stream >>= function
|
|
|
|
| None -> assert false
|
2018-06-06 17:15:44 +04:00
|
|
|
| Some (head, _) ->
|
2018-04-16 02:44:24 +04:00
|
|
|
let rec loop n =
|
|
|
|
if n >= 0 then
|
2018-06-06 17:21:56 +04:00
|
|
|
let block = `Hash (head, n) in
|
|
|
|
Shell_services.Blocks.hash ctxt ~chain ~block () >>=? fun hash ->
|
|
|
|
Shell_services.Blocks.Header.shell_header ctxt
|
|
|
|
~chain ~block () >>=? fun shell ->
|
|
|
|
process hash shell >>=? function
|
2018-04-22 14:53:35 +04:00
|
|
|
| Some block ->
|
2018-04-16 02:44:24 +04:00
|
|
|
stop () ;
|
2018-04-22 14:53:35 +04:00
|
|
|
return block
|
|
|
|
| None ->
|
2018-04-16 02:44:24 +04:00
|
|
|
loop (n-1)
|
|
|
|
else
|
|
|
|
let exception WrapError of error list in
|
|
|
|
Lwt.catch
|
|
|
|
(fun () ->
|
|
|
|
let stream = Lwt_stream.map_list_s fetch_predecessors stream in
|
|
|
|
Lwt_stream.find_s
|
2018-06-06 17:21:56 +04:00
|
|
|
(fun (hash, header) ->
|
|
|
|
process hash header >>= function
|
2018-04-22 14:53:35 +04:00
|
|
|
| Ok None -> Lwt.return false
|
|
|
|
| Ok (Some _) -> Lwt.return true
|
2018-04-16 02:44:24 +04:00
|
|
|
| Error err ->
|
|
|
|
Lwt.fail (WrapError err)) stream >>= return)
|
|
|
|
(function
|
|
|
|
| WrapError e -> Lwt.return (Error e)
|
2018-04-22 14:53:35 +04:00
|
|
|
| exn -> Lwt.fail exn) >>=? function
|
|
|
|
| None ->
|
|
|
|
failwith "..."
|
2018-06-06 17:21:56 +04:00
|
|
|
| Some (hash, _) ->
|
2018-04-22 14:53:35 +04:00
|
|
|
stop () ;
|
|
|
|
match Block_hash.Table.find_opt blocks hash with
|
|
|
|
| None | Some None -> assert false
|
|
|
|
| Some (Some (hash, _)) ->
|
|
|
|
return hash in
|
2018-04-16 02:44:24 +04:00
|
|
|
Block_services.Empty.hash
|
|
|
|
ctxt ~block:(`Hash (head, predecessors+1)) () >>=? fun oldest ->
|
|
|
|
Block_hash.Table.add blocks oldest None ;
|
|
|
|
loop predecessors
|