Levelization, improve structure of source files:

Source files are moved between modules, includes changed and added,
and some code rewritten, with the goal of reducing cross-module dependencies
and eliminating cycles in the dependency graph of classes.

* Remove RippleAddress dependency in CKey_test
* ByteOrder.h, Blob.h, and strHex.h are moved to basics/. This makes
  the basics/ module fully independent of other ripple sources.
* types/ is merged into protocol/. The protocol module now contains
  all primitive types specific to the Ripple protocol.
* Move ErrorCodes to protocol/
* Move base_uint to basics/
* Move Base58 to crypto/
* Remove dependence on Serializer in GenerateDeterministicKey
* Eliminate unity header json.h
* Remove obsolete unity headers
* Remove unnecessary includes
This commit is contained in:
Vinnie Falco
2014-12-19 08:57:30 -08:00
parent e3fbb83ad0
commit 60f27178b8
109 changed files with 859 additions and 493 deletions

View File

@@ -0,0 +1,72 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifdef BEAST_WIN32
#include <Winsock2.h>
// <Winsock2.h> defines min, max and does other stupid things
# ifdef max
# undef max
# endif
# ifdef min
# undef min
# endif
#endif
namespace ripple {
#if BEAST_WIN32
// from: http://stackoverflow.com/questions/3022552/is-there-any-standard-htonl-like-function-for-64-bits-integers-in-c
// but we don't need to check the endianness
std::uint64_t htobe64 (uint64_t value)
{
// The answer is 42
//static const int num = 42;
// Check the endianness
//if (*reinterpret_cast<const char*>(&num) == num)
//{
const uint32_t high_part = htonl (static_cast<uint32_t> (value >> 32));
const uint32_t low_part = htonl (static_cast<uint32_t> (value & 0xFFFFFFFFLL));
return (static_cast<uint64_t> (low_part) << 32) | high_part;
//} else
//{
// return value;
//}
}
std::uint64_t be64toh (uint64_t value)
{
return (_byteswap_uint64 (value));
}
std::uint32_t htobe32 (uint32_t value)
{
return (htonl (value));
}
std::uint32_t be32toh (uint32_t value)
{
return ( _byteswap_ulong (value));
}
#endif
}

View File

@@ -0,0 +1,174 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <ripple/protocol/ErrorCodes.h>
#include <unordered_map>
#include <utility>
namespace std {
template <>
struct hash <ripple::error_code_i>
{
std::size_t operator() (ripple::error_code_i value) const
{
return value;
}
};
}
namespace ripple {
namespace RPC {
namespace detail {
class ErrorCategory
{
public:
using Map = std::unordered_map <error_code_i, ErrorInfo> ;
ErrorCategory ()
: m_unknown (rpcUNKNOWN, "unknown", "An unknown error code.")
{
add (rpcACT_BITCOIN, "actBitcoin", "Account is bitcoin address.");
add (rpcACT_EXISTS, "actExists", "Account already exists.");
add (rpcACT_MALFORMED, "actMalformed", "Account malformed.");
add (rpcACT_NOT_FOUND, "actNotFound", "Account not found.");
add (rpcATX_DEPRECATED, "deprecated", "Use the new API or specify a ledger range.");
add (rpcBAD_BLOB, "badBlob", "Blob must be a non-empty hex string.");
add (rpcBAD_FEATURE, "badFeature", "Feature unknown or invalid.");
add (rpcBAD_ISSUER, "badIssuer", "Issuer account malformed.");
add (rpcBAD_MARKET, "badMarket", "No such market.");
add (rpcBAD_SECRET, "badSecret", "Secret does not match account.");
add (rpcBAD_SEED, "badSeed", "Disallowed seed.");
add (rpcBAD_SYNTAX, "badSyntax", "Syntax error.");
add (rpcCOMMAND_MISSING, "commandMissing", "Missing command entry.");
add (rpcDST_ACT_MALFORMED, "dstActMalformed", "Destination account is malformed.");
add (rpcDST_ACT_MISSING, "dstActMissing", "Destination account does not exist.");
add (rpcDST_AMT_MALFORMED, "dstAmtMalformed", "Destination amount/currency/issuer is malformed.");
add (rpcDST_ISR_MALFORMED, "dstIsrMalformed", "Destination issuer is malformed.");
add (rpcFAIL_GEN_DECRYPT, "failGenDecrypt", "Failed to decrypt generator.");
add (rpcFORBIDDEN, "forbidden", "Bad credentials.");
add (rpcGENERAL, "general", "Generic error reason.");
add (rpcGETS_ACT_MALFORMED, "getsActMalformed", "Gets account malformed.");
add (rpcGETS_AMT_MALFORMED, "getsAmtMalformed", "Gets amount malformed.");
add (rpcHIGH_FEE, "highFee", "Current transaction fee exceeds your limit.");
add (rpcHOST_IP_MALFORMED, "hostIpMalformed", "Host IP is malformed.");
add (rpcINSUF_FUNDS, "insufFunds", "Insufficient funds.");
add (rpcINTERNAL, "internal", "Internal error.");
add (rpcINVALID_PARAMS, "invalidParams", "Invalid parameters.");
add (rpcJSON_RPC, "json_rpc", "JSON-RPC transport error.");
add (rpcLGR_IDXS_INVALID, "lgrIdxsInvalid", "Ledger indexes invalid.");
add (rpcLGR_IDX_MALFORMED, "lgrIdxMalformed", "Ledger index malformed.");
add (rpcLGR_NOT_FOUND, "lgrNotFound", "Ledger not found.");
add (rpcLOAD_FAILED, "loadFailed", "Load failed");
add (rpcMASTER_DISABLED, "masterDisabled", "Master key is disabled.");
add (rpcNOT_ENABLED, "notEnabled", "Not enabled in configuration.");
add (rpcNOT_IMPL, "notImpl", "Not implemented.");
add (rpcNOT_READY, "notReady", "Not ready to handle this request.");
add (rpcNOT_STANDALONE, "notStandAlone", "Operation valid in debug mode only.");
add (rpcNOT_SUPPORTED, "notSupported", "Operation not supported.");
add (rpcNO_ACCOUNT, "noAccount", "No such account.");
add (rpcNO_CLOSED, "noClosed", "Closed ledger is unavailable.");
add (rpcNO_CURRENT, "noCurrent", "Current ledger is unavailable.");
add (rpcNO_EVENTS, "noEvents", "Current transport does not support events.");
add (rpcNO_GEN_DECRYPT, "noGenDecrypt", "Password failed to decrypt master public generator.");
add (rpcNO_NETWORK, "noNetwork", "Not synced to Ripple network.");
add (rpcNO_PATH, "noPath", "Unable to find a ripple path.");
add (rpcNO_PERMISSION, "noPermission", "You don't have permission for this command.");
add (rpcNO_PF_REQUEST, "noPathRequest", "No pathfinding request in progress.");
add (rpcPASSWD_CHANGED, "passwdChanged", "Wrong key, password changed.");
add (rpcPAYS_ACT_MALFORMED, "paysActMalformed", "Pays account malformed.");
add (rpcPAYS_AMT_MALFORMED, "paysAmtMalformed", "Pays amount malformed.");
add (rpcPORT_MALFORMED, "portMalformed", "Port is malformed.");
add (rpcPUBLIC_MALFORMED, "publicMalformed", "Public key is malformed.");
add (rpcQUALITY_MALFORMED, "qualityMalformed", "Quality malformed.");
add (rpcSLOW_DOWN, "slowDown", "You are placing too much load on the server.");
add (rpcSRC_ACT_MALFORMED, "srcActMalformed", "Source account is malformed.");
add (rpcSRC_ACT_MISSING, "srcActMissing", "Source account not provided.");
add (rpcSRC_ACT_NOT_FOUND, "srcActNotFound", "Source account not found.");
add (rpcSRC_AMT_MALFORMED, "srcAmtMalformed", "Source amount/currency/issuer is malformed.");
add (rpcSRC_CUR_MALFORMED, "srcCurMalformed", "Source currency is malformed.");
add (rpcSRC_ISR_MALFORMED, "srcIsrMalformed", "Source issuer is malformed.");
add (rpcSRC_MISSING, "srcMissing", "Source is missing.");
add (rpcSRC_UNCLAIMED, "srcUnclaimed", "Source account is not claimed.");
add (rpcTOO_BUSY, "tooBusy", "The server is too busy to help you now.");
add (rpcTXN_NOT_FOUND, "txnNotFound", "Transaction not found.");
add (rpcUNKNOWN_COMMAND, "unknownCmd", "Unknown method.");
add (rpcWRONG_SEED, "wrongSeed", "The regular key does not point as the master key.");
}
ErrorInfo const& get (error_code_i code) const
{
Map::const_iterator const iter (m_map.find (code));
if (iter != m_map.end())
return iter->second;
return m_unknown;
}
private:
void add (error_code_i code, std::string const& token,
std::string const& message)
{
std::pair <Map::iterator, bool> result (
m_map.emplace (std::piecewise_construct,
std::forward_as_tuple (code), std::forward_as_tuple (
code, token, message)));
if (! result.second)
throw std::invalid_argument ("duplicate error code");
}
private:
Map m_map;
ErrorInfo m_unknown;
};
}
//------------------------------------------------------------------------------
ErrorInfo const& get_error_info (error_code_i code)
{
static detail::ErrorCategory category;
return category.get (code);
}
Json::Value make_error (error_code_i code)
{
Json::Value json;
inject_error (code, json);
return json;
}
Json::Value make_error (error_code_i code, std::string const& message)
{
Json::Value json;
inject_error (code, message, json);
return json;
}
bool contains_error (Json::Value const& json)
{
if (json.isObject() && json.isMember ("error"))
return true;
return false;
}
}
}

