From 161220f258cf9eb640d354951a48b45598301b1a Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Mon, 31 Aug 2015 23:52:23 +0300 Subject: [PATCH] ReadTxInList, ReadTxOutList and ReadTxList are internal now. --- Novacoin/CBlock.cs | 17 +++++++++++++---- Novacoin/CTransaction.cs | 14 +++++++------- Novacoin/CTxIn.cs | 12 ++++++------ Novacoin/CTxOut.cs | 12 ++++++------ 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/Novacoin/CBlock.cs b/Novacoin/CBlock.cs index 79d07d4..101d20e 100644 --- a/Novacoin/CBlock.cs +++ b/Novacoin/CBlock.cs @@ -88,16 +88,25 @@ namespace Novacoin { 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) { diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs index d1cc3dd..e67ed45 100644 --- a/Novacoin/CTransaction.cs +++ b/Novacoin/CTransaction.cs @@ -342,12 +342,12 @@ namespace Novacoin /// /// Bytes sequence /// Transactions array - 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++) @@ -355,16 +355,16 @@ namespace Novacoin // 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; diff --git a/Novacoin/CTxIn.cs b/Novacoin/CTxIn.cs index 067ad46..376f27f 100644 --- a/Novacoin/CTxIn.cs +++ b/Novacoin/CTxIn.cs @@ -84,23 +84,23 @@ namespace Novacoin /// /// Read vin list from byte sequence. /// - /// Reference to byte sequence + /// Reference to binary reader /// Inputs array - 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 diff --git a/Novacoin/CTxOut.cs b/Novacoin/CTxOut.cs index eb24845..43c1f63 100644 --- a/Novacoin/CTxOut.cs +++ b/Novacoin/CTxOut.cs @@ -59,21 +59,21 @@ namespace Novacoin /// /// Read vout list from byte sequence. /// - /// Reference to byte sequence + /// Reference to binary reader /// Outputs array - 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; -- 1.7.1