Source file io_helpers.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
(** Helpers for loading contexts, saving contexts, writing to contexts, etc.
Also contains the [Key_map] module, heavily used for preparing benchmarks
and computing statistics. *)
let assert_ok ~msg = function
| Ok x -> x
| Error errs ->
Format.eprintf "%s:@.%a@." msg pp_print_trace errs ;
exit 1
let prepare_genesis base_dir =
let open Lwt_result_syntax in
let*! index = Tezos_context.Context.init ~readonly:false base_dir in
let genesis_block =
Block_hash.of_b58check_exn
"BLockGenesisGenesisGenesisGenesisGenesisGeneskvg68z"
in
let* context_hash =
Tezos_context.Context.commit_genesis
index
~chain_id:(Chain_id.of_block_hash genesis_block)
~time:(Time.Protocol.of_seconds 0L)
~protocol:Protocol_hash.zero
in
let*! o = Tezos_context.Context.checkout index context_hash in
match o with
| None -> assert false
| Some context ->
let context =
Tezos_shell_context.Shell_context.wrap_disk_context context
in
return (index, context, context_hash)
let commit context =
let context = Tezos_shell_context.Shell_context.unwrap_disk_context context in
Tezos_context.Context.commit
~time:
(Time.Protocol.of_seconds
(Int64.of_int (int_of_float @@ Unix.gettimeofday ())))
context
let prepare_empty_context base_dir =
let open Lwt_result_syntax in
let* index, context, _context_hash = prepare_genesis base_dir in
let*! context_hash = commit context in
let*! () = Tezos_context.Context.close index in
return context_hash
let load_context_from_disk_lwt base_dir context_hash =
let open Lwt_syntax in
let* index = Tezos_context.Context.init ~readonly:false base_dir in
let* o = Tezos_context.Context.checkout index context_hash in
match o with
| None -> assert false
| Some context ->
Lwt.return
(Tezos_shell_context.Shell_context.wrap_disk_context context, index)
let load_context_from_disk base_dir context_hash =
Lwt_main.run (load_context_from_disk_lwt base_dir context_hash)
let with_context ~base_dir ~context_hash f =
let context, index = load_context_from_disk base_dir context_hash in
Lwt_main.run
(let open Lwt_syntax in
let* res = f context in
let* () = Tezos_context.Context.close index in
Lwt.return res)
let prepare_base_dir base_dir =
Unix.unlink base_dir ;
Unix.mkdir base_dir 0o700
let initialize_key rng_state context path storage_size =
let bytes = Base_samplers.uniform_bytes rng_state ~nbytes:storage_size in
Tezos_protocol_environment.Context.add context path bytes
let commit_and_reload base_dir index context =
let open Lwt_syntax in
let* context_hash = commit context in
let* () = Tezos_context.Context.close index in
load_context_from_disk_lwt base_dir context_hash
(** Maps from string lists to bytes. No balancing. A key cannot be a prefix
or a suffix to another key. *)
module Key_map = struct
module String_map = String.Map
type 'a t = Leaf of 'a | Node of 'a t String_map.t
exception Collision_with_prefix
exception Collision_with_suffix
let empty = Node String_map.empty
let is_empty = function
| Leaf _ -> false
| Node map -> String_map.is_empty map
let rec insert (key : string list) data (tree : 'a t) =
if is_empty tree then
match key with
| [] -> Leaf data
| seg :: tl ->
let subtree = insert tl data empty in
let singleton = String_map.singleton seg subtree in
Node singleton
else
match key with
| [] -> raise Collision_with_prefix
| seg :: tl -> (
match tree with
| Leaf _ -> raise Collision_with_suffix
| Node map ->
let subtree =
match String_map.find_opt seg map with
| None -> insert tl data empty
| Some subtree -> insert tl data subtree
in
let map = String_map.add seg subtree map in
Node map)
let rec does_not_collide key tree =
if is_empty tree then `Key_does_not_collide
else
match (key, tree) with
| [], Leaf _ -> `Key_exists
| _, Leaf _ -> `Key_has_prefix
| [], Node _ -> `Key_has_suffix
| seg :: tl, Node map -> (
match String_map.find_opt seg map with
| None -> `Key_does_not_collide
| Some subtree -> does_not_collide tl subtree)
let rec mem key tree =
match (key, tree) with
| [], Leaf _ -> true
| _, Leaf _ -> false
| [], Node _ -> false
| seg :: tl, Node map -> (
match String_map.find_opt seg map with
| None -> false
| Some subtree -> mem tl subtree)
let rec find_opt key tree =
match (key, tree) with
| [], Leaf v -> Some v
| _ :: _, Leaf _ -> None
| [], Node _ -> None
| seg :: tl, Node map -> (
match String_map.find_opt seg map with
| None -> None
| Some subtree -> find_opt tl subtree)
let rec to_seq path acc tree =
match tree with
| Leaf v -> fun () -> Seq.Cons ((List.rev path, v), acc)
| Node map ->
String_map.fold
(fun seg subtree acc -> to_seq (seg :: path) acc subtree)
map
acc
let to_seq tree = to_seq [] Seq.empty tree
let of_seq seq = Seq.fold_left (fun map (k, v) -> insert k v map) empty seq
let fold_lwt f m accu =
Seq.S.fold_left (fun acc (k, v) -> f k v acc) accu (to_seq m)
let sample_uniform map =
if is_empty map then None
else
let seq = to_seq map in
let arr = Array.of_seq seq in
let len = Array.length arr in
let i = Random.int len in
Some arr.(i)
let encoding value_encoding =
let open Data_encoding in
conv
(fun map -> List.of_seq (to_seq map))
(fun l -> of_seq (List.to_seq l))
(list (tup2 (list string) value_encoding))
end
let rec take_n n list acc =
if n = 0 then (List.rev acc, list)
else
match list with
| [] -> Stdlib.invalid_arg "take_n"
| x :: tl -> take_n (n - 1) tl (x :: acc)
let sample_without_replacement n list =
let first_n, rest = take_n n list [] in
let reservoir = Array.of_list first_n in
let reject = ref [] in
List.iteri
(fun index elt ->
let i = n + index in
let j = Random.int (i + 1) in
if j < n then (
reject := reservoir.(j) :: !reject ;
reservoir.(j) <- elt)
else reject := elt :: !reject)
rest ;
(Array.to_list reservoir, !reject)
let buffer_size = 8192
let buffer = Bytes.create buffer_size
let file_copy input_name output_name =
let open Unix in
let fd_in = openfile input_name [O_RDONLY] 0 in
let fd_out = openfile output_name [O_WRONLY; O_CREAT; O_TRUNC] 0o660 in
let rec copy_loop () =
match read fd_in buffer 0 buffer_size with
| 0 -> ()
| r ->
ignore (write fd_out buffer 0 r) ;
copy_loop ()
in
copy_loop () ;
close fd_in ;
close fd_out
let set_infos filename infos =
let open Unix in
utimes filename infos.st_atime infos.st_mtime ;
chmod filename infos.st_perm ;
try chown filename infos.st_uid infos.st_gid
with Unix_error (EPERM, _, _) -> ()
let iter_dir f dirname =
let open Unix in
let d = opendir dirname in
try
while true do
f (readdir d)
done
with End_of_file -> closedir d
let rec copy_rec source dest =
let open Unix in
let infos = lstat source in
match infos.st_kind with
| S_REG ->
file_copy source dest ;
set_infos dest infos
| S_LNK ->
let link = readlink source in
symlink link dest
| S_DIR ->
mkdir dest 0o755 ;
iter_dir
(fun file ->
if
file <> Filename.current_dir_name
&& file <> Filename.parent_dir_name
then
copy_rec (Filename.concat source file) (Filename.concat dest file))
source ;
set_infos dest infos
| _ -> prerr_endline ("Can't cope with special file " ^ source)