From 79b2a4e82561440c45d7d5c4e33c9cd88d1554c9 Mon Sep 17 00:00:00 2001
From: Pietro <pietro.abate@inria.fr>
Date: Fri, 15 Jun 2018 13:16:09 +0200
Subject: [PATCH] Client: wait for the node to be bootstrapped before injecting
 an operation

---
 src/lib_client_base/client_confirmations.ml  | 25 +++++++++++++++++++-
 src/lib_client_base/client_confirmations.mli |  3 +++
 src/proto_alpha/lib_client/injection.ml      |  1 +
 3 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/lib_client_base/client_confirmations.ml b/src/lib_client_base/client_confirmations.ml
index b2c6cfedf..c691c3af9 100644
--- a/src/lib_client_base/client_confirmations.ml
+++ b/src/lib_client_base/client_confirmations.ml
@@ -7,6 +7,30 @@
 (*                                                                        *)
 (**************************************************************************)
 
+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 ;
+      Lwt.return ()
+    else
+      Lwt.return ()
+  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 ())
+       else Lwt.return ()) stream >>= fun () ->
+  display := true ;
+  ctxt#answer "Node is bootstrapped, ready for injecting operations." >>= fun () ->
+  return ()
+
 let wait_for_operation_inclusion
     (ctxt : #Client_context.full)
     ~chain
@@ -133,4 +157,3 @@ let wait_for_operation_inclusion
         ctxt ~block:(`Hash (head, predecessors+1)) () >>=? fun oldest ->
       Block_hash.Table.add blocks oldest None ;
       loop predecessors
-
diff --git a/src/lib_client_base/client_confirmations.mli b/src/lib_client_base/client_confirmations.mli
index 6e1cc39ad..612945829 100644
--- a/src/lib_client_base/client_confirmations.mli
+++ b/src/lib_client_base/client_confirmations.mli
@@ -21,3 +21,6 @@ val wait_for_operation_inclusion:
   ?confirmations:int ->
   Operation_hash.t ->
   (Block_hash.t * int * int) tzresult Lwt.t
+
+val wait_for_bootstrapped:
+  #Client_context.full -> unit tzresult Lwt.t
diff --git a/src/proto_alpha/lib_client/injection.ml b/src/proto_alpha/lib_client/injection.ml
index 2b7ef9fc1..3c35abd4e 100644
--- a/src/proto_alpha/lib_client/injection.ml
+++ b/src/proto_alpha/lib_client/injection.ml
@@ -315,6 +315,7 @@ let may_patch_limits
 let inject_operation
     (type kind) cctxt ~chain ~block
     ?confirmations ?branch ?src_sk (contents: kind contents_list)  =
+  Client_confirmations.wait_for_bootstrapped cctxt >>=? fun () ->
   may_patch_limits
     cctxt ~chain ~block ?branch contents >>=? fun contents ->
   preapply cctxt ~chain ~block