Skip to content
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

Best-offer debugging #2944

Merged
merged 5 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ AC_ARG_ENABLE([extrachecks],
[build with additional debugging checks enabled]))
AS_IF([test "x$enable_extrachecks" = "xyes"], [
# don't try to detect which c++ library we're using
CXXFLAGS="$CXXFLAGS -D_GLIBCXX_DEBUG=1 -D_GLIBCXX_SANITIZE_VECTOR=1 -D_LIBCPP_DEBUG=0"
CXXFLAGS="$CXXFLAGS -D_GLIBCXX_DEBUG=1 -D_GLIBCXX_SANITIZE_VECTOR=1 -D_LIBCPP_DEBUG=0 -DBEST_OFFER_DEBUGGING"
])

AC_ARG_ENABLE([ccache],
Expand Down
26 changes: 25 additions & 1 deletion src/ledger/InMemoryLedgerTxnRoot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@
namespace stellar
{

InMemoryLedgerTxnRoot::InMemoryLedgerTxnRoot()
InMemoryLedgerTxnRoot::InMemoryLedgerTxnRoot(
#ifdef BEST_OFFER_DEBUGGING
bool bestOfferDebuggingEnabled
#endif
)
: mHeader(std::make_unique<LedgerHeader>())
#ifdef BEST_OFFER_DEBUGGING
, mBestOfferDebuggingEnabled(bestOfferDebuggingEnabled)
#endif
{
}

Expand Down Expand Up @@ -140,4 +147,21 @@ InMemoryLedgerTxnRoot::resetForFuzzer()
abort();
}
#endif // FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION

#ifdef BEST_OFFER_DEBUGGING
bool
InMemoryLedgerTxnRoot::bestOfferDebuggingEnabled() const
{
return mBestOfferDebuggingEnabled;
}

std::shared_ptr<LedgerEntry const>
InMemoryLedgerTxnRoot::getBestOfferSlow(Asset const& buying,
Asset const& selling,
OfferDescriptor const* worseThan,
std::unordered_set<int64_t>& exclude)
{
return nullptr;
}
#endif
}
19 changes: 18 additions & 1 deletion src/ledger/InMemoryLedgerTxnRoot.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ class InMemoryLedgerTxnRoot : public AbstractLedgerTxnParent
{
std::unique_ptr<LedgerHeader> mHeader;

#ifdef BEST_OFFER_DEBUGGING
bool const mBestOfferDebuggingEnabled;
#endif

public:
InMemoryLedgerTxnRoot();
InMemoryLedgerTxnRoot(
#ifdef BEST_OFFER_DEBUGGING
bool bestOfferDebuggingEnabled
#endif
);
void addChild(AbstractLedgerTxn& child) override;
void commitChild(EntryIterator iter, LedgerTxnConsistency cons) override;
void rollbackChild() override;
Expand Down Expand Up @@ -61,5 +69,14 @@ class InMemoryLedgerTxnRoot : public AbstractLedgerTxnParent
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
void resetForFuzzer() override;
#endif // FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION

#ifdef BEST_OFFER_DEBUGGING
bool bestOfferDebuggingEnabled() const override;

std::shared_ptr<LedgerEntry const>
getBestOfferSlow(Asset const& buying, Asset const& selling,
OfferDescriptor const* worseThan,
std::unordered_set<int64_t>& exclude) override;
#endif
};
}
242 changes: 239 additions & 3 deletions src/ledger/LedgerTxn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ AssetPairHash::operator()(AssetPair const& key) const
return hashAsset(key.buying) ^ (hashAsset(key.selling) << 1);
}

#ifdef BEST_OFFER_DEBUGGING
void
compareOffers(std::shared_ptr<LedgerEntry const> debugBest,
std::shared_ptr<LedgerEntry const> best)
{
if ((bool)debugBest ^ (bool)best)
{
if (best)
{
printErrorAndAbort("best offer not null when it should be");
}
else
{
printErrorAndAbort("best offer null when it should not be");
}
}
if (best && *best != *debugBest)
{
printErrorAndAbort("best offer mismatch");
}
}
#endif

// Implementation of AbstractLedgerTxnParent --------------------------------
AbstractLedgerTxnParent::~AbstractLedgerTxnParent()
{
Expand Down Expand Up @@ -646,10 +669,118 @@ LedgerTxn::Impl::getAllOffers()
return offers;
}

