From 3a70d88fe606355e0121a74fa03932f364288b66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Mon, 23 Jan 2017 11:09:48 +0100 Subject: [PATCH] Shell: animated generation of `P2p.Identity` --- src/node/net/p2p_types.ml | 47 ++++++++++++++++++++++++++++++++++++-- src/node/net/p2p_types.mli | 6 +++++ src/utils/crypto_box.ml | 17 ++++++++++---- src/utils/crypto_box.mli | 2 +- 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/src/node/net/p2p_types.ml b/src/node/net/p2p_types.ml index 5ed7ded49..f8b8690fa 100644 --- a/src/node/net/p2p_types.ml +++ b/src/node/net/p2p_types.ml @@ -193,12 +193,55 @@ module Identity = struct (req "secret_key" Crypto_box.secret_key_encoding) (req "proof_of_work_stamp" Crypto_box.nonce_encoding)) - let generate target = + let generate ?max target = let secret_key, public_key, gid = Crypto_box.random_keypair () in let proof_of_work_stamp = - Crypto_box.generate_proof_of_work public_key target in + Crypto_box.generate_proof_of_work ?max public_key target in { gid ; public_key ; secret_key ; proof_of_work_stamp } + let animation = [| + "|.....|" ; + "|o....|" ; + "|oo...|" ; + "|ooo..|" ; + "|.ooo.|" ; + "|..ooo|" ; + "|...oo|" ; + "|....o|" ; + "|.....|" ; + "|.....|" ; + "|.....|" ; + "|.....|" ; + |] + + let init = String.make (String.length animation.(0)) '\ ' + let clean = String.make (String.length animation.(0)) '\b' + let animation = Array.map (fun x -> clean ^ x) animation + let animation_size = Array.length animation + let duration = 1200 / animation_size + + let generate_with_animation ppf target = + Format.fprintf ppf "%s%!" init ; + let count = ref 10000 in + let rec loop n = + let start = Mtime.counter () in + Format.fprintf ppf "%s%!" animation.(n mod animation_size); + try generate ~max:!count target + with Not_found -> + let time = Mtime.to_ms (Mtime.count start) in + count := + if time <= 0. then + !count * 10 + else + !count * duration / int_of_float time ; + loop (n+1) + in + let id = loop 0 in + Format.fprintf ppf "%s%s\n%!" clean init ; + id + + let generate target = generate target + end module Connection_info = struct diff --git a/src/node/net/p2p_types.mli b/src/node/net/p2p_types.mli index f85ed323a..a09283a69 100644 --- a/src/node/net/p2p_types.mli +++ b/src/node/net/p2p_types.mli @@ -94,6 +94,12 @@ module Identity : sig val generate : Crypto_box.target -> t (** [generate target] is a freshly minted identity whose proof of work stamp difficulty is at least equal to [target]. *) + + val generate_with_animation : + Format.formatter -> Crypto_box.target -> t + (** [generate_with_animation ppf target] is a freshly minted identity + whose proof of work stamp difficulty is at least equal to [target]. *) + end diff --git a/src/utils/crypto_box.ml b/src/utils/crypto_box.ml index e8d58a80d..8e9cefe63 100644 --- a/src/utils/crypto_box.ml +++ b/src/utils/crypto_box.ml @@ -71,11 +71,18 @@ let check_proof_of_work pk nonce target = ] in compare_target hash target -let generate_proof_of_work pk target = - let rec loop nonce = - if check_proof_of_work pk nonce target then nonce - else loop (increment_nonce nonce) in - loop (random_nonce ()) +let generate_proof_of_work ?max pk target = + let may_interupt = + match max with + | None -> (fun _ -> ()) + | Some max -> (fun cpt -> if max < cpt then raise Not_found) in + let rec loop nonce cpt = + may_interupt cpt ; + if check_proof_of_work pk nonce target then + nonce + else + loop (increment_nonce nonce) (cpt + 1) in + loop (random_nonce ()) 0 let public_key_encoding = let open Data_encoding in diff --git a/src/utils/crypto_box.mli b/src/utils/crypto_box.mli index 0ae416919..cbeedd440 100644 --- a/src/utils/crypto_box.mli +++ b/src/utils/crypto_box.mli @@ -38,5 +38,5 @@ val fast_box : channel_key -> MBytes.t -> nonce -> MBytes.t val fast_box_open : channel_key -> MBytes.t -> nonce -> MBytes.t option val check_proof_of_work : public_key -> nonce -> target -> bool -val generate_proof_of_work : public_key -> target -> nonce +val generate_proof_of_work : ?max:int -> public_key -> target -> nonce