package tezos-protocol-017-PtNairob

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

Source file alpha_services.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(* Copyright (c) 2019-2020 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 Alpha_context

let custom_root = RPC_path.open_root

module Seed_computation = struct
  module S = struct
    let seed_computation_status_encoding =
      let open Seed in
      Data_encoding.(
        union
          [
            case
              (Tag 0)
              ~title:"Nonce revelation stage"
              (obj1 (req "nonce_revelation_stage" unit))
              (function Nonce_revelation_stage -> Some () | _ -> None)
              (fun () -> Nonce_revelation_stage);
            case
              (Tag 1)
              ~title:"VDF revelation stage"
              (obj2
                 (req "seed_discriminant" Seed.seed_encoding)
                 (req "seed_challenge" Seed.seed_encoding))
              (function
                | Vdf_revelation_stage {seed_discriminant; seed_challenge} ->
                    Some (seed_discriminant, seed_challenge)
                | _ -> None)
              (fun (seed_discriminant, seed_challenge) ->
                Vdf_revelation_stage {seed_discriminant; seed_challenge});
            case
              (Tag 2)
              ~title:"Computation finished"
              (obj1 (req "computation_finished" unit))
              (function Computation_finished -> Some () | _ -> None)
              (fun () -> Computation_finished);
          ])

    let seed_computation =
      RPC_service.get_service
        ~description:"Seed computation status"
        ~query:RPC_query.empty
        ~output:seed_computation_status_encoding
        RPC_path.(custom_root / "context" / "seed_computation")
  end

  let () =
    let open Services_registration in
    register0 ~chunked:false S.seed_computation (fun ctxt () () ->
        Seed.get_seed_computation_status ctxt)

  let get ctxt block =
    RPC_context.make_call0 S.seed_computation ctxt block () ()
end

module Seed = struct
  module S = struct
    open Data_encoding

    let seed =
      RPC_service.post_service
        ~description:"Seed of the cycle to which the block belongs."
        ~query:RPC_query.empty
        ~input:empty
        ~output:Seed.seed_encoding
        RPC_path.(custom_root / "context" / "seed")
  end

  let () =
    let open Services_registration in
    register0 ~chunked:false S.seed (fun ctxt () () ->
        let l = Level.current ctxt in
        Seed.for_cycle ctxt l.cycle)

  let get ctxt block = RPC_context.make_call0 S.seed ctxt block () ()
end

module Nonce = struct
  type info = Revealed of Nonce.t | Missing of Nonce_hash.t | Forgotten

  let info_encoding =
    let open Data_encoding in
    union
      [
        case
          (Tag 0)
          ~title:"Revealed"
          (obj1 (req "nonce" Nonce.encoding))
          (function Revealed nonce -> Some nonce | _ -> None)
          (fun nonce -> Revealed nonce);
        case
          (Tag 1)
          ~title:"Missing"
          (obj1 (req "hash" Nonce_hash.encoding))
          (function Missing nonce -> Some nonce | _ -> None)
          (fun nonce -> Missing nonce);
        case
          (Tag 2)
          ~title:"Forgotten"
          empty
          (function Forgotten -> Some () | _ -> None)
          (fun () -> Forgotten);
      ]

  module S = struct
    let get =
      RPC_service.get_service
        ~description:"Info about the nonce of a previous block."
        ~query:RPC_query.empty
        ~output:info_encoding
        RPC_path.(custom_root / "context" / "nonces" /: Raw_level.rpc_arg)
  end

  let register () =
    let open Services_registration in
    register1 ~chunked:false S.get (fun ctxt raw_level () () ->
        let level = Level.from_raw ctxt raw_level in
        Nonce.get ctxt level >|= function
        | Ok (Revealed nonce) -> ok (Revealed nonce)
        | Ok (Unrevealed {nonce_hash; _}) -> ok (Missing nonce_hash)
        | Error _ -> ok Forgotten)

  let get ctxt block level = RPC_context.make_call1 S.get ctxt block level () ()
end

type error += No_available_snapshots of {min_cycle : int32}

let () =
  Error_monad.register_error_kind
    `Permanent
    ~id:"no_available_snapshots"
    ~title:"No available snapshots"
    ~description:"No available snapshots"
    ~pp:(fun ppf min_cycle ->
      Format.fprintf ppf "No available snapshots until cycle %ld" min_cycle)
    Data_encoding.(obj1 (req "min_cycle" int32))
    (function
      | No_available_snapshots {min_cycle} -> Some min_cycle | _ -> None)
    (fun min_cycle -> No_available_snapshots {min_cycle})

module Snapshot_index = struct
  module S = struct
    let cycle_query : Cycle.t option RPC_query.t =
      let open RPC_query in
      query (fun x -> x)
      |+ opt_field "cycle" Cycle.rpc_arg (fun cycle -> cycle)
      |> seal

    let selected_snapshot =
      RPC_service.get_service
        ~description:
          "Returns the index of the selected snapshot for the current cycle or \
           for the specific `cycle` passed as argument, if any."
        ~query:cycle_query
        ~output:Data_encoding.int31
        RPC_path.(custom_root / "context" / "selected_snapshot")
  end

  let register () =
    let open Services_registration in
    register0 ~chunked:false S.selected_snapshot (fun ctxt cycle () ->
        (* max_snapshot_index can be determined using constants only *)
        let blocks_per_stake_snapshot =
          Alpha_context.Constants.blocks_per_stake_snapshot ctxt
        in
        let blocks_per_cycle = Alpha_context.Constants.blocks_per_cycle ctxt in
        let preserved_cycles =
          Int32.of_int (Alpha_context.Constants.preserved_cycles ctxt)
        in
        let cycle =
          match cycle with
          | None -> Level.(current ctxt).cycle
          | Some cycle -> cycle
        in
        if Compare.Int32.(Cycle.to_int32 cycle <= Int32.succ preserved_cycles)
        then
          (* Early cycles are corner cases, fail if requested *)
          tzfail
            (No_available_snapshots {min_cycle = Int32.add preserved_cycles 2l})
        else
          let max_snapshot_index =
            Int32.div blocks_per_cycle blocks_per_stake_snapshot |> Int32.to_int
          in
          Alpha_context.Stake_distribution.compute_snapshot_index
            ctxt
            cycle
            ~max_snapshot_index)

  let get ctxt block ?cycle () =
    RPC_context.make_call0 S.selected_snapshot ctxt block cycle ()
end

module Contract = Contract_services
module Constants = Constants_services
module Delegate = Delegate_services
module Voting = Voting_services
module Sapling = Sapling_services
module Tx_rollup = Tx_rollup_services

module Liquidity_baking = struct
  module S = struct
    let get_cpmm_address =
      RPC_service.get_service
        ~description:"Liquidity baking CPMM address"
        ~query:RPC_query.empty
        ~output:Alpha_context.Contract.originated_encoding
        RPC_path.(custom_root / "context" / "liquidity_baking" / "cpmm_address")
  end

  let register () =
    let open Services_registration in
    register0 ~chunked:false S.get_cpmm_address (fun ctxt () () ->
        Alpha_context.Liquidity_baking.get_cpmm_address ctxt)

  let get_cpmm_address ctxt block =
    RPC_context.make_call0 S.get_cpmm_address ctxt block () ()
end

module Cache = struct
  module S = struct
    let cached_contracts =
      RPC_service.get_service
        ~description:"Return the list of cached contracts"
        ~query:RPC_query.empty
        ~output:Data_encoding.(list @@ tup2 Contract_hash.encoding int31)
        RPC_path.(custom_root / "context" / "cache" / "contracts" / "all")

    let contract_cache_size =
      RPC_service.get_service
        ~description:"Return the size of the contract cache"
        ~query:RPC_query.empty
        ~output:Data_encoding.int31
        RPC_path.(custom_root / "context" / "cache" / "contracts" / "size")

    let contract_cache_size_limit =
      RPC_service.get_service
        ~description:"Return the size limit of the contract cache"
        ~query:RPC_query.empty
        ~output:Data_encoding.int31
        RPC_path.(
          custom_root / "context" / "cache" / "contracts" / "size_limit")

    let contract_rank =
      RPC_service.post_service
        ~description:
          "Return the number of cached contracts older than the provided \
           contract"
        ~query:RPC_query.empty
        ~input:Alpha_context.Contract.originated_encoding
        ~output:Data_encoding.(option int31)
        RPC_path.(custom_root / "context" / "cache" / "contracts" / "rank")
  end

  let register () =
    let open Services_registration in
    register0 ~chunked:true S.cached_contracts (fun ctxt () () ->
        Script_cache.entries ctxt |> Lwt.return) ;
    register0 ~chunked:false S.contract_cache_size (fun ctxt () () ->
        Script_cache.size ctxt |> return) ;
    register0 ~chunked:false S.contract_cache_size_limit (fun ctxt () () ->
        Script_cache.size_limit ctxt |> return) ;
    register0 ~chunked:false S.contract_rank (fun ctxt () contract ->
        Script_cache.contract_rank ctxt contract |> return)

  let cached_contracts ctxt block =
    RPC_context.make_call0 S.cached_contracts ctxt block () ()

  let contract_cache_size ctxt block =
    RPC_context.make_call0 S.contract_cache_size ctxt block () ()

  let contract_cache_size_limit ctxt block =
    RPC_context.make_call0 S.contract_cache_size_limit ctxt block () ()

  let contract_rank ctxt block contract =
    RPC_context.make_call0 S.contract_rank ctxt block () contract
end

let register () =
  Contract.register () ;
  Constants.register () ;
  Delegate.register () ;
  Nonce.register () ;
  Snapshot_index.register () ;
  Voting.register () ;
  Sapling.register () ;
  Liquidity_baking.register () ;
  Cache.register () ;
  Tx_rollup.register ()
OCaml

Innovation. Community. Security.