From cc948a567459f7df008429c99671077e0f953e22 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Mon, 17 Aug 2015 12:43:09 +0300 Subject: [PATCH] Implement ReadTxInList and ReadTxoutList as static methods --- Novacoin/CBlock.cs | 2 +- Novacoin/CTransaction.cs | 33 +++++++-------------------------- Novacoin/CTxIn.cs | 23 +++++++++++++++++++++++ Novacoin/CTxOut.cs | 21 +++++++++++++++++++++ 4 files changed, 52 insertions(+), 27 deletions(-) diff --git a/Novacoin/CBlock.cs b/Novacoin/CBlock.cs index 26ac7b1..b006f0d 100644 --- a/Novacoin/CBlock.cs +++ b/Novacoin/CBlock.cs @@ -42,7 +42,7 @@ namespace Novacoin header.nNonce = Interop.LEBytesToUInt32(wBytes.GetItems(4)); // Parse transactions list - tx = CTransaction.ParseTransactionsList(ref wBytes); + tx = CTransaction.ReadTransactionsList(ref wBytes); // Read block signature signature = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes)); diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs index 292a3f4..8146685 100644 --- a/Novacoin/CTransaction.cs +++ b/Novacoin/CTransaction.cs @@ -81,11 +81,11 @@ namespace Novacoin } /// - /// Parse transactions array which is encoded in the block body. + /// Read transactions array which is encoded in the block body. /// /// Bytes sequence /// Transactions array - public static CTransaction[] ParseTransactionsList(ref WrappedList wTxBytes) + public static CTransaction[] ReadTransactionsList(ref WrappedList wTxBytes) { CTransaction[] tx; @@ -101,30 +101,11 @@ namespace Novacoin tx[nTx].nVersion = Interop.LEBytesToUInt32(wTxBytes.GetItems(4)); tx[nTx].nTime = Interop.LEBytesToUInt32(wTxBytes.GetItems(4)); - int nInputs = (int)VarInt.ReadVarInt(ref wTxBytes); - tx[nTx].inputs = new CTxIn[nInputs]; - - for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++) - { - // Fill inputs array - tx[nTx].inputs[nCurrentInput] = new CTxIn(); - - tx[nTx].inputs[nCurrentInput].txID = new Hash256(wTxBytes.GetItems(32)); - tx[nTx].inputs[nCurrentInput].n = Interop.LEBytesToUInt32(wTxBytes.GetItems(4)); - tx[nTx].inputs[nCurrentInput].scriptSig = wTxBytes.GetItems((int)VarInt.ReadVarInt(ref wTxBytes)); - tx[nTx].inputs[nCurrentInput].nSequence = Interop.LEBytesToUInt32(wTxBytes.GetItems(4)); - } - - int nOutputs = (int)VarInt.ReadVarInt(ref wTxBytes); - tx[nTx].outputs = new CTxOut[nOutputs]; - - for (int nCurrentOutput = 0; nCurrentOutput < nOutputs; nCurrentOutput++) - { - // Fill outputs array - tx[nTx].outputs[nCurrentOutput] = new CTxOut(); - tx[nTx].outputs[nCurrentOutput].nValue = Interop.LEBytesToUInt64(wTxBytes.GetItems(8)); - tx[nTx].outputs[nCurrentOutput].scriptPubKey = wTxBytes.GetItems((int)VarInt.ReadVarInt(ref wTxBytes)); - } + // Inputs array + tx[nTx].inputs = CTxIn.ReadTxInList(ref wTxBytes); + + // outputs array + tx[nTx].outputs = CTxOut.ReadTxOutList(ref wTxBytes); tx[nTx].nLockTime = Interop.LEBytesToUInt32(wTxBytes.GetItems(4)); } diff --git a/Novacoin/CTxIn.cs b/Novacoin/CTxIn.cs index b840f1f..52c355b 100644 --- a/Novacoin/CTxIn.cs +++ b/Novacoin/CTxIn.cs @@ -48,6 +48,29 @@ namespace Novacoin { } + public static CTxIn[] ReadTxInList(ref WrappedList wBytes) + { + CTxIn[] vin; + + // Get amount + int nInputs = (int)VarInt.ReadVarInt(ref wBytes); + vin = new CTxIn[nInputs]; + + for (int nIndex = 0; nIndex < nInputs; nIndex++) + { + // Fill inputs array + vin[nIndex] = new CTxIn(); + + vin[nIndex].txID = new Hash256(wBytes.GetItems(32)); + vin[nIndex].n = Interop.LEBytesToUInt32(wBytes.GetItems(4)); + vin[nIndex].scriptSig = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes)); + vin[nIndex].nSequence = Interop.LEBytesToUInt32(wBytes.GetItems(4)); + } + + // Return inputs array + return vin; + } + /// /// Get raw bytes representation of our input. /// diff --git a/Novacoin/CTxOut.cs b/Novacoin/CTxOut.cs index 0bb013c..eac9409 100644 --- a/Novacoin/CTxOut.cs +++ b/Novacoin/CTxOut.cs @@ -37,6 +37,27 @@ namespace Novacoin } /// + /// Read vout list from byte sequence. + /// + /// Reference to byte sequence + /// Outputs array + public static CTxOut[] ReadTxOutList(ref WrappedList wBytes) + { + int nOutputs = (int)VarInt.ReadVarInt(ref wBytes); + CTxOut[] vout =new CTxOut[nOutputs]; + + for (int nIndex = 0; nIndex < nOutputs; nIndex++) + { + // Fill outputs array + vout[nIndex] = new CTxOut(); + vout[nIndex].nValue = Interop.LEBytesToUInt64(wBytes.GetItems(8)); + vout[nIndex].scriptPubKey = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes)); + } + + return vout; + } + + /// /// Get raw bytes representation of our output. /// /// Byte sequence. -- 1.7.1