File : transactions.ads
1 with Ada.Streams; use Ada.Streams;
2 with Interfaces; use Interfaces;
3 with Hashes; use Hashes;
4
5
6 package Transactions is
7
8 -- Exactly what is printed on the tin
9 type ByteArray is array (Positive range <>) of Unsigned_8;
10
11 -- 'Physical' constants of Bitcoin.
12 MAX_BLOCK_LENGTH : constant := 1000000;
13 MAX_SCRIPT_LENGTH : constant := 10000;
14 MAX_TX_INPUTS : constant := 25000; -- excessive
15 MAX_TX_OUTPUTS : constant := 25000; -- excessive
16
17 -- Hard Limits on a Tx
18 subtype TxInputs_Bounds is Positive range 1 .. MAX_TX_INPUTS;
19 subtype TxOutputs_Bounds is Positive range 1 .. MAX_TX_OUTPUTS;
20
21 -- These are universal
22 subtype Satoshis is Unsigned_64 range 0 .. Unsigned_64'Last;
23
24 -- Zero-satoshi outputs exist...
25 subtype TxValue is Satoshis range 0 .. Satoshis'Last;
26
27 package Traditional is
28
29 -- Bitcoin Scripts.
30 subtype Scripts_Bounds is Positive range 1 .. MAX_SCRIPT_LENGTH;
31 subtype Scriptolade_Bounds is Positive range 1 .. MAX_BLOCK_LENGTH;
32
33 -- Represented as segment of a scriptolade
34 type Script is
35 record
36 Offset : Scriptolade_Bounds := Scripts_Bounds'First; -- where is it
37 Length : Scripts_Bounds := 1;
38 end record;
39
40 -- An OutPoint points to a transaction and a particular output of it.
41 type OutPoint is
42 record
43 Hash : SHA256Hash; -- The referenced transaction
44 Index : Unsigned_32 := 0; -- Index of an output in that tx (from 0).
45 end record;
46
47 -- A Transaction Input. Consists of OutPoint, the Script, and Sequence.
48 type TxIn is
49 record
50 PreviousOutput : OutPoint; -- The output spent by this input
51 SignatureScript : Script; -- Proves that it can spend this.
52 Sequence : Unsigned_32
53 := 16#FFFFFFFF#; -- Useless, but it gets hashed over, so must store
54 end record;
55
56 -- A Transaction Output. Consists of a Value and a Script.
57 type TxOut is
58 record
59 Value : TxValue := 1; -- How much is being spent in this Tx
60 PkScript : Script; -- Conditions for the Tx's spendability.
61 end record;
62
63 -- Arrays of of Transaction Inputs.
64 type TxInputs is array (Positive range <>) of TxIn;
65
66 -- Arrays of Transaction Outputs.
67 type TxOutputs is array (Positive range <>) of TxOut;
68
69 -- Everything needed to determine the physical footprint of a FastTx:
70 type FastTxFrame is
71 record
72 N_Outs : TxOutputs_Bounds := 1; -- Number of Outputs
73 N_Ins : TxInputs_Bounds := 1; -- Number of Inputs
74 N_Script : Scriptolade_Bounds := 1; -- Length of Scriptolade
75 end record;
76
77 -- A Bitcoin Transaction -- Fast representation
78 type Tx(Inp_Slots : TxInputs_Bounds := MAX_TX_INPUTS;
79 Out_Slots : TxOutputs_Bounds := MAX_TX_OUTPUTS;
80 Scriptolade_Size : Scriptolade_Bounds := MAX_BLOCK_LENGTH) is
81 record
82 -- What the indexer wants to see immediately:
83 Hash : SHA256Hash; -- SHA256(SHA256(originaltx))
84 -- What indexer needs if actually snarfing:
85 Frame : FastTxFrame := (N_Outs => Out_Slots,
86 N_Ins => Inp_Slots,
87 N_Script => Scriptolade_Size);
88 Outputs : TxOutputs(TxOutputs_Bounds'First .. Out_Slots);
89 Inputs : TxInputs(TxInputs_Bounds'First .. Inp_Slots);
90 -- Scalars, from the original:
91 NBytes : Unsigned_32 := 0; -- length of the original, in bytes
92 Version : Integer_32 := 0;
93 LockTime : Unsigned_32 := 0;
94 -- Scriptolade
95 Scripts : ByteArray(1 .. Scriptolade_Size) := (others => 0);
96 end record;
97
98 -- Eggogs
99 TradTxNotRead : exception; -- Trad Tx Read did not complete
100 TradTxNotWritten : exception; -- Trad Tx Write did not complete
101
102
103 -- Read a Traditional representation of a Tx into its Fast Form
104 procedure TradTx_Read(Stream : not null access Root_Stream_Type'Class;
105 T : out Tx);
106 -- for Tx'Read use TradTx_Read;
107
108 -- Produce a Traditional representation of a Tx from its Fast Form
109 procedure TradTx_Write(Stream : not null access Root_Stream_Type'Class;
110 T : in Tx);
111 -- for Tx'Write use TradTx_Write;
112
113 end Traditional;
114
115 ---------------------------------------------------------------------------
116
117 end Transactions;