From 7051325ba4becde9390f8748c099ec9a21750c89 Mon Sep 17 00:00:00 2001 From: Benjamin Canou Date: Mon, 17 Sep 2018 09:18:26 +0200 Subject: [PATCH] P2p, client: fix network protocol version negociation --- src/bin_client/main_client.ml | 2 +- src/lib_base/p2p_version.ml | 10 +++++++--- src/lib_base/p2p_version.mli | 12 ++++++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/bin_client/main_client.ml b/src/bin_client/main_client.ml index 55af0cb36..984c30f04 100644 --- a/src/bin_client/main_client.ml +++ b/src/bin_client/main_client.ml @@ -97,7 +97,7 @@ let check_network ctxt = default () ; Lwt.return_none | Ok versions -> - match String.split_on_char '_' (List.hd versions).name with + match String.split_on_char '_' (P2p_version.best versions).name with | "SANDBOXED" :: _ -> sandbox () ; Lwt.return_some `Sandbox diff --git a/src/lib_base/p2p_version.ml b/src/lib_base/p2p_version.ml index 3cb6a4e90..198778c7f 100644 --- a/src/lib_base/p2p_version.ml +++ b/src/lib_base/p2p_version.ml @@ -42,8 +42,6 @@ let encoding = (req "major" uint16) (req "minor" uint16)) -(* the common version for a pair of peers, if any, is the maximum one, - in lexicographic order *) let common la lb = let la = List.sort (fun l r -> compare r l) la in let lb = List.sort (fun l r -> compare r l) lb in @@ -51,6 +49,12 @@ let common la lb = | [], _ | _, [] -> None | ((a :: ta) as la), ((b :: tb) as lb) -> if a = b then Some a - else if a < b then find (ta, lb) + else if a > b then find (ta, lb) else find (la, tb) in find (la, lb) + +let best lv = + if lv = [] then + invalid_arg "P2p_version.best" + else + List.hd (List.sort (fun l r -> compare r l) lv) diff --git a/src/lib_base/p2p_version.mli b/src/lib_base/p2p_version.mli index 9b52282f5..edc8c11bc 100644 --- a/src/lib_base/p2p_version.mli +++ b/src/lib_base/p2p_version.mli @@ -23,16 +23,24 @@ (* *) (*****************************************************************************) -(** Protocol version *) +(** Network protocol version. *) +(** Type of a network protocol version. *) type t = { name : string ; major : int ; minor : int ; } -(** Type of a protocol version. *) val pp : Format.formatter -> t -> unit val encoding : t Data_encoding.t + +(** Selects the prefered common version for a pair of version + lists. Used during network protocol negociation. If any, it is the + maximum one, in lexicographic order (name, then major, minor). *) val common : t list -> t list -> t option +(** Gives the prefered version in a list: the one selected by + {!common} among the list of compatible ones. *) +val best : t list -> t +