Skip to content

Commit

Permalink
Check invariants
Browse files Browse the repository at this point in the history
  • Loading branch information
sisuresh committed Jul 2, 2024
1 parent e0e5b03 commit df49917
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/ledger/LedgerManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "herder/TxSetFrame.h"
#include "herder/Upgrades.h"
#include "history/HistoryManager.h"
#include "invariant/InvariantDoesNotHold.h"
#include "invariant/InvariantManager.h"
#include "ledger/FlushAndRotateMetaDebugWork.h"
#include "ledger/LedgerHeaderUtils.h"
#include "ledger/LedgerRange.h"
Expand Down Expand Up @@ -1607,6 +1609,7 @@ LedgerManagerImpl::applyCluster(ClusterEntryMap& entryMap, Config const& config,
txFAILED);
}
txBundle.mOpMetrics = res.mOpMetrics;
txBundle.mDelta = res.mDelta;
}
}

Expand Down Expand Up @@ -1746,6 +1749,31 @@ LedgerManagerImpl::applySorobanStage(Application& app, AbstractLedgerTxn& ltx,
{
for (auto const& txBundle : cluster)
{
// First check the invariants
if (txBundle.resPayload->isSuccess())
{
try
{
// Soroban transactions don't have access to the ledger
// header, so they can't modify it. Pass in the current
// header as both current and previous.
txBundle.mDelta->header.current =
ltxInner.loadHeader().current();
txBundle.mDelta->header.previous =
ltxInner.loadHeader().current();
app.getInvariantManager().checkOnOperationApply(
txBundle.tx->getRawOperations().at(0),
txBundle.resPayload->getOpResultAt(0),
*txBundle.mDelta);
}
catch (InvariantDoesNotHold& e)
{
printErrorAndAbort(
"Invariant failure while applying operations: ",
e.what());
}
}

txBundle.tx->processPostApply(mApp, ltxInner, txBundle.meta,
txBundle.resPayload);

Expand Down
28 changes: 23 additions & 5 deletions src/transactions/TransactionFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,8 @@ TransactionFrame::parallelApply(

if (!fastFail && res.mSuccess)
{
res.mDelta = std::make_shared<LedgerTxnDelta>();
auto& delta = *res.mDelta;
// Build OperationMeta
LedgerEntryChanges changes;
for (auto const& newUpdates : res.mModifiedEntryMap)
Expand Down Expand Up @@ -1605,6 +1607,27 @@ TransactionFrame::parallelApply(
changes.emplace_back(LEDGER_ENTRY_CREATED);
changes.back().created() = *le;
}

LedgerTxnDelta::EntryDelta entryDelta;
if (prevLe)
{
entryDelta.previous =
std::make_shared<InternalLedgerEntry>(*prevLe);
}
if (le)
{
auto deltaLe = *le;
// This is for the invariants check in LedgerManager
deltaLe.lastModifiedLedgerSeq = ledgerSeq;

entryDelta.current =
std::make_shared<InternalLedgerEntry>(deltaLe);
}

delta.entry[lk] = entryDelta;
// Note that we don't set delta.header here because Soroban
// transactions don't modify the header. The header will be set
// right before we cal into the invariants.
}

xdr::xvector<OperationMeta> operationMetas;
Expand Down Expand Up @@ -1634,11 +1657,6 @@ TransactionFrame::parallelApply(

return res;
}
/* catch (InvariantDoesNotHold& e)
{
printErrorAndAbort("Invariant failure while applying operations: ",
e.what());
} */
catch (std::bad_alloc& e)
{
printErrorAndAbort("Exception while applying operations: ", e.what());
Expand Down
2 changes: 2 additions & 0 deletions src/transactions/TransactionFrameBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct ParallelOpReturnVal
bool mSuccess{false};
ModifiedEntryMap mModifiedEntryMap;
std::shared_ptr<SorobanOpMetrics> mOpMetrics;
std::shared_ptr<LedgerTxnDelta> mDelta;
};

// temporary. Remove this
Expand All @@ -70,6 +71,7 @@ class TxBundle
// TODO: Stop using mutable
mutable TransactionMetaFrame meta;
mutable std::shared_ptr<SorobanOpMetrics> mOpMetrics;
mutable std::shared_ptr<LedgerTxnDelta> mDelta;
};

typedef std::vector<TxBundle> Cluster;
Expand Down

0 comments on commit df49917

Please sign in to comment.