From a4910835863e3d6be630849f92ea134fbd52c870 Mon Sep 17 00:00:00 2001 From: Marco Stronati Date: Thu, 12 Apr 2018 15:28:03 +0200 Subject: [PATCH] Alpha: added rpc 'proto/delegate//frozen_balances' --- .../lib_protocol/src/alpha_context.mli | 9 ++++++ .../lib_protocol/src/delegate_services.ml | 32 +++++++++++++++++++ .../lib_protocol/src/delegate_services.mli | 3 ++ .../lib_protocol/src/delegate_storage.ml | 23 ++++++++++--- .../lib_protocol/src/delegate_storage.mli | 11 +++++++ 5 files changed, 73 insertions(+), 5 deletions(-) diff --git a/src/proto_alpha/lib_protocol/src/alpha_context.mli b/src/proto_alpha/lib_protocol/src/alpha_context.mli index 5d62224a2..4658c3727 100644 --- a/src/proto_alpha/lib_protocol/src/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/src/alpha_context.mli @@ -550,6 +550,15 @@ module Delegate : sig val frozen_balance: context -> public_key_hash -> Tez.t tzresult Lwt.t + type frozen_balances = { + deposit : Tez.t ; + fees : Tez.t ; + rewards : Tez.t ; + } + + val frozen_balances: + context -> public_key_hash -> frozen_balances tzresult Lwt.t + val full_balance: context -> public_key_hash -> Tez.t tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/src/delegate_services.ml b/src/proto_alpha/lib_protocol/src/delegate_services.ml index e79c1e7b5..72c8c1216 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_services.ml +++ b/src/proto_alpha/lib_protocol/src/delegate_services.ml @@ -322,5 +322,37 @@ module Endorser = struct end +module S = struct + let frozen_balances_encoding = + let open Delegate in + Data_encoding.( + conv + (fun { deposit ; fees ; rewards } -> + (deposit, fees, rewards)) + (fun (deposit, fees, rewards) -> + { deposit ; fees ; rewards } + ) + (obj3 + (req "deposit" Tez.encoding) + (req "fees" Tez.encoding) + (req "rewards" Tez.encoding))) + + let frozen_balances = + RPC_service.post_service + ~description: "Returns the amount of frozen tokens associated to a given key." + ~query: RPC_query.empty + ~input: Data_encoding.empty + ~output: frozen_balances_encoding + RPC_path.(open_root / "delegate" /: Ed25519.Public_key_hash.rpc_arg / "frozen_balances") +end +let () = + let open Services_registration in + register1 S.frozen_balances begin fun ctxt pkh () () -> + Delegate.frozen_balances ctxt pkh + end + +let frozen_balances ctxt block pkh = + RPC_context.make_call1 S.frozen_balances ctxt block pkh () () + let baking_rights = Baker.I.baking_rights let endorsement_rights = Endorser.I.endorsement_rights diff --git a/src/proto_alpha/lib_protocol/src/delegate_services.mli b/src/proto_alpha/lib_protocol/src/delegate_services.mli index 1bd40a0be..54ef254e8 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_services.mli +++ b/src/proto_alpha/lib_protocol/src/delegate_services.mli @@ -9,6 +9,9 @@ open Alpha_context +val frozen_balances : 'a #RPC_context.simple -> 'a -> Ed25519.Public_key_hash.t -> + Delegate.frozen_balances shell_tzresult Lwt.t + module Baker : sig val rights: diff --git a/src/proto_alpha/lib_protocol/src/delegate_storage.ml b/src/proto_alpha/lib_protocol/src/delegate_storage.ml index 9f74a37a6..3990f3a5e 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_storage.ml +++ b/src/proto_alpha/lib_protocol/src/delegate_storage.ml @@ -359,25 +359,38 @@ let has_frozen_balance ctxt delegate cycle = get_frozen_rewards ctxt contract cycle >>=? fun rewards -> return Tez_repr.(rewards <> zero) -let frozen_balance ctxt delegate = +type frozen_balances = { + deposit : Tez_repr.t ; + fees : Tez_repr.t ; + rewards : Tez_repr.t ; +} + +let frozen_balances ctxt delegate = let contract = Contract_repr.implicit_contract delegate in let balance = Ok Tez_repr.zero in Storage.Contract.Frozen_deposits.fold (ctxt, contract) ~init:balance ~f:(fun _cycle amount acc -> Lwt.return acc >>=? fun acc -> - Lwt.return (Tez_repr.(acc +? amount))) >>= fun balance -> + Lwt.return (Tez_repr.(acc +? amount))) >>=? fun deposit -> Storage.Contract.Frozen_fees.fold (ctxt, contract) ~init:balance ~f:(fun _cycle amount acc -> Lwt.return acc >>=? fun acc -> - Lwt.return (Tez_repr.(acc +? amount))) >>= fun balance -> + Lwt.return (Tez_repr.(acc +? amount))) >>=? fun fees -> Storage.Contract.Frozen_rewards.fold (ctxt, contract) ~init:balance ~f:(fun _cycle amount acc -> Lwt.return acc >>=? fun acc -> - Lwt.return (Tez_repr.(acc +? amount))) >>= fun balance -> - Lwt.return balance + Lwt.return (Tez_repr.(acc +? amount))) >>=? fun rewards -> + return { deposit ; fees ; rewards } + +let frozen_balance ctxt delegate = + frozen_balances ctxt delegate >>=? fun { deposit ; fees ; rewards } -> + Error_monad.fold_left_s (fun amount sum -> Lwt.return Tez_repr.(amount +? sum)) + Tez_repr.zero + [deposit; fees; rewards] >>= + Lwt.return let full_balance ctxt delegate = 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 a94953146..b9be3054c 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_storage.mli +++ b/src/proto_alpha/lib_protocol/src/delegate_storage.mli @@ -90,6 +90,17 @@ val frozen_balance: Raw_context.t -> Signature.Public_key_hash.t -> Tez_repr.t tzresult Lwt.t +type frozen_balances = { + deposit : Tez_repr.t ; + fees : Tez_repr.t ; + rewards : Tez_repr.t ; +} + +(** Returns the amount of frozen deposit, fees and rewards associated to a given key. *) +val frozen_balances: + Raw_context.t -> Ed25519.Public_key_hash.t -> + frozen_balances tzresult Lwt.t + (** Returns the full 'balance' of the implicit contract associated to a given key, i.e. the sum of the spendable balance and of the frozen balance. *)