mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-27 23:25:53 +00:00
Implement transaction_entry
This commit is contained in:
@@ -59,41 +59,32 @@ make_HttpContext(
|
||||
range};
|
||||
}
|
||||
|
||||
void
|
||||
inject_error(Error err, boost::json::object& json)
|
||||
boost::json::object
|
||||
make_error(Error err)
|
||||
{
|
||||
boost::json::object json;
|
||||
ripple::RPC::ErrorInfo const& info(ripple::RPC::get_error_info(err));
|
||||
json["error"] = info.token;
|
||||
json["error_code"] = static_cast<std::uint32_t>(err);
|
||||
json["error_message"] = info.message;
|
||||
json["status"] = "error";
|
||||
json["type"] = "response";
|
||||
}
|
||||
|
||||
void
|
||||
inject_error(Error err, std::string const& message, boost::json::object& json)
|
||||
{
|
||||
ripple::RPC::ErrorInfo const& info(ripple::RPC::get_error_info(err));
|
||||
json["error"] = info.token;
|
||||
json["error_code"] = static_cast<std::uint32_t>(err);
|
||||
json["error_message"] = message;
|
||||
json["status"] = "error";
|
||||
json["type"] = "response";
|
||||
}
|
||||
|
||||
boost::json::object
|
||||
make_error(Error err)
|
||||
{
|
||||
boost::json::object json{};
|
||||
inject_error(err, json);
|
||||
return json;
|
||||
}
|
||||
|
||||
boost::json::object
|
||||
make_error(Error err, std::string const& message)
|
||||
make_error(Status const& status)
|
||||
{
|
||||
boost::json::object json{};
|
||||
inject_error(err, message, json);
|
||||
boost::json::object json;
|
||||
ripple::RPC::ErrorInfo const& info(
|
||||
ripple::RPC::get_error_info(status.error));
|
||||
json["error"] =
|
||||
status.strCode.size() ? status.strCode.c_str() : info.token.c_str();
|
||||
json["error_code"] = static_cast<std::uint32_t>(status.error);
|
||||
json["error_message"] =
|
||||
status.message.size() ? status.message.c_str() : info.message.c_str();
|
||||
json["status"] = "error";
|
||||
json["type"] = "response";
|
||||
return json;
|
||||
}
|
||||
static std::unordered_map<std::string, std::function<Result(Context const&)>>
|
||||
|
||||
@@ -63,6 +63,7 @@ using Error = ripple::error_code_i;
|
||||
struct Status
|
||||
{
|
||||
Error error = Error::rpcSUCCESS;
|
||||
std::string strCode = "";
|
||||
std::string message = "";
|
||||
|
||||
Status(){};
|
||||
@@ -73,6 +74,10 @@ struct Status
|
||||
: error(error_), message(message_)
|
||||
{
|
||||
}
|
||||
Status(Error error_, std::string strCode_, std::string message_)
|
||||
: error(error_), strCode(strCode_), message(message_)
|
||||
{
|
||||
}
|
||||
|
||||
/** Returns true if the Status is *not* OK. */
|
||||
operator bool() const
|
||||
@@ -114,18 +119,13 @@ public:
|
||||
return account.c_str();
|
||||
}
|
||||
};
|
||||
void
|
||||
inject_error(Error err, boost::json::object& json);
|
||||
|
||||
void
|
||||
inject_error(Error err, std::string const& message, boost::json::object& json);
|
||||
boost::json::object
|
||||
make_error(Status const& status);
|
||||
|
||||
boost::json::object
|
||||
make_error(Error err);
|
||||
|
||||
boost::json::object
|
||||
make_error(Error err, std::string const& message);
|
||||
|
||||
std::optional<Context>
|
||||
make_WsContext(
|
||||
boost::json::object const& request,
|
||||
|
||||
43
src/rpc/handlers/TransactionEntry.cpp
Normal file
43
src/rpc/handlers/TransactionEntry.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <rpc/RPCHelpers.h>
|
||||
|
||||
namespace RPC {
|
||||
|
||||
Result
|
||||
doTransactionEntry(Context const& context)
|
||||
{
|
||||
boost::json::object response;
|
||||
auto v = ledgerInfoFromRequest(context);
|
||||
if (auto status = std::get_if<Status>(&v))
|
||||
return *status;
|
||||
|
||||
auto lgrInfo = std::get<ripple::LedgerInfo>(v);
|
||||
|
||||
ripple::uint256 hash;
|
||||
if (!hash.parseHex(getRequiredString(context.params, "tx_hash")))
|
||||
return Status{Error::rpcINVALID_PARAMS, "malformedTransaction"};
|
||||
|
||||
auto dbResponse = context.backend->fetchTransaction(hash);
|
||||
// Note: transaction_entry is meant to only search a specified ledger for
|
||||
// the specified transaction. tx searches the entire range of history. For
|
||||
// rippled, having two separate commands made sense, as tx would use SQLite
|
||||
// and transaction_entry used the nodestore. For clio though, there is no
|
||||
// difference between the implementation of these two, as clio only stores
|
||||
// transactions in a transactions table, where the key is the hash. However,
|
||||
// the API for transaction_entry says the method only searches the specified
|
||||
// ledger; we simulate that here by returning not found if the transaction
|
||||
// is in a different ledger than the one specified.
|
||||
if (!dbResponse || dbResponse->ledgerSequence != lgrInfo.seq)
|
||||
return Status{
|
||||
Error::rpcTXN_NOT_FOUND,
|
||||
"transactionNotFound",
|
||||
"Transaction not found."};
|
||||
|
||||
auto [txn, meta] = toExpandedJson(*dbResponse);
|
||||
response["tx_json"] = std::move(txn);
|
||||
response["metadata"] = std::move(meta);
|
||||
response["ledger_index"] = lgrInfo.seq;
|
||||
response["ledger_hash"] = ripple::strHex(lgrInfo.hash);
|
||||
return response;
|
||||
}
|
||||
|
||||
} // namespace RPC
|
||||
@@ -172,7 +172,7 @@ handle_request(
|
||||
|
||||
if (auto status = std::get_if<RPC::Status>(&v))
|
||||
{
|
||||
auto error = RPC::make_error(status->error);
|
||||
auto error = RPC::make_error(*status);
|
||||
|
||||
error["request"] = request;
|
||||
|
||||
|
||||
@@ -267,8 +267,7 @@ public:
|
||||
|
||||
if (auto status = std::get_if<RPC::Status>(&v))
|
||||
{
|
||||
auto error =
|
||||
RPC::make_error(status->error, status->message);
|
||||
auto error = RPC::make_error(*status);
|
||||
|
||||
if (!id.is_null())
|
||||
error["id"] = id;
|
||||
|
||||
Reference in New Issue
Block a user