diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 0a837114015f1b52801496bbdb4e3ab1a9dbbd9a..d089c29096dac19e9b32ecf45cc7a88c38e82d40 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2112,8 +2112,6 @@ module Delegate : sig Level.t -> (context * punishing_amounts) tzresult Lwt.t - val full_balance : context -> public_key_hash -> Tez.t tzresult Lwt.t - type level_participation = Participated | Didn't_participate val record_baking_activity_and_pay_rewards_and_fees : @@ -2140,8 +2138,6 @@ module Delegate : sig (** See {!Contract_delegate_storage.delegated_contracts}. *) val delegated_contracts : context -> public_key_hash -> Contract.t list Lwt.t - val delegated_balance : context -> public_key_hash -> Tez.t tzresult Lwt.t - val registered : context -> public_key_hash -> bool Lwt.t val deactivated : context -> public_key_hash -> bool tzresult Lwt.t @@ -2237,6 +2233,26 @@ module Delegate : sig Tez.t -> (context * Receipt.balance_updates) tzresult Lwt.t end + + (** The functions in this module are considered too costly to be used in + the protocol. + They are meant to be used only to answer RPC calls. *) + module For_RPC : sig + (** Returns the full 'balance' of the implicit contract associated to a + given key, i.e. the sum of the spendable balance (given by [balance] or + [Contract_storage.get_balance]) and of the frozen balance of the + contract. + + The frozen balance is composed of all frozen bonds associated to the + contract (given by [Contract_storage.get_frozen_bonds]), all unstaked + frozen deposits, and of the fraction of the frozen deposits that + actually belongs to the delegate and not to its costakers. + + Only use this function for RPCs: this is expensive. *) + val full_balance : context -> public_key_hash -> Tez.t tzresult Lwt.t + + val delegated_balance : context -> public_key_hash -> Tez.t tzresult Lwt.t + end end (** This module re-exports definitions from {!Voting_period_repr} and diff --git a/src/proto_alpha/lib_protocol/delegate_services.ml b/src/proto_alpha/lib_protocol/delegate_services.ml index 497daaa32d3e89cd0022bf89235d07cd419b5446..3689d8540f20ddb982a91e4639f97180223a4afa 100644 --- a/src/proto_alpha/lib_protocol/delegate_services.ml +++ b/src/proto_alpha/lib_protocol/delegate_services.ml @@ -460,11 +460,11 @@ let register () = return delegates) ; register1 ~chunked:false S.info (fun ctxt pkh () () -> check_delegate_registered ctxt pkh >>=? fun () -> - Delegate.full_balance ctxt pkh >>=? fun full_balance -> + Delegate.For_RPC.full_balance ctxt pkh >>=? fun full_balance -> Delegate.frozen_deposits ctxt pkh >>=? fun frozen_deposits -> 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.For_RPC.delegated_balance ctxt pkh >>=? fun delegated_balance -> Delegate.deactivated ctxt pkh >>=? fun deactivated -> Delegate.last_cycle_before_deactivation ctxt pkh >>=? fun grace_period -> Vote.get_delegate_info ctxt pkh >>=? fun voting_info -> @@ -488,7 +488,7 @@ let register () = }) ; register1 ~chunked:false S.full_balance (fun ctxt pkh () () -> trace (Balance_rpc_non_delegate pkh) (check_delegate_registered ctxt pkh) - >>=? fun () -> Delegate.full_balance ctxt pkh) ; + >>=? fun () -> Delegate.For_RPC.full_balance ctxt pkh) ; register1 ~chunked:false S.current_frozen_deposits (fun ctxt pkh () () -> check_delegate_registered ctxt pkh >>=? fun () -> Delegate.frozen_deposits ctxt pkh >>=? fun deposits -> @@ -521,7 +521,7 @@ let register () = Delegate.delegated_contracts ctxt pkh >|= ok) ; register1 ~chunked:false S.delegated_balance (fun ctxt pkh () () -> check_delegate_registered ctxt pkh >>=? fun () -> - Delegate.delegated_balance ctxt pkh) ; + Delegate.For_RPC.delegated_balance ctxt pkh) ; register1 ~chunked:false S.deactivated (fun ctxt pkh () () -> check_delegate_registered ctxt pkh >>=? fun () -> Delegate.deactivated ctxt pkh) ; diff --git a/src/proto_alpha/lib_protocol/delegate_storage.ml b/src/proto_alpha/lib_protocol/delegate_storage.ml index f0d8f3e861fe03a80eeb3e1691f72af481c660cf..9c7156d63b30e043d9c1217d8d4ea1b68815d0e2 100644 --- a/src/proto_alpha/lib_protocol/delegate_storage.ml +++ b/src/proto_alpha/lib_protocol/delegate_storage.ml @@ -268,19 +268,6 @@ let reset_forbidden_delegates ctxt = then Lwt.return ctxt else set_forbidden_delegates ctxt Signature.Public_key_hash.Set.empty -let full_balance ctxt delegate = - frozen_deposits ctxt delegate >>=? fun frozen_deposits -> - let delegate_contract = Contract_repr.Implicit delegate in - Contract_storage.get_balance_and_frozen_bonds ctxt delegate_contract - >>=? fun balance_and_frozen_bonds -> - Lwt.return - Tez_repr.(frozen_deposits.current_amount +? balance_and_frozen_bonds) - -let delegated_balance ctxt delegate = - staking_balance ctxt delegate >>=? fun staking_balance -> - full_balance ctxt delegate >>=? fun self_staking_balance -> - Lwt.return Tez_repr.(staking_balance -? self_staking_balance) - let drain ctxt ~delegate ~destination = let open Lwt_result_syntax in let destination_contract = Contract_repr.Implicit destination in @@ -312,3 +299,44 @@ let drain ctxt ~delegate ~destination = not is_destination_allocated, fees, balance_updates1 @ balance_updates2 ) + +module For_RPC = struct + let full_balance ctxt delegate = + Staking_pseudotokens_storage.costaking_pseudotokens_balance + ctxt + (Contract_repr.Implicit delegate) + >>=? fun pseudotokens -> + Staking_pseudotokens_storage.tez_of_frozen_deposits_pseudotokens + ctxt + delegate + pseudotokens + >>=? fun own_frozen_deposits -> + (Unstake_requests_storage.prepare_finalize_unstake + ctxt + (Contract_repr.Implicit delegate) + >>=? function + | None -> return Tez_repr.zero + | Some {finalizable; unfinalizable} -> + Lwt.return + ( List.fold_left_e + (fun acc (_cycle, tz) -> Tez_repr.(acc +? tz)) + Tez_repr.zero + unfinalizable.requests + >>? fun sum_unfinalizable -> + List.fold_left_e + (fun acc (_, _cycle, tz) -> Tez_repr.(acc +? tz)) + sum_unfinalizable + finalizable )) + >>=? fun unstaked_frozen -> + Lwt.return Tez_repr.(own_frozen_deposits +? unstaked_frozen) + >>=? fun all_frozen -> + let delegate_contract = Contract_repr.Implicit delegate in + Contract_storage.get_balance_and_frozen_bonds ctxt delegate_contract + >>=? fun balance_and_frozen_bonds -> + Lwt.return Tez_repr.(all_frozen +? balance_and_frozen_bonds) + + let delegated_balance ctxt delegate = + staking_balance ctxt delegate >>=? fun staking_balance -> + full_balance ctxt delegate >>=? fun self_staking_balance -> + Lwt.return Tez_repr.(staking_balance -? self_staking_balance) +end diff --git a/src/proto_alpha/lib_protocol/delegate_storage.mli b/src/proto_alpha/lib_protocol/delegate_storage.mli index a24132e3a9ed3c85b439f35d0f7259b6e6e95321..a9248eaad9e65d49cbadc0ffde263c36c5332be1 100644 --- a/src/proto_alpha/lib_protocol/delegate_storage.mli +++ b/src/proto_alpha/lib_protocol/delegate_storage.mli @@ -137,24 +137,30 @@ val load_forbidden_delegates : Raw_context.t -> Raw_context.t tzresult Lwt.t in-memory. *) val reset_forbidden_delegates : Raw_context.t -> Raw_context.t Lwt.t -(** Returns the full 'balance' of the implicit contract associated to - a given key, i.e. the sum of the spendable balance (given by [balance] or - [Contract_storage.get_balance]) and of the frozen balance. The frozen - balance is composed of all frozen bonds associated to the contract (given by - [Contract_storage.get_frozen_bonds]) and of the frozen deposits (given by - [frozen_deposits]). - - Only use this function for RPCs: this is expensive. *) -val full_balance : - Raw_context.t -> Signature.Public_key_hash.t -> Tez_repr.t tzresult Lwt.t - -(** Only use this function for RPCs: this is expensive. *) -val delegated_balance : - Raw_context.t -> Signature.Public_key_hash.t -> Tez_repr.t tzresult Lwt.t - val drain : Raw_context.t -> delegate:Signature.Public_key_hash.t -> destination:Signature.Public_key_hash.t -> (Raw_context.t * bool * Tez_repr.t * Receipt_repr.balance_updates) tzresult Lwt.t + +(** The functions in this module are considered too costly to be used in + the protocol. + They are meant to be used only to answer RPC calls. +*) +module For_RPC : sig + (** Returns the full 'balance' of the implicit contract associated to + a given key, i.e. the sum of the spendable balance (given by [balance] or + [Contract_storage.get_balance]) and of the frozen balance. The frozen + balance is composed of all frozen bonds associated to the contract (given by + [Contract_storage.get_frozen_bonds]) and of the part of the frozen deposits + (given by [frozen_deposits]) that belongs to the delegate. + + Only use this function for RPCs: this is expensive. *) + val full_balance : + Raw_context.t -> Signature.Public_key_hash.t -> Tez_repr.t tzresult Lwt.t + + (** Only use this function for RPCs: this is expensive. *) + val delegated_balance : + Raw_context.t -> Signature.Public_key_hash.t -> Tez_repr.t tzresult Lwt.t +end diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml index 4b0ead290976950a3f1ae62cd7e876b7ab094861..0fcb1c611786825b0b0a0ba832e91d765c512f41 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_delegation.ml @@ -328,7 +328,7 @@ let delegated_implicit_bootstrap_contract () = (* Test delegation amount *) Incremental.begin_construction b >>=? fun i -> let ctxt = Incremental.alpha_ctxt i in - Delegate.delegated_balance ctxt to_pkh >|= Environment.wrap_tzresult + Delegate.For_RPC.delegated_balance ctxt to_pkh >|= Environment.wrap_tzresult >>=? fun amount -> Assert.equal_tez ~loc:__LOC__ amount (Tez.of_mutez_exn 4_000_000_000_000L) diff --git a/src/proto_alpha/lib_protocol/test/integration/test_frozen_bonds.ml b/src/proto_alpha/lib_protocol/test/integration/test_frozen_bonds.ml index 8aeeee5514e090abd26683c80c94fbb6e40b011c..c54f5e484b7a1f3c913ecebc4df547f4ba72505f 100644 --- a/src/proto_alpha/lib_protocol/test/integration/test_frozen_bonds.ml +++ b/src/proto_alpha/lib_protocol/test/integration/test_frozen_bonds.ml @@ -304,7 +304,8 @@ let test_total_stake ~user_is_delegate () = let check_delegated_balance_is ctxt ~loc delegate expected_balance = (* Fetch the delegated balance of d. *) - Delegate.delegated_balance ctxt delegate >>>=? fun delegated_balance -> + Delegate.For_RPC.delegated_balance ctxt delegate + >>>=? fun delegated_balance -> (* Check that the delegated balance of [delegate] is as explected. *) Assert.equal_tez ~loc delegated_balance expected_balance