-
Notifications
You must be signed in to change notification settings - Fork 5
/
bitcoin-transactions.ads
142 lines (118 loc) · 4.94 KB
/
bitcoin-transactions.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
-- 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;
with Bitcoin_Like;
with Bitcoin_Like.Scripts;
with Bitcoin_Like.Transactions;
with Bitcoin_Like.Transaction_Inputs;
with Bitcoin_Like.Transaction_Outputs;
with Bitcoin_Like.Transaction_Input_Witnesses;
with Bitcoin_Like.Data_Accessors;
generic
with package Data_Readers is new Bitcoin_Like.Data_Accessors.Readers (<>);
with package Data_Writers is new Bitcoin_Like.Data_Accessors.Writers (<>);
package Bitcoin.Transactions is
-- min. possible size for standard input is
-- 32 bytes txid, 4 bytes prev_in, 1 byte for empty script, 4 bytes for sequence number
Min_Transaction_Input_Size: constant Positive := 41;
-- min. possible size for standard output is
-- 8 bytes value, 1 byte size of scriptPubKey, 1 byte OP_RETURN
Min_Transaction_Output_Size: constant Positive := 10;
Min_Transaction_Auxiliary_Data_Size: Constant Positive :=
(
4 -- version_number
+ 5 -- compact size for number of inputs
+ 1 -- compact size for number of outputs (minumal number of outputs)
+ 4 -- lock_time
);
-- NOTE: The max number of inputs and outputs in a transaction might be too large
-- for the practical cases. These constants influence the size of the records,
-- and therefore influence the amount of memory consumed to hold the transaction
-- record. If memory is limited, values of these constants can be reduced.
Max_Inputs_Per_Transaction: constant Positive :=
(
(Bitcoin.Max_Standard_Transaction_Weight / 4) -- non-witness weight is not discounted
- Min_Transaction_Auxiliary_Data_Size
- Min_Transaction_Output_Size
) / Min_Transaction_Input_Size;
Max_Outputs_Per_Transaction: constant Positive :=
(
(Bitcoin.Max_Standard_Transaction_Weight / 4) -- non-witness weight is not discounted
- Min_Transaction_Auxiliary_Data_Size
- Min_Transaction_Input_Size
) / Min_Transaction_Output_Size;
Max_Inputs_Per_Nonstandard_Transaction: constant Positive :=
(
-- non-standard transaction might take the whole block
(Bitcoin.Max_Block_Weight / 4) -- non-witness weight is not discounted
- Min_Transaction_Auxiliary_Data_Size
- Min_Transaction_Output_Size
- (Min_Transaction_Auxiliary_Data_Size
+ Min_Transaction_Input_Size
+ Min_Transaction_Output_Size) -- min size of coinbase transaction
) / Min_Transaction_Input_Size;
type Version_Type is range 1..2;
type Previous_Output_Index_Type is range 0..Max_Inputs_Per_Nonstandard_Transaction;
package P2SH_Scripts is new Bitcoin_Like.Scripts(
Max_Data_Size => Bitcoin.Max_Script_Element_Size,
Data_Readers => Data_Readers,
Data_Writers => Data_Writers
);
package P2WSH_Scripts is new Bitcoin_Like.Scripts(
Max_Data_Size => Bitcoin.Max_Standard_P2WSH_Script_Size,
Data_Readers => Data_Readers,
Data_Writers => Data_Writers
);
package Signature_Scripts is new Bitcoin_Like.Scripts(
Max_Data_Size => Bitcoin.Max_Standard_Signature_Script_Size,
Data_Readers => Data_Readers,
Data_Writers => Data_Writers
);
package Pubkey_Scripts is new Bitcoin_Like.Scripts(
Max_Data_Size => Bitcoin.Max_Standard_Pubkey_Script_Size,
Data_Readers => Data_Readers,
Data_Writers => Data_Writers
);
package Inputs is new Bitcoin_Like.Transaction_Inputs(
Data_Readers => Data_Readers,
Data_Writers => Data_Writers,
Scripts => Signature_Scripts,
Previous_Output_Index_Type => Previous_Output_Index_Type
);
package Outputs is new Bitcoin_Like.Transaction_Outputs(
Data_Readers => Data_Readers,
Data_Writers => Data_Writers,
Scripts => Pubkey_Scripts,
Satoshi_Type => Bitcoin.Satoshi_Type
);
package Input_Witnesses is new Bitcoin_Like.Transaction_Input_Witnesses(
Data_Readers => Data_Readers,
Data_Writers => Data_Writers,
Scripts => P2WSH_Scripts,
Max_Witness_Data_Size => Bitcoin.Max_Standard_P2WSH_Stack_Item_Size,
Max_Witness_Stack_Items => Bitcoin.Max_Standard_P2WSH_Stack_Items
);
package Transactions is new Bitcoin_Like.Transactions(
Data_Readers => Data_Readers,
Data_Writers => Data_Writers,
Version_Type => Version_Type,
Inputs => Inputs,
Outputs => Outputs,
Input_Witnesses => Input_Witnesses,
-- It is not practical to put bigger numbers here, because with
-- maximum allowed script size the memory required becomes too big.
-- For practical use, dynamic memory allocation should be employed.
-- But for demostration purposes, this will suffice.
Max_Inputs => 100,
Max_Outputs => 100
);
end Bitcoin.Transactions;