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
type t = Nairobi | Oxford | Alpha
type constants =
| Constants_sandbox
| Constants_mainnet
| Constants_mainnet_with_chain_id
| Constants_test
let constants_to_string = function
| Constants_sandbox -> "sandbox"
| Constants_mainnet -> "mainnet"
| Constants_mainnet_with_chain_id -> "mainnet-with-chain-id"
| Constants_test -> "test"
let name = function
| Alpha -> "Alpha"
| Nairobi -> "Nairobi"
| Oxford -> "Oxford"
let number = function Nairobi -> 017 | Oxford -> 018 | Alpha -> 019
let directory = function
| Alpha -> "proto_alpha"
| Nairobi -> "proto_017_PtNairob"
| Oxford -> "proto_018_Proxford"
let tag protocol = String.lowercase_ascii (name protocol)
let hash = function
| Alpha -> "ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK"
| Nairobi -> "PtNairobiyssHuh87hEhfVBGCVrK3WnS8Z2FT4ymB5tAa4r1nQf"
| Oxford -> "ProxfordSW2S7fvchT1Zgj2avb5UES194neRyYVXoaDGvF9egt8"
let genesis_hash = "ProtoGenesisGenesisGenesisGenesisGenesisGenesk612im"
let demo_noops_hash = "ProtoDemoNoopsDemoNoopsDemoNoopsDemoNoopsDemo6XBoYp"
let demo_counter_hash = "ProtoDemoCounterDemoCounterDemoCounterDemoCou4LSpdT"
let protocol_zero_hash = "PrihK96nBAFSxVL1GLJTVhu9YnzkMFiBeuJRPA8NwuZVZCE1L6i"
let default_constants = Constants_sandbox
let parameter_file ?(constants = default_constants) protocol =
let name = constants_to_string constants in
sf "src/%s/parameters/%s-parameters.json" (directory protocol) name
let daemon_name = function Alpha -> "alpha" | p -> String.sub (hash p) 0 8
let accuser proto = "./octez-accuser-" ^ daemon_name proto
let baker proto = "./octez-baker-" ^ daemon_name proto
let sc_rollup_client proto = "./octez-smart-rollup-client-" ^ daemon_name proto
let encoding_prefix = function
| Alpha -> "alpha"
| p -> sf "%03d-%s" (number p) (String.sub (hash p) 0 8)
type parameter_overrides =
(string list * [`None | `Int of int | `String_of_int of int | JSON.u]) list
let default_bootstrap_accounts =
Array.to_list Account.Bootstrap.keys |> List.map @@ fun key -> (key, None)
type bootstrap_contract = {
delegate : string option;
amount : Tez.t;
script : Ezjsonm.value;
hash : string option;
}
type bootstrap_smart_rollup = {
address : string;
pvm_kind : string;
boot_sector : string;
parameters_ty : Ezjsonm.value;
}
let write_parameter_file :
?bootstrap_accounts:(Account.key * int option) list ->
?additional_bootstrap_accounts:(Account.key * int option * bool) list ->
?bootstrap_smart_rollups:bootstrap_smart_rollup list ->
?bootstrap_contracts:bootstrap_contract list ->
base:(string, t * constants option) Either.t ->
parameter_overrides ->
string Lwt.t =
fun ?(bootstrap_accounts = default_bootstrap_accounts)
?(additional_bootstrap_accounts = [])
?(bootstrap_smart_rollups = [])
?(bootstrap_contracts = [])
~base
parameter_overrides ->
let overriden_parameters = Temp.file "parameters.json" in
let original_parameters =
let file =
Either.fold
~left:Fun.id
~right:(fun (x, constants) -> parameter_file ?constants x)
base
in
JSON.parse_file file |> JSON.unannotate
in
let parameter_overrides =
if List.mem_assoc ["bootstrap_accounts"] parameter_overrides then
parameter_overrides
else
let bootstrap_accounts =
List.map
(fun ((account : Account.key), default_balance) ->
`A
[
`String account.public_key;
`String
(string_of_int
(Option.value ~default:4000000000000 default_balance));
])
bootstrap_accounts
in
(["bootstrap_accounts"], `A bootstrap_accounts) :: parameter_overrides
in
let parameter_overrides =
if List.mem_assoc ["bootstrap_smart_rollups"] parameter_overrides then
parameter_overrides
else
let bootstrap_smart_rollups =
List.map
(fun {address; pvm_kind; boot_sector; parameters_ty} ->
`O
[
("address", `String address);
("pvm_kind", `String pvm_kind);
("kernel", `String boot_sector);
("parameters_ty", parameters_ty);
])
bootstrap_smart_rollups
in
match bootstrap_smart_rollups with
| [] ->
parameter_overrides
| bootstrap_smart_rollups ->
(["bootstrap_smart_rollups"], `A bootstrap_smart_rollups)
:: parameter_overrides
in
let parameter_overrides =
if List.mem_assoc ["bootstrap_contracts"] parameter_overrides then
parameter_overrides
else
let bootstrap_contracts =
List.map
(fun {delegate; amount; script; hash} ->
let delegate =
match delegate with
| Some delegate -> [("delegate", `String delegate)]
| None -> []
in
let hash =
match hash with
| Some hash -> [("hash", `String hash)]
| None -> []
in
`O
([("amount", `String (Tez.to_string amount)); ("script", script)]
@ delegate @ hash))
bootstrap_contracts
in
match bootstrap_contracts with
| [] -> parameter_overrides
| bootstrap_contracts ->
(["bootstrap_contracts"], `A bootstrap_contracts)
:: parameter_overrides
in
let parameters =
List.fold_left
(fun acc (path, value) ->
let value =
match value with
| `None -> None
| `Int i -> Some (`Float (float i))
| `String_of_int i -> Some (`String (string_of_int i))
| #JSON.u as value -> Some value
in
Ezjsonm.update acc path value)
original_parameters
parameter_overrides
in
let parameters =
let path = ["bootstrap_accounts"] in
let existing_accounts =
Ezjsonm.get_list Fun.id (Ezjsonm.find parameters path)
in
let additional_bootstrap_accounts =
List.map
(fun ((account : Account.key), default_balance, is_revealed) ->
`A
[
`String
(if is_revealed then account.public_key
else account.public_key_hash);
`String
(string_of_int
(Option.value ~default:4000000000000 default_balance));
])
additional_bootstrap_accounts
in
Ezjsonm.update
parameters
path
(Some (`A (existing_accounts @ additional_bootstrap_accounts)))
in
JSON.encode_to_file_u overriden_parameters parameters ;
Lwt.return overriden_parameters
let next_protocol = function
| Nairobi -> Some Alpha
| Oxford -> None
| Alpha -> None
let previous_protocol = function
| Alpha -> Some Nairobi
| Oxford -> Some Nairobi
| Nairobi -> None
let all = [Nairobi; Oxford; Alpha]
type supported_protocols =
| Any_protocol
| From_protocol of int
| Until_protocol of int
| Between_protocols of int * int
let is_supported supported_protocols protocol =
match supported_protocols with
| Any_protocol -> true
| From_protocol n -> number protocol >= n
| Until_protocol n -> number protocol <= n
| Between_protocols (a, b) ->
let n = number protocol in
a <= n && n <= b
let show_supported_protocols = function
| Any_protocol -> "Any_protocol"
| From_protocol n -> sf "From_protocol %d" n
| Until_protocol n -> sf "Until_protocol %d" n
| Between_protocols (a, b) -> sf "Between_protocol (%d, %d)" a b
let iter_on_supported_protocols ~title ~protocols ?(supports = Any_protocol) f =
match List.filter (is_supported supports) protocols with
| [] ->
failwith
(sf
"test %s was registered with ~protocols:[%s] %s, which results in \
an empty list of protocols"
title
(String.concat ", " (List.map name protocols))
(show_supported_protocols supports))
| supported_protocols -> List.iter f supported_protocols
let add_to_test_parameters protocol title tags =
(name protocol ^ ": " ^ title, tag protocol :: tags)
let register_test ~__FILE__ ~title ~tags ?supports body protocols =
iter_on_supported_protocols ~title ~protocols ?supports @@ fun protocol ->
let title, tags = add_to_test_parameters protocol title tags in
Test.register ~__FILE__ ~title ~tags (fun () -> body protocol)
let register_long_test ~__FILE__ ~title ~tags ?supports ?team ~executors
~timeout body protocols =
iter_on_supported_protocols ~title ~protocols ?supports @@ fun protocol ->
let title, tags = add_to_test_parameters protocol title tags in
Long_test.register ~__FILE__ ~title ~tags ?team ~executors ~timeout (fun () ->
body protocol)
let register_regression_test ~__FILE__ ~title ~tags ?supports body protocols =
iter_on_supported_protocols ~title ~protocols ?supports @@ fun protocol ->
let title, tags = add_to_test_parameters protocol title tags in
Regression.register ~__FILE__ ~title ~tags (fun () -> body protocol)