Source file block_repr_unix.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
open Block_repr
let decode_block_repr encoding block_bytes =
try Data_encoding.Binary.of_bytes_exn encoding block_bytes
with _ ->
let legacy_block =
Data_encoding.Binary.of_bytes_exn legacy_encoding block_bytes
in
let legacy_metadata = legacy_block.legacy_metadata in
let metadata =
match legacy_metadata with
| Some metadata ->
let {
legacy_message;
legacy_max_operations_ttl;
legacy_last_allowed_fork_level;
legacy_block_metadata;
legacy_operations_metadata;
} =
metadata
in
let operations_metadata =
(List.map (List.map (fun x -> Block_validation.Metadata x)))
legacy_operations_metadata
in
Some
({
message = legacy_message;
max_operations_ttl = legacy_max_operations_ttl;
last_allowed_fork_level = legacy_last_allowed_fork_level;
block_metadata = legacy_block_metadata;
operations_metadata;
}
: metadata)
| None -> None
in
{
hash = legacy_block.legacy_hash;
contents = legacy_block.legacy_contents;
metadata;
}
let read_next_block_exn fd =
let open Lwt_syntax in
let length_bytes = Bytes.create 4 in
let* () = Lwt_utils_unix.read_bytes ~pos:0 ~len:4 fd length_bytes in
let block_length_int32 = Bytes.get_int32_be length_bytes 0 in
let block_length = Int32.to_int block_length_int32 in
let block_bytes = Bytes.extend length_bytes 0 block_length in
let* () = Lwt_utils_unix.read_bytes ~pos:4 ~len:block_length fd block_bytes in
Lwt.return (decode_block_repr encoding block_bytes, 4 + block_length)
let read_next_block fd = Option.catch_s (fun () -> read_next_block_exn fd)
let pread_block_exn fd ~file_offset =
let open Lwt_syntax in
let length_bytes = Bytes.create 4 in
let* () =
Lwt_utils_unix.read_bytes ~file_offset ~pos:0 ~len:4 fd length_bytes
in
let block_length_int32 = Bytes.get_int32_be length_bytes 0 in
let block_length = Int32.to_int block_length_int32 in
let block_bytes = Bytes.extend length_bytes 0 block_length in
let* () =
Lwt_utils_unix.read_bytes
~file_offset:(file_offset + 4)
~pos:4
~len:block_length
fd
block_bytes
in
Lwt.return (decode_block_repr encoding block_bytes, 4 + block_length)
let pread_block fd ~file_offset =
Option.catch_s (fun () -> pread_block_exn fd ~file_offset)
let raw_pruned_block_length bytes =
let offset = 4 in
let offset = offset + Block_hash.size in
let = Bytes.get_int32_be bytes offset in
let offset = offset + 4 + Int32.to_int header_length in
let operations_length = Bytes.get_int32_be bytes offset in
let offset = offset + 4 + Int32.to_int operations_length in
let offset =
if Bytes.get_uint8 bytes offset = 0xff then
offset + 1 + Block_metadata_hash.size
else offset + 1
in
let offset =
if Bytes.get_uint8 bytes offset = 0xff then
let offset = offset + 1 in
let operation_metadata_hashes_length = Bytes.get_int32_be bytes offset in
offset + 4 + Int32.to_int operation_metadata_hashes_length
else offset + 1
in
offset
let prune_raw_block_bytes bytes =
let pruned_block_length = raw_pruned_block_length bytes in
Bytes.set_int32_be bytes 0 (Int32.of_int (pruned_block_length - 4)) ;
pruned_block_length
let hash_offset = 4
let = hash_offset + Block_hash.size
let level_offset = header_length_offset + 4
let predecessor_offset = level_offset + 4 + 1
let raw_get_block_hash block_bytes =
Block_hash.of_bytes_exn (Bytes.sub block_bytes hash_offset Block_hash.size)
let raw_get_block_level block_bytes =
Bytes.get_int32_be block_bytes level_offset
let raw_get_block_predecessor block_bytes =
Block_hash.of_bytes_exn
(Bytes.sub block_bytes predecessor_offset Block_hash.size)
let raw_get_last_allowed_fork_level block_bytes total_block_length =
let = Bytes.get_int32_be block_bytes header_length_offset in
let operations_length_offset =
header_length_offset + 4 + Int32.to_int header_length
in
let operations_length =
Bytes.get_int32_be block_bytes operations_length_offset
in
let block_metadata_hash_offset =
operations_length_offset + 4 + Int32.to_int operations_length
in
let operation_metadata_hashes_offset =
if Bytes.get_uint8 block_bytes block_metadata_hash_offset = 0xff then
block_metadata_hash_offset + 1 + Block_metadata_hash.size
else block_metadata_hash_offset + 1
in
let metadata_offset =
if Bytes.get_uint8 block_bytes operation_metadata_hashes_offset = 0xff then
let operation_metadata_hashes_length =
Bytes.get_int32_be block_bytes (operation_metadata_hashes_offset + 1)
in
operation_metadata_hashes_offset + 1 + 4
+ Int32.to_int operation_metadata_hashes_length
else operation_metadata_hashes_offset + 1
in
if metadata_offset = total_block_length then None
else
let lafl_offset =
2
+
if Bytes.get_uint8 block_bytes metadata_offset = 0xff then
let message_length =
Bytes.get_int32_be block_bytes (metadata_offset + 1)
in
metadata_offset + 1 + 4 + Int32.to_int message_length
else metadata_offset + 1
in
Some (Bytes.get_int32_be block_bytes lafl_offset)
let fitness_length_offset =
predecessor_offset + Block_hash.size + 8 + 1
+ 32
let raw_get_context block_bytes =
let fitness_length = Bytes.get_int32_be block_bytes fitness_length_offset in
let context_offset =
fitness_length_offset + 4 + Int32.to_int fitness_length
in
Context_hash.of_bytes_exn
(Bytes.sub block_bytes context_offset Context_hash.size)