From 2976353636d9804ab78b253ac524f44f9c9f687b Mon Sep 17 00:00:00 2001 From: Benjamin Canou Date: Sun, 17 Jun 2018 00:07:58 +0200 Subject: [PATCH] Client: more caching of public keys --- src/bin_signer/handler.ml | 2 +- src/lib_client_base/client_keys.ml | 18 ++++++++++++++---- src/lib_client_base/client_keys.mli | 2 ++ src/lib_client_base_unix/client_main_run.ml | 2 +- .../lib_baking/client_baking_endorsement.ml | 2 +- .../lib_baking/client_baking_forge.ml | 2 +- .../lib_client/client_proto_programs.ml | 2 +- src/proto_alpha/lib_client/injection.ml | 2 +- .../lib_client/client_proto_main.ml | 2 +- 9 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/bin_signer/handler.ml b/src/bin_signer/handler.ml index e953a1242..246fe5e25 100644 --- a/src/bin_signer/handler.ml +++ b/src/bin_signer/handler.ml @@ -53,7 +53,7 @@ let sign end >>=? fun () -> Client_keys.get_key cctxt pkh >>=? fun (name, _pkh, sk_uri) -> log "Signing data for key %s" name >>= fun () -> - Client_keys.sign sk_uri data >>=? fun signature -> + Client_keys.sign cctxt sk_uri data >>=? fun signature -> return signature let public_key (cctxt : #Client_context.wallet) pkh = diff --git a/src/lib_client_base/client_keys.ml b/src/lib_client_base/client_keys.ml index 70e6b9c81..4e5ffd684 100644 --- a/src/lib_client_base/client_keys.ml +++ b/src/lib_client_base/client_keys.ml @@ -176,20 +176,30 @@ let public_key_hash pk_uri = public_key pk_uri >>=? fun pk -> return (Signature.Public_key.hash pk, Some pk) -let sign ?watermark sk_uri buf = +let sign cctxt ?watermark sk_uri buf = let scheme = Option.unopt ~default:"" (Uri.scheme sk_uri) in find_signer_for_key ~scheme >>=? fun signer -> let module Signer = (val signer : SIGNER) in Signer.sign ?watermark sk_uri buf >>=? fun signature -> Signer.neuterize sk_uri >>=? fun pk_uri -> - public_key pk_uri >>=? fun pubkey -> + Secret_key.rev_find cctxt sk_uri >>=? begin function + | None -> + public_key pk_uri + | Some name -> + Public_key.find cctxt name >>=? function + | (_, None) -> + public_key pk_uri >>=? fun pk -> + Public_key.update cctxt name (pk_uri, Some pk) >>=? fun () -> + return pk + | (_, Some pubkey) -> return pubkey + end >>=? fun pubkey -> fail_unless (Signature.check ?watermark pubkey signature buf) (Signature_mismatch sk_uri) >>=? fun () -> return signature -let append ?watermark loc buf = - sign ?watermark loc buf >>|? fun signature -> +let append cctxt ?watermark loc buf = + sign cctxt ?watermark loc buf >>|? fun signature -> Signature.concat buf signature let check ?watermark pk_uri signature buf = diff --git a/src/lib_client_base/client_keys.mli b/src/lib_client_base/client_keys.mli index b65f1ed65..f0fc2b8a7 100644 --- a/src/lib_client_base/client_keys.mli +++ b/src/lib_client_base/client_keys.mli @@ -76,10 +76,12 @@ val public_key_hash : pk_uri -> (Signature.Public_key_hash.t * Signature.Public_ val neuterize : sk_uri -> pk_uri tzresult Lwt.t val sign : + #Client_context.wallet -> ?watermark:Signature.watermark -> sk_uri -> MBytes.t -> Signature.t tzresult Lwt.t val append : + #Client_context.wallet -> ?watermark:Signature.watermark -> sk_uri -> MBytes.t -> MBytes.t tzresult Lwt.t diff --git a/src/lib_client_base_unix/client_main_run.ml b/src/lib_client_base_unix/client_main_run.ml index 1c978d0b8..9b355376b 100644 --- a/src/lib_client_base_unix/client_main_run.ml +++ b/src/lib_client_base_unix/client_main_run.ml @@ -121,7 +121,7 @@ let main select_commands = | _ -> None) keys with | sk_uri :: _ -> - Client_keys.sign sk_uri payload + Client_keys.sign client_config sk_uri payload | [] -> failwith "remote signer expects authentication signature, \ but no authorized key was found in the wallet" diff --git a/src/proto_alpha/lib_baking/client_baking_endorsement.ml b/src/proto_alpha/lib_baking/client_baking_endorsement.ml index 370c1ab87..83e11329c 100644 --- a/src/proto_alpha/lib_baking/client_baking_endorsement.ml +++ b/src/proto_alpha/lib_baking/client_baking_endorsement.ml @@ -103,7 +103,7 @@ let inject_endorsement ~level:level ~slots () >>=? fun bytes -> - Client_keys.append + Client_keys.append cctxt src_sk ~watermark:Endorsement bytes >>=? fun signed_bytes -> Shell_services.Injection.operation cctxt ?async ~chain signed_bytes >>=? fun oph -> iter_s diff --git a/src/proto_alpha/lib_baking/client_baking_forge.ml b/src/proto_alpha/lib_baking/client_baking_forge.ml index 94d022000..f374813ea 100644 --- a/src/proto_alpha/lib_baking/client_baking_forge.ml +++ b/src/proto_alpha/lib_baking/client_baking_forge.ml @@ -37,7 +37,7 @@ let forge_block_header Data_encoding.Binary.to_bytes_exn Alpha_context.Block_header.unsigned_encoding (shell, contents) in - Client_keys.append delegate_sk ~watermark:Block_header unsigned_header + Client_keys.append cctxt delegate_sk ~watermark:Block_header unsigned_header else loop () in loop () diff --git a/src/proto_alpha/lib_client/client_proto_programs.ml b/src/proto_alpha/lib_client/client_proto_programs.ml index 516f043ad..98d1ddef2 100644 --- a/src/proto_alpha/lib_client/client_proto_programs.ml +++ b/src/proto_alpha/lib_client/client_proto_programs.ml @@ -125,7 +125,7 @@ let hash_and_sign sk = Alpha_services.Helpers.Scripts.hash_data cctxt (chain, block) (data.expanded, typ.expanded, gas) >>=? fun (hash, gas) -> - Client_keys.sign sk (MBytes.of_string hash) >>=? fun signature -> + Client_keys.sign cctxt sk (MBytes.of_string hash) >>=? fun signature -> return (hash, Signature.to_b58check signature, gas) let typecheck_data diff --git a/src/proto_alpha/lib_client/injection.ml b/src/proto_alpha/lib_client/injection.ml index 2132d6afc..ed5c4fe0c 100644 --- a/src/proto_alpha/lib_client/injection.ml +++ b/src/proto_alpha/lib_client/injection.ml @@ -48,7 +48,7 @@ let preapply (type t) match src_sk with | None -> return None | Some src_sk -> - Client_keys.sign + Client_keys.sign cctxt ~watermark src_sk bytes >>=? fun signature -> return (Some signature) end >>=? fun signature -> diff --git a/src/proto_genesis/lib_client/client_proto_main.ml b/src/proto_genesis/lib_client/client_proto_main.ml index 08491cc92..ba4d896c8 100644 --- a/src/proto_genesis/lib_client/client_proto_main.ml +++ b/src/proto_genesis/lib_client/client_proto_main.ml @@ -19,7 +19,7 @@ let bake cctxt ?(timestamp = Time.now ()) block command sk = cctxt ~block ~timestamp ~protocol_data [] >>=? fun (shell_header, _) -> let blk = Data.Command.forge shell_header command in - Client_keys.append sk blk >>=? fun signed_blk -> + Client_keys.append cctxt sk blk >>=? fun signed_blk -> Shell_services.Injection.block cctxt signed_blk [] let int64_parameter =