From 7d73605f5c9c0901210cf5950f58edd18c82fbb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Fri, 20 Apr 2018 23:53:40 +0200 Subject: [PATCH] Alpha: fix endorsement rewards It should be computed with the priority of the endorsed blocks, not with the priority of the block where it is included. --- .../lib_protocol/src/alpha_context.ml | 5 +++++ .../lib_protocol/src/alpha_context.mli | 7 +++++++ src/proto_alpha/lib_protocol/src/apply.ml | 14 ++++++++------ .../lib_protocol/src/helpers_services.ml | 3 +-- .../lib_protocol/src/init_storage.ml | 1 + src/proto_alpha/lib_protocol/src/main.ml | 18 +++++++----------- src/proto_alpha/lib_protocol/src/storage.ml | 6 ++++++ src/proto_alpha/lib_protocol/src/storage.mli | 6 ++++++ .../lib_protocol/test/helpers/helpers_apply.ml | 1 - 9 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/proto_alpha/lib_protocol/src/alpha_context.ml b/src/proto_alpha/lib_protocol/src/alpha_context.ml index 2b3a6bd15..583506008 100644 --- a/src/proto_alpha/lib_protocol/src/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/src/alpha_context.ml @@ -106,6 +106,11 @@ module Commitment = struct include Commitment_storage end +module Global = struct + let get_last_block_priority = Storage.Last_block_priority.get + let set_last_block_priority = Storage.Last_block_priority.set +end + let prepare_first_block = Init_storage.prepare_first_block let prepare = Init_storage.prepare diff --git a/src/proto_alpha/lib_protocol/src/alpha_context.mli b/src/proto_alpha/lib_protocol/src/alpha_context.mli index 37f40e0a1..fa8792e23 100644 --- a/src/proto_alpha/lib_protocol/src/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/src/alpha_context.mli @@ -873,6 +873,13 @@ module Bootstrap : sig end +module Global : sig + + val get_last_block_priority: context -> int tzresult Lwt.t + val set_last_block_priority: context -> int -> context tzresult Lwt.t + +end + val prepare_first_block: Context.t -> level:Int32.t -> diff --git a/src/proto_alpha/lib_protocol/src/apply.ml b/src/proto_alpha/lib_protocol/src/apply.ml index 7b3e94abe..133fda2ea 100644 --- a/src/proto_alpha/lib_protocol/src/apply.ml +++ b/src/proto_alpha/lib_protocol/src/apply.ml @@ -330,7 +330,7 @@ let () = open Apply_operation_result let apply_consensus_operation_content ctxt - pred_block block_priority operation = function + pred_block operation = function | Endorsements { block ; level ; slots } -> begin match Level.pred ctxt (Level.current ctxt) with @@ -354,6 +354,7 @@ let apply_consensus_operation_content ctxt let delegate = Signature.Public_key.hash delegate in let ctxt = Fitness.increase ~gap:(List.length slots) ctxt in Baking.freeze_endorsement_deposit ctxt delegate >>=? fun ctxt -> + Global.get_last_block_priority ctxt >>=? fun block_priority -> Baking.endorsement_reward ctxt ~block_priority >>=? fun reward -> Delegate.freeze_rewards ctxt delegate reward >>=? fun ctxt -> return (ctxt, Endorsements_result (delegate, slots)) @@ -538,7 +539,7 @@ let apply_manager_operations ctxt source ops = apply ctxt applied rest in apply ctxt [] ops -let apply_sourced_operation ctxt pred_block block_prio operation ops = +let apply_sourced_operation ctxt pred_block operation ops = match ops with | Manager_operations { source ; fee ; counter ; operations ; gas_limit } -> let revealed_public_keys = @@ -575,7 +576,7 @@ let apply_sourced_operation ctxt pred_block block_prio operation ops = operation_results }) | Consensus_operation content -> apply_consensus_operation_content ctxt - pred_block block_prio operation content >>=? fun (ctxt, result) -> + pred_block operation content >>=? fun (ctxt, result) -> return (ctxt, Consensus_operation_result result) | Amendment_operation { source ; operation = content } -> Roll.delegate_pubkey ctxt source >>=? fun delegate -> @@ -697,8 +698,7 @@ let apply_anonymous_operation ctxt kind = Contract.(credit ctxt (implicit_contract (Signature.Ed25519 pkh)) amount) >>=? fun ctxt -> return (ctxt, Activation_result [(* FIXME *)]) -let apply_operation - ctxt pred_block block_prio hash operation = +let apply_operation ctxt pred_block hash operation = let ctxt = Contract.init_origination_nonce ctxt hash in begin match operation.contents with | Anonymous_operations ops -> @@ -710,7 +710,7 @@ let apply_operation >>=? fun (ctxt, results) -> return (ctxt, Anonymous_operations_result (List.rev results)) | Sourced_operation ops -> - apply_sourced_operation ctxt pred_block block_prio operation ops + apply_sourced_operation ctxt pred_block operation ops >>=? fun (ctxt, result) -> return (ctxt, Sourced_operation_result result) end >>=? fun (ctxt, result) -> @@ -791,6 +791,8 @@ let finalize_application ctxt protocol_data delegate = Nonce.record_hash ctxt { nonce_hash ; delegate ; rewards ; fees } end >>=? fun ctxt -> + Alpha_context.Global.set_last_block_priority + ctxt protocol_data.priority >>=? fun ctxt -> (* end of cycle *) may_snapshot_roll ctxt >>=? fun ctxt -> may_start_new_cycle ctxt >>=? fun ctxt -> diff --git a/src/proto_alpha/lib_protocol/src/helpers_services.ml b/src/proto_alpha/lib_protocol/src/helpers_services.ml index 990b6499c..f4091e996 100644 --- a/src/proto_alpha/lib_protocol/src/helpers_services.ml +++ b/src/proto_alpha/lib_protocol/src/helpers_services.ml @@ -141,8 +141,7 @@ module I = struct | None -> Error_monad.fail Operation.Cannot_parse_operation | Some (shell, contents) -> let operation = { shell ; contents ; signature } in - let block_prio = 0 in - Apply.apply_operation ctxt pred_block block_prio hash operation + Apply.apply_operation ctxt pred_block hash operation >>=? fun (_, result) -> return result end diff --git a/src/proto_alpha/lib_protocol/src/init_storage.ml b/src/proto_alpha/lib_protocol/src/init_storage.ml index e6227576a..08a043df3 100644 --- a/src/proto_alpha/lib_protocol/src/init_storage.ml +++ b/src/proto_alpha/lib_protocol/src/init_storage.ml @@ -21,6 +21,7 @@ let prepare_first_block ctxt ~level ~timestamp ~fitness = param.bootstrap_accounts >>=? fun ctxt -> Roll_storage.init_first_cycles ctxt >>=? fun ctxt -> Vote_storage.init ctxt >>=? fun ctxt -> + Storage.Last_block_priority.init ctxt 0 >>=? fun ctxt -> return ctxt let prepare ctxt ~level ~timestamp ~fitness = diff --git a/src/proto_alpha/lib_protocol/src/main.ml b/src/proto_alpha/lib_protocol/src/main.ml index b3c663d08..f756720ff 100644 --- a/src/proto_alpha/lib_protocol/src/main.ml +++ b/src/proto_alpha/lib_protocol/src/main.ml @@ -103,19 +103,15 @@ let begin_construction return { mode ; ctxt ; op_count = 0 ; deposit } let apply_operation ({ mode ; ctxt ; op_count ; _ } as data) operation = - let pred_block, block_prio = + let predecessor = match mode with - | Partial_construction { predecessor } -> - predecessor, 0 + | Partial_construction { predecessor } | Application - { block_header = { shell = { predecessor ; _ } ; - protocol_data ; _ } ; _ } - | Full_construction { predecessor ; protocol_data ; _ } -> - predecessor, - protocol_data.priority in - Apply.apply_operation ctxt pred_block block_prio - (Alpha_context.Operation.hash operation) operation - >>=? fun (ctxt, _) -> + { block_header = { shell = { predecessor ; _ } ; _ } ; _ } + | Full_construction { predecessor ; _ } -> + predecessor in + Apply.apply_operation ctxt predecessor + (Alpha_context.Operation.hash operation) operation >>=? fun (ctxt, _) -> let op_count = op_count + 1 in return { data with ctxt ; op_count } diff --git a/src/proto_alpha/lib_protocol/src/storage.ml b/src/proto_alpha/lib_protocol/src/storage.ml index 8ca742824..7d427600d 100644 --- a/src/proto_alpha/lib_protocol/src/storage.ml +++ b/src/proto_alpha/lib_protocol/src/storage.ml @@ -39,6 +39,12 @@ module String_index = struct | [] | _ :: _ :: _ -> None end +module Last_block_priority = + Make_single_data_storage + (Raw_context) + (struct let name = ["last_block_priority"] end) + (Make_value(Int)) + (** Contracts handling *) module Contract = struct diff --git a/src/proto_alpha/lib_protocol/src/storage.mli b/src/proto_alpha/lib_protocol/src/storage.mli index c0e6ad3e4..7c982b93c 100644 --- a/src/proto_alpha/lib_protocol/src/storage.mli +++ b/src/proto_alpha/lib_protocol/src/storage.mli @@ -20,6 +20,12 @@ open Storage_sigs +module Last_block_priority : sig + val get : Raw_context.t -> int tzresult Lwt.t + val set : Raw_context.t -> int -> Raw_context.t tzresult Lwt.t + val init : Raw_context.t -> int -> Raw_context.t tzresult Lwt.t +end + module Roll : sig (** Storage from this submodule must only be accessed through the 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 d75baa355..0a759d6a9 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/helpers_apply.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/helpers_apply.ml @@ -40,7 +40,6 @@ let operation Proto_alpha.Apply.apply_operation tc pred_block_hash - 0 hash operation >>=? bind_result