3 using System.Collections.Generic;
8 /// Represents the block. Block consists of header, transaction array and header signature.
15 public CBlockHeader header;
18 /// Transactions array.
20 public CTransaction[] vtx;
23 /// Block header signature.
25 public byte[] signature = new byte[0];
27 public CBlock(CBlock b)
29 header = new CBlockHeader(b.header);
31 for (int i = 0; i < b.vtx.Length; i++)
33 vtx[i] = new CTransaction(b.vtx[i]);
36 b.signature.CopyTo(signature, 0);
40 /// Parse byte sequence and initialize new block instance
42 /// <param name="blockBytes"></param>
43 public CBlock (IList<byte> blockBytes)
45 ByteQueue wBytes = new ByteQueue(blockBytes);
47 // Fill the block header fields
48 header = new CBlockHeader(wBytes.Get(80));
50 // Parse transactions list
51 vtx = CTransaction.ReadTransactionsList(ref wBytes);
53 // Read block signature
54 signature = wBytes.Get((int)wBytes.GetVarInt());
59 // Initialize empty array of transactions. Please note that such
60 // configuration is not valid real block since it has to provide
61 // at least one transaction.
62 vtx = new CTransaction[0];
66 /// Is this a Proof-of-Stake block?
68 public bool IsProofOfStake
72 return (vtx.Length > 1 && vtx[1].IsCoinStake);
76 public bool SignatureOK
80 IList<IEnumerable<byte>> solutions;
85 if (signature.Length == 0)
87 return false; // No signature
90 if (!ScriptCode.Solver(vtx[1].vout[1].scriptPubKey, out whichType, out solutions))
92 return false; // No solutions found
95 if (whichType == txnouttype.TX_PUBKEY)
101 pubkey = new CPubKey(solutions[0]);
105 return false; // Error while loading public key
108 return pubkey.VerifySignature(header.Hash, signature);
113 // Proof-of-Work blocks have no signature
123 /// Convert current instance into sequence of bytes
125 /// <returns>Byte sequence</returns>
126 public IList<byte> Bytes
130 List<byte> r = new List<byte>();
132 r.AddRange(header.Bytes);
133 r.AddRange(VarInt.EncodeVarInt(vtx.LongLength)); // transactions count
135 foreach (CTransaction tx in vtx)
137 r.AddRange(tx.Bytes);
140 r.AddRange(VarInt.EncodeVarInt(signature.LongLength));
141 r.AddRange(signature);
147 public override string ToString()
149 StringBuilder sb = new StringBuilder();
151 sb.AppendFormat("CBlock(\n header={0},\n", header.ToString());
153 foreach(CTransaction tx in vtx)
155 sb.AppendFormat("{0},\n", tx.ToString());
158 sb.AppendFormat("signature={0})\n", Interop.ToHex(signature));
159 sb.AppendFormat("signatureOK={0})\n", SignatureOK);
163 return sb.ToString();