Make wallet.h bit compact
[novacoin.git] / src / main.h
index 17c52c7..2247fa6 100644 (file)
@@ -53,6 +53,7 @@ static const int64_t MIN_TXOUT_AMOUNT = CENT/100;
 
 
 inline bool MoneyRange(int64_t nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
+inline bool MoneyRange(CBigNum nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
 // Threshold for nLockTime: below this value it is interpreted as block number, otherwise as UNIX timestamp.
 static const unsigned int LOCKTIME_THRESHOLD = 500000000; // Tue Nov  5 00:53:20 1985 UTC
 // Maximum number of script-checking threads allowed
@@ -85,7 +86,7 @@ extern const string strMessageMagic;
 extern int64_t nTimeBestReceived;
 extern CCriticalSection cs_setpwalletRegistered;
 extern set<CWallet*> setpwalletRegistered;
-extern uint8_t pchMessageStart[4];
+extern uint32_t nNetworkID;
 extern map<uint256, CBlock*> mapOrphanBlocks;
 
 // Settings
@@ -138,7 +139,7 @@ uint256 WantedByOrphan(const CBlock* pblockOrphan);
 const CBlockIndex* GetLastBlockIndex(const CBlockIndex* pindex, bool fProofOfStake);
 void ResendWalletTransactions(bool fForceResend=false);
 
-bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType);
+bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, uint32_t nIn, unsigned int flags, int nHashType);
 
 
 
@@ -190,7 +191,7 @@ public:
         if (IsNull())
             return "null";
         else
-            return strprintf("(nFile=%u, nBlockPos=%u, nTxPos=%u)", nFile, nBlockPos, nTxPos);
+            return strprintf("(nFile=%" PRIu32 ", nBlockPos=%" PRIu32 ", nTxPos=%" PRIu32 ")", nFile, nBlockPos, nTxPos);
     }
 
     void print() const
@@ -209,7 +210,7 @@ public:
     uint32_t n;
 
     CInPoint() { SetNull(); }
-    CInPoint(CTransaction* ptxIn, unsigned int nIn) { ptx = ptxIn; n = nIn; }
+    CInPoint(CTransaction* ptxIn, uint32_t nIn) { ptx = ptxIn; n = nIn; }
     void SetNull() { ptx = NULL; n = numeric_limits<uint32_t>::max(); }
     bool IsNull() const { return (ptx == NULL && n == numeric_limits<uint32_t>::max()); }
 };
@@ -224,7 +225,7 @@ public:
     uint32_t n;
 
     COutPoint() { SetNull(); }
-    COutPoint(uint256 hashIn, unsigned int nIn) { hash = hashIn; n = nIn; }
+    COutPoint(uint256 hashIn, uint32_t nIn) { hash = hashIn; n = nIn; }
     IMPLEMENT_SERIALIZE( READWRITE(FLATDATA(*this)); )
     void SetNull() { hash = 0; n = numeric_limits<uint32_t>::max(); }
     bool IsNull() const { return (hash == 0 && n == numeric_limits<uint32_t>::max()); }
@@ -246,7 +247,7 @@ public:
 
     string ToString() const
     {
-        return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10).c_str(), n);
+        return strprintf("COutPoint(%s, %" PRIu32 ")", hash.ToString().substr(0,10).c_str(), n);
     }
 
     void print() const
@@ -327,7 +328,7 @@ public:
         else
             str += strprintf(", scriptSig=%s", scriptSig.ToString().substr(0,24).c_str());
         if (nSequence != numeric_limits<unsigned int>::max())
-            str += strprintf(", nSequence=%u", nSequence);
+            str += strprintf(", nSequence=%" PRIu32, nSequence);
         str += ")";
         return str;
     }
@@ -581,14 +582,14 @@ public:
      */
     int64_t GetValueOut() const
     {
-        int64_t nValueOut = 0;
-        for(const CTxOut& txout :  vout)
+        CBigNum nValueOut = 0;
+        for(const auto& txout :  vout)
         {
             nValueOut += txout.nValue;
             if (!MoneyRange(txout.nValue) || !MoneyRange(nValueOut))
                 throw runtime_error("CTransaction::GetValueOut() : value out of range");
         }
-        return nValueOut;
+        return nValueOut.getint64();
     }
 
     /** Amount of bitcoins coming in to this transaction
@@ -726,34 +727,6 @@ protected:
     const CTxOut& GetOutputFor(const CTxIn& input, const MapPrevTx& inputs) const;
 };
 
-/** Closure representing one script verification
- *  Note that this stores references to the spending transaction */
-class CScriptCheck
-{
-private:
-    CScript scriptPubKey;
-    const CTransaction *ptxTo;
-    unsigned int nIn;
-    unsigned int nFlags;
-    int nHashType;
-
-public:
-    CScriptCheck() {}
-    CScriptCheck(const CTransaction& txFromIn, const CTransaction& txToIn, unsigned int nInIn, unsigned int nFlagsIn, int nHashTypeIn) :
-        scriptPubKey(txFromIn.vout[txToIn.vin[nInIn].prevout.n].scriptPubKey),
-        ptxTo(&txToIn), nIn(nInIn), nFlags(nFlagsIn), nHashType(nHashTypeIn) { }
-
-    bool operator()() const;
-
-    void swap(CScriptCheck &check) {
-        scriptPubKey.swap(check.scriptPubKey);
-        std::swap(ptxTo, check.ptxTo);
-        std::swap(nIn, check.nIn);
-        std::swap(nFlags, check.nFlags);
-        std::swap(nHashType, check.nHashType);
-    }
-};
-
 
 
 
