Source file block_header_repr.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
(** Block header *)
type contents = {
payload_hash : Block_payload_hash.t;
payload_round : Round_repr.t;
seed_nonce_hash : Nonce_hash.t option;
proof_of_work_nonce : bytes;
per_block_votes : Per_block_votes_repr.per_block_votes;
}
type protocol_data = {contents : contents; signature : Signature.t}
type t = {shell : Block_header.shell_header; protocol_data : protocol_data}
type raw = Block_header.t
let raw_encoding = Block_header.encoding
let = Block_header.shell_header_encoding
type block_watermark =
let bytes_of_block_watermark = function
| Block_header chain_id ->
Bytes.cat (Bytes.of_string "\x11") (Chain_id.to_bytes chain_id)
let to_watermark b = Signature.Custom (bytes_of_block_watermark b)
let of_watermark = function
| Signature.Custom b ->
if Compare.Int.(Bytes.length b > 0) then
match Bytes.get b 0 with
| '\x11' ->
Option.map
(fun chain_id -> Block_header chain_id)
(Chain_id.of_bytes_opt (Bytes.sub b 1 (Bytes.length b - 1)))
| _ -> None
else None
| _ -> None
let contents_encoding =
let open Data_encoding in
let json =
conv
(fun {
payload_hash;
payload_round;
seed_nonce_hash;
proof_of_work_nonce;
per_block_votes = {liquidity_baking_vote; adaptive_issuance_vote};
} ->
( payload_hash,
payload_round,
proof_of_work_nonce,
seed_nonce_hash,
liquidity_baking_vote,
adaptive_issuance_vote ))
(fun ( payload_hash,
payload_round,
proof_of_work_nonce,
seed_nonce_hash,
liquidity_baking_vote,
adaptive_issuance_vote ) ->
{
payload_hash;
payload_round;
seed_nonce_hash;
proof_of_work_nonce;
per_block_votes = {liquidity_baking_vote; adaptive_issuance_vote};
})
(obj6
(req "payload_hash" Block_payload_hash.encoding)
(req "payload_round" Round_repr.encoding)
(req
"proof_of_work_nonce"
(Fixed.bytes Hex Constants_repr.proof_of_work_nonce_size))
(opt "seed_nonce_hash" Nonce_hash.encoding)
(req
"liquidity_baking_toggle_vote"
Per_block_votes_repr.liquidity_baking_vote_encoding)
(req
"adaptive_issuance_vote"
Per_block_votes_repr.adaptive_issuance_vote_encoding))
in
let binary =
conv
(fun {
payload_hash;
payload_round;
seed_nonce_hash;
proof_of_work_nonce;
per_block_votes;
} ->
( payload_hash,
payload_round,
proof_of_work_nonce,
seed_nonce_hash,
per_block_votes ))
(fun ( payload_hash,
payload_round,
proof_of_work_nonce,
seed_nonce_hash,
per_block_votes ) ->
{
payload_hash;
payload_round;
seed_nonce_hash;
proof_of_work_nonce;
per_block_votes;
})
(obj5
(req "payload_hash" Block_payload_hash.encoding)
(req "payload_round" Round_repr.encoding)
(req
"proof_of_work_nonce"
(Fixed.bytes Hex Constants_repr.proof_of_work_nonce_size))
(opt "seed_nonce_hash" Nonce_hash.encoding)
(req "per_block_votes" Per_block_votes_repr.per_block_votes_encoding))
in
def "block_header.alpha.unsigned_contents" @@ splitted ~binary ~json
let protocol_data_encoding =
let open Data_encoding in
def "block_header.alpha.signed_contents"
@@ conv
(fun {contents; signature} -> (contents, signature))
(fun (contents, signature) -> {contents; signature})
(merge_objs
contents_encoding
(obj1 (req "signature" Signature.encoding)))
let raw {shell; protocol_data} =
let protocol_data =
Data_encoding.Binary.to_bytes_exn protocol_data_encoding protocol_data
in
{Block_header.shell; protocol_data}
let unsigned_encoding =
let open Data_encoding in
merge_objs Block_header.shell_header_encoding contents_encoding
let encoding =
let open Data_encoding in
def "block_header.alpha.full_header"
@@ conv
(fun {shell; protocol_data} -> (shell, protocol_data))
(fun (shell, protocol_data) -> {shell; protocol_data})
(merge_objs Block_header.shell_header_encoding protocol_data_encoding)
(** Constants *)
let =
let fake_level = Raw_level_repr.root in
let fake_round = Round_repr.zero in
let fake_fitness =
Fitness_repr.create_without_locked_round
~level:fake_level
~predecessor_round:fake_round
~round:fake_round
in
let fake_shell =
{
Block_header.level = 0l;
proto_level = 0;
predecessor = Block_hash.zero;
timestamp = Time.of_seconds 0L;
validation_passes = 0;
operations_hash = Operation_list_list_hash.zero;
fitness = Fitness_repr.to_raw fake_fitness;
context = Context_hash.zero;
}
and fake_contents =
{
payload_hash = Block_payload_hash.zero;
payload_round = Round_repr.zero;
proof_of_work_nonce =
Bytes.make Constants_repr.proof_of_work_nonce_size '0';
seed_nonce_hash = Some Nonce_hash.zero;
per_block_votes =
{
liquidity_baking_vote = Per_block_vote_pass;
adaptive_issuance_vote = Per_block_vote_pass;
};
}
in
Data_encoding.Binary.length
encoding
{
shell = fake_shell;
protocol_data = {contents = fake_contents; signature = Signature.zero};
}
(** Header parsing entry point *)
let hash_raw = Block_header.hash
let hash {shell; protocol_data} =
Block_header.hash
{
shell;
protocol_data =
Data_encoding.Binary.to_bytes_exn protocol_data_encoding protocol_data;
}
type error +=
|
Invalid_block_signature of
Block_hash.t * Signature.Public_key_hash.t
| Invalid_stamp
|
Invalid_payload_round of {
payload_round : Round_repr.t;
round : Round_repr.t;
}
| Invalid_commitment of {expected : bool}
| Wrong_timestamp of Time.t * Time.t
let () =
register_error_kind
`Permanent
~id:"block_header.invalid_block_signature"
~title:"Invalid block signature"
~description:"A block was not signed with the expected private key."
~pp:(fun ppf (block, pkh) ->
Format.fprintf
ppf
"Invalid signature for block %a. Expected: %a."
Block_hash.pp_short
block
Signature.Public_key_hash.pp_short
pkh)
Data_encoding.(
obj2
(req "block" Block_hash.encoding)
(req "expected" Signature.Public_key_hash.encoding))
(function
| Invalid_block_signature (block, pkh) -> Some (block, pkh) | _ -> None)
(fun (block, pkh) -> Invalid_block_signature (block, pkh)) ;
register_error_kind
`Permanent
~id:"block_header.invalid_stamp"
~title:"Insufficient block proof-of-work stamp"
~description:"The block's proof-of-work stamp is insufficient"
~pp:(fun ppf () -> Format.fprintf ppf "Insufficient proof-of-work stamp")
Data_encoding.empty
(function Invalid_stamp -> Some () | _ -> None)
(fun () -> Invalid_stamp) ;
register_error_kind
`Permanent
~id:"block_header.invalid_payload_round"
~title:"Invalid payload round"
~description:"The given payload round is invalid."
~pp:(fun ppf (payload_round, round) ->
Format.fprintf
ppf
"The provided payload round (%a) is after the block round (%a)."
Round_repr.pp
payload_round
Round_repr.pp
round)
Data_encoding.(
obj2
(req "payload_round" Round_repr.encoding)
(req "round" Round_repr.encoding))
(function
| Invalid_payload_round {payload_round; round} ->
Some (payload_round, round)
| _ -> None)
(fun (payload_round, round) -> Invalid_payload_round {payload_round; round}) ;
register_error_kind
`Permanent
~id:"block_header.invalid_commitment"
~title:"Invalid commitment in block header"
~description:"The block header has invalid commitment."
~pp:(fun ppf expected ->
if expected then
Format.fprintf ppf "Missing seed's nonce commitment in block header."
else
Format.fprintf ppf "Unexpected seed's nonce commitment in block header.")
Data_encoding.(obj1 (req "expected" bool))
(function Invalid_commitment {expected} -> Some expected | _ -> None)
(fun expected -> Invalid_commitment {expected}) ;
register_error_kind
`Permanent
~id:"block_header.wrong_timestamp"
~title:"Wrong timestamp"
~description:"Block timestamp not the expected one."
~pp:(fun ppf (block_ts, expected_ts) ->
Format.fprintf
ppf
"Wrong timestamp: block timestamp (%a) not the expected one (%a)"
Time.pp_hum
block_ts
Time.pp_hum
expected_ts)
Data_encoding.(
obj2
(req "block_timestamp" Time.encoding)
(req "expected_timestamp" Time.encoding))
(function Wrong_timestamp (t1, t2) -> Some (t1, t2) | _ -> None)
(fun (t1, t2) -> Wrong_timestamp (t1, t2))
let check_signature (block : t) (chain_id : Chain_id.t)
(key : Signature.Public_key.t) =
let open Result_syntax in
let check_signature key ({shell; protocol_data = {contents; signature}} : t) =
let =
Data_encoding.Binary.to_bytes_exn unsigned_encoding (shell, contents)
in
Signature.check
~watermark:(to_watermark (Block_header chain_id))
key
signature
unsigned_header
in
if check_signature key block then return_unit
else
tzfail (Invalid_block_signature (hash block, Signature.Public_key.hash key))
let check_payload_round ~round ~payload_round =
error_when
Round_repr.(payload_round > round)
(Invalid_payload_round {payload_round; round})
let check_timestamp round_durations ~timestamp ~round ~predecessor_timestamp
~predecessor_round =
let open Result_syntax in
let* expected_timestamp =
Round_repr.timestamp_of_round
round_durations
~predecessor_timestamp
~predecessor_round
~round
in
if Time_repr.(expected_timestamp = timestamp) then return_unit
else tzfail (Wrong_timestamp (timestamp, expected_timestamp))
module Proof_of_work = struct
let check_hash hash stamp_threshold =
let bytes = Block_hash.to_bytes hash in
let word = TzEndian.get_int64 bytes 0 in
Compare.Uint64.(word <= stamp_threshold)
let shell contents stamp_threshold =
let hash =
hash {shell; protocol_data = {contents; signature = Signature.zero}}
in
check_hash hash stamp_threshold
let check_proof_of_work_stamp ~proof_of_work_threshold block =
let open Result_syntax in
if
check_header_proof_of_work_stamp
block.shell
block.protocol_data.contents
proof_of_work_threshold
then return_unit
else tzfail Invalid_stamp
end
let ~( : t) ~(chain_id : Chain_id.t)
~(predecessor_timestamp : Time.t) ~(predecessor_round : Round_repr.t)
~(fitness : Fitness_repr.t) ~(timestamp : Time.t)
~(delegate_pk : Signature.Public_key.t)
~(round_durations : Round_repr.Durations.t)
~(proof_of_work_threshold : int64) ~(expected_commitment : bool) =
let open Result_syntax in
let {payload_round; seed_nonce_hash; _} =
block_header.protocol_data.contents
in
let raw_level = block_header.shell.level in
let* () =
Proof_of_work.check_proof_of_work_stamp
~proof_of_work_threshold
block_header
in
let* level = Raw_level_repr.of_int32 raw_level in
let* () = check_signature block_header chain_id delegate_pk in
let round = Fitness_repr.round fitness in
let* () = check_payload_round ~round ~payload_round in
let* () =
check_timestamp
round_durations
~predecessor_timestamp
~predecessor_round
~timestamp
~round
in
let* () =
Fitness_repr.check_except_locked_round fitness ~level ~predecessor_round
in
let has_commitment =
match seed_nonce_hash with None -> false | Some _ -> true
in
error_unless
Compare.Bool.(has_commitment = expected_commitment)
(Invalid_commitment {expected = expected_commitment})