#ifdef BEST_OFFER_DEBUGGING
bool
LedgerTxn::bestOfferDebuggingEnabled() const
{
return getImpl()->bestOfferDebuggingEnabled();
}

bool
LedgerTxn::Impl::bestOfferDebuggingEnabled() const
{
return mParent.bestOfferDebuggingEnabled();
}

std::shared_ptr<LedgerEntry const>
LedgerTxn::getBestOfferSlow(Asset const& buying, Asset const& selling,
OfferDescriptor const* worseThan,
std::unordered_set<int64_t>& exclude)
{
return getImpl()->getBestOfferSlow(buying, selling, worseThan, exclude);
}

std::shared_ptr<LedgerEntry const>
LedgerTxn::Impl::getBestOfferSlow(Asset const& buying, Asset const& selling,
OfferDescriptor const* worseThan,
std::unordered_set<int64_t>& exclude)
{
std::shared_ptr<InternalLedgerEntry const> selfBest;
for (auto const& kv : mEntry)
{
if (kv.first.type() != InternalLedgerEntryType::LEDGER_ENTRY)
{
continue;
}

auto const& key = kv.first.ledgerKey();
if (key.type() != OFFER)
{
continue;
}

if (!exclude.insert(key.offer().offerID).second)
{
continue;
}

if (kv.second)
{
auto const& le = kv.second->ledgerEntry();
if (!(le.data.offer().buying == buying &&
le.data.offer().selling == selling))
{
continue;
}

if (worseThan && !isBetterOffer(*worseThan, le))
sisuresh marked this conversation as resolved.
Show resolved Hide resolved
{
continue;
}

if (!selfBest || isBetterOffer(le, selfBest->ledgerEntry()))
{
selfBest = kv.second;
}
}
}

auto parentBest =
mParent.getBestOfferSlow(buying, selling, worseThan, exclude);
if (selfBest && !parentBest)
{
return std::make_shared<LedgerEntry const>(selfBest->ledgerEntry());
}
else if (parentBest && !selfBest)
{
return std::make_shared<LedgerEntry const>(*parentBest);
}
else if (parentBest && selfBest)
{
return isBetterOffer(selfBest->ledgerEntry(), *parentBest)
? std::make_shared<LedgerEntry const>(
selfBest->ledgerEntry())
: std::make_shared<LedgerEntry const>(*parentBest);
}
return nullptr;
}

std::shared_ptr<LedgerEntry const>
LedgerTxn::Impl::checkBestOffer(Asset const& buying, Asset const& selling,
OfferDescriptor const* worseThan,
std::shared_ptr<LedgerEntry const> best)
{
if (!bestOfferDebuggingEnabled())
{
return best;
}

std::unordered_set<int64_t> exclude;
auto debugBest = getBestOfferSlow(buying, selling, worseThan, exclude);
compareOffers(debugBest, best);
return best;
}
#endif

std::shared_ptr<LedgerEntry const>
LedgerTxn::getBestOffer(Asset const& buying, Asset const& selling)
{
#ifdef BEST_OFFER_DEBUGGING
return getImpl()->checkBestOffer(buying, selling, nullptr,
getImpl()->getBestOffer(buying, selling));
#else
return getImpl()->getBestOffer(buying, selling);
#endif
}

std::shared_ptr<LedgerEntry const>
Expand Down Expand Up @@ -752,7 +883,13 @@ std::shared_ptr<LedgerEntry const>
LedgerTxn::getBestOffer(Asset const& buying, Asset const& selling,
OfferDescriptor const& worseThan)
{
#ifdef BEST_OFFER_DEBUGGING
return getImpl()->checkBestOffer(
buying, selling, &worseThan,
getImpl()->getBestOffer(buying, selling, worseThan));
#else
return getImpl()->getBestOffer(buying, selling, worseThan);
#endif
}

std::shared_ptr<LedgerEntry const>
Expand Down Expand Up @@ -1980,13 +2117,28 @@ LedgerTxn::Impl::WorstBestOfferIteratorImpl::clone() const
size_t const LedgerTxnRoot::Impl::MIN_BEST_OFFERS_BATCH_SIZE = 5;