View File

@@ -0,0 +1,493 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <ripple/basics/UnorderedContainers.h>
#include <ripple/protocol/Book.h>
#include <ripple/protocol/Issue.h>
#include <beast/unit_test/suite.h>
#include <map>
#include <set>
#include <typeinfo>
#include <unordered_set>
#if BEAST_MSVC
# define STL_SET_HAS_EMPLACE 1
#else
# define STL_SET_HAS_EMPLACE 0
#endif
#ifndef RIPPLE_ASSETS_ENABLE_STD_HASH
# if BEAST_MAC || BEAST_IOS
# define RIPPLE_ASSETS_ENABLE_STD_HASH 0
# else
# define RIPPLE_ASSETS_ENABLE_STD_HASH 1
# endif
#endif
namespace ripple {
class Issue_test : public beast::unit_test::suite
{
public:
// Comparison, hash tests for uint60 (via base_uint)
template <typename Unsigned>
void testUnsigned ()
{
Unsigned const u1 (1);
Unsigned const u2 (2);
Unsigned const u3 (3);
expect (u1 != u2);
expect (u1 < u2);
expect (u1 <= u2);
expect (u2 <= u2);
expect (u2 == u2);
expect (u2 >= u2);
expect (u3 >= u2);
expect (u3 > u2);
std::hash <Unsigned> hash;
expect (hash (u1) == hash (u1));
expect (hash (u2) == hash (u2));
expect (hash (u3) == hash (u3));
expect (hash (u1) != hash (u2));
expect (hash (u1) != hash (u3));
expect (hash (u2) != hash (u3));
}
//--------------------------------------------------------------------------
// Comparison, hash tests for IssueType
template <class Issue>
void testIssueType ()
{
Currency const c1 (1); Account const i1 (1);
Currency const c2 (2); Account const i2 (2);
Currency const c3 (3); Account const i3 (3);
expect (Issue (c1, i1) != Issue (c2, i1));
expect (Issue (c1, i1) < Issue (c2, i1));
expect (Issue (c1, i1) <= Issue (c2, i1));
expect (Issue (c2, i1) <= Issue (c2, i1));
expect (Issue (c2, i1) == Issue (c2, i1));
expect (Issue (c2, i1) >= Issue (c2, i1));
expect (Issue (c3, i1) >= Issue (c2, i1));
expect (Issue (c3, i1) > Issue (c2, i1));
expect (Issue (c1, i1) != Issue (c1, i2));
expect (Issue (c1, i1) < Issue (c1, i2));
expect (Issue (c1, i1) <= Issue (c1, i2));
expect (Issue (c1, i2) <= Issue (c1, i2));
expect (Issue (c1, i2) == Issue (c1, i2));
expect (Issue (c1, i2) >= Issue (c1, i2));
expect (Issue (c1, i3) >= Issue (c1, i2));
expect (Issue (c1, i3) > Issue (c1, i2));
std::hash <Issue> hash;
expect (hash (Issue (c1, i1)) == hash (Issue (c1, i1)));
expect (hash (Issue (c1, i2)) == hash (Issue (c1, i2)));
expect (hash (Issue (c1, i3)) == hash (Issue (c1, i3)));
expect (hash (Issue (c2, i1)) == hash (Issue (c2, i1)));
expect (hash (Issue (c2, i2)) == hash (Issue (c2, i2)));
expect (hash (Issue (c2, i3)) == hash (Issue (c2, i3)));
expect (hash (Issue (c3, i1)) == hash (Issue (c3, i1)));
expect (hash (Issue (c3, i2)) == hash (Issue (c3, i2)));
expect (hash (Issue (c3, i3)) == hash (Issue (c3, i3)));
expect (hash (Issue (c1, i1)) != hash (Issue (c1, i2)));
expect (hash (Issue (c1, i1)) != hash (Issue (c1, i3)));
expect (hash (Issue (c1, i1)) != hash (Issue (c2, i1)));
expect (hash (Issue (c1, i1)) != hash (Issue (c2, i2)));
expect (hash (Issue (c1, i1)) != hash (Issue (c2, i3)));
expect (hash (Issue (c1, i1)) != hash (Issue (c3, i1)));
expect (hash (Issue (c1, i1)) != hash (Issue (c3, i2)));
expect (hash (Issue (c1, i1)) != hash (Issue (c3, i3)));
}
template <class Set>
void testIssueSet ()
{
Currency const c1 (1);
Account const i1 (1);
Currency const c2 (2);
Account const i2 (2);
IssueRef const a1 (c1, i1);
IssueRef const a2 (c2, i2);
{
Set c;
c.insert (a1);
if (! expect (c.size () == 1)) return;
c.insert (a2);
if (! expect (c.size () == 2)) return;
if (! expect (c.erase (Issue (c1, i2)) == 0)) return;
if (! expect (c.erase (Issue (c1, i1)) == 1)) return;
if (! expect (c.erase (Issue (c2, i2)) == 1)) return;
if (! expect (c.empty ())) return;
}
{
Set c;
c.insert (a1);
if (! expect (c.size () == 1)) return;
c.insert (a2);
if (! expect (c.size () == 2)) return;
if (! expect (c.erase (IssueRef (c1, i2)) == 0)) return;
if (! expect (c.erase (IssueRef (c1, i1)) == 1)) return;
if (! expect (c.erase (IssueRef (c2, i2)) == 1)) return;
if (! expect (c.empty ())) return;
#if STL_SET_HAS_EMPLACE
c.emplace (c1, i1);
if (! expect (c.size() == 1)) return;
c.emplace (c2, i2);
if (! expect (c.size() == 2)) return;
#endif
}
}
template <class Map>
void testIssueMap ()
{
Currency const c1 (1);
Account const i1 (1);
Currency const c2 (2);
Account const i2 (2);
IssueRef const a1 (c1, i1);
IssueRef const a2 (c2, i2);
{
Map c;
c.insert (std::make_pair (a1, 1));
if (! expect (c.size () == 1)) return;
c.insert (std::make_pair (a2, 2));
if (! expect (c.size () == 2)) return;
if (! expect (c.erase (Issue (c1, i2)) == 0)) return;
if (! expect (c.erase (Issue (c1, i1)) == 1)) return;
if (! expect (c.erase (Issue (c2, i2)) == 1)) return;
if (! expect (c.empty ())) return;
}
{
Map c;
c.insert (std::make_pair (a1, 1));
if (! expect (c.size () == 1)) return;
c.insert (std::make_pair (a2, 2));
if (! expect (c.size () == 2)) return;
if (! expect (c.erase (IssueRef (c1, i2)) == 0)) return;
if (! expect (c.erase (IssueRef (c1, i1)) == 1)) return;
if (! expect (c.erase (IssueRef (c2, i2)) == 1)) return;
if (! expect (c.empty ())) return;
}
}
void testIssueSets ()
{
testcase ("std::set <Issue>");
testIssueSet <std::set <Issue>> ();
testcase ("std::set <IssueRef>");
testIssueSet <std::set <IssueRef>> ();
#if RIPPLE_ASSETS_ENABLE_STD_HASH
testcase ("std::unordered_set <Issue>");
testIssueSet <std::unordered_set <Issue>> ();
testcase ("std::unordered_set <IssueRef>");
testIssueSet <std::unordered_set <IssueRef>> ();
#endif
testcase ("hash_set <Issue>");
testIssueSet <hash_set <Issue>> ();
testcase ("hash_set <IssueRef>");
testIssueSet <hash_set <IssueRef>> ();
}
void testIssueMaps ()
{
testcase ("std::map <Issue, int>");
testIssueMap <std::map <Issue, int>> ();
testcase ("std::map <IssueRef, int>");
testIssueMap <std::map <IssueRef, int>> ();
#if RIPPLE_ASSETS_ENABLE_STD_HASH
testcase ("std::unordered_map <Issue, int>");
testIssueMap <std::unordered_map <Issue, int>> ();
testcase ("std::unordered_map <IssueRef, int>");
testIssueMap <std::unordered_map <IssueRef, int>> ();
testcase ("hash_map <Issue, int>");
testIssueMap <hash_map <Issue, int>> ();
testcase ("hash_map <IssueRef, int>");
testIssueMap <hash_map <IssueRef, int>> ();
#endif
}
//--------------------------------------------------------------------------
// Comparison, hash tests for BookType
template <class Book>
void testBook ()
{
Currency const c1 (1); Account const i1 (1);
Currency const c2 (2); Account const i2 (2);
Currency const c3 (3); Account const i3 (3);
Issue a1 (c1, i1);
Issue a2 (c1, i2);
Issue a3 (c2, i2);
Issue a4 (c3, i2);
expect (Book (a1, a2) != Book (a2, a3));
expect (Book (a1, a2) < Book (a2, a3));
expect (Book (a1, a2) <= Book (a2, a3));
expect (Book (a2, a3) <= Book (a2, a3));
expect (Book (a2, a3) == Book (a2, a3));
expect (Book (a2, a3) >= Book (a2, a3));
expect (Book (a3, a4) >= Book (a2, a3));
expect (Book (a3, a4) > Book (a2, a3));
std::hash <Book> hash;
// log << std::hex << hash (Book (a1, a2));
// log << std::hex << hash (Book (a1, a2));
//
// log << std::hex << hash (Book (a1, a3));
// log << std::hex << hash (Book (a1, a3));
//
// log << std::hex << hash (Book (a1, a4));
// log << std::hex << hash (Book (a1, a4));
//
// log << std::hex << hash (Book (a2, a3));
// log << std::hex << hash (Book (a2, a3));
//
// log << std::hex << hash (Book (a2, a4));
// log << std::hex << hash (Book (a2, a4));
//
// log << std::hex << hash (Book (a3, a4));
// log << std::hex << hash (Book (a3, a4));
expect (hash (Book (a1, a2)) == hash (Book (a1, a2)));
expect (hash (Book (a1, a3)) == hash (Book (a1, a3)));
expect (hash (Book (a1, a4)) == hash (Book (a1, a4)));
expect (hash (Book (a2, a3)) == hash (Book (a2, a3)));
expect (hash (Book (a2, a4)) == hash (Book (a2, a4)));
expect (hash (Book (a3, a4)) == hash (Book (a3, a4)));
expect (hash (Book (a1, a2)) != hash (Book (a1, a3)));
expect (hash (Book (a1, a2)) != hash (Book (a1, a4)));
expect (hash (Book (a1, a2)) != hash (Book (a2, a3)));
expect (hash (Book (a1, a2)) != hash (Book (a2, a4)));
expect (hash (Book (a1, a2)) != hash (Book (a3, a4)));
}
//--------------------------------------------------------------------------
template <class Set>
void testBookSet ()
{
Currency const c1 (1);
Account const i1 (1);
Currency const c2 (2);
Account const i2 (2);
IssueRef const a1 (c1, i1);
IssueRef const a2 (c2, i2);
BookRef const b1 (a1, a2);
BookRef const b2 (a2, a1);
{
Set c;
c.insert (b1);
if (! expect (c.size () == 1)) return;
c.insert (b2);
if (! expect (c.size () == 2)) return;
if (! expect (c.erase (Book (a1, a1)) == 0)) return;
if (! expect (c.erase (Book (a1, a2)) == 1)) return;
if (! expect (c.erase (Book (a2, a1)) == 1)) return;
if (! expect (c.empty ())) return;
}
{
Set c;
c.insert (b1);
if (! expect (c.size () == 1)) return;
c.insert (b2);
if (! expect (c.size () == 2)) return;
if (! expect (c.erase (BookRef (a1, a1)) == 0)) return;
if (! expect (c.erase (BookRef (a1, a2)) == 1)) return;
if (! expect (c.erase (BookRef (a2, a1)) == 1)) return;
if (! expect (c.empty ())) return;
#if STL_SET_HAS_EMPLACE
c.emplace (a1, a2);
if (! expect (c.size() == 1)) return;
c.emplace (a2, a1);
if (! expect (c.size() == 2)) return;
#endif
}
}
template <class Map>
void testBookMap ()
{
Currency const c1 (1);
Account const i1 (1);
Currency const c2 (2);
Account const i2 (2);
IssueRef const a1 (c1, i1);
IssueRef const a2 (c2, i2);
BookRef const b1 (a1, a2);
BookRef const b2 (a2, a1);
//typename Map::value_type value_type;
//std::pair <BookRef const, int> value_type;
{
Map c;
//c.insert (value_type (b1, 1));
c.insert (std::make_pair (b1, 1));
if (! expect (c.size () == 1)) return;
//c.insert (value_type (b2, 2));
c.insert (std::make_pair (b2, 1));
if (! expect (c.size () == 2)) return;
if (! expect (c.erase (Book (a1, a1)) == 0)) return;
if (! expect (c.erase (Book (a1, a2)) == 1)) return;
if (! expect (c.erase (Book (a2, a1)) == 1)) return;
if (! expect (c.empty ())) return;
}
{
Map c;
//c.insert (value_type (b1, 1));
c.insert (std::make_pair (b1, 1));
if (! expect (c.size () == 1)) return;
//c.insert (value_type (b2, 2));
c.insert (std::make_pair (b2, 1));
if (! expect (c.size () == 2)) return;
if (! expect (c.erase (BookRef (a1, a1)) == 0)) return;
if (! expect (c.erase (BookRef (a1, a2)) == 1)) return;
if (! expect (c.erase (BookRef (a2, a1)) == 1)) return;
if (! expect (c.empty ())) return;
}
}
void testBookSets ()
{
testcase ("std::set <Book>");
testBookSet <std::set <Book>> ();
testcase ("std::set <BookRef>");
testBookSet <std::set <BookRef>> ();
#if RIPPLE_ASSETS_ENABLE_STD_HASH
testcase ("std::unordered_set <Book>");
testBookSet <std::unordered_set <Book>> ();
testcase ("std::unordered_set <BookRef>");
testBookSet <std::unordered_set <BookRef>> ();
#endif
testcase ("hash_set <Book>");
testBookSet <hash_set <Book>> ();
testcase ("hash_set <BookRef>");
testBookSet <hash_set <BookRef>> ();
}
void testBookMaps ()
{
testcase ("std::map <Book, int>");
testBookMap <std::map <Book, int>> ();
testcase ("std::map <BookRef, int>");
testBookMap <std::map <BookRef, int>> ();
#if RIPPLE_ASSETS_ENABLE_STD_HASH
testcase ("std::unordered_map <Book, int>");
testBookMap <std::unordered_map <Book, int>> ();
testcase ("std::unordered_map <BookRef, int>");
testBookMap <std::unordered_map <BookRef, int>> ();
testcase ("hash_map <Book, int>");
testBookMap <hash_map <Book, int>> ();
testcase ("hash_map <BookRef, int>");
testBookMap <hash_map <BookRef, int>> ();
#endif
}
//--------------------------------------------------------------------------
void run()
{
testcase ("Currency");
testUnsigned <Currency> ();
testcase ("Account");
testUnsigned <Account> ();
// ---
testcase ("Issue");
testIssueType <Issue> ();
testcase ("IssueRef");
testIssueType <IssueRef> ();
testIssueSets ();
testIssueMaps ();
// ---
testcase ("Book");
testBook <Book> ();
testcase ("BookRef");
testBook <BookRef> ();
testBookSets ();
testBookMaps ();
}
};
BEAST_DEFINE_TESTSUITE(Issue,types,ripple);
}

