Skip to content

Commit

Permalink
WIP: Publish DeliverMax using appropriate API
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronek committed Oct 5, 2023
1 parent 7bc82dd commit 8a74181
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 26 deletions.
6 changes: 5 additions & 1 deletion src/ripple/app/ledger/BookListeners.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ BookListeners::removeSubscriber(std::uint64_t seq)
void
BookListeners::publish(
Json::Value const& jvObj,
Json::Value const& jvObjApiVer2,
hash_set<std::uint64_t>& havePublished)
{
std::lock_guard sl(mLock);
Expand All @@ -54,7 +55,10 @@ BookListeners::publish(
// Only publish jvObj if this is the first occurence
if (havePublished.emplace(p->getSeq()).second)
{
p->send(jvObj, true);
if (p->getApiVersion() == 1)
p->send(jvObj, true);
else
p->send(jvObjApiVer2, true);
}
++it;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/app/ledger/BookListeners.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class BookListeners
*/
void
publish(Json::Value const& jvObj, hash_set<std::uint64_t>& havePublished);
publish(Json::Value const& jvObj, Json::Value const& jvObjApiVer2, hash_set<std::uint64_t>& havePublished);

private:
std::recursive_mutex mLock;
Expand Down
5 changes: 3 additions & 2 deletions src/ripple/app/ledger/OrderBookDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ void
OrderBookDB::processTxn(
std::shared_ptr<ReadView const> const& ledger,
const AcceptedLedgerTx& alTx,
Json::Value const& jvObj)
Json::Value const& jvObj,
Json::Value const& jvObjApiVer2)
{
std::lock_guard sl(mLock);

Expand All @@ -277,7 +278,7 @@ OrderBookDB::processTxn(
{data->getFieldAmount(sfTakerGets).issue(),
data->getFieldAmount(sfTakerPays).issue()});
if (listeners)
listeners->publish(jvObj, havePublished);
listeners->publish(jvObj, jvObjApiVer2, havePublished);
}
};

Expand Down
3 changes: 2 additions & 1 deletion src/ripple/app/ledger/OrderBookDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class OrderBookDB
processTxn(
std::shared_ptr<ReadView const> const& ledger,
const AcceptedLedgerTx& alTx,
Json::Value const& jvObj);
Json::Value const& jvObj,
Json::Value const& jvObjApiVer2);

private:
Application& app_;
Expand Down
3 changes: 2 additions & 1 deletion src/ripple/app/ledger/impl/LedgerToJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ fillJsonTx(
txn,
{txn->getTransactionID(), fill.ledger.seq(), *stMeta});

insertDeliverMax(txJson[jss::metaData], *fill.context, txnType);
RPC::insertDeliverMax(
txJson[jss::metaData], txnType, fill.context->apiVersion);
}
}

