-
Notifications
You must be signed in to change notification settings - Fork 79
Feat: add mptoken_issuance_history RPC #3141
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
Open
BryanJ1ang
wants to merge
5
commits into
XRPLF:develop
Choose a base branch
from
BryanJ1ang:brjiang-mpt-rpc-handler
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
05f2826
feat: implemented mptoken_issuance_history rpc
BryanJ1ang b147be7
Cleaned up comments
BryanJ1ang 5e3cfea
refactor: read MPT backfill status from the backend directly
BryanJ1ang 03d443e
Added log
BryanJ1ang f1ce356
Merge branch 'develop' into brjiang-mpt-rpc-handler
BryanJ1ang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,310 @@ | ||
| #include "rpc/handlers/MPTokenIssuanceHistory.hpp" | ||
|
|
||
| #include "data/Types.hpp" | ||
| #include "migration/MigratiorStatus.hpp" | ||
| #include "rpc/Errors.hpp" | ||
| #include "rpc/JS.hpp" | ||
| #include "rpc/RPCHelpers.hpp" | ||
| #include "rpc/common/Types.hpp" | ||
| #include "util/Assert.hpp" | ||
| #include "util/JsonUtils.hpp" | ||
| #include "util/Profiler.hpp" | ||
| #include "util/log/Logger.hpp" | ||
|
|
||
| #include <boost/json/conversion.hpp> | ||
| #include <boost/json/object.hpp> | ||
| #include <boost/json/value.hpp> | ||
| #include <boost/json/value_from.hpp> | ||
| #include <boost/json/value_to.hpp> | ||
| #include <xrpl/basics/base_uint.h> | ||
| #include <xrpl/basics/chrono.h> | ||
| #include <xrpl/basics/strHex.h> | ||
| #include <xrpl/protocol/LedgerHeader.h> | ||
| #include <xrpl/protocol/jss.h> | ||
|
|
||
| #include <atomic> | ||
| #include <cstdint> | ||
| #include <limits> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <utility> | ||
|
|
||
| namespace rpc { | ||
|
|
||
| MPTokenIssuanceHistoryHandler::Result | ||
| MPTokenIssuanceHistoryHandler::process( | ||
| MPTokenIssuanceHistoryHandler::Input const& input, | ||
| Context const& ctx | ||
| ) const | ||
| { | ||
| // Fail closed unless the backfill is done: partial history must never be served. | ||
| if (not migrated_->load(std::memory_order_relaxed)) { | ||
| auto const statusString = sharedPtrBackend_->fetchMigratorStatus(kMigratorName, ctx.yield); | ||
| if (statusString.has_value() and | ||
| migration::MigratorStatus::fromString(*statusString) == | ||
| migration::MigratorStatus::Status::Migrated) { | ||
| migrated_->store(true, std::memory_order_relaxed); | ||
| } else { | ||
| return Error{Status{ | ||
| RippledError::RpcNotReady, | ||
| "mptoken_issuance_history is unavailable until the MPT transaction-history " | ||
| "backfill " | ||
| "completes on this node. Run: ./clio_server --migrate " | ||
| "MPTTransactionHistoryMigrator " | ||
| "CONFIG" | ||
| }}; | ||
| } | ||
| } | ||
|
|
||
| auto const range = sharedPtrBackend_->fetchLedgerRange(); | ||
| ASSERT(range.has_value(), "MPTokenIssuanceHistory's ledger range must be available"); | ||
|
|
||
| auto [minIndex, maxIndex] = *range; // NOLINT(bugprone-unchecked-optional-access) | ||
|
|
||
| if (input.ledgerIndexMin) { | ||
| // NOLINTBEGIN(bugprone-unchecked-optional-access) | ||
| if (range->maxSequence < input.ledgerIndexMin || range->minSequence > input.ledgerIndexMin) | ||
| return Error{Status{RippledError::RpcLgrIdxMalformed, "ledgerSeqMinOutOfRange"}}; | ||
| // NOLINTEND(bugprone-unchecked-optional-access) | ||
|
|
||
| minIndex = *input.ledgerIndexMin; | ||
| } | ||
|
|
||
| if (input.ledgerIndexMax) { | ||
| // NOLINTBEGIN(bugprone-unchecked-optional-access) | ||
| if (range->maxSequence < input.ledgerIndexMax || range->minSequence > input.ledgerIndexMax) | ||
| return Error{Status{RippledError::RpcLgrIdxMalformed, "ledgerSeqMaxOutOfRange"}}; | ||
| // NOLINTEND(bugprone-unchecked-optional-access) | ||
|
|
||
| maxIndex = *input.ledgerIndexMax; | ||
| } | ||
|
|
||
| if (minIndex > maxIndex) | ||
| return Error{Status{RippledError::RpcLgrIdxsInvalid}}; | ||
|
|
||
| if (input.ledgerHash || input.ledgerIndex) { | ||
| // rippled does not have this check | ||
| if (input.ledgerIndexMax || input.ledgerIndexMin) { | ||
| return Error{Status{RippledError::RpcInvalidParams, "containsLedgerSpecifierAndRange"}}; | ||
| } | ||
|
|
||
| auto const expectedLgrInfo = getLedgerHeaderFromHashOrSeq( | ||
| *sharedPtrBackend_, | ||
| ctx.yield, | ||
| input.ledgerHash, | ||
| input.ledgerIndex, | ||
| range->maxSequence // NOLINT(bugprone-unchecked-optional-access) | ||
| ); | ||
|
|
||
| if (not expectedLgrInfo.has_value()) | ||
| return Error{expectedLgrInfo.error()}; | ||
|
|
||
| maxIndex = minIndex = expectedLgrInfo->seq; | ||
| } | ||
|
|
||
| std::optional<data::TransactionsCursor> cursor; | ||
|
|
||
| // if marker exists | ||
| if (input.marker) { | ||
| cursor = {input.marker->ledger, input.marker->seq}; | ||
| } else { | ||
| if (input.forward) { | ||
| cursor = {minIndex, 0}; | ||
| } else { | ||
| cursor = {maxIndex, std::numeric_limits<int32_t>::max()}; | ||
| } | ||
| } | ||
|
|
||
| auto const limit = input.limit.value_or(kLimitDefault); | ||
| auto const mptIssuanceID = xrpl::uint192{input.mptIssuanceID.c_str()}; | ||
|
|
||
| // tx_type is applied post-fetch below, as account_tx does. | ||
| auto const [txnsAndCursor, timeDiff] = util::timed([&]() -> data::TransactionsAndCursor { | ||
| if (input.account) { | ||
| auto const account = accountFromStringStrict(*input.account); | ||
| ASSERT(account.has_value(), "Account must be decodable after spec validation"); | ||
| return sharedPtrBackend_->fetchAccountMPTokenIssuanceTransactions( | ||
| mptIssuanceID, *account, limit, input.forward, cursor, ctx.yield | ||
| ); | ||
| } | ||
| return sharedPtrBackend_->fetchMPTokenIssuanceTransactions( | ||
| mptIssuanceID, limit, input.forward, cursor, ctx.yield | ||
| ); | ||
| }); | ||
| LOG(log_.info()) << "db fetch took " << timeDiff | ||
| << " milliseconds - num blobs = " << txnsAndCursor.txns.size(); | ||
|
|
||
| Output response; | ||
| auto const [blobs, retCursor] = txnsAndCursor; | ||
|
|
||
| if (retCursor) | ||
| response.marker = {.ledger = retCursor->ledgerSequence, .seq = retCursor->transactionIndex}; | ||
|
|
||
| for (auto const& txnPlusMeta : blobs) { | ||
| // A hash with no matching Transactions row yields a default-constructed record in-position. | ||
| // Skip it before the range check so it neither shortens the page nor disturbs the marker. | ||
| if (txnPlusMeta.transaction.empty() || txnPlusMeta.metadata.empty()) { | ||
| LOG(log_.warn()) << "Skipping index entry with no matching transaction record; " | ||
| "mpt_issuance_id = " | ||
| << input.mptIssuanceID; | ||
| continue; | ||
| } | ||
|
|
||
| // over the range | ||
| if ((txnPlusMeta.ledgerSequence < minIndex && !input.forward) || | ||
| (txnPlusMeta.ledgerSequence > maxIndex && input.forward)) { | ||
| response.marker = std::nullopt; | ||
| break; | ||
| } | ||
| if (txnPlusMeta.ledgerSequence > maxIndex && !input.forward) { | ||
| LOG(log_.debug()) << "Skipping over transactions from incomplete ledger"; | ||
| continue; | ||
| } | ||
|
|
||
| boost::json::object obj; | ||
|
|
||
| // tx_type needs the expanded form to read TransactionType, even when binary is set | ||
| if (!input.binary || input.transactionTypeInLowercase.has_value()) { | ||
| auto [txn, meta] = toExpandedJson(txnPlusMeta, ctx.apiVersion); | ||
|
|
||
| if (txn.contains(JS(TransactionType)) && input.transactionTypeInLowercase.has_value() && | ||
| util::toLower(boost::json::value_to<std::string>(txn[JS(TransactionType)])) != | ||
| *input.transactionTypeInLowercase) | ||
| continue; | ||
|
|
||
| if (!input.binary) { | ||
| auto const txKey = ctx.apiVersion > 1u ? JS(tx_json) : JS(tx); | ||
| obj[JS(meta)] = std::move(meta); | ||
| obj[txKey] = std::move(txn); | ||
| obj[txKey].as_object()[JS(ledger_index)] = txnPlusMeta.ledgerSequence; | ||
| obj[txKey].as_object()[JS(date)] = txnPlusMeta.date; | ||
| if (ctx.apiVersion > 1u) { | ||
| obj[JS(ledger_index)] = txnPlusMeta.ledgerSequence; | ||
| if (obj[txKey].as_object().contains(JS(hash))) { | ||
| obj[JS(hash)] = obj[txKey].at(JS(hash)); | ||
| obj[txKey].as_object().erase(JS(hash)); | ||
| } | ||
| if (auto const lgrInfo = sharedPtrBackend_->fetchLedgerBySequence( | ||
| txnPlusMeta.ledgerSequence, ctx.yield | ||
| ); | ||
| lgrInfo) { | ||
| obj[JS(close_time_iso)] = xrpl::toStringIso(lgrInfo->closeTime); | ||
| obj[JS(ledger_hash)] = xrpl::strHex(lgrInfo->hash); | ||
| } | ||
| } | ||
| obj[JS(validated)] = true; | ||
| response.transactions.push_back(std::move(obj)); | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| // binary is true | ||
| obj = toJsonWithBinaryTx(txnPlusMeta, ctx.apiVersion); | ||
| obj[JS(ledger_index)] = txnPlusMeta.ledgerSequence; | ||
| obj[JS(date)] = txnPlusMeta.date; | ||
| obj[JS(validated)] = true; | ||
| response.transactions.push_back(std::move(obj)); | ||
| } | ||
|
|
||
| response.limit = input.limit; | ||
| response.mptIssuanceID = xrpl::to_string(mptIssuanceID); | ||
| response.ledgerIndexMin = minIndex; | ||
| response.ledgerIndexMax = maxIndex; | ||
|
|
||
| return response; | ||
| } | ||
|
|
||
| void | ||
| tag_invoke( | ||
| boost::json::value_from_tag, | ||
| boost::json::value& jv, | ||
| MPTokenIssuanceHistoryHandler::Output const& output | ||
| ) | ||
| { | ||
| jv = { | ||
| {JS(mpt_issuance_id), output.mptIssuanceID}, | ||
| {JS(ledger_index_min), output.ledgerIndexMin}, | ||
| {JS(ledger_index_max), output.ledgerIndexMax}, | ||
| {JS(transactions), output.transactions}, | ||
| {JS(validated), output.validated}, | ||
| }; | ||
|
|
||
| if (output.marker) | ||
| jv.as_object()[JS(marker)] = boost::json::value_from(*(output.marker)); | ||
|
|
||
| if (output.limit) | ||
| jv.as_object()[JS(limit)] = *(output.limit); | ||
| } | ||
|
|
||
| void | ||
| tag_invoke( | ||
| boost::json::value_from_tag, | ||
| boost::json::value& jv, | ||
| MPTokenIssuanceHistoryHandler::Marker const& marker | ||
| ) | ||
| { | ||
| jv = { | ||
| {JS(ledger), marker.ledger}, | ||
| {JS(seq), marker.seq}, | ||
| }; | ||
| } | ||
|
|
||
| MPTokenIssuanceHistoryHandler::Input | ||
| tag_invoke( | ||
| boost::json::value_to_tag<MPTokenIssuanceHistoryHandler::Input>, | ||
| boost::json::value const& jv | ||
| ) | ||
| { | ||
| auto const& jsonObject = jv.as_object(); | ||
| auto input = MPTokenIssuanceHistoryHandler::Input{}; | ||
|
|
||
| input.mptIssuanceID = boost::json::value_to<std::string>(jsonObject.at(JS(mpt_issuance_id))); | ||
|
|
||
| if (jsonObject.contains(JS(account))) | ||
| input.account = boost::json::value_to<std::string>(jsonObject.at(JS(account))); | ||
|
|
||
| if (jsonObject.contains("tx_type")) { | ||
| input.transactionTypeInLowercase = | ||
| boost::json::value_to<std::string>(jsonObject.at("tx_type")); | ||
| } | ||
|
|
||
| if (jsonObject.contains(JS(ledger_index_min)) && | ||
| util::integralValueAs<int32_t>(jsonObject.at(JS(ledger_index_min))) != -1) | ||
| input.ledgerIndexMin = util::integralValueAs<uint32_t>(jsonObject.at(JS(ledger_index_min))); | ||
|
|
||
| if (jsonObject.contains(JS(ledger_index_max)) && | ||
| util::integralValueAs<int32_t>(jsonObject.at(JS(ledger_index_max))) != -1) | ||
| input.ledgerIndexMax = util::integralValueAs<uint32_t>(jsonObject.at(JS(ledger_index_max))); | ||
|
|
||
| if (jsonObject.contains(JS(ledger_hash))) | ||
| input.ledgerHash = boost::json::value_to<std::string>(jsonObject.at(JS(ledger_hash))); | ||
|
|
||
| if (jsonObject.contains(JS(ledger_index))) { | ||
| auto const expectedLedgerIndex = util::getLedgerIndex(jsonObject.at(JS(ledger_index))); | ||
| if (expectedLedgerIndex.has_value()) | ||
| input.ledgerIndex = *expectedLedgerIndex; | ||
| } | ||
|
|
||
| if (jsonObject.contains(JS(binary))) | ||
| input.binary = jsonObject.at(JS(binary)).as_bool(); | ||
|
|
||
| if (jsonObject.contains(JS(forward))) | ||
| input.forward = jsonObject.at(JS(forward)).as_bool(); | ||
|
|
||
| if (jsonObject.contains(JS(limit))) | ||
| input.limit = util::integralValueAs<uint32_t>(jsonObject.at(JS(limit))); | ||
|
|
||
| if (jsonObject.contains(JS(marker))) { | ||
| input.marker = MPTokenIssuanceHistoryHandler::Marker{ | ||
| .ledger = util::integralValueAs<uint32_t>( | ||
| jsonObject.at(JS(marker)).as_object().at(JS(ledger)) | ||
| ), | ||
| .seq = | ||
| util::integralValueAs<uint32_t>(jsonObject.at(JS(marker)).as_object().at(JS(seq))) | ||
| }; | ||
| } | ||
|
|
||
| return input; | ||
| } | ||
|
|
||
| } // namespace rpc | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.