Transactions fee handling
[novacoin.git] / src / main.h
index 6317b29..f76374d 100644 (file)
@@ -5,6 +5,7 @@
 #ifndef BITCOIN_MAIN_H
 #define BITCOIN_MAIN_H
 
+#include "timestamps.h"
 #include "bignum.h"
 #include "sync.h"
 #include "net.h"
@@ -38,20 +39,13 @@ static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
 static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
 static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
 
-static const int64 MIN_TX_FEE = CENT;
-static const int64 MIN_RELAY_TX_FEE = CENT;
+static const int64 MIN_TX_FEE = CENT / 20;
+static const int64 MIN_RELAY_TX_FEE = CENT / 50;
+
 static const int64 MAX_MONEY = 2000000000 * COIN;
 static const int64 MAX_MINT_PROOF_OF_WORK = 100 * COIN;
 static const int64 MAX_MINT_PROOF_OF_STAKE = 1 * COIN;
-static const int64 MIN_TXOUT_AMOUNT = MIN_TX_FEE;
-
-static const unsigned int ENTROPY_SWITCH_TIME = 1362791041; // Sat, 09 Mar 2013 01:04:01 GMT
-static const unsigned int STAKE_SWITCH_TIME = 1371686400; // Thu, 20 Jun 2013 00:00:00 GMT
-static const unsigned int TARGETS_SWITCH_TIME = 1374278400; // Sat, 20 Jul 2013 00:00:00 GMT
-static const unsigned int CHAINCHECKS_SWITCH_TIME = 1379635200; // Fri, 20 Sep 2013 00:00:00 GMT
-static const unsigned int STAKECURVE_SWITCH_TIME = 1382227200; // Sun, 20 Oct 2013 00:00:00 GMT
-static const unsigned int OUTPUT_SWITCH_TIME = 1398916800; // Thu, 01 May 2014 04:00:00 GMT
-
+static const int64 MIN_TXOUT_AMOUNT = CENT / 100;
 
 inline bool MoneyRange(int64 nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
 // Threshold for nLockTime: below this value it is interpreted as block number, otherwise as UNIX timestamp.
@@ -100,6 +94,7 @@ extern int64 nTransactionFee;
 extern int64 nMinimumInputValue;
 extern bool fUseFastIndex;
 extern unsigned int nDerivationMethodIndex;
+extern bool fEnforceCanonical;
 
 // Minimum disk space required - used in CheckDiskSpace()
 static const uint64 nMinDiskSpace = 52428800;
@@ -464,6 +459,11 @@ public:
         return SerializeHash(*this);
     }
 
+    uint256 GetMetaHash() const
+    {
+        return SignatureHash(CScript(), *this, 0, SIGHASH_ALL);
+    }
+
     bool IsFinal(int nBlockHeight=0, int64 nBlockTime=0) const
     {
         // Time based nLockTime implemented in 0.1.6
@@ -579,7 +579,7 @@ public:
         return dPriority > COIN * 144 / 250;
     }
 
-    int64 GetMinFee(unsigned int nBlockSize=1, bool fAllowFree=false, enum GetMinFee_mode mode=GMF_BLOCK, unsigned int nBytes = 0) const;
+    int64 GetMinFee(unsigned int nBlockSize=1, bool fAllowFree=false, enum GetMinFee_mode mode=GMF_BLOCK, unsigned int nBytes = 0, int64 nMinTxFee = MIN_TX_FEE, int64 nMinRelayTxFee = MIN_RELAY_TX_FEE) const;
 
     friend bool operator==(const CTransaction& a, const CTransaction& b)
     {
@@ -784,6 +784,7 @@ public:
  *
  * Serialized format:
  * - VARINT(nVersion)
+ * - VARINT(nFlags)
  * - VARINT(nCode)
  * - unspentness bitvector, for vout[2] and further; least significant byte first
  * - the non-spent CTxOuts (via CTxOutCompressor)
@@ -791,20 +792,26 @@ public:
  * - VARINT(nTime + is_coinstake)
  * - VARINT(nBlockTime)
  *
+ * The nFlag value consists of:
+ * - bit 1: is coinbase
+ * - bit 2: is coinstake
+ * - bit 3: is pruned
+ *
  * The nCode value consists of:
- * - bit 1: IsCoinBase()
  * - bit 2: vout[0] is not spent
  * - bit 4: vout[1] is not spent
  * - The higher bits encode N, the number of non-zero bytes in the following bitvector.
  *   - In case both bit 2 and bit 4 are unset, they encode N-1, as there must be at
  *     least one non-spent output).
  *
