mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-27 14:35:52 +00:00
Fix RPC book_offers handling of negative balances and self-issued IOUs.
This commit is contained in:
@@ -1784,102 +1784,121 @@ void NetworkOPs::getBookPage(Ledger::pointer lpLedger, const uint160& uTakerPays
|
|||||||
STAmount saDirRate;
|
STAmount saDirRate;
|
||||||
|
|
||||||
// unsigned int iLeft = iLimit;
|
// unsigned int iLeft = iLimit;
|
||||||
|
SLE::pointer sleGetsIssuer = lesActive.entryCache(ltACCOUNT_ROOT, Ledger::getAccountRootIndex(uTakerGetsIssuerID));
|
||||||
|
|
||||||
while (!bDone) {
|
if (!sleGetsIssuer)
|
||||||
if (bDirectAdvance) {
|
{
|
||||||
bDirectAdvance = false;
|
// Order book can not exist if TakerGetsIssuer account does not exist.
|
||||||
|
|
||||||
cLog(lsTRACE) << boost::str(boost::format("getBookPage: bDirectAdvance"));
|
nothing();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while (!bDone) {
|
||||||
|
if (bDirectAdvance) {
|
||||||
|
bDirectAdvance = false;
|
||||||
|
|
||||||
sleOfferDir = lesActive.entryCache(ltDIR_NODE, lpLedger->getNextLedgerIndex(uTipIndex, uBookEnd));
|
cLog(lsTRACE) << boost::str(boost::format("getBookPage: bDirectAdvance"));
|
||||||
if (!sleOfferDir)
|
|
||||||
{
|
|
||||||
cLog(lsTRACE) << boost::str(boost::format("getBookPage: bDone"));
|
|
||||||
bDone = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
uTipIndex = sleOfferDir->getIndex();
|
|
||||||
saDirRate = STAmount::setRate(Ledger::getQuality(uTipIndex));
|
|
||||||
SLE::pointer sleBookNode;
|
|
||||||
|
|
||||||
lesActive.dirFirst(uTipIndex, sleBookNode, uBookEntry, uOfferIndex);
|
sleOfferDir = lesActive.entryCache(ltDIR_NODE, lpLedger->getNextLedgerIndex(uTipIndex, uBookEnd));
|
||||||
|
if (!sleOfferDir)
|
||||||
|
{
|
||||||
|
cLog(lsTRACE) << boost::str(boost::format("getBookPage: bDone"));
|
||||||
|
bDone = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
uTipIndex = sleOfferDir->getIndex();
|
||||||
|
saDirRate = STAmount::setRate(Ledger::getQuality(uTipIndex));
|
||||||
|
SLE::pointer sleBookNode;
|
||||||
|
|
||||||
cLog(lsTRACE) << boost::str(boost::format("getBookPage: uTipIndex=%s") % uTipIndex);
|
lesActive.dirFirst(uTipIndex, sleBookNode, uBookEntry, uOfferIndex);
|
||||||
cLog(lsTRACE) << boost::str(boost::format("getBookPage: uOfferIndex=%s") % uOfferIndex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!bDone)
|
cLog(lsTRACE) << boost::str(boost::format("getBookPage: uTipIndex=%s") % uTipIndex);
|
||||||
{
|
cLog(lsTRACE) << boost::str(boost::format("getBookPage: uOfferIndex=%s") % uOfferIndex);
|
||||||
SLE::pointer sleOffer = lesActive.entryCache(ltOFFER, uOfferIndex);
|
}
|
||||||
const uint160 uOfferOwnerID = sleOffer->getFieldAccount(sfAccount).getAccountID();
|
|
||||||
STAmount saOwnerFunds;
|
|
||||||
|
|
||||||
boost::unordered_map<uint160, STAmount>::const_iterator umBalanceEntry = umBalance.find(uOfferOwnerID);
|
|
||||||
|
|
||||||
if (umBalanceEntry == umBalance.end())
|
|
||||||
{
|
|
||||||
// Did not find balance in table.
|
|
||||||
STAmount saDefault(uTakerGetsCurrencyID, uTakerGetsIssuerID);
|
|
||||||
// cLog(lsINFO) << boost::str(boost::format("getBookPage: saDefault=%s") % saDefault.getFullText());
|
|
||||||
|
|
||||||
saOwnerFunds = lesActive.accountFunds(uOfferOwnerID, saDefault);
|
|
||||||
// cLog(lsINFO) << boost::str(boost::format("getBookPage: saOwnerFunds=%s (new)") % saOwnerFunds.getFullText());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
saOwnerFunds = umBalanceEntry->second;
|
|
||||||
// cLog(lsINFO) << boost::str(boost::format("getBookPage: saOwnerFunds=%s (cached)") % saOwnerFunds.getFullText());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
STAmount saTakerGets = sleOffer->getFieldAmount(sfTakerGets);
|
if (!bDone)
|
||||||
STAmount saTakerPays = sleOffer->getFieldAmount(sfTakerPays);
|
|
||||||
|
|
||||||
Json::Value jvOffer = sleOffer->getJson(0);
|
|
||||||
|
|
||||||
STAmount saTakerGetsFunded;
|
|
||||||
|
|
||||||
if (saOwnerFunds >= saTakerGets)
|
|
||||||
{
|
{
|
||||||
// Sufficient funds no shenanigans.
|
SLE::pointer sleOffer = lesActive.entryCache(ltOFFER, uOfferIndex);
|
||||||
saTakerGetsFunded = saTakerGets;
|
const uint160 uOfferOwnerID = sleOffer->getFieldAccount(sfAccount).getAccountID();
|
||||||
}
|
STAmount saTakerGets = sleOffer->getFieldAmount(sfTakerGets);
|
||||||
else
|
STAmount saTakerPays = sleOffer->getFieldAmount(sfTakerPays);
|
||||||
{
|
STAmount saOwnerFunds;
|
||||||
// cLog(lsINFO) << boost::str(boost::format("getBookPage: saTakerGets=%s") % saTakerGets.getFullText());
|
|
||||||
// cLog(lsINFO) << boost::str(boost::format("getBookPage: saTakerPays=%s") % saTakerPays.getFullText());
|
|
||||||
// cLog(lsINFO) << boost::str(boost::format("getBookPage: saOwnerFunds=%s") % saOwnerFunds.getFullText());
|
|
||||||
// cLog(lsINFO) << boost::str(boost::format("getBookPage: saDirRate=%s") % saDirRate.getText());
|
|
||||||
// cLog(lsINFO) << boost::str(boost::format("getBookPage: multiply=%s") % STAmount::multiply(saTakerGetsFunded, saDirRate).getFullText());
|
|
||||||
// cLog(lsINFO) << boost::str(boost::format("getBookPage: multiply=%s") % STAmount::multiply(saTakerGetsFunded, saDirRate, saTakerPays).getFullText());
|
|
||||||
STAmount saTakerPaysFunded;
|
|
||||||
|
|
||||||
saTakerGetsFunded = saOwnerFunds;
|
if (uTakerGetsIssuerID == uOfferOwnerID)
|
||||||
saTakerPaysFunded = std::min(saTakerPays, STAmount::multiply(saTakerGetsFunded, saDirRate, saTakerPays));
|
{
|
||||||
|
// If offer is selling issuer's own IOUs, it is fully funded.
|
||||||
|
saOwnerFunds = saTakerGets;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
boost::unordered_map<uint160, STAmount>::const_iterator umBalanceEntry = umBalance.find(uOfferOwnerID);
|
||||||
|
|
||||||
// Only provide, if not fully funded.
|
if (umBalanceEntry != umBalance.end())
|
||||||
jvOffer["taker_gets_funded"] = saTakerGetsFunded.getJson(0);
|
{
|
||||||
jvOffer["taker_pays_funded"] = saTakerPaysFunded.getJson(0);
|
// Found in running balance table.
|
||||||
}
|
|
||||||
|
|
||||||
STAmount saOwnerBalance = saOwnerFunds-saTakerGetsFunded;
|
saOwnerFunds = umBalanceEntry->second;
|
||||||
|
// cLog(lsINFO) << boost::str(boost::format("getBookPage: saOwnerFunds=%s (cached)") % saOwnerFunds.getFullText());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Did not find balance in table.
|
||||||
|
|
||||||
umBalance[uOfferOwnerID] = saOwnerBalance;
|
saOwnerFunds = lesActive.accountHolds(uOfferOwnerID, uTakerGetsCurrencyID, uTakerGetsIssuerID);
|
||||||
|
// cLog(lsINFO) << boost::str(boost::format("getBookPage: saOwnerFunds=%s (new)") % saOwnerFunds.getFullText());
|
||||||
|
if (saOwnerFunds.isNegative())
|
||||||
|
saOwnerFunds.zero();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!saOwnerFunds.isZero() || uOfferOwnerID == uTakerID)
|
Json::Value jvOffer = sleOffer->getJson(0);
|
||||||
{
|
|
||||||
// Only provide funded offers and offers of the taker.
|
|
||||||
jvOffers.append(jvOffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!lesActive.dirNext(uTipIndex, sleOfferDir, uBookEntry, uOfferIndex))
|
STAmount saTakerGetsFunded;
|
||||||
{
|
|
||||||
bDirectAdvance = true;
|
if (saOwnerFunds >= saTakerGets)
|
||||||
}
|
{
|
||||||
else
|
// Sufficient funds no shenanigans.
|
||||||
{
|
saTakerGetsFunded = saTakerGets;
|
||||||
cLog(lsTRACE) << boost::str(boost::format("getBookPage: uOfferIndex=%s") % uOfferIndex);
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// cLog(lsINFO) << boost::str(boost::format("getBookPage: saTakerGets=%s") % saTakerGets.getFullText());
|
||||||
|
// cLog(lsINFO) << boost::str(boost::format("getBookPage: saTakerPays=%s") % saTakerPays.getFullText());
|
||||||
|
// cLog(lsINFO) << boost::str(boost::format("getBookPage: saOwnerFunds=%s") % saOwnerFunds.getFullText());
|
||||||
|
// cLog(lsINFO) << boost::str(boost::format("getBookPage: saDirRate=%s") % saDirRate.getText());
|
||||||
|
// cLog(lsINFO) << boost::str(boost::format("getBookPage: multiply=%s") % STAmount::multiply(saTakerGetsFunded, saDirRate).getFullText());
|
||||||
|
// cLog(lsINFO) << boost::str(boost::format("getBookPage: multiply=%s") % STAmount::multiply(saTakerGetsFunded, saDirRate, saTakerPays).getFullText());
|
||||||
|
STAmount saTakerPaysFunded;
|
||||||
|
|
||||||
|
saTakerGetsFunded = saOwnerFunds;
|
||||||
|
saTakerPaysFunded = std::min(saTakerPays, STAmount::multiply(saTakerGetsFunded, saDirRate, saTakerPays));
|
||||||
|
|
||||||
|
// Only provide, if not fully funded.
|
||||||
|
jvOffer["taker_gets_funded"] = saTakerGetsFunded.getJson(0);
|
||||||
|
jvOffer["taker_pays_funded"] = saTakerPaysFunded.getJson(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
STAmount saOwnerBalance = saOwnerFunds-saTakerGetsFunded;
|
||||||
|
|
||||||
|
umBalance[uOfferOwnerID] = saOwnerBalance;
|
||||||
|
|
||||||
|
if (!saOwnerFunds.isZero() || uOfferOwnerID == uTakerID)
|
||||||
|
{
|
||||||
|
// Only provide funded offers and offers of the taker.
|
||||||
|
jvOffers.append(jvOffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!lesActive.dirNext(uTipIndex, sleOfferDir, uBookEntry, uOfferIndex))
|
||||||
|
{
|
||||||
|
bDirectAdvance = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cLog(lsTRACE) << boost::str(boost::format("getBookPage: uOfferIndex=%s") % uOfferIndex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user