Skip to content

Commit

Permalink
Add book_changes rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardAH committed Jun 16, 2022
1 parent e32bc67 commit 27846df
Show file tree
Hide file tree
Showing 14 changed files with 430 additions and 115 deletions.
1 change: 1 addition & 0 deletions src/ripple/app/main/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ printHelp(const po::options_description& desc)
"[strict]\n"
" account_tx accountID [ledger_min [ledger_max [limit "
"[offset]]]] [binary] [count] [descending]\n"
" book_changes [<ledger hash|id>]\n"
" book_offers <taker_pays> <taker_gets> [<taker [<ledger> "
"[<limit> [<proof> [<marker>]]]]]\n"
" can_delete [<ledgerid>|<ledgerhash>|now|always|never]\n"
Expand Down
47 changes: 45 additions & 2 deletions src/ripple/app/misc/NetworkOPs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include <ripple/protocol/STParsedJSON.h>
#include <ripple/resource/Fees.h>
#include <ripple/resource/ResourceManager.h>
#include <ripple/rpc/BookChanges.h>
#include <ripple/rpc/DeliveredAmount.h>
#include <ripple/rpc/impl/RPCHelpers.h>
#include <boost/asio/ip/host_name.hpp>
Expand Down Expand Up @@ -502,6 +503,11 @@ class NetworkOPsImp final : public NetworkOPs
bool
unsubLedger(std::uint64_t uListener) override;

bool
subBookChanges(InfoSub::ref ispListener) override;
bool
unsubBookChanges(std::uint64_t uListener) override;

bool
subServer(InfoSub::ref ispListener, Json::Value& jvResult, bool admin)
override;
Expand Down Expand Up @@ -743,9 +749,10 @@ class NetworkOPsImp final : public NetworkOPs
sValidations, // Received validations.
sPeerStatus, // Peer status changes.
sConsensusPhase, // Consensus phase
sBookChanges, // Per-ledger order book changes

sLastEntry = sConsensusPhase // as this name implies, any new entry
// must be ADDED ABOVE this one
sLastEntry = sBookChanges // as this name implies, any new entry
// must be ADDED ABOVE this one
};
std::array<SubMapType, SubTypes::sLastEntry + 1> mStreamMaps;

Expand Down Expand Up @@ -2898,6 +2905,24 @@ NetworkOPsImp::pubLedger(std::shared_ptr<ReadView const> const& lpAccepted)
}
}

if (!mStreamMaps[sBookChanges].empty())
{
Json::Value jvObj = ripple::RPC::computeBookChanges(lpAccepted);

auto it = mStreamMaps[sBookChanges].begin();
while (it != mStreamMaps[sBookChanges].end())
{
InfoSub::pointer p = it->second.lock();
if (p)
{
p->send(jvObj, true);
++it;
}
else
it = mStreamMaps[sBookChanges].erase(it);
}
}

{
static bool firstTime = true;
if (firstTime)
Expand Down Expand Up @@ -3876,6 +3901,16 @@ NetworkOPsImp::subLedger(InfoSub::ref isrListener, Json::Value& jvResult)
.second;
}

// <-- bool: true=added, false=already there
bool
NetworkOPsImp::subBookChanges(InfoSub::ref isrListener)
{
std::lock_guard sl(mSubLock);
return mStreamMaps[sBookChanges]
.emplace(isrListener->getSeq(), isrListener)
.second;
}

// <-- bool: true=erased, false=was not there
bool
NetworkOPsImp::unsubLedger(std::uint64_t uSeq)
Expand All @@ -3884,6 +3919,14 @@ NetworkOPsImp::unsubLedger(std::uint64_t uSeq)
return mStreamMaps[sLedger].erase(uSeq);
}

// <-- bool: true=erased, false=was not there
bool
NetworkOPsImp::unsubBookChanges(std::uint64_t uSeq)
{
std::lock_guard sl(mSubLock);
return mStreamMaps[sBookChanges].erase(uSeq);
}

// <-- bool: true=added, false=already there
bool
NetworkOPsImp::subManifests(InfoSub::ref isrListener)
Expand Down
5 changes: 5 additions & 0 deletions src/ripple/net/InfoSub.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ class InfoSub : public CountedObject<InfoSub>
virtual bool
unsubLedger(std::uint64_t uListener) = 0;

virtual bool
subBookChanges(ref ispListener) = 0;
virtual bool
unsubBookChanges(std::uint64_t uListener) = 0;

