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 header = new CBlockHeader();
47 WrappedList<byte> wBytes = new WrappedList<byte>(blockBytes);
49 // Fill the block header fields
50 header.nVersion = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
51 header.prevHash = new Hash256(wBytes.GetItems(32));
52 header.merkleRoot = new Hash256(wBytes.GetItems(32));
53 header.nTime = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
54 header.nBits = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
55 header.nNonce = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
57 // Parse transactions list
58 vtx = CTransaction.ReadTransactionsList(ref wBytes);
60 // Read block signature
61 signature = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
66 // Initialize empty array of transactions. Please note that such
67 // configuration is not valid real block since it has to provide
68 // at least one transaction.
69 vtx = new CTransaction[0];
73 /// Convert current instance into sequence of bytes
75 /// <returns>Byte sequence</returns>
76 public IList<byte> Bytes
80 List<byte> r = new List<byte>();
82 r.AddRange(header.Bytes);
83 r.AddRange(VarInt.EncodeVarInt(vtx.LongLength)); // transactions count
85 foreach (CTransaction tx in vtx)
90 r.AddRange(VarInt.EncodeVarInt(signature.LongLength));
91 r.AddRange(signature);
97 public override string ToString()
99 StringBuilder sb = new StringBuilder();
101 sb.AppendFormat("CBlock(\n header={0},\n", header.ToString());
103 foreach(CTransaction tx in vtx)
105 sb.AppendFormat("{0},\n", tx.ToString());
108 sb.AppendFormat("signature={0})\n", Interop.ToHex(signature));
111 return sb.ToString();