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

Legacy tx sets: avoid using read-only LCL meant for main thread only #4659

Merged
merged 2 commits into from
Mar 4, 2025
Merged
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/herder/HerderSCPDriver.cpp
Original file line number Diff line number Diff line change
@@ -709,7 +709,7 @@ HerderSCPDriver::combineCandidates(uint64_t slotIndex,
auto cTxSet = mPendingEnvelopes.getTxSet(sv.txSetHash);
releaseAssert(cTxSet);
// Only valid applicable tx sets should be combined.
auto cApplicableTxSet = cTxSet->prepareForApply(mApp);
auto cApplicableTxSet = cTxSet->prepareForApply(mApp, lcl.header);
releaseAssert(cApplicableTxSet);
if (cTxSet->previousLedgerHash() == lcl.hash)
{
@@ -1246,7 +1246,7 @@ HerderSCPDriver::checkAndCacheTxSetValid(TxSetXDRFrame const& txSet,
ApplicableTxSetFrameConstPtr applicableTxSet;
if (txSet.previousLedgerHash() == lcl.hash)
{
applicableTxSet = txSet.prepareForApply(mApp);
applicableTxSet = txSet.prepareForApply(mApp, lcl.header);
}

bool res = true;
8 changes: 4 additions & 4 deletions src/herder/TxSetFrame.cpp
Original file line number Diff line number Diff line change
@@ -783,7 +783,7 @@ makeTxSetFromTransactions(PerPhaseTransactionList const& txPhases,
#endif

ApplicableTxSetFrameConstPtr outputApplicableTxSet =
outputTxSet->prepareForApply(app);
outputTxSet->prepareForApply(app, lclHeader.header);

if (!outputApplicableTxSet)
{
@@ -934,7 +934,8 @@ TxSetXDRFrame::toStellarMessage() const
#endif

ApplicableTxSetFrameConstPtr
TxSetXDRFrame::prepareForApply(Application& app) const
TxSetXDRFrame::prepareForApply(Application& app,
LedgerHeader const& lclHeader) const
{
#ifdef BUILD_TESTS
if (mApplicableTxSetOverride)
@@ -972,8 +973,7 @@ TxSetXDRFrame::prepareForApply(Application& app) const
{
auto const& xdrTxSet = std::get<TransactionSet>(mXDRTxSet);
auto maybePhase = TxSetPhaseFrame::makeFromWireLegacy(
app.getLedgerManager().getLastClosedLedgerHeader().header,
app.getNetworkID(), xdrTxSet.txs);
lclHeader, app.getNetworkID(), xdrTxSet.txs);
if (!maybePhase)
{
return nullptr;
3 changes: 2 additions & 1 deletion src/herder/TxSetFrame.h
Original file line number Diff line number Diff line change
@@ -143,7 +143,8 @@ class TxSetXDRFrame : public NonMovableOrCopyable
// This may *only* be called when LCL hash matches the `previousLedgerHash`
// of this `TxSetFrame` - tx sets with a wrong ledger hash shouldn't even
// be attempted to be interpreted.
ApplicableTxSetFrameConstPtr prepareForApply(Application& app) const;
ApplicableTxSetFrameConstPtr
prepareForApply(Application& app, LedgerHeader const& lclHeader) const;

bool isGeneralizedTxSet() const;

13 changes: 10 additions & 3 deletions src/herder/test/TestTxSetUtils.cpp
Original file line number Diff line number Diff line change
@@ -113,7 +113,8 @@ makeNonValidatedTxSet(std::vector<TransactionFrameBasePtr> const& txs,
{
auto xdrTxSet = makeTxSetXDR(txs, previousLedgerHash);
auto txSet = TxSetXDRFrame::makeFromWire(xdrTxSet);
auto applicableTxSet = txSet->prepareForApply(app);
auto applicableTxSet = txSet->prepareForApply(
app, app.getLedgerManager().getLastClosedLedgerHeader().header);
return std::make_pair(txSet, std::move(applicableTxSet));
}
} // namespace
@@ -135,7 +136,10 @@ makeNonValidatedGeneralizedTxSet(
auto xdrTxSet = makeGeneralizedTxSetXDR(txsPerBaseFee, previousLedgerHash,
*useParallelSorobanPhase);
auto txSet = TxSetXDRFrame::makeFromWire(xdrTxSet);
return std::make_pair(txSet, txSet->prepareForApply(app));
return std::make_pair(
txSet,
txSet->prepareForApply(
app, app.getLedgerManager().getLastClosedLedgerHeader().header));
}

std::pair<TxSetXDRFrameConstPtr, ApplicableTxSetFrameConstPtr>
@@ -214,7 +218,10 @@ makeNonValidatedGeneralizedTxSet(PhaseComponents const& classicTxsPerBaseFee,
}
normalizeParallelPhaseXDR(phase);
auto txSet = TxSetXDRFrame::makeFromWire(xdrTxSet);
return std::make_pair(txSet, txSet->prepareForApply(app));
return std::make_pair(
txSet,
txSet->prepareForApply(
app, app.getLedgerManager().getLastClosedLedgerHeader().header));
}
#endif

32 changes: 26 additions & 6 deletions src/herder/test/TxSetTests.cpp
Original file line number Diff line number Diff line change
@@ -41,21 +41,33 @@ TEST_CASE("generalized tx set XDR validation", "[txset]")
SECTION("no phases")
{
auto txSet = TxSetXDRFrame::makeFromWire(xdrTxSet);
REQUIRE(txSet->prepareForApply(*app) == nullptr);
REQUIRE(
txSet->prepareForApply(
*app,
app->getLedgerManager().getLastClosedLedgerHeader().header) ==
nullptr);
}
SECTION("one phase")
{
auto txSet = TxSetXDRFrame::makeFromWire(xdrTxSet);
xdrTxSet.v1TxSet().phases.emplace_back();
REQUIRE(txSet->prepareForApply(*app) == nullptr);
REQUIRE(
txSet->prepareForApply(
*app,
app->getLedgerManager().getLastClosedLedgerHeader().header) ==
nullptr);
}
SECTION("too many phases")
{
xdrTxSet.v1TxSet().phases.emplace_back();
xdrTxSet.v1TxSet().phases.emplace_back();
xdrTxSet.v1TxSet().phases.emplace_back();
auto txSet = TxSetXDRFrame::makeFromWire(xdrTxSet);
REQUIRE(txSet->prepareForApply(*app) == nullptr);
REQUIRE(
txSet->prepareForApply(
*app,
app->getLedgerManager().getLastClosedLedgerHeader().header) ==
nullptr);
}

SECTION("two phase scenarios")
@@ -442,11 +454,17 @@ TEST_CASE("generalized tx set XDR validation", "[txset]")
bool valid = classicIsValid && sorobanIsValid;
if (valid)
{
REQUIRE(txSet->prepareForApply(*app) != nullptr);
REQUIRE(txSet->prepareForApply(
*app, app->getLedgerManager()
.getLastClosedLedgerHeader()
.header) != nullptr);
}
else
{
REQUIRE(txSet->prepareForApply(*app) == nullptr);
REQUIRE(txSet->prepareForApply(
*app, app->getLedgerManager()
.getLastClosedLedgerHeader()
.header) == nullptr);
}
}
}
@@ -502,7 +520,9 @@ testGeneralizedTxSetXDRConversion(ProtocolVersion protocolVersion)
auto checkXdrRoundtrip = [&](GeneralizedTransactionSet const& txSetXdr) {
auto txSetFrame = TxSetXDRFrame::makeFromWire(txSetXdr);
ApplicableTxSetFrameConstPtr applicableFrame =
txSetFrame->prepareForApply(*app);
txSetFrame->prepareForApply(
*app,
app->getLedgerManager().getLastClosedLedgerHeader().header);
REQUIRE(applicableFrame->checkValid(*app, 0, 0));
GeneralizedTransactionSet newXdr;
applicableFrame->toWireTxSetFrame()->toXDR(newXdr);
2 changes: 1 addition & 1 deletion src/ledger/LedgerManagerImpl.cpp
Original file line number Diff line number Diff line change
@@ -877,7 +877,7 @@ LedgerManagerImpl::applyLedger(LedgerCloseData const& ledgerData,
header.current().scpValue = sv;

maybeResetLedgerCloseMetaDebugStream(header.current().ledgerSeq);
auto applicableTxSet = txSet->prepareForApply(mApp);
auto applicableTxSet = txSet->prepareForApply(mApp, prevHeader);

if (applicableTxSet == nullptr)
{