mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Tidy up some STAmount and base_uint operations:
* Constrain use of arithmetic operators in STAmount * Prevent constructor conversion of uint256 to uint128 - make intent clear * Prevent implicit conversion of uint64_t to uint256 * Prevent implicit conversion of uint64_t to uint160
This commit is contained in:
committed by
Vinnie Falco
parent
6ae329f4a6
commit
912d74e805
@@ -31,6 +31,12 @@ namespace ripple {
|
||||
|
||||
class uint128 : public base_uint128
|
||||
{
|
||||
private:
|
||||
uint128 (value_type const* ptr)
|
||||
{
|
||||
memcpy (pn, ptr, bytes);
|
||||
}
|
||||
|
||||
public:
|
||||
typedef base_uint128 basetype;
|
||||
|
||||
@@ -52,15 +58,6 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
// VFALCO NOTE This looks dangerous and wouldn't be obvious at call
|
||||
// sites what is being performed here.
|
||||
//
|
||||
explicit uint128 (const base_uint256& b)
|
||||
{
|
||||
for (int i = 0; i < WIDTH; i++)
|
||||
pn[i] = b.pn[i];
|
||||
}
|
||||
|
||||
explicit uint128 (Blob const& vch)
|
||||
{
|
||||
if (vch.size () == size ())
|
||||
@@ -69,6 +66,10 @@ public:
|
||||
zero ();
|
||||
}
|
||||
|
||||
static uint128 low128(base_uint256 const& b)
|
||||
{
|
||||
return uint128 (b.data());
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
uint160 (std::uint64_t b)
|
||||
explicit uint160 (std::uint64_t b)
|
||||
{
|
||||
*this = b;
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
uint256 (std::uint64_t b)
|
||||
explicit uint256 (std::uint64_t b)
|
||||
{
|
||||
*this = b;
|
||||
}
|
||||
|
||||
@@ -50,6 +50,9 @@ inline int Testuint256AdHoc (std::vector<std::string> vArg);
|
||||
template<unsigned int BITS>
|
||||
class base_uint
|
||||
{
|
||||
static_assert ((BITS % 32) == 0,
|
||||
"The length of a base_uint must be a multiple of 32.");
|
||||
|
||||
protected:
|
||||
enum { WIDTH = BITS / 32 };
|
||||
|
||||
@@ -129,9 +132,6 @@ protected:
|
||||
*/
|
||||
base_uint (void const* data, FromVoid)
|
||||
{
|
||||
// BITS must be a multiple of 32
|
||||
static_bassert ((BITS % 32) == 0);
|
||||
|
||||
memcpy (&pn [0], data, BITS / 8);
|
||||
}
|
||||
public:
|
||||
|
||||
@@ -70,7 +70,7 @@ public:
|
||||
}
|
||||
virtual uint160 getUint160 ()
|
||||
{
|
||||
return (0);
|
||||
return uint160(0);
|
||||
}
|
||||
|
||||
//virtual bool isCurrency(){ return(false); }
|
||||
|
||||
@@ -30,7 +30,7 @@ uint128 CKey::PassPhraseToKey (const std::string& passPhrase)
|
||||
|
||||
s.addRaw (passPhrase);
|
||||
uint256 hash256 = s.getSHA512Half ();
|
||||
uint128 ret (hash256);
|
||||
uint128 ret (uint128::low128(hash256));
|
||||
|
||||
s.secureErase ();
|
||||
|
||||
|
||||
@@ -765,19 +765,23 @@ STAmount& STAmount::operator= (std::uint64_t v)
|
||||
|
||||
STAmount& STAmount::operator+= (std::uint64_t v)
|
||||
{
|
||||
if (mIsNative)
|
||||
setSNValue (getSNValue () + static_cast<std::int64_t> (v));
|
||||
else *this += STAmount (mCurrency, v);
|
||||
assert (mIsNative);
|
||||
|
||||
if (!mIsNative)
|
||||
throw std::runtime_error ("not native");
|
||||
|
||||
setSNValue (getSNValue () + static_cast<std::int64_t> (v));
|
||||
return *this;
|
||||
}
|
||||
|
||||
STAmount& STAmount::operator-= (std::uint64_t v)
|
||||
{
|
||||
if (mIsNative)
|
||||
setSNValue (getSNValue () - static_cast<std::int64_t> (v));
|
||||
else *this -= STAmount (mCurrency, v);
|
||||
assert (mIsNative);
|
||||
|
||||
if (!mIsNative)
|
||||
throw std::runtime_error ("not native");
|
||||
|
||||
setSNValue (getSNValue () - static_cast<std::int64_t> (v));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user