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);
77 /// Was this signed correctly?
79 public bool SignatureOK
85 if (signature.Length == 0)
87 return false; // No signature
91 IList<IEnumerable<byte>> solutions;
93 if (!ScriptCode.Solver(vtx[1].vout[1].scriptPubKey, out whichType, out solutions))
95 return false; // No solutions found
98 if (whichType == txnouttype.TX_PUBKEY)
104 pubkey = new CPubKey(solutions[0]);
108 return false; // Error while loading public key
111 return pubkey.VerifySignature(header.Hash, signature);
116 // Proof-of-Work blocks have no signature
126 /// Get current instance as sequence of bytes
128 /// <returns>Byte sequence</returns>
129 public IList<byte> Bytes
133 List<byte> r = new List<byte>();
135 r.AddRange(header.Bytes);
136 r.AddRange(VarInt.EncodeVarInt(vtx.LongLength)); // transactions count
138 foreach (CTransaction tx in vtx)
140 r.AddRange(tx.Bytes);
143 r.AddRange(VarInt.EncodeVarInt(signature.LongLength));
144 r.AddRange(signature);
150 public override string ToString()
152 StringBuilder sb = new StringBuilder();
154 sb.AppendFormat("CBlock(\n header={0},\n", header.ToString());
156 foreach(CTransaction tx in vtx)
158 sb.AppendFormat("{0}", tx.ToString());
163 sb.AppendFormat(", signature={0}, signatureOK={1}\n", Interop.ToHex(signature), SignatureOK);
169 return sb.ToString();