LedgerTxnRoot::LedgerTxnRoot(Database& db, size_t entryCacheSize,
size_t prefetchBatchSize)
: mImpl(std::make_unique<Impl>(db, entryCacheSize, prefetchBatchSize))
size_t prefetchBatchSize
#ifdef BEST_OFFER_DEBUGGING
,
bool bestOfferDebuggingEnabled
#endif
)
: mImpl(std::make_unique<Impl>(db, entryCacheSize, prefetchBatchSize
#ifdef BEST_OFFER_DEBUGGING
,
bestOfferDebuggingEnabled
#endif
))
{
}

LedgerTxnRoot::Impl::Impl(Database& db, size_t entryCacheSize,
size_t prefetchBatchSize)
size_t prefetchBatchSize
#ifdef BEST_OFFER_DEBUGGING
,
bool bestOfferDebuggingEnabled
#endif
)
: mMaxBestOffersBatchSize(
std::min(std::max(prefetchBatchSize, MIN_BEST_OFFERS_BATCH_SIZE),
MAX_OFFERS_TO_CROSS))
Expand All @@ -1995,6 +2147,9 @@ LedgerTxnRoot::Impl::Impl(Database& db, size_t entryCacheSize,
, mEntryCache(entryCacheSize)
, mBulkLoadBatchSize(prefetchBatchSize)
, mChild(nullptr)
#ifdef BEST_OFFER_DEBUGGING
, mBestOfferDebuggingEnabled(bestOfferDebuggingEnabled)
#endif
{
}

Expand Down Expand Up @@ -2484,17 +2639,98 @@ LedgerTxnRoot::Impl::getAllOffers()
return offersByKey;
}

#ifdef BEST_OFFER_DEBUGGING
bool
LedgerTxnRoot::bestOfferDebuggingEnabled() const
{
return mImpl->bestOfferDebuggingEnabled();
}

bool
LedgerTxnRoot::Impl::bestOfferDebuggingEnabled() const
{
return mBestOfferDebuggingEnabled;
}

std::shared_ptr<LedgerEntry const>
LedgerTxnRoot::getBestOfferSlow(Asset const& buying, Asset const& selling,
OfferDescriptor const* worseThan,
std::unordered_set<int64_t>& exclude)
{
return mImpl->getBestOfferSlow(buying, selling, worseThan, exclude);
}

std::shared_ptr<LedgerEntry const>
LedgerTxnRoot::Impl::getBestOfferSlow(Asset const& buying, Asset const& selling,
OfferDescriptor const* worseThan,
std::unordered_set<int64_t>& exclude)
{
size_t const BATCH_SIZE = 1024;
size_t nOffers = 0;
std::deque<LedgerEntry> offers;
do
{
nOffers += BATCH_SIZE;
loadBestOffers(offers, buying, selling, nOffers);

for (auto const& le : offers)
{
if (worseThan && !isBetterOffer(*worseThan, le))
{
continue;
}

if (exclude.find(le.data.offer().offerID) == exclude.end())
{
return std::make_shared<LedgerEntry const>(le);
}
}

offers.clear();
} while (offers.size() == nOffers);

return nullptr;
}

std::shared_ptr<LedgerEntry const>
LedgerTxnRoot::Impl::checkBestOffer(Asset const& buying, Asset const& selling,
OfferDescriptor const* worseThan,
std::shared_ptr<LedgerEntry const> best)
{
if (!bestOfferDebuggingEnabled())
{
return best;
}

std::unordered_set<int64_t> exclude;
auto debugBest = getBestOfferSlow(buying, selling, worseThan, exclude);
compareOffers(debugBest, best);
return best;
}
#endif

std::shared_ptr<LedgerEntry const>
LedgerTxnRoot::getBestOffer(Asset const& buying, Asset const& selling)
{
#ifdef BEST_OFFER_DEBUGGING
return mImpl->checkBestOffer(buying, selling, nullptr,
mImpl->getBestOffer(buying, selling, nullptr));
#else
return mImpl->getBestOffer(buying, selling, nullptr);
#endif
}

std::shared_ptr<LedgerEntry const>
LedgerTxnRoot::getBestOffer(Asset const& buying, Asset const& selling,
OfferDescriptor const& worseThan)
{
#ifdef BEST_OFFER_DEBUGGING
return mImpl->checkBestOffer(
buying, selling, &worseThan,
mImpl->getBestOffer(buying, selling, &worseThan));
#else
return mImpl->getBestOffer(buying, selling, &worseThan);
#endif
}

static std::deque<LedgerEntry>::const_iterator
Expand Down
Loading