Close file if fseek failed
[novacoin.git] / src / main.cpp
index fd1f25f..98c2105 100644 (file)
@@ -116,13 +116,6 @@ bool static IsFromMe(CTransaction& tx)
     return false;
 }
 
-// erases transaction with the given hash from all wallets
-void static EraseFromWallets(uint256 hash)
-{
-    for(CWallet* pwallet :  setpwalletRegistered)
-        pwallet->EraseFromWallet(hash);
-}
-
 // make sure all wallets know about the given transaction, in the given block
 void SyncWithWallets(const CTransaction& tx, const CBlock* pblock, bool fUpdate, bool fConnect)
 {
@@ -366,7 +359,7 @@ bool CTransaction::AreInputsStandard(const MapPrevTx& mapInputs) const
     if (IsCoinBase())
         return true; // Coinbases don't use vin normally
 
-    for (unsigned int i = 0; i < vin.size(); i++)
+    for (uint32_t i = 0; i < vin.size(); i++)
     {
         const CTxOut& prev = GetOutputFor(vin[i], mapInputs);
 
@@ -499,17 +492,14 @@ bool CTransaction::CheckTransaction() const
         return DoS(100, error("CTransaction::CheckTransaction() : size limits failed"));
 
     // Check for negative or overflow output values
-    int64_t nValueOut = 0;
-    for (unsigned int i = 0; i < vout.size(); i++)
+    CBigNum nValueOut = 0;
+    for (uint32_t i = 0; i < vout.size(); i++)
     {
-        const CTxOut& txout = vout[i];
+        const auto& txout = vout[i];
         if (txout.IsEmpty() && !IsCoinBase() && !IsCoinStake())
             return DoS(100, error("CTransaction::CheckTransaction() : txout empty for user transaction"));
-
-        if (txout.nValue < 0)
-            return DoS(100, error("CTransaction::CheckTransaction() : txout.nValue is negative"));
-        if (txout.nValue > MAX_MONEY)
-            return DoS(100, error("CTransaction::CheckTransaction() : txout.nValue too high"));
+        if (!MoneyRange(txout.nValue))
+            return DoS(100, error("CTransaction::CheckTransaction() : txout.nValue is out of range"));
         nValueOut += txout.nValue;
         if (!MoneyRange(nValueOut))
             return DoS(100, error("CTransaction::CheckTransaction() : txout total out of range"));
@@ -638,30 +628,13 @@ bool CTxMemPool::accept(CTxDB& txdb, CTransaction &tx, bool fCheckInputs,
             return false;
 
     // Check for conflicts with in-memory transactions
-    CTransaction* ptxOld = NULL;
     for (unsigned int i = 0; i < tx.vin.size(); i++)
     {
         auto outpoint = tx.vin[i].prevout;
         if (mapNextTx.count(outpoint))
         {
-            // Disable replacement feature for now
+            // Replacement feature isn't supported by Novacoin.
             return false;
-
-            // Allow replacing with a newer version of the same transaction
-            if (i != 0)
-                return false;
-            ptxOld = mapNextTx[outpoint].ptx;
-            if (ptxOld->IsFinal())
-                return false;
-            if (!tx.IsNewerThan(*ptxOld))
-                return false;
-            for (unsigned int i = 0; i < tx.vin.size(); i++)
-            {
-                auto outpoint = tx.vin[i].prevout;
-                if (!mapNextTx.count(outpoint) || mapNextTx[outpoint].ptx != ptxOld)
-                    return false;
-            }
-            break;
         }
     }
 
@@ -733,19 +706,9 @@ bool CTxMemPool::accept(CTxDB& txdb, CTransaction &tx, bool fCheckInputs,
     // Store transaction in memory
     {
         LOCK(cs);
-        if (ptxOld)
-        {
-            printf("CTxMemPool::accept() : replacing tx %s with new version\n", ptxOld->GetHash().ToString().c_str());
-            remove(*ptxOld);
-        }
         addUnchecked(hash, tx);
     }
 
-    ///// are we sure this is ok when loading transactions or restoring block txes
-    // If updated, erase old tx from wallet
-    if (ptxOld)
-        EraseFromWallets(ptxOld->GetHash());
-
     printf("CTxMemPool::accept() : accepted %s (poolsz %" PRIszu ")\n",
            hash.ToString().substr(0,10).c_str(),
            mapTx.size());
@@ -1408,12 +1371,15 @@ int64_t CTransaction::GetValueIn(const MapPrevTx& inputs) const
     if (IsCoinBase())
         return 0;
 
-    int64_t nResult = 0;
-    for (unsigned int i = 0; i < vin.size(); i++)
+    CBigNum nResult = 0;
+    for (uint32_t i = 0; i < vin.size(); i++)
     {
-        nResult += GetOutputFor(vin[i], inputs).nValue;
+        const auto& txOut = GetOutputFor(vin[i], inputs);
+        nResult += txOut.nValue;
+        if (!MoneyRange(txOut.nValue) || !MoneyRange(nResult))
+            throw runtime_error("CTransaction::GetValueIn() : value out of range");
     }
-    return nResult;
+    return nResult.getint64();
 
 }
 
@@ -1455,32 +1421,34 @@ bool CTransaction::ConnectInputs(CTxDB& txdb, MapPrevTx inputs, map<uint256, CTx
     if (!IsCoinBase())
     {
         int64_t nValueIn = 0;
-        int64_t nFees = 0;
-        for (unsigned int i = 0; i < vin.size(); i++)
         {
-            auto prevout = vin[i].prevout;
-            assert(inputs.count(prevout.hash) > 0);
-            CTxIndex& txindex = inputs[prevout.hash].first;
-            CTransaction& txPrev = inputs[prevout.hash].second;
-
-            if (prevout.n >= txPrev.vout.size() || prevout.n >= txindex.vSpent.size())
-                return DoS(100, error("ConnectInputs() : %s prevout.n out of range %d %" PRIszu " %" PRIszu " prev tx %s\n%s", GetHash().ToString().substr(0,10).c_str(), prevout.n, txPrev.vout.size(), txindex.vSpent.size(), prevout.hash.ToString().substr(0,10).c_str(), txPrev.ToString().c_str()));
-
-            // If prev is coinbase or coinstake, check that it's matured
-            if (txPrev.IsCoinBase() || txPrev.IsCoinStake())
-                for (const CBlockIndex* pindex = pindexBlock; pindex && pindexBlock->nHeight - pindex->nHeight < nCoinbaseMaturity; pindex = pindex->pprev)
-                    if (pindex->nBlockPos == txindex.pos.nBlockPos && pindex->nFile == txindex.pos.nFile)
-                        return error("ConnectInputs() : tried to spend %s at depth %d", txPrev.IsCoinBase() ? "coinbase" : "coinstake", pindexBlock->nHeight - pindex->nHeight);
-
-            // ppcoin: check transaction timestamp
-            if (txPrev.nTime > nTime)
-                return DoS(100, error("ConnectInputs() : transaction timestamp earlier than input transaction"));
-
-            // Check for negative or overflow input values
-            nValueIn += txPrev.vout[prevout.n].nValue;
-            if (!MoneyRange(txPrev.vout[prevout.n].nValue) || !MoneyRange(nValueIn))
-                return DoS(100, error("ConnectInputs() : txin values out of range"));
-
+            CBigNum bnValueIn = 0;
+            for (uint32_t i = 0; i < vin.size(); i++)
+            {
+                auto prevout = vin[i].prevout;
+                assert(inputs.count(prevout.hash) > 0);
+                CTxIndex& txindex = inputs[prevout.hash].first;
+                CTransaction& txPrev = inputs[prevout.hash].second;
+
+                if (prevout.n >= txPrev.vout.size() || prevout.n >= txindex.vSpent.size())
+                    return DoS(100, error("ConnectInputs() : %s prevout.n out of range %d %" PRIszu " %" PRIszu " prev tx %s\n%s", GetHash().ToString().substr(0,10).c_str(), prevout.n, txPrev.vout.size(), txindex.vSpent.size(), prevout.hash.ToString().substr(0,10).c_str(), txPrev.ToString().c_str()));
+
+                // If prev is coinbase or coinstake, check that it's matured
+                if (txPrev.IsCoinBase() || txPrev.IsCoinStake())
+                    for (const CBlockIndex* pindex = pindexBlock; pindex && pindexBlock->nHeight - pindex->nHeight < nCoinbaseMaturity; pindex = pindex->pprev)
+                        if (pindex->nBlockPos == txindex.pos.nBlockPos && pindex->nFile == txindex.pos.nFile)
+                            return error("ConnectInputs() : tried to spend %s at depth %d", txPrev.IsCoinBase() ? "coinbase" : "coinstake", pindexBlock->nHeight - pindex->nHeight);
+
+                // ppcoin: check transaction timestamp
+                if (txPrev.nTime > nTime)
+                    return DoS(100, error("ConnectInputs() : transaction timestamp earlier than input transaction"));
+
+                // Check for negative or overflow input values
+                bnValueIn += txPrev.vout[prevout.n].nValue;
+                if (!MoneyRange(txPrev.vout[prevout.n].nValue) || !MoneyRange(bnValueIn))
+                    return DoS(100, error("ConnectInputs() : txin values out of range"));
+            }
+            nValueIn = bnValueIn.getint64();
         }
 
         if (pvChecks)
@@ -1562,11 +1530,7 @@ bool CTransaction::ConnectInputs(CTxDB& txdb, MapPrevTx inputs, map<uint256, CTx
 
             // Tally transaction fees
             auto nTxFee = nValueIn - GetValueOut();
-            if (nTxFee < 0)
-                return DoS(100, error("ConnectInputs() : %s nTxFee < 0", GetHash().ToString().substr(0,10).c_str()));
-
-            nFees += nTxFee;
-            if (!MoneyRange(nFees))
+            if (!MoneyRange(nTxFee))
                 return DoS(100, error("ConnectInputs() : nFees out of range"));
         }
     }
@@ -1583,8 +1547,8 @@ bool CTransaction::ClientConnectInputs()
     // Take over previous transactions' spent pointers
     {
         LOCK(mempool.cs);
-        int64_t nValueIn = 0;
-        for (unsigned int i = 0; i < vin.size(); i++)
+        CBigNum bnValueIn = 0;
+        for (uint32_t i = 0; i < vin.size(); i++)
         {
             // Get prev tx from single transactions in memory
             auto prevout = vin[i].prevout;
@@ -1609,12 +1573,12 @@ bool CTransaction::ClientConnectInputs()
             // // Flag outpoints as used
             // txPrev.vout[prevout.n].posNext = posThisTx;
 
-            nValueIn += txPrev.vout[prevout.n].nValue;
+            bnValueIn += txPrev.vout[prevout.n].nValue;
 
-            if (!MoneyRange(txPrev.vout[prevout.n].nValue) || !MoneyRange(nValueIn))
+            if (!MoneyRange(txPrev.vout[prevout.n].nValue) || !MoneyRange(bnValueIn))
                 return error("ClientConnectInputs() : txin values out of range");
         }
-        if (GetValueOut() > nValueIn)
+        if (GetValueOut() > bnValueIn.getint64())
             return false;
     }
 
@@ -2355,10 +2319,10 @@ bool CBlock::AcceptBlock()
     bool cpSatisfies = Checkpoints::CheckSync(hash, pindexPrev);
 
     // Check that the block satisfies synchronized checkpoint
-    if (CheckpointsMode == Checkpoints::STRICT && !cpSatisfies)
+    if (CheckpointsMode == Checkpoints::CP_STRICT && !cpSatisfies)
         return error("AcceptBlock() : rejected by synchronized checkpoint");
 
-    if (CheckpointsMode == Checkpoints::ADVISORY && !cpSatisfies)
+    if (CheckpointsMode == Checkpoints::CP_ADVISORY && !cpSatisfies)
         strMiscWarning = _("WARNING: syncronized checkpoint violation detected, but skipped!");
 
     // Enforce rule that the coinbase starts with serialized block height
@@ -2713,7 +2677,10 @@ FILE* AppendBlockFile(unsigned int& nFileRet)
         if (!file)
             return NULL;
         if (fseek(file, 0, SEEK_END) != 0)
+        {
+            fclose(file);
             return NULL;
+        }
         // FAT32 file size max 4GB, fseek and ftell max 2GB, so we must stay under 2GB
         if (ftell(file) < (long)(0x7F000000 - MAX_SIZE))
         {
@@ -2741,10 +2708,7 @@ bool LoadBlockIndex(bool fAllowNew)
 {
     if (fTestNet)
     {
-        pchMessageStart[0] = 0xcd;
-        pchMessageStart[1] = 0xf2;
-        pchMessageStart[2] = 0xc0;
-        pchMessageStart[3] = 0xef;
+        nNetworkID = 0xefc0f2cd;
 
         bnProofOfWorkLimit = bnProofOfWorkLimitTestNet; // 16 bits PoW target limit for testnet
         nStakeMinAge = 2 * nOneHour; // test net min age is 2 hours
@@ -2970,7 +2934,7 @@ bool LoadExternalBlockFile(FILE* fileIn, CClientUIInterface& uiInterface)
                         auto nBlockLength = *reinterpret_cast<const uint32_t*>(&(*(it+4)));
                         auto SeekToNext = [&pchData, &it, &nPos, &nBlockLength]() {
                             auto previt = it;
-                            it = search(it+8, pchData.end(), BEGIN(pchMessageStart), END(pchMessageStart));
+                            it = search(it+8, pchData.end(), BEGIN(nNetworkID), END(nNetworkID));
                             if (it != pchData.end())
                                 nPos += (it - previt);
                         };
@@ -3106,36 +3070,31 @@ string GetWarnings(string strFor)
 
 bool static AlreadyHave(CTxDB& txdb, const CInv& inv)
 {
-    switch (inv.type)
+    int nType = inv.GetType();
+    auto nHash = inv.GetHash();
+
+    switch (nType)
     {
     case MSG_TX:
         {
         bool txInMap = false;
             {
             LOCK(mempool.cs);
-            txInMap = (mempool.exists(inv.hash));
+            txInMap = (mempool.exists(nHash));
             }
         return txInMap ||
-               mapOrphanTransactions.count(inv.hash) ||
-               txdb.ContainsTx(inv.hash);
+               mapOrphanTransactions.count(nHash) ||
+               txdb.ContainsTx(nHash);
         }
 
     case MSG_BLOCK:
-        return mapBlockIndex.count(inv.hash) ||
-               mapOrphanBlocks.count(inv.hash);
+        return mapBlockIndex.count(nHash) ||
+               mapOrphanBlocks.count(nHash);
     }
     // Don't know what it is, just say we already got one
     return true;
 }
 
-
-
-
-// The message start string is designed to be unlikely to occur in normal data.
-// The characters are rarely used upper ASCII, not valid as UTF-8, and produce
-// a large 4-byte int at any alignment.
-uint8_t pchMessageStart[4] = { 0xe4, 0xe8, 0xe9, 0xe5 };
-
 bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
 {
     static map<CService, CPubKey> mapReuseKey;
@@ -3371,10 +3330,10 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
         }
 
         // find last block in inv vector
-        size_t nLastBlock = std::numeric_limits<size_t>::max();
+        int nLastBlock = -1;
         for (size_t nInv = 0; nInv < vInv.size(); nInv++) {
-            if (vInv[vInv.size() - 1 - nInv].type == MSG_BLOCK) {
-                nLastBlock = vInv.size() - 1 - nInv;
+            if (vInv[vInv.size() - 1 - nInv].GetType() == MSG_BLOCK) {
+                nLastBlock = (int) (vInv.size() - 1 - nInv);
                 break;
             }
         }
@@ -3382,6 +3341,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
         for (size_t nInv = 0; nInv < vInv.size(); nInv++)
         {
             const CInv &inv = vInv[nInv];
+            int nType = inv.GetType();
+            auto nHash = inv.GetHash();
 
             if (fShutdown)
                 return true;
@@ -3393,19 +3354,19 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
 
             if (!fAlreadyHave)
                 pfrom->AskFor(inv);
-            else if (inv.type == MSG_BLOCK && mapOrphanBlocks.count(inv.hash)) {
-                pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(mapOrphanBlocks[inv.hash]));
-            } else if (nInv == nLastBlock) {
+            else if (nType == MSG_BLOCK && mapOrphanBlocks.count(nHash)) {
+                pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(mapOrphanBlocks[nHash]));
+            } else if (nType == nLastBlock) {
                 // In case we are on a very long side-chain, it is possible that we already have
                 // the last block in an inv bundle sent in response to getblocks. Try to detect
                 // this situation and push another getblocks to continue.
-                pfrom->PushGetBlocks(mapBlockIndex[inv.hash], uint256(0));
+                pfrom->PushGetBlocks(mapBlockIndex[nHash], uint256(0));
                 if (fDebug)
                     printf("force request: %s\n", inv.ToString().c_str());
             }
 
             // Track requests for our stuff
-            Inventory(inv.hash);
+            Inventory(nHash);
         }
     }
 
@@ -3430,10 +3391,13 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
             if (fDebugNet || (vInv.size() == 1))
                 printf("received getdata for: %s\n", inv.ToString().c_str());
 
-            if (inv.type == MSG_BLOCK)
+            int nType = inv.GetType();
+            auto nHash = inv.GetHash();
+
+            if (nType == MSG_BLOCK)
             {
                 // Send block from disk
-                auto mi = mapBlockIndex.find(inv.hash);
+                auto mi = mapBlockIndex.find(nHash);
                 if (mi != mapBlockIndex.end())
                 {
                     CBlock block;
@@ -3441,7 +3405,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
                     pfrom->PushMessage("block", block);
 
                     // Trigger them to send a getblocks request for the next batch of inventory
-                    if (inv.hash == pfrom->hashContinue)
+                    if (nHash == pfrom->hashContinue)
                     {
                         // ppcoin: send latest proof-of-work block to allow the
                         // download node to accept as orphan (proof-of-stake 
@@ -3465,10 +3429,10 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
                         pushed = true;
                     }
                 }
-                if (!pushed && inv.type == MSG_TX) {
+                if (!pushed && nType == MSG_TX) {
                     LOCK(mempool.cs);
-                    if (mempool.exists(inv.hash)) {
-                        CTransaction tx = mempool.lookup(inv.hash);
+                    if (mempool.exists(nHash)) {
+                        CTransaction tx = mempool.lookup(nHash);
                         CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
                         ss.reserve(1000);
                         ss << tx;
@@ -3478,7 +3442,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
             }
 
             // Track requests for our stuff
-            Inventory(inv.hash);
+            Inventory(nHash);
         }
     }
 
@@ -3585,11 +3549,13 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
         bool fMissingInputs = false;
         if (tx.AcceptToMemoryPool(txdb, true, &fMissingInputs))
         {
+            auto nHash = inv.GetHash();
+
             SyncWithWallets(tx, NULL, true);
-            RelayTransaction(tx, inv.hash);
+            RelayTransaction(tx, nHash);
             mapAlreadyAskedFor.erase(inv);
-            vWorkQueue.push_back(inv.hash);
-            vEraseQueue.push_back(inv.hash);
+            vWorkQueue.push_back(nHash);
+            vEraseQueue.push_back(nHash);
 
             // Recursively process any orphan transactions that depended on this one
             for (unsigned int i = 0; i < vWorkQueue.size(); i++)
@@ -3824,7 +3790,7 @@ bool ProcessMessages(CNode* pfrom)
             break;
 
         // Scan for message start
-        CDataStream::iterator pstart = search(vRecv.begin(), vRecv.end(), BEGIN(pchMessageStart), END(pchMessageStart));
+        CDataStream::iterator pstart = search(vRecv.begin(), vRecv.end(), BEGIN(nNetworkID), END(nNetworkID));
         int nHeaderSize = vRecv.GetSerializeSize(CMessageHeader());
         if (vRecv.end() - pstart < nHeaderSize)
         {
@@ -4000,13 +3966,13 @@ bool SendMessages(CNode* pto)
                     continue;
 
                 // trickle out tx inv to protect privacy
-                if (inv.type == MSG_TX && !fSendTrickle)
+                if (inv.GetType() == MSG_TX && !fSendTrickle)
                 {
                     // 1/4 of tx invs blast to all immediately
                     static uint256 hashSalt;
                     if (hashSalt == 0)
                         hashSalt = GetRandHash();
-                    uint256 hashRand = inv.hash ^ hashSalt;
+                    uint256 hashRand = inv.GetHash() ^ hashSalt;
                     hashRand = Hash(hashRand.begin(), hashRand.end());
                     bool fTrickleWait = ((hashRand & 3) != 0);