ScriptCode: fix stack depth precondition and add preproccessor directive checking...
[NovacoinLibrary.git] / Novacoin / CTransaction.cs
1 \feff/**
2  *  Novacoin classes library
3  *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
4
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using System;
20 using System.Text;
21 using System.Collections.Generic;
22
23 namespace Novacoin
24 {
25     /// <summary>
26     /// Represents the transaction. Any transaction must provide one input and one output at least.
27     /// </summary>
28     public class CTransaction
29     {
30         /// <summary>
31         /// Version of transaction schema.
32         /// </summary>
33         public uint nVersion = 1;
34
35         /// <summary>
36         /// Transaction timestamp.
37         /// </summary>
38         public uint nTime = 0;
39
40         /// <summary>
41         /// Array of transaction inputs
42         /// </summary>
43         public CTxIn[] vin;
44
45         /// <summary>
46         /// Array of transaction outputs
47         /// </summary>
48         public CTxOut[] vout;
49
50         /// <summary>
51         /// Block height or timestamp when transaction is final
52         /// </summary>
53         public uint nLockTime = 0;
54
55         /// <summary>
56         /// Initialize an empty instance
57         /// </summary>
58         public CTransaction()
59         {
60             // Initialize empty input and output arrays. Please note that such 
61             // configuration is not valid for real transaction, you have to supply 
62             // at least one input and one output.
63             vin = new CTxIn[0];
64             vout = new CTxOut[0];
65         }
66
67         /// <summary>
68         /// Initialize new instance as a copy of another transaction
69         /// </summary>
70         /// <param name="tx">Transaction to copy from</param>
71         public CTransaction(CTransaction tx)
72         {
73             nVersion = tx.nVersion;
74             nTime = tx.nTime;
75
76             vin = new CTxIn[tx.vin.Length];
77
78             for (int i = 0; i < vin.Length; i++)
79             {
80                 vin[i] = new CTxIn(tx.vin[i]);
81             }
82
83             vout = new CTxOut[tx.vout.Length];
84
85             for (int i = 0; i < vout.Length; i++)
86             {
87                 vout[i] = new CTxOut(tx.vout[i]);
88             }
89
90             nLockTime = tx.nLockTime;
91         }
92
93
94         /// <summary>
95         /// Parse byte sequence and initialize new instance of CTransaction
96         /// </summary>
97         /// <param name="txBytes">Byte sequence</param>
98                 public CTransaction(byte[] txBytes)
99         {
100             var wBytes = new ByteQueue(txBytes);
101
102             nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
103             nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
104
105             int nInputs = (int)wBytes.GetVarInt();
106             vin = new CTxIn[nInputs];
107
108             for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++)
109             {
110                 // Fill inputs array
111                 vin[nCurrentInput] = new CTxIn();
112                 
113                 vin[nCurrentInput].prevout = new COutPoint(wBytes.Get(36));
114
115                 int nScriptSigLen = (int)wBytes.GetVarInt();
116                 vin[nCurrentInput].scriptSig = new CScript(wBytes.Get(nScriptSigLen));
117
118                 vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
119             }
120
121             int nOutputs = (int)wBytes.GetVarInt();
122             vout = new CTxOut[nOutputs];
123
124             for (int nCurrentOutput = 0; nCurrentOutput < nOutputs; nCurrentOutput++)
125             {
126                 // Fill outputs array
127                 vout[nCurrentOutput] = new CTxOut();
128                 vout[nCurrentOutput].nValue = BitConverter.ToInt64(wBytes.Get(8), 0);
129
130                 int nScriptPKLen = (int)wBytes.GetVarInt();
131                 vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
132             }
133
134             nLockTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
135         }
136
137         /// <summary>
138         /// Serialized size
139         /// </summary>
140         public int Size
141         {
142             get
143             {
144                 int nSize = 12; // nVersion, nTime, nLockLime
145
146                 nSize += VarInt.GetEncodedSize(vin.Length);
147                 nSize += VarInt.GetEncodedSize(vout.Length);
148
149                 foreach (var input in vin)
150                 {
151                     nSize += input.Size;
152                 }
153
154                 foreach (var output in vout)
155                 {
156                     nSize += output.Size;
157                 }
158
159                 return nSize;
160             }
161         }
162
163         /// <summary>
164         /// Read transactions array which is encoded in the block body.
165         /// </summary>
166         /// <param name="wTxBytes">Bytes sequence</param>
167         /// <returns>Transactions array</returns>
168         public static CTransaction[] ReadTransactionsList(ref ByteQueue wTxBytes)
169         {
170             // Read amount of transactions
171             int nTransactions = (int)wTxBytes.GetVarInt();
172             var tx = new CTransaction[nTransactions];
173
174             for (int nTx = 0; nTx < nTransactions; nTx++)
175             {
176                 // Fill the transactions array
177                 tx[nTx] = new CTransaction();
178
179                 tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
180                 tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
181
182                 // Inputs array
183                 tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
184
185                 // outputs array
186                 tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
187
188                 tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
189             }
190
191             return tx;
192         }
193
194         public bool IsCoinBase
195         {
196             get { return (vin.Length == 1 && vin[0].prevout.IsNull && vout.Length >= 1); }
197         }
198
199         public bool IsCoinStake
200         {
201             get
202             {
203                 return (vin.Length > 0 && (!vin[0].prevout.IsNull) && vout.Length >= 2 && vout[0].IsEmpty);
204             }
205         }
206
207         /// <summary>
208         /// Transaction hash
209         /// </summary>
210         public Hash256 Hash
211         {
212             get { return Hash256.Compute256(this); }
213         }
214
215         /// <summary>
216         /// A sequence of bytes, which corresponds to the current state of CTransaction.
217         /// </summary>
218         public static implicit operator byte[] (CTransaction tx)
219         {
220             var resultBytes = new List<byte>();
221
222             resultBytes.AddRange(BitConverter.GetBytes(tx.nVersion));
223             resultBytes.AddRange(BitConverter.GetBytes(tx.nTime));
224             resultBytes.AddRange(VarInt.EncodeVarInt(tx.vin.LongLength));
225
226             foreach (var input in tx.vin)
227             {
228                 resultBytes.AddRange((byte[])input);
229             }
230
231             resultBytes.AddRange(VarInt.EncodeVarInt(tx.vout.LongLength));
232
233             foreach (var output in tx.vout)
234             {
235                 resultBytes.AddRange((byte[])output);
236             }
237
238             resultBytes.AddRange(BitConverter.GetBytes(tx.nLockTime));
239
240             return resultBytes.ToArray();
241         }
242
243         public override string ToString()
244         {
245             var sb = new StringBuilder();
246
247             sb.AppendFormat("CTransaction(\n nVersion={0},\n nTime={1},\n", nVersion, nTime);
248
249             foreach (var txin in vin)
250             {
251                 sb.AppendFormat(" {0},\n", txin.ToString());
252             }
253
254             foreach (var txout in vout)
255             {
256                 sb.AppendFormat(" {0},\n", txout.ToString());
257             }
258
259             sb.AppendFormat("\nnLockTime={0}\n)", nLockTime);
260
261             return sb.ToString();
262         }
263         }
264 }