Modularize app/tx:

* Move LedgerView declaration
* Move CrossType
* Move Clock declaration
* Move Quality to protocol/
* Move Amounts to protocol/
* Move book to tx/impl
* Remove 0.27 legacy support
* Remove unused AmountType
* Remove core namespace
* Use STAmount
This commit is contained in:
Vinnie Falco
2015-05-18 12:43:34 -07:00
parent 399c43cae6
commit 2f3834359e
75 changed files with 503 additions and 3541 deletions

View File

@@ -46,8 +46,18 @@ struct Protocol
static int const txMaxSizeBytes = 1024 * 1024; // 1048576
};
/** A ledger index.
/** A clock representing network time.
This measures seconds since the Ripple epoch as seen
by the ledger close clock.
*/
class Clock // : public abstract_clock <std::chrono::seconds>
{
public:
using time_point = std::uint32_t;
using duration = std::chrono::seconds;
};
/** A ledger index. */
// VFALCO TODO pick one. I like Index since its not an abbreviation
typedef std::uint32_t LedgerIndex;
// VFALCO NOTE "LedgerSeq" appears in some SQL statement text

View File

@@ -0,0 +1,187 @@
//------------------------------------------------------------------------------
/*
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.
*/
//==============================================================================
#ifndef RIPPLE_PROTOCOL_QUALITY_H_INCLUDED
#define RIPPLE_PROTOCOL_QUALITY_H_INCLUDED
#include <ripple/protocol/STAmount.h>
#include <beast/utility/noexcept.h>
#include <cstdint>
#include <ostream>
namespace ripple {
/** Represents a pair of input and output currencies.
The input currency can be converted to the output
currency by multiplying by the rate, represented by
Quality.
For offers, "in" is always TakerPays and "out" is
always TakerGets.
*/
struct Amounts
{
Amounts() = default;
Amounts (STAmount const& in_, STAmount const& out_)
: in (in_)
, out (out_)
{
}
/** Returns `true` if either quantity is not positive. */
bool
empty() const noexcept
{
return in <= zero || out <= zero;
}
STAmount in;
STAmount out;
};
inline
bool
operator== (Amounts const& lhs, Amounts const& rhs) noexcept
{
return lhs.in == rhs.in && lhs.out == rhs.out;
}
inline
bool
operator!= (Amounts const& lhs, Amounts const& rhs) noexcept
{
return ! (lhs == rhs);
}
//------------------------------------------------------------------------------
// Ripple specific constant used for parsing qualities and other things
#define QUALITY_ONE 1000000000 // 10e9
/** Represents the logical ratio of output currency to input currency.
Internally this is stored using a custom floating point representation,
as the inverse of the ratio, so that quality will be descending in
a sequence of actual values that represent qualities.
*/
class Quality
{
public:
// Type of the internal representation. Higher qualities
// have lower unsigned integer representations.
typedef std::uint64_t value_type;
private:
value_type m_value;
public:
Quality() = default;
/** Create a quality from the integer encoding of an STAmount */
explicit
Quality (std::uint64_t value);
/** Create a quality from the ratio of two amounts. */
explicit
Quality (Amounts const& amount);
/** Advances to the next higher quality level. */
/** @{ */
Quality&
operator++();
Quality
operator++ (int);
/** @} */
/** Advances to the next lower quality level. */
/** @{ */
Quality&
operator--();
Quality
operator-- (int);
/** @} */
/** Returns the quality as STAmount. */
STAmount
rate () const
{
return amountFromQuality (m_value);
}
/** Returns the scaled amount with in capped.
Math is avoided if the result is exact. The output is clamped
to prevent money creation.
*/
Amounts
ceil_in (Amounts const& amount, STAmount const& limit) const;
/** Returns the scaled amount with out capped.
Math is avoided if the result is exact. The input is clamped
to prevent money creation.
*/
Amounts
ceil_out (Amounts const& amount, STAmount const& limit) const;
/** Returns `true` if lhs is lower quality than `rhs`.
Lower quality means the taker receives a worse deal.
Higher quality is better for the taker.
*/
friend
bool
operator< (Quality const& lhs, Quality const& rhs) noexcept
{
return lhs.m_value > rhs.m_value;
}
friend
bool
operator== (Quality const& lhs, Quality const& rhs) noexcept
{
return lhs.m_value == rhs.m_value;
}
friend
bool
operator!= (Quality const& lhs, Quality const& rhs) noexcept
{
return ! (lhs == rhs);
}
friend
std::ostream&
operator<< (std::ostream& os, Quality const& quality)
{
os << quality.m_value;
return os;
}
};
/** Calculate the quality of a two-hop path given the two hops.
@param lhs The first leg of the path: input to intermediate.
@param rhs The second leg of the path: intermediate to output.
*/
Quality
composed_quality (Quality const& lhs, Quality const& rhs);
}
#endif

View File

