package tezos-protocol-018-Proxford

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file stake_context.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2023 Nomadic Labs, <contact@nomadic-labs.com>               *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Stake_repr

let staking_weight ctxt {frozen; delegated} =
  let frozen = Tez_repr.to_mutez frozen in
  let delegated = Tez_repr.to_mutez delegated in
  let edge_of_staking_over_delegation =
    Constants_storage.adaptive_issuance_edge_of_staking_over_delegation ctxt
  in
  if Constants_storage.adaptive_issuance_enable ctxt then
    Int64.(add frozen (div delegated (of_int edge_of_staking_over_delegation)))
  else Int64.add frozen delegated

let compare ctxt s1 s2 =
  Int64.compare (staking_weight ctxt s1) (staking_weight ctxt s2)

let voting_weight ctxt {Stake_repr.Full.own_frozen; staked_frozen; delegated} =
  let open Result_syntax in
  let+ frozen = Tez_repr.(own_frozen +? staked_frozen) in
  staking_weight ctxt (Stake_repr.make ~frozen ~delegated)

let apply_limits ctxt staking_parameters
    {Stake_repr.Full.own_frozen; staked_frozen; delegated} =
  let open Result_syntax in
  let limit_of_delegation_over_baking =
    Int64.of_int (Constants_storage.limit_of_delegation_over_baking ctxt)
  in
  let global_limit_of_staking_over_baking_millionth =
    Int64.(
      mul
        1_000_000L
        (of_int
           (Constants_storage
            .adaptive_issuance_global_limit_of_staking_over_baking
              ctxt)))
  in
  let {Staking_parameters_repr.limit_of_staking_over_baking_millionth; _} =
    staking_parameters
  in
  let limit_of_staking_over_baking_millionth =
    let delegate_limit_of_staking_over_baking_millionth =
      Int64.of_int32 limit_of_staking_over_baking_millionth
    in
    Compare.Int64.min
      global_limit_of_staking_over_baking_millionth
      delegate_limit_of_staking_over_baking_millionth
  in
  let allowed_staked_frozen =
    match
      Tez_repr.mul_ratio
        own_frozen
        ~num:limit_of_staking_over_baking_millionth
        ~den:1_000_000L
    with
    | Ok max_allowed_staked_frozen ->
        Tez_repr.min staked_frozen max_allowed_staked_frozen
    | Error _max_allowed_staked_frozen_overflows -> staked_frozen
  in
  (* Overstaked tez count as delegated. *)
  let* overstaked = Tez_repr.(staked_frozen -? allowed_staked_frozen) in
  let* delegated = Tez_repr.(delegated +? overstaked) in
  (* Overdelegated tez don't count. *)
  let delegated =
    match Tez_repr.(own_frozen *? limit_of_delegation_over_baking) with
    | Ok max_allowed_delegated -> Tez_repr.min max_allowed_delegated delegated
    | Error _max_allowed_delegated_overflows -> delegated
  in
  let+ frozen = Tez_repr.(own_frozen +? allowed_staked_frozen) in
  Stake_repr.make ~frozen ~delegated

let baking_weight ctxt staking_parameters f =
  let open Result_syntax in
  let+ s = apply_limits ctxt staking_parameters f in
  staking_weight ctxt s
OCaml

Innovation. Community. Security.