Skip to content

Commit

Permalink
MPT amount base 10
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnxie999 committed Sep 9, 2024
1 parent 8fd18a5 commit 1fa15b1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 31 deletions.
35 changes: 26 additions & 9 deletions src/libxrpl/protocol/STInteger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ STUInt8::getText() const
}

template <>
Json::Value STUInt8::getJson(JsonOptions) const
Json::Value
STUInt8::getJson(JsonOptions) const
{
if (getFName() == sfTransactionResult)
{
Expand Down Expand Up @@ -118,7 +119,8 @@ STUInt16::getText() const
}

template <>
Json::Value STUInt16::getJson(JsonOptions) const
Json::Value
STUInt16::getJson(JsonOptions) const
{
if (getFName() == sfLedgerEntryType)
{
Expand Down Expand Up @@ -164,7 +166,8 @@ STUInt32::getText() const
}

template <>
Json::Value STUInt32::getJson(JsonOptions) const
Json::Value
STUInt32::getJson(JsonOptions) const
{
return value_;
}
Expand Down Expand Up @@ -192,13 +195,27 @@ STUInt64::getText() const
}

template <>
Json::Value STUInt64::getJson(JsonOptions) const
Json::Value
STUInt64::getJson(JsonOptions) const
{
std::string str(16, 0);
auto ret = std::to_chars(str.data(), str.data() + str.size(), value_, 16);
assert(ret.ec == std::errc());
str.resize(std::distance(str.data(), ret.ptr));
return str;
auto convertToString = [](uint64_t const value, int const base) {
std::string str(
base == 10 ? 20 : 16, 0); // Allocate space depending on base
auto ret =
std::to_chars(str.data(), str.data() + str.size(), value, base);
assert(ret.ec == std::errc());
str.resize(std::distance(str.data(), ret.ptr));
return str;
};

if (auto const& fName = getFName(); fName == sfMaximumAmount ||
fName == sfOutstandingAmount || fName == sfLockedAmount ||
fName == sfMPTAmount)
{
return convertToString(value_, 10); // Convert to base 10
}

return convertToString(value_, 16); // Convert to base 16
}

} // namespace ripple
6 changes: 5 additions & 1 deletion src/libxrpl/protocol/STParsedJSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,12 @@ parseLeaf(

std::uint64_t val;

// if the field is amount, serialize as base 10
auto [p, ec] = std::from_chars(
str.data(), str.data() + str.size(), val, 16);
str.data(),
str.data() + str.size(),
val,
(field == sfMaximumAmount) ? 10 : 16);

if (ec != std::errc() || (p != str.data() + str.size()))
Throw<std::invalid_argument>("invalid data");
Expand Down
16 changes: 8 additions & 8 deletions src/test/app/MPToken_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ class MPToken_test : public beast::unit_test::suite

// tries to set a txfee while not enabling in the flag
mptAlice.create(
{.maxAmt = 100,
{.maxAmt = "100",
.assetScale = 0,
.transferFee = 1,
.metadata = "test",
.err = temMALFORMED});

// tries to set a txfee greater than max
mptAlice.create(
{.maxAmt = 100,
{.maxAmt = "100",
.assetScale = 0,
.transferFee = maxTransferFee + 1,
.metadata = "test",
Expand All @@ -70,31 +70,31 @@ class MPToken_test : public beast::unit_test::suite

// tries to set a txfee while not enabling transfer
mptAlice.create(
{.maxAmt = 100,
{.maxAmt = "100",
.assetScale = 0,
.transferFee = maxTransferFee,
.metadata = "test",
.err = temMALFORMED});

// empty metadata returns error
mptAlice.create(
{.maxAmt = 100,
{.maxAmt = "100",
.assetScale = 0,
.transferFee = 0,
.metadata = "",
.err = temMALFORMED});

// MaximumAmout of 0 returns error
mptAlice.create(
{.maxAmt = 0,
{.maxAmt = "0",
.assetScale = 1,
.transferFee = 1,
.metadata = "test",
.err = temMALFORMED});

// MaximumAmount larger than 63 bit returns error
mptAlice.create(
{.maxAmt = 0xFFFFFFFFFFFFFFF0ull,
{.maxAmt = "18446744073709551600",
.assetScale = 0,
.transferFee = 0,
.metadata = "test",
Expand All @@ -116,7 +116,7 @@ class MPToken_test : public beast::unit_test::suite
Env env{*this, features};
MPTTester mptAlice(env, alice);
mptAlice.create(
{.maxAmt = 0x7FFFFFFFFFFFFFFF,
{.maxAmt = "9223372036854775807",
.assetScale = 1,
.transferFee = 10,
.metadata = "123",
Expand Down Expand Up @@ -781,7 +781,7 @@ class MPToken_test : public beast::unit_test::suite
MPTTester mptAlice(env, alice, {.holders = {&bob}});

mptAlice.create(
{.maxAmt = 100,
{.maxAmt = "100",
.ownerCount = 1,
.holderCount = 0,
.flags = tfMPTCanTransfer});
Expand Down
13 changes: 1 addition & 12 deletions src/test/jtx/impl/mpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ namespace ripple {
namespace test {
namespace jtx {

static std::array<std::uint8_t, 8>
uint64ToByteArray(std::uint64_t value)
{
value = boost::endian::native_to_big(value);
std::array<std::uint8_t, 8> result;
std::memcpy(result.data(), &value, sizeof(value));
return result;
}

void
mptflags::operator()(Env& env) const
{
Expand Down Expand Up @@ -105,10 +96,8 @@ MPTTester::create(const MPTCreate& arg)
jv[sfTransferFee.jsonName] = *arg.transferFee;
if (arg.metadata)
jv[sfMPTokenMetadata.jsonName] = strHex(*arg.metadata);

// convert maxAmt to hex string, since json doesn't accept 64-bit int
if (arg.maxAmt)
jv[sfMaximumAmount.jsonName] = strHex(uint64ToByteArray(*arg.maxAmt));
jv[sfMaximumAmount.jsonName] = *arg.maxAmt;
if (submit(arg, jv) != tesSUCCESS)
{
// Verify issuance doesn't exist
Expand Down
2 changes: 1 addition & 1 deletion src/test/jtx/mpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct MPTConstr

struct MPTCreate
{
std::optional<std::int64_t> maxAmt = std::nullopt;
std::optional<std::string> maxAmt = std::nullopt;
std::optional<std::uint8_t> assetScale = std::nullopt;
std::optional<std::uint16_t> transferFee = std::nullopt;
std::optional<std::string> metadata = std::nullopt;
Expand Down

0 comments on commit 1fa15b1

Please sign in to comment.