Merge pull request #1106 from jgarzik/sign-compare
authorGavin Andresen <gavinandresen@gmail.com>
Tue, 17 Apr 2012 17:55:56 +0000 (10:55 -0700)
committerGavin Andresen <gavinandresen@gmail.com>
Tue, 17 Apr 2012 17:55:56 +0000 (10:55 -0700)
Fix many sign-comparison warnings found in bitcoin codebase

1  2 
src/main.cpp
src/net.h
src/serialize.h

diff --combined src/main.cpp
@@@ -288,7 -288,7 +288,7 @@@ bool CTransaction::AreInputsStandard(co
      if (IsCoinBase())
          return true; // Coinbases don't use vin normally
  
-     for (int i = 0; i < vin.size(); i++)
+     for (unsigned int i = 0; i < vin.size(); i++)
      {
          const CTxOut& prev = GetOutputFor(vin[i], mapInputs);
  
@@@ -484,7 -484,7 +484,7 @@@ bool CTransaction::AcceptToMemoryPool(C
  
      // Check for conflicts with in-memory transactions
      CTransaction* ptxOld = NULL;
-     for (int i = 0; i < vin.size(); i++)
+     for (unsigned int i = 0; i < vin.size(); i++)
      {
          COutPoint outpoint = vin[i].prevout;
          if (mapNextTx.count(outpoint))
                  return false;
              if (!IsNewerThan(*ptxOld))
                  return false;
-             for (int i = 0; i < vin.size(); i++)
+             for (unsigned int i = 0; i < vin.size(); i++)
              {
                  COutPoint outpoint = vin[i].prevout;
                  if (!mapNextTx.count(outpoint) || mapNextTx[outpoint].ptx != ptxOld)
@@@ -603,7 -603,7 +603,7 @@@ bool CTransaction::AddToMemoryPoolUnche
          LOCK(cs_mapTransactions);
          uint256 hash = GetHash();
          mapTransactions[hash] = *this;
-         for (int i = 0; i < vin.size(); i++)
+         for (unsigned int i = 0; i < vin.size(); i++)
              mapNextTx[vin[i].prevout] = CInPoint(&mapTransactions[hash], i);
          nTransactionsUpdated++;
          ++nPooledTx;
@@@ -989,7 -989,7 +989,7 @@@ bool CTransaction::FetchInputs(CTxDB& t
      if (IsCoinBase())
          return true; // Coinbase transactions have no inputs to fetch.
  
-     for (int i = 0; i < vin.size(); i++)
+     for (unsigned int i = 0; i < vin.size(); i++)
      {
          COutPoint prevout = vin[i].prevout;
          if (inputsRet.count(prevout.hash))
      }
  
      // Make sure all prevout.n's are valid:
-     for (int i = 0; i < vin.size(); i++)
+     for (unsigned int i = 0; i < vin.size(); i++)
      {
          const COutPoint prevout = vin[i].prevout;
          assert(inputsRet.count(prevout.hash) != 0);
@@@ -1071,7 -1071,7 +1071,7 @@@ int64 CTransaction::GetValueIn(const Ma
          return 0;
  
      int64 nResult = 0;
-     for (int i = 0; i < vin.size(); i++)
+     for (unsigned int i = 0; i < vin.size(); i++)
      {
          nResult += GetOutputFor(vin[i], inputs).nValue;
      }
@@@ -1085,7 -1085,7 +1085,7 @@@ int CTransaction::GetP2SHSigOpCount(con
          return 0;
  
      int nSigOps = 0;
-     for (int i = 0; i < vin.size(); i++)
+     for (unsigned int i = 0; i < vin.size(); i++)
      {
          const CTxOut& prevout = GetOutputFor(vin[i], inputs);
          if (prevout.scriptPubKey.IsPayToScriptHash())
@@@ -1106,7 -1106,7 +1106,7 @@@ bool CTransaction::ConnectInputs(MapPre
      {
          int64 nValueIn = 0;
          int64 nFees = 0;
-         for (int i = 0; i < vin.size(); i++)
+         for (unsigned int i = 0; i < vin.size(); i++)
          {
              COutPoint prevout = vin[i].prevout;
              assert(inputs.count(prevout.hash) > 0);
@@@ -1185,7 -1185,7 +1185,7 @@@ bool CTransaction::ClientConnectInputs(
      {
          LOCK(cs_mapTransactions);
          int64 nValueIn = 0;
-         for (int i = 0; i < vin.size(); i++)
+         for (unsigned int i = 0; i < vin.size(); i++)
          {
              // Get prev tx from single transactions in memory
              COutPoint prevout = vin[i].prevout;
@@@ -1396,7 -1396,7 +1396,7 @@@ bool static Reorganize(CTxDB& txdb, CBl
  
      // Connect longer branch
      vector<CTransaction> vDelete;
-     for (int i = 0; i < vConnect.size(); i++)
+     for (unsigned int i = 0; i < vConnect.size(); i++)
      {
          CBlockIndex* pindex = vConnect[i];
          CBlock block;
@@@ -1635,7 -1635,7 +1635,7 @@@ bool CBlock::CheckBlock() cons
      // First transaction must be coinbase, the rest must not be
      if (vtx.empty() || !vtx[0].IsCoinBase())
          return DoS(100, error("CheckBlock() : first tx is not coinbase"));
-     for (int i = 1; i < vtx.size(); i++)
+     for (unsigned int i = 1; i < vtx.size(); i++)
          if (vtx[i].IsCoinBase())
              return DoS(100, error("CheckBlock() : more than one coinbase"));
  
@@@ -1771,7 -1771,7 +1771,7 @@@ bool ProcessBlock(CNode* pfrom, CBlock
      // Recursively process any orphan blocks that depended on this one
      vector<uint256> vWorkQueue;
      vWorkQueue.push_back(hash);
-     for (int i = 0; i < vWorkQueue.size(); i++)
+     for (unsigned int i = 0; i < vWorkQueue.size(); i++)
      {
          uint256 hashPrev = vWorkQueue[i];
          for (multimap<uint256, CBlock*>::iterator mi = mapOrphanBlocksByPrev.lower_bound(hashPrev);
@@@ -1995,7 -1995,7 +1995,7 @@@ void PrintBlockTree(
  
          // put the main timechain first
          vector<CBlockIndex*>& vNext = mapNext[pindex];
-         for (int i = 0; i < vNext.size(); i++)
+         for (unsigned int i = 0; i < vNext.size(); i++)
          {
              if (vNext[i]->pnext)
              {
          }
  
          // iterate children
-         for (int i = 0; i < vNext.size(); i++)
+         for (unsigned int i = 0; i < vNext.size(); i++)
              vStack.push_back(make_pair(nCol+i, vNext[i]));
      }
  }
@@@ -2135,21 -2135,8 +2135,21 @@@ bool static AlreadyHave(CTxDB& txdb, co
  {
      switch (inv.type)
      {
 -    case MSG_TX:    return mapTransactions.count(inv.hash) || mapOrphanTransactions.count(inv.hash) || txdb.ContainsTx(inv.hash);
 -    case MSG_BLOCK: return mapBlockIndex.count(inv.hash) || mapOrphanBlocks.count(inv.hash);
 +    case MSG_TX:
 +        {
 +        bool txInMap = false;
 +            {
 +            LOCK(cs_mapTransactions);
 +            txInMap = (mapTransactions.count(inv.hash) != 0);
 +            }
 +        return txInMap ||
 +               mapOrphanTransactions.count(inv.hash) ||
 +               txdb.ContainsTx(inv.hash);
 +        }
 +
 +    case MSG_BLOCK:
 +        return mapBlockIndex.count(inv.hash) ||
 +               mapOrphanBlocks.count(inv.hash);
      }
      // Don't know what it is, just say we already got one
      return true;
@@@ -2196,7 -2183,7 +2196,7 @@@ bool static ProcessMessage(CNode* pfrom
          CAddress addrFrom;
          uint64 nNonce = 1;
          vRecv >> pfrom->nVersion >> pfrom->nServices >> nTime >> addrMe;
 -        if (pfrom->nVersion < 209)
 +        if (pfrom->nVersion < MIN_PROTO_VERSION)
          {
              // Since February 20, 2012, the protocol is initiated at version 209,
              // and earlier versions are no longer supported
              }
  
              // Get recent addresses
 -            if (pfrom->nVersion >= 31402 || addrman.size() < 1000)
 +            if (pfrom->nVersion >= CADDR_TIME_VERSION || addrman.size() < 1000)
              {
                  pfrom->PushMessage("getaddr");
                  pfrom->fGetAddr = true;
          // Ask the first connected node for block updates
          static int nAskedForBlocks = 0;
          if (!pfrom->fClient &&
 -            (pfrom->nVersion < 32000 || pfrom->nVersion >= 32400) &&
 +            (pfrom->nVersion < NOBLKS_VERSION_START ||
 +             pfrom->nVersion >= NOBLKS_VERSION_END) &&
               (nAskedForBlocks < 1 || vNodes.size() <= 1))
          {
              nAskedForBlocks++;
          vRecv >> vAddr;
  
          // Don't want addr from older versions unless seeding
 -        if (pfrom->nVersion < 31402 && addrman.size() > 1000)
 +        if (pfrom->nVersion < CADDR_TIME_VERSION && addrman.size() > 1000)
              return true;
          if (vAddr.size() > 1000)
          {
                      multimap<uint256, CNode*> mapMix;
                      BOOST_FOREACH(CNode* pnode, vNodes)
                      {
 -                        if (pnode->nVersion < 31402)
 +                        if (pnode->nVersion < CADDR_TIME_VERSION)
                              continue;
                          unsigned int nPointer;
                          memcpy(&nPointer, &pnode, sizeof(nPointer));
          }
  
          CTxDB txdb("r");
-         for (int nInv = 0; nInv < vInv.size(); nInv++)
+         for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
          {
              const CInv &inv = vInv[nInv];
  
              vWorkQueue.push_back(inv.hash);
  
              // Recursively process any orphan transactions that depended on this one
-             for (int i = 0; i < vWorkQueue.size(); i++)
+             for (unsigned int i = 0; i < vWorkQueue.size(); i++)
              {
                  uint256 hashPrev = vWorkQueue[i];
                  for (multimap<uint256, CDataStream*>::iterator mi = mapOrphanTransactionsByPrev.lower_bound(hashPrev);
  
      else if (strCommand == "ping")
      {
 +        if (pfrom->nVersion > BIP0031_VERSION)
 +        {
 +            uint64 nonce = 0;
 +            vRecv >> nonce;
 +            // Echo the message back with the nonce. This allows for two useful features:
 +            //
 +            // 1) A remote node can quickly check if the connection is operational
 +            // 2) Remote nodes can measure the latency of the network thread. If this node
 +            //    is overloaded it won't respond to pings quickly and the remote node can
 +            //    avoid sending us more work, like chain download requests.
 +            //
 +            // The nonce stops the remote getting confused between different pings: without
 +            // it, if the remote node sends a ping once per second and this node takes 5
 +            // seconds to respond to each, the 5th ping the remote sends would appear to
 +            // return very quickly.
 +            pfrom->PushMessage("pong", nonce);
 +        }
      }
  
  
@@@ -2844,14 -2813,9 +2844,14 @@@ bool SendMessages(CNode* pto, bool fSen
          if (pto->nVersion == 0)
              return true;
  
 -        // Keep-alive ping
 -        if (pto->nLastSend && GetTime() - pto->nLastSend > 30 * 60 && pto->vSend.empty())
 -            pto->PushMessage("ping");
 +        // Keep-alive ping. We send a nonce of zero because we don't use it anywhere 
 +        // right now.
 +        if (pto->nLastSend && GetTime() - pto->nLastSend > 30 * 60 && pto->vSend.empty()) {
 +            if (pto->nVersion > BIP0031_VERSION)
 +                pto->PushMessage("ping", 0);
 +            else
 +                pto->PushMessage("ping");
 +        }
  
          // Resend wallet transactions that haven't gotten in a block yet
          ResendWalletTransactions();
@@@ -3330,7 -3294,7 +3330,7 @@@ void FormatHashBuffers(CBlock* pblock, 
      FormatHashBlocks(&tmp.hash1, sizeof(tmp.hash1));
  
      // Byte swap all the input buffer
-     for (int i = 0; i < sizeof(tmp)/4; i++)
+     for (unsigned int i = 0; i < sizeof(tmp)/4; i++)
          ((unsigned int*)&tmp)[i] = ByteReverse(((unsigned int*)&tmp)[i]);
  
      // Precalc the first half of the first hash, which stays constant
@@@ -3455,7 -3419,7 +3455,7 @@@ void static BitcoinMiner(CWallet *pwall
              // Check if something found
              if (nNonceFound != -1)
              {
-                 for (int i = 0; i < sizeof(hash)/4; i++)
+                 for (unsigned int i = 0; i < sizeof(hash)/4; i++)
                      ((unsigned int*)&hash)[i] = ByteReverse(((unsigned int*)&hash)[i]);
  
                  if (hash <= hashTarget)
diff --combined src/net.h
+++ b/src/net.h
@@@ -117,7 -117,7 +117,7 @@@ public
      int64 nLastRecv;
      int64 nLastSendEmpty;
      int64 nTimeConnected;
-     unsigned int nHeaderStart;
+     int nHeaderStart;
      unsigned int nMessageStart;
      CAddress addr;
      int nVersion;
@@@ -163,8 -163,8 +163,8 @@@ public
          hSocket = hSocketIn;
          vSend.SetType(SER_NETWORK);
          vRecv.SetType(SER_NETWORK);
 -        vSend.SetVersion(209);
 -        vRecv.SetVersion(209);
 +        vSend.SetVersion(MIN_PROTO_VERSION);
 +        vRecv.SetVersion(MIN_PROTO_VERSION);
          nLastSend = 0;
          nLastRecv = 0;
          nLastSendEmpty = GetTime();
@@@ -299,7 -299,7 +299,7 @@@ public
  
      void AbortMessage()
      {
-         if (nHeaderStart == -1)
+         if (nHeaderStart < 0)
              return;
          vSend.resize(nHeaderStart);
          nHeaderStart = -1;
              return;
          }
  
-         if (nHeaderStart == -1)
+         if (nHeaderStart < 0)
              return;
  
          // Set the size
  
      void EndMessageAbortIfEmpty()
      {
-         if (nHeaderStart == -1)
+         if (nHeaderStart < 0)
              return;
          int nSize = vSend.size() - nMessageStart;
          if (nSize > 0)
diff --combined src/serialize.h
@@@ -20,7 -20,6 +20,7 @@@
  #include <boost/tuple/tuple_io.hpp>
  
  #include "allocators.h"
 +#include "version.h"
  
  typedef long long  int64;
  typedef unsigned long long  uint64;
@@@ -30,6 -29,8 +30,6 @@@ class CDataStream
  class CAutoFile;
  static const unsigned int MAX_SIZE = 0x02000000;
  
 -static const int PROTOCOL_VERSION = 60000;
 -
  // Used to bypass the rule against non-const reference to temporary
  // where it makes sense with wrappers such as CFlatData or CTxDB
  template<typename T>
@@@ -275,6 -276,48 +275,6 @@@ public
      }
  };
  
 -
 -
 -/** string stored as a fixed length field */
 -template<std::size_t LEN>
 -class CFixedFieldString
 -{
 -protected:
 -    const std::string* pcstr;
 -    std::string* pstr;
 -public:
 -    explicit CFixedFieldString(const std::string& str) : pcstr(&str), pstr(NULL) { }
 -    explicit CFixedFieldString(std::string& str) : pcstr(&str), pstr(&str) { }
 -
 -    unsigned int GetSerializeSize(int, int=0) const
 -    {
 -        return LEN;
 -    }
 -
 -    template<typename Stream>
 -    void Serialize(Stream& s, int, int=0) const
 -    {
 -        char pszBuf[LEN];
 -        strncpy(pszBuf, pcstr->c_str(), LEN);
 -        s.write(pszBuf, LEN);
 -    }
 -
 -    template<typename Stream>
 -    void Unserialize(Stream& s, int, int=0)
 -    {
 -        if (pstr == NULL)
 -            throw std::ios_base::failure("CFixedFieldString::Unserialize : trying to unserialize to const string");
 -        char pszBuf[LEN+1];
 -        s.read(pszBuf, LEN);
 -        pszBuf[LEN] = '\0';
 -        *pstr = pszBuf;
 -    }
 -};
 -
 -
 -
 -
 -
  //
  // Forward declarations
  //
@@@ -436,6 -479,10 +436,6 @@@ inline void Serialize(Stream& os, cons
  template<typename Stream, typename T, typename A>
  void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
  {
 -    //unsigned int nSize = ReadCompactSize(is);
 -    //v.resize(nSize);
 -    //is.read((char*)&v[0], nSize * sizeof(T));
 -
      // Limit size per read so bogus size value won't cause out of memory
      v.clear();
      unsigned int nSize = ReadCompactSize(is);
  template<typename Stream, typename T, typename A>
  void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
  {
 -    //unsigned int nSize = ReadCompactSize(is);
 -    //v.resize(nSize);
 -    //for (std::vector<T, A>::iterator vi = v.begin(); vi != v.end(); ++vi)
 -    //    Unserialize(is, (*vi), nType, nVersion);
 -
      v.clear();
      unsigned int nSize = ReadCompactSize(is);
      unsigned int i = 0;
@@@ -1138,7 -1190,7 +1138,7 @@@ public
      void ReadVersion()           { *this >> nVersion; }
      void WriteVersion()          { *this << nVersion; }
  
-     CAutoFile& read(char* pch, int nSize)
+     CAutoFile& read(char* pch, size_t nSize)
      {
          if (!file)
              throw std::ios_base::failure("CAutoFile::read : file handle is NULL");
          return (*this);
      }
  
-     CAutoFile& write(const char* pch, int nSize)
+     CAutoFile& write(const char* pch, size_t nSize)
      {
          if (!file)
              throw std::ios_base::failure("CAutoFile::write : file handle is NULL");