X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCTransaction.cs;h=d1de2656782f257c466473880cc50a0be1b9dd3e;hb=6c6e3b0c68e764ea520dde48d5e44c757f1dabbb;hp=d8572eb17eab124d236813eff5c807fd44a74124;hpb=0279648337efe9bbe32b5204e247529243805484;p=NovacoinLibrary.git
diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs
index d8572eb..d1de265 100644
--- a/Novacoin/CTransaction.cs
+++ b/Novacoin/CTransaction.cs
@@ -22,20 +22,47 @@ using System.Collections.Generic;
namespace Novacoin
{
+ [Serializable]
+ public class TransactionConstructorException : Exception
+ {
+ public TransactionConstructorException()
+ {
+ }
+
+ public TransactionConstructorException(string message)
+ : base(message)
+ {
+ }
+
+ public TransactionConstructorException(string message, Exception inner)
+ : base(message, inner)
+ {
+ }
+ }
+
///
/// Represents the transaction. Any transaction must provide one input and one output at least.
///
public class CTransaction
{
///
+ /// One coin = 1000000 satoshis.
+ ///
+ public const ulong nCoin = 1000000;
+ ///
+ /// Sanity checking threshold.
+ ///
+ public const ulong nMaxMoney = 2000000000 * nCoin;
+
+ ///
/// Version of transaction schema.
///
- public uint nVersion = 1;
+ public uint nVersion;
///
/// Transaction timestamp.
///
- public uint nTime = 0;
+ public uint nTime;
///
/// Array of transaction inputs
@@ -50,7 +77,7 @@ namespace Novacoin
///
/// Block height or timestamp when transaction is final
///
- public uint nLockTime = 0;
+ public uint nLockTime;
///
/// Initialize an empty instance
@@ -60,8 +87,11 @@ namespace Novacoin
// Initialize empty input and output arrays. Please note that such
// configuration is not valid for real transaction, you have to supply
// at least one input and one output.
+ nVersion = 1;
+ nTime = 0;
vin = new CTxIn[0];
vout = new CTxOut[0];
+ nLockTime = 0;
}
///
@@ -90,48 +120,220 @@ namespace Novacoin
nLockTime = tx.nLockTime;
}
-
///
- /// Parse byte sequence and initialize new instance of CTransaction
+ /// Attempts to execute all transaction scripts and validate the results.
///
- /// Byte sequence
- public CTransaction(byte[] txBytes)
+ /// Checking result.
+ public bool VerifyScripts()
{
- var wBytes = new ByteQueue(txBytes);
+ if (IsCoinBase)
+ {
+ return true;
+ }
+
+ CTransaction txPrev = null;
+ for (int i = 0; i < vin.Length; i++)
+ {
+ var outpoint = vin[i].prevout;
- nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
- nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
+ if (!CBlockStore.Instance.GetTransaction(outpoint.hash, ref txPrev))
+ return false;
- int nInputs = (int)(int)wBytes.GetVarInt();
- vin = new CTxIn[nInputs];
+ if (!ScriptCode.VerifyScript(vin[i].scriptSig, txPrev.vout[outpoint.n].scriptPubKey, this, i, (int)scriptflag.SCRIPT_VERIFY_P2SH, 0))
+ return false;
+ }
- for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++)
+ return true;
+ }
+
+ ///
+ /// Calculate amount of signature operations without trying to properly evaluate P2SH scripts.
+ ///
+ public uint LegacySigOpCount
+ {
+ get
{
- // Fill inputs array
- vin[nCurrentInput] = new CTxIn();
-
- vin[nCurrentInput].prevout = new COutPoint(wBytes.Get(36));
+ uint nSigOps = 0;
+ foreach (var txin in vin)
+ {
+ nSigOps += txin.scriptSig.GetSigOpCount(false);
+ }
+ foreach (var txout in vout)
+ {
+ nSigOps += txout.scriptPubKey.GetSigOpCount(false);
+ }
+
+ return nSigOps;
+ }
+ }
- int nScriptSigLen = (int)wBytes.GetVarInt();
- vin[nCurrentInput].scriptSig = new CScript(wBytes.Get(nScriptSigLen));
+ ///
+ /// Basic sanity checkings
+ ///
+ /// Checking result
+ public bool CheckTransaction()
+ {
+ if (Size > 250000 || vin.Length == 0 || vout.Length == 0)
+ {
+ return false;
+ }
- vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
+ // Check for empty or overflow output values
+ ulong nValueOut = 0;
+ for (int i = 0; i < vout.Length; i++)
+ {
+ CTxOut txout = vout[i];
+ if (txout.IsEmpty && !IsCoinBase && !IsCoinStake)
+ {
+ // Empty outputs aren't allowed for user transactions.
+ return false;
+ }
+
+ nValueOut += txout.nValue;
+ if (!MoneyRange(nValueOut))
+ {
+ return false;
+ }
}
- int nOutputs = (int)wBytes.GetVarInt();
- vout = new CTxOut[nOutputs];
+ // Check for duplicate inputs
+ var InOutPoints = new List();
+ foreach (var txin in vin)
+ {
+ if (InOutPoints.IndexOf(txin.prevout) != -1)
+ {
+ // Duplicate input.
+ return false;
+ }
+ InOutPoints.Add(txin.prevout);
+ }
- for (int nCurrentOutput = 0; nCurrentOutput < nOutputs; nCurrentOutput++)
+ if (IsCoinBase)
+ {
+ if (vin[0].scriptSig.Size < 2 || vin[0].scriptSig.Size > 100)
+ {
+ // Script size is invalid
+ return false;
+ }
+ }
+ else
{
- // Fill outputs array
- vout[nCurrentOutput] = new CTxOut();
- vout[nCurrentOutput].nValue = BitConverter.ToInt64(wBytes.Get(8), 0);
+ foreach (var txin in vin)
+ {
+ if (txin.prevout.IsNull)
+ {
+ // Null input in non-coinbase transaction.
+ return false;
+ }
+ }
+ }
+
+ return true;
+ }
- int nScriptPKLen = (int)wBytes.GetVarInt();
- vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
+ public bool IsFinal(uint nBlockHeight = 0, uint nBlockTime = 0)
+ {
+ // Time based nLockTime
+ if (nLockTime == 0)
+ {
+ return true;
}
+ if (nBlockHeight == 0)
+ {
+ nBlockHeight = uint.MaxValue; // TODO: stupid stub here, should be best height instead.
+ }
+ if (nBlockTime == 0)
+ {
+ nBlockTime = NetUtils.GetAdjustedTime();
+ }
+ if (nLockTime < (nLockTime < NetUtils.nLockTimeThreshold ? nBlockHeight : nBlockTime))
+ {
+ return true;
+ }
+ foreach (var txin in vin)
+ {
+ if (!txin.IsFinal)
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ ///
+ /// Parse byte sequence and initialize new instance of CTransaction
+ ///
+ /// Byte sequence
+ public CTransaction(byte[] txBytes)
+ {
+ try
+ {
+ var wBytes = new ByteQueue(txBytes);
+
+ nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
+ nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
+
+ int nInputs = (int)wBytes.GetVarInt();
+ vin = new CTxIn[nInputs];
+
+ for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++)
+ {
+ // Fill inputs array
+ vin[nCurrentInput] = new CTxIn();
+
+ vin[nCurrentInput].prevout = new COutPoint(wBytes.Get(36));
+
+ int nScriptSigLen = (int)wBytes.GetVarInt();
+ vin[nCurrentInput].scriptSig = new CScript(wBytes.Get(nScriptSigLen));
+
+ vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
+ }
+
+ int nOutputs = (int)wBytes.GetVarInt();
+ vout = new CTxOut[nOutputs];
+
+ for (int nCurrentOutput = 0; nCurrentOutput < nOutputs; nCurrentOutput++)
+ {
+ // Fill outputs array
+ vout[nCurrentOutput] = new CTxOut();
+ vout[nCurrentOutput].nValue = BitConverter.ToUInt64(wBytes.Get(8), 0);
- nLockTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
+ int nScriptPKLen = (int)wBytes.GetVarInt();
+ vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
+ }
+
+ nLockTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
+ }
+ catch (Exception e)
+ {
+ throw new TransactionConstructorException("Deserialization failed", e);
+ }
+ }
+
+ ///
+ /// Serialized size
+ ///
+ public int Size
+ {
+ get
+ {
+ int nSize = 12; // nVersion, nTime, nLockLime
+
+ nSize += VarInt.GetEncodedSize(vin.Length);
+ nSize += VarInt.GetEncodedSize(vout.Length);
+
+ foreach (var input in vin)
+ {
+ nSize += input.Size;
+ }
+
+ foreach (var output in vout)
+ {
+ nSize += output.Size;
+ }
+
+ return nSize;
+ }
}
///
@@ -141,28 +343,36 @@ namespace Novacoin
/// Transactions array
public static CTransaction[] ReadTransactionsList(ref ByteQueue wTxBytes)
{
- // Read amount of transactions
- int nTransactions = (int)wTxBytes.GetVarInt();
- var tx = new CTransaction[nTransactions];
-
- for (int nTx = 0; nTx < nTransactions; nTx++)
+ try
{
- // Fill the transactions array
- tx[nTx] = new CTransaction();
+ // Read amount of transactions
+ int nTransactions = (int)wTxBytes.GetVarInt();
+ var tx = new CTransaction[nTransactions];
- tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
- tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+ for (int nTx = 0; nTx < nTransactions; nTx++)
+ {
+ // Fill the transactions array
+ tx[nTx] = new CTransaction();
- // Inputs array
- tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
+ tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+ tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
- // outputs array
- tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
+ // Inputs array
+ tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
- tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
- }
+ // outputs array
+ tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
- return tx;
+ tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+ }
+
+ return tx;
+
+ }
+ catch (Exception e)
+ {
+ throw new TransactionConstructorException("Deserialization failed", e);
+ }
}
public bool IsCoinBase
@@ -234,5 +444,7 @@ namespace Novacoin
return sb.ToString();
}
- }
+
+ public static bool MoneyRange(ulong nValue) { return (nValue <= nMaxMoney); }
+ }
}