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/>.
22 using System.Collections.Generic;
23 using System.Security.Cryptography;
28 /// Represents the block. Block consists of header, transaction array and header signature.
35 public CBlockHeader header;
38 /// Transactions array.
40 public CTransaction[] vtx;
43 /// Block header signature.
45 public byte[] signature = new byte[0];
47 public CBlock(CBlock b)
49 header = new CBlockHeader(b.header);
51 for (int i = 0; i < b.vtx.Length; i++)
53 vtx[i] = new CTransaction(b.vtx[i]);
56 b.signature.CopyTo(signature, 0);
60 /// Parse byte sequence and initialize new block instance
62 /// <param name="blockBytes"></param>
63 public CBlock (IList<byte> blockBytes)
65 ByteQueue wBytes = new ByteQueue(blockBytes);
67 // Fill the block header fields
68 header = new CBlockHeader(wBytes.Get(80));
70 // Parse transactions list
71 vtx = CTransaction.ReadTransactionsList(ref wBytes);
73 // Read block signature
74 signature = wBytes.Get((int)wBytes.GetVarInt());
79 // Initialize empty array of transactions. Please note that such
80 // configuration is not valid real block since it has to provide
81 // at least one transaction.
82 vtx = new CTransaction[0];
86 /// Is this a Proof-of-Stake block?
88 public bool IsProofOfStake
92 return (vtx.Length > 1 && vtx[1].IsCoinStake);
97 /// Was this signed correctly?
99 public bool SignatureOK
105 if (signature.Length == 0)
107 return false; // No signature
110 txnouttype whichType;
111 IList<byte[]> solutions;
113 if (!ScriptCode.Solver(vtx[1].vout[1].scriptPubKey, out whichType, out solutions))
115 return false; // No solutions found
118 if (whichType == txnouttype.TX_PUBKEY)
124 pubkey = new CPubKey(solutions[0]);
128 return false; // Error while loading public key
131 return pubkey.VerifySignature(header.Hash, signature);
136 // Proof-of-Work blocks have no signature
146 /// Get current instance as sequence of bytes
148 /// <returns>Byte sequence</returns>
149 public IList<byte> Bytes
153 var r = new List<byte>();
155 r.AddRange(header.Bytes);
156 r.AddRange(VarInt.EncodeVarInt(vtx.LongLength)); // transactions count
158 foreach (var tx in vtx)
160 r.AddRange(tx.Bytes);
163 r.AddRange(VarInt.EncodeVarInt(signature.LongLength));
164 r.AddRange(signature);
173 public Hash256 hashMerkleRoot
177 var merkleTree = new List<byte>();
179 foreach (var tx in vtx)
181 merkleTree.AddRange(tx.Hash.hashBytes);
184 var hasher = new SHA256Managed();
188 for (int nSize = vtx.Length; nSize > 1; nSize = (nSize + 1) / 2)
190 for (int i = 0; i < nSize; i += 2)
192 int i2 = Math.Min(i + 1, nSize - 1);
194 var pair = new List<byte>();
196 pair.AddRange(merkleTree.GetRange((j + i)*32, 32));
197 pair.AddRange(merkleTree.GetRange((j + i2)*32, 32));
199 var digest1 = hasher.ComputeHash(pair.ToArray());
200 var digest2 = hasher.ComputeHash(digest1);
202 merkleTree.AddRange(digest2);
207 return (merkleTree.Count == 0) ? new Hash256() : new Hash256(merkleTree.GetRange(merkleTree.Count-32, 32));
211 public override string ToString()
213 var sb = new StringBuilder();
215 sb.AppendFormat("CBlock(\n header={0},\n", header.ToString());
217 foreach(var tx in vtx)
219 sb.AppendFormat("{0}", tx.ToString());
224 sb.AppendFormat(", signature={0}, signatureOK={1}\n", Interop.ToHex(signature), SignatureOK);
229 return sb.ToString();