-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbitcoin_like-data_accessors.ads
402 lines (372 loc) · 16.8 KB
/
bitcoin_like-data_accessors.ads
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
-- Copyright (C) 2019-2020 Dmitry Petukhov https://github.com/dgpv
--
-- This file is part of spark-bitcoin-transaction-example
--
-- It is subject to the license terms in the LICENSE file found in the top-level
-- directory of this distribution.
--
-- No part of spark-bitcoin-transaction-example, including this file, may be copied, modified,
-- propagated, or distributed except according to the terms contained in the
-- LICENSE file.
pragma SPARK_Mode(On);
with Data_Controls;
with Utils; use Utils;
with Bitcoin_Like.Utils; use Bitcoin_Like.Utils;
with Bitcoin_Like.Transaction_Model; use Bitcoin_Like.Transaction_Model;
package Bitcoin_Like.Data_Accessors is
generic
with package Status_Control is new Data_Controls.Combined_Status_Control (
Model_Types => Transaction_Model_Types,
others => <>
);
with procedure Read_Byte_Procedure(Byte: out Byte_Type);
package Readers is
use Status_Control;
-- we cannot enforce pre/post conditions on generic subprogram parameter,
-- but we can wrap it in a procedure that will have the pre/post condtions.
procedure Read_Byte(Byte: out Byte_Type)
with
Pre => Status_OK,
Post => (
if Status_OK
then (
Bytes_Processed_Since(Raw_Data_Status'Old) = 1
and then
Ghost_Data_Tape = Ghost_Data_Tape'Old'Update(
Bytes_Processed_With(Raw_Data_Status'Old) => Byte
)
)
else Bytes_Processed_Since(Raw_Data_Status'Old) = 0
),
Depends => (
Raw_Data_Status =>+ null, Structural_Status =>+ Raw_Data_Status,
Byte => Raw_Data_Status, Ghost_Data_Tape =>+ Raw_Data_Status
),
Global => (In_Out => (Raw_Data_Status, Structural_Status, Ghost_Data_Tape));
generic
type Octet_Type is mod <>;
package Block_Reader is
type Block_Type is array (Positive range <>) of Octet_Type;
procedure Read_Block(Block: out Block_Type)
with
Pre => Status_OK and Block'Length > 0,
Post => (
if Status_OK
then (
Bytes_Processed_Since(Raw_Data_Status'Old) = Block'Length
and then
Block_Ghost_Tape_Match(Block, Bytes_Processed_With(Raw_Data_Status'Old))
and then (
for all I in Ghost_Data_Tape'Range => (
if (I < Bytes_Processed_With(Raw_Data_Status'Old)
or I >= Bytes_Processed_With(Raw_Data_Status'Old) + Block'Length)
then Ghost_Data_Tape(I) = Ghost_Data_Tape'Old(I)
)
)
)
else Bytes_Processed_Since(Raw_Data_Status'Old) in 0..Block'Length-1
),
Depends => (
(Block, Structural_Status, Raw_Data_Status) =>+ (Block, Structural_Status, Raw_Data_Status),
Ghost_Data_Tape =>+ (Block, Raw_Data_Status, Structural_Status)
),
Global => (In_Out => (Raw_Data_Status, Structural_Status, Ghost_Data_Tape));
procedure Read_Block_Zero_Fill(
Block : out Block_Type;
Length : in Positive
)
with
Pre => Status_OK and Block'Length > 0 and Block'Length >= Length,
Post => (
if Status_OK
then (
Bytes_Processed_Since(Raw_Data_Status'Old) = Length
and then
Block_Ghost_Tape_Match(Block(Block'First..Block'First + (Length - 1)),
Bytes_Processed_With(Raw_Data_Status'Old))
and then (
for all I in Ghost_Data_Tape'Range => (
if (I < Bytes_Processed_With(Raw_Data_Status'Old)
or I >= Bytes_Processed_With(Raw_Data_Status'Old) + Length)
then Ghost_Data_Tape(I) = Ghost_Data_Tape'Old(I)
)
)
)
else Bytes_Processed_Since(Raw_Data_Status'Old) in 0..Length-1
),
Depends => (
(Block, Structural_Status, Raw_Data_Status)
=>+ (Length, Raw_Data_Status, Structural_Status),
Ghost_Data_Tape =>+ (Length, Raw_Data_Status, Structural_Status)
),
Global => (In_Out => (Raw_Data_Status, Structural_Status, Ghost_Data_Tape));
-- XXX spotted potential compiler bug - it was not able to find 'Block_Reader' package
-- when function was declared like this here:
-- function Block_Ghost_Tape_Match(Block: Block_Type; Offset: Natural) return Boolean is (True);
function Block_Ghost_Tape_Match(Block: Block_Type; Offset: Natural) return Boolean
with Ghost => True,
Pre => Offset <= Natural'Last - Block'Length,
Post => (
Block_Ghost_Tape_Match'Result
= (for all I in 1..Block'Length =>
Octet_Type(Ghost_Data_Tape(Offset + (I - 1))) = Block(Block'First + (I - 1)))
),
Global => (Input => Ghost_Data_Tape);
end Block_Reader;
function Compact_Data_Size_Ghost_Tape_Match(Size: Compact_Size_Type; Offset: Natural) return Boolean
is (
if Size < 16#FD# then
Offset <= Natural'Last and then Ghost_Data_Tape(Offset) = Byte_Type(Size)
elsif Size <= 16#FFFF# then
(
Offset <= Natural'Last - 2
and then
Ghost_Data_Tape(Offset) = 16#FD#
and then
Unsigned_Ranged_Int16(Size) = (
Unsigned_Ranged_Int16(Ghost_Data_Tape(Offset + 1))
+ Unsigned_Ranged_Int16(Ghost_Data_Tape(Offset + 2)) * 2**8
)
)
elsif Unsigned_Modular_Int64(Size) <= 16#FFFFFFFF# then
(
Offset <= Natural'Last - 4
and then
Ghost_Data_Tape(Offset) = 16#FE#
and then
Unsigned_Ranged_Int32(Size) = (
Unsigned_Ranged_Int32(Ghost_Data_Tape(Offset + 1))
+ Unsigned_Ranged_Int32(Ghost_Data_Tape(Offset + 2)) * 2**8
+ Unsigned_Ranged_Int32(Ghost_Data_Tape(Offset + 3)) * 2**16
+ Unsigned_Ranged_Int32(Ghost_Data_Tape(Offset + 4)) * 2**24
)
)
else
Offset <= Natural'Last - 8
and then
Ghost_Data_Tape(Offset) = 16#FF#
and then
Unsigned_Modular_Int64(Size) = (
Unsigned_Modular_Int64(Ghost_Data_Tape(Offset + 1))
+ Unsigned_Modular_Int64(Ghost_Data_Tape(Offset + 2)) * 2**8
+ Unsigned_Modular_Int64(Ghost_Data_Tape(Offset + 3)) * 2**16
+ Unsigned_Modular_Int64(Ghost_Data_Tape(Offset + 4)) * 2**24
+ Unsigned_Modular_Int64(Ghost_Data_Tape(Offset + 5)) * 2**32
+ Unsigned_Modular_Int64(Ghost_Data_Tape(Offset + 6)) * 2**40
+ Unsigned_Modular_Int64(Ghost_Data_Tape(Offset + 7)) * 2**48
+ Unsigned_Modular_Int64(Ghost_Data_Tape(Offset + 8)) * 2**56
)
)
with Ghost => True,
Pre => Offset <= Natural'Last - Size_of_Compact_Size(Size),
Global => (Input => Ghost_Data_Tape);
procedure Read_Compact_Size(Size: out Compact_Size_Type)
with
Pre => Status_OK,
Post => (
if Status_OK
then (
Bytes_Processed_Since(Raw_Data_Status'Old) = Size_of_Compact_Size(Size)
and then
Compact_Data_Size_Ghost_Tape_Match(Size, Bytes_Processed_With(Raw_Data_Status'Old))
and then
(
for all I in Ghost_Data_Tape'Range => (
if (I < Bytes_Processed_With(Raw_Data_Status'Old)
or I >= Bytes_Processed_With(Raw_Data_Status'Old) + Size_of_Compact_Size(Size))
then Ghost_Data_Tape(I) = Ghost_Data_Tape'Old(I)
)
)
)
else Size = 0
),
Depends => (
Size => (Raw_Data_Status, Structural_Status),
Raw_Data_Status =>+ Structural_Status,
Structural_Status =>+ Raw_Data_Status,
Ghost_Data_Tape =>+ (Raw_Data_Status, Structural_Status)
),
Global => (In_Out => (Raw_Data_Status, Structural_Status, Ghost_Data_Tape));
procedure Read_Unsigned_16(Value: out Unsigned_Ranged_Int16)
with
Pre => Status_OK,
Post => (
if Status_OK
then (
Bytes_Processed_Since(Raw_Data_Status'Old) = 2
and then
Value = (
Unsigned_Ranged_Int16(
Ghost_Data_Tape(Bytes_Processed_With(Raw_Data_Status'Old)))
+ Unsigned_Ranged_Int16(
Ghost_Data_Tape(Bytes_Processed_With(Raw_Data_Status'Old) + 1)) * 2**8
)
and then
(
for all I in Ghost_Data_Tape'Range =>
(if (I < Bytes_Processed_With(Raw_Data_Status'Old)
or I >= Bytes_Processed_With(Raw_Data_Status'Old) + 2)
then Ghost_Data_Tape(I) = Ghost_Data_Tape'Old(I))
)
)
else Bytes_Processed_Since(Raw_Data_Status'Old) in 0..1 and Value = 0
),
Depends => (
Value => (Raw_Data_Status, Structural_Status),
(Structural_Status, Raw_Data_Status) =>+ (Raw_Data_Status, Structural_Status),
Ghost_Data_Tape =>+ (Raw_Data_Status, Structural_Status)
),
Global => (In_Out => (Raw_Data_Status, Structural_Status, Ghost_Data_Tape));
function Unsigned32_Ghost_Tape_Match(Value: Unsigned_Ranged_Int32; Offset: Natural) return Boolean
is (
Value = (
Unsigned_Ranged_Int32(Ghost_Data_Tape(Offset))
+ Unsigned_Ranged_Int32(Ghost_Data_Tape(Offset + 1)) * 2**8
+ Unsigned_Ranged_Int32(Ghost_Data_Tape(Offset + 2)) * 2**16
+ Unsigned_Ranged_Int32(Ghost_Data_Tape(Offset + 3)) * 2**24
)
) with Ghost => True,
Pre => Offset <= Natural'Last - 4,
Global => (Input => Ghost_Data_Tape);
procedure Read_Unsigned_32(Value: out Unsigned_Ranged_Int32)
with
Pre => Status_OK,
Post => (
if Status_OK
then (
Bytes_Processed_Since(Raw_Data_Status'Old) = 4
and then
Unsigned32_Ghost_Tape_Match(Value, Bytes_Processed_With(Raw_Data_Status'Old))
and then
(
for all I in Ghost_Data_Tape'Range =>
(if (I < Bytes_Processed_With(Raw_Data_Status'Old)
or I >= Bytes_Processed_With(Raw_Data_Status'Old) + 4)
then Ghost_Data_Tape(I) = Ghost_Data_Tape'Old(I))
)
)
else Bytes_Processed_Since(Raw_Data_Status'Old) in 0..3 and Value = 0
),
Depends => (
Value => (Raw_Data_Status, Structural_Status),
(Structural_Status, Raw_Data_Status) =>+ (Raw_Data_Status, Structural_Status),
Ghost_Data_Tape =>+ (Raw_Data_Status, Structural_Status)
),
Global => (In_Out => (Raw_Data_Status, Structural_Status, Ghost_Data_Tape));
function Unsigned64_Ghost_Tape_Match(Value: Unsigned_Modular_Int64; Offset: Natural) return Boolean
is (
Value = (
Unsigned_Modular_Int64(Ghost_Data_Tape(Offset))
+ Unsigned_Modular_Int64(Ghost_Data_Tape(Offset + 1)) * 2**8
+ Unsigned_Modular_Int64(Ghost_Data_Tape(Offset + 2)) * 2**16
+ Unsigned_Modular_Int64(Ghost_Data_Tape(Offset + 3)) * 2**24
+ Unsigned_Modular_Int64(Ghost_Data_Tape(Offset + 4)) * 2**32
+ Unsigned_Modular_Int64(Ghost_Data_Tape(Offset + 5)) * 2**40
+ Unsigned_Modular_Int64(Ghost_Data_Tape(Offset + 6)) * 2**48
+ Unsigned_Modular_Int64(Ghost_Data_Tape(Offset + 7)) * 2**56
)
) with Ghost => True,
Pre => Offset <= Natural'Last - 8,
Global => (Input => Ghost_Data_Tape);
procedure Read_Unsigned_64(Value: out Unsigned_Modular_Int64)
with
Pre => Status_OK,
Post => (
if Status_OK
then (
Bytes_Processed_Since(Raw_Data_Status'Old) = 8
and then
Unsigned64_Ghost_Tape_Match(Value, Bytes_Processed_With(Raw_Data_Status'Old))
and then
(
for all I in Ghost_Data_Tape'Range => (
if (I < Bytes_Processed_With(Raw_Data_Status'Old)
or I >= Bytes_Processed_With(Raw_Data_Status'Old) + 8)
then Ghost_Data_Tape(I) = Ghost_Data_Tape'Old(I)
)
)
)
else Bytes_Processed_Since(Raw_Data_Status'Old) in 0..7 and Value = 0
),
Depends => (
Value => (Raw_Data_Status, Structural_Status),
(Structural_Status, Raw_Data_Status) =>+ (Raw_Data_Status, Structural_Status),
Ghost_Data_Tape =>+ (Raw_Data_Status, Structural_Status)
),
Global => (In_Out => (Raw_Data_Status, Structural_Status, Ghost_Data_Tape));
end Readers;
generic
with package Status_Control is new Data_Controls.Combined_Status_Control (<>);
with procedure Write_Byte_Procedure(Byte: in Byte_Type);
package Writers is
use Status_Control;
procedure Write_Byte(Byte: in Byte_Type)
with
Pre => Status_OK,
Post => (
if Status_OK
then Bytes_Processed_Since(Raw_Data_Status'Old) = 1
else Bytes_Processed_Since(Raw_Data_Status'Old) = 0
),
Depends => (Raw_Data_Status =>+ null, Structural_Status =>+ Raw_Data_Status, null => Byte),
Global => (In_Out => (Raw_Data_Status, Structural_Status));
generic
type Octet_Type is mod <>;
package Block_Writer is
type Block_Type is array (Positive range <>) of Octet_Type;
procedure Write_Block(Block: in Block_Type)
with
Pre => Status_OK and Block'Length > 0,
Post => (
if Status_OK
then Bytes_Processed_Since(Raw_Data_Status'Old) = Block'Length
else Bytes_Processed_Since(Raw_Data_Status'Old) in 0..Block'Length-1
),
Depends => (
Raw_Data_Status =>+ (Block, Structural_Status),
Structural_Status =>+ (Block, Raw_Data_Status)
),
Global => (In_Out => (Raw_Data_Status, Structural_Status));
end Block_Writer;
procedure Write_Compact_Size(Size: in Compact_Size_Type)
with
Pre => Status_OK,
Post => (
if Status_OK
then Bytes_Processed_Since(Raw_Data_Status'Old) = Size_of_Compact_Size(Size)
else Bytes_Processed_Since(Raw_Data_Status'Old) in 0..Size_of_Compact_Size(Size)-1
),
Depends => (Raw_Data_Status =>+ (Size, Structural_Status), Structural_Status =>+ (Size, Raw_Data_Status)),
Global => (In_Out => (Raw_Data_Status, Structural_Status));
procedure Write_Unsigned_16(Value: in Unsigned_Ranged_Int16)
with
Pre => Status_OK,
Post => (
if Status_OK
then Bytes_Processed_Since(Raw_Data_Status'Old) = 2
else Bytes_Processed_Since(Raw_Data_Status'Old) in 0..1
),
Depends => ((Raw_Data_Status, Structural_Status) =>+ (Structural_Status, Raw_Data_Status), null => Value),
Global => (In_Out => (Raw_Data_Status, Structural_Status));
procedure Write_Unsigned_32(Value: in Unsigned_Ranged_Int32)
with
Pre => Status_OK,
Post => (
if Status_OK
then Bytes_Processed_Since(Raw_Data_Status'Old) = 4
else Bytes_Processed_Since(Raw_Data_Status'Old) in 0..3
),
Depends => ((Raw_Data_Status, Structural_Status) =>+ (Structural_Status, Raw_Data_Status), null => Value),
Global => (In_Out => (Raw_Data_Status, Structural_Status));
procedure Write_Unsigned_64(Value: in Unsigned_Modular_Int64)
with
Pre => Status_OK,
Post => (
if Status_OK
then Bytes_Processed_Since(Raw_Data_Status'Old) = 8
else Bytes_Processed_Since(Raw_Data_Status'Old) in 0..7
),
Depends => ((Raw_Data_Status, Structural_Status) =>+ (Structural_Status, Raw_Data_Status), null => Value),
Global => (In_Out => (Raw_Data_Status, Structural_Status));
end Writers;
end Bitcoin_Like.Data_Accessors;