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 7 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
8 changes: 7 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 @@ -349,6 +351,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 Expand Up @@ -597,6 +600,9 @@ extern SF_ACCOUNT const sfAttestationRewardAccount;
extern SF_ACCOUNT const sfLockingChainDoor;
extern SF_ACCOUNT const sfIssuingChainDoor;

// number (common)
extern SF_NUMBER const sfQuantity;

// path set
extern SField const sfPaths;

Expand Down
66 changes: 66 additions & 0 deletions include/xrpl/protocol/STNumber.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//------------------------------------------------------------------------------
/*
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>

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.

class STNumber final : public STBase,
thejohnfreeman marked this conversation as resolved.
Show resolved Hide resolved
public CountedObject<STNumber>,
public Number
{
public:
STNumber() = default;

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

View check run for this annotation

Codecov / codecov/patch

include/xrpl/protocol/STNumber.h#L34

Added line #L34 was not covered by tests
explicit STNumber(SField const& n, Number const& v = Number());
STNumber(SerialIter& sit, SField const& name);
thejohnfreeman marked this conversation as resolved.
Show resolved Hide resolved

using Number::operator=;

thejohnfreeman marked this conversation as resolved.
Show resolved Hide resolved
SerializedTypeID
getSType() const override;
std::string
getText() const override;
Json::Value getJson(JsonOptions) 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

STBase*
copy(std::size_t n, void* buf) const override;
thejohnfreeman marked this conversation as resolved.
Show resolved Hide resolved
STBase*
move(std::size_t n, void* buf) override;

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.

};

} // 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 @@ -243,6 +243,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 @@ -374,6 +376,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
11 changes: 11 additions & 0 deletions include/xrpl/protocol/Serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ class Serializer
add32(HashPrefix p);
int
add64(std::uint64_t i); // native currency amounts
//
int
addi32(std::int32_t i); // Number::exponent_
int
addi64(std::int64_t i); // Number::mantissa_

template <typename Integer>
int addInteger(Integer);
Expand Down Expand Up @@ -357,6 +362,12 @@ class SerialIter
std::uint64_t
get64();

std::int32_t
geti32();

std::int64_t
geti64();

template <std::size_t Bits, class Tag = void>
base_uint<Bits, Tag>
getBitString();
Expand Down
2 changes: 2 additions & 0 deletions src/libxrpl/protocol/SField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ CONSTRUCT_TYPED_SFIELD(sfHookHash, "HookHash", UINT256,
CONSTRUCT_TYPED_SFIELD(sfHookNamespace, "HookNamespace", UINT256, 32);
CONSTRUCT_TYPED_SFIELD(sfHookSetTxnID, "HookSetTxnID", UINT256, 33);

CONSTRUCT_TYPED_SFIELD(sfQuantity, "Quantity", NUMBER, 1);

// currency amount (common)
CONSTRUCT_TYPED_SFIELD(sfAmount, "Amount", AMOUNT, 1);
CONSTRUCT_TYPED_SFIELD(sfBalance, "Balance", AMOUNT, 2);
Expand Down
104 changes: 104 additions & 0 deletions src/libxrpl/protocol/STNumber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//------------------------------------------------------------------------------
/*
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& n, Number const& v) : STBase(n), Number(v)
{
}

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

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

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

Json::Value STNumber::getJson(JsonOptions) const

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

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STNumber.cpp#L51

Added line #L51 was not covered by tests
{
// TODO: come up with a string representation.
thejohnfreeman marked this conversation as resolved.
Show resolved Hide resolved
return to_string(*this);
}

void
STNumber::add(Serializer& s) const
{
assert(getFName().isBinary());
assert(getFName().fieldType == getSType());
s.addi64(this->mantissa());
s.addi32(this->exponent());
}

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

void
STNumber::setValue(Number const& v)

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

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STNumber.cpp#L73

Added line #L73 was not covered by tests
{
*this = v;

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

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STNumber.cpp#L75

Added line #L75 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STNumber.cpp#L79

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

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

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STNumber.cpp#L81

Added line #L81 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STNumber.cpp#L85

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

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

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STNumber.cpp#L87

Added line #L87 was not covered by tests
}

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

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

} // 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 @@ -659,6 +660,13 @@
return getFieldByConstRef<STCurrency>(field, empty);
}

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

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

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STObject.cpp#L664

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

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

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STObject.cpp#L667

Added line #L667 was not covered by tests
}

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

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

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

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STObject.cpp#L771

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

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

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STObject.cpp#L773

Added line #L773 was not covered by tests
}

void
STObject::setFieldPathSet(SField const& field, STPathSet const& v)
{
Expand Down
7 changes: 7 additions & 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 Expand Up @@ -135,6 +136,9 @@
case STI_AMOUNT:
construct<STAmount>(sit, name);
return;
case STI_NUMBER:
construct<STNumber>(sit, name);
return;

Check warning on line 141 in src/libxrpl/protocol/STVar.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STVar.cpp#L139-L141

Added lines #L139 - L141 were not covered by tests
case STI_UINT128:
construct<STUInt128>(sit, name);
return;
Expand Down Expand Up @@ -199,6 +203,9 @@
case STI_AMOUNT:
construct<STAmount>(name);
return;
case STI_NUMBER:
construct<STNumber>(name);
return;

Check warning on line 208 in src/libxrpl/protocol/STVar.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/STVar.cpp#L206-L208

Added lines #L206 - L208 were not covered by tests
case STI_UINT128:
construct<STUInt128>(name);
return;
Expand Down
51 changes: 51 additions & 0 deletions src/libxrpl/protocol/Serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <xrpl/basics/contract.h>
#include <xrpl/protocol/Serializer.h>
#include <xrpl/protocol/digest.h>
#include <boost/endian/conversion.hpp>
thejohnfreeman marked this conversation as resolved.
Show resolved Hide resolved
#include <type_traits>

namespace ripple {
Expand Down Expand Up @@ -71,6 +72,32 @@
return ret;
}

int
Serializer::addi32(std::int32_t i)
thejohnfreeman marked this conversation as resolved.
Show resolved Hide resolved
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider adding a private common (template?) function for 32 and 64 bit serialization. They are practically the same except for the most significant byte handling.

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
Serializer::addi64(std::int64_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 <>
int
Serializer::addInteger(unsigned char i)
Expand Down Expand Up @@ -410,6 +437,30 @@
(std::uint64_t(t[6]) << 8) + std::uint64_t(t[7]);
}

std::int32_t
SerialIter::geti32()
{
if (remain_ < 4)
Throw<std::runtime_error>("invalid SerialIter geti32");

Check warning on line 444 in src/libxrpl/protocol/Serializer.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/Serializer.cpp#L444

Added line #L444 was not covered by tests
auto t = p_;
p_ += 4;
used_ += 4;
remain_ -= 4;
return boost::endian::load_big_s32(t);
}

std::int64_t
SerialIter::geti64()
{
if (remain_ < 8)
Throw<std::runtime_error>("invalid SerialIter geti64");

Check warning on line 456 in src/libxrpl/protocol/Serializer.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxrpl/protocol/Serializer.cpp#L456

Added line #L456 was not covered by tests
auto t = p_;
p_ += 8;
used_ += 8;
remain_ -= 8;
return boost::endian::load_big_s64(t);
}

void
SerialIter::getFieldID(int& type, int& name)
{
Expand Down
Loading
Loading