@@ -965,9 +938,9 @@ public:
         if (nHeight >= 9689 || fTestNet)
         {
             // Take last bit of block hash as entropy bit
-            unsigned int nEntropyBit = (GetHash().Get64()) & (uint64_t)1;
+            auto nEntropyBit = (GetHash().Get32()) & (uint32_t)1;
             if (fDebug && GetBoolArg("-printstakemodifier"))
-                printf("GetStakeEntropyBit: nTime=%u hashBlock=%s nEntropyBit=%u\n", nTime, GetHash().ToString().c_str(), nEntropyBit);
+                printf("GetStakeEntropyBit: nTime=%" PRIu32 " hashBlock=%s nEntropyBit=%" PRIu32 "\n", nTime, GetHash().ToString().c_str(), nEntropyBit);
             return nEntropyBit;
         }
 
@@ -975,9 +948,9 @@ public:
         int nBitNum = nHeight & 0xFF;
         int nItemNum = nHeight / 0xFF;
 
-        unsigned int nEntropyBit = (unsigned int) ((entropyStore[nItemNum] & (uint256(1) << nBitNum)) >> nBitNum).Get64();
+        auto nEntropyBit = ((entropyStore[nItemNum] & (uint256(1) << nBitNum)) >> nBitNum).Get32();
         if (fDebug && GetBoolArg("-printstakemodifier"))
-            printf("GetStakeEntropyBit: from pregenerated table, nHeight=%d nEntropyBit=%u\n", nHeight, nEntropyBit);
+            printf("GetStakeEntropyBit: from pregenerated table, nHeight=%" PRIu32 " nEntropyBit=%" PRIu32 "\n", nHeight, nEntropyBit);
         return nEntropyBit;
     }
 
@@ -996,7 +969,6 @@ public:
     {
         if (IsProofOfStake())
             return { vtx[1].vin[0].prevout, vtx[1].nTime };
-
         return { COutPoint(), (unsigned int)0 };
     }
 
@@ -1004,7 +976,7 @@ public:
     int64_t GetMaxTransactionTime() const
     {
         int64_t maxTransactionTime = 0;
-        for(const auto& tx :  vtx)
+        for(const auto& tx : vtx)
             maxTransactionTime = max(maxTransactionTime, (int64_t)tx.nTime);
         return maxTransactionTime;
     }
@@ -1051,9 +1023,9 @@ public:
         for(const uint256& otherside :  vMerkleBranch)
         {
             if (nIndex & 1)
-                hash = Hash(BEGIN(otherside), END(otherside), BEGIN(hash), END(hash));
+                hash = Hash(otherside.begin(), otherside.end(), hash.begin(), hash.end());
             else
-                hash = Hash(BEGIN(hash), END(hash), BEGIN(otherside), END(otherside));
+                hash = Hash(hash.begin(), hash.end(), otherside.begin(), otherside.end());
             nIndex >>= 1;
         }
         return hash;
@@ -1069,7 +1041,7 @@ public:
 
         // Write index header
         unsigned int nSize = fileout.GetSerializeSize(*this);
-        fileout << FLATDATA(pchMessageStart) << nSize;
+        fileout << nNetworkID << nSize;
 
         // Write block
         long fileOutPos = ftell(fileout);
@@ -1116,7 +1088,7 @@ public:
 
     void print() const
     {
-        printf("CBlock(hash=%s, ver=%d, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%" PRIszu ", vchBlockSig=%s)\n",
+        printf("CBlock(hash=%s, ver=%" PRId32 ", hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%" PRIu32 ", nBits=%08x, nNonce=%" PRIu32 ", vtx=%" PRIszu ", vchBlockSig=%s)\n",
             GetHash().ToString().c_str(),
             nVersion,
             hashPrevBlock.ToString().c_str(),
@@ -1645,12 +1617,12 @@ public:
         return mapTx.size();
     }
 
-    bool exists(uint256 hash)
+    bool exists(const uint256 &hash)
     {
         return (mapTx.count(hash) != 0);
     }
 
-    CTransaction& lookup(uint256 hash)
+    CTransaction& lookup(const uint256 &hash)
     {
         return mapTx[hash];
     }