Shell: animated generation of P2p.Identity
This commit is contained in:
parent
5e1eddf681
commit
3a70d88fe6
@ -193,12 +193,55 @@ module Identity = struct
|
|||||||
(req "secret_key" Crypto_box.secret_key_encoding)
|
(req "secret_key" Crypto_box.secret_key_encoding)
|
||||||
(req "proof_of_work_stamp" Crypto_box.nonce_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 secret_key, public_key, gid = Crypto_box.random_keypair () in
|
||||||
let proof_of_work_stamp =
|
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 }
|
{ 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
|
end
|
||||||
|
|
||||||
module Connection_info = struct
|
module Connection_info = struct
|
||||||
|
@ -94,6 +94,12 @@ module Identity : sig
|
|||||||
val generate : Crypto_box.target -> t
|
val generate : Crypto_box.target -> t
|
||||||
(** [generate target] is a freshly minted identity whose proof of
|
(** [generate target] is a freshly minted identity whose proof of
|
||||||
work stamp difficulty is at least equal to [target]. *)
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -71,11 +71,18 @@ let check_proof_of_work pk nonce target =
|
|||||||
] in
|
] in
|
||||||
compare_target hash target
|
compare_target hash target
|
||||||
|
|
||||||
let generate_proof_of_work pk target =
|
let generate_proof_of_work ?max pk target =
|
||||||
let rec loop nonce =
|
let may_interupt =
|
||||||
if check_proof_of_work pk nonce target then nonce
|
match max with
|
||||||
else loop (increment_nonce nonce) in
|
| None -> (fun _ -> ())
|
||||||
loop (random_nonce ())
|
| 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 public_key_encoding =
|
||||||
let open Data_encoding in
|
let open Data_encoding in
|
||||||
|
@ -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 fast_box_open : channel_key -> MBytes.t -> nonce -> MBytes.t option
|
||||||
|
|
||||||
val check_proof_of_work : public_key -> nonce -> target -> bool
|
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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user