- * Example: 0104835800816115944e077fe7c803cfa57f29b36bf87c1d358bb85e40f1d75240f1d752
- *          <><><--------------------------------------------><----><------><------>
- *          |  \                  |                            /      /       /
- *     version code            vout[1]                     height timestamp block timestamp
+ * Example: 010004835800816115944e077fe7c803cfa57f29b36bf87c1d358bb85e40f1d75240f1d752
+ *          <><><><--------------------------------------------><----><------><------>
+ *          |  | \                  |                            /      /       /
+ *     version |  code            vout[1]                     height timestamp block timestamp
+ *           flags
  *
  *    - version = 1
+ *    - flags = 4
  *    - code = 4 (vout[1] is not spent, and 0 non-zero bytes of bitvector follow)
  *    - unspentness bitvector: as 0 non-zero bytes follow, it has length 0
  *    - vout[1]: 835800816115944e077fe7c803cfa57f29b36bf87c1d35
@@ -813,17 +820,20 @@ public:
  *               * 816115944e077fe7c803cfa57f29b36bf87c1d35: address uint160
  *    - height = 203998
  *    - time   = 1389883712
+ *    - is_coinbase = 0
  *    - is_coinstake = 0
  *    - block time   = 1389883712
  *
  *
- * Example: 0109044086ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4eebbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa486af3b40f1d75240f1d752
- *          <><><--><--------------------------------------------------><----------------------------------------------><----><------><------>
- *         /  \   \                     |                                                           |                     /      /       /
- *  version  code  unspentness       vout[4]                                                     vout[16]           height   timestamp block timestamp
+ * Example: 010508044086ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4eebbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa486af3b40f1d75240f1d752
+ *          <><><><--><--------------------------------------------------><----------------------------------------------><----><------><------>
+ *          /  | \   \                     |                                                           |                     /      /       /
+ *     version | code unspentness       vout[4]                                                     vout[16]              height timestamp block timestamp
+ *           flags
  *
  *  - version = 1
- *  - code = 9 (coinbase, neither vout[0] or vout[1] are unspent,
+ *  - flags = 5
+ *  - code = 8 (neither vout[0] or vout[1] are unspent,
  *                2 (1, +1 because both bit 2 and bit 4 are unset) non-zero bitvector bytes follow)
  *  - unspentness bitvector: bits 2 (0x04) and 14 (0x4000) are set, so vout[2+2] and vout[14+2] are unspent
  *  - vout[4]: 86ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4ee
@@ -836,8 +846,22 @@ public:
  *              * 8c988f1a4a4de2161e0f50aac7f17e7f9555caa4: address uint160
  *  - height = 120891
  *  - time   = 1389883712
+ *  - is_coinbase = 1
  *  - is_coinstake = 0
  *  - block time   = 1389883712
+ *
+ * Example: 010686af3b40f1d75240f1d752
+ *          <><><----><------><------>
+ *          /  |    \      |        \
+ *   version flags height timestamp block timestamp
+ *
+ *  - version = 1
+ *  - flags = 6 (00000110)
+ *  - height = 120891
+ *  - time   = 1389883712
+ *  - is_coinbase = 0
+ *  - is_coinstake = 1
+ *  - block time   = 1389883712
  */
 class CCoins
 {
@@ -872,7 +896,7 @@ public:
 
     // remove spent outputs at the end of vout
     void Cleanup() {
-        while (vout.size() > 0 && vout.back().IsNull())
+        while (vout.size() > 0 && (vout.back().IsNull() || vout.back().IsEmpty()))
             vout.pop_back();
     }
 
@@ -921,9 +945,15 @@ public:
 
     unsigned int GetSerializeSize(int nType, int nVersion) const {
         unsigned int nSize = 0;
+        bool fPruned = IsPruned();
 
         // version
         nSize += ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion);
+        unsigned char nFlags = 0;
+        // coinbase, coinstake and prune flags
+        nFlags = (fCoinBase ? 1 : 0)<<0 | (fCoinStake ? 1 : 0)<<1 | (fPruned ? 1 : 0)<<2;
+        // size of flags
+        nSize += ::GetSerializeSize(VARINT(nFlags), nType, nVersion);
 
         if (!IsPruned()) {
             unsigned int nMaskSize = 0, nMaskCode = 0;
@@ -932,7 +962,7 @@ public:
             bool fSecond = vout.size() > 1 && !vout[1].IsNull();
 
             assert(fFirst || fSecond || nMaskCode);
-            unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fCoinStake ? 1 : 0) + (fSecond ? 4 : 0);
+            unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
             // size of header code
             nSize += ::GetSerializeSize(VARINT(nCode), nType, nVersion);
             // spentness bitmask
@@ -941,35 +971,46 @@ public:
             for (unsigned int i = 0; i < vout.size(); i++)
                 if (!vout[i].IsNull())
                     nSize += ::GetSerializeSize(CTxOutCompressor(REF(vout[i])), nType, nVersion);
+            // height
+            nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion);
+            // timestamp and coinstake flag
+            nSize += ::GetSerializeSize(VARINT(nTime), nType, nVersion);
+            // block timestamp
+            nSize += ::GetSerializeSize(VARINT(nBlockTime), nType, nVersion);
         }
         else {
-            unsigned int nCode = UINT_MAX;
-            // size of header code
-            nSize += ::GetSerializeSize(VARINT(nCode), nType, nVersion);
+            // size of height
+            nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion);
+            // size of timestamp
+            nSize += ::GetSerializeSize(VARINT(nTime), nType, nVersion);
+            // size of block timestamp
+            nSize += ::GetSerializeSize(VARINT(nBlockTime), nType, nVersion);
         }
 
