Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new serialized type: STNumber #5121

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions include/xrpl/basics/Number.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ class Number
int exponent_{std::numeric_limits<int>::lowest()};

public:
// The range for the mantissa when normalized
constexpr static std::int64_t minMantissa = 1'000'000'000'000'000LL;
constexpr static std::int64_t maxMantissa = 9'999'999'999'999'999LL;

// The range for the exponent when normalized
constexpr static int minExponent = -32768;
constexpr static int maxExponent = 32768;

struct unchecked
{
explicit unchecked() = default;
Expand Down Expand Up @@ -191,14 +199,6 @@ class Number
constexpr bool
isnormal() const noexcept;

// The range for the mantissa when normalized
constexpr static std::int64_t minMantissa = 1'000'000'000'000'000LL;
constexpr static std::int64_t maxMantissa = 9'999'999'999'999'999LL;

// The range for the exponent when normalized
constexpr static int minExponent = -32768;
constexpr static int maxExponent = 32768;

class Guard;
};

Expand Down
5 changes: 4 additions & 1 deletion include/xrpl/protocol/SField.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ template <int>
class STBitString;
template <class>
class STInteger;
class STNumber;
class STXChainBridge;
class STVector256;
class STCurrency;
Expand All @@ -70,8 +71,9 @@ class STCurrency;
STYPE(STI_AMOUNT, 6) \
STYPE(STI_VL, 7) \
STYPE(STI_ACCOUNT, 8) \
STYPE(STI_NUMBER, 9) \
\
/* 9-13 are reserved */ \
/* 10-13 are reserved */ \
STYPE(STI_OBJECT, 14) \
STYPE(STI_ARRAY, 15) \
\
Expand Down Expand Up @@ -355,6 +357,7 @@ using SF_ACCOUNT = TypedField<STAccount>;
using SF_AMOUNT = TypedField<STAmount>;
using SF_ISSUE = TypedField<STIssue>;
using SF_CURRENCY = TypedField<STCurrency>;
using SF_NUMBER = TypedField<STNumber>;
using SF_VL = TypedField<STBlob>;
using SF_VECTOR256 = TypedField<STVector256>;
using SF_XCHAIN_BRIDGE = TypedField<STXChainBridge>;
Expand Down
88 changes: 88 additions & 0 deletions include/xrpl/protocol/STNumber.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2024 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 XRPL_PROTOCOL_STNUMBER_H_INCLUDED
#define XRPL_PROTOCOL_STNUMBER_H_INCLUDED

#include <xrpl/basics/CountedObject.h>
#include <xrpl/basics/Number.h>
#include <xrpl/protocol/STBase.h>

#include <ostream>

namespace ripple {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a brief description of the class.

/**
* A serializable number.
*
* This type is-a `Number`, and can be used everywhere that is accepted.
* This type simply integrates `Number` with the serialization framework,
* letting it be used for fields in ledger entries and transactions.
* It is effectively an `STAmount` sans `Asset`:
* it can represent a value of any token type (XRP, IOU, or MPT)
* without paying the storage cost of duplicating asset information
* that may be deduced from the context.
*/
class STNumber : public STBase, public CountedObject<STNumber>
{
private:
Number value_;

public:
using value_type = Number;

STNumber() = default;

Check warning on line 50 in include/xrpl/protocol/STNumber.h

View check run for this annotation

Codecov / codecov/patch

include/xrpl/protocol/STNumber.h#L50

Added line #L50 was not covered by tests
explicit STNumber(SField const& field, Number const& value = Number());
STNumber(SerialIter& sit, SField const& field);

thejohnfreeman marked this conversation as resolved.
Show resolved Hide resolved
SerializedTypeID
getSType() const override;
std::string
getText() const override;
void
add(Serializer& s) const override;

Number const&
value() const;
void
setValue(Number const& v);
thejohnfreeman marked this conversation as resolved.
Show resolved Hide resolved

bool
isEquivalent(STBase const& t) const override;
bool
isDefault() const override;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add overload for operator<<(std::ostream& os, STNumber const& x), otherwise std::cout of STNumber doesn't compile.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't it just use the existing overload for Number?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ambiguous because both Number and STBase have it overloaded.


operator Number() const
{
return value_;
}

private:
STBase*
copy(std::size_t n, void* buf) const override;
STBase*
move(std::size_t n, void* buf) override;
};

std::ostream&
operator<<(std::ostream& out, STNumber const& rhs);

} // namespace ripple

