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;
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 /// Convert current instance into sequence of bytes
68 /// <returns>Byte sequence</returns>
69 public IList<byte> Bytes
73 List<byte> r = new List<byte>();
75 r.AddRange(header.Bytes);
76 r.AddRange(VarInt.EncodeVarInt(vtx.LongLength)); // transactions count
78 foreach (CTransaction tx in vtx)
83 r.AddRange(VarInt.EncodeVarInt(signature.LongLength));
84 r.AddRange(signature);
90 public override string ToString()
92 StringBuilder sb = new StringBuilder();
94 sb.AppendFormat("CBlock(\n header={0},\n", header.ToString());
96 foreach(CTransaction tx in vtx)
98 sb.AppendFormat("{0},\n", tx.ToString());
101 sb.AppendFormat("signature={0})\n", Interop.ToHex(signature));
104 return sb.ToString();