From 86561363b2c0cd7129b8d8625d2c410039af4935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Wed, 21 Feb 2018 21:01:02 +0100 Subject: [PATCH] Alpha: explicit operation for key revelation --- .../lib_protocol/src/alpha_context.mli | 2 +- src/proto_alpha/lib_protocol/src/apply.ml | 8 ++++- .../lib_protocol/src/helpers_services.ml | 13 ++++++-- .../lib_protocol/src/operation_repr.ml | 32 +++++++++++++------ .../lib_protocol/src/operation_repr.mli | 2 +- .../test/helpers/helpers_operation.ml | 3 +- 6 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/proto_alpha/lib_protocol/src/alpha_context.mli b/src/proto_alpha/lib_protocol/src/alpha_context.mli index 6137f0f73..9317bacaa 100644 --- a/src/proto_alpha/lib_protocol/src/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/src/alpha_context.mli @@ -561,7 +561,6 @@ and sourced_operations = } | Manager_operations of { source: Contract.contract ; - public_key: Ed25519.Public_key.t option ; fee: Tez.t ; counter: counter ; operations: manager_operation list ; @@ -587,6 +586,7 @@ and amendment_operation = } and manager_operation = + | Reveal of Ed25519.Public_key.t | Transaction of { amount: Tez.t ; parameters: Script.expr option ; diff --git a/src/proto_alpha/lib_protocol/src/apply.ml b/src/proto_alpha/lib_protocol/src/apply.ml index b050027fd..2b53b76d2 100644 --- a/src/proto_alpha/lib_protocol/src/apply.ml +++ b/src/proto_alpha/lib_protocol/src/apply.ml @@ -138,6 +138,7 @@ let apply_amendment_operation_content ctxt delegate = function let apply_manager_operation_content ctxt origination_nonce source = function + | Reveal _ -> return (ctxt, origination_nonce, None) | Transaction { amount ; parameters ; destination } -> begin Contract.spend ctxt source amount >>=? fun ctxt -> Contract.credit ctxt destination amount >>=? fun ctxt -> @@ -224,7 +225,12 @@ let apply_sourced_operation ctxt baker_contract pred_block block_prio operation origination_nonce ops = match ops with - | Manager_operations { source ; public_key ; fee ; counter ; operations = contents } -> + | Manager_operations { source ; fee ; counter ; operations = contents } -> + let public_key = + List.fold_left (fun acc op -> + match op with + | Reveal pk -> Some pk + | _ -> acc) None contents in Contract.must_exist ctxt source >>=? fun () -> Contract.update_manager_key ctxt source public_key >>=? fun (ctxt,public_key) -> Operation.check_signature public_key operation >>=? fun () -> diff --git a/src/proto_alpha/lib_protocol/src/helpers_services.ml b/src/proto_alpha/lib_protocol/src/helpers_services.ml index 85d37e466..27edc711f 100644 --- a/src/proto_alpha/lib_protocol/src/helpers_services.ml +++ b/src/proto_alpha/lib_protocol/src/helpers_services.ml @@ -305,8 +305,12 @@ module Forge = struct let operations ctxt block ~branch ~source ?sourcePubKey ~counter ~fee operations = + let operations = + match sourcePubKey with + | None -> operations + | Some pk -> Reveal pk :: operations in let ops = - Manager_operations { source ; public_key = sourcePubKey ; + Manager_operations { source ; counter ; operations ; fee } in (RPC_context.make_call0 S.operations ctxt block () ({ branch }, Sourced_operations ops)) @@ -459,8 +463,13 @@ module Parse = struct match contents with | Anonymous_operations _ -> return () | Sourced_operations (Manager_operations op) -> + let public_key = + List.fold_left (fun acc op -> + match op with + | Reveal pk -> Some pk + | _ -> acc) None op.operations in begin - match op.public_key with + match public_key with | Some key -> return key | None -> Contract.get_manager ctxt op.source >>=? fun manager -> diff --git a/src/proto_alpha/lib_protocol/src/operation_repr.ml b/src/proto_alpha/lib_protocol/src/operation_repr.ml index 9bab3ece3..d27d32151 100644 --- a/src/proto_alpha/lib_protocol/src/operation_repr.ml +++ b/src/proto_alpha/lib_protocol/src/operation_repr.ml @@ -45,7 +45,6 @@ and sourced_operations = } | Manager_operations of { source: Contract_repr.contract ; - public_key: Ed25519.Public_key.t option ; fee: Tez_repr.tez ; counter: counter ; operations: manager_operation list ; @@ -71,6 +70,7 @@ and amendment_operation = } and manager_operation = + | Reveal of Ed25519.Public_key.t | Transaction of { amount: Tez_repr.tez ; parameters: Script_repr.expr option ; @@ -96,6 +96,18 @@ module Encoding = struct open Data_encoding + let reveal_encoding = + (obj2 + (req "kind" (constant "reveal")) + (req "public_key" Ed25519.Public_key.encoding)) + + let reveal_case tag = + case tag reveal_encoding + (function + | Reveal pkh -> Some ((), pkh) + | _ -> None) + (fun ((), pkh) -> Reveal pkh) + let transaction_encoding = (obj4 (req "kind" (constant "transaction")) @@ -149,27 +161,27 @@ module Encoding = struct (fun ((), key) -> Delegation key) let manager_kind_encoding = - (obj6 + (obj5 (req "kind" (constant "manager")) (req "source" Contract_repr.encoding) - (opt "public_key" Ed25519.Public_key.encoding) (req "fee" Tez_repr.encoding) (req "counter" int32) (req "operations" (list (union ~tag_size:`Uint8 [ - transaction_case (Tag 0) ; - origination_case (Tag 1) ; - delegation_case (Tag 2) ; + reveal_case (Tag 0) ; + transaction_case (Tag 1) ; + origination_case (Tag 2) ; + delegation_case (Tag 3) ; ])))) let manager_kind_case tag = case tag manager_kind_encoding (function - | Manager_operations { source; public_key ; fee ; counter ;operations } -> - Some ((), source, public_key, fee, counter, operations) + | Manager_operations { source; fee ; counter ;operations } -> + Some ((), source, fee, counter, operations) | _ -> None) - (fun ((), source, public_key, fee, counter, operations) -> - Manager_operations { source; public_key ; fee ; counter ; operations }) + (fun ((), source, fee, counter, operations) -> + Manager_operations { source; fee ; counter ; operations }) let endorsement_encoding = (obj4 diff --git a/src/proto_alpha/lib_protocol/src/operation_repr.mli b/src/proto_alpha/lib_protocol/src/operation_repr.mli index 6eb49b8c9..b0d745c2b 100644 --- a/src/proto_alpha/lib_protocol/src/operation_repr.mli +++ b/src/proto_alpha/lib_protocol/src/operation_repr.mli @@ -45,7 +45,6 @@ and sourced_operations = } | Manager_operations of { source: Contract_repr.contract ; - public_key: Ed25519.Public_key.t option ; fee: Tez_repr.tez ; counter: counter ; operations: manager_operation list ; @@ -71,6 +70,7 @@ and amendment_operation = } and manager_operation = + | Reveal of Ed25519.Public_key.t | Transaction of { amount: Tez_repr.tez ; parameters: Script_repr.expr option ; diff --git a/src/proto_alpha/lib_protocol/test/helpers/helpers_operation.ml b/src/proto_alpha/lib_protocol/test/helpers/helpers_operation.ml index 85d328d9e..a079689da 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/helpers_operation.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/helpers_operation.ml @@ -20,10 +20,9 @@ let manager (src : Helpers_account.t) ?(fee = Tez.zero) operations context = return @@ Manager_operations { source = src.contract ; - public_key = Some src.pub ; fee ; counter ; - operations + operations = Reveal src.pub :: operations ; }