Cleanups, add comments, some fixes.

This commit is contained in:
JoelKatz
2012-04-10 14:36:53 -07:00
parent ad1ae6af23
commit 7ebd7a1845

View File

@@ -6,12 +6,18 @@
#include "SerializedTypes.h" #include "SerializedTypes.h"
// amount = value * [10 ^ offset]
// representation range is 10^80 - 10^(-80)
// on the wire, high 8 bits are (offset+142), low 56 bits are value
// value is zero if amount is zero, otherwise value is 10^15 - (10^16 - 1) inclusive
void STAmount::canonicalize() void STAmount::canonicalize()
{ {
if(value==0) if(value==0)
{ {
offset=0; offset=0;
value=0; value=0;
return;
} }
while(value<cMinValue) while(value<cMinValue)
{ {
@@ -20,7 +26,7 @@ void STAmount::canonicalize()
offset-=1; offset-=1;
} }
while(value>cMaxValue) while(value>cMaxValue)
{ // Here we can make it throw on precision loss if we wish: ((value%10)!=0) {
if(offset>=cMaxOffset) throw std::runtime_error("value underflow"); if(offset>=cMaxOffset) throw std::runtime_error("value underflow");
value/=10; value/=10;
offset+=1; offset+=1;
@@ -32,8 +38,10 @@ void STAmount::canonicalize()
STAmount* STAmount::construct(SerializerIterator& sit, const char *name) STAmount* STAmount::construct(SerializerIterator& sit, const char *name)
{ {
uint64 value = sit.get64(); uint64 value = sit.get64();
int offset = static_cast<int>(value>>(64-8));
value&=~(255ull<<(64-8)); int offset = static_cast<int>(value >> (64-8));
value &= ~(255ull << (64-8));
if(value==0) if(value==0)
{ {
if(offset!=0) if(offset!=0)
@@ -41,7 +49,7 @@ STAmount* STAmount::construct(SerializerIterator& sit, const char *name)
} }
else else
{ {
offset-=142; offset -= 142; // center the range
if( (value<cMinValue) || (value>cMaxValue) || (offset<cMinOffset) || (offset>cMaxOffset) ) if( (value<cMinValue) || (value>cMaxValue) || (offset<cMinOffset) || (offset>cMaxOffset) )
throw std::runtime_error("invalid currency value"); throw std::runtime_error("invalid currency value");
} }
@@ -49,7 +57,7 @@ STAmount* STAmount::construct(SerializerIterator& sit, const char *name)
} }
std::string STAmount::getText() const std::string STAmount::getText() const
{ { // This should be re-implemented to put a '.' in the string form of the integer value
std::ostringstream str; std::ostringstream str;
str << std::setprecision(16) << static_cast<double>(*this); str << std::setprecision(16) << static_cast<double>(*this);
return str.str(); return str.str();
@@ -57,44 +65,48 @@ std::string STAmount::getText() const
void STAmount::add(Serializer& s) const void STAmount::add(Serializer& s) const
{ {
uint64 v=value; if (value==0)
if(v!=0) v+=(static_cast<uint64>(offset+142) << (64-8)); s.add64(0);
s.add64(v); else
s.add64(value + (static_cast<uint64>(offset+142) << (64-8))));
} }
bool STAmount::isEquivalent(const SerializedType& t) const bool STAmount::isEquivalent(const SerializedType& t) const
{ {
const STAmount* v=dynamic_cast<const STAmount*>(&t); const STAmount* v = dynamic_cast<const STAmount*>(&t);
if(!v) return false; if(!v) return false;
return (value==v->value) && (offset==v->offset); return (value == v->value) && (offset == v->offset);
} }
bool STAmount::operator==(const STAmount& a) const bool STAmount::operator==(const STAmount& a) const
{ {
return (offset==a.offset) && (value==a.value); return (offset == a.offset) && (value == a.value);
} }
bool STAmount::operator!=(const STAmount& a) const bool STAmount::operator!=(const STAmount& a) const
{ {
return (offset!=a.offset) || (value!=a.value); return (offset != a.offset) || (value != a.value);
} }
bool STAmount::operator<(const STAmount& a) const bool STAmount::operator<(const STAmount& a) const
{ {
if(offset<a.offset) return true; if(value == 0) return false;
if(a.offset<offset) return false; if(offset < a.offset) return true;
if(a.offset < offset) return false;
return value < a.value; return value < a.value;
} }
bool STAmount::operator>(const STAmount& a) const bool STAmount::operator>(const STAmount& a) const
{ {
if(offset>a.offset) return true; if(value == 0) return a.value != 0;
if(a.offset>offset) return false; if(offset > a.offset) return true;
if(a.offset > offset) return false;
return value > a.value; return value > a.value;
} }
bool STAmount::operator<=(const STAmount& a) const bool STAmount::operator<=(const STAmount& a) const
{ {
if(value == 0) return a.value== 0;
if(offset<a.offset) return true; if(offset<a.offset) return true;
if(a.offset<offset) return false; if(a.offset<offset) return false;
return value <= a.value; return value <= a.value;
@@ -102,6 +114,7 @@ bool STAmount::operator<=(const STAmount& a) const
bool STAmount::operator>=(const STAmount& a) const bool STAmount::operator>=(const STAmount& a) const
{ {
if(value == 0) return true;
if(offset>a.offset) return true; if(offset>a.offset) return true;
if(a.offset>offset) return false; if(a.offset>offset) return false;
return value >= a.value; return value >= a.value;
@@ -175,7 +188,7 @@ STAmount operator-(STAmount v1, STAmount v2)
v2.value/=10; v2.value/=10;
v2.offset+=1; v2.offset+=1;
} }
if(v1.value < v2.value) throw std::runtime_error("value overflow"); if(v1.value < v2.value) throw std::runtime_error("value underflow");
return STAmount(v1.name, v1.value - v2.value, v1.offset); return STAmount(v1.name, v1.value - v2.value, v1.offset);
} }
@@ -183,22 +196,21 @@ STAmount getRate(const STAmount& offerIn, const STAmount& offerOut)
{ {
CBigNum numerator, denominator, quotient; CBigNum numerator, denominator, quotient;
if(offerOut.value==0) throw std::runtime_error("illegal offer"); if(offerOut.value == 0) throw std::runtime_error("illegal offer");
if(offerIn.value==0) return STAmount(); if(offerIn.value == 0) return STAmount();
if( (BN_zero(&numerator)!=1) || (BN_zero(&denominator)!=1) || // Compute (numerator * 10^16) / denominator
(BN_add_word(&numerator, offerIn.value)!=1) || if( (BN_zero(&numerator) != 1) || (BN_zero(&denominator) != 1) ||
(BN_add_word(&denominator, offerOut.value)!=1) || (BN_add_word(&numerator, offerIn.value) != 1) ||
(BN_mul_word(&numerator, 1000000000000000ull)!=1) || (BN_add_word(&denominator, offerOut.value) != 1) ||
(BN_div(&quotient, NULL, &numerator, &denominator, CAutoBN_CTX())!=1) ) (BN_mul_word(&numerator, 10000000000000000ull) != 1) ||
throw std::runtime_error("internal bn error"); (BN_div(&quotient, NULL, &numerator, &denominator, CAutoBN_CTX()) != 1) )
int offset=offerIn.offset - offerOut.offset - 15;
while(BN_num_bits(&quotient)>60)
{ {
offset+=3; throw std::runtime_error("internal bn error");
BN_div_word(&quotient, 1000);
} }
return STAmount(quotient.getulong(), offset);
// 10^15 <= quotient <= 10^17
assert(BN_num_bytes(&quotient)<=60);
return STAmount(quotient.getulong(), offerIn.offset - offerOut.offset - 16);
} }