#endif
4 changes: 4 additions & 0 deletions include/xrpl/protocol/STObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ class STObject : public STBase, public CountedObject<STObject>
getFieldArray(SField const& field) const;
const STCurrency&
getFieldCurrency(SField const& field) const;
STNumber const&
getFieldNumber(SField const& field) const;

/** Get the value of a field.
@param A TypedField built from an SField value representing the desired
Expand Down Expand Up @@ -376,6 +378,8 @@ class STObject : public STBase, public CountedObject<STObject>
void
setFieldCurrency(SField const& field, STCurrency const&);
void
setFieldNumber(SField const& field, STNumber const&);
void
setFieldPathSet(SField const& field, STPathSet const&);
void
setFieldV256(SField const& field, STVector256 const& v);
Expand Down
39 changes: 37 additions & 2 deletions include/xrpl/protocol/Serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,43 @@ class Serializer
add8(unsigned char i);
int
add16(std::uint16_t i);

template <typename T>
requires(std::is_same_v<
std::make_unsigned_t<std::remove_cv_t<T>>,
std::uint32_t>)
int
add32(std::uint32_t i); // ledger indexes, account sequence, timestamps
add32(T i)
{
int ret = mData.size();
mData.push_back(static_cast<unsigned char>((i >> 24) & 0xff));
mData.push_back(static_cast<unsigned char>((i >> 16) & 0xff));
mData.push_back(static_cast<unsigned char>((i >> 8) & 0xff));
mData.push_back(static_cast<unsigned char>(i & 0xff));
return ret;
}

int
add32(HashPrefix p);

template <typename T>
requires(std::is_same_v<
std::make_unsigned_t<std::remove_cv_t<T>>,
std::uint64_t>)
int
add64(std::uint64_t i); // native currency amounts
add64(T i)
{
int ret = mData.size();
mData.push_back(static_cast<unsigned char>((i >> 56) & 0xff));
mData.push_back(static_cast<unsigned char>((i >> 48) & 0xff));
mData.push_back(static_cast<unsigned char>((i >> 40) & 0xff));
mData.push_back(static_cast<unsigned char>((i >> 32) & 0xff));
mData.push_back(static_cast<unsigned char>((i >> 24) & 0xff));
mData.push_back(static_cast<unsigned char>((i >> 16) & 0xff));
mData.push_back(static_cast<unsigned char>((i >> 8) & 0xff));
mData.push_back(static_cast<unsigned char>(i & 0xff));
return ret;
}

template <typename Integer>
int addInteger(Integer);
Expand Down Expand Up @@ -353,9 +384,13 @@ class SerialIter

std::uint32_t
get32();
std::int32_t
geti32();

std::uint64_t
get64();
std::int64_t
geti64();

template <std::size_t Bits, class Tag = void>
base_uint<Bits, Tag>
Expand Down
3 changes: 3 additions & 0 deletions include/xrpl/protocol/detail/sfields.macro
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ TYPED_SFIELD(sfHookHash, UINT256, 31)
TYPED_SFIELD(sfHookNamespace, UINT256, 32)
TYPED_SFIELD(sfHookSetTxnID, UINT256, 33)

// number (common)
TYPED_SFIELD(sfNumber, NUMBER, 1)

// currency amount (common)
TYPED_SFIELD(sfAmount, AMOUNT, 1)
TYPED_SFIELD(sfBalance, AMOUNT, 2)
Expand Down
1 change: 1 addition & 0 deletions include/xrpl/protocol/st.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <xrpl/protocol/STBlob.h>
#include <xrpl/protocol/STInteger.h>
#include <xrpl/protocol/STLedgerEntry.h>
#include <xrpl/protocol/STNumber.h>
#include <xrpl/protocol/STObject.h>
#include <xrpl/protocol/STParsedJSON.h>
#include <xrpl/protocol/STPathSet.h>
Expand Down
105 changes: 105 additions & 0 deletions src/libxrpl/protocol/STNumber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2023 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 <xrpl/protocol/STNumber.h>

#include <xrpl/protocol/SField.h>

namespace ripple {

STNumber::STNumber(SField const& field, Number const& value)
: STBase(field), value_(value)
{
}

STNumber::STNumber(SerialIter& sit, SField const& field) : STBase(field)
{
// We must call these methods in separate statements
// to guarantee their order of execution.
auto mantissa = sit.geti64();
auto exponent = sit.geti32();
value_ = Number{mantissa, exponent};
}

SerializedTypeID
STNumber::getSType() const
{
return STI_NUMBER;
}

std::string
STNumber::getText() const
{
return to_string(value_);
}

void
STNumber::add(Serializer& s) const
{
assert(getFName().isBinary());
assert(getFName().fieldType == getSType());
s.add64(value_.mantissa());
s.add32(value_.exponent());
}

Number const&
STNumber::value() const
{
return value_;
}

void
STNumber::setValue(Number const& v)

Check warning on line 68 in src/libxrpl/protocol/STNumber.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STNumber.cpp#L68

Added line #L68 was not covered by tests
{
value_ = v;

Check warning on line 70 in src/libxrpl/protocol/STNumber.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STNumber.cpp#L70

Added line #L70 was not covered by tests
}

STBase*
STNumber::copy(std::size_t n, void* buf) const

Check warning on line 74 in src/libxrpl/protocol/STNumber.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STNumber.cpp#L74

Added line #L74 was not covered by tests
{
return emplace(n, buf, *this);

Check warning on line 76 in src/libxrpl/protocol/STNumber.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STNumber.cpp#L76

Added line #L76 was not covered by tests
}

STBase*
STNumber::move(std::size_t n, void* buf)

Check warning on line 80 in src/libxrpl/protocol/STNumber.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STNumber.cpp#L80

Added line #L80 was not covered by tests
{
return emplace(n, buf, std::move(*this));

Check warning on line 82 in src/libxrpl/protocol/STNumber.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STNumber.cpp#L82

Added line #L82 was not covered by tests
}

bool
STNumber::isEquivalent(STBase const& t) const
{
assert(t.getSType() == this->getSType());
STNumber const& v = dynamic_cast<STNumber const&>(t);
return value_ == v;
}

bool
STNumber::isDefault() const
{
return value_ == Number();
}

std::ostream&
operator<<(std::ostream& out, STNumber const& rhs)

Check warning on line 100 in src/libxrpl/protocol/STNumber.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STNumber.cpp#L100

Added line #L100 was not covered by tests
{
return out << rhs.getText();
}

} // namespace ripple
14 changes: 14 additions & 0 deletions src/libxrpl/protocol/STObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <xrpl/protocol/STArray.h>
#include <xrpl/protocol/STBlob.h>
#include <xrpl/protocol/STCurrency.h>
#include <xrpl/protocol/STNumber.h>
#include <xrpl/protocol/STObject.h>

namespace ripple {
Expand Down Expand Up @@ -665,6 +666,13 @@
return getFieldByConstRef<STCurrency>(field, empty);
}

STNumber const&
STObject::getFieldNumber(SField const& field) const

Check warning on line 670 in src/libxrpl/protocol/STObject.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STObject.cpp#L670

Added line #L670 was not covered by tests
{
static STNumber const empty{};
return getFieldByConstRef<STNumber>(field, empty);

Check warning on line 673 in src/libxrpl/protocol/STObject.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STObject.cpp#L673

Added line #L673 was not covered by tests
}

void
STObject::set(std::unique_ptr<STBase> v)
{
Expand Down Expand Up @@ -765,6 +773,12 @@
setFieldUsingAssignment(field, v);
}

void
STObject::setFieldNumber(SField const& field, STNumber const& v)

Check warning on line 777 in src/libxrpl/protocol/STObject.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STObject.cpp#L777

Added line #L777 was not covered by tests
{
setFieldUsingAssignment(field, v);

Check warning on line 779 in src/libxrpl/protocol/STObject.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STObject.cpp#L779

Added line #L779 was not covered by tests
}

void
STObject::setFieldPathSet(SField const& field, STPathSet const& v)
{
Expand Down
1 change: 1 addition & 0 deletions src/libxrpl/protocol/STVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <xrpl/protocol/STCurrency.h>
#include <xrpl/protocol/STInteger.h>
#include <xrpl/protocol/STIssue.h>
#include <xrpl/protocol/STNumber.h>
#include <xrpl/protocol/STObject.h>
#include <xrpl/protocol/STPathSet.h>
#include <xrpl/protocol/STVector256.h>
Expand Down
Loading
Loading