/// <param name="blockBytes"></param>
public CBlock (IList<byte> blockBytes)
{
- WrappedList<byte> wBytes = new WrappedList<byte>(blockBytes);
+ ByteQueue wBytes = new ByteQueue(blockBytes);
// Fill the block header fields
- header = new CBlockHeader(wBytes.GetItems(80));
+ header = new CBlockHeader(wBytes.Get(80));
// Parse transactions list
vtx = CTransaction.ReadTransactionsList(ref wBytes);
// Read block signature
- signature = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
+ signature = wBytes.Get((int)VarInt.ReadVarInt(ref wBytes));
}
public CBlock()
}
/// <summary>
- /// Return a new instance of WrappedList object for current code bytes
+ /// Return a new instance of ByteQueue object for current code bytes
/// </summary>
/// <returns></returns>
- public WrappedList<byte> GetWrappedList()
+ public ByteQueue GetWrappedList()
{
- return new WrappedList<byte>(codeBytes);
+ return new ByteQueue(codeBytes);
}
/// <summary>
{
get
{
- WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
+ ByteQueue wCodeBytes = new ByteQueue(codeBytes);
opcodetype opcode; // Current opcode
IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
{
get
{
- WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
+ ByteQueue wCodeBytes = new ByteQueue(codeBytes);
opcodetype opcode; // Current opcode
IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
/// <returns>Amount of sigops</returns>
public int GetSigOpCount(bool fAccurate)
{
- WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
+ ByteQueue wCodeBytes = new ByteQueue(codeBytes);
opcodetype opcode; // Current opcode
IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
// This is a pay-to-script-hash scriptPubKey;
// get the last item that the scriptSig
// pushes onto the stack:
- WrappedList<byte> wScriptSig = scriptSig.GetWrappedList();
+ ByteQueue wScriptSig = scriptSig.GetWrappedList();
opcodetype opcode; // Current opcode
IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
public override string ToString()
{
StringBuilder sb = new StringBuilder();
- WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
+ ByteQueue wCodeBytes = new ByteQueue(codeBytes);
opcodetype opcode; // Current opcode
IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
/// <param name="txBytes">Byte sequence</param>
public CTransaction(IList<byte> txBytes)
{
- WrappedList<byte> wBytes = new WrappedList<byte>(txBytes);
+ ByteQueue wBytes = new ByteQueue(txBytes);
- nVersion = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
- nTime = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+ nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
+ nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
int nInputs = (int)VarInt.ReadVarInt(ref wBytes);
vin = new CTxIn[nInputs];
// Fill inputs array
vin[nCurrentInput] = new CTxIn();
- vin[nCurrentInput].prevout = new COutPoint(wBytes.GetItems(36));
+ vin[nCurrentInput].prevout = new COutPoint(wBytes.Get(36));
int nScriptSigLen = (int)VarInt.ReadVarInt(ref wBytes);
- vin[nCurrentInput].scriptSig = new CScript(wBytes.GetItems(nScriptSigLen));
+ vin[nCurrentInput].scriptSig = new CScript(wBytes.Get(nScriptSigLen));
- vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+ vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
}
int nOutputs = (int)VarInt.ReadVarInt(ref wBytes);
{
// Fill outputs array
vout[nCurrentOutput] = new CTxOut();
- vout[nCurrentOutput].nValue = BitConverter.ToInt64(wBytes.GetItems(8), 0);
+ vout[nCurrentOutput].nValue = BitConverter.ToInt64(wBytes.Get(8), 0);
int nScriptPKLen = (int)VarInt.ReadVarInt(ref wBytes);
- vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.GetItems(nScriptPKLen));
+ vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
}
- nLockTime = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+ nLockTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
}
/// <summary>
/// </summary>
/// <param name="wTxBytes">Bytes sequence</param>
/// <returns>Transactions array</returns>
- public static CTransaction[] ReadTransactionsList(ref WrappedList<byte> wTxBytes)
+ public static CTransaction[] ReadTransactionsList(ref ByteQueue wTxBytes)
{
CTransaction[] tx;
// Fill the transactions array
tx[nTx] = new CTransaction();
- tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.GetItems(4), 0);
- tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.GetItems(4), 0);
+ tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+ tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
// Inputs array
tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
// outputs array
tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
- tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.GetItems(4), 0);
+ tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
}
return tx;
/// </summary>
/// <param name="wBytes">Reference to byte sequence</param>
/// <returns>Inputs array</returns>
- public static CTxIn[] ReadTxInList(ref WrappedList<byte> wBytes)
+ public static CTxIn[] ReadTxInList(ref ByteQueue wBytes)
{
CTxIn[] vin;
{
// Fill inputs array
vin[nIndex] = new CTxIn();
- vin[nIndex].prevout = new COutPoint(wBytes.GetItems(36));
- vin[nIndex].scriptSig = new CScript(wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes)));
- vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+ vin[nIndex].prevout = new COutPoint(wBytes.Get(36));
+ vin[nIndex].scriptSig = new CScript(wBytes.Get((int)VarInt.ReadVarInt(ref wBytes)));
+ vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
}
// Return inputs array
/// </summary>
/// <param name="wBytes">Reference to byte sequence</param>
/// <returns>Outputs array</returns>
- public static CTxOut[] ReadTxOutList(ref WrappedList<byte> wBytes)
+ public static CTxOut[] ReadTxOutList(ref ByteQueue wBytes)
{
int nOutputs = (int)VarInt.ReadVarInt(ref wBytes);
CTxOut[] vout =new CTxOut[nOutputs];
{
// Fill outputs array
vout[nIndex] = new CTxOut();
- vout[nIndex].nValue = BitConverter.ToUInt32(wBytes.GetItems(8), 0);
+ vout[nIndex].nValue = BitConverter.ToUInt32(wBytes.Get(8), 0);
int nScriptPKLen = (int)VarInt.ReadVarInt(ref wBytes);
- vout[nIndex].scriptPubKey = new CScript(wBytes.GetItems(nScriptPKLen));
+ vout[nIndex].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
}
return vout;
/// <summary>
/// Get next opcode from passed list of bytes and extract push arguments if there are some.
/// </summary>
- /// <param name="codeBytes">WrappedList reference.</param>
+ /// <param name="codeBytes">ByteQueue reference.</param>
/// <param name="opcodeRet">Found opcode.</param>
/// <param name="bytesRet">IEnumerable out param which is used to get the push arguments.</param>
/// <returns>Result of operation</returns>
- public static bool GetOp(ref WrappedList<byte> codeBytes, out opcodetype opcodeRet, out IEnumerable<byte> bytesRet)
+ public static bool GetOp(ref ByteQueue codeBytes, out opcodetype opcodeRet, out IEnumerable<byte> bytesRet)
{
bytesRet = new List<byte>();
opcodeRet = opcodetype.OP_INVALIDOPCODE;
try
{
// Read instruction
- opcode = (opcodetype)codeBytes.GetItem();
+ opcode = (opcodetype)codeBytes.Get();
}
catch (WrappedListException)
{
{
// The next byte contains the number of bytes to be pushed onto the stack,
// i.e. you have something like OP_PUSHDATA1 0x01 [0x5a]
- szBytes[3] = (byte)codeBytes.GetItem();
+ szBytes[3] = (byte)codeBytes.Get();
}
else if (opcode == opcodetype.OP_PUSHDATA2)
{
// The next two bytes contain the number of bytes to be pushed onto the stack,
// i.e. now your operation will seem like this: OP_PUSHDATA2 0x00 0x01 [0x5a]
- codeBytes.GetItems(2).CopyTo(szBytes, 2);
+ codeBytes.Get(2).CopyTo(szBytes, 2);
}
else if (opcode == opcodetype.OP_PUSHDATA4)
{
// The next four bytes contain the number of bytes to be pushed onto the stack,
// OP_PUSHDATA4 0x00 0x00 0x00 0x01 [0x5a]
- szBytes = codeBytes.GetItems(4);
+ szBytes = codeBytes.Get(4);
}
}
catch (WrappedListException)
try
{
// Read found number of bytes into list of OP_PUSHDATAn arguments.
- bytesRet = codeBytes.GetEnumerableItems(nSize);
+ bytesRet = codeBytes.GetEnumerable(nSize);
}
catch (WrappedListException)
{
opcodetype opcode1, opcode2;
// Compare
- WrappedList<byte> wl1 = script1.GetWrappedList();
- WrappedList<byte> wl2 = script2.GetWrappedList();
+ ByteQueue wl1 = script1.GetWrappedList();
+ ByteQueue wl2 = script2.GetWrappedList();
IEnumerable<byte> args1, args2;
while (true)
{
- if (wl1.GetCurrentItem() == last1 && wl2.GetCurrentItem() == last2)
+ if (wl1.GetCurrent() == last1 && wl2.GetCurrent() == last2)
{
// Found a match
typeRet = templateTuple.Item1;
/// </summary>
/// <param name="wBytes"></param>
/// <returns></returns>
- public static ulong ReadVarInt(ref WrappedList<byte> wBytes)
+ public static ulong ReadVarInt(ref ByteQueue wBytes)
{
- byte prefix = wBytes.GetItem();
+ byte prefix = wBytes.Get();
switch (prefix)
{
case 0xfd: // ushort
- return BitConverter.ToUInt16(wBytes.GetItems(2), 0);
+ return BitConverter.ToUInt16(wBytes.Get(2), 0);
case 0xfe: // uint
- return BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+ return BitConverter.ToUInt32(wBytes.Get(4), 0);
case 0xff: // ulong
- return BitConverter.ToUInt64(wBytes.GetItems(8), 0);
+ return BitConverter.ToUInt64(wBytes.Get(8), 0);
default:
return prefix;
}
}
}
- public class WrappedList<T>
+ public class ByteQueue
{
private int Index;
- private List<T> Elements;
+ private List<byte> Elements;
- public WrappedList(IList<T> List, int Start)
+ public ByteQueue(IList<byte> List, int Start)
{
- Elements = new List<T>(List);
+ Elements = new List<byte>(List);
Index = Start;
}
- public WrappedList(IList<T> List)
+ public ByteQueue(IList<byte> List)
{
- Elements = new List<T>(List);
+ Elements = new List<byte>(List);
Index = 0;
}
- public T GetItem()
+ public byte Get()
{
if (Elements.Count <= Index)
{
return Elements[Index++];
}
- public T GetCurrentItem()
+ public byte GetCurrent()
{
return Elements[Index];
}
- public T[] GetItems(int Count)
+ public byte[] Get(int Count)
{
if (Elements.Count - Index < Count)
{
throw new WrappedListException("Unable to read requested amount of data.");
}
- T[] result = Elements.Skip(Index).Take(Count).ToArray();
+ byte[] result = Elements.Skip(Index).Take(Count).ToArray();
Index += Count;
return result;
}
- public T[] GetCurrentItems(int Count)
+ public byte[] GetCurrent(int Count)
{
if (Elements.Count - Index < Count)
{
throw new WrappedListException("Unable to read requested amount of data.");
}
- T[] result = Elements.Skip(Index).Take(Count).ToArray();
+ byte[] result = Elements.Skip(Index).Take(Count).ToArray();
return result;
}
- public IEnumerable<T> GetEnumerableItems(int Count)
+ public IEnumerable<byte> GetEnumerable(int Count)
{
if (Elements.Count - Index < Count)
{
throw new WrappedListException("Unable to read requested amount of data.");
}
- IEnumerable<T> result = Elements.Skip(Index).Take(Count);
+ IEnumerable<byte> result = Elements.Skip(Index).Take(Count);
Index += Count;
return result;