Skip to content

Commit

Permalink
Account tx type improvement (#1108)
Browse files Browse the repository at this point in the history
Fix #1090
  • Loading branch information
cindyyan317 authored Jan 16, 2024
1 parent eeaccba commit f97e069
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 107 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ target_sources (clio PRIVATE
src/util/Random.cpp
src/util/Taggable.cpp
src/util/TerminationHandler.cpp
src/util/TxUtil.cpp
)

# Clio server
Expand All @@ -190,6 +191,7 @@ if (tests)
unittests/util/TestGlobals.cpp
unittests/util/AssertTests.cpp
unittests/util/BatchingTests.cpp
unittests/util/TxUtilTests.cpp
unittests/util/TestObject.cpp
unittests/util/StringUtils.cpp
unittests/util/prometheus/CounterTests.cpp
Expand Down
73 changes: 7 additions & 66 deletions src/rpc/handlers/AccountTx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,68 +39,17 @@
#include <ripple/protocol/AccountID.h>
#include <ripple/protocol/ErrorCodes.h>
#include <ripple/protocol/LedgerHeader.h>
#include <ripple/protocol/TxFormats.h>
#include <ripple/protocol/jss.h>

#include <algorithm>
#include <cstdint>
#include <iterator>
#include <limits>
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <variant>

namespace rpc {

// found here : https://xrpl.org/transaction-types.html
std::unordered_map<std::string, ripple::TxType> const AccountTxHandler::TYPESMAP{
{JSL(AccountSet), ripple::ttACCOUNT_SET},
{JSL(AccountDelete), ripple::ttACCOUNT_DELETE},
{JSL(AMMBid), ripple::ttAMM_BID},
{JSL(AMMCreate), ripple::ttAMM_CREATE},
{JSL(AMMDelete), ripple::ttAMM_DELETE},
{JSL(AMMDeposit), ripple::ttAMM_DEPOSIT},
{JSL(AMMVote), ripple::ttAMM_VOTE},
{JSL(AMMWithdraw), ripple::ttAMM_WITHDRAW},
{JSL(CheckCancel), ripple::ttCHECK_CANCEL},
{JSL(CheckCash), ripple::ttCHECK_CASH},
{JSL(CheckCreate), ripple::ttCHECK_CREATE},
{JSL(Clawback), ripple::ttCLAWBACK},
{JSL(DepositPreauth), ripple::ttDEPOSIT_PREAUTH},
{JSL(EscrowCancel), ripple::ttESCROW_CANCEL},
{JSL(EscrowCreate), ripple::ttESCROW_CREATE},
{JSL(EscrowFinish), ripple::ttESCROW_FINISH},
{JSL(NFTokenAcceptOffer), ripple::ttNFTOKEN_ACCEPT_OFFER},
{JSL(NFTokenBurn), ripple::ttNFTOKEN_BURN},
{JSL(NFTokenCancelOffer), ripple::ttNFTOKEN_CANCEL_OFFER},
{JSL(NFTokenCreateOffer), ripple::ttNFTOKEN_CREATE_OFFER},
{JSL(NFTokenMint), ripple::ttNFTOKEN_MINT},
{JSL(OfferCancel), ripple::ttOFFER_CANCEL},
{JSL(OfferCreate), ripple::ttOFFER_CREATE},
{JSL(Payment), ripple::ttPAYMENT},
{JSL(PaymentChannelClaim), ripple::ttPAYCHAN_CLAIM},
{JSL(PaymentChannelCreate), ripple::ttCHECK_CREATE},
{JSL(PaymentChannelFund), ripple::ttPAYCHAN_FUND},
{JSL(SetRegularKey), ripple::ttREGULAR_KEY_SET},
{JSL(SignerListSet), ripple::ttSIGNER_LIST_SET},
{JSL(TicketCreate), ripple::ttTICKET_CREATE},
{JSL(TrustSet), ripple::ttTRUST_SET},
{JSL(DIDSet), ripple::ttDID_SET},
{JSL(DIDDelete), ripple::ttDID_DELETE},
};

// TODO: should be std::views::keys when clang supports it
std::unordered_set<std::string> const AccountTxHandler::TYPES_KEYS = [] {
std::unordered_set<std::string> keys;
std::transform(TYPESMAP.begin(), TYPESMAP.end(), std::inserter(keys, keys.begin()), [](auto const& pair) {
return pair.first;
});
return keys;
}();

// TODO: this is currently very similar to nft_history but its own copy for time
// being. we should aim to reuse common logic in some way in the future.
AccountTxHandler::Result
Expand Down Expand Up @@ -197,19 +146,13 @@ AccountTxHandler::process(AccountTxHandler::Input input, Context const& ctx) con

boost::json::object obj;

// if binary is true or transactionType is specified, we need to expand the transaction
if (!input.binary || input.transactionType.has_value()) {
// if binary is false or transactionType is specified, we need to expand the transaction
if (!input.binary || input.transactionTypeInLowercase.has_value()) {
auto [txn, meta] = toExpandedJson(txnPlusMeta, ctx.apiVersion, NFTokenjson::ENABLE);

if (txn.contains(JS(TransactionType))) {
auto const objTransactionType = txn[JS(TransactionType)];
auto const strType = util::toLower(objTransactionType.as_string().c_str());

// if transactionType does not match
if (input.transactionType.has_value() && AccountTxHandler::TYPESMAP.contains(strType) &&
AccountTxHandler::TYPESMAP.at(strType) != input.transactionType.value())
continue;
}
if (txn.contains(JS(TransactionType)) && input.transactionTypeInLowercase.has_value() &&
util::toLower(txn[JS(TransactionType)].as_string().c_str()) != input.transactionTypeInLowercase.value())
continue;

if (!input.binary) {
auto const txKey = ctx.apiVersion < 2u ? JS(tx) : JS(tx_json);
Expand Down Expand Up @@ -324,10 +267,8 @@ tag_invoke(boost::json::value_to_tag<AccountTxHandler::Input>, boost::json::valu
};
}

if (jsonObject.contains("tx_type")) {
auto objTransactionType = jsonObject.at("tx_type");
input.transactionType = AccountTxHandler::TYPESMAP.at(objTransactionType.as_string().c_str());
}
if (jsonObject.contains("tx_type"))
input.transactionTypeInLowercase = jsonObject.at("tx_type").as_string().c_str();

return input;
}
Expand Down
27 changes: 21 additions & 6 deletions src/rpc/handlers/AccountTx.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,31 @@
#pragma once

#include "data/BackendInterface.h"
#include "rpc/RPCHelpers.h"
#include "rpc/Errors.h"
#include "rpc/JS.h"
#include "rpc/common/JsonBool.h"
#include "rpc/common/MetaProcessors.h"
#include "rpc/common/Modifiers.h"
#include "rpc/common/Types.h"
#include "rpc/common/Validators.h"
#include "util/TxUtil.h"
#include "util/log/Logger.h"

#include <boost/json/array.hpp>
#include <boost/json/conversion.hpp>
#include <boost/json/object.hpp>
#include <boost/json/value.hpp>
#include <ripple/protocol/ErrorCodes.h>
#include <ripple/protocol/TxFormats.h>
#include <ripple/protocol/jss.h>

#include <cstdint>
#include <limits>
#include <memory>
#include <optional>
#include <string>
#include <unordered_set>

namespace rpc {

/**
Expand All @@ -39,9 +56,6 @@ class AccountTxHandler {
util::Logger log_{"RPC"};
std::shared_ptr<BackendInterface> sharedPtrBackend_;

static std::unordered_map<std::string, ripple::TxType> const TYPESMAP;
static std::unordered_set<std::string> const TYPES_KEYS;

public:
// no max limit
static auto constexpr LIMIT_MIN = 1;
Expand Down Expand Up @@ -77,7 +91,7 @@ class AccountTxHandler {
JsonBool forward{false};
std::optional<uint32_t> limit;
std::optional<Marker> marker;
std::optional<ripple::TxType> transactionType;
std::optional<std::string> transactionTypeInLowercase;
};

using Result = HandlerReturnType<Output>;
Expand All @@ -89,6 +103,7 @@ class AccountTxHandler {
static RpcSpecConstRef
spec([[maybe_unused]] uint32_t apiVersion)
{
auto const& typesKeysInLowercase = util::getTxTypesInLowercase();
static auto const rpcSpecForV1 = RpcSpec{
{JS(account), validation::Required{}, validation::AccountValidator},
{JS(ledger_hash), validation::Uint256HexStringValidator},
Expand All @@ -112,7 +127,7 @@ class AccountTxHandler {
"tx_type",
validation::Type<std::string>{},
modifiers::ToLower{},
validation::OneOf<std::string>(TYPES_KEYS.cbegin(), TYPES_KEYS.cend()),
validation::OneOf<std::string>(typesKeysInLowercase.cbegin(), typesKeysInLowercase.cend()),
},
};

Expand Down
47 changes: 47 additions & 0 deletions src/util/TxUtil.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2024, the clio developers.
Permission to use, copy, modify, and 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 "util/JsonUtils.h"

#include <ripple/protocol/TxFormats.h>

#include <algorithm>
#include <iterator>
#include <string>
#include <unordered_set>

namespace util {

std::unordered_set<std::string> const&
getTxTypesInLowercase()
{
static std::unordered_set<std::string> const typesKeysInLowercase = []() {
std::unordered_set<std::string> keys;
std::transform(
ripple::TxFormats::getInstance().begin(),
ripple::TxFormats::getInstance().end(),
std::inserter(keys, keys.begin()),
[](auto const& pair) { return util::toLower(pair.getName()); }
);
return keys;
}();

return typesKeysInLowercase;
}
} // namespace util
28 changes: 28 additions & 0 deletions src/util/TxUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2024, the clio developers.
Permission to use, copy, modify, and 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.
*/
//==============================================================================

#pragma once

#include <string>
#include <unordered_set>

namespace util {
std::unordered_set<std::string> const&
getTxTypesInLowercase();
} // namespace util
Loading

0 comments on commit f97e069

Please sign in to comment.