Source file client_baking_blocks.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
open Protocol
open Alpha_context
open Protocol_client_context
type block_info = {
hash : Block_hash.t;
chain_id : Chain_id.t;
predecessor : Block_hash.t;
fitness : Bytes.t list;
timestamp : Time.Protocol.t;
protocol : Protocol_hash.t;
next_protocol : Protocol_hash.t;
proto_level : int;
level : Raw_level.t;
context : Context_hash.t;
}
let raw_info cctxt ?(chain = `Main) hash =
let open Lwt_result_syntax in
let block = `Hash (hash, 0) in
let* chain_id = Shell_services.Chain.chain_id cctxt ~chain () in
let* {current_protocol = protocol; next_protocol} =
Shell_services.Blocks.protocols cctxt ~chain ~block ()
in
let {
Tezos_base.Block_header.predecessor;
fitness;
timestamp;
level;
context;
proto_level;
_;
} =
shell_header
in
match Raw_level.of_int32 level with
| Ok level ->
return
{
hash;
chain_id;
predecessor;
fitness;
timestamp;
protocol;
next_protocol;
proto_level;
level;
context;
}
| Error _ -> failwith "Cannot convert level into int32"
let info cctxt ?(chain = `Main) block =
let open Lwt_result_syntax in
let* hash = Shell_services.Blocks.hash cctxt ~chain ~block () in
let* =
Shell_services.Blocks.Header.shell_header cctxt ~chain ~block ()
in
raw_info cctxt ~chain hash shell_header
module Block_seen_event = struct
type t = {
hash : Block_hash.t;
header : Tezos_base.Block_header.t;
occurrence : [`Valid_blocks of Chain_id.t | `Heads];
}
let make hash occurrence = {hash; header; occurrence}
module Definition = struct
let section = None
let name = "block-seen-" ^ Protocol.name
type nonrec t = t
let encoding =
let open Data_encoding in
let v0_encoding =
conv
(function {hash; ; occurrence} -> (hash, occurrence, header))
(fun (hash, occurrence, ) -> make hash header occurrence)
(obj3
(req "hash" Block_hash.encoding)
(req
"occurrence"
(union
[
case
~title:"heads"
(Tag 0)
(obj1 (req "occurrence-kind" (constant "heads")))
(function `Heads -> Some () | _ -> None)
(fun () -> `Heads);
case
~title:"valid-blocks"
(Tag 1)
(obj2
(req "occurrence-kind" (constant "valid-blocks"))
(req "chain-id" Chain_id.encoding))
(function
| `Valid_blocks ch -> Some ((), ch) | _ -> None)
(fun ((), ch) -> `Valid_blocks ch);
]))
(req "header" Tezos_base.Block_header.encoding))
in
With_version.(encoding ~name (first_version v0_encoding))
let pp ~all_fields:_ ~block:_ ppf {hash; _} =
Format.fprintf ppf "Saw block %a" Block_hash.pp_short hash
let doc = "Block observed while monitoring a blockchain."
let level = Internal_event.Info
end
module Event = Internal_event.Make (Definition)
end
let monitor_applied_blocks cctxt ?chains ?protocols ~next_protocols () =
let open Lwt_result_syntax in
let* block_stream, stop =
Monitor_services.applied_blocks cctxt ?chains ?protocols ?next_protocols ()
in
return
( Lwt_stream.map_s
(fun (chain, block, , _ops) ->
let* () =
Block_seen_event.(
Event.emit (make block header (`Valid_blocks chain)))
in
raw_info
cctxt
~chain:(`Hash chain)
block
header.Tezos_base.Block_header.shell)
block_stream,
stop )
let monitor_heads cctxt ~next_protocols chain =
let open Lwt_result_syntax in
let* block_stream, stop =
Monitor_services.heads cctxt ?next_protocols chain
in
return
( Lwt_stream.map_s
(fun (block, ({Tezos_base.Block_header.shell; _} as )) ->
let* () = Block_seen_event.(Event.emit (make block header `Heads)) in
raw_info cctxt ~chain block shell)
block_stream,
stop )
type error +=
| Unexpected_empty_block_list of {
chain : string;
block_hash : Block_hash.t;
length : int;
}
let () =
register_error_kind
`Permanent
~id:"Client_baking_blocks.unexpected_empty_block_list"
~title:"Unexpected empty blocklist"
~description:
"The block list retrieved by Shell_services.Blocks.list is empty"
~pp:(fun ppf (chain, block_hash, length) ->
Format.fprintf
ppf
"Unexpected empty block list retrieved from chain %s at block %a, \
length %d"
chain
Block_hash.pp
block_hash
length)
Data_encoding.(
obj3
(req "chain" string)
(req "block_hash" Block_hash.encoding)
(req "length" int31))
(function
| Unexpected_empty_block_list {chain; block_hash; length} ->
Some (chain, block_hash, length)
| _ -> None)
(fun (chain, block_hash, length) ->
Unexpected_empty_block_list {chain; block_hash; length})
let blocks_from_current_cycle cctxt ?(chain = `Main) block ?(offset = 0l) () =
let open Lwt_result_syntax in
let* hash = Shell_services.Blocks.hash cctxt ~chain ~block () in
let* {level; _} =
Shell_services.Blocks.Header.shell_header cctxt ~chain ~block ()
in
let*! result =
Plugin.RPC.levels_in_current_cycle cctxt ~offset (chain, block)
in
match result with
| Error (Tezos_rpc.Context.Not_found _ :: _) -> return_nil
| Error _ as err -> Lwt.return err
| Ok (first, last) ->
let length = Int32.to_int (Int32.sub level (Raw_level.to_int32 first)) in
let* head =
let* list =
Shell_services.Blocks.list cctxt ~chain ~heads:[hash] ~length ()
in
match list with
| hd :: _ -> return hd
| [] ->
tzfail
(Unexpected_empty_block_list
{
chain = Block_services.chain_to_string chain;
block_hash = hash;
length;
})
in
let blocks =
List.drop_n (length - Int32.to_int (Raw_level.diff last first)) head
in
if Int32.equal level (Raw_level.to_int32 last) then return (hash :: blocks)
else return blocks