2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2016. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
open Tezos_hash
|
|
|
|
|
|
|
|
(** Block header *)
|
|
|
|
|
|
|
|
(** Exported type *)
|
2017-04-20 17:21:10 +04:00
|
|
|
type t = {
|
2017-04-19 21:21:23 +04:00
|
|
|
shell: Block_header.shell_header ;
|
2016-09-08 21:13:10 +04:00
|
|
|
proto: proto_header ;
|
2017-02-28 05:56:40 +04:00
|
|
|
signature: Ed25519.Signature.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
and proto_header = {
|
2017-04-10 15:01:22 +04:00
|
|
|
priority: int ;
|
2016-09-08 21:13:10 +04:00
|
|
|
seed_nonce_hash: Nonce_hash.t ;
|
2017-04-02 18:07:19 +04:00
|
|
|
proof_of_work_nonce: MBytes.t ;
|
|
|
|
}
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-04-20 17:21:10 +04:00
|
|
|
type block_header = t
|
|
|
|
|
|
|
|
type raw = Tezos_data.Block_header.t
|
|
|
|
type shell_header = Tezos_data.Block_header.shell_header
|
|
|
|
|
|
|
|
let raw_encoding = Tezos_data.Block_header.encoding
|
|
|
|
let shell_header_encoding = Tezos_data.Block_header.shell_header_encoding
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
let proto_header_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
2017-04-10 15:01:22 +04:00
|
|
|
(fun { priority ; seed_nonce_hash ; proof_of_work_nonce } ->
|
|
|
|
(priority, seed_nonce_hash, proof_of_work_nonce))
|
|
|
|
(fun (priority, seed_nonce_hash, proof_of_work_nonce) ->
|
|
|
|
{ priority ; seed_nonce_hash ; proof_of_work_nonce })
|
|
|
|
(obj3
|
|
|
|
(req "priority" uint16)
|
|
|
|
(req "seed_nonce_hash" Nonce_hash.encoding)
|
|
|
|
(req "proof_of_work_nonce"
|
|
|
|
(Fixed.bytes Constants_repr.proof_of_work_nonce_size)))
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let signed_proto_header_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
merge_objs
|
|
|
|
proto_header_encoding
|
2017-02-28 05:56:40 +04:00
|
|
|
(obj1 (req "signature" Ed25519.Signature.encoding))
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let unsigned_header_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
merge_objs
|
2017-04-19 21:21:23 +04:00
|
|
|
Block_header.shell_header_encoding
|
2016-09-08 21:13:10 +04:00
|
|
|
proto_header_encoding
|
|
|
|
|
2017-04-20 17:21:10 +04:00
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
|
|
|
(fun { shell ; proto ; signature } ->
|
|
|
|
(shell, (proto, signature)))
|
|
|
|
(fun (shell, (proto, signature)) ->
|
|
|
|
{ shell ; proto ; signature })
|
|
|
|
(merge_objs
|
|
|
|
Block_header.shell_header_encoding
|
|
|
|
signed_proto_header_encoding)
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
(** Constants *)
|
|
|
|
|
|
|
|
let max_header_length =
|
|
|
|
match Data_encoding.classify signed_proto_header_encoding with
|
|
|
|
| `Fixed n -> n
|
|
|
|
| `Dynamic | `Variable -> assert false
|
|
|
|
|
|
|
|
(** Header parsing entry point *)
|
|
|
|
|
|
|
|
type error +=
|
|
|
|
| Cant_parse_proto_header
|
|
|
|
|
2017-04-20 17:21:10 +04:00
|
|
|
let parse
|
2017-04-12 20:22:40 +04:00
|
|
|
({ shell = { net_id ; level ; proto_level ; predecessor ;
|
2017-09-29 20:43:13 +04:00
|
|
|
timestamp ; fitness ; validation_passes ; operations_hash } ;
|
2017-04-20 17:21:10 +04:00
|
|
|
proto } : Block_header.t) : block_header tzresult =
|
2016-09-08 21:13:10 +04:00
|
|
|
match Data_encoding.Binary.of_bytes signed_proto_header_encoding proto with
|
|
|
|
| None -> Error [Cant_parse_proto_header]
|
|
|
|
| Some (proto, signature) ->
|
|
|
|
let shell =
|
2017-04-19 21:21:23 +04:00
|
|
|
{ Block_header.net_id ; level ; proto_level ; predecessor ;
|
2017-09-29 20:43:13 +04:00
|
|
|
timestamp ; fitness ; validation_passes ; operations_hash } in
|
2016-09-08 21:13:10 +04:00
|
|
|
Ok { shell ; proto ; signature }
|
|
|
|
|
2017-04-26 17:01:39 +04:00
|
|
|
let parse_unsigned_proto_header bytes =
|
|
|
|
match Data_encoding.Binary.of_bytes proto_header_encoding bytes with
|
|
|
|
| None -> Error [Cant_parse_proto_header]
|
|
|
|
| Some proto -> Ok proto
|
|
|
|
|
2017-04-20 17:21:10 +04:00
|
|
|
let forge_unsigned shell proto =
|
2016-09-08 21:13:10 +04:00
|
|
|
Data_encoding.Binary.to_bytes unsigned_header_encoding (shell, proto)
|
2017-04-20 17:21:10 +04:00
|
|
|
|
2017-04-26 17:01:39 +04:00
|
|
|
let forge_unsigned_proto_header proto =
|
|
|
|
Data_encoding.Binary.to_bytes proto_header_encoding proto
|
|
|
|
|
2017-04-20 17:21:10 +04:00
|
|
|
let hash_raw = Block_header.hash
|
|
|
|
let hash { shell ; proto ; signature } =
|
|
|
|
Block_header.hash
|
|
|
|
{ shell ;
|
|
|
|
proto =
|
|
|
|
Data_encoding.Binary.to_bytes
|
|
|
|
signed_proto_header_encoding
|
|
|
|
(proto, signature ) }
|