View File

@@ -26,7 +26,7 @@
#include <ripple/crypto/RFC1751.h>
#include <ripple/protocol/RippleAddress.h>
#include <ripple/protocol/Serializer.h>
#include <ripple/types/RipplePublicKey.h>
#include <ripple/protocol/RipplePublicKey.h>
#include <beast/unit_test/suite.h>
#include <openssl/ripemd.h>
#include <openssl/bn.h>
@@ -957,6 +957,17 @@ public:
= naAccountPrivate1.accountPrivateDecrypt (naAccountPublic0, vucTextCipher);
expect (vucTextSrc == vucTextRecovered, "Encrypt-decrypt failed.");
{
RippleAddress nSeed;
uint128 seed1, seed2;
seed1.SetHex ("71ED064155FFADFA38782C5E0158CB26");
nSeed.setSeed (seed1);
expect (nSeed.humanSeed() == "shHM53KPZ87Gwdqarm1bAmPeXg8Tn",
"Incorrect human seed");
expect (nSeed.humanSeed1751() == "MAD BODY ACE MINT OKAY HUB WHAT DATA SACK FLAT DANA MATH",
"Incorrect 1751 seed");
}
}
};

View File

@@ -20,7 +20,7 @@
#include <ripple/basics/Log.h>
#include <ripple/protocol/JsonFields.h>
#include <ripple/crypto/CBigNum.h>
#include <ripple/core/SystemParameters.h>
#include <ripple/protocol/SystemParameters.h>
#include <ripple/protocol/STAmount.h>
#include <ripple/protocol/UintTypes.h>
#include <beast/module/core/text/LexicalCast.h>

