Source file daemon_event.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
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2023 TriliTech <contact@trili.tech>                         *)
(* Copyright (c) 2023 Nomadic Labs, <contact@nomadic-labs.com>               *)
(* Copyright (c) 2023 Functori, <contact@functori.com>                       *)
(* Copyright (c) 2023 Marigold <contact@marigold.dev>                        *)
(*                                                                           *)
(* 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

module Simple = struct
  include Internal_event.Simple

  let section = ["smart_rollup_node"; "daemon"]

  let head_processing =
    declare_2
      ~section
      ~name:"smart_rollup_node_daemon_process_head"
      ~msg:"Processing head {hash} at level {level}"
      ~level:Notice
      ("hash", Block_hash.encoding)
      ("level", Data_encoding.int32)

  let new_head_processed =
    declare_3
      ~section
      ~name:"smart_rollup_node_daemon_new_head_processed"
      ~msg:
        "Finished processing layer 1 head {hash} at level {level} in \
         {process_time}"
      ~level:Notice
      ("hash", Block_hash.encoding)
      ("level", Data_encoding.int32)
      ("process_time", Time.System.Span.encoding)
      ~pp3:Ptime.Span.pp

  let new_head_degraded =
    declare_2
      ~section
      ~name:"smart_rollup_node_daemon_new_head_degraded"
      ~msg:"[DEGRADED MODE] Seen layer 1 head {hash} at level {level}"
      ~level:Error
      ("hash", Block_hash.encoding)
      ("level", Data_encoding.int32)

  let processing_heads_iteration =
    declare_3
      ~section
      ~name:"smart_rollup_node_daemon_processing_heads"
      ~msg:
        "A new iteration of process_heads has been triggered: processing \
         {number} heads from level {from} to level {to}"
      ~level:Debug
      ("number", Data_encoding.int31)
      ("from", Data_encoding.int32)
      ("to", Data_encoding.int32)

  let new_heads_processed =
    declare_3
      ~section
      ~name:"smart_rollup_node_daemon_new_heads_processed"
      ~msg:
        "Finished processing {number} layer 1 heads for levels {from} to {to}"
      ~level:Info
      ("number", Data_encoding.int31)
      ("from", Data_encoding.int32)
      ("to", Data_encoding.int32)

  let included_successful_operation =
    declare_1
      ~section
      ~name:"smart_rollup_node_daemon_included_successful_operation"
      ~msg:"Operation {operation} was included as successful"
      ~level:Debug
      ("operation", L1_operation.encoding)
      ~pp1:L1_operation.pp

  let included_failed_operation =
    declare_3
      ~section
      ~name:"smart_rollup_node_daemon_included_failed_operation"
      ~msg:
        "[Warning]: Operation {operation} was included as {status} with error \
         {error}"
      ~level:Warning
      ("operation", L1_operation.encoding)
      ( "status",
        Data_encoding.(
          string_enum
            [
              ("failed", `Failed);
              ("backtracked", `Backtracked);
              ("skipped", `Skipped);
            ]) )
      ("error", Data_encoding.option Error_monad.trace_encoding)
      ~pp1:L1_operation.pp
      ~pp3:
        (fun ppf -> function
          | None -> Format.pp_print_string ppf "none"
          | Some e -> Error_monad.pp_print_trace ppf e)

  let migration =
    declare_5
      ~name:"smart_rollup_node_daemon_protocol_migration"
      ~msg:
        "{catching_up} from {old_protocol} ({old_protocol_level}) to \
         {new_protocol} ({new_protocol_level}) "
      ~level:Notice
      ("catching_up", Data_encoding.bool)
      ("old_protocol", Protocol_hash.encoding)
      ("old_protocol_level", Data_encoding.int31)
      ("new_protocol", Protocol_hash.encoding)
      ("new_protocol_level", Data_encoding.int31)
      ~pp1:(fun ppf catching_up ->
        Format.pp_print_string
          ppf
          (if catching_up then "Catching up on migration" else "Migration"))

  let switched_protocol =
    declare_3
      ~name:"smart_rollup_node_daemon_switched_protocol"
      ~msg:"Switched to {protocol} ({proto_level}) with constants {constants} "
      ~level:Notice
      ("protocol", Protocol_hash.encoding)
      ("proto_level", Data_encoding.int31)
      ("constants", Rollup_constants.encoding)
      ~pp3:(fun fmt c ->
        Data_encoding.Json.pp
          fmt
          (Data_encoding.Json.construct Rollup_constants.encoding c))

  let error =
    declare_1
      ~section
      ~name:"smart_rollup_node_daemon_error"
      ~msg:"[Fatal daemon error]: {error}"
      ~level:Fatal
      ("error", trace_encoding)
      ~pp1:pp_print_trace

  let degraded_mode =
    declare_0
      ~section
      ~name:"smart_rollup_node_daemon_degraded_mode"
      ~msg:
        "[Daemon error]: entering degraded mode - only playing refutation game \
         to defend commitments and publishing pending commitments"
      ~level:Error
      ()

  let refutation_loop_retry =
    declare_1
      ~section
      ~name:"smart_rollup_node_daemon_refutation_loop_retry"
      ~msg:"[Refutation daemon error]: restarting refutation daemon in {delay}."
      ~level:Warning
      ("delay", Time.System.Span.encoding)
      ~pp1:Ptime.Span.pp

  let main_loop_retry =
    declare_1
      ~section
      ~name:"smart_rollup_node_daemon_main_loop_retry"
      ~msg:"Restarting main rollup node loop in {delay}."
      ~level:Warning
      ("delay", Time.System.Span.encoding)
      ~pp1:Ptime.Span.pp

  let exit_bailout_mode =
    declare_0
      ~section
      ~name:"smart_rollup_node_daemon_exit_bailout_mode"
      ~msg:
        "Exit bailout mode - stakes have been recovered, and the node is \
         exiting safely now"
      ~level:Notice
      ()
end

let head_processing hash level = Simple.(emit head_processing (hash, level))

let new_head_processed hash level process_time =
  Simple.(emit new_head_processed (hash, level, process_time))

let new_head_degraded hash level = Simple.(emit new_head_degraded (hash, level))

let new_heads_iteration event = function
  | oldest :: rest ->
      let newest =
        match List.rev rest with [] -> oldest | newest :: _ -> newest
      in
      let number =
        Int32.sub newest.Layer1.level oldest.Layer1.level
        |> Int32.succ |> Int32.to_int
      in
      Simple.emit event (number, oldest.level, newest.level)
  | [] -> Lwt.return_unit

let processing_heads_iteration =
  new_heads_iteration Simple.processing_heads_iteration

let new_heads_processed = new_heads_iteration Simple.new_heads_processed

let included_operation ?errors status operation =
  match status with
  | `Applied -> Simple.(emit included_successful_operation) operation
  | `Failed | `Backtracked | `Skipped ->
      Simple.(emit included_failed_operation) (operation, status, errors)

let migration ~catching_up (old_protocol, old_protocol_level)
    (new_protocol, new_protocol_level) =
  Simple.(emit migration)
    ( catching_up,
      old_protocol,
      old_protocol_level,
      new_protocol,
      new_protocol_level )

let switched_protocol protocol proto_level constants =
  Simple.(emit switched_protocol) (protocol, proto_level, constants)

let error e = Simple.(emit error) e

let degraded_mode () = Simple.(emit degraded_mode) ()

let refutation_loop_retry d =
  Simple.(emit refutation_loop_retry) (Time.System.Span.of_seconds_exn d)

let main_loop_retry d =
  Simple.(emit main_loop_retry) (Time.System.Span.of_seconds_exn d)

let exit_bailout_mode () = Simple.(emit exit_bailout_mode) ()