ligo/src/proto/alpha/block_repr.ml

90 lines
2.8 KiB
OCaml
Raw Normal View History

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 *)
type header = {
2016-10-19 22:47:04 +04:00
shell: Updater.shell_block ;
2016-09-08 21:13:10 +04:00
proto: proto_header ;
signature: Ed25519.Signature.t ;
2016-09-08 21:13:10 +04:00
}
and proto_header = {
mining_slot: mining_slot ;
seed_nonce_hash: Nonce_hash.t ;
proof_of_work_nonce: MBytes.t ;
}
2016-09-08 21:13:10 +04:00
and mining_slot = {
level: Raw_level_repr.t ;
priority: int ;
}
2016-09-08 21:13:10 +04:00
let mining_slot_encoding =
let open Data_encoding in
conv
(fun { level ; priority } -> level, priority)
(fun (level, priority) -> { level ; priority })
(obj2
(req "level" Raw_level_repr.encoding)
(req "priority" uint16))
2016-09-08 21:13:10 +04:00
let proto_header_encoding =
let open Data_encoding in
conv
(fun { mining_slot ; seed_nonce_hash ; proof_of_work_nonce } ->
(mining_slot, (seed_nonce_hash, proof_of_work_nonce)))
(fun (mining_slot, (seed_nonce_hash, proof_of_work_nonce)) ->
2016-09-08 21:13:10 +04:00
{ mining_slot ; seed_nonce_hash ; proof_of_work_nonce })
(merge_objs
mining_slot_encoding
(obj2
(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
(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
2016-10-19 22:47:04 +04:00
Updater.shell_block_encoding
2016-09-08 21:13:10 +04:00
proto_header_encoding
(** 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
let parse_header
({ shell = { net_id ; predecessor ; timestamp ; fitness ; operations } ;
2016-10-19 22:47:04 +04:00
proto } : Updater.raw_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 =
{ Updater.net_id ; predecessor ; timestamp ; fitness ; operations } in
Ok { shell ; proto ; signature }
let forge_header shell proto =
Data_encoding.Binary.to_bytes unsigned_header_encoding (shell, proto)