View File

@@ -1,575 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <ripple/basics/Log.h>
#include <ripple/crypto/CBigNum.h>
#include <ripple/protocol/STAmount.h>
#include <beast/unit_test/suite.h>
namespace ripple {
class STAmount_test : public beast::unit_test::suite
{
public:
static STAmount serializeAndDeserialize (STAmount const& s)
{
Serializer ser;
s.add (ser);
SerializerIterator sit (ser);
return STAmount::deserialize (sit);
}
//--------------------------------------------------------------------------
bool roundTest (int n, int d, int m)
{
// check STAmount rounding
STAmount num (noIssue(), n);
STAmount den (noIssue(), d);
STAmount mul (noIssue(), m);
STAmount quot = divide (n, d, noIssue());
STAmount res = multiply (quot, mul, noIssue());
expect (! res.isNative (), "Product should not be native");
res.roundSelf ();
STAmount cmp (noIssue(), (n * m) / d);
expect (! cmp.isNative (), "Comparison amount should not be native");
if (res != cmp)
{
cmp.throwComparable (res);
WriteLog (lsWARNING, STAmount) << "(" << num.getText () << "/" << den.getText () << ") X " << mul.getText () << " = "
<< res.getText () << " not " << cmp.getText ();
fail ("Rounding");
return false;
}
else
{
pass ();
}
return true;
}
void mulTest (int a, int b)
{
STAmount aa (noIssue(), a);
STAmount bb (noIssue(), b);
STAmount prod1 (multiply (aa, bb, noIssue()));
expect (! prod1.isNative ());
STAmount prod2 (noIssue(), static_cast<std::uint64_t> (a) * static_cast<std::uint64_t> (b));
if (prod1 != prod2)
{
WriteLog (lsWARNING, STAmount) << "nn(" << aa.getFullText () << " * " << bb.getFullText () << ") = " << prod1.getFullText ()
<< " not " << prod2.getFullText ();
fail ("Multiplication result is not exact");
}
else
{
pass ();
}
aa = a;
prod1 = multiply (aa, bb, noIssue());
if (prod1 != prod2)
{
WriteLog (lsWARNING, STAmount) << "n(" << aa.getFullText () << " * " << bb.getFullText () << ") = " << prod1.getFullText ()
<< " not " << prod2.getFullText ();
fail ("Multiplication result is not exact");
}
else
{
pass ();
}
}
//--------------------------------------------------------------------------
void testSetValue (
std::string const& value, Issue const& issue, bool success = true)
{
STAmount amount (issue);
bool const result = amount.setValue (value);
expect (result == success, "parse " + value);
if (success)
expect (amount.getText () == value, "format " + value);
}
void testSetValue ()
{
{
testcase ("set value (native)");
Issue const xrp (xrpIssue ());
// fractional XRP (i.e. drops)
testSetValue ("1", xrp);
testSetValue ("22", xrp);
testSetValue ("333", xrp);
testSetValue ("4444", xrp);
testSetValue ("55555", xrp);
testSetValue ("666666", xrp);
// 1 XRP up to 100 billion, in powers of 10 (in drops)
testSetValue ("1000000", xrp);
testSetValue ("10000000", xrp);
testSetValue ("100000000", xrp);
testSetValue ("1000000000", xrp);
testSetValue ("10000000000", xrp);
testSetValue ("100000000000", xrp);
testSetValue ("1000000000000", xrp);
testSetValue ("10000000000000", xrp);
testSetValue ("100000000000000", xrp);
testSetValue ("1000000000000000", xrp);
testSetValue ("10000000000000000", xrp);
testSetValue ("100000000000000000", xrp);
// Invalid native values:
testSetValue ("1.1", xrp, false);
testSetValue ("100000000000000001", xrp, false);
testSetValue ("1000000000000000000", xrp, false);
}
{
testcase ("set value (iou)");
Issue const usd (Currency (0x5553440000000000), Account (0x4985601));
testSetValue ("1", usd);
testSetValue ("10", usd);
testSetValue ("100", usd);
testSetValue ("1000", usd);
testSetValue ("10000", usd);
testSetValue ("100000", usd);
testSetValue ("1000000", usd);
testSetValue ("10000000", usd);
testSetValue ("100000000", usd);
testSetValue ("1000000000", usd);
testSetValue ("10000000000", usd);
testSetValue ("1234567.1", usd);
testSetValue ("1234567.12", usd);
testSetValue ("1234567.123", usd);
testSetValue ("1234567.1234", usd);
testSetValue ("1234567.12345", usd);
testSetValue ("1234567.123456", usd);
testSetValue ("1234567.1234567", usd);
testSetValue ("1234567.12345678", usd);
testSetValue ("1234567.123456789", usd);
}
}
//--------------------------------------------------------------------------
void testNativeCurrency ()
{
testcase ("native currency");
STAmount zeroSt, one (1), hundred (100);
// VFALCO NOTE Why repeat "STAmount fail" so many times??
unexpected (serializeAndDeserialize (zeroSt) != zeroSt, "STAmount fail");
unexpected (serializeAndDeserialize (one) != one, "STAmount fail");
unexpected (serializeAndDeserialize (hundred) != hundred, "STAmount fail");
unexpected (!zeroSt.isNative (), "STAmount fail");
unexpected (!hundred.isNative (), "STAmount fail");
unexpected (zeroSt != zero, "STAmount fail");
unexpected (one == zero, "STAmount fail");
unexpected (hundred == zero, "STAmount fail");
unexpected ((zeroSt < zeroSt), "STAmount fail");
unexpected (! (zeroSt < one), "STAmount fail");
unexpected (! (zeroSt < hundred), "STAmount fail");
unexpected ((one < zeroSt), "STAmount fail");
unexpected ((one < one), "STAmount fail");
unexpected (! (one < hundred), "STAmount fail");
unexpected ((hundred < zeroSt), "STAmount fail");
unexpected ((hundred < one), "STAmount fail");
unexpected ((hundred < hundred), "STAmount fail");
unexpected ((zeroSt > zeroSt), "STAmount fail");
unexpected ((zeroSt > one), "STAmount fail");
unexpected ((zeroSt > hundred), "STAmount fail");
unexpected (! (one > zeroSt), "STAmount fail");
unexpected ((one > one), "STAmount fail");
unexpected ((one > hundred), "STAmount fail");
unexpected (! (hundred > zeroSt), "STAmount fail");
unexpected (! (hundred > one), "STAmount fail");
unexpected ((hundred > hundred), "STAmount fail");
unexpected (! (zeroSt <= zeroSt), "STAmount fail");
unexpected (! (zeroSt <= one), "STAmount fail");
unexpected (! (zeroSt <= hundred), "STAmount fail");
unexpected ((one <= zeroSt), "STAmount fail");
unexpected (! (one <= one), "STAmount fail");
unexpected (! (one <= hundred), "STAmount fail");
unexpected ((hundred <= zeroSt), "STAmount fail");
unexpected ((hundred <= one), "STAmount fail");
unexpected (! (hundred <= hundred), "STAmount fail");
unexpected (! (zeroSt >= zeroSt), "STAmount fail");
unexpected ((zeroSt >= one), "STAmount fail");
unexpected ((zeroSt >= hundred), "STAmount fail");
unexpected (! (one >= zeroSt), "STAmount fail");
unexpected (! (one >= one), "STAmount fail");
unexpected ((one >= hundred), "STAmount fail");
unexpected (! (hundred >= zeroSt), "STAmount fail");
unexpected (! (hundred >= one), "STAmount fail");
unexpected (! (hundred >= hundred), "STAmount fail");
unexpected (! (zeroSt == zeroSt), "STAmount fail");
unexpected ((zeroSt == one), "STAmount fail");
unexpected ((zeroSt == hundred), "STAmount fail");
unexpected ((one == zeroSt), "STAmount fail");
unexpected (! (one == one), "STAmount fail");
unexpected ((one == hundred), "STAmount fail");
unexpected ((hundred == zeroSt), "STAmount fail");
unexpected ((hundred == one), "STAmount fail");
unexpected (! (hundred == hundred), "STAmount fail");
unexpected ((zeroSt != zeroSt), "STAmount fail");
unexpected (! (zeroSt != one), "STAmount fail");
unexpected (! (zeroSt != hundred), "STAmount fail");
unexpected (! (one != zeroSt), "STAmount fail");
unexpected ((one != one), "STAmount fail");
unexpected (! (one != hundred), "STAmount fail");
unexpected (! (hundred != zeroSt), "STAmount fail");
unexpected (! (hundred != one), "STAmount fail");
unexpected ((hundred != hundred), "STAmount fail");
unexpected (STAmount ().getText () != "0", "STAmount fail");
unexpected (STAmount (31).getText () != "31", "STAmount fail");
unexpected (STAmount (310).getText () != "310", "STAmount fail");
unexpected (to_string (Currency ()) != "XRP", "cHC(XRP)");
Currency c;
unexpected (!to_currency (c, "USD"), "create USD currency");
unexpected (to_string (c) != "USD", "check USD currency");
const std::string cur = "015841551A748AD2C1F76FF6ECB0CCCD00000000";
unexpected (!to_currency (c, cur), "create custom currency");
unexpected (to_string (c) != cur, "check custom currency");
unexpected (c != Currency (cur), "check custom currency");
}
//--------------------------------------------------------------------------
void testCustomCurrency ()
{
testcase ("custom currency");
STAmount zeroSt (noIssue()), one (noIssue(), 1), hundred (noIssue(), 100);
unexpected (serializeAndDeserialize (zeroSt) != zeroSt, "STAmount fail");
unexpected (serializeAndDeserialize (one) != one, "STAmount fail");
unexpected (serializeAndDeserialize (hundred) != hundred, "STAmount fail");
unexpected (zeroSt.isNative (), "STAmount fail");
unexpected (hundred.isNative (), "STAmount fail");
unexpected (zeroSt != zero, "STAmount fail");
unexpected (one == zero, "STAmount fail");
unexpected (hundred == zero, "STAmount fail");
unexpected ((zeroSt < zeroSt), "STAmount fail");
unexpected (! (zeroSt < one), "STAmount fail");
unexpected (! (zeroSt < hundred), "STAmount fail");
unexpected ((one < zeroSt), "STAmount fail");
unexpected ((one < one), "STAmount fail");
unexpected (! (one < hundred), "STAmount fail");
unexpected ((hundred < zeroSt), "STAmount fail");
unexpected ((hundred < one), "STAmount fail");
unexpected ((hundred < hundred), "STAmount fail");
unexpected ((zeroSt > zeroSt), "STAmount fail");
unexpected ((zeroSt > one), "STAmount fail");
unexpected ((zeroSt > hundred), "STAmount fail");
unexpected (! (one > zeroSt), "STAmount fail");
unexpected ((one > one), "STAmount fail");
unexpected ((one > hundred), "STAmount fail");
unexpected (! (hundred > zeroSt), "STAmount fail");
unexpected (! (hundred > one), "STAmount fail");
unexpected ((hundred > hundred), "STAmount fail");
unexpected (! (zeroSt <= zeroSt), "STAmount fail");
unexpected (! (zeroSt <= one), "STAmount fail");
unexpected (! (zeroSt <= hundred), "STAmount fail");
unexpected ((one <= zeroSt), "STAmount fail");
unexpected (! (one <= one), "STAmount fail");
unexpected (! (one <= hundred), "STAmount fail");
unexpected ((hundred <= zeroSt), "STAmount fail");
unexpected ((hundred <= one), "STAmount fail");
unexpected (! (hundred <= hundred), "STAmount fail");
unexpected (! (zeroSt >= zeroSt), "STAmount fail");
unexpected ((zeroSt >= one), "STAmount fail");
unexpected ((zeroSt >= hundred), "STAmount fail");
unexpected (! (one >= zeroSt), "STAmount fail");
unexpected (! (one >= one), "STAmount fail");
unexpected ((one >= hundred), "STAmount fail");
unexpected (! (hundred >= zeroSt), "STAmount fail");
unexpected (! (hundred >= one), "STAmount fail");
unexpected (! (hundred >= hundred), "STAmount fail");
unexpected (! (zeroSt == zeroSt), "STAmount fail");
unexpected ((zeroSt == one), "STAmount fail");
unexpected ((zeroSt == hundred), "STAmount fail");
unexpected ((one == zeroSt), "STAmount fail");
unexpected (! (one == one), "STAmount fail");
unexpected ((one == hundred), "STAmount fail");
unexpected ((hundred == zeroSt), "STAmount fail");
unexpected ((hundred == one), "STAmount fail");
unexpected (! (hundred == hundred), "STAmount fail");
unexpected ((zeroSt != zeroSt), "STAmount fail");
unexpected (! (zeroSt != one), "STAmount fail");
unexpected (! (zeroSt != hundred), "STAmount fail");
unexpected (! (one != zeroSt), "STAmount fail");
unexpected ((one != one), "STAmount fail");
unexpected (! (one != hundred), "STAmount fail");
unexpected (! (hundred != zeroSt), "STAmount fail");
unexpected (! (hundred != one), "STAmount fail");
unexpected ((hundred != hundred), "STAmount fail");
unexpected (STAmount (noIssue()).getText () != "0", "STAmount fail");
unexpected (STAmount (noIssue(), 31).getText () != "31", "STAmount fail");
unexpected (STAmount (noIssue(), 31, 1).getText () != "310", "STAmount fail");
unexpected (STAmount (noIssue(), 31, -1).getText () != "3.1", "STAmount fail");
unexpected (STAmount (noIssue(), 31, -2).getText () != "0.31", "STAmount fail");
unexpected (multiply (STAmount (noIssue(), 20), STAmount (3), noIssue()).getText () != "60",
"STAmount multiply fail 1");
unexpected (multiply (STAmount (noIssue(), 20), STAmount (3), xrpIssue ()).getText () != "60",
"STAmount multiply fail 2");
unexpected (multiply (STAmount (20), STAmount (3), noIssue()).getText () != "60",
"STAmount multiply fail 3");
unexpected (multiply (STAmount (20), STAmount (3), xrpIssue ()).getText () != "60",
"STAmount multiply fail 4");
if (divide (STAmount (noIssue(), 60), STAmount (3), noIssue()).getText () != "20")
{
WriteLog (lsFATAL, STAmount) << "60/3 = " <<
divide (STAmount (noIssue(), 60),
STAmount (3), noIssue()).getText ();
fail ("STAmount divide fail");
}
else
{
pass ();
}
unexpected (divide (STAmount (noIssue(), 60), STAmount (3), xrpIssue ()).getText () != "20",
"STAmount divide fail");
unexpected (divide (STAmount (noIssue(), 60), STAmount (noIssue(), 3), noIssue()).getText () != "20",
"STAmount divide fail");
unexpected (divide (STAmount (noIssue(), 60), STAmount (noIssue(), 3), xrpIssue ()).getText () != "20",
"STAmount divide fail");
STAmount a1 (noIssue(), 60), a2 (noIssue(), 10, -1);
unexpected (divide (a2, a1, noIssue()) != amountFromQuality (getRate (a1, a2)),
"STAmount setRate(getRate) fail");
unexpected (divide (a1, a2, noIssue()) != amountFromQuality (getRate (a2, a1)),
"STAmount setRate(getRate) fail");
}
//--------------------------------------------------------------------------
void testArithmetic ()
{
testcase ("arithmetic");
CBigNum b;
for (int i = 0; i < 16; ++i)
{
std::uint64_t r = rand ();
r <<= 32;
r |= rand ();
b.setuint64 (r);
if (b.getuint64 () != r)
{
WriteLog (lsFATAL, STAmount) << r << " != " << b.getuint64 () << " " << b.ToString (16);
fail ("setull64/getull64 failure");
}
else
{
pass ();
}
}
// Test currency multiplication and division operations such as
// convertToDisplayAmount, convertToInternalAmount, getRate, getClaimed, and getNeeded
unexpected (getRate (STAmount (1), STAmount (10)) != (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 1");
unexpected (getRate (STAmount (10), STAmount (1)) != (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 2");
unexpected (getRate (STAmount (noIssue(), 1), STAmount (noIssue(), 10)) != (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 3");
unexpected (getRate (STAmount (noIssue(), 10), STAmount (noIssue(), 1)) != (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 4");
unexpected (getRate (STAmount (noIssue(), 1), STAmount (10)) != (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 5");
unexpected (getRate (STAmount (noIssue(), 10), STAmount (1)) != (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 6");
unexpected (getRate (STAmount (1), STAmount (noIssue(), 10)) != (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 7");
unexpected (getRate (STAmount (10), STAmount (noIssue(), 1)) != (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
"STAmount getRate fail 8");
roundTest (1, 3, 3);
roundTest (2, 3, 9);
roundTest (1, 7, 21);
roundTest (1, 2, 4);
roundTest (3, 9, 18);
roundTest (7, 11, 44);
for (int i = 0; i <= 100000; ++i)
mulTest (rand () % 10000000, rand () % 10000000);
}
//--------------------------------------------------------------------------
void testUnderflow ()
{
testcase ("underflow");
STAmount bigNative (STAmount::cMaxNative / 2);
STAmount bigValue (noIssue(),
(STAmount::cMinValue + STAmount::cMaxValue) / 2,
STAmount::cMaxOffset - 1);
STAmount smallValue (noIssue(),
(STAmount::cMinValue + STAmount::cMaxValue) / 2,
STAmount::cMinOffset + 1);
STAmount zeroSt (noIssue(), 0);
STAmount smallXsmall = multiply (smallValue, smallValue, noIssue());
expect (smallXsmall == zero, "smallXsmall != 0");
STAmount bigDsmall = divide (smallValue, bigValue, noIssue());
expect (bigDsmall == zero, "small/big != 0: " + bigDsmall.getText ());
#if 0
// TODO(tom): this test makes no sense - we should have no way to have
// the currency not be XRP while the account is XRP.
bigDsmall = divide (smallValue, bigNative, noCurrency(), xrpAccount ());
#endif
expect (bigDsmall == zero,
"small/bigNative != 0: " + bigDsmall.getText ());
bigDsmall = divide (smallValue, bigValue, xrpIssue ());
expect (bigDsmall == zero,
"(small/big)->N != 0: " + bigDsmall.getText ());
bigDsmall = divide (smallValue, bigNative, xrpIssue ());
expect (bigDsmall == zero,
"(small/bigNative)->N != 0: " + bigDsmall.getText ());
// very bad offer
std::uint64_t r = getRate (smallValue, bigValue);
expect (r == 0, "getRate(smallOut/bigIn) != 0");
// very good offer
r = getRate (bigValue, smallValue);
expect (r == 0, "getRate(smallIn/bigOUt) != 0");
}
//--------------------------------------------------------------------------
void testRounding ()
{
// VFALCO TODO There are no actual tests here, just printed output?
// Change this to actually do something.
#if 0
beginTestCase ("rounding ");
std::uint64_t value = 25000000000000000ull;
int offset = -14;
canonicalizeRound (false, value, offset, true);
STAmount one (noIssue(), 1);
STAmount two (noIssue(), 2);
STAmount three (noIssue(), 3);
STAmount oneThird1 = divRound (one, three, noIssue(), false);
STAmount oneThird2 = divide (one, three, noIssue());
STAmount oneThird3 = divRound (one, three, noIssue(), true);
WriteLog (lsINFO, STAmount) << oneThird1;
WriteLog (lsINFO, STAmount) << oneThird2;
WriteLog (lsINFO, STAmount) << oneThird3;
STAmount twoThird1 = divRound (two, three, noIssue(), false);
STAmount twoThird2 = divide (two, three, noIssue());
STAmount twoThird3 = divRound (two, three, noIssue(), true);
WriteLog (lsINFO, STAmount) << twoThird1;
WriteLog (lsINFO, STAmount) << twoThird2;
WriteLog (lsINFO, STAmount) << twoThird3;
STAmount oneA = mulRound (oneThird1, three, noIssue(), false);
STAmount oneB = multiply (oneThird2, three, noIssue());
STAmount oneC = mulRound (oneThird3, three, noIssue(), true);
WriteLog (lsINFO, STAmount) << oneA;
WriteLog (lsINFO, STAmount) << oneB;
WriteLog (lsINFO, STAmount) << oneC;
STAmount fourThirdsA = addRound (twoThird2, twoThird2, false);
STAmount fourThirdsB = twoThird2 + twoThird2;
STAmount fourThirdsC = addRound (twoThird2, twoThird2, true);
WriteLog (lsINFO, STAmount) << fourThirdsA;
WriteLog (lsINFO, STAmount) << fourThirdsB;
WriteLog (lsINFO, STAmount) << fourThirdsC;
STAmount dripTest1 = mulRound (twoThird2, two, xrpIssue (), false);
STAmount dripTest2 = multiply (twoThird2, two, xrpIssue ());
STAmount dripTest3 = mulRound (twoThird2, two, xrpIssue (), true);
WriteLog (lsINFO, STAmount) << dripTest1;
WriteLog (lsINFO, STAmount) << dripTest2;
WriteLog (lsINFO, STAmount) << dripTest3;
#endif
}
//--------------------------------------------------------------------------
void run ()
{
testSetValue ();
testNativeCurrency ();
testCustomCurrency ();
testArithmetic ();
testUnderflow ();
testRounding ();
}
};
BEAST_DEFINE_TESTSUITE(STAmount,ripple_data,ripple);
} // ripple

View File

@@ -19,7 +19,7 @@
#include <ripple/basics/StringUtilities.h>
#include <ripple/protocol/STInteger.h>
#include <ripple/rpc/ErrorCodes.h> // VFALCO Questionable dependency
#include <ripple/protocol/ErrorCodes.h>
#include <beast/module/core/text/LexicalCast.h>
#include <cassert>

View File

@@ -17,7 +17,6 @@
*/
//==============================================================================
#include <ripple/core/Config.h>
#include <ripple/protocol/STValidation.h>
#include <ripple/protocol/HashPrefix.h>

View File

@@ -17,7 +17,7 @@
*/
//==============================================================================
#include <ripple/core/SystemParameters.h>
#include <ripple/protocol/SystemParameters.h>
#include <ripple/protocol/RippleAddress.h>
#include <ripple/protocol/UintTypes.h>

View File

@@ -0,0 +1,63 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
namespace ripple {
char charHex (int iDigit)
{
if (iDigit >= 0)
{
if(iDigit < 10)
return '0' + iDigit;
if(iDigit < 16)
return 'A' - 10 + iDigit;
}
return 0;
}
int charUnHex (unsigned char c)
{
struct HexTab
{
int hex[256];
HexTab ()
{
std::fill (std::begin (hex), std::end (hex), -1);
for (int i = 0; i < 10; ++i)
hex ['0'+i] = i;
for (int i = 0; i < 6; ++i)
{
hex ['A'+i] = 10 + i;
hex ['a'+i] = 10 + i;
}
}
int operator[] (unsigned char c) const
{
return hex[c];
}
};
static HexTab xtab;
return xtab[c];
}
}