virtual bool
subManifests(ref ispListener) = 0;
virtual bool
Expand Down
1 change: 1 addition & 0 deletions src/ripple/net/impl/RPCCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,7 @@ class RPCParser
{"account_objects", &RPCParser::parseAccountItems, 1, 5},
{"account_offers", &RPCParser::parseAccountItems, 1, 4},
{"account_tx", &RPCParser::parseAccountTransactions, 1, 8},
{"book_changes", &RPCParser::parseLedgerId, 1, 1},
{"book_offers", &RPCParser::parseBookOffers, 2, 7},
{"can_delete", &RPCParser::parseCanDelete, 0, 1},
{"channel_authorize", &RPCParser::parseChannelAuthorize, 3, 4},
Expand Down
8 changes: 8 additions & 0 deletions src/ripple/protocol/jss.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,13 @@ JSS(build_path); // in: TransactionSign
JSS(build_version); // out: NetworkOPs
JSS(cancel_after); // out: AccountChannels
JSS(can_delete); // out: CanDelete
JSS(changes); // out: BookChanges
JSS(channel_id); // out: AccountChannels
JSS(channels); // out: AccountChannels
JSS(check); // in: AccountObjects
JSS(check_nodes); // in: LedgerCleaner
JSS(clear); // in/out: FetchInfo
JSS(close); // out: BookChanges
JSS(close_flags); // out: LedgerToJson
JSS(close_time); // in: Application, out: NetworkOPs,
// RCLCxPeerPos, LedgerToJson
Expand All @@ -193,6 +195,8 @@ JSS(converge_time_s); // out: NetworkOPs
JSS(cookie); // out: NetworkOPs
JSS(count); // in: AccountTx*, ValidatorList
JSS(counters); // in/out: retrieve counters
JSS(cur_a); // out: BookChanges
JSS(cur_b); // out: BookChanges
JSS(currentShard); // out: NodeToShardStatus
JSS(currentShardIndex); // out: NodeToShardStatus
JSS(currency); // in: paths/PathRequest, STAmount
Expand Down Expand Up @@ -282,6 +286,7 @@ JSS(hashes); // in: AccountObjects
JSS(have_header); // out: InboundLedger
JSS(have_state); // out: InboundLedger
JSS(have_transactions); // out: InboundLedger
JSS(high); // out: BookChanges
JSS(highest_sequence); // out: AccountInfo
JSS(highest_ticket); // out: AccountInfo
JSS(historical_perminute); // historical_perminute.
Expand Down Expand Up @@ -363,6 +368,7 @@ JSS(load_fee); // out: LoadFeeTrackImp, NetworkOPs
JSS(local); // out: resource/Logic.h
JSS(local_txs); // out: GetCounts
JSS(local_static_keys); // out: ValidatorList
JSS(low); // out: BookChanges
JSS(lowest_sequence); // out: AccountInfo
JSS(lowest_ticket); // out: AccountInfo
JSS(majority); // out: RPC feature
Expand Down Expand Up @@ -639,6 +645,8 @@ JSS(validator_sites); // out: ValidatorSites
JSS(value); // out: STAmount
JSS(version); // out: RPCVersion
JSS(vetoed); // out: AmendmentTableImpl
JSS(vol_a); // out: BookChanges
JSS(vol_b); // out: BookChanges
JSS(vote); // in: Feature
JSS(warning); // rpc:
JSS(warnings); // out: server_info, server_state
Expand Down
213 changes: 213 additions & 0 deletions src/ripple/rpc/BookChanges.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2019 Ripple Labs Inc.
Permission to use, copy, modify, and/or 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.
*/
//==============================================================================

#ifndef RIPPLE_RPC_BOOKCHANGES_H_INCLUDED
#define RIPPLE_RPC_BOOKCAHNGES_H_INCLUDED

namespace Json {
class Value;
}

