Source file client_daemon.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
let rec retry (cctxt : #Protocol_client_context.full) ?max_delay ~delay ~factor
~tries f x =
f x >>= function
| Ok _ as r -> Lwt.return r
| Error
(RPC_client_errors.Request_failed {error = Connection_failed _; _} :: _)
as err
when tries > 0 -> (
cctxt#message "Connection refused, retrying in %.2f seconds..." delay
>>= fun () ->
Lwt.pick
[
(Lwt_unix.sleep delay >|= fun () -> `Continue);
(Lwt_exit.clean_up_starts >|= fun _ -> `Killed);
]
>>= function
| `Killed -> Lwt.return err
| `Continue ->
let next_delay = delay *. factor in
let delay =
Option.fold
~none:next_delay
~some:(fun max_delay -> Float.min next_delay max_delay)
max_delay
in
retry cctxt ?max_delay ~delay ~factor ~tries:(tries - 1) f x)
| Error _ as err -> Lwt.return err
let rec retry_on_disconnection (cctxt : #Protocol_client_context.full) f =
f () >>= function
| Ok () -> return_unit
| Error (Baking_errors.Node_connection_lost :: _) ->
cctxt#warning
"Lost connection with the node. Retrying to establish connection..."
>>= fun () ->
Client_confirmations.wait_for_bootstrapped
~retry:(retry cctxt ~max_delay:10. ~delay:1. ~factor:1.5 ~tries:max_int)
cctxt
>>=? fun () -> retry_on_disconnection cctxt f
| Error err ->
cctxt#error "Unexpected error: %a. Exiting..." pp_print_trace err
let await_protocol_start (cctxt : #Protocol_client_context.full) ~chain =
cctxt#message "Waiting for protocol %s to start..." Protocol.name
>>= fun () -> Node_rpc.await_protocol_activation cctxt ~chain ()
module Baker = struct
let run (cctxt : Protocol_client_context.full) ?minimal_fees
?minimal_nanotez_per_gas_unit ?minimal_nanotez_per_byte ?liquidity_baking
? ?dal_node_endpoint ?force_apply ?context_path
?state_recorder ~chain ~keep_alive delegates =
let process () =
Config_services.user_activated_upgrades cctxt
>>=? fun user_activated_upgrades ->
let config =
Baking_configuration.make
?minimal_fees
?minimal_nanotez_per_gas_unit
?minimal_nanotez_per_byte
?liquidity_baking
?extra_operations
?dal_node_endpoint
?force_apply
?context_path
~user_activated_upgrades
?state_recorder
()
in
cctxt#message
"Baker v%a (%s) for %a started."
Tezos_version.Version.pp
Tezos_version_value.Current_git_info.version
Tezos_version_value.Current_git_info.abbreviated_commit_hash
Protocol_hash.pp_short
Protocol.hash
>>= fun () ->
let canceler = Lwt_canceler.create () in
let _ =
Lwt_exit.register_clean_up_callback ~loc:__LOC__ (fun _ ->
cctxt#message "Shutting down the baker..." >>= fun () ->
Lwt_canceler.cancel canceler >>= fun _ -> Lwt.return_unit)
in
Baking_scheduling.run cctxt ~canceler ~chain config delegates
in
Client_confirmations.wait_for_bootstrapped
~retry:(retry cctxt ~delay:1. ~factor:1.5 ~tries:5)
cctxt
>>=? fun () ->
await_protocol_start cctxt ~chain >>=? fun () ->
if keep_alive then retry_on_disconnection cctxt process else process ()
end
module Accuser = struct
let run (cctxt : #Protocol_client_context.full) ~chain ~preserved_levels
~keep_alive =
let process () =
cctxt#message
"Accuser v%a (%s) for %a started."
Tezos_version.Version.pp
Tezos_version_value.Current_git_info.version
Tezos_version_value.Current_git_info.abbreviated_commit_hash
Protocol_hash.pp_short
Protocol.hash
>>= fun () ->
Client_baking_blocks.monitor_applied_blocks
~next_protocols:(Some [Protocol.hash])
cctxt
~chains:[chain]
()
>>=? fun (valid_blocks_stream, _) ->
let canceler = Lwt_canceler.create () in
let _ =
Lwt_exit.register_clean_up_callback ~loc:__LOC__ (fun _ ->
cctxt#message "Shutting down the accuser..." >>= fun () ->
Lwt_canceler.cancel canceler >>= fun _ -> Lwt.return_unit)
in
Client_baking_denunciation.create
cctxt
~canceler
~preserved_levels
valid_blocks_stream
in
Client_confirmations.wait_for_bootstrapped
~retry:(retry cctxt ~delay:1. ~factor:1.5 ~tries:5)
cctxt
>>=? fun () ->
await_protocol_start cctxt ~chain >>=? fun () ->
if keep_alive then retry_on_disconnection cctxt process else process ()
end
module VDF = struct
let run (cctxt : Protocol_client_context.full) ~chain ~keep_alive =
let open Lwt_result_syntax in
let process () =
let*! () =
cctxt#message
"VDF daemon v%a (%s) for %a started."
Tezos_version.Version.pp
Tezos_version_value.Current_git_info.version
Tezos_version_value.Current_git_info.abbreviated_commit_hash
Protocol_hash.pp_short
Protocol.hash
in
let* chain_id = Shell_services.Chain.chain_id cctxt ~chain () in
let* constants =
Protocol.Alpha_services.Constants.all cctxt (`Hash chain_id, `Head 0)
in
let canceler = Lwt_canceler.create () in
let _ =
Lwt_exit.register_clean_up_callback ~loc:__LOC__ (fun _ ->
let*! () = cctxt#message "Shutting down the VDF daemon..." in
let*! _ = Lwt_canceler.cancel canceler in
Lwt.return_unit)
in
Baking_vdf.start_vdf_worker cctxt ~canceler constants chain
in
let* () =
Client_confirmations.wait_for_bootstrapped
~retry:(retry cctxt ~delay:1. ~factor:1.5 ~tries:5)
cctxt
in
let* () = await_protocol_start cctxt ~chain in
if keep_alive then retry_on_disconnection cctxt process else process ()
end