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;
28 /// Parse byte sequence and initialize new block instance
30 /// <param name="blockBytes"></param>
31 public CBlock (IList<byte> blockBytes)
33 header = new CBlockHeader();
35 WrappedList<byte> wBytes = new WrappedList<byte>(blockBytes);
37 // Fill the block header fields
38 header.nVersion = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
39 header.prevHash = new Hash256(wBytes.GetItems(32));
40 header.merkleRoot = new Hash256(wBytes.GetItems(32));
41 header.nTime = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
42 header.nBits = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
43 header.nNonce = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
45 // Parse transactions list
46 vtx = CTransaction.ReadTransactionsList(ref wBytes);
48 // Read block signature
49 signature = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
53 /// Convert current instance into sequence of bytes
55 /// <returns>Byte sequence</returns>
56 public IList<byte> Bytes
60 List<byte> r = new List<byte>();
62 r.AddRange(header.Bytes);
63 r.AddRange(VarInt.EncodeVarInt(vtx.LongLength)); // transactions count
65 foreach (CTransaction tx in vtx)
70 r.AddRange(VarInt.EncodeVarInt(signature.LongLength));
71 r.AddRange(signature);
77 public override string ToString()
79 StringBuilder sb = new StringBuilder();
81 sb.AppendFormat("CBlock(\n header={0},\n", header.ToString());
83 foreach(CTransaction tx in vtx)
85 sb.AppendFormat("{0},\n", tx.ToString());
88 sb.AppendFormat("signature={0})\n", Interop.ToHex(signature));