diff --git a/src/proto_alpha/lib_protocol/src/alpha_context.mli b/src/proto_alpha/lib_protocol/src/alpha_context.mli index 5bfb35090..a9bfac038 100644 --- a/src/proto_alpha/lib_protocol/src/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/src/alpha_context.mli @@ -637,13 +637,17 @@ module Delegate : sig } val frozen_balance_encoding: frozen_balance Data_encoding.t - val frozen_balances_encoding: frozen_balance Cycle.Map.t Data_encoding.t + val frozen_balance_by_cycle_encoding: frozen_balance Cycle.Map.t Data_encoding.t - val frozen_balances: + val frozen_balance_by_cycle: context -> Signature.Public_key_hash.t -> frozen_balance Cycle.Map.t Lwt.t - val get_delegated_contracts: + val staking_balance: + context -> Signature.Public_key_hash.t -> + Tez.t tzresult Lwt.t + + val delegated_contracts: context -> Signature.Public_key_hash.t -> Contract_hash.t list 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 6058f9f69..3502a6e78 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_services.ml +++ b/src/proto_alpha/lib_protocol/src/delegate_services.ml @@ -12,9 +12,10 @@ open Alpha_context type info = { balance: Tez.t ; frozen_balance: Tez.t ; - frozen_balances: Delegate.frozen_balance Cycle.Map.t ; - delegated_balance: Tez.t ; + frozen_balance_by_cycle: Delegate.frozen_balance Cycle.Map.t ; + staking_balance: Tez.t ; delegated_contracts: Contract_hash.t list ; + delegated_balance: Tez.t ; deactivated: bool ; grace_period: Cycle.t ; } @@ -22,20 +23,25 @@ type info = { let info_encoding = let open Data_encoding in conv - (fun { balance ; frozen_balance ; frozen_balances ; delegated_balance ; - delegated_contracts ; deactivated ; grace_period } -> - (balance, frozen_balance, frozen_balances, delegated_balance, - delegated_contracts, deactivated, grace_period)) - (fun (balance, frozen_balance, frozen_balances, delegated_balance, - delegated_contracts, deactivated, grace_period) -> - { balance ; frozen_balance ; frozen_balances ; delegated_balance ; - delegated_contracts ; deactivated ; grace_period }) - (obj7 + (fun { balance ; frozen_balance ; frozen_balance_by_cycle ; + staking_balance ; delegated_contracts ; delegated_balance ; + deactivated ; grace_period } -> + (balance, frozen_balance, frozen_balance_by_cycle, + staking_balance, delegated_contracts, delegated_balance, + deactivated, grace_period)) + (fun (balance, frozen_balance, frozen_balance_by_cycle, + staking_balance, delegated_contracts, delegated_balance, + deactivated, grace_period) -> + { balance ; frozen_balance ; frozen_balance_by_cycle ; + staking_balance ; delegated_contracts ; delegated_balance ; + deactivated ; grace_period }) + (obj8 (req "balance" Tez.encoding) (req "frozen_balance" Tez.encoding) - (req "frozen_balances" Delegate.frozen_balances_encoding) - (req "delegated_balance" Tez.encoding) + (req "frozen_balance_by_cycle" Delegate.frozen_balance_by_cycle_encoding) + (req "staking_balance" Tez.encoding) (req "delegated_contracts" (list Contract_hash.encoding)) + (req "delegated_balance" Tez.encoding) (req "deactivated" bool) (req "grace_period" Cycle.encoding)) @@ -92,15 +98,16 @@ module S = struct ~output: Tez.encoding RPC_path.(path / "frozen_balance") - let frozen_balances = + let frozen_balance_by_cycle = RPC_service.get_service ~description: - "Returns the amount of frozen tokens associated to a given delegate." + "Returns the frozen balances of a given delegate, \ + indexed by the cycle by which it will be unfrozen" ~query: RPC_query.empty - ~output: Delegate.frozen_balances_encoding - RPC_path.(path / "frozen_balances") + ~output: Delegate.frozen_balance_by_cycle_encoding + RPC_path.(path / "frozen_balance_by_cycle") - let delegated_balance = + let staking_balance = RPC_service.get_service ~description: "Returns the total amount of token delegated to a given delegate. \ @@ -110,7 +117,7 @@ module S = struct until they are unfrozen." ~query: RPC_query.empty ~output: Tez.encoding - RPC_path.(path / "delegated_balance") + RPC_path.(path / "staking_balance") let delegated_contracts = RPC_service.get_service @@ -120,6 +127,16 @@ module S = struct ~output: (list Contract_hash.encoding) RPC_path.(path / "delegated_contracts") + let delegated_balance = + RPC_service.get_service + ~description: + "The includes the balance of all the contracts that delegates \ + to it. This excludes the delegate own balance and its frozen \ + balances." + ~query: RPC_query.empty + ~output: Tez.encoding + RPC_path.(path / "delegated_balance") + let deactivated = RPC_service.get_service ~description: @@ -164,14 +181,16 @@ let () = register1 S.info begin fun ctxt pkh () () -> Delegate.full_balance ctxt pkh >>=? fun balance -> Delegate.frozen_balance ctxt pkh >>=? fun frozen_balance -> - Delegate.frozen_balances ctxt pkh >>= fun frozen_balances -> + Delegate.frozen_balance_by_cycle ctxt pkh >>= fun frozen_balance_by_cycle -> + Delegate.staking_balance ctxt pkh >>=? fun staking_balance -> + Delegate.delegated_contracts ctxt pkh >>= fun delegated_contracts -> Delegate.delegated_balance ctxt pkh >>=? fun delegated_balance -> - Delegate.get_delegated_contracts ctxt pkh >>= fun delegated_contracts -> Delegate.deactivated ctxt pkh >>= fun deactivated -> Delegate.grace_period ctxt pkh >>=? fun grace_period -> return { - balance ; frozen_balance ; frozen_balances ; delegated_balance ; - delegated_contracts ; deactivated ; grace_period + balance ; frozen_balance ; frozen_balance_by_cycle ; + staking_balance ; delegated_contracts ; delegated_balance ; + deactivated ; grace_period } end ; register1 S.balance begin fun ctxt pkh () () -> @@ -180,15 +199,18 @@ let () = register1 S.frozen_balance begin fun ctxt pkh () () -> Delegate.frozen_balance ctxt pkh end ; - register1 S.frozen_balances begin fun ctxt pkh () () -> - Delegate.frozen_balances ctxt pkh >>= return + register1 S.frozen_balance_by_cycle begin fun ctxt pkh () () -> + Delegate.frozen_balance_by_cycle ctxt pkh >>= return + end ; + register1 S.staking_balance begin fun ctxt pkh () () -> + Delegate.staking_balance ctxt pkh + end ; + register1 S.delegated_contracts begin fun ctxt pkh () () -> + Delegate.delegated_contracts ctxt pkh >>= return end ; register1 S.delegated_balance begin fun ctxt pkh () () -> Delegate.delegated_balance ctxt pkh end ; - register1 S.delegated_contracts begin fun ctxt pkh () () -> - Delegate.get_delegated_contracts ctxt pkh >>= return - end ; register1 S.deactivated begin fun ctxt pkh () () -> Delegate.deactivated ctxt pkh >>= return end ; @@ -208,15 +230,18 @@ let balance ctxt block pkh = let frozen_balance ctxt block pkh = RPC_context.make_call1 S.frozen_balance ctxt block pkh () () -let frozen_balances ctxt block pkh = - RPC_context.make_call1 S.frozen_balances ctxt block pkh () () +let frozen_balance_by_cycle ctxt block pkh = + RPC_context.make_call1 S.frozen_balance_by_cycle ctxt block pkh () () -let delegated_balance ctxt block pkh = - RPC_context.make_call1 S.delegated_balance ctxt block pkh () () +let staking_balance ctxt block pkh = + RPC_context.make_call1 S.staking_balance ctxt block pkh () () let delegated_contracts ctxt block pkh = RPC_context.make_call1 S.delegated_contracts ctxt block pkh () () +let delegated_balance ctxt block pkh = + RPC_context.make_call1 S.delegated_balance ctxt block pkh () () + let deactivated ctxt block pkh = RPC_context.make_call1 S.deactivated ctxt block pkh () () diff --git a/src/proto_alpha/lib_protocol/src/delegate_services.mli b/src/proto_alpha/lib_protocol/src/delegate_services.mli index d9c13b9c5..56c4cf37d 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_services.mli +++ b/src/proto_alpha/lib_protocol/src/delegate_services.mli @@ -18,9 +18,10 @@ val list: type info = { balance: Tez.t ; frozen_balance: Tez.t ; - frozen_balances: Delegate.frozen_balance Cycle.Map.t ; - delegated_balance: Tez.t ; + frozen_balance_by_cycle: Delegate.frozen_balance Cycle.Map.t ; + staking_balance: Tez.t ; delegated_contracts: Contract_hash.t list ; + delegated_balance: Tez.t ; deactivated: bool ; grace_period: Cycle.t ; } @@ -42,12 +43,12 @@ val frozen_balance: Signature.Public_key_hash.t -> Tez.t shell_tzresult Lwt.t -val frozen_balances: +val frozen_balance_by_cycle: 'a #RPC_context.simple -> 'a -> Signature.Public_key_hash.t -> Delegate.frozen_balance Cycle.Map.t shell_tzresult Lwt.t -val delegated_balance: +val staking_balance: 'a #RPC_context.simple -> 'a -> Signature.Public_key_hash.t -> Tez.t shell_tzresult Lwt.t @@ -57,6 +58,11 @@ val delegated_contracts: Signature.Public_key_hash.t -> Contract_hash.t list shell_tzresult Lwt.t +val delegated_balance: + 'a #RPC_context.simple -> 'a -> + Signature.Public_key_hash.t -> + Tez.t shell_tzresult Lwt.t + val deactivated: 'a #RPC_context.simple -> 'a -> Signature.Public_key_hash.t -> diff --git a/src/proto_alpha/lib_protocol/src/delegate_storage.ml b/src/proto_alpha/lib_protocol/src/delegate_storage.ml index 974b0b5eb..02abd5b34 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_storage.ml +++ b/src/proto_alpha/lib_protocol/src/delegate_storage.ml @@ -193,7 +193,7 @@ let remove ctxt contract = let fold = Storage.Delegates.fold let list = Storage.Delegates.elements -let get_delegated_contracts ctxt delegate = +let delegated_contracts ctxt delegate = let contract = Contract_repr.implicit_contract delegate in Storage.Contract.Delegated.elements (ctxt, contract) @@ -368,7 +368,7 @@ let frozen_balance_encoding = (req "fees" Tez_repr.encoding) (req "rewards" Tez_repr.encoding)) -let frozen_balances_encoding = +let frozen_balance_by_cycle_encoding = let open Data_encoding in conv (Cycle_repr.Map.bindings) @@ -384,7 +384,7 @@ let empty_frozen_balance = fees = Tez_repr.zero ; rewards = Tez_repr.zero } -let frozen_balances ctxt delegate = +let frozen_balance_by_cycle ctxt delegate = let contract = Contract_repr.implicit_contract delegate in let map = Cycle_repr.Map.empty in Storage.Contract.Frozen_deposits.fold @@ -449,10 +449,26 @@ let grace_period ctxt delegate = let contract = Contract_repr.implicit_contract delegate in Storage.Contract.Delegate_desactivation.get ctxt contract -let delegated_balance ctxt delegate = +let staking_balance ctxt delegate = let token_per_rolls = Constants_storage.tokens_per_roll ctxt in Roll_storage.get_rolls ctxt delegate >>=? fun rolls -> Roll_storage.get_change ctxt delegate >>=? fun change -> let rolls = Int64.of_int (List.length rolls) in Lwt.return Tez_repr.(token_per_rolls *? rolls) >>=? fun balance -> Lwt.return Tez_repr.(balance +? change) + +let delegated_balance ctxt delegate = + let contract = Contract_repr.implicit_contract delegate in + staking_balance ctxt delegate >>=? fun staking_balance -> + Storage.Contract.Balance.get ctxt contract >>= fun self_staking_balance -> + Storage.Contract.Frozen_deposits.fold + (ctxt, contract) ~init:self_staking_balance + ~f:(fun _cycle amount acc -> + Lwt.return acc >>=? fun acc -> + Lwt.return (Tez_repr.(acc +? amount))) >>= fun self_staking_balance -> + Storage.Contract.Frozen_fees.fold + (ctxt, contract) ~init:self_staking_balance + ~f:(fun _cycle amount acc -> + Lwt.return acc >>=? fun acc -> + Lwt.return (Tez_repr.(acc +? amount))) >>=? fun self_staking_balance -> + Lwt.return Tez_repr.(staking_balance -? self_staking_balance) diff --git a/src/proto_alpha/lib_protocol/src/delegate_storage.mli b/src/proto_alpha/lib_protocol/src/delegate_storage.mli index a8dfb3002..abe18728b 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_storage.mli +++ b/src/proto_alpha/lib_protocol/src/delegate_storage.mli @@ -107,12 +107,13 @@ type frozen_balance = { } val frozen_balance_encoding: frozen_balance Data_encoding.t -val frozen_balances_encoding: frozen_balance Cycle_repr.Map.t Data_encoding.t +val frozen_balance_by_cycle_encoding: + frozen_balance Cycle_repr.Map.t Data_encoding.t (** Returns the amount of frozen deposit, fees and rewards associated to a given delegate, indexed by the cycle by which at the end the balance will be unfrozen. *) -val frozen_balances: +val frozen_balance_by_cycle: Raw_context.t -> Signature.Public_key_hash.t -> frozen_balance Cycle_repr.Map.t Lwt.t @@ -123,8 +124,12 @@ val full_balance: Raw_context.t -> Signature.Public_key_hash.t -> Tez_repr.t tzresult Lwt.t +val staking_balance: + Raw_context.t -> Signature.Public_key_hash.t -> + Tez_repr.t tzresult Lwt.t + (** Returns the list of contract that delegated towards a given delegate *) -val get_delegated_contracts: +val delegated_contracts: Raw_context.t -> Signature.Public_key_hash.t -> Contract_hash.t list Lwt.t diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index 2d9d3816d..2cae7aec8 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -103,7 +103,7 @@ module Contract = struct invalid_arg "get_balance: no frozen accounts for an originated contract." | Some pkh -> - Alpha_services.Delegate.frozen_balances + Alpha_services.Delegate.frozen_balance_by_cycle rpc_ctxt ctxt pkh >>=? fun map -> Lwt.return @@ Cycle.Map.fold