package octez-smart-rollup-node-lib

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

Source file metadata.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
(*****************************************************************************)
(*                                                                           *)
(* SPDX-License-Identifier: MIT                                              *)
(* Copyright (c) 2023 Functori <contact@functori.com>                        *)
(*                                                                           *)
(*****************************************************************************)

type genesis_info = {level : int32; commitment_hash : Commitment.Hash.t}

let genesis_info_encoding =
  let open Data_encoding in
  conv
    (fun {level; commitment_hash} -> (level, commitment_hash))
    (fun (level, commitment_hash) -> {level; commitment_hash})
    (obj2 (req "level" int32) (req "commitment_hash" Commitment.Hash.encoding))

let path ~dir = Filename.concat dir "metadata"

let read_metadata_file encoding ~dir =
  let open Lwt_result_syntax in
  protect @@ fun () ->
  let filename = path ~dir in
  let*! exists = Lwt_unix.file_exists filename in
  if not exists then return_none
  else
    let* json = Lwt_utils_unix.Json.read_file filename in
    return_some (Data_encoding.Json.destruct encoding json)

let write_metadata_file encoding ~dir version =
  let open Lwt_result_syntax in
  protect @@ fun () ->
  let filename = path ~dir in
  let*! () = Lwt_utils_unix.create_dir dir in
  let json = Data_encoding.Json.construct encoding version in
  Lwt_utils_unix.Json.write_file filename json

module V0 = struct
  type t = {rollup_address : Address.t; context_version : Context.Version.t}

  let encoding =
    let open Data_encoding in
    conv
      (fun {rollup_address; context_version} ->
        (rollup_address, context_version))
      (fun (rollup_address, context_version) ->
        {rollup_address; context_version})
      (obj2
         (req "rollup_address" Address.encoding)
         (req "context_version" Context.Version.encoding))

  let read_metadata_file = read_metadata_file encoding

  let write_metadata_file = write_metadata_file encoding
end

module V1 = struct
  type t = {
    rollup_address : Address.t;
    context_version : Context.Version.t;
    kind : Kind.t;
    genesis_info : genesis_info;
  }

  let encoding =
    let open Data_encoding in
    conv
      (fun {rollup_address; context_version; kind; genesis_info} ->
        (rollup_address, context_version, kind, genesis_info))
      (fun (rollup_address, context_version, kind, genesis_info) ->
        {rollup_address; context_version; kind; genesis_info})
      (obj4
         (req "rollup_address" Address.encoding)
         (req "context_version" Context.Version.encoding)
         (req "kind" Kind.encoding)
         (req "genesis_info" genesis_info_encoding))

  let read_metadata_file = read_metadata_file encoding

  let write_metadata_file = write_metadata_file encoding
end

module Versioned = struct
  type t = V0 of V0.t | V1 of V1.t

  let encoding =
    let open Data_encoding in
    union
      [
        case
          ~title:"metadata.v1"
          Json_only
          V1.encoding
          (function V1 m -> Some m | _ -> None)
          (fun m -> V1 m);
        case
          ~title:"metadata.v0"
          Json_only
          V0.encoding
          (function V0 m -> Some m | _ -> None)
          (fun m -> V0 m);
      ]

  let read_metadata_file = read_metadata_file encoding

  let write_metadata_file = write_metadata_file encoding
end

include V1
OCaml

Innovation. Community. Security.