From f600fc57d458a2adc0d7ab4b291d5337cc2754aa Mon Sep 17 00:00:00 2001 From: bruno Date: Mon, 19 Feb 2018 18:25:57 +0100 Subject: [PATCH] Shell: improve pp for block locator --- src/lib_base/block_locator.ml | 21 ++++++++++++++++++++- src/lib_base/block_locator.mli | 2 ++ src/lib_shell_services/validation_errors.ml | 6 +++--- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/lib_base/block_locator.ml b/src/lib_base/block_locator.ml index 99b7ddea9..055a7ea9f 100644 --- a/src/lib_base/block_locator.ml +++ b/src/lib_base/block_locator.ml @@ -16,7 +16,26 @@ and raw = Block_header.t * Block_hash.t list let raw x = x -let pp ppf loc = Block_header.pp ppf (fst loc) +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 + Format.fprintf ppf "%a (%i)\n%a" Block_hash.pp hd acc pp_hash_list (tl , acc - d , new_d , new_r) in + 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) let encoding = let open Data_encoding in diff --git a/src/lib_base/block_locator.mli b/src/lib_base/block_locator.mli index 4c332fc77..3a741b29b 100644 --- a/src/lib_base/block_locator.mli +++ b/src/lib_base/block_locator.mli @@ -17,6 +17,8 @@ val raw: t -> raw val pp : Format.formatter -> t -> unit +val pp_short : Format.formatter -> t -> unit + val encoding: t Data_encoding.t val compute: predecessor: (Block_hash.t -> int -> Block_hash.t option Lwt.t) -> diff --git a/src/lib_shell_services/validation_errors.ml b/src/lib_shell_services/validation_errors.ml index 6db5aab9e..969052149 100644 --- a/src/lib_shell_services/validation_errors.ml +++ b/src/lib_shell_services/validation_errors.ml @@ -136,9 +136,9 @@ let () = ~description:"Block locator is invalid." ~pp: (fun ppf (id, locator) -> Format.fprintf ppf - "Invalid block locator %a on peer %a" - Block_locator.pp locator - P2p_peer.Id.pp id) + "Invalid block locator on peer %a:\n%a" + P2p_peer.Id.pp id + Block_locator.pp locator) Data_encoding.(obj2 (req "id" P2p_peer.Id.encoding) (req "locator" Block_locator.encoding))