Expand Down
95 changes: 86 additions & 9 deletions src/ripple/app/misc/NetworkOPs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include <ripple/resource/Fees.h>
#include <ripple/resource/ResourceManager.h>
#include <ripple/rpc/BookChanges.h>
#include <ripple/rpc/DeliverMax.h>
#include <ripple/rpc/DeliveredAmount.h>
#include <ripple/rpc/ServerHandler.h>
#include <ripple/rpc/impl/RPCHelpers.h>
Expand Down Expand Up @@ -3149,11 +3150,18 @@ NetworkOPsImp::pubValidatedTransaction(

Json::Value jvObj =
transJson(*stTxn, transaction.getResult(), true, ledger);
Json::Value jvObjApi2 =
transJson(*stTxn, transaction.getResult(), true, ledger);

{
auto const& meta = transaction.getMeta();
jvObj[jss::meta] = meta.getJson(JsonOptions::none);
RPC::insertDeliveredAmount(jvObj[jss::meta], *ledger, stTxn, meta);
RPC::insertDeliverMax(jvObj[jss::meta], stTxn->getTxnType(), 1);

jvObjApi2[jss::meta] = meta.getJson(JsonOptions::none);
RPC::insertDeliveredAmount(jvObjApi2[jss::meta], *ledger, stTxn, meta);
RPC::insertDeliverMax(jvObjApi2[jss::meta], stTxn->getTxnType(), 2);
}

{
Expand All @@ -3166,7 +3174,10 @@ NetworkOPsImp::pubValidatedTransaction(

if (p)
{
p->send(jvObj, true);
if (p->getApiVersion() == 1)
p->send(jvObj, true);
else
p->send(jvObjApi2, true);
++it;
}
else
Expand All @@ -3181,7 +3192,10 @@ NetworkOPsImp::pubValidatedTransaction(

if (p)
{
p->send(jvObj, true);
if (p->getApiVersion() == 1)
p->send(jvObj, true);
else
p->send(jvObjApi2, true);
++it;
}
else
Expand All @@ -3190,7 +3204,7 @@ NetworkOPsImp::pubValidatedTransaction(
}

if (transaction.getResult() == tesSUCCESS)
app_.getOrderBookDB().processTxn(ledger, transaction, jvObj);
app_.getOrderBookDB().processTxn(ledger, transaction, jvObj, jvObjApi2);

pubAccountTransaction(ledger, transaction, last);
}
Expand Down Expand Up @@ -3296,28 +3310,53 @@ NetworkOPsImp::pubAccountTransaction(

Json::Value jvObj =
transJson(*stTxn, transaction.getResult(), true, ledger);
Json::Value jvObjApi2 =
transJson(*stTxn, transaction.getResult(), true, ledger);

{
auto const& meta = transaction.getMeta();

jvObj[jss::meta] = meta.getJson(JsonOptions::none);
RPC::insertDeliveredAmount(jvObj[jss::meta], *ledger, stTxn, meta);
RPC::insertDeliverMax(jvObj[jss::meta], stTxn->getTxnType(), 1);

jvObjApi2[jss::meta] = meta.getJson(JsonOptions::none);
RPC::insertDeliveredAmount(
jvObjApi2[jss::meta], *ledger, stTxn, meta);
RPC::insertDeliverMax(jvObjApi2[jss::meta], stTxn->getTxnType(), 2);
}

for (InfoSub::ref isrListener : notify)
isrListener->send(jvObj, true);
{
if (isrListener->getApiVersion() == 1)
isrListener->send(jvObj, true);
else
isrListener->send(jvObjApi2, true);
}

if (last)
{
jvObj[jss::account_history_boundary] = true;
jvObjApi2[jss::account_history_boundary] = true;
}

assert(!jvObj.isMember(jss::account_history_tx_stream));
for (auto& info : accountHistoryNotify)
{
auto& index = info.index_;
if (index->forwardTxIndex_ == 0 && !index->haveHistorical_)
{
jvObj[jss::account_history_tx_first] = true;
jvObj[jss::account_history_tx_index] = index->forwardTxIndex_++;
info.sink_->send(jvObj, true);
jvObjApi2[jss::account_history_tx_first] = true;
}
jvObj[jss::account_history_tx_index] = index->forwardTxIndex_;
jvObjApi2[jss::account_history_tx_index] = index->forwardTxIndex_;
index->forwardTxIndex_ += 1;

if (info.sink_->getApiVersion() == 1)
info.sink_->send(jvObj, true);
else
info.sink_->send(jvObjApi2, true);
}
}
}
Expand Down Expand Up @@ -3585,6 +3624,24 @@ NetworkOPsImp::addAccountHistoryJob(SubAccountHistoryInfoWeak subInfo)
return false;
};

auto send2 = [&](Json::Value const& jvObj,
Json::Value const& jvObjApi2,
bool unsubscribe) -> bool {
if (auto sptr = subInfo.sinkWptr_.lock(); sptr)
{
if (sptr->getApiVersion() == 1)
sptr->send(jvObj, true);
else
sptr->send(jvObjApi2, true);

if (unsubscribe)
unsubAccountHistory(sptr, accountId, false);
return true;
}

return false;
};

auto getMoreTxns =
[&](std::uint32_t minLedger,
std::uint32_t maxLedger,
Expand Down Expand Up @@ -3743,21 +3800,41 @@ NetworkOPsImp::addAccountHistoryJob(SubAccountHistoryInfoWeak subInfo)
send(rpcError(rpcINTERNAL), true);
return;
}

Json::Value jvTx = transJson(
*stTxn, meta->getResultTER(), true, curTxLedger);
jvTx[jss::meta] = meta->getJson(JsonOptions::none);
jvTx[jss::account_history_tx_index] = txHistoryIndex--;
jvTx[jss::account_history_tx_index] = txHistoryIndex;

Json::Value jvTxApi2 = transJson(
*stTxn, meta->getResultTER(), true, curTxLedger);
jvTxApi2[jss::meta] = meta->getJson(JsonOptions::none);
jvTxApi2[jss::account_history_tx_index] =
txHistoryIndex;
txHistoryIndex -= 1;

if (i + 1 == num_txns ||
txns[i + 1].first->getLedger() != tx->getLedger())
{
jvTx[jss::account_history_boundary] = true;
jvTxApi2[jss::account_history_boundary] = true;
}

RPC::insertDeliveredAmount(
jvTx[jss::meta], *curTxLedger, stTxn, *meta);
RPC::insertDeliverMax(
jvTx[jss::meta], stTxn->getTxnType(), 1);

RPC::insertDeliveredAmount(
jvTxApi2[jss::meta], *curTxLedger, stTxn, *meta);
RPC::insertDeliverMax(
jvTxApi2[jss::meta], stTxn->getTxnType(), 2);

if (isFirstTx(tx, meta))
{
jvTx[jss::account_history_tx_first] = true;
send(jvTx, false);
jvTxApi2[jss::account_history_tx_first] = true;
send2(jvTx, jvTxApi2, false);

JLOG(m_journal.trace())
<< "AccountHistory job for account "
Expand All @@ -3767,7 +3844,7 @@ NetworkOPsImp::addAccountHistoryJob(SubAccountHistoryInfoWeak subInfo)
}
else
{
send(jvTx, false);
send2(jvTx, jvTxApi2, false);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/ripple/net/InfoSub.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ class InfoSub : public CountedObject<InfoSub>
std::shared_ptr<InfoSubRequest> const&
getRequest();

void
setApiVersion(int apiVersion);

int
getApiVersion() const noexcept;

protected:
std::mutex mLock;

Expand All @@ -240,6 +246,7 @@ class InfoSub : public CountedObject<InfoSub>
std::shared_ptr<InfoSubRequest> request_;
std::uint64_t mSeq;
hash_set<AccountID> accountHistorySubscriptions_;
int apiVersion = 1;

static int
assign_id()
Expand Down
12 changes: 12 additions & 0 deletions src/ripple/net/impl/InfoSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,16 @@ InfoSub::getRequest()
return request_;
}

void
InfoSub::setApiVersion(int apiVersion)
{
this->apiVersion = apiVersion;
}

int
InfoSub::getApiVersion() const noexcept
{
return this->apiVersion;
}

} // namespace ripple
4 changes: 2 additions & 2 deletions src/ripple/rpc/DeliverMax.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ struct Context;
Copy `Amount` field to `DeliverMax` field to the `meta` input/output
parameter. This only applies to certain transaction types.
When context.apiVersion > 1 will also remove `Amount`.
When apiVersion > 1 will also remove `Amount`.
This function is meant to be used on Json API output.
@{
*/

void
insertDeliverMax(Json::Value& meta, RPC::Context const& context, TxType txnType);
insertDeliverMax(Json::Value& meta, TxType txnType, int apiVersion);

/** @} */

Expand Down
6 changes: 4 additions & 2 deletions src/ripple/rpc/handlers/AccountTx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,10 @@ populateJsonResponse(
jvObj[jss::meta], context, txn, *txnMeta);
auto const& sttx = txn->getSTransaction();
insertNFTSyntheticInJson(jvObj, sttx, *txnMeta);
insertDeliverMax(
jvObj[jss::meta], context, sttx->getTxnType());
RPC::insertDeliverMax(
jvObj[jss::meta],
sttx->getTxnType(),
context.apiVersion);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/ripple/rpc/handlers/PathFind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ doPathFind(RPC::JsonContext& context)
if (!context.infoSub)
return rpcError(rpcNO_EVENTS);

context.infoSub->setApiVersion(context.apiVersion);

auto sSubCommand = context.params[jss::subcommand].asString();

if (sSubCommand == "create")
Expand Down
1 change: 1 addition & 0 deletions src/ripple/rpc/handlers/Subscribe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ doSubscribe(RPC::JsonContext& context)
{
ispSub = context.infoSub;
}
ispSub->setApiVersion(context.apiVersion);

if (context.params.isMember(jss::streams))
{
Expand Down
6 changes: 4 additions & 2 deletions src/ripple/rpc/handlers/TransactionEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ doTransactionEntry(RPC::JsonContext& context)
if (stobj)
{
jvResult[jss::metadata] = stobj->getJson(JsonOptions::none);
insertDeliverMax(
jvResult[jss::metadata], context, sttx->getTxnType());
RPC::insertDeliverMax(
jvResult[jss::metadata],
sttx->getTxnType(),
context.apiVersion);
}
// 'accounts'
// 'engine_...'
Expand Down
6 changes: 4 additions & 2 deletions src/ripple/rpc/handlers/Tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,10 @@ populateJsonResponse(
auto const& sttx = result.txn->getSTransaction();
insertNFTSyntheticInJson(response, sttx, *meta);
// TODO fix misnomer, we might be replacing Amount
insertDeliverMax(
response[jss::meta], context, sttx->getTxnType());
RPC::insertDeliverMax(
response[jss::meta],
sttx->getTxnType(),
context.apiVersion);
}
}
response[jss::validated] = result.validated;
Expand Down
3 changes: 2 additions & 1 deletion src/ripple/rpc/handlers/TxHistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ doTxHistory(RPC::JsonContext& context)
{
Json::Value tx_json = t->getJson(JsonOptions::none);
// TODO fix misnomer, we might be replacing Amount
insertDeliverMax(tx_json, context, t->getSTransaction()->getTxnType());
RPC::insertDeliverMax(
tx_json, t->getSTransaction()->getTxnType(), context.apiVersion);
txs.append(tx_json);
}

Expand Down
Loading

0 comments on commit 8a74181

Please sign in to comment.