Source file client_confirmations.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
let in_block operation_hash operations =
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)
type operation_status =
| Confirmed of (Block_hash.t * int * int)
| Pending
| Still_not_found
let wait_for_operation_inclusion (ctxt : #Client_context.full) ~chain
?(predecessors = 10) ?(confirmations = 1) ?branch operation_hash =
let open Lwt_result_syntax in
let exception WrapError of error list in
let exception Outdated of Operation_hash.t in
let blocks : ((Block_hash.t * int * int) * int) option Block_hash.Table.t =
Block_hash.Table.create ~random:true confirmations
in
let fetch_predecessors (hash, ) =
let rec loop acc (_hash, ) =
let predecessor = header.Block_header.predecessor in
if Block_hash.Table.mem blocks predecessor then return acc
else
let* shell =
Chain_services.Blocks.Header.shell_header
ctxt
~chain
~block:(`Hash (predecessor, 0))
()
in
let block = (predecessor, shell) in
loop (block :: acc) block
in
let*! r = loop [(hash, header.Block_header.shell)] (hash, header.shell) in
match r with
| Ok blocks -> Lwt.return blocks
| Error err ->
let*! () =
ctxt#warning
"Error while fetching block (ignored): %a"
pp_print_trace
err
in
Lwt.return_nil
in
let process hash =
let block = `Hash (hash, 0) in
let predecessor = header.Tezos_base.Block_header.predecessor in
let pred_block =
WithExceptions.Option.to_exn ~none:Not_found
@@ Block_hash.Table.find blocks predecessor
in
match pred_block with
| Some (block_with_op, n) ->
let*! () =
ctxt#answer
"Operation received %d confirmations as of block: %a"
(n + 1)
Block_hash.pp
hash
in
Block_hash.Table.add blocks hash (Some (block_with_op, n + 1)) ;
if n + 1 < confirmations then return Pending
else return (Confirmed block_with_op)
| None -> (
let* operations =
Shell_services.Blocks.Operation_hashes.operation_hashes
ctxt
~chain
~block
()
in
match in_block operation_hash operations with
| None ->
Block_hash.Table.add blocks hash None ;
return Still_not_found
| Some (i, j) ->
let*! () =
ctxt#answer
"Operation found in block: %a (pass: %d, offset: %d)"
Block_hash.pp
hash
i
j
in
Block_hash.Table.add blocks hash (Some ((hash, i, j), 0)) ;
if confirmations <= 0 then return (Confirmed (hash, i, j))
else return Pending)
in
let check_branch_alive () =
match branch with
| Some branch_hash -> (
let*! r =
Shell_services.Blocks.live_blocks ctxt ~chain ~block:(`Head 0) ()
in
match r with
| Ok live_blocks ->
if Block_hash.Set.mem branch_hash live_blocks then Lwt.return_unit
else
let*! () =
ctxt#error
"The operation %a is outdated and may never be included in \
the chain.@,\
We recommend to use an external block explorer."
Operation_hash.pp
operation_hash
in
Lwt.fail (Outdated operation_hash)
| Error err -> Lwt.fail (WrapError err))
| None -> Lwt.return_unit
in
let* stream, stop = Shell_services.Monitor.heads ctxt chain in
let*! o = Lwt_stream.get stream in
match o with
| None -> assert false
| Some (head, _) ->
let rec loop n =
if n >= 0 then
let block = `Hash (head, n) in
let* hash = Shell_services.Blocks.hash ctxt ~chain ~block () in
let* shell =
Shell_services.Blocks.Header.shell_header ctxt ~chain ~block ()
in
let* r = process hash shell in
match r with
| Confirmed block ->
stop () ;
return block
| Pending | Still_not_found -> loop (n - 1)
else
let* o =
Lwt.catch
(fun () ->
Lwt_result.ok
@@
let stream = Lwt_stream.map_list_s fetch_predecessors stream in
Lwt_stream.find_s
(fun (hash, ) ->
let*! r = process hash header in
match r with
| Ok Pending -> Lwt.return_false
| Ok Still_not_found ->
let*! () = check_branch_alive () in
Lwt.return_false
| Ok (Confirmed _) -> Lwt.return_true
| Error err -> Lwt.fail (WrapError err))
stream)
(function
| WrapError e -> Lwt.return_error e | exn -> Lwt.fail exn)
in
match o with
| None -> failwith "..."
| Some (hash, _) -> (
stop () ;
match Block_hash.Table.find blocks hash with
| None | Some None -> assert false
| Some (Some (hash, _)) -> return hash)
in
let* block_hook =
match branch with
| Some branch_hash ->
let* =
Shell_services.Blocks.Header.shell_header
ctxt
~chain
~block:(`Hash (branch_hash, 0))
()
in
let branch_level = branch_header.Block_header.level in
let* head_shell =
Shell_services.Blocks.Header.shell_header
ctxt
~chain
~block:(`Hash (head, 0))
()
in
let head_level = head_shell.Block_header.level in
return Int32.(to_int (sub head_level branch_level))
| None ->
let* =
Shell_services.Blocks.Header.shell_header ctxt ~chain ()
in
let head_level = Int32.to_int head_header.level in
let block_hook = min predecessors (head_level - 1) in
assert (head_level - (block_hook + 1) >= 0) ;
return block_hook
in
let* oldest =
Block_services.Empty.hash
ctxt
~chain
~block:(`Hash (head, block_hook + 1))
()
in
Block_hash.Table.add blocks oldest None ;
loop block_hook
let lookup_operation_in_previous_block ctxt chain operation_hash i =
let open Lwt_result_syntax in
let* block = Block_services.Empty.hash ctxt ~block:(`Head i) () in
let* operations =
Shell_services.Blocks.Operation_hashes.operation_hashes
ctxt
~chain
~block:(`Hash (block, 0))
()
in
match in_block operation_hash operations with
| None -> return_none
| Some (a, b) -> return_some (block, a, b)
let lookup_operation_in_previous_blocks (ctxt : #Client_context.full) ~chain
~predecessors operation_hash =
let open Lwt_result_syntax in
let rec loop i =
if i = predecessors + 1 then return_none
else
let* o = lookup_operation_in_previous_block ctxt chain operation_hash i in
match o with
| None -> loop (i + 1)
| Some (block, a, b) -> return_some (block, a, b)
in
loop 0
let wait_for_bootstrapped ?(retry = fun f x -> f x)
(ctxt : #Client_context.full) =
let open Lwt_result_syntax in
let display = ref false in
Lwt.dont_wait
(fun () ->
let*! () = ctxt#sleep 0.3 in
if not !display then (
let*! () = ctxt#answer "Waiting for the node to be bootstrapped..." in
display := true ;
Lwt.return_unit)
else Lwt.return_unit)
(fun exc ->
let (_ : unit Lwt.t) =
let*! () =
ctxt#error "Uncaught exception: %s\n%!" (Printexc.to_string exc)
in
ctxt#error "Progress not monitored anymore\n%!"
in
()) ;
let* stream, _stop = retry Monitor_services.bootstrapped ctxt in
let*! () =
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.System.pp_hum
(Time.System.of_protocol_exn time)
Time.System.pp_hum
(ctxt#now ())
else Lwt.return_unit)
stream
in
display := true ;
let*! () = ctxt#answer "Node is bootstrapped." in
return_unit