{
try
{
- ByteQueue wBytes = new ByteQueue(ref blockBytes);
+ var stream = new MemoryStream(blockBytes);
+ var reader = new BinaryReader(stream);
// Fill the block header fields
- header = new CBlockHeader(wBytes.Get(80));
+ header = new CBlockHeader();
+ header.nVersion = reader.ReadUInt32();
+ header.prevHash = new ScryptHash256(reader.ReadBytes(32));
+ header.merkleRoot = new Hash256(reader.ReadBytes(32));
+ header.nTime = reader.ReadUInt32();
+ header.nBits = reader.ReadUInt32();
+ header.nNonce = reader.ReadUInt32();
// Parse transactions list
- vtx = CTransaction.ReadTransactionsList(ref wBytes);
+ vtx = CTransaction.ReadTransactionsList(ref reader);
// Read block signature
- signature = wBytes.Get((int)wBytes.GetVarInt());
+ signature = reader.ReadBytes((int)VarInt.ReadVarInt(ref reader));
+
+ reader.Close();
}
catch (Exception e)
{
/// </summary>
/// <param name="wTxBytes">Bytes sequence</param>
/// <returns>Transactions array</returns>
- public static CTransaction[] ReadTransactionsList(ref ByteQueue wTxBytes)
+ internal static CTransaction[] ReadTransactionsList(ref BinaryReader reader)
{
try
{
// Read amount of transactions
- int nTransactions = (int)wTxBytes.GetVarInt();
+ int nTransactions = (int)VarInt.ReadVarInt(ref reader);
var tx = new CTransaction[nTransactions];
for (int nTx = 0; nTx < nTransactions; nTx++)
// Fill the transactions array
tx[nTx] = new CTransaction();
- tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
- tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+ tx[nTx].nVersion = reader.ReadUInt32();
+ tx[nTx].nTime = reader.ReadUInt32();
// Inputs array
- tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
+ tx[nTx].vin = CTxIn.ReadTxInList(ref reader);
// outputs array
- tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
+ tx[nTx].vout = CTxOut.ReadTxOutList(ref reader);
- tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+ tx[nTx].nLockTime = reader.ReadUInt32();
}
return tx;
/// <summary>
/// Read vin list from byte sequence.
/// </summary>
- /// <param name="wBytes">Reference to byte sequence</param>
+ /// <param name="wBytes">Reference to binary reader</param>
/// <returns>Inputs array</returns>
- public static CTxIn[] ReadTxInList(ref ByteQueue wBytes)
+ internal static CTxIn[] ReadTxInList(ref BinaryReader reader)
{
try
{
// Get amount
- int nInputs = (int)wBytes.GetVarInt();
+ int nInputs = (int)VarInt.ReadVarInt(ref reader);
var vin = new CTxIn[nInputs];
for (int nIndex = 0; nIndex < nInputs; nIndex++)
{
// Fill inputs array
vin[nIndex] = new CTxIn();
- vin[nIndex].prevout = new COutPoint(wBytes.Get(36));
- vin[nIndex].scriptSig = new CScript(wBytes.Get((int)wBytes.GetVarInt()));
- vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
+ vin[nIndex].prevout = new COutPoint(reader.ReadBytes(36));
+ vin[nIndex].scriptSig = new CScript(reader.ReadBytes((int)VarInt.ReadVarInt(ref reader)));
+ vin[nIndex].nSequence = reader.ReadUInt32();
}
// Return inputs array
/// <summary>
/// Read vout list from byte sequence.
/// </summary>
- /// <param name="wBytes">Reference to byte sequence</param>
+ /// <param name="wBytes">Reference to binary reader</param>
/// <returns>Outputs array</returns>
- public static CTxOut[] ReadTxOutList(ref ByteQueue wBytes)
+ internal static CTxOut[] ReadTxOutList(ref BinaryReader reader)
{
- int nOutputs = (int)wBytes.GetVarInt();
+ int nOutputs = (int)VarInt.ReadVarInt(ref reader);
var vout =new CTxOut[nOutputs];
for (int nIndex = 0; nIndex < nOutputs; nIndex++)
{
// Fill outputs array
vout[nIndex] = new CTxOut();
- vout[nIndex].nValue = BitConverter.ToUInt64(wBytes.Get(8), 0);
+ vout[nIndex].nValue = reader.ReadUInt64();
- int nScriptPKLen = (int)wBytes.GetVarInt();
- vout[nIndex].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
+ int nScriptPKLen = (int)VarInt.ReadVarInt(ref reader);
+ vout[nIndex].scriptPubKey = new CScript(reader.ReadBytes(nScriptPKLen));
}
return vout;