-        // height
-        nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion);
-        // timestamp and coinstake flag
-        nSize += ::GetSerializeSize(VARINT(nTime*2+(fCoinStake ? 1 : 0)), nType, nVersion);
-        // block timestamp
-        nSize += ::GetSerializeSize(VARINT(nBlockTime), nType, nVersion);
         return nSize;
     }
 
     template<typename Stream>
     void Serialize(Stream &s, int nType, int nVersion) const {
-        unsigned int nMaskSize = 0, nMaskCode = 0;
-        CalcMaskSize(nMaskSize, nMaskCode);
-        bool fFirst = vout.size() > 0 && !vout[0].IsNull();
-        bool fSecond = vout.size() > 1 && !vout[1].IsNull();
+        bool fPruned = IsPruned();
+        unsigned char nFlags = 0;
+        nFlags = (fCoinBase ? 1 : 0)<<0 | (fCoinStake ? 1 : 0)<<1 | (fPruned ? 1 : 0)<<2;
 
         // version
         ::Serialize(s, VARINT(this->nVersion), nType, nVersion);
+        // flags
+        ::Serialize(s, VARINT(nFlags), nType, nVersion);
+
+        if (!fPruned) {
+            unsigned int nMaskSize = 0, nMaskCode = 0;
+            CalcMaskSize(nMaskSize, nMaskCode);
+            bool fFirst = vout.size() > 0 && !vout[0].IsNull();
+            bool fSecond = vout.size() > 1 && !vout[1].IsNull();
 
-        if (!IsPruned()) {
             assert(fFirst || fSecond || nMaskCode);
-            unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
+
+            unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
+
             // header code
             ::Serialize(s, VARINT(nCode), nType, nVersion);
             // spentness bitmask
@@ -985,30 +1026,40 @@ public:
                 if (!vout[i].IsNull())
                     ::Serialize(s, CTxOutCompressor(REF(vout[i])), nType, nVersion);
             }
+            // coinbase height
+            ::Serialize(s, VARINT(nHeight), nType, nVersion);
+            // transaction timestamp and coinstake flag
+            ::Serialize(s, VARINT(nTime), nType, nVersion);
+            // block timestamp
+            ::Serialize(s, VARINT(nBlockTime), nType, nVersion);
         }
         else {
-            unsigned int nCode = UINT_MAX;
-            // size of header code
-            ::Serialize(s, VARINT(nCode), nType, nVersion);
+            // coinbase height
+            ::Serialize(s, VARINT(nHeight), nType, nVersion);
+            // transaction timestamp
+            ::Serialize(s, VARINT(nTime), nType, nVersion);
+            // block timestamp
+            ::Serialize(s, VARINT(nBlockTime), nType, nVersion);
         }
-        // coinbase height
-        ::Serialize(s, VARINT(nHeight), nType, nVersion);
-        // transaction timestamp and coinstake flag
-        ::Serialize(s, VARINT(nTime*2+(fCoinStake ? 1 : 0)), nType, nVersion);
-        // block time
-        ::Serialize(s, VARINT(nBlockTime), nType, nVersion);
     }
 
     template<typename Stream>
     void Unserialize(Stream &s, int nType, int nVersion) {
-        unsigned int nCode = 0, nCodeTime = 0;
+        unsigned char nFlags = 0;
+
         // version
         ::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
-        // header code
-        ::Unserialize(s, VARINT(nCode), nType, nVersion);
-        if (nCode != UINT_MAX)
-        {
-            fCoinBase = nCode & 1;
+        // coinbase and coinstake flags
+        ::Unserialize(s, VARINT(nFlags), nType, nVersion);
+
+        fCoinBase = nFlags & (1<<0);
+        fCoinStake = nFlags & (1<<1);
+        bool fPruned = nFlags & (1<<2);
+
+        if (!fPruned) {
+            unsigned int nCode = 0;
+            // header code
+            ::Unserialize(s, VARINT(nCode), nType, nVersion);
             std::vector<bool> vAvail(2, false);
             vAvail[0] = nCode & 2;
             vAvail[1] = nCode & 4;
@@ -1030,15 +1081,22 @@ public:
                 if (vAvail[i])
                     ::Unserialize(s, REF(CTxOutCompressor(vout[i])), nType, nVersion);
             }
+            // coinbase height
+            ::Unserialize(s, VARINT(nHeight), nType, nVersion);
+            // transaction timestamp
+            ::Unserialize(s, VARINT(nTime), nType, nVersion);
+            nTime = nTime;
+            // block timestamp
+            ::Unserialize(s, VARINT(nBlockTime), nType, nVersion);
+        }
+        else {
+            // coinbase height
+            ::Unserialize(s, VARINT(nHeight), nType, nVersion);
+            // transaction timestamp
+            ::Unserialize(s, VARINT(nTime), nType, nVersion);
+            // block timestamp
+            ::Unserialize(s, VARINT(nBlockTime), nType, nVersion);
         }
-        // coinbase height
-        ::Unserialize(s, VARINT(nHeight), nType, nVersion);
-        // transaction timestamp
-        ::Unserialize(s, VARINT(nCodeTime), nType, nVersion);
-        nTime = nCodeTime / 2;
-        fCoinStake = nCodeTime & 1;
-        // block timestamp
-        ::Unserialize(s, VARINT(nBlockTime), nType, nVersion);
         Cleanup();
     }
 
