From 3aef2ed5050f536d883e2c7515bc50a016863ea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Thu, 15 Mar 2018 10:28:41 +0100 Subject: [PATCH] Alpha: remove hash from `Operation_repr.t` --- .../lib_baking/client_baking_forge.ml | 3 +- .../lib_protocol/src/alpha_context.mli | 4 +-- .../lib_protocol/src/alpha_services.ml | 8 +++-- .../lib_protocol/src/alpha_services.mli | 2 +- src/proto_alpha/lib_protocol/src/apply.ml | 6 ++-- .../lib_protocol/src/helpers_services.ml | 14 ++++----- src/proto_alpha/lib_protocol/src/main.ml | 5 +-- .../lib_protocol/src/operation_repr.ml | 31 ++++++++++--------- .../lib_protocol/src/operation_repr.mli | 5 ++- .../test/helpers/helpers_apply.ml | 2 ++ .../test/helpers/helpers_operation.ml | 6 +--- .../test/helpers/helpers_script.ml | 3 +- 12 files changed, 47 insertions(+), 42 deletions(-) diff --git a/src/proto_alpha/lib_baking/client_baking_forge.ml b/src/proto_alpha/lib_baking/client_baking_forge.ml index 823fcb9e0..490db6979 100644 --- a/src/proto_alpha/lib_baking/client_baking_forge.ml +++ b/src/proto_alpha/lib_baking/client_baking_forge.ml @@ -98,8 +98,7 @@ let classify_operations (ops: Operation.raw list) = let t = Array.make (List.length Proto_alpha.Main.validation_passes) [] in List.iter (fun op -> - let h = Operation.hash_raw op in - match Operation.parse h op with + match Operation.parse op with | Ok o -> List.iter (fun pass -> t.(pass) <- op :: t.(pass)) diff --git a/src/proto_alpha/lib_protocol/src/alpha_context.mli b/src/proto_alpha/lib_protocol/src/alpha_context.mli index abf127dbb..7186cd2cd 100644 --- a/src/proto_alpha/lib_protocol/src/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/src/alpha_context.mli @@ -568,7 +568,6 @@ module Vote : sig end type operation = { - hash: Operation_hash.t ; shell: Operation.shell_header ; contents: proto_operation ; signature: signature option ; @@ -654,10 +653,11 @@ module Operation : sig type t = operation val encoding: operation Data_encoding.t + val hash: operation -> Operation_hash.t val hash_raw: raw -> Operation_hash.t type error += Cannot_parse_operation (* `Branch *) - val parse: Operation_hash.t -> Operation.t -> operation tzresult + val parse: Operation.t -> operation tzresult val acceptable_passes: operation -> int list val parse_proto: diff --git a/src/proto_alpha/lib_protocol/src/alpha_services.ml b/src/proto_alpha/lib_protocol/src/alpha_services.ml index c55a4d81c..a9fe53f10 100644 --- a/src/proto_alpha/lib_protocol/src/alpha_services.ml +++ b/src/proto_alpha/lib_protocol/src/alpha_services.ml @@ -20,7 +20,9 @@ module S = struct ~description: "All the operations of the block (fully decoded)." ~query: RPC_query.empty ~input: empty - ~output: (list (list (dynamic_size Operation.encoding))) + ~output: (list (list (merge_objs + (obj1 (req "hash" Operation_hash.encoding)) + (dynamic_size Operation.encoding)))) RPC_path.(custom_root / "operations") let header = @@ -55,7 +57,9 @@ let () = ctxt.operation_hashes () >>= fun operation_hashes -> ctxt.operations () >>= fun operations -> map2_s - (map2_s (fun x y -> Lwt.return (Operation.parse x y))) + (map2_s (fun h op -> + Lwt.return (Operation.parse op) >>=? fun op -> + return (h, op))) operation_hashes operations end ; register0_fullctxt S.header begin fun { block_header ; _ } () () -> diff --git a/src/proto_alpha/lib_protocol/src/alpha_services.mli b/src/proto_alpha/lib_protocol/src/alpha_services.mli index 7bb70390d..75c56503b 100644 --- a/src/proto_alpha/lib_protocol/src/alpha_services.mli +++ b/src/proto_alpha/lib_protocol/src/alpha_services.mli @@ -10,7 +10,7 @@ open Alpha_context val operations: - 'a #RPC_context.simple -> 'a -> Operation.t list list shell_tzresult Lwt.t + 'a #RPC_context.simple -> 'a -> (Operation_hash.t * Operation.t) list list shell_tzresult Lwt.t val header: 'a #RPC_context.simple -> 'a -> Block_header.t shell_tzresult Lwt.t val priority: diff --git a/src/proto_alpha/lib_protocol/src/apply.ml b/src/proto_alpha/lib_protocol/src/apply.ml index afedbd78d..4d68782ee 100644 --- a/src/proto_alpha/lib_protocol/src/apply.ml +++ b/src/proto_alpha/lib_protocol/src/apply.ml @@ -305,10 +305,10 @@ let apply_anonymous_operation ctxt delegate origination_nonce kind = fail Too_many_faucet let apply_operation - ctxt delegate pred_block block_prio operation = + ctxt delegate pred_block block_prio hash operation = match operation.contents with | Anonymous_operations ops -> - let origination_nonce = Contract.initial_origination_nonce operation.hash in + let origination_nonce = Contract.initial_origination_nonce hash in fold_left_s (fun (ctxt, origination_nonce, fees, rewards) op -> apply_anonymous_operation ctxt delegate origination_nonce op @@ -321,7 +321,7 @@ let apply_operation return (ctxt, Contract.originated_contracts origination_nonce, None, fees, rewards) | Sourced_operations op -> - let origination_nonce = Contract.initial_origination_nonce operation.hash in + let origination_nonce = Contract.initial_origination_nonce hash in apply_sourced_operation ctxt pred_block block_prio operation origination_nonce op >>=? fun (ctxt, origination_nonce, err, diff --git a/src/proto_alpha/lib_protocol/src/helpers_services.ml b/src/proto_alpha/lib_protocol/src/helpers_services.ml index be557c426..7929fb6ea 100644 --- a/src/proto_alpha/lib_protocol/src/helpers_services.ml +++ b/src/proto_alpha/lib_protocol/src/helpers_services.ml @@ -132,13 +132,13 @@ module I = struct forged_operation with | None -> Error_monad.fail Operation.Cannot_parse_operation | Some (shell, contents) -> - let operation = { hash ; shell ; contents ; signature } in + let operation = { shell ; contents ; signature } in let level = Alpha_context.Level.current ctxt in Baking.baking_priorities ctxt level >>=? fun (Misc.LCons (baker_pk, _)) -> let baker_pkh = Ed25519.Public_key.hash baker_pk in let block_prio = 0 in Apply.apply_operation - ctxt (Some baker_pkh) pred_block block_prio operation + ctxt (Some baker_pkh) pred_block block_prio hash operation >>=? function | (_ctxt, _, Some script_err, _, _) -> Lwt.return (Error script_err) | (_ctxt, contracts, None,_ , _) -> Lwt.return (Ok contracts) @@ -480,20 +480,20 @@ module Parse = struct Roll.delegate_pubkey ctxt manager end >>=? fun public_key -> Operation.check_signature public_key - { signature ; shell ; contents ; hash = Operation_hash.zero } + { signature ; shell ; contents } | Sourced_operations (Consensus_operation (Endorsements { level ; slots ; _ })) -> let level = Level.from_raw ctxt level in Baking.check_endorsements_rights ctxt level slots >>=? fun public_key -> Operation.check_signature public_key - { signature ; shell ; contents ; hash = Operation_hash.zero } + { signature ; shell ; contents } | Sourced_operations (Amendment_operation { source ; _ }) -> Roll.delegate_pubkey ctxt source >>=? fun source -> Operation.check_signature source - { signature ; shell ; contents ; hash = Operation_hash.zero } + { signature ; shell ; contents } | Sourced_operations (Dictator_operation _) -> let key = Constants.dictator_pubkey ctxt in Operation.check_signature key - { signature ; shell ; contents ; hash = Operation_hash.zero } + { signature ; shell ; contents } end @@ -501,7 +501,7 @@ module Parse = struct let open Services_registration in register0 S.operations begin fun ctxt () (operations, check) -> map_s begin fun raw -> - Lwt.return (Operation.parse (Operation.hash_raw raw) raw) >>=? fun op -> + Lwt.return (Operation.parse raw) >>=? fun op -> begin match check with | Some true -> I.check_signature ctxt op.signature op.shell op.contents | Some false | None -> return () diff --git a/src/proto_alpha/lib_protocol/src/main.ml b/src/proto_alpha/lib_protocol/src/main.ml index 71b86cb9e..6bc662f11 100644 --- a/src/proto_alpha/lib_protocol/src/main.ml +++ b/src/proto_alpha/lib_protocol/src/main.ml @@ -11,7 +11,7 @@ type operation = Alpha_context.operation -let parse_operation = Alpha_context.Operation.parse +let parse_operation _hash op = Alpha_context.Operation.parse op let acceptable_passes = Alpha_context.Operation.acceptable_passes let max_block_length = @@ -118,7 +118,8 @@ let apply_operation ({ mode ; ctxt ; op_count ; _ } as data) operation = predecessor, protocol_data.priority, Some baker in - Apply.apply_operation ctxt baker pred_block block_prio operation + Apply.apply_operation ctxt baker pred_block block_prio + (Alpha_context.Operation.hash operation) operation >>=? fun (ctxt, _contracts, _ignored_script_error, fees, rewards) -> let op_count = op_count + 1 in Lwt.return Alpha_context.Tez.(fees >>? (+?) data.fees) >>=? fun fees -> diff --git a/src/proto_alpha/lib_protocol/src/operation_repr.ml b/src/proto_alpha/lib_protocol/src/operation_repr.ml index d27d32151..f22ad3895 100644 --- a/src/proto_alpha/lib_protocol/src/operation_repr.ml +++ b/src/proto_alpha/lib_protocol/src/operation_repr.ml @@ -17,7 +17,6 @@ type raw = Operation.t = { let raw_encoding = Operation.encoding type operation = { - hash: Operation_hash.t ; shell: Operation.shell_header ; contents: proto_operation ; signature: Ed25519.Signature.t option ; @@ -356,15 +355,13 @@ type error += Cannot_parse_operation let encoding = let open Data_encoding in conv - (fun { hash ; shell ; contents ; signature } -> - (hash, (shell, (contents, signature)))) - (fun (hash, (shell, (contents, signature))) -> - { hash ; shell ; contents ; signature }) + (fun { shell ; contents ; signature } -> + (shell, (contents, signature))) + (fun (shell, (contents, signature)) -> + { shell ; contents ; signature }) (merge_objs - (obj1 (req "hash" Operation_hash.encoding)) - (merge_objs - Operation.shell_header_encoding - Encoding.signed_proto_operation_encoding)) + Operation.shell_header_encoding + Encoding.signed_proto_operation_encoding) let () = register_error_kind @@ -379,12 +376,12 @@ let () = (function Cannot_parse_operation -> Some () | _ -> None) (fun () -> Cannot_parse_operation) -let parse hash (op: Operation.t) = +let parse (op: Operation.t) = match Data_encoding.Binary.of_bytes Encoding.signed_proto_operation_encoding op.proto with | Some (contents, signature) -> - ok { hash ; shell = op.shell ; contents ; signature } + ok { shell = op.shell ; contents ; signature } | None -> error Cannot_parse_operation let acceptable_passes op = @@ -425,7 +422,7 @@ let forge shell proto = Data_encoding.Binary.to_bytes Encoding.unsigned_operation_encoding (shell, proto) -let check_signature key { shell ; contents ; signature } = +let check_signature key { shell ; contents ; signature } = match contents, signature with | Anonymous_operations _, _ -> return () | Sourced_operations _, None -> @@ -444,6 +441,12 @@ let parse_proto bytes = | Some (proto, signature) -> return (proto, signature) | None -> fail Cannot_parse_operation -include Encoding - let hash_raw = Operation.hash +let hash o = + let proto = + Data_encoding.Binary.to_bytes + Encoding.signed_proto_operation_encoding + (o.contents, o.signature) in + Operation.hash { shell = o.shell ; proto } + +include Encoding diff --git a/src/proto_alpha/lib_protocol/src/operation_repr.mli b/src/proto_alpha/lib_protocol/src/operation_repr.mli index b0d745c2b..b9d95dd75 100644 --- a/src/proto_alpha/lib_protocol/src/operation_repr.mli +++ b/src/proto_alpha/lib_protocol/src/operation_repr.mli @@ -17,7 +17,6 @@ type raw = Operation.t = { val raw_encoding: raw Data_encoding.t type operation = { - hash: Operation_hash.t ; shell: Operation.shell_header ; contents: proto_operation ; signature: Ed25519.Signature.t option ; @@ -97,9 +96,9 @@ type error += Cannot_parse_operation (* `Branch *) val encoding: operation Data_encoding.t val hash_raw: raw -> Operation_hash.t +val hash: operation -> Operation_hash.t -val parse: - Operation_hash.t -> Operation.t -> operation tzresult +val parse: Operation.t -> operation tzresult val acceptable_passes: operation -> int list diff --git a/src/proto_alpha/lib_protocol/test/helpers/helpers_apply.ml b/src/proto_alpha/lib_protocol/test/helpers/helpers_apply.ml index 34ca8e998..d97c01086 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/helpers_apply.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/helpers_apply.ml @@ -13,11 +13,13 @@ let operation ~tc ?(baker: Helpers_account.t option) ?(src: Helpers_account.t option) pred_block_hash op_sh proto_op = return @@ Helpers_operation.apply_of_proto src op_sh proto_op >>=? fun operation -> + let hash = Proto_alpha.Alpha_context.Operation.hash operation in Proto_alpha.Apply.apply_operation tc (Option.map ~f:(fun x -> x.Helpers_account.hpub) baker) pred_block_hash 0 + hash operation >>=? fun (tc, contracts, err, _fees, _rewards) -> return ((contracts, err), tc) 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 a079689da..f5ed95e2b 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/helpers_operation.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/helpers_operation.ml @@ -122,12 +122,8 @@ let main_of_proto (src: Helpers_account.t) operation_header protocol_operation = let apply_of_proto (source: Helpers_account.t option) operation_header protocol_operation = - let (proto, signature) = sign source operation_header protocol_operation in - let data_operation: Tezos_base.Operation.t = - {shell = operation_header ; proto} in - let hash = Tezos_base.Operation.hash data_operation in + let (_proto, signature) = sign source operation_header protocol_operation in { - hash ; shell = operation_header ; contents = protocol_operation ; signature diff --git a/src/proto_alpha/lib_protocol/test/helpers/helpers_script.ml b/src/proto_alpha/lib_protocol/test/helpers/helpers_script.ml index afcb590d5..ec39e7adb 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/helpers_script.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/helpers_script.ml @@ -26,7 +26,8 @@ let execute_code_pred let op_header = Helpers_block.get_op_header_res pred in let apply_op = Helpers_operation.apply_of_proto (Some op) op_header dummy_protop in - let dummy_nonce = Contract.initial_origination_nonce apply_op.hash in + let hash = Operation.hash apply_op in + let dummy_nonce = Contract.initial_origination_nonce hash in let amount = Tez.zero in let gaz = Gas.of_int (Alpha_context.Constants.max_gas tc) in let return = Script_interpreter.execute