From 0c4b739010f8f038fa9fcd28e35c626cc6e5d073 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Tue, 10 Jul 2012 21:20:09 -0700 Subject: [PATCH 1/3] Fix RPC decimal parsing. --- src/Amount.cpp | 18 +++++++++++++++--- src/RPCServer.cpp | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Amount.cpp b/src/Amount.cpp index dcce9a8bd8..32a556a0e1 100644 --- a/src/Amount.cpp +++ b/src/Amount.cpp @@ -88,19 +88,32 @@ std::string STAmount::getCurrencyHuman() } // Not meant to be the ultimate parser. For use by RPC which is supposed to be sane and trusted. +// Native has special handling: +// - Integer values are in base units. +// - Float values are in float units. +// - To avoid a mistake float value for native are specified with a "^" in place of a "." bool STAmount::setValue(const std::string& sAmount, const std::string& sCurrency) { if (!currencyFromString(mCurrency, sCurrency)) return false; + mIsNative = !mCurrency; + uint64 uValue; int iOffset; - size_t uDecimal = sAmount.find_first_of("^"); + size_t uDecimal = sAmount.find_first_of(mIsNative ? "^" : "."); bool bInteger = uDecimal == std::string::npos; if (bInteger) { - uValue = sAmount.empty() ? 0 : boost::lexical_cast(sAmount); + try + { + uValue = sAmount.empty() ? 0 : boost::lexical_cast(sAmount); + } + catch (...) + { + return false; + } iOffset = 0; } else @@ -125,7 +138,6 @@ bool STAmount::setValue(const std::string& sAmount, const std::string& sCurrency uValue += uFraction; } - mIsNative = !mCurrency; if (mIsNative) { if (bInteger) diff --git a/src/RPCServer.cpp b/src/RPCServer.cpp index bb6afb04ee..afcd9e3c39 100644 --- a/src/RPCServer.cpp +++ b/src/RPCServer.cpp @@ -1124,6 +1124,8 @@ Json::Value RPCServer::doRippleLinesGet(const Json::Value ¶ms) jPeer["node"] = uNode.ToString(); jPeer["account"] = rsLine->getAccountIDPeer().humanAccountID(); + // Amount reported is positive if current account hold's other account's IOUs. + // Amount reported is negative if other account hold's current account's IOUs. jPeer["balance"] = saBalance.getText(); jPeer["currency"] = saBalance.getCurrencyHuman(); jPeer["limit"] = saLimit.getJson(0); From 8ae768d2a79bb2de62dbca0bfffe4b5d0b17da5f Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Tue, 10 Jul 2012 21:38:41 -0700 Subject: [PATCH 2/3] Add hack to show currency in Json for STAmount. --- src/Amount.cpp | 18 ++++++++++++++++-- src/SerializedTypes.h | 4 +++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Amount.cpp b/src/Amount.cpp index 32a556a0e1..ac987ce567 100644 --- a/src/Amount.cpp +++ b/src/Amount.cpp @@ -44,7 +44,8 @@ bool STAmount::currencyFromString(uint160& uDstCurrency, const std::string& sCur return bSuccess; } -std::string STAmount::getCurrencyHuman() +// XXX Broken for custom currencies? +std::string STAmount::getCurrencyHuman() const { std::string sCurrency; @@ -823,6 +824,20 @@ STAmount STAmount::deserialize(SerializerIterator& it) return ret; } +Json::Value STAmount::getJson(int) const +{ + Json::Value elem(Json::objectValue); + + elem["value"] = getText(); + + // This is a hack, many places don't specify a currency. STAmount is used just as a value. + if (!mIsNative) + elem["currency"] = getCurrencyHuman(); + + return elem; +} + +// For unit tests: static STAmount serdes(const STAmount &s) { Serializer ser; @@ -834,7 +849,6 @@ static STAmount serdes(const STAmount &s) return STAmount::deserialize(sit); } - BOOST_AUTO_TEST_SUITE(amount) BOOST_AUTO_TEST_CASE( setValue_test ) diff --git a/src/SerializedTypes.h b/src/SerializedTypes.h index 9b97202850..3d8bd3f7f5 100644 --- a/src/SerializedTypes.h +++ b/src/SerializedTypes.h @@ -271,7 +271,7 @@ public: int64 getSNValue() const; void setSNValue(int64); - std::string getCurrencyHuman(); + std::string getCurrencyHuman() const; bool isNative() const { return mIsNative; } bool isZero() const { return mValue == 0; } @@ -342,6 +342,8 @@ public: static STAmount deserialize(SerializerIterator&); static bool currencyFromString(uint160& uDstCurrency, const std::string& sCurrency); + + Json::Value getJson(int) const; }; class STHash128 : public SerializedType From 8266c87dc9124ab0c84a4a59985b06feb4829e6c Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Tue, 10 Jul 2012 21:44:11 -0700 Subject: [PATCH 3/3] Have RPC ripple_lines_get now show redundant currencies. --- src/RPCServer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/RPCServer.cpp b/src/RPCServer.cpp index afcd9e3c39..b5acd690cf 100644 --- a/src/RPCServer.cpp +++ b/src/RPCServer.cpp @@ -1128,8 +1128,8 @@ Json::Value RPCServer::doRippleLinesGet(const Json::Value ¶ms) // Amount reported is negative if other account hold's current account's IOUs. jPeer["balance"] = saBalance.getText(); jPeer["currency"] = saBalance.getCurrencyHuman(); - jPeer["limit"] = saLimit.getJson(0); - jPeer["limit_peer"] = saLimitPeer.getJson(0); + jPeer["limit"] = saLimit.getText(); + jPeer["limit_peer"] = saLimitPeer.getText(); jsonLines.append(jPeer); }