Merge #14838: Use const in COutPoint class

cf4b0327ed Use std::numeric_limits<UNSIGNED>::max()) instead of (UNSIGNED)-1 (practicalswift)
6b82fc59eb Use const in COutPoint class (Hennadii Stepanov)

Pull request description:

  Refactoring:
  - all cases of using `(uint32_t) -1` in `COutPoint` class are replaced with const;
  - also all remaining instances of `(UNSIGNED)-1` transformed to `std::numeric_limits<UNSIGNED>::max()` (by @practicalswift).

Tree-SHA512: fc7fe9838b6e5136d8b97ea3d6f64c4aaa1215f4369832df432cab017396620bb6e30520a64180ceab6de222562ac11eab243a78dfa5a658ba018835a34caa19
pull/643/head
Wladimir J. van der Laan 6 years ago
commit 127b30cce8
No known key found for this signature in database
GPG Key ID: 1E4AED62986CD25D

@ -8,6 +8,7 @@
#include <assert.h> #include <assert.h>
#include <cstring> #include <cstring>
#include <limits>
#include <stdexcept> #include <stdexcept>
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
@ -189,7 +190,7 @@ public:
{ {
// prefix operator // prefix operator
int i = 0; int i = 0;
while (i < WIDTH && --pn[i] == (uint32_t)-1) while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
i++; i++;
return *this; return *this;
} }

@ -21,7 +21,9 @@ public:
uint256 hash; uint256 hash;
uint32_t n; uint32_t n;
COutPoint(): n((uint32_t) -1) { } static constexpr uint32_t NULL_INDEX = std::numeric_limits<uint32_t>::max();
COutPoint(): n(NULL_INDEX) { }
COutPoint(const uint256& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { } COutPoint(const uint256& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { }
ADD_SERIALIZE_METHODS; ADD_SERIALIZE_METHODS;
@ -32,8 +34,8 @@ public:
READWRITE(n); READWRITE(n);
} }
void SetNull() { hash.SetNull(); n = (uint32_t) -1; } void SetNull() { hash.SetNull(); n = NULL_INDEX; }
bool IsNull() const { return (hash.IsNull() && n == (uint32_t) -1); } bool IsNull() const { return (hash.IsNull() && n == NULL_INDEX); }
friend bool operator<(const COutPoint& a, const COutPoint& b) friend bool operator<(const COutPoint& a, const COutPoint& b)
{ {

@ -761,7 +761,7 @@ protected:
public: public:
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) : CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0) nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize, 0)
{ {
src = fileIn; src = fileIn;
} }
@ -846,7 +846,7 @@ public:
// prevent reading beyond a certain position // prevent reading beyond a certain position
// no argument removes the limit // no argument removes the limit
bool SetLimit(uint64_t nPos = (uint64_t)(-1)) { bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
if (nPos < nReadPos) if (nPos < nReadPos)
return false; return false;
nReadLimit = nPos; nReadLimit = nPos;

@ -200,7 +200,7 @@ BOOST_AUTO_TEST_CASE(varints)
} }
for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) { for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
uint64_t j = -1; uint64_t j = std::numeric_limits<uint64_t>::max();
ss >> VARINT(j); ss >> VARINT(j);
BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i); BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
} }

@ -105,7 +105,7 @@ void static RandomTransaction(CMutableTransaction &tx, bool fSingle) {
txin.prevout.hash = InsecureRand256(); txin.prevout.hash = InsecureRand256();
txin.prevout.n = InsecureRandBits(2); txin.prevout.n = InsecureRandBits(2);
RandomScript(txin.scriptSig); RandomScript(txin.scriptSig);
txin.nSequence = (InsecureRandBool()) ? InsecureRand32() : (unsigned int)-1; txin.nSequence = (InsecureRandBool()) ? InsecureRand32() : std::numeric_limits<uint32_t>::max();
} }
for (int out = 0; out < outs; out++) { for (int out = 0; out < outs; out++) {
tx.vout.push_back(CTxOut()); tx.vout.push_back(CTxOut());

Loading…
Cancel
Save