From 0ac1e1e842b58bd922fe1cc6c6e388e8b38bae2a Mon Sep 17 00:00:00 2001 From: Marco Stronati Date: Wed, 27 Jun 2018 17:25:58 +0200 Subject: [PATCH] Alpha: richer block receipt Added: - nonce_hash - consumed_gas - deactivated delegats at end cycle - unfrozen balances at end cycle --- .../lib_protocol/src/alpha_context.mli | 3 +- src/proto_alpha/lib_protocol/src/apply.ml | 58 ++++++++++++------- .../lib_protocol/src/apply_results.ml | 24 ++++++-- .../lib_protocol/src/apply_results.mli | 4 ++ .../lib_protocol/src/delegate_storage.ml | 41 +++++++------ .../lib_protocol/src/delegate_storage.mli | 9 +-- src/proto_alpha/lib_protocol/src/main.ml | 21 +++++-- .../lib_protocol/src/seed_storage.mli | 3 + 8 files changed, 110 insertions(+), 53 deletions(-) diff --git a/src/proto_alpha/lib_protocol/src/alpha_context.mli b/src/proto_alpha/lib_protocol/src/alpha_context.mli index 26e92e8bc..e1e2f3b0e 100644 --- a/src/proto_alpha/lib_protocol/src/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/src/alpha_context.mli @@ -634,7 +634,8 @@ module Delegate : sig context -> public_key_hash -> Tez.t -> context tzresult Lwt.t val cycle_end: - context -> Cycle.t -> Nonce.unrevealed list -> context tzresult Lwt.t + context -> Cycle.t -> Nonce.unrevealed list -> + (context * balance_updates * Signature.Public_key_hash.t list) tzresult Lwt.t type frozen_balance = { deposit : Tez.t ; diff --git a/src/proto_alpha/lib_protocol/src/apply.ml b/src/proto_alpha/lib_protocol/src/apply.ml index 73e9eca44..0829fcd91 100644 --- a/src/proto_alpha/lib_protocol/src/apply.ml +++ b/src/proto_alpha/lib_protocol/src/apply.ml @@ -686,10 +686,10 @@ let apply_contents_list let level = Level.from_raw ctxt level in return (ctxt, Single_result (Endorsement_result - { balance_updates = - [ Contract (Contract.implicit_contract delegate), Debited deposit; - Deposits (delegate, level.cycle), Credited deposit; - Rewards (delegate, level.cycle), Credited reward; ] ; + { balance_updates = Delegate.cleanup_balance_updates + [ Contract (Contract.implicit_contract delegate), Debited deposit; + Deposits (delegate, level.cycle), Credited deposit; + Rewards (delegate, level.cycle), Credited reward; ] ; delegate ; slots })) | Single (Seed_nonce_revelation { level ; nonce }) -> let level = Level.from_raw ctxt level in @@ -733,11 +733,12 @@ let apply_contents_list add_rewards ctxt reward >>=? fun ctxt -> let current_cycle = (Level.current ctxt).cycle in return (ctxt, Single_result - (Double_endorsement_evidence_result [ - Deposits (delegate1, level.cycle), Debited balance.deposit ; - Fees (delegate1, level.cycle), Debited balance.fees ; - Rewards (delegate1, level.cycle), Debited balance.rewards ; - Rewards (baker, current_cycle), Credited reward ])) + (Double_endorsement_evidence_result + (Delegate.cleanup_balance_updates [ + Deposits (delegate1, level.cycle), Debited balance.deposit ; + Fees (delegate1, level.cycle), Debited balance.fees ; + Rewards (delegate1, level.cycle), Debited balance.rewards ; + Rewards (baker, current_cycle), Credited reward ]))) | _, _ -> fail Invalid_double_endorsement_evidence end | Single (Double_baking_evidence { bh1 ; bh2 }) -> @@ -786,11 +787,12 @@ let apply_contents_list add_rewards ctxt reward >>=? fun ctxt -> let current_cycle = (Level.current ctxt).cycle in return (ctxt, Single_result - (Double_baking_evidence_result [ - Deposits (delegate, level.cycle), Debited balance.deposit ; - Fees (delegate, level.cycle), Debited balance.fees ; - Rewards (delegate, level.cycle), Debited balance.rewards ; - Rewards (baker, current_cycle), Credited reward ; ])) + (Double_baking_evidence_result + (Delegate.cleanup_balance_updates [ + Deposits (delegate, level.cycle), Debited balance.deposit ; + Fees (delegate, level.cycle), Debited balance.fees ; + Rewards (delegate, level.cycle), Debited balance.rewards ; + Rewards (baker, current_cycle), Credited reward ; ]))) | Single (Activate_account { id = pkh ; activation_code }) -> begin let blinded_pkh = Blinded_public_key_hash.of_ed25519_pkh activation_code pkh in @@ -851,13 +853,13 @@ let may_snapshot_roll ctxt = let may_start_new_cycle ctxt = Baking.dawn_of_a_new_cycle ctxt >>=? function - | None -> return ctxt + | None -> return (ctxt, [], []) | Some last_cycle -> Seed.cycle_end ctxt last_cycle >>=? fun (ctxt, unrevealed) -> Roll.cycle_end ctxt last_cycle >>=? fun ctxt -> - Delegate.cycle_end ctxt last_cycle unrevealed >>=? fun ctxt -> + Delegate.cycle_end ctxt last_cycle unrevealed >>=? fun (ctxt, update_balances, deactivated) -> Bootstrap.cycle_end ctxt last_cycle >>=? fun ctxt -> - return ctxt + return (ctxt, update_balances, deactivated) let begin_full_construction ctxt pred_timestamp protocol_data = Baking.check_baking_rights @@ -905,7 +907,8 @@ let begin_application ctxt chain_id block_header pred_timestamp = let finalize_application ctxt protocol_data delegate = let deposit = Constants.block_security_deposit ctxt in add_deposit ctxt delegate deposit >>=? fun ctxt -> - add_rewards ctxt (Constants.block_reward ctxt) >>=? fun ctxt -> + let reward = (Constants.block_reward ctxt) in + add_rewards ctxt reward >>=? fun ctxt -> Signature.Public_key_hash.Map.fold (fun delegate deposit ctxt -> ctxt >>=? fun ctxt -> @@ -928,6 +931,21 @@ let finalize_application ctxt protocol_data delegate = ctxt protocol_data.priority >>=? fun ctxt -> (* end of cycle *) may_snapshot_roll ctxt >>=? fun ctxt -> - may_start_new_cycle ctxt >>=? fun ctxt -> + may_start_new_cycle ctxt >>=? fun (ctxt, balance_updates, deactivated) -> Amendment.may_start_new_voting_cycle ctxt >>=? fun ctxt -> - return ctxt + let cycle = (Level.current ctxt).cycle in + let balance_updates = + Delegate.(cleanup_balance_updates + ([ Contract (Contract.implicit_contract delegate), Debited deposit ; + Deposits (delegate, cycle), Credited deposit ; + Rewards (delegate, cycle), Credited reward ] @ balance_updates)) in + let consumed_gas = Z.sub (Constants.hard_gas_limit_per_block ctxt) (Alpha_context.Gas.block_level ctxt) in + Alpha_context.Vote.get_current_period_kind ctxt >>=? fun voting_period_kind -> + let receipt = Apply_results.{ baker = delegate ; + level = Level.current ctxt; + voting_period_kind ; + nonce_hash = protocol_data.seed_nonce_hash ; + consumed_gas ; + deactivated ; + balance_updates } in + return (ctxt, receipt) diff --git a/src/proto_alpha/lib_protocol/src/apply_results.ml b/src/proto_alpha/lib_protocol/src/apply_results.ml index 88403afb0..382ef1d14 100644 --- a/src/proto_alpha/lib_protocol/src/apply_results.ml +++ b/src/proto_alpha/lib_protocol/src/apply_results.ml @@ -912,17 +912,29 @@ type block_metadata = { baker: Signature.Public_key_hash.t ; level: Level.t ; voting_period_kind: Voting_period.kind ; + nonce_hash: Nonce_hash.t option ; + consumed_gas: Z.t ; + deactivated: Signature.Public_key_hash.t list ; + balance_updates: Delegate.balance_updates ; } let block_metadata_encoding = let open Data_encoding in def "block_header.alpha.metadata" @@ conv - (fun { baker ; level ; voting_period_kind} -> - (baker, level, voting_period_kind)) - (fun (baker, level, voting_period_kind) -> - { baker ; level ; voting_period_kind}) - (obj3 + (fun { baker ; level ; voting_period_kind ; nonce_hash ; + consumed_gas ; deactivated ; balance_updates } -> + ( baker, level, voting_period_kind, nonce_hash, + consumed_gas, deactivated, balance_updates )) + (fun ( baker, level, voting_period_kind, nonce_hash, + consumed_gas, deactivated, balance_updates ) -> + { baker ; level ; voting_period_kind ; nonce_hash ; + consumed_gas ; deactivated ; balance_updates }) + (obj7 (req "baker" Signature.Public_key_hash.encoding) (req "level" Level.encoding) - (req "voting_period_kind" Voting_period.kind_encoding)) + (req "voting_period_kind" Voting_period.kind_encoding) + (req "nonce_hash" (option Nonce_hash.encoding)) + (req "consumed_gas" (check_size 10 n)) + (req "deactivated" (list Signature.Public_key_hash.encoding)) + (req "balance_updates" Delegate.balance_updates_encoding)) diff --git a/src/proto_alpha/lib_protocol/src/apply_results.mli b/src/proto_alpha/lib_protocol/src/apply_results.mli index 9889e3986..e3f9117f5 100644 --- a/src/proto_alpha/lib_protocol/src/apply_results.mli +++ b/src/proto_alpha/lib_protocol/src/apply_results.mli @@ -131,5 +131,9 @@ type block_metadata = { baker: Signature.Public_key_hash.t ; level: Level.t ; voting_period_kind: Voting_period.kind ; + nonce_hash: Nonce_hash.t option ; + consumed_gas: Z.t ; + deactivated: Signature.Public_key_hash.t list ; + balance_updates: Delegate.balance_updates ; } val block_metadata_encoding: block_metadata Data_encoding.encoding diff --git a/src/proto_alpha/lib_protocol/src/delegate_storage.ml b/src/proto_alpha/lib_protocol/src/delegate_storage.ml index 185ec8d48..f961117e9 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_storage.ml +++ b/src/proto_alpha/lib_protocol/src/delegate_storage.ml @@ -411,46 +411,53 @@ let unfreeze ctxt delegate cycle = get_frozen_fees ctxt contract cycle >>=? fun fees -> get_frozen_rewards ctxt contract cycle >>=? fun rewards -> Storage.Contract.Balance.get ctxt contract >>=? fun balance -> - Lwt.return Tez_repr.(balance +? deposit) >>=? fun balance -> - Lwt.return Tez_repr.(balance +? fees) >>=? fun balance -> - Lwt.return Tez_repr.(balance +? rewards) >>=? fun balance -> + Lwt.return Tez_repr.(deposit +? fees) >>=? fun unfrozen_amount -> + Lwt.return Tez_repr.(unfrozen_amount +? rewards) >>=? fun unfrozen_amount -> + Lwt.return Tez_repr.(balance +? unfrozen_amount) >>=? fun balance -> Storage.Contract.Balance.set ctxt contract balance >>=? fun ctxt -> Roll_storage.Delegate.add_amount ctxt delegate rewards >>=? fun ctxt -> 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 -> - return ctxt + return (ctxt, (cleanup_balance_updates + [(Deposits (delegate, cycle), Debited deposit) ; + (Fees (delegate, cycle), Debited fees) ; + (Rewards (delegate, cycle), Debited rewards) ; + (Contract (Contract_repr.implicit_contract delegate), Credited unfrozen_amount)])) let cycle_end ctxt last_cycle unrevealed = let preserved = Constants_storage.preserved_cycles ctxt in begin match Cycle_repr.pred last_cycle with - | None -> return ctxt + | None -> return (ctxt,[]) | Some revealed_cycle -> List.fold_left - (fun ctxt (u : Nonce_storage.unrevealed) -> - ctxt >>=? fun ctxt -> + (fun acc (u : Nonce_storage.unrevealed) -> + acc >>=? fun (ctxt, balance_updates) -> burn_fees ctxt u.delegate revealed_cycle u.fees >>=? fun ctxt -> burn_rewards ctxt u.delegate revealed_cycle u.rewards >>=? fun ctxt -> - return ctxt) - (return ctxt) unrevealed - end >>=? fun ctxt -> + let bus = [(Fees (u.delegate, revealed_cycle), Debited u.fees); + (Rewards (u.delegate, revealed_cycle), Debited u.rewards)] in + return (ctxt, bus @ balance_updates)) + (return (ctxt,[])) unrevealed + end >>=? fun (ctxt, balance_updates) -> match Cycle_repr.sub last_cycle preserved with - | None -> return ctxt + | None -> return (ctxt, balance_updates, []) | Some unfrozen_cycle -> fold ctxt - ~init:(Ok ctxt) - ~f:(fun delegate ctxt -> - Lwt.return ctxt >>=? fun ctxt -> - unfreeze ctxt delegate unfrozen_cycle >>=? fun ctxt -> + ~init:(Ok (ctxt, balance_updates, [])) + ~f:(fun delegate acc -> + Lwt.return acc >>=? fun (ctxt, bus, deactivated) -> + unfreeze ctxt delegate unfrozen_cycle >>=? fun (ctxt, balance_updates) -> Storage.Contract.Delegate_desactivation.get ctxt (Contract_repr.implicit_contract delegate) >>=? fun cycle -> if Cycle_repr.(cycle <= last_cycle) then - Roll_storage.Delegate.set_inactive ctxt delegate + Roll_storage.Delegate.set_inactive ctxt delegate >>=? fun ctxt -> + return (ctxt, balance_updates @ bus, delegate::deactivated) else - return ctxt) + return (ctxt, balance_updates @ bus, deactivated)) let punish ctxt delegate cycle = let contract = Contract_repr.implicit_contract delegate in diff --git a/src/proto_alpha/lib_protocol/src/delegate_storage.mli b/src/proto_alpha/lib_protocol/src/delegate_storage.mli index 31125af0a..f8127d725 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_storage.mli +++ b/src/proto_alpha/lib_protocol/src/delegate_storage.mli @@ -106,12 +106,13 @@ val freeze_rewards: Raw_context.t tzresult Lwt.t (** Trigger the context maintenance at the end of cycle 'n', i.e.: - unfroze deposit/fees/rewards from 'n - preserved_cycle' ; punish the - provided unrevealed seeds (tipically seed from from cycle 'n - - 1'). *) + unfreeze deposit/fees/rewards from 'n - preserved_cycle' ; punish the + provided unrevealed seeds (tipically seed from cycle 'n - 1'). + Returns a list of account with the amount that was unfrozen for each + and the list of deactivated delegates. *) val cycle_end: Raw_context.t -> Cycle_repr.t -> Nonce_storage.unrevealed list -> - Raw_context.t tzresult Lwt.t + (Raw_context.t * balance_updates * Signature.Public_key_hash.t list) tzresult Lwt.t (** Burn all then frozen deposit/fees/rewards for a delegate at a given cycle. Returns the burned amounts. *) diff --git a/src/proto_alpha/lib_protocol/src/main.ml b/src/proto_alpha/lib_protocol/src/main.ml index e7dd9a137..e69a9c30f 100644 --- a/src/proto_alpha/lib_protocol/src/main.ml +++ b/src/proto_alpha/lib_protocol/src/main.ml @@ -192,16 +192,28 @@ let finalize_block { mode ; ctxt ; op_count } = (Alpha_context.get_deposits ctxt) (return ctxt) >>=? fun ctxt -> let ctxt = Alpha_context.finalize ctxt in - return (ctxt, Apply_results.{ baker ; level ; voting_period_kind }) + return (ctxt, Apply_results.{ baker ; + level ; + voting_period_kind ; + nonce_hash = None ; + consumed_gas = Z.zero ; + deactivated = []; + balance_updates = []}) | Partial_application { baker ; _ } -> let level = Alpha_context. Level.current ctxt in Alpha_context.Vote.get_current_period_kind ctxt >>=? fun voting_period_kind -> let ctxt = Alpha_context.finalize ctxt in - return (ctxt, Apply_results.{ baker ; level ; voting_period_kind }) + return (ctxt, Apply_results.{ baker ; + level ; + voting_period_kind ; + nonce_hash = None ; + consumed_gas = Z.zero ; + deactivated = []; + balance_updates = []}) | Application { baker ; block_header = { protocol_data = { contents = protocol_data ; _ } ; _ } } | Full_construction { protocol_data ; baker ; _ } -> - Apply.finalize_application ctxt protocol_data baker >>=? fun ctxt -> + Apply.finalize_application ctxt protocol_data baker >>=? fun (ctxt, receipt) -> let level = Alpha_context.Level.current ctxt in let priority = protocol_data.priority in let raw_level = Alpha_context.Raw_level.to_int32 level.level in @@ -210,9 +222,8 @@ let finalize_block { mode ; ctxt ; op_count } = Format.asprintf "lvl %ld, fit %Ld, prio %d, %d ops" raw_level fitness priority op_count in - Alpha_context.Vote.get_current_period_kind ctxt >>=? fun voting_period_kind -> let ctxt = Alpha_context.finalize ~commit_message ctxt in - return (ctxt, Apply_results.{ baker ; level ; voting_period_kind }) + return (ctxt, receipt) let compare_operations op1 op2 = let open Alpha_context in diff --git a/src/proto_alpha/lib_protocol/src/seed_storage.mli b/src/proto_alpha/lib_protocol/src/seed_storage.mli index 8219c59bc..ca46fa8ab 100644 --- a/src/proto_alpha/lib_protocol/src/seed_storage.mli +++ b/src/proto_alpha/lib_protocol/src/seed_storage.mli @@ -20,6 +20,9 @@ val init: val for_cycle: Raw_context.t -> Cycle_repr.t -> Seed_repr.seed tzresult Lwt.t +(** If it is the end of the cycle, computes and stores the seed of cycle at + distance [preserved_cycle+2] in the future using the seed of the previous + cycle and the revelations of the current one. *) val cycle_end: Raw_context.t -> Cycle_repr.t -> (Raw_context.t * Nonce_storage.unrevealed list) tzresult Lwt.t