2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
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.
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.
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/>.
21 using System.Collections.Generic;
26 /// Represents the block. Block consists of header, transaction array and header signature.
33 public CBlockHeader header;
36 /// Transactions array.
38 public CTransaction[] vtx;
41 /// Block header signature.
43 public byte[] signature = new byte[0];
45 public CBlock(CBlock b)
47 header = new CBlockHeader(b.header);
49 for (int i = 0; i < b.vtx.Length; i++)
51 vtx[i] = new CTransaction(b.vtx[i]);
54 b.signature.CopyTo(signature, 0);
58 /// Parse byte sequence and initialize new block instance
60 /// <param name="blockBytes"></param>
61 public CBlock (IList<byte> blockBytes)
63 ByteQueue wBytes = new ByteQueue(blockBytes);
65 // Fill the block header fields
66 header = new CBlockHeader(wBytes.Get(80));
68 // Parse transactions list
69 vtx = CTransaction.ReadTransactionsList(ref wBytes);
71 // Read block signature
72 signature = wBytes.Get((int)wBytes.GetVarInt());
77 // Initialize empty array of transactions. Please note that such
78 // configuration is not valid real block since it has to provide
79 // at least one transaction.
80 vtx = new CTransaction[0];
84 /// Is this a Proof-of-Stake block?
86 public bool IsProofOfStake
90 return (vtx.Length > 1 && vtx[1].IsCoinStake);
95 /// Was this signed correctly?
97 public bool SignatureOK
103 if (signature.Length == 0)
105 return false; // No signature
108 txnouttype whichType;
109 IList<byte[]> solutions;
111 if (!ScriptCode.Solver(vtx[1].vout[1].scriptPubKey, out whichType, out solutions))
113 return false; // No solutions found
116 if (whichType == txnouttype.TX_PUBKEY)
122 pubkey = new CPubKey(solutions[0]);
126 return false; // Error while loading public key
129 return pubkey.VerifySignature(header.Hash, signature);
134 // Proof-of-Work blocks have no signature
144 /// Get current instance as sequence of bytes
146 /// <returns>Byte sequence</returns>
147 public IList<byte> Bytes
151 var r = new List<byte>();
153 r.AddRange(header.Bytes);
154 r.AddRange(VarInt.EncodeVarInt(vtx.LongLength)); // transactions count
156 foreach (var tx in vtx)
158 r.AddRange(tx.Bytes);
161 r.AddRange(VarInt.EncodeVarInt(signature.LongLength));
162 r.AddRange(signature);
168 public override string ToString()
170 var sb = new StringBuilder();
172 sb.AppendFormat("CBlock(\n header={0},\n", header.ToString());
174 foreach(var tx in vtx)
176 sb.AppendFormat("{0}", tx.ToString());
181 sb.AppendFormat(", signature={0}, signatureOK={1}\n", Interop.ToHex(signature), SignatureOK);
186 return sb.ToString();