X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCTransaction.cs;h=e67ed459b6f64e1f8c7d8a7195e286c1d2ccc4cb;hb=161220f258cf9eb640d354951a48b45598301b1a;hp=d8572eb17eab124d236813eff5c807fd44a74124;hpb=0279648337efe9bbe32b5204e247529243805484;p=NovacoinLibrary.git diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs index d8572eb..e67ed45 100644 --- a/Novacoin/CTransaction.cs +++ b/Novacoin/CTransaction.cs @@ -19,23 +19,51 @@ using System; using System.Text; using System.Collections.Generic; +using System.IO; 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 +78,7 @@ namespace Novacoin /// /// Block height or timestamp when transaction is final /// - public uint nLockTime = 0; + public uint nLockTime; /// /// Initialize an empty instance @@ -60,8 +88,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 +121,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; + + if (!CBlockStore.Instance.GetTransaction(outpoint.hash, ref txPrev)) + return false; - nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0); - nTime = BitConverter.ToUInt32(wBytes.Get(4), 0); + if (!ScriptCode.VerifyScript(vin[i].scriptSig, txPrev.vout[outpoint.n].scriptPubKey, this, i, (int)scriptflag.SCRIPT_VERIFY_P2SH, 0)) + return false; + } - int nInputs = (int)(int)wBytes.GetVarInt(); - vin = new CTxIn[nInputs]; + return true; + } - for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++) + /// + /// 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(ref 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)); - nLockTime = BitConverter.ToUInt32(wBytes.Get(4), 0); + 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); + + 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; + } } /// @@ -139,30 +342,38 @@ namespace Novacoin /// /// Bytes sequence /// Transactions array - public static CTransaction[] ReadTransactionsList(ref ByteQueue wTxBytes) + internal static CTransaction[] ReadTransactionsList(ref BinaryReader reader) { - // 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)VarInt.ReadVarInt(ref reader); + 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 = reader.ReadUInt32(); + tx[nTx].nTime = reader.ReadUInt32(); - // outputs array - tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes); + // Inputs array + tx[nTx].vin = CTxIn.ReadTxInList(ref reader); - tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0); - } + // outputs array + tx[nTx].vout = CTxOut.ReadTxOutList(ref reader); + + tx[nTx].nLockTime = reader.ReadUInt32(); + } + + return tx; - return tx; + } + catch (Exception e) + { + throw new TransactionConstructorException("Deserialization failed", e); + } } public bool IsCoinBase @@ -191,29 +402,34 @@ namespace Novacoin /// public static implicit operator byte[] (CTransaction tx) { - var resultBytes = new List(); + var stream = new MemoryStream(); + var writer = new BinaryWriter(stream); - resultBytes.AddRange(BitConverter.GetBytes(tx.nVersion)); - resultBytes.AddRange(BitConverter.GetBytes(tx.nTime)); - resultBytes.AddRange(VarInt.EncodeVarInt(tx.vin.LongLength)); + writer.Write(tx.nVersion); + writer.Write(tx.nTime); + writer.Write(VarInt.EncodeVarInt(tx.vin.LongLength)); foreach (var input in tx.vin) { - resultBytes.AddRange((byte[])input); + writer.Write(input); } - resultBytes.AddRange(VarInt.EncodeVarInt(tx.vout.LongLength)); + writer.Write(VarInt.EncodeVarInt(tx.vout.LongLength)); foreach (var output in tx.vout) { - resultBytes.AddRange((byte[])output); + writer.Write(output); } - resultBytes.AddRange(BitConverter.GetBytes(tx.nLockTime)); + writer.Write(tx.nLockTime); + var resultBytes = stream.ToArray(); + writer.Close(); - return resultBytes.ToArray(); + return resultBytes; } + + public override string ToString() { var sb = new StringBuilder(); @@ -234,5 +450,7 @@ namespace Novacoin return sb.ToString(); } - } + + public static bool MoneyRange(ulong nValue) { return (nValue <= nMaxMoney); } + } }