GetMinTxFee & use unsigned int for sizes.
authorCryptoManiac <balthazar@yandex.ru>
Mon, 7 Sep 2015 21:30:19 +0000 (00:30 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Mon, 7 Sep 2015 21:30:19 +0000 (00:30 +0300)
Novacoin/CBlock.cs
Novacoin/CBlockStore.cs
Novacoin/CScript.cs
Novacoin/CTransaction.cs
Novacoin/CTxIn.cs
Novacoin/CTxOut.cs
Novacoin/DatabaseObjects.cs
Novacoin/VarInt.cs

index 6c10d6e..afc3e48 100644 (file)
@@ -382,18 +382,18 @@ namespace Novacoin
         /// <summary>
         /// Serialized size
         /// </summary>
-        public int Size
+        public uint Size
         {
             get
             {
-                int nSize = 80 + VarInt.GetEncodedSize(vtx.Length); // CBlockHeader + NumTx
+                uint nSize = 80 + VarInt.GetEncodedSize(vtx.Length); // CBlockHeader + NumTx
 
                 foreach (var tx in vtx)
                 {
                     nSize += tx.Size;
                 }
 
-                nSize += VarInt.GetEncodedSize(signature.Length) + signature.Length;
+                nSize += VarInt.GetEncodedSize(signature.Length) + (uint)signature.Length;
 
                 return nSize;
             }
@@ -404,11 +404,11 @@ namespace Novacoin
         /// </summary>
         /// <param name="nTx">Transaction index.</param>
         /// <returns>Offset in bytes from the beginning of block header.</returns>
-        public int GetTxOffset(int nTx)
+        public uint GetTxOffset(int nTx)
         {
             Contract.Requires<ArgumentException>(nTx >= 0 && nTx < vtx.Length, "Transaction index you've specified is incorrect.");
 
-            int nOffset = 80 + VarInt.GetEncodedSize(vtx.Length); // CBlockHeader + NumTx
+            uint nOffset = 80 + VarInt.GetEncodedSize(vtx.Length); // CBlockHeader + NumTx
 
             for (int i = 0; i < nTx; i++)
             {
index 97ce54e..e1d1994 100644 (file)
@@ -904,10 +904,9 @@ namespace Novacoin
                         return false; // unable to get coin age for coinstake
                     }
 
-                    int nTxSize = (tx.nTime > NetInfo.nStakeValidationSwitchTime) ? tx.Size : 0;
                     ulong nReward = tx.nValueOut - nValueIn;
 
-                    ulong nCalculatedReward = CBlock.GetProofOfStakeReward(nCoinAge, cursorBlock.nBits, tx.nTime) - CTransaction.GetMinFee(1, false, CTransaction.MinFeeMode.GMF_BLOCK, nTxSize) + CTransaction.nCent;
+                    ulong nCalculatedReward = CBlock.GetProofOfStakeReward(nCoinAge, cursorBlock.nBits, tx.nTime) - tx.GetMinFee(1, false, CTransaction.MinFeeMode.GMF_BLOCK) + CTransaction.nCent;
 
                     if (nReward > nCalculatedReward)
                     {
index d27b337..2ac0c73 100644 (file)
@@ -402,7 +402,7 @@ namespace Novacoin
             // get the last item that the scriptSig
             // pushes onto the stack:
             InstructionQueue wScriptSig = scriptSig.GetInstructionQueue();
-            int nScriptSigSize = scriptSig.Size;
+            uint nScriptSigSize = scriptSig.Size;
 
             instruction opcode; // Current instruction
             byte[] pushArgs = new byte[0]; // OP_PUSHDATAn argument
@@ -512,9 +512,9 @@ namespace Novacoin
         /// <summary>
         /// Script size
         /// </summary>
-        public int Size
+        public uint Size
         {
-            get { return codeBytes.Count; }
+            get { return (uint) codeBytes.Count; }
         }
 
         public CScriptID ScriptID
index b0d4f08..3ebde5f 100644 (file)
@@ -57,11 +57,16 @@ namespace Novacoin
         /// One coin = 1000000 satoshis.
         /// </summary>
         public const ulong nCoin = 1000000;
+        
         /// <summary>
         /// Sanity checking threshold.
         /// </summary>
         public const ulong nMaxMoney = 2000000000 * nCoin;
 
+        public const ulong nMinTxFee = nCent / 10;
+        public const ulong nMinRelayTxFee = nCent / 50;
+        public const ulong nMinTxoutAmount = nCent / 100;        
+
         /// <summary>
         /// Maximum transaction size is 250Kb
         /// </summary>
@@ -334,11 +339,11 @@ namespace Novacoin
         /// <summary>
         /// Serialized size
         /// </summary>
-        public int Size
+        public uint Size
         {
             get
             {
-                int nSize = 12; // nVersion, nTime, nLockLime
+                uint nSize = 12; // nVersion, nTime, nLockLime
 
                 nSize += VarInt.GetEncodedSize(vin.Length);
                 nSize += VarInt.GetEncodedSize(vout.Length);
@@ -557,9 +562,78 @@ namespace Novacoin
             throw new NotImplementedException();
         }
 
-        internal static ulong GetMinFee(int v1, bool v2, MinFeeMode gMF_BLOCK, int nTxSize)
+        public ulong GetMinFee(uint nBlockSize, bool fAllowFree, MinFeeMode mode)
         {
-            throw new NotImplementedException();
+            ulong nMinTxFee = CTransaction.nMinTxFee, nMinRelayTxFee = CTransaction.nMinRelayTxFee;
+            uint nBytes = Size;
+
+            if (IsCoinStake)
+            {
+                // Enforce 0.01 as minimum fee for old approach or coinstake
+                nMinTxFee = nCent;
+                nMinRelayTxFee = nCent;
+
+                if (nTime < NetInfo.nStakeValidationSwitchTime)
+                {
+                    // Enforce zero size for compatibility with old blocks.
+                    nBytes = 0;
+                }
+            }
+
+            // Base fee is either nMinTxFee or nMinRelayTxFee
+            ulong nBaseFee = (mode == MinFeeMode.GMF_RELAY) ? nMinRelayTxFee : nMinTxFee;
+
+            uint nNewBlockSize = nBlockSize + nBytes;
+            ulong nMinFee = (1 + (ulong)nBytes / 1000) * nBaseFee;
+
+            if (fAllowFree)
+            {
+                if (nBlockSize == 1)
+                {
+                    // Transactions under 1K are free
+                    if (nBytes < 1000)
+                        nMinFee = 0;
+                }
+                else
+                {
+                    // Free transaction area
+                    if (nNewBlockSize < 27000)
+                        nMinFee = 0;
+                }
+            }
+
+            // To limit dust spam, require additional MIN_TX_FEE/MIN_RELAY_TX_FEE for
+            //    each non empty output which is less than 0.01
+            //
+            // It's safe to ignore empty outputs here, because these inputs are allowed
+            //     only for coinbase and coinstake transactions.
+            foreach (var txout in vout)
+            {
+                if (txout.nValue < nCent && !txout.IsEmpty)
+                {
+                    nMinFee += nBaseFee;
+                }
+            }
+
+            var nMaxBlockSizeGen = CBlock.nMaxBlockSize / 2;
+
+            // Raise the price as the block approaches full
+            if (nBlockSize != 1 && nNewBlockSize >= nMaxBlockSizeGen / 2)
+            {
+                if (nNewBlockSize >= nMaxBlockSizeGen)
+                {
+                    return nMaxMoney;
+                }
+
+                nMinFee *= nMaxBlockSizeGen / (nMaxBlockSizeGen - nNewBlockSize);
+            }
+
+            if (!MoneyRange(nMinFee))
+            {
+                nMinFee = nMaxMoney;
+            }
+
+            return nMinFee;
         }
     }
 }
index 94cd8b1..480e1d1 100644 (file)
@@ -115,10 +115,10 @@ namespace Novacoin
         /// <summary>
         /// Serialized size
         /// </summary>
-        public int Size
+        public uint Size
         {
             get {
-                int nSize = 40; // COutPoint, nSequence
+                uint nSize = 40; // COutPoint, nSequence
                 nSize += VarInt.GetEncodedSize(scriptSig.Size);
                 nSize += scriptSig.Size;
 
index 3017fea..36f4817 100644 (file)
@@ -183,7 +183,7 @@ namespace Novacoin
         /// <summary>
         /// Serialized size
         /// </summary>
-        public int Size
+        public uint Size
         {
             get
             {
index a78f93b..b297f6d 100644 (file)
@@ -612,7 +612,7 @@ namespace Novacoin
             {
                 reader.Seek(nBlockPos + nTxOffset, SeekOrigin.Begin); // Seek to transaction offset
 
-                if (nTxSize != reader.Read(buffer, 0, nTxSize))
+                if (nTxSize != reader.Read(buffer, 0, (int)nTxSize))
                 {
                     return false;
                 }
@@ -647,9 +647,9 @@ namespace Novacoin
         /// Transaction size accessor
         /// </summary>
         [Ignore]
-        public int nTxSize
+        public uint nTxSize
         {
-            get { return (int)VarInt.DecodeVarInt(TxSize); }
+            get { return (uint)VarInt.DecodeVarInt(TxSize); }
             private set { TxSize = VarInt.EncodeVarInt(value); }
         }
 
index 614e3c6..ba174e9 100644 (file)
@@ -83,7 +83,7 @@ namespace Novacoin
             return EncodeVarInt((ulong)n);
         }
 
-        public static int GetEncodedSize(long n)
+        public static uint GetEncodedSize(long n)
         {
             if (n <= 0xfc)
             {