From aa1450d8e628e8081663477a94774db39c510e1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=BD=20Kim=20Quy=C3=AAn?= Date: Sat, 23 Jun 2018 21:33:08 +0000 Subject: [PATCH] Alpha: update operation receipt for endorser --- .../lib_client/operation_result.ml | 4 +- src/proto_alpha/lib_protocol/src/apply.ml | 71 ++++++++----------- .../src/apply_operation_result.ml | 14 ++-- .../src/apply_operation_result.mli | 5 +- .../lib_protocol/src/delegate_storage.ml | 2 +- .../lib_protocol/src/delegate_storage.mli | 2 +- src/proto_alpha/lib_protocol/src/main.ml | 23 +++--- 7 files changed, 55 insertions(+), 66 deletions(-) diff --git a/src/proto_alpha/lib_client/operation_result.ml b/src/proto_alpha/lib_client/operation_result.ml index b402aaae0..e113ee407 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -313,13 +313,15 @@ let rec pp_contents_and_result_list : pp_balance_updates bus | Single_and_result (Endorsement { level }, - Endorsement_result (delegate, slots)) -> + Endorsement_result { balance_updates ; delegate ; slots }) -> Format.fprintf ppf "@[Endorsement:@,\ Level: %a@,\ + Balance updates:%a@,\ Delegate: %a@,\ Slots: %a@]" Raw_level.pp level + pp_balance_updates balance_updates Signature.Public_key_hash.pp delegate (Format.pp_print_list ~pp_sep:Format.pp_print_space diff --git a/src/proto_alpha/lib_protocol/src/apply.ml b/src/proto_alpha/lib_protocol/src/apply.ml index 5b19bc9c0..57a5f056f 100644 --- a/src/proto_alpha/lib_protocol/src/apply.ml +++ b/src/proto_alpha/lib_protocol/src/apply.ml @@ -322,17 +322,6 @@ let () = open Apply_operation_result -(** - Transaction: source/payer A -> destination B - - payer: is the signer of the operation, the one that pays the storage fee. - This fee is not going to the baker but it will be burned. - - source: is the one who will pay for the fees: space and gas consumption. - This amount will go to the baker. - - internal: set to true in the case of transaction between smart contracts. - Where there is a new storage cost for each smart contract may occurs. The - source may not be the same A any more. -*) - let apply_manager_operation_content : type kind. ( Alpha_context.t -> Script_ir_translator.unparsing_mode -> payer:Contract.t -> source:Contract.t -> @@ -543,10 +532,8 @@ let apply_manager_contents Manager_operation_result { balance_updates = cleanup_balance_updates - [ - Contract source, Debited fee ; - Rewards (baker, level.cycle), Credited fee - ] ; + [ Contract source, Debited fee ; + Rewards (baker, level.cycle), Credited fee ] ; operation_result ; internal_operation_results }) @@ -582,7 +569,8 @@ let rec precheck_manager_contents_list let rec apply_manager_contents_list : type kind. - Alpha_context.t -> _ -> _ -> kind Kind.manager contents_list -> + Alpha_context.t -> Script_ir_translator.unparsing_mode -> + public_key_hash -> kind Kind.manager contents_list -> (context * kind Kind.manager contents_result_list) Lwt.t = fun ctxt mode baker contents_list -> match contents_list with @@ -651,7 +639,14 @@ let apply_contents_list Global.get_last_block_priority ctxt >>=? fun block_priority -> Baking.endorsement_reward ctxt ~block_priority gap >>=? fun reward -> Delegate.freeze_rewards ctxt delegate reward >>=? fun ctxt -> - return (ctxt, Single_result (Endorsement_result (delegate, slots))) + let level = Level.from_raw ctxt level in + return (ctxt, Single_result + (Endorsement_result + { balance_updates = + [ Rewards (baker, level.cycle), Credited reward; + Rewards (delegate, level.cycle), Debited reward; + Deposits (delegate, level.cycle), Credited deposit; ] ; + delegate ; slots })) | Single (Seed_nonce_revelation { level ; nonce }) -> let level = Level.from_raw ctxt level in Nonce.reveal ctxt level nonce >>=? fun ctxt -> @@ -660,9 +655,8 @@ let apply_contents_list add_rewards ctxt seed_nonce_revelation_tip >>=? fun ctxt -> return (ctxt, Single_result (Seed_nonce_revelation_result - [ - Rewards (baker, level.cycle), Credited seed_nonce_revelation_tip - ])) + [ Rewards (baker, level.cycle), + Credited seed_nonce_revelation_tip ])) | Single (Double_endorsement_evidence { op1 ; op2 }) -> begin match op1.protocol_data.contents, op2.protocol_data.contents with | Single (Endorsement e1), @@ -694,15 +688,12 @@ let apply_contents_list | Ok v -> v | Error _ -> Tez.zero in add_rewards ctxt reward >>=? fun ctxt -> - return - (ctxt, - Single_result - (Double_endorsement_evidence_result [ - Rewards (baker, level.cycle), Credited reward; - Rewards (delegate1, level.cycle), Debited balance.rewards; - Deposits (delegate1, level.cycle), Debited balance.deposit; - Fees (delegate1, level.cycle), Debited balance.fees - ])) + return (ctxt, Single_result + (Double_endorsement_evidence_result [ + Rewards (baker, level.cycle), Credited reward; + Rewards (delegate1, level.cycle), Debited balance.rewards; + Deposits (delegate1, level.cycle), Debited balance.deposit; + Fees (delegate1, level.cycle), Debited balance.fees ])) | _, _ -> fail Invalid_double_endorsement_evidence end | Single (Double_baking_evidence { bh1 ; bh2 }) -> @@ -749,15 +740,12 @@ let apply_contents_list | Ok v -> v | Error _ -> Tez.zero in add_rewards ctxt reward >>=? fun ctxt -> - return - (ctxt, - Single_result - (Double_baking_evidence_result [ - Rewards (baker, level.cycle), Credited reward ; - Rewards (delegate, level.cycle), Debited balance.rewards ; - Deposits (delegate, level.cycle), Debited balance.deposit ; - Fees (delegate, level.cycle), Debited balance.fees ; - ])) + return (ctxt, Single_result + (Double_baking_evidence_result [ + Rewards (baker, level.cycle), Credited reward ; + Rewards (delegate, level.cycle), Debited balance.rewards ; + Deposits (delegate, level.cycle), Debited balance.deposit ; + Fees (delegate, level.cycle), Debited balance.fees ; ])) | Single (Activate_account { id = pkh ; activation_code }) -> begin let blinded_pkh = Blinded_public_key_hash.of_ed25519_pkh activation_code pkh in @@ -767,11 +755,8 @@ let apply_contents_list Commitment.delete ctxt blinded_pkh >>=? fun ctxt -> let contract = Contract.implicit_contract (Signature.Ed25519 pkh) in Contract.(credit ctxt contract amount) >>=? fun ctxt -> - return (ctxt, Single_result ( - Activate_account_result - [ - Contract contract, Credited amount - ])) + return (ctxt, Single_result (Activate_account_result + [ Contract contract, Credited amount ])) end | Single (Proposals { source ; period ; proposals }) -> Roll.delegate_pubkey ctxt source >>=? fun delegate -> diff --git a/src/proto_alpha/lib_protocol/src/apply_operation_result.ml b/src/proto_alpha/lib_protocol/src/apply_operation_result.ml index 05fd306b5..ea203d6c6 100644 --- a/src/proto_alpha/lib_protocol/src/apply_operation_result.ml +++ b/src/proto_alpha/lib_protocol/src/apply_operation_result.ml @@ -339,7 +339,10 @@ let internal_operation_result_encoding : type 'kind contents_result = | Endorsement_result : - Signature.Public_key_hash.t * int list -> Kind.endorsement contents_result + { balance_updates : balance_updates ; + delegate : Signature.Public_key_hash.t ; + slots: int list ; + } -> Kind.endorsement contents_result | Seed_nonce_revelation_result : balance_updates -> Kind.seed_nonce_revelation contents_result | Double_endorsement_evidence_result : @@ -402,7 +405,8 @@ module Encoding = struct Case { op_case = Operation.Encoding.endorsement_case ; encoding = - (obj2 + (obj3 + (req "balance_updates" balance_updates_encoding) (req "delegate" Signature.Public_key_hash.encoding) (req "slots" (list uint8))); select = @@ -415,9 +419,11 @@ module Encoding = struct | _ -> None) ; proj = (function - | Endorsement_result (d, s) -> (d, s)) ; + | Endorsement_result { balance_updates ; delegate ; slots } + -> (balance_updates, delegate, slots)) ; inj = - (fun (d, s) -> Endorsement_result (d, s)) + (fun (balance_updates, delegate, slots) -> + Endorsement_result { balance_updates ; delegate ; slots }) } let seed_nonce_revelation_case = diff --git a/src/proto_alpha/lib_protocol/src/apply_operation_result.mli b/src/proto_alpha/lib_protocol/src/apply_operation_result.mli index fc463b5b9..7a9e3cce0 100644 --- a/src/proto_alpha/lib_protocol/src/apply_operation_result.mli +++ b/src/proto_alpha/lib_protocol/src/apply_operation_result.mli @@ -54,7 +54,10 @@ and packed_contents_result_list = (** Result of applying an {!Operation.contents}. Follows the same structure. *) and 'kind contents_result = | Endorsement_result : - Signature.Public_key_hash.t * int list -> Kind.endorsement contents_result + { balance_updates : balance_updates ; + delegate : Signature.Public_key_hash.t ; + slots: int list ; + } -> Kind.endorsement contents_result | Seed_nonce_revelation_result : balance_updates -> Kind.seed_nonce_revelation contents_result | Double_endorsement_evidence_result : diff --git a/src/proto_alpha/lib_protocol/src/delegate_storage.ml b/src/proto_alpha/lib_protocol/src/delegate_storage.ml index a8f82fa39..0eed840e6 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_storage.ml +++ b/src/proto_alpha/lib_protocol/src/delegate_storage.ml @@ -376,7 +376,7 @@ let punish ctxt delegate cycle = get_frozen_rewards ctxt contract cycle >>=? fun rewards -> Roll_storage.Delegate.remove_amount ctxt delegate deposit >>=? fun ctxt -> Roll_storage.Delegate.remove_amount ctxt delegate fees >>=? fun ctxt -> - (* Rewards are not in the delegate balnace yet... *) + (* Rewards are not in the delegate balance yet... *) Storage.Contract.Frozen_deposits.remove (ctxt, contract) cycle >>= fun ctxt -> Storage.Contract.Frozen_fees.remove (ctxt, contract) cycle >>= fun ctxt -> Storage.Contract.Frozen_rewards.remove (ctxt, contract) cycle >>= fun ctxt -> diff --git a/src/proto_alpha/lib_protocol/src/delegate_storage.mli b/src/proto_alpha/lib_protocol/src/delegate_storage.mli index eec393d5e..c3e2f1306 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_storage.mli +++ b/src/proto_alpha/lib_protocol/src/delegate_storage.mli @@ -94,7 +94,7 @@ val cycle_end: Raw_context.t tzresult Lwt.t (** Burn all then frozen deposit/fees/rewards for a delegate at a given - cycle. Returns the burned amount. *) + cycle. Returns the burned amounts. *) val punish: Raw_context.t -> Signature.Public_key_hash.t -> Cycle_repr.t -> (Raw_context.t * frozen_balance) tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/src/main.ml b/src/proto_alpha/lib_protocol/src/main.ml index 3ebff5e49..7f8ac7734 100644 --- a/src/proto_alpha/lib_protocol/src/main.ml +++ b/src/proto_alpha/lib_protocol/src/main.ml @@ -158,24 +158,17 @@ let apply_operation | _ -> let { shell ; protocol_data = Operation_data protocol_data } = operation in let operation : _ Alpha_context.operation = { shell ; protocol_data } in - let predecessor = + let predecessor, baker = match mode with | Partial_application - { block_header = { shell = { predecessor ; _ } ; _ } ; _ } + { block_header = { shell = { predecessor ; _ } ; _ } ; baker } | Application - { block_header = { shell = { predecessor ; _ } ; _ } ; _ } - | Full_construction { predecessor ; _ } -> - predecessor - | Partial_construction { predecessor } -> - predecessor in - let baker = - match mode with - | Partial_application { baker ; _ } - | Application { baker ; _ } - | Full_construction { baker ; _ } -> - baker - | Partial_construction _ -> - Signature.Public_key_hash.zero in + { block_header = { shell = { predecessor ; _ } ; _ } ; baker } + | Full_construction { predecessor ; baker ; _ } + -> predecessor, baker + | Partial_construction { predecessor } + -> predecessor, Signature.Public_key_hash.zero + in Apply.apply_operation ctxt Optimized predecessor baker (Alpha_context.Operation.hash operation) operation >>=? fun (ctxt, result) ->