diff --git a/src/proto_alpha/lib_protocol/src/alpha_context.mli b/src/proto_alpha/lib_protocol/src/alpha_context.mli index 536f4ceb7..35feeffd6 100644 --- a/src/proto_alpha/lib_protocol/src/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/src/alpha_context.mli @@ -104,6 +104,8 @@ module Cycle : sig val sub: cycle -> int -> cycle option val to_int32: cycle -> int32 + module Map : S.MAP with type key = cycle + end module Gas : sig @@ -617,6 +619,9 @@ module Delegate : sig context -> public_key_hash -> Cycle.t -> (context * Tez.t) tzresult Lwt.t + val full_balance: + context -> public_key_hash -> Tez.t tzresult Lwt.t + val has_frozen_balance: context -> public_key_hash -> Cycle.t -> bool tzresult Lwt.t @@ -624,17 +629,34 @@ module Delegate : sig val frozen_balance: context -> public_key_hash -> Tez.t tzresult Lwt.t - type frozen_balances = { + type frozen_balance = { deposit : Tez.t ; fees : Tez.t ; rewards : Tez.t ; } - val frozen_balances: - context -> public_key_hash -> frozen_balances tzresult Lwt.t + val frozen_balance_encoding: frozen_balance Data_encoding.t + val frozen_balances_encoding: frozen_balance Cycle.Map.t Data_encoding.t - val full_balance: - context -> public_key_hash -> Tez.t tzresult Lwt.t + val frozen_balances: + context -> Signature.Public_key_hash.t -> + frozen_balance Cycle.Map.t Lwt.t + + val get_delegated_contracts: + context -> Signature.Public_key_hash.t -> + Contract_hash.t list Lwt.t + + val delegated_balance: + context -> Signature.Public_key_hash.t -> + Tez.t tzresult Lwt.t + + val deactivated: + context -> Signature.Public_key_hash.t -> + bool Lwt.t + + val grace_period: + context -> Signature.Public_key_hash.t -> + Cycle.t tzresult Lwt.t end @@ -865,6 +887,11 @@ end module Roll : sig + type t = private int32 + type roll = t + + val encoding: roll Data_encoding.t + val snapshot_rolls: context -> context tzresult Lwt.t val cycle_end: context -> Cycle.t -> context tzresult Lwt.t @@ -877,6 +904,11 @@ module Roll : sig val delegate_pubkey: context -> public_key_hash -> public_key tzresult Lwt.t + val get_rolls: + context -> Signature.Public_key_hash.t -> roll list tzresult Lwt.t + val get_change: + context -> Signature.Public_key_hash.t -> Tez.t tzresult Lwt.t + end module Commitment : sig diff --git a/src/proto_alpha/lib_protocol/src/cycle_repr.ml b/src/proto_alpha/lib_protocol/src/cycle_repr.ml index cadd30a90..d6179d538 100644 --- a/src/proto_alpha/lib_protocol/src/cycle_repr.ml +++ b/src/proto_alpha/lib_protocol/src/cycle_repr.ml @@ -28,6 +28,8 @@ let pp ppf cycle = Format.fprintf ppf "%ld" cycle include (Compare.Int32 : Compare.S with type t := t) +module Map = Map.Make(Compare.Int32) + let root = 0l let succ = Int32.succ let pred = function diff --git a/src/proto_alpha/lib_protocol/src/cycle_repr.mli b/src/proto_alpha/lib_protocol/src/cycle_repr.mli index 724780989..3e4308d4f 100644 --- a/src/proto_alpha/lib_protocol/src/cycle_repr.mli +++ b/src/proto_alpha/lib_protocol/src/cycle_repr.mli @@ -23,6 +23,8 @@ val succ: cycle -> cycle val to_int32: cycle -> int32 val of_int32_exn: int32 -> cycle +module Map : S.MAP with type key = cycle + module Index : sig (* Storage_functors.INDEX with type t = cycle *) type t = cycle diff --git a/src/proto_alpha/lib_protocol/src/delegate_services.ml b/src/proto_alpha/lib_protocol/src/delegate_services.ml index fb748a760..22d8e605e 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_services.ml +++ b/src/proto_alpha/lib_protocol/src/delegate_services.ml @@ -16,6 +16,221 @@ let slots_range_encoding = (opt "first_level" Raw_level.encoding) (opt "last_level" Raw_level.encoding)) +type info = { + balance: Tez.t ; + frozen_balance: Tez.t ; + frozen_balances: Delegate.frozen_balance Cycle.Map.t ; + delegated_balance: Tez.t ; + delegated_contracts: Contract_hash.t list ; + deactivated: bool ; + grace_period: Cycle.t ; +} + +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 + (req "balance" Tez.encoding) + (req "frozen_balance" Tez.encoding) + (req "frozen_balances" Delegate.frozen_balances_encoding) + (req "delegated_balance" Tez.encoding) + (req "delegated_contracts" (list Contract_hash.encoding)) + (req "deactivated" bool) + (req "grace_period" Cycle.encoding)) + +module S = struct + + let path = RPC_path.(open_root / "delegate") + + open Data_encoding + + type list_query = { + active: bool ; + inactive: bool ; + } + let list_query :list_query RPC_query.t = + let open RPC_query in + query (fun active inactive -> { active ; inactive }) + |+ flag "active" (fun t -> t.active) + |+ flag "inactive" (fun t -> t.inactive) + |> seal + + let list_delegate = + RPC_service.get_service + ~description: + "List all registred delegates." + ~query: list_query + ~output: (list Signature.Public_key_hash.encoding) + path + + let path = RPC_path.(path /: Signature.Public_key_hash.rpc_arg) + + let info = + RPC_service.get_service + ~description: + "Everything about a delegate." + ~query: RPC_query.empty + ~output: info_encoding + path + + let balance = + RPC_service.get_service + ~description: + "Returns the full balance of a given delegate, \ + including the frozen balances." + ~query: RPC_query.empty + ~output: Tez.encoding + RPC_path.(path / "balance") + + let frozen_balance = + RPC_service.get_service + ~description: + "Returns the total frozen balances of a given delegate, \ + this includes the frozen deposits, rewards and fees." + ~query: RPC_query.empty + ~output: Tez.encoding + RPC_path.(path / "change") + + let frozen_balances = + RPC_service.get_service + ~description: + "Returns the amount of frozen tokens associated to a given delegate." + ~query: RPC_query.empty + ~output: Delegate.frozen_balances_encoding + RPC_path.(path / "frozen_balances") + + let delegated_balance = + RPC_service.get_service + ~description: + "Returns the total amount of token delegated to a given delegate. \ + This includes the balance of all the contracts that delegates \ + to it, but also the balance of the delegate itself and its frozen \ + fees and deposits. The rewards do not count in the delegated balance \ + until they are unfrozen." + ~query: RPC_query.empty + ~output: Tez.encoding + RPC_path.(path / "delegated_balance") + + let delegated_contracts = + RPC_service.get_service + ~description: + "Returns the list of contract that delegates to a given delegate." + ~query: RPC_query.empty + ~output: (list Contract_hash.encoding) + RPC_path.(path / "delegated_contracts") + + let deactivated = + RPC_service.get_service + ~description: + "Returns whether the delegate is currently tagged as deactivated or not." + ~query: RPC_query.empty + ~output: bool + RPC_path.(path / "deactivated") + + let grace_period = + RPC_service.get_service + ~description: + "Returns the cycle by the end of which the delegate might be \ + deactivated, whether should she failed to execute any delegate \ + action until then. \ + A deactivated delegate might be reactivated \ + (without loosing any rolls) by simply re-register as a delegate. \ + For deactivated delegate this value contains the cycle by which \ + they were deactivated." + ~query: RPC_query.empty + ~output: Cycle.encoding + RPC_path.(path / "grace_period") + +end + +let () = + let open Services_registration in + register0 S.list_delegate begin fun ctxt q () -> + Delegate.list ctxt >>= fun delegates -> + if q.active && q.inactive then + return delegates + else if q.active then + Lwt_list.filter_p + (fun pkh -> Delegate.deactivated ctxt pkh >|= not) + delegates >>= return + else if q.inactive then + Lwt_list.filter_p + (fun pkh -> Delegate.deactivated ctxt pkh) + delegates >>= return + else + return [] + end ; + 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.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 + } + end ; + register1 S.balance begin fun ctxt pkh () () -> + Delegate.full_balance ctxt pkh + end ; + 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 + 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 ; + register1 S.grace_period begin fun ctxt pkh () () -> + Delegate.grace_period ctxt pkh + end + +let list ctxt block ?(active = true) ?(inactive = false) () = + RPC_context.make_call0 S.list_delegate ctxt block { active ; inactive } () + +let info ctxt block pkh = + RPC_context.make_call1 S.info ctxt block pkh () () + +let balance ctxt block pkh = + RPC_context.make_call1 S.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 delegated_balance ctxt block pkh = + RPC_context.make_call1 S.delegated_balance ctxt block pkh () () + +let delegated_contracts ctxt block pkh = + RPC_context.make_call1 S.delegated_contracts ctxt block pkh () () + +let deactivated ctxt block pkh = + RPC_context.make_call1 S.deactivated ctxt block pkh () () + +let grace_period ctxt block pkh = + RPC_context.make_call1 S.grace_period ctxt block pkh () () + + module Baker = struct module S = struct @@ -322,37 +537,6 @@ 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" /: Signature.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 174e90e90..ae1966a0f 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_services.mli +++ b/src/proto_alpha/lib_protocol/src/delegate_services.mli @@ -9,10 +9,64 @@ open Alpha_context +val list: + 'a #RPC_context.simple -> 'a -> + ?active:bool -> + ?inactive:bool -> + unit -> Signature.Public_key_hash.t list shell_tzresult Lwt.t + +type info = { + balance: Tez.t ; + frozen_balance: Tez.t ; + frozen_balances: Delegate.frozen_balance Cycle.Map.t ; + delegated_balance: Tez.t ; + delegated_contracts: Contract_hash.t list ; + deactivated: bool ; + grace_period: Cycle.t ; +} + +val info_encoding: info Data_encoding.t + +val info: + 'a #RPC_context.simple -> 'a -> + Signature.Public_key_hash.t -> + info shell_tzresult Lwt.t + +val balance: + 'a #RPC_context.simple -> 'a -> + Signature.Public_key_hash.t -> + Tez.t shell_tzresult Lwt.t + +val frozen_balance: + 'a #RPC_context.simple -> 'a -> + Signature.Public_key_hash.t -> + Tez.t shell_tzresult Lwt.t + val frozen_balances: 'a #RPC_context.simple -> 'a -> Signature.Public_key_hash.t -> - Delegate.frozen_balances shell_tzresult Lwt.t + Delegate.frozen_balance Cycle.Map.t shell_tzresult Lwt.t + +val delegated_balance: + 'a #RPC_context.simple -> 'a -> + Signature.Public_key_hash.t -> + Tez.t shell_tzresult Lwt.t + +val delegated_contracts: + 'a #RPC_context.simple -> 'a -> + Signature.Public_key_hash.t -> + Contract_hash.t list shell_tzresult Lwt.t + +val deactivated: + 'a #RPC_context.simple -> 'a -> + Signature.Public_key_hash.t -> + bool shell_tzresult Lwt.t + +val grace_period: + 'a #RPC_context.simple -> 'a -> + Signature.Public_key_hash.t -> + Cycle.t shell_tzresult Lwt.t + module Baker : sig diff --git a/src/proto_alpha/lib_protocol/src/delegate_storage.ml b/src/proto_alpha/lib_protocol/src/delegate_storage.ml index 8c93847e2..974b0b5eb 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_storage.ml +++ b/src/proto_alpha/lib_protocol/src/delegate_storage.ml @@ -193,6 +193,9 @@ let remove ctxt contract = let fold = Storage.Delegates.fold let list = Storage.Delegates.elements +let get_delegated_contracts ctxt delegate = + let contract = Contract_repr.implicit_contract delegate in + Storage.Contract.Delegated.elements (ctxt, contract) let get_frozen_deposit ctxt contract cycle = Storage.Contract.Frozen_deposits.get_option (ctxt, contract) cycle >>=? function @@ -349,41 +352,107 @@ let has_frozen_balance ctxt delegate cycle = get_frozen_rewards ctxt contract cycle >>=? fun rewards -> return Tez_repr.(rewards <> zero) -type frozen_balances = { +type frozen_balance = { deposit : Tez_repr.t ; fees : Tez_repr.t ; rewards : Tez_repr.t ; } +let frozen_balance_encoding = + let open Data_encoding in + conv + (fun { deposit ; fees ; rewards } -> (deposit, fees, rewards)) + (fun (deposit, fees, rewards) -> { deposit ; fees ; rewards }) + (obj3 + (req "deposit" Tez_repr.encoding) + (req "fees" Tez_repr.encoding) + (req "rewards" Tez_repr.encoding)) + +let frozen_balances_encoding = + let open Data_encoding in + conv + (Cycle_repr.Map.bindings) + (List.fold_left + (fun m (c, b) -> Cycle_repr.Map.add c b m) + Cycle_repr.Map.empty) + (list (merge_objs + (obj1 (req "cycle" Cycle_repr.encoding)) + frozen_balance_encoding)) + +let empty_frozen_balance = + { deposit = Tez_repr.zero ; + fees = Tez_repr.zero ; + rewards = Tez_repr.zero } + let frozen_balances ctxt delegate = + let contract = Contract_repr.implicit_contract delegate in + let map = Cycle_repr.Map.empty in + Storage.Contract.Frozen_deposits.fold + (ctxt, contract) ~init:map + ~f:(fun cycle amount map -> + Lwt.return + (Cycle_repr.Map.add cycle + { empty_frozen_balance with deposit = amount } map)) >>= fun map -> + Storage.Contract.Frozen_fees.fold + (ctxt, contract) ~init:map + ~f:(fun cycle amount map -> + let balance = + match Cycle_repr.Map.find_opt cycle map with + | None -> empty_frozen_balance + | Some balance -> balance in + Lwt.return + (Cycle_repr.Map.add cycle + { balance with fees = amount } map)) >>= fun map -> + Storage.Contract.Frozen_rewards.fold + (ctxt, contract) ~init:map + ~f:(fun cycle amount map -> + let balance = + match Cycle_repr.Map.find_opt cycle map with + | None -> empty_frozen_balance + | Some balance -> balance in + Lwt.return + (Cycle_repr.Map.add cycle + { balance with rewards = amount } map)) >>= fun map -> + Lwt.return map + +let frozen_balance 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 deposit -> + Lwt.return (Tez_repr.(acc +? amount))) >>= fun balance -> 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 fees -> + Lwt.return (Tez_repr.(acc +? amount))) >>= fun balance -> 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 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 + Lwt.return (Tez_repr.(acc +? amount))) >>= fun balance -> + Lwt.return balance let full_balance ctxt delegate = let contract = Contract_repr.implicit_contract delegate in frozen_balance ctxt delegate >>=? fun frozen_balance -> Storage.Contract.Balance.get ctxt contract >>=? fun balance -> Lwt.return Tez_repr.(frozen_balance +? balance) + +let deactivated ctxt delegate = + let contract = Contract_repr.implicit_contract delegate in + Storage.Contract.Inactive_delegate.mem ctxt contract + +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 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) diff --git a/src/proto_alpha/lib_protocol/src/delegate_storage.mli b/src/proto_alpha/lib_protocol/src/delegate_storage.mli index 0577255ee..a8dfb3002 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_storage.mli +++ b/src/proto_alpha/lib_protocol/src/delegate_storage.mli @@ -94,21 +94,27 @@ val has_frozen_balance: Raw_context.t -> Signature.Public_key_hash.t -> Cycle_repr.t -> bool tzresult Lwt.t -(** Returns the amount of frozen tokens associated to a given key. *) +(** Returns the amount of frozen deposit, fees and rewards associated + to a given delegate. *) val frozen_balance: Raw_context.t -> Signature.Public_key_hash.t -> Tez_repr.t tzresult Lwt.t -type frozen_balances = { +type frozen_balance = { 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_balance_encoding: frozen_balance Data_encoding.t +val frozen_balances_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: Raw_context.t -> Signature.Public_key_hash.t -> - frozen_balances tzresult Lwt.t + frozen_balance Cycle_repr.Map.t 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 @@ -116,3 +122,20 @@ val frozen_balances: val full_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: + Raw_context.t -> Signature.Public_key_hash.t -> + Contract_hash.t list Lwt.t + +val delegated_balance: + Raw_context.t -> Signature.Public_key_hash.t -> + Tez_repr.t tzresult Lwt.t + +val deactivated: + Raw_context.t -> Signature.Public_key_hash.t -> + bool Lwt.t + +val grace_period: + Raw_context.t -> Signature.Public_key_hash.t -> + Cycle_repr.t tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/src/roll_repr.mli b/src/proto_alpha/lib_protocol/src/roll_repr.mli index efbcde559..69035782a 100644 --- a/src/proto_alpha/lib_protocol/src/roll_repr.mli +++ b/src/proto_alpha/lib_protocol/src/roll_repr.mli @@ -7,7 +7,7 @@ (* *) (**************************************************************************) -type t +type t = private int32 type roll = t val encoding: roll Data_encoding.t diff --git a/src/proto_alpha/lib_protocol/src/roll_storage.ml b/src/proto_alpha/lib_protocol/src/roll_storage.ml index e0763d25f..15de1f2d7 100644 --- a/src/proto_alpha/lib_protocol/src/roll_storage.ml +++ b/src/proto_alpha/lib_protocol/src/roll_storage.ml @@ -131,6 +131,23 @@ let baking_rights_owner c level ~priority = let endorsement_rights_owner c level ~slot = Random.owner c "endorsement" level slot +let traverse_rolls ctxt head = + let rec loop acc roll = + Storage.Roll.Successor.get_option ctxt roll >>=? function + | None -> return (List.rev acc) + | Some next -> loop (next :: acc) next in + loop [head] head + +let get_rolls ctxt delegate = + Storage.Roll.Delegate_roll_list.get_option ctxt delegate >>=? function + | None -> return [] + | Some head_roll -> traverse_rolls ctxt head_roll + +let get_change c delegate = + Storage.Roll.Delegate_change.get_option c delegate >>=? function + | None -> return Tez_repr.zero + | Some change -> return change + module Delegate = struct let fresh_roll c = diff --git a/src/proto_alpha/lib_protocol/src/roll_storage.mli b/src/proto_alpha/lib_protocol/src/roll_storage.mli index 6de338a1c..680b39b8e 100644 --- a/src/proto_alpha/lib_protocol/src/roll_storage.mli +++ b/src/proto_alpha/lib_protocol/src/roll_storage.mli @@ -70,6 +70,11 @@ val delegate_pubkey: Raw_context.t -> Signature.Public_key_hash.t -> Signature.Public_key.t tzresult Lwt.t +val get_rolls: + Raw_context.t -> Signature.Public_key_hash.t -> Roll_repr.t list tzresult Lwt.t +val get_change: + Raw_context.t -> Signature.Public_key_hash.t -> Tez_repr.t tzresult Lwt.t + (**/**) val get_contract_delegate: diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index 5ae342546..80cf51f74 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -97,16 +97,23 @@ module Contract = struct Alpha_services.Contract.balance rpc_ctxt ctxt contract | _ -> match Alpha_context.Contract.is_implicit contract with - | None -> invalid_arg "get_balance: no frozen accounts for an originated contract." + | None -> + invalid_arg + "get_balance: no frozen accounts for an originated contract." | Some pkh -> - Alpha_services.Delegate.frozen_balances rpc_ctxt ctxt pkh >>|? - fun Delegate.({deposit ; fees ; rewards }) -> - begin match kind with - | Deposit -> deposit - | Fees -> fees - | Rewards -> rewards - | _ -> invalid_arg "get_balance: pass Main, Deposit, Fees or Rewards." - end + Alpha_services.Delegate.frozen_balances + rpc_ctxt ctxt pkh >>=? fun map -> + Lwt.return @@ + Cycle.Map.fold + (fun _cycle { Delegate.deposit ; fees ; rewards } acc -> + acc >>?fun acc -> + match kind with + | Deposit -> Test_tez.Tez.(acc +? deposit) + | Fees -> Test_tez.Tez.(acc +? fees) + | Rewards -> Test_tez.Tez.(acc +? rewards) + | _ -> assert false) + map + (Ok Tez.zero) end let counter ctxt contract =