@@ -0,0 +1,123 @@
//------------------------------------------------------------------------------
/*
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 <BeastConfig.h>
#include <ripple/protocol/Quality.h>
#include <cassert>
#include <limits>
namespace ripple {
Quality::Quality (std::uint64_t value)
: m_value (value)
{
}
Quality::Quality (Amounts const& amount)
: m_value (getRate (amount.out, amount.in))
{
}
Quality&
Quality::operator++()
{
assert (m_value > 0);
--m_value;
return *this;
}
Quality
Quality::operator++ (int)
{
Quality prev (*this);
++*this;
return prev;
}
Quality&
Quality::operator--()
{
assert (m_value < std::numeric_limits<value_type>::max());
++m_value;
return *this;
}
Quality
Quality::operator-- (int)
{
Quality prev (*this);
--*this;
return prev;
}
Amounts
Quality::ceil_in (Amounts const& amount, STAmount const& limit) const
{
if (amount.in > limit)
{
Amounts result (limit, divRound (
limit, rate(), amount.out.issue (), true));
// Clamp out
if (result.out > amount.out)
result.out = amount.out;
assert (result.in == limit);
return result;
}
assert (amount.in <= limit);
return amount;
}
Amounts
Quality::ceil_out (Amounts const& amount, STAmount const& limit) const
{
if (amount.out > limit)
{
Amounts result (mulRound (
limit, rate(), amount.in.issue (), true), limit);
// Clamp in
if (result.in > amount.in)
result.in = amount.in;
assert (result.out == limit);
return result;
}
assert (amount.out <= limit);
return amount;
}
Quality
composed_quality (Quality const& lhs, Quality const& rhs)
{
STAmount const lhs_rate (lhs.rate ());
assert (lhs_rate != zero);
STAmount const rhs_rate (rhs.rate ());
assert (rhs_rate != zero);
STAmount const rate (mulRound (
lhs_rate, rhs_rate, lhs_rate.issue (), true));
std::uint64_t const stored_exponent (rate.exponent () + 100);
std::uint64_t const stored_mantissa (rate.mantissa());
assert ((stored_exponent > 0) && (stored_exponent <= 255));
return Quality ((stored_exponent << (64 - 8)) | stored_mantissa);
}
}

View File

@@ -28,7 +28,6 @@
#include <ripple/protocol/TxFlags.h>
#include <ripple/basics/StringUtilities.h>
#include <ripple/json/to_string.h>
#include <ripple/legacy/0.27/Emulate027.h>
#include <beast/unit_test/suite.h>
#include <boost/format.hpp>
#include <array>

View File

@@ -0,0 +1,322 @@
//------------------------------------------------------------------------------
/*
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 <BeastConfig.h>
#include <ripple/protocol/Quality.h>
#include <beast/unit_test/suite.h>
#include <beast/cxx14/type_traits.h>
namespace ripple {
class Quality_test : public beast::unit_test::suite
{
public:
// Create a raw, non-integral amount from mantissa and exponent
STAmount
static raw (std::uint64_t mantissa, int exponent)
{
return STAmount ({Currency(3), Account(3)}, mantissa, exponent);
}
template <class Integer>
static
STAmount
amount (Integer integer,
std::enable_if_t <std::is_signed <Integer>::value>* = 0)
{
static_assert (std::is_integral <Integer>::value, "");
return STAmount (integer, false);
}
template <class Integer>
static
STAmount
amount (Integer integer,
std::enable_if_t <! std::is_signed <Integer>::value>* = 0)
{
static_assert (std::is_integral <Integer>::value, "");
if (integer < 0)
return STAmount (-integer, true);
return STAmount (integer, false);
}
template <class In, class Out>
static
Amounts
amounts (In in, Out out)
{
return Amounts (amount(in), amount(out));
}
template <class In1, class Out1, class Int, class In2, class Out2>
void
ceil_in (Quality const& q,
In1 in, Out1 out, Int limit, In2 in_expected, Out2 out_expected)
{
auto expect_result (amounts (in_expected, out_expected));
auto actual_result (q.ceil_in (amounts(in, out), amount(limit)));
expect (actual_result == expect_result);
}
template <class In1, class Out1, class Int, class In2, class Out2>
void
ceil_out (Quality const& q,
In1 in, Out1 out, Int limit, In2 in_expected, Out2 out_expected)
{
auto const expect_result (amounts (in_expected, out_expected));
auto const actual_result (q.ceil_out (amounts(in, out), amount(limit)));
expect (actual_result == expect_result);
}
void
test_ceil_in ()
{
testcase ("ceil_in");
{
// 1 in, 1 out:
Quality q (Amounts (amount(1), amount(1)));
ceil_in (q,
1, 1, // 1 in, 1 out
1, // limit: 1
1, 1); // 1 in, 1 out
ceil_in (q,
10, 10, // 10 in, 10 out
5, // limit: 5
5, 5); // 5 in, 5 out
ceil_in (q,
5, 5, // 5 in, 5 out
10, // limit: 10
5, 5); // 5 in, 5 out
}
{
// 1 in, 2 out:
Quality q (Amounts (amount(1), amount(2)));
ceil_in (q,
40, 80, // 40 in, 80 out
40, // limit: 40
40, 80); // 40 in, 20 out
ceil_in (q,
40, 80, // 40 in, 80 out
20, // limit: 20
20, 40); // 20 in, 40 out
ceil_in (q,
40, 80, // 40 in, 80 out
60, // limit: 60
40, 80); // 40 in, 80 out
}
{
// 2 in, 1 out:
Quality q (Amounts (amount(2), amount(1)));
ceil_in (q,
40, 20, // 40 in, 20 out
20, // limit: 20
20, 10); // 20 in, 10 out
ceil_in (q,
40, 20, // 40 in, 20 out
40, // limit: 40
40, 20); // 40 in, 20 out
ceil_in (q,
40, 20, // 40 in, 20 out
50, // limit: 40
40, 20); // 40 in, 20 out
}
}
void
test_ceil_out ()
{
testcase ("ceil_out");
{
// 1 in, 1 out:
Quality q (Amounts (amount(1),amount(1)));
ceil_out (q,
1, 1, // 1 in, 1 out
1, // limit 1
1, 1); // 1 in, 1 out
ceil_out (q,
10, 10, // 10 in, 10 out
5, // limit 5
5, 5); // 5 in, 5 out
ceil_out (q,
10, 10, // 10 in, 10 out
20, // limit 20
10, 10); // 10 in, 10 out
}
{
// 1 in, 2 out:
Quality q (Amounts (amount(1),amount(2)));
ceil_out (q,
40, 80, // 40 in, 80 out
40, // limit 40
20, 40); // 20 in, 40 out
ceil_out (q,
40, 80, // 40 in, 80 out
80, // limit 80
40, 80); // 40 in, 80 out
ceil_out (q,
40, 80, // 40 in, 80 out
100, // limit 100
40, 80); // 40 in, 80 out
}
{
// 2 in, 1 out:
Quality q (Amounts (amount(2),amount(1)));
ceil_out (q,
40, 20, // 40 in, 20 out
20, // limit 20
40, 20); // 40 in, 20 out
ceil_out (q,
40, 20, // 40 in, 20 out
40, // limit 40
40, 20); // 40 in, 20 out
ceil_out (q,
40, 20, // 40 in, 20 out
10, // limit 10
20, 10); // 20 in, 10 out
}
}
void
test_raw()
{
testcase ("raw");
{
Quality q (0x5d048191fb9130daull); // 126836389.7680090
Amounts const value (
amount(349469768), // 349.469768 XRP
raw (2755280000000000ull, -15)); // 2.75528
STAmount const limit (
raw (4131113916555555, -16)); // .4131113916555555
Amounts const result (q.ceil_out (value, limit));
expect (result.in != zero);
}
}
void
test_comparisons()
{
testcase ("comparisons");
STAmount const amount1 (noIssue(), 231);
STAmount const amount2 (noIssue(), 462);
STAmount const amount3 (noIssue(), 924);
Quality const q11 (Amounts (amount1, amount1));
Quality const q12 (Amounts (amount1, amount2));
Quality const q13 (Amounts (amount1, amount3));
Quality const q21 (Amounts (amount2, amount1));
Quality const q31 (Amounts (amount3, amount1));
expect (q11 == q11);
expect (q11 < q12);
expect (q12 < q13);
expect (q31 < q21);
expect (q21 < q11);
expect (q31 != q21);
}
void
test_composition ()
{
testcase ("composition");
STAmount const amount1 (noIssue(), 231);
STAmount const amount2 (noIssue(), 462);
STAmount const amount3 (noIssue(), 924);
Quality const q11 (Amounts (amount1, amount1));
Quality const q12 (Amounts (amount1, amount2));
Quality const q13 (Amounts (amount1, amount3));
Quality const q21 (Amounts (amount2, amount1));
Quality const q31 (Amounts (amount3, amount1));
expect (composed_quality (q12, q21) == q11);
Quality const q13_31 (composed_quality (q13, q31));
Quality const q31_13 (composed_quality (q31, q13));
expect (q13_31 == q31_13);
expect (q13_31 == q11);
}
void
test_operations ()
{
testcase ("operations");
Quality const q11 (Amounts (
STAmount (noIssue(), 731),
STAmount (noIssue(), 731)));
Quality qa (q11);
Quality qb (q11);
expect (qa == qb);
expect (++qa != q11);
expect (qa != qb);
expect (--qb != q11);
expect (qa != qb);
expect (qb < qa);
expect (qb++ < qa);
expect (qb++ < qa);
expect (qb++ == qa);
expect (qa < qb);
}
void
run()
{
test_comparisons ();
test_composition ();
test_operations ();
test_ceil_in ();
test_ceil_out ();
test_raw ();
}
};
BEAST_DEFINE_TESTSUITE(Quality,protocol,ripple);
}