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);
50 vtx = new CTransaction[b.vtx.Length];
52 for (int i = 0; i < b.vtx.Length; i++)
54 vtx[i] = new CTransaction(b.vtx[i]);
57 b.signature.CopyTo(signature, 0);
61 /// Parse byte sequence and initialize new block instance
63 /// <param name="blockBytes"></param>
64 public CBlock (byte[] blockBytes)
66 ByteQueue wBytes = new ByteQueue(blockBytes);
68 // Fill the block header fields
69 header = new CBlockHeader(wBytes.Get(80));
71 // Parse transactions list
72 vtx = CTransaction.ReadTransactionsList(ref wBytes);
74 // Read block signature
75 signature = wBytes.Get((int)wBytes.GetVarInt());
80 // Initialize empty array of transactions. Please note that such
81 // configuration is not valid real block since it has to provide
82 // at least one transaction.
83 vtx = new CTransaction[0];
87 /// Is this a Proof-of-Stake block?
89 public bool IsProofOfStake
93 return (vtx.Length > 1 && vtx[1].IsCoinStake);
98 /// Was this signed correctly?
100 public bool SignatureOK
106 if (signature.Length == 0)
108 return false; // No signature
111 txnouttype whichType;
112 IList<byte[]> solutions;
114 if (!ScriptCode.Solver(vtx[1].vout[1].scriptPubKey, out whichType, out solutions))
116 return false; // No solutions found
119 if (whichType == txnouttype.TX_PUBKEY)
125 pubkey = new CPubKey(solutions[0]);
129 return false; // Error while loading public key
132 return pubkey.VerifySignature(header.Hash, signature);
137 // Proof-of-Work blocks have no signature
147 /// Get instance as sequence of bytes
149 /// <returns>Byte sequence</returns>
150 public static implicit operator byte[] (CBlock b)
152 var r = new List<byte>();
154 r.AddRange((byte[])b.header);
155 r.AddRange(VarInt.EncodeVarInt(b.vtx.LongLength)); // transactions count
157 foreach (var tx in b.vtx)
159 r.AddRange((byte[])tx);
162 r.AddRange(VarInt.EncodeVarInt(b.signature.LongLength));
163 r.AddRange(b.signature);
171 public Hash256 hashMerkleRoot
175 var merkleTree = new List<byte>();
177 foreach (var tx in vtx)
179 merkleTree.AddRange(Hash256.ComputeRaw256(tx));
183 for (int nLevelSize = vtx.Length; nLevelSize > 1; nLevelSize = (nLevelSize + 1) / 2)
185 for (int nLeft = 0; nLeft < nLevelSize; nLeft += 2)
187 int nRight = Math.Min(nLeft + 1, nLevelSize - 1);
189 var left = merkleTree.GetRange((levelOffset + nLeft) * 32, 32).ToArray();
190 var right = merkleTree.GetRange((levelOffset + nRight) * 32, 32).ToArray();
192 merkleTree.AddRange(Hash256.ComputeRaw256(ref left, ref right));
194 levelOffset += nLevelSize;
197 return (merkleTree.Count == 0) ? new Hash256() : new Hash256(merkleTree.GetRange(merkleTree.Count-32, 32).ToArray());
201 public override string ToString()
203 var sb = new StringBuilder();
205 sb.AppendFormat("CBlock(\n header={0},\n", header.ToString());
207 foreach(var tx in vtx)
209 sb.AppendFormat("{0}", tx.ToString());
214 sb.AppendFormat(", signature={0}, signatureOK={1}\n", Interop.ToHex(signature), SignatureOK);
219 return sb.ToString();