-
Notifications
You must be signed in to change notification settings - Fork 5
/
bitcoin_like-transaction_inputs.adb
264 lines (234 loc) · 11.2 KB
/
bitcoin_like-transaction_inputs.adb
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
-- 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 Bitcoin_Like.Utils; use Bitcoin_Like.Utils;
package body Bitcoin_Like.Transaction_Inputs is
procedure Deserialize(Input: out Input_Type) is
use Data_Checkpoints;
U32_Value: Unsigned_Ranged_Int32;
Raw_Data_Status_Old: Raw_Data.Status_Type := Raw_Data_Status with Ghost;
begin
Data_Checkpoint(Tag_Txid);
Txid_Reader.Read_Block(Txid_Reader.Block_Type(Input.Previous_Output_Point.Txid));
if Got_Error then
Input := Empty_Input;
return;
end if;
Data_Checkpoint(Tag_Prevout_Index);
pragma Assert_And_Cut (
Status_OK
and then
Current_Data_Tag = Tag_Prevout_Index
and then
Data_Checkpoints.Partial_State_Consistent(
Bytes_Processed_Since(Raw_Data_Status_Old),
Tag_Txid
)
and then
Txid_Reader.Block_Ghost_Tape_Match(
Txid_Reader.Block_Type(Input.Previous_Output_Point.Txid),
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(
Tag_Txid, Data_Checkpoints.State))
and then (
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(Tag_Txid, Data_Checkpoints.State)
= Bytes_Processed_With(Raw_Data_Status_Old))
and then (Bytes_Processed = Bytes_Processed_With(Raw_Data_Status_Old) + Input.Previous_Output_Point.Txid'Length)
and then Data_Checkpoints.Checkpoint_States.Bytes_Processed_Prev(Data_Checkpoints.State) = Bytes_Processed
);
Data_Readers.Read_Unsigned_32(U32_Value);
if Got_Error then
Input := Empty_Input;
return;
end if;
if U32_Value > Unsigned_Ranged_Int32(Previous_Output_Index_Type'Last) then
Register_Structural_Error(
"Encountered index for previous output" & Unsigned_Ranged_Int32'Image(U32_Value)
& " is larger than maximum expected length of" & Previous_Output_Index_Type'Image(Previous_Output_Index_Type'Last)
);
Input := Empty_Input;
return;
end if;
-- If we want to ensure that the fields are only set within appropriate contexts,
-- we can use setter procedures that would check Current_Data_Tag in precondition.
--
-- Set_Previous_Output_Index(Input, Previous_Output_Index_Type(U32_Value));
Input.Previous_Output_Point.Index := Previous_Output_Index_Type(U32_Value);
Data_Checkpoint(Tag_Signature_Script);
pragma Assert_And_Cut (
Status_OK
and then
Current_Data_Tag = Tag_Signature_Script
and then
Data_Checkpoints.Partial_State_Consistent(
Bytes_Processed_Since(Raw_Data_Status_Old),
Tag_Prevout_Index
)
and then
Txid_Reader.Block_Ghost_Tape_Match(
Txid_Reader.Block_Type(Input.Previous_Output_Point.Txid),
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(
Tag_Txid, Data_Checkpoints.State))
and then (
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(Tag_Txid, Data_Checkpoints.State)
= Bytes_Processed_With(Raw_Data_Status_Old))
and then
Data_Readers.Unsigned32_Ghost_Tape_Match(
Unsigned_Ranged_Int32(Input.Previous_Output_Point.Index),
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(
Tag_Prevout_Index, Data_Checkpoints.State))
and then (
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(Tag_Prevout_Index, Data_Checkpoints.State)
= Bytes_Processed_With(Raw_Data_Status_Old) + Input.Previous_Output_Point.Txid'Length)
and then (Bytes_Processed = Bytes_Processed_With(Raw_Data_Status_Old) + Input.Previous_Output_Point.Txid'Length + 4)
and then Data_Checkpoints.Checkpoint_States.Bytes_Processed_Prev(Data_Checkpoints.State) = Bytes_Processed
);
Scripts.Deserialize(Input.Signature_Script);
if Got_Error then
Input := Empty_Input;
return;
end if;
-- prover tends to lose the state to verify this condition,
-- this Assert helps to retain it until the next Assert_And_Cut
pragma Assert (
Txid_Reader.Block_Ghost_Tape_Match(
Txid_Reader.Block_Type(Input.Previous_Output_Point.Txid),
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(
Tag_Txid, Data_Checkpoints.State))
);
Data_Checkpoint(Tag_Sequence_Number);
pragma Assert_And_Cut (
Status_OK
and then
Current_Data_Tag = Tag_Sequence_Number
and then
Data_Checkpoints.Partial_State_Consistent(
Bytes_Processed_Since(Raw_Data_Status_Old),
Tag_Signature_Script
)
and then
Txid_Reader.Block_Ghost_Tape_Match(
Txid_Reader.Block_Type(Input.Previous_Output_Point.Txid),
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(
Tag_Txid, Data_Checkpoints.State))
and then (
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(Tag_Txid, Data_Checkpoints.State)
= Bytes_Processed_With(Raw_Data_Status_Old))
and then
Data_Readers.Unsigned32_Ghost_Tape_Match(
Unsigned_Ranged_Int32(Input.Previous_Output_Point.Index),
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(
Tag_Prevout_Index, Data_Checkpoints.State))
and then (
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(Tag_Prevout_Index, Data_Checkpoints.State)
= Bytes_Processed_With(Raw_Data_Status_Old) + Input.Previous_Output_Point.Txid'Length)
and then
Scripts.Script_Ghost_Tape_Match(
Input.Signature_Script,
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(
Tag_Signature_Script, Data_Checkpoints.State))
and then (
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(Tag_Signature_Script, Data_Checkpoints.State)
= Bytes_Processed_With(Raw_Data_Status_Old) + Input.Previous_Output_Point.Txid'Length + 4)
and then
Data_Checkpoints.Element_Size_Match(
Tag_Signature_Script,
Long_Natural(
Size_of_Compact_Size(Scripts.Script_Length(Input.Signature_Script))
+ Scripts.Script_Length(Input.Signature_Script)
)
)
and then (Bytes_Processed = Bytes_Processed_With(Raw_Data_Status_Old) + Input.Previous_Output_Point.Txid'Length + 4 + Size_of_Compact_Size(Scripts.Script_Length(Input.Signature_Script)) + Scripts.Script_Length(Input.Signature_Script))
and then Data_Checkpoints.Checkpoint_States.Bytes_Processed_Prev(Data_Checkpoints.State) = Bytes_Processed
);
Data_Readers.Read_Unsigned_32(U32_Value);
if Got_Error then
Input := Empty_Input;
return;
end if;
Input.Sequence := Input_Sequence_Type(U32_Value);
-- prover tends to lose the state to verify this conditions,
-- this Assert helps to retain it until the next Assert_And_Cut
pragma Assert (
Txid_Reader.Block_Ghost_Tape_Match(
Txid_Reader.Block_Type(Input.Previous_Output_Point.Txid),
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(
Tag_Txid, Data_Checkpoints.State))
and then
Data_Readers.Unsigned32_Ghost_Tape_Match(
Unsigned_Ranged_Int32(Input.Previous_Output_Point.Index),
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(
Tag_Prevout_Index, Data_Checkpoints.State))
and then
Scripts.Script_Ghost_Tape_Match(
Input.Signature_Script,
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(
Tag_Signature_Script, Data_Checkpoints.State))
);
Data_Checkpoint_Final;
pragma Assert_And_Cut (
Status_OK
and then
Data_Checkpoints.Final_State_Consistent(
Bytes_Processed_Since(Raw_Data_Status_Old)
)
and then
Txid_Reader.Block_Ghost_Tape_Match(
Txid_Reader.Block_Type(Input.Previous_Output_Point.Txid),
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(
Tag_Txid, Data_Checkpoints.State))
and then (
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(Tag_Txid, Data_Checkpoints.State)
= Bytes_Processed_With(Raw_Data_Status_Old))
and then
Data_Readers.Unsigned32_Ghost_Tape_Match(
Unsigned_Ranged_Int32(Input.Previous_Output_Point.Index),
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(
Tag_Prevout_Index, Data_Checkpoints.State))
and then (
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(Tag_Prevout_Index, Data_Checkpoints.State)
= Bytes_Processed_With(Raw_Data_Status_Old) + Input.Previous_Output_Point.Txid'Length)
and then
Scripts.Script_Ghost_Tape_Match(
Input.Signature_Script,
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(
Tag_Signature_Script, Data_Checkpoints.State))
and then (
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(Tag_Signature_Script, Data_Checkpoints.State)
= Bytes_Processed_With(Raw_Data_Status_Old) + Input.Previous_Output_Point.Txid'Length + 4)
and then
Data_Checkpoints.Element_Size_Match(
Tag_Signature_Script,
Long_Natural(
Size_of_Compact_Size(Scripts.Script_Length(Input.Signature_Script))
+ Scripts.Script_Length(Input.Signature_Script)
)
)
and then (
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(Tag_Sequence_Number, Data_Checkpoints.State)
= Bytes_Processed_With(Raw_Data_Status_Old)
+ Input.Previous_Output_Point.Txid'Length + 4
+ Size_of_Compact_Size(Scripts.Script_Length(Input.Signature_Script))
+ Scripts.Script_Length(Input.Signature_Script))
and then
Data_Readers.Unsigned32_Ghost_Tape_Match(
Unsigned_Ranged_Int32(Input.Sequence),
Data_Checkpoints.Checkpoint_States.Ghost_Tape_Position(
Tag_Sequence_Number, Data_Checkpoints.State))
and then
Input_Serialized_Data_Length(Input)
= Long_Natural(Bytes_Processed_Since(Raw_Data_Status_Old))
and then
Input_Ghost_Tape_Match(Input, Bytes_Processed_With(Raw_Data_Status_Old))
-- and then (Bytes_Processed = Bytes_Processed_With(Raw_Data_Status_Old) + Input.Previous_Output_Point.Txid'Length + Size_of_Compact_Size(Scripts.Script_Length(Input.Signature_Script)) + Scripts.Script_Length(Input.Signature_Script) + 4)
-- and then Data_Checkpoints.Checkpoint_States.Bytes_Processed_Prev(Data_Checkpoints.State) = Bytes_Processed
);
end Deserialize;
end Bitcoin_Like.Transaction_Inputs;