From bfee7a30828eec866ef22880ec20b8653f65fa4f Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Fri, 12 Oct 2012 18:24:32 -0700 Subject: [PATCH] I apologize for the disaster that was toUInt64. It has been nuked from orbit. --- src/Amount.cpp | 45 ++++++++++++++++++++++++++----------------- src/SerializedTypes.h | 2 +- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/Amount.cpp b/src/Amount.cpp index ae518f8ad..9a12ea7e2 100644 --- a/src/Amount.cpp +++ b/src/Amount.cpp @@ -378,7 +378,7 @@ bool STAmount::setFullValue(const std::string& sAmount, const std::string& sCurr void STAmount::canonicalize() { - if (!mCurrency) + if (mCurrency.isZero()) { // native currency amounts should always have an offset of zero mIsNative = true; @@ -401,7 +401,11 @@ void STAmount::canonicalize() --mOffset; } - assert(mValue <= cMaxNative); + if (mValue > cMaxNative) + { + assert(false); + throw std::runtime_error("Native currency amount out of range"); + } return; } @@ -430,9 +434,9 @@ void STAmount::canonicalize() mValue /= 10; ++mOffset; } - assert((mValue == 0) || ((mValue >= cMinValue) && (mValue <= cMaxValue)) ); - assert((mValue == 0) || ((mOffset >= cMinOffset) && (mOffset <= cMaxOffset)) ); - assert((mValue != 0) || (mOffset != -100) ); + assert((mValue == 0) || ((mValue >= cMinValue) && (mValue <= cMaxValue))); + assert((mValue == 0) || ((mOffset >= cMinOffset) && (mOffset <= cMaxOffset))); + assert((mValue != 0) || (mOffset != -100)); } void STAmount::add(Serializer& s) const @@ -476,15 +480,20 @@ void STAmount::setValue(const STAmount &a) mIsNegative = a.mIsNegative; } -uint64 STAmount::toUInt64() const -{ // makes them sort easily - if (mIsNative) - return mValue; - if (mValue == 0) - return 0x4000000000000000ull; - if (mIsNegative) - return ((cMaxNative + 1) - mValue) | (static_cast(mOffset + 97) << (64 - 10)); - return mValue | (static_cast(mOffset + 256 + 97) << (64 - 10)); +int STAmount::compare(const STAmount& a) const +{ // Compares the value of a to the value of this STAmount, amounts must be comparable + if (mIsNegative != a.mIsNegative) return mIsNegative ? -1 : 1; + + if (!mValue) return a.mValue ? 1 : 0; + if (!a.mValue) return 1; + + if (mOffset > a.mOffset) return mIsNegative ? 1 : -1; + if (mOffset < a.mOffset) return mIsNegative ? -1 : 1; + + if (mValue > a.mValue) return mIsNegative ? 1 : -1; + if (mValue < a.mValue) return mIsNegative ? -1 : 1; + + return 0; } STAmount* STAmount::construct(SerializerIterator& sit, SField::ref name) @@ -633,25 +642,25 @@ bool STAmount::operator!=(const STAmount& a) const bool STAmount::operator<(const STAmount& a) const { throwComparable(a); - return toUInt64() < a.toUInt64(); + return compare(a) < 0; } bool STAmount::operator>(const STAmount& a) const { throwComparable(a); - return toUInt64() > a.toUInt64(); + return compare(a) > 0; } bool STAmount::operator<=(const STAmount& a) const { throwComparable(a); - return toUInt64() <= a.toUInt64(); + return compare(a) <= 0; } bool STAmount::operator>=(const STAmount& a) const { throwComparable(a); - return toUInt64() >= a.toUInt64(); + return compare(a) >= 0; } STAmount& STAmount::operator+=(const STAmount& a) diff --git a/src/SerializedTypes.h b/src/SerializedTypes.h index 90438442a..d7df1d1b7 100644 --- a/src/SerializedTypes.h +++ b/src/SerializedTypes.h @@ -224,7 +224,6 @@ protected: : SerializedType(name), mCurrency(cur), mIssuer(iss), mValue(val), mOffset(off), mIsNative(isNat), mIsNegative(isNeg) { ; } - uint64 toUInt64() const; static uint64 muldiv(uint64, uint64, uint64); public: @@ -287,6 +286,7 @@ public: void negate() { if (!isZero()) mIsNegative = !mIsNegative; } void zero() { mOffset = mIsNative ? -100 : 0; mValue = 0; mIsNegative = false; } + int compare(const STAmount&) const; const uint160& getIssuer() const { return mIssuer; } void setIssuer(const uint160& uIssuer) { mIssuer = uIssuer; }