2018-06-29 16:08:08 +04:00
|
|
|
(*****************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Open Source License *)
|
|
|
|
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* Permission is hereby granted, free of charge, to any person obtaining a *)
|
|
|
|
(* copy of this software and associated documentation files (the "Software"),*)
|
|
|
|
(* to deal in the Software without restriction, including without limitation *)
|
|
|
|
(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)
|
|
|
|
(* and/or sell copies of the Software, and to permit persons to whom the *)
|
|
|
|
(* Software is furnished to do so, subject to the following conditions: *)
|
|
|
|
(* *)
|
|
|
|
(* The above copyright notice and this permission notice shall be included *)
|
|
|
|
(* in all copies or substantial portions of the Software. *)
|
|
|
|
(* *)
|
|
|
|
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
|
|
|
|
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)
|
|
|
|
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)
|
|
|
|
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
|
|
|
|
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)
|
|
|
|
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)
|
|
|
|
(* DEALINGS IN THE SOFTWARE. *)
|
|
|
|
(* *)
|
|
|
|
(*****************************************************************************)
|
2017-09-29 20:43:13 +04:00
|
|
|
|
2018-01-22 22:25:07 +04:00
|
|
|
open Lwt.Infix
|
|
|
|
|
2017-12-17 22:51:06 +04:00
|
|
|
type t = raw
|
|
|
|
and raw = Block_header.t * Block_hash.t list
|
2017-09-29 20:43:13 +04:00
|
|
|
|
2017-12-17 22:51:06 +04:00
|
|
|
let raw x = x
|
2017-09-29 20:43:13 +04:00
|
|
|
|
2018-02-19 21:25:57 +04:00
|
|
|
let pp ppf (hd, h_lst) =
|
|
|
|
let repeats = 10 in
|
|
|
|
let coef = 2 in
|
|
|
|
(* list of hashes *)
|
|
|
|
let rec pp_hash_list ppf (h_lst , acc , d , r) =
|
|
|
|
match h_lst with
|
|
|
|
| [] ->
|
|
|
|
Format.fprintf ppf ""
|
|
|
|
| hd :: tl ->
|
|
|
|
let new_d = if r > 1 then d else d * coef in
|
|
|
|
let new_r = if r > 1 then r - 1 else repeats in
|
2018-03-21 18:38:41 +04:00
|
|
|
Format.fprintf ppf "%a (%i)\n%a"
|
|
|
|
Block_hash.pp hd acc pp_hash_list (tl , acc - d , new_d , new_r) in
|
2018-02-19 21:25:57 +04:00
|
|
|
Format.fprintf ppf "%a (head)\n%a"
|
|
|
|
Block_hash.pp (Block_header.hash hd)
|
|
|
|
pp_hash_list (h_lst , -1, 1, repeats - 1)
|
|
|
|
|
|
|
|
let pp_short ppf (hd, h_lst) =
|
|
|
|
Format.fprintf ppf "head: %a, %d predecessors"
|
|
|
|
Block_hash.pp (Block_header.hash hd)
|
|
|
|
(List.length h_lst)
|
2018-02-13 17:12:09 +04:00
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
(* TODO add a [description] *)
|
2017-11-11 06:34:12 +04:00
|
|
|
(obj2
|
|
|
|
(req "current_head" (dynamic_size Block_header.encoding))
|
2018-06-02 17:49:46 +04:00
|
|
|
(req "history" (Variable.list Block_hash.encoding)))
|
|
|
|
|
|
|
|
let bounded_encoding ?max_header_size ?max_length () =
|
|
|
|
let open Data_encoding in
|
|
|
|
(* TODO add a [description] *)
|
|
|
|
(obj2
|
|
|
|
(req "current_head"
|
|
|
|
(dynamic_size
|
|
|
|
(Block_header.bounded_encoding ?max_size:max_header_size ())))
|
|
|
|
(req "history" (Variable.list ?max_length Block_hash.encoding)))
|
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
|
2018-03-21 18:38:41 +04:00
|
|
|
type seed = {
|
|
|
|
sender_id: P2p_peer.Id.t ;
|
|
|
|
receiver_id: P2p_peer.Id.t ;
|
|
|
|
}
|
|
|
|
|
|
|
|
(* Random generator for locator steps.
|
|
|
|
|
|
|
|
We draw steps by sequence of 10. The first sequence's steps are of
|
|
|
|
length 1 (consecutive). The second sequence's steps are of a random
|
|
|
|
length between 1 and 2. The third sequence's steps are of a random
|
|
|
|
length between 2 and 4, and so on...
|
|
|
|
|
|
|
|
The sequence is deterministic for a given triple of sender,
|
|
|
|
receiver and block hash. *)
|
|
|
|
module Step : sig
|
|
|
|
|
|
|
|
type state
|
|
|
|
val init: seed -> Block_hash.t -> state
|
|
|
|
val next: state -> int * state
|
|
|
|
|
|
|
|
end = struct
|
|
|
|
|
2018-06-06 12:34:09 +04:00
|
|
|
type state = Int32.t * int * MBytes.t
|
2018-03-21 18:38:41 +04:00
|
|
|
|
|
|
|
let init seed head =
|
2018-05-05 02:27:15 +04:00
|
|
|
let open Hacl.Hash in
|
|
|
|
let st = SHA256.init () in
|
|
|
|
List.iter (SHA256.update st) [
|
|
|
|
P2p_peer.Id.to_bytes seed.sender_id ;
|
|
|
|
P2p_peer.Id.to_bytes seed.receiver_id ;
|
|
|
|
Block_hash.to_bytes head ] ;
|
2018-06-06 12:34:09 +04:00
|
|
|
(1l, 9, SHA256.finish st)
|
2018-03-21 18:38:41 +04:00
|
|
|
|
|
|
|
let draw seed n =
|
2018-06-06 12:34:09 +04:00
|
|
|
Int32.rem (MBytes.get_int32 seed 0) n,
|
2018-05-05 02:27:15 +04:00
|
|
|
Hacl.Hash.SHA256.digest seed
|
2018-03-21 18:38:41 +04:00
|
|
|
|
|
|
|
let next (step, counter, seed) =
|
|
|
|
let random_gap, seed =
|
2018-06-06 12:34:09 +04:00
|
|
|
if step <= 1l then
|
|
|
|
0l, seed
|
2018-03-21 18:38:41 +04:00
|
|
|
else
|
2018-06-06 12:34:09 +04:00
|
|
|
draw seed (Int32.succ (Int32.div step 2l)) in
|
2018-03-21 18:38:41 +04:00
|
|
|
let new_state =
|
|
|
|
if counter = 0 then
|
2018-06-06 12:34:09 +04:00
|
|
|
(Int32.mul step 2l, 9, seed)
|
2018-01-25 15:00:42 +04:00
|
|
|
else
|
2018-03-21 18:38:41 +04:00
|
|
|
(step, counter - 1, seed) in
|
2018-06-06 12:34:09 +04:00
|
|
|
Int32.to_int (Int32.sub step random_gap), new_state
|
2018-03-21 18:38:41 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
let estimated_length seed (head, hist) =
|
|
|
|
let rec loop acc state = function
|
|
|
|
| [] -> acc
|
|
|
|
| _ :: hist ->
|
|
|
|
let step, state = Step.next state in
|
|
|
|
loop (acc + step) state hist in
|
|
|
|
let state = Step.init seed (Block_header.hash head) in
|
|
|
|
let step, state = Step.next state in
|
|
|
|
loop step state hist
|
|
|
|
|
|
|
|
let fold ~f ~init (head, hist) seed =
|
|
|
|
let rec loop state acc = function
|
|
|
|
| [] | [_] -> acc
|
|
|
|
| block :: (pred :: rem as hist) ->
|
|
|
|
let step, state = Step.next state in
|
|
|
|
let acc = f acc ~block ~pred ~step ~strict_step:(rem <> []) in
|
|
|
|
loop state acc hist in
|
|
|
|
let head = Block_header.hash head in
|
|
|
|
let state = Step.init seed head in
|
|
|
|
loop state init (head :: hist)
|
|
|
|
|
|
|
|
type step = {
|
|
|
|
block: Block_hash.t ;
|
|
|
|
predecessor: Block_hash.t ;
|
|
|
|
step: int ;
|
|
|
|
strict_step: bool ;
|
|
|
|
}
|
|
|
|
|
2018-06-12 23:07:50 +04:00
|
|
|
let pp_step ppf step = Format.fprintf ppf "%d%s" step.step (if step.strict_step then "" else " max")
|
|
|
|
|
2018-03-21 18:38:41 +04:00
|
|
|
let to_steps seed locator =
|
|
|
|
fold locator seed
|
|
|
|
~init:[]
|
|
|
|
~f: begin fun acc ~block ~pred ~step ~strict_step ->
|
|
|
|
{ block ; predecessor = pred ; step ; strict_step } :: acc
|
|
|
|
end
|
|
|
|
|
|
|
|
let compute ~predecessor ~genesis block_hash header seed ~size =
|
|
|
|
let rec loop acc size state block =
|
2018-03-12 20:11:30 +04:00
|
|
|
if size = 0 then
|
2018-03-21 18:38:41 +04:00
|
|
|
Lwt.return (List.rev acc)
|
2018-03-12 20:11:30 +04:00
|
|
|
else
|
2018-03-21 18:38:41 +04:00
|
|
|
let step, state = Step.next state in
|
|
|
|
predecessor block step >>= function
|
|
|
|
| None ->
|
|
|
|
(* We reached genesis before size *)
|
|
|
|
if Block_hash.equal block genesis then
|
|
|
|
Lwt.return (List.rev acc)
|
|
|
|
else
|
|
|
|
Lwt.return (List.rev (genesis :: acc))
|
|
|
|
| Some pred ->
|
|
|
|
loop (pred :: acc) (size - 1) state pred in
|
|
|
|
if size <= 0 then
|
|
|
|
Lwt.return (header, [])
|
|
|
|
else
|
|
|
|
let state = Step.init seed block_hash in
|
|
|
|
let step, state = Step.next state in
|
|
|
|
predecessor block_hash step >>= function
|
|
|
|
| None -> Lwt.return (header, [])
|
|
|
|
| Some p ->
|
|
|
|
loop [p] (size-1) state p >>= fun hist ->
|
|
|
|
Lwt.return (header, hist)
|
2017-09-29 20:43:13 +04:00
|
|
|
|
2017-12-17 22:51:06 +04:00
|
|
|
type validity =
|
|
|
|
| Unknown
|
|
|
|
| Known_valid
|
|
|
|
| Known_invalid
|
2017-09-29 20:43:13 +04:00
|
|
|
|
2018-03-21 18:38:41 +04:00
|
|
|
let unknown_prefix ~is_known (head, hist) =
|
2017-12-17 22:51:06 +04:00
|
|
|
let rec loop hist acc =
|
|
|
|
match hist with
|
|
|
|
| [] -> Lwt.return_none
|
|
|
|
| h :: t ->
|
2018-03-21 18:38:41 +04:00
|
|
|
is_known h >>= function
|
2017-12-17 22:51:06 +04:00
|
|
|
| Known_valid ->
|
|
|
|
Lwt.return_some (h, (List.rev (h :: acc)))
|
|
|
|
| Known_invalid ->
|
|
|
|
Lwt.return_none
|
|
|
|
| Unknown ->
|
|
|
|
loop t (h :: acc)
|
2017-09-29 20:43:13 +04:00
|
|
|
in
|
2018-03-21 18:38:41 +04:00
|
|
|
is_known (Block_header.hash head) >>= function
|
2017-12-17 22:51:06 +04:00
|
|
|
| Known_valid ->
|
|
|
|
Lwt.return_some (Block_header.hash head, (head, []))
|
|
|
|
| Known_invalid ->
|
|
|
|
Lwt.return_none
|
|
|
|
| Unknown ->
|
|
|
|
loop hist [] >>= function
|
2017-09-29 20:43:13 +04:00
|
|
|
| None ->
|
2017-12-17 22:51:06 +04:00
|
|
|
Lwt.return_none
|
|
|
|
| Some (tail, hist) ->
|
|
|
|
Lwt.return_some (tail, (head, hist))
|