@@ -1083,6 +1141,7 @@ public:
         BOOST_FOREACH(const CTxOut &out, vout)
             if (!out.IsNull())
                 return false;
+
         return true;
     }
 };
@@ -1231,29 +1290,9 @@ public:
 
     void UpdateTime(const CBlockIndex* pindexPrev);
 
-    // ppcoin: entropy bit for stake modifier if chosen by modifier
-    unsigned int GetStakeEntropyBit(unsigned int nTime) const
-    {
-        // Protocol switch to support p2pool at novacoin block #9689
-        if (nTime >= ENTROPY_SWITCH_TIME || fTestNet)
-        {
-            // Take last bit of block hash as entropy bit
-            unsigned int nEntropyBit = ((GetHash().Get64()) & 1llu);
-            if (fDebug && GetBoolArg("-printstakemodifier"))
-                printf("GetStakeEntropyBit: nTime=%u hashBlock=%s nEntropyBit=%u\n", nTime, GetHash().ToString().c_str(), nEntropyBit);
-            return nEntropyBit;
-        }
-        // Before novacoin block #9689 - old protocol
-        uint160 hashSig = Hash160(vchBlockSig);
-        if (fDebug && GetBoolArg("-printstakemodifier"))
-            printf("GetStakeEntropyBit: hashSig=%s", hashSig.ToString().c_str());
-        hashSig >>= 159; // take the first bit of the hash
-        if (fDebug && GetBoolArg("-printstakemodifier"))
-            printf(" entropybit=%"PRI64d"\n", hashSig.Get64());
-        return hashSig.Get64();
-    }
+    unsigned int GetStakeEntropyBit(unsigned int nTime) const;
 
-    // ppcoin: two types of block: proof-of-work or proof-of-stake
+    // two types of block: proof-of-work or proof-of-stake
     bool IsProofOfStake() const
     {
         return (vtx.size() > 1 && vtx[1].IsCoinStake());
@@ -1269,7 +1308,7 @@ public:
         return IsProofOfStake()? std::make_pair(vtx[1].vin[0].prevout, vtx[1].nTime) : std::make_pair(COutPoint(), (unsigned int)0);
     }
 
-    // ppcoin: get max transaction timestamp
+    // get max transaction timestamp
     int64 GetMaxTransactionTime() const
     {
         int64 maxTransactionTime = 0;