From b0e6be93ff1ede8b30a7e83360324869b5cc71c5 Mon Sep 17 00:00:00 2001 From: Nik Bougalis Date: Sun, 23 Aug 2015 09:22:34 -0700 Subject: [PATCH] Add Rate class to support scaling of IOU and XRP amounts --- Builds/VisualStudio2015/RippleD.vcxproj | 6 ++ .../VisualStudio2015/RippleD.vcxproj.filters | 6 ++ src/ripple/protocol/Quality.h | 2 +- src/ripple/protocol/Rate.h | 78 +++++++++++++++++++ src/ripple/protocol/impl/Rate2.cpp | 28 +++++++ src/ripple/unity/protocol.cpp | 1 + 6 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/ripple/protocol/Rate.h create mode 100644 src/ripple/protocol/impl/Rate2.cpp diff --git a/Builds/VisualStudio2015/RippleD.vcxproj b/Builds/VisualStudio2015/RippleD.vcxproj index 8c5fc2d7e3..4c78178611 100644 --- a/Builds/VisualStudio2015/RippleD.vcxproj +++ b/Builds/VisualStudio2015/RippleD.vcxproj @@ -2799,6 +2799,10 @@ True True + + True + True + True True @@ -2921,6 +2925,8 @@ + + diff --git a/Builds/VisualStudio2015/RippleD.vcxproj.filters b/Builds/VisualStudio2015/RippleD.vcxproj.filters index be0613c568..94be47bd1a 100644 --- a/Builds/VisualStudio2015/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2015/RippleD.vcxproj.filters @@ -3501,6 +3501,9 @@ ripple\protocol\impl + + ripple\protocol\impl + ripple\protocol\impl @@ -3612,6 +3615,9 @@ ripple\protocol + + ripple\protocol + ripple\protocol diff --git a/src/ripple/protocol/Quality.h b/src/ripple/protocol/Quality.h index b5e3d7b8d0..1ed9b42de2 100644 --- a/src/ripple/protocol/Quality.h +++ b/src/ripple/protocol/Quality.h @@ -74,7 +74,7 @@ operator!= (Amounts const& lhs, Amounts const& rhs) noexcept //------------------------------------------------------------------------------ // Ripple specific constant used for parsing qualities and other things -#define QUALITY_ONE 1000000000 // 10e9 +#define QUALITY_ONE 1000000000 /** Represents the logical ratio of output currency to input currency. Internally this is stored using a custom floating point representation, diff --git a/src/ripple/protocol/Rate.h b/src/ripple/protocol/Rate.h new file mode 100644 index 0000000000..3a31d79aa1 --- /dev/null +++ b/src/ripple/protocol/Rate.h @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2015 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_RATE_H_INCLUDED +#define RIPPLE_PROTOCOL_RATE_H_INCLUDED + +#include +#include +#include +#include +#include +#include + +namespace ripple { + +/** Represents a transfer rate + + Transfer rates are specified as fractions of 1 billion. For example, a + transfer rate of 1% is represented as 1010000000. +*/ +struct Rate + : private boost::totally_ordered +{ + std::uint32_t value; + + Rate () = default; + + Rate (std::uint32_t rate) + : value (rate) + { + assert (rate != 0); + } +}; + +inline +bool +operator== (Rate const& lhs, Rate const& rhs) noexcept +{ + return lhs.value == rhs.value; +} + +inline +bool +operator< (Rate const& lhs, Rate const& rhs) noexcept +{ + return lhs.value < rhs.value; +} + +inline +std::ostream& +operator<< (std::ostream& os, Rate const& rate) +{ + os << rate.value; + return os; +} + +/** A transfer rate signifying a 1:1 exchange */ +extern Rate const parityRate; + +} + +#endif diff --git a/src/ripple/protocol/impl/Rate2.cpp b/src/ripple/protocol/impl/Rate2.cpp new file mode 100644 index 0000000000..0d44d6ad1c --- /dev/null +++ b/src/ripple/protocol/impl/Rate2.cpp @@ -0,0 +1,28 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2015 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 +#include +#include + +namespace ripple { + +Rate const parityRate (QUALITY_ONE); + +} diff --git a/src/ripple/unity/protocol.cpp b/src/ripple/unity/protocol.cpp index 26de4fede9..eb69f77605 100644 --- a/src/ripple/unity/protocol.cpp +++ b/src/ripple/unity/protocol.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include