namespace ripple {

class ReadView;
class Transaction;
class TxMeta;
class STTx;

namespace RPC {

template <class L>
Json::Value
computeBookChanges(std::shared_ptr<L const> const& lpAccepted)
{
std::map<
std::string,
std::tuple<
STAmount, // side A volume
STAmount, // side B volume
STAmount, // high rate
STAmount, // low rate
STAmount, // open rate
STAmount // close rate
>>
tally;

for (auto& tx : lpAccepted->txs)
{
if (!tx.first || !tx.second ||
!tx.first->isFieldPresent(sfTransactionType))
continue;

std::optional<uint32_t> offerCancel;
uint16_t tt = tx.first->getFieldU16(sfTransactionType);
switch (tt)
{
case ttOFFER_CANCEL:
case ttOFFER_CREATE: {
if (tx.first->isFieldPresent(sfOfferSequence))
offerCancel = tx.first->getFieldU32(sfOfferSequence);
break;
}
// in future if any other ways emerge to cancel an offer
// this switch makes them easy to add
default:
break;
}

for (auto const& node : tx.second->getFieldArray(sfAffectedNodes))
{
SField const& metaType = node.getFName();
uint16_t nodeType = node.getFieldU16(sfLedgerEntryType);

// we only care about ltOFFER objects being modified or
// deleted
if (nodeType != ltOFFER || metaType == sfCreatedNode)
continue;

// if either FF or PF are missing we can't compute
// but generally these are cancelled rather than crossed
// so skipping them is consistent
if (!node.isFieldPresent(sfFinalFields) ||
!node.isFieldPresent(sfPreviousFields))
continue;

STObject& finalFields = (const_cast<STObject&>(node))
.getField(sfFinalFields)
.downcast<STObject>();

STObject& previousFields = (const_cast<STObject&>(node))
.getField(sfPreviousFields)
.downcast<STObject>();

// defensive case that should never be hit
if (!finalFields.isFieldPresent(sfTakerGets) ||
!finalFields.isFieldPresent(sfTakerPays) ||
!previousFields.isFieldPresent(sfTakerGets) ||
!previousFields.isFieldPresent(sfTakerPays))
continue;

// filter out any offers deleted by explicit offer cancels
if (metaType == sfDeletedNode && offerCancel &&
finalFields.getFieldU32(sfSequence) == *offerCancel)
continue;

// compute the difference in gets and pays actually
// affected onto the offer
STAmount deltaGets = finalFields.getFieldAmount(sfTakerGets) -
previousFields.getFieldAmount(sfTakerGets);
STAmount deltaPays = finalFields.getFieldAmount(sfTakerPays) -
previousFields.getFieldAmount(sfTakerPays);

std::string g{to_string(deltaGets.issue())};
std::string p{to_string(deltaPays.issue())};

bool const noswap =
isXRP(deltaGets) ? true : (isXRP(deltaPays) ? false : (g < p));

STAmount first = noswap ? deltaGets : deltaPays;
STAmount second = noswap ? deltaPays : deltaGets;

// defensively programmed, should (probably) never happen
if (second == beast::zero)
continue;

STAmount rate = divide(first, second, noIssue());

if (first < beast::zero)
first = -first;

if (second < beast::zero)
second = -second;

std::stringstream ss;
if (noswap)
ss << g << "|" << p;
else
ss << p << "|" << g;

std::string key{ss.str()};

if (tally.find(key) == tally.end())
tally[key] = {
first, // side A vol
second, // side B vol
rate, // high
rate, // low
rate, // open
rate // close
};
else
{
// increment volume
auto& entry = tally[key];

std::get<0>(entry) += first; // side A vol
std::get<1>(entry) += second; // side B vol

if (std::get<2>(entry) < rate) // high
std::get<2>(entry) = rate;

if (std::get<3>(entry) > rate) // low
std::get<3>(entry) = rate;

std::get<5>(entry) = rate; // close
}
}
}

Json::Value jvObj(Json::objectValue);
jvObj[jss::type] = "bookChanges";
jvObj[jss::ledger_index] = lpAccepted->info().seq;
jvObj[jss::ledger_hash] = to_string(lpAccepted->info().hash);
jvObj[jss::ledger_time] = Json::Value::UInt(
lpAccepted->info().closeTime.time_since_epoch().count());

jvObj[jss::changes] = Json::arrayValue;

for (auto const& entry : tally)
{
Json::Value& inner = jvObj[jss::changes].append(Json::objectValue);

STAmount volA = std::get<0>(entry.second);
STAmount volB = std::get<1>(entry.second);

inner[jss::cur_a] =
(isXRP(volA) ? "XRP_drops" : to_string(volA.issue()));
inner[jss::cur_b] =
(isXRP(volB) ? "XRP_drops" : to_string(volB.issue()));

inner[jss::vol_a] =
(isXRP(volA) ? to_string(volA.xrp()) : to_string(volA.iou()));
inner[jss::vol_b] =
(isXRP(volB) ? to_string(volB.xrp()) : to_string(volB.iou()));

inner[jss::high] = to_string(std::get<2>(entry.second).iou());
inner[jss::low] = to_string(std::get<3>(entry.second).iou());
inner[jss::open] = to_string(std::get<4>(entry.second).iou());
inner[jss::close] = to_string(std::get<5>(entry.second).iou());
}

return jvObj;
}

} // namespace RPC
} // namespace ripple

#endif
Loading

0 comments on commit 27846df

Please sign in to comment.