diff --git a/src/catchup/ApplyCheckpointWork.cpp b/src/catchup/ApplyCheckpointWork.cpp index f26c369080..17c6fc7d05 100644 --- a/src/catchup/ApplyCheckpointWork.cpp +++ b/src/catchup/ApplyCheckpointWork.cpp @@ -155,6 +155,15 @@ ApplyCheckpointWork::getCurrentTxSet() } else { + // The legacy field has to be empty when the generalized tx set + // is present. + if (!mTxHistoryEntry.txSet.txs.empty()) + { + throw std::runtime_error(fmt::format( + FMT_STRING("txset entry for {:d} has both legacy and " + "generalized transactions"), + seq)); + } return TxSetXDRFrame::makeFromWire( mTxHistoryEntry.ext.generalizedTxSet()); } @@ -349,6 +358,18 @@ ApplyCheckpointWork::getNextLedgerCloseData() "hash"), header.ledgerSeq, txset->sizeTxTotal())); } + + // Empty tx sets are not stored in history archives, so if no txs are + // present, the tx set must match the canonical empty tx set. + if (txset->getContentsHash() != + TxSetXDRFrame::makeEmpty(lclHeader)->getContentsHash()) + { + throw std::runtime_error(fmt::format( + FMT_STRING("replay txset for {:d} is not the canonical empty " + "tx set, but its ledger header carries the " + "empty-tx-set hash"), + header.ledgerSeq)); + } } else if (header.scpValue.txSetHash != txset->getContentsHash()) { diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index 4b613889cc..72e5b50c7a 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -1774,6 +1774,16 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger, mTransactionQueue.ban( invalidTxPhases[static_cast(TxSetPhase::CLASSIC)]); +#ifdef BUILD_TESTS + if (mNominatedTxSetOverride) + { + proposedSet = mNominatedTxSetOverride(lcl); + CLOG_INFO(Herder, "Nominating overridden tx set {} for slot {}", + hexAbbrev(proposedSet->getContentsHash()), + lcl.header.ledgerSeq + 1); + } +#endif + // Stop before addTxSet below: its side effects can include SCP callbacks // and even externalizing a ledger, which are not tx set building. mSCPMetrics.mTxSetBuild.Update( diff --git a/src/herder/HerderImpl.h b/src/herder/HerderImpl.h index b775be7d2e..fd4141ae27 100644 --- a/src/herder/HerderImpl.h +++ b/src/herder/HerderImpl.h @@ -149,6 +149,11 @@ class HerderImpl : public Herder { mFlowControlExtraBuffer = std::make_optional(bytes); } + + // When set, this node nominates the tx set returned by this function + // (given the LCL) instead of the one it builds. + std::function + mNominatedTxSetOverride; #endif void sendSCPStateToPeer(uint32 ledgerSeq, Peer::pointer peer) override; diff --git a/src/herder/TxSetFrame.cpp b/src/herder/TxSetFrame.cpp index de6a803219..358fb10c8a 100644 --- a/src/herder/TxSetFrame.cpp +++ b/src/herder/TxSetFrame.cpp @@ -104,8 +104,20 @@ validateSequentialPhaseXDRStructure(TransactionPhase const& phase) } bool -validateParallelComponent(ParallelTxsComponent const& component) -{ +validateParallelComponent(ParallelTxsComponent const& component, + uint32_t ledgerVersion) +{ + // A base fee without any transactions to apply it to has no effect, so + // base fee should be 0, (see + // CANONICAL_EMPTY_SOROBAN_PHASE_PROTOCOL_VERSION) comment in + // ProtocolVersion.h for more details). + if (component.executionStages.empty() && component.baseFee && + protocolVersionStartsFrom( + ledgerVersion, CANONICAL_EMPTY_SOROBAN_PHASE_PROTOCOL_VERSION)) + { + CLOG_DEBUG(Herder, "Got bad txSet: base fee without stages"); + return false; + } for (auto const& stage : component.executionStages) { if (stage.empty()) @@ -126,7 +138,8 @@ validateParallelComponent(ParallelTxsComponent const& component) } bool -validateTxSetXDRStructure(GeneralizedTransactionSet const& txSet) +validateTxSetXDRStructure(GeneralizedTransactionSet const& txSet, + uint32_t ledgerVersion) { int const MAX_PHASE = 1; @@ -164,7 +177,8 @@ validateTxSetXDRStructure(GeneralizedTransactionSet const& txSet) phase.v()); return false; } - if (!validateParallelComponent(phase.parallelTxsComponent())) + if (!validateParallelComponent(phase.parallelTxsComponent(), + ledgerVersion)) { return false; } @@ -1133,7 +1147,7 @@ TxSetXDRFrame::prepareForApply(Application& app, if (isGeneralizedTxSet()) { auto const& xdrTxSet = std::get(mXDRTxSet); - if (!validateTxSetXDRStructure(xdrTxSet)) + if (!validateTxSetXDRStructure(xdrTxSet, lclHeader.ledgerVersion)) { CLOG_DEBUG(Herder, "Got bad generalized txSet with invalid XDR structure"); diff --git a/src/herder/test/HerderTests.cpp b/src/herder/test/HerderTests.cpp index f9eb5740d3..65518affa0 100644 --- a/src/herder/test/HerderTests.cpp +++ b/src/herder/test/HerderTests.cpp @@ -9409,6 +9409,95 @@ TEST_CASE_VERSIONS("Herder properly validates when tx set is missing", }); } +// One of four validators nominates a non-canonical empty tx set (a tx set +// without transactions that isn't encoded as `TxSetXDRFrame::makeEmpty`, +// which history can't replay) on every ledger. Before the protocol gate the +// network externalizes it, after the gate it's rejected as invalid. +TEST_CASE("network does not externalize non-canonical empty tx set", + "[herder][txset]") +{ + auto runTest = [](uint32_t protocolVersion, bool expectExternalized) { + auto makeConfig = [&](int i) { + auto cfg = getTestConfig(i, Config::TESTDB_DEFAULT); + cfg.LEDGER_PROTOCOL_VERSION = protocolVersion; + cfg.TESTING_UPGRADE_LEDGER_PROTOCOL_VERSION = protocolVersion; + // Measured in wall-clock time, so it would never expire here + cfg.TX_SET_DOWNLOAD_TIMEOUT = std::chrono::milliseconds{0}; + return cfg; + }; + + // No transactions, but the parallel Soroban phase carries a base fee, + // so this doesn't hash like `makeEmpty` + auto makeNonCanonicalEmptyTxSet = [](Hash const& prevHash) { + GeneralizedTransactionSet xdr(1); + xdr.v1TxSet().previousLedgerHash = prevHash; + xdr.v1TxSet().phases.emplace_back(0); + xdr.v1TxSet() + .phases.emplace_back(1) + .parallelTxsComponent() + .baseFee.activate() = 12345; + return TxSetXDRFrame::makeFromWire(xdr); + }; + auto simulation = Topologies::core( + 4, 0.75, Simulation::OVER_LOOPBACK, + sha256(getTestConfig().NETWORK_PASSPHRASE), makeConfig); + auto nodes = simulation->getNodes(); + static_cast(nodes[0]->getHerder()) + .mNominatedTxSetOverride = + [&](LedgerHeaderHistoryEntry const& lcl) { + return makeNonCanonicalEmptyTxSet(lcl.hash); + }; + simulation->startAllNodes(); + + // Inspect every ledger an honest node closes + auto& lm = nodes[1]->getLedgerManager(); + uint32_t seen = lm.getLastClosedLedgerNum(); + uint32_t const target = seen + 20; + uint32_t externalized = 0, replaced = 0; + simulation->crankUntil( + [&]() { + auto const& lcl = lm.getLastClosedLedgerHeader(); + if (lcl.header.ledgerSeq > seen) + { + REQUIRE(lcl.header.ledgerSeq == seen + 1); + seen = lcl.header.ledgerSeq; + auto const& sv = lcl.header.scpValue; + externalized += + sv.txSetHash == makeNonCanonicalEmptyTxSet( + lcl.header.previousLedgerHash) + ->getContentsHash(); + replaced += isEmptyTxSetStellarValue(sv); + } + return seen >= target; + }, + 30 * simulation->getExpectedLedgerCloseTime(), false); + + CAPTURE(externalized, replaced); + if (expectExternalized) + { + REQUIRE(externalized > 0); + } + else + { + REQUIRE(externalized == 0); + REQUIRE(replaced > 0); + } + }; + + SECTION("before protocol gate") + { + runTest(static_cast( + CANONICAL_EMPTY_SOROBAN_PHASE_PROTOCOL_VERSION) - + 1, + /*expectExternalized=*/true); + } + SECTION("current protocol") + { + runTest(Config::CURRENT_LEDGER_PROTOCOL_VERSION, + /*expectExternalized=*/false); + } +} + // This tests that the network externalizes an empty-tx-set value when a // voted-for value is not available on the network. TEST_CASE("network externalizes empty-tx-set on missing value", "[herder][tx]") diff --git a/src/herder/test/TxSetTests.cpp b/src/herder/test/TxSetTests.cpp index a2fbc4840d..f94b311f75 100644 --- a/src/herder/test/TxSetTests.cpp +++ b/src/herder/test/TxSetTests.cpp @@ -328,11 +328,21 @@ TEST_CASE("generalized tx set XDR validation", "[txset]") auto prevScenariosSize = scenarios[1].size(); + // A base fee without transactions is only allowed prior to + // CANONICAL_EMPTY_SOROBAN_PHASE_PROTOCOL_VERSION. + bool const baseFeeWithoutTxsValid = protocolVersionIsBefore( + cfg.LEDGER_PROTOCOL_VERSION, + CANONICAL_EMPTY_SOROBAN_PHASE_PROTOCOL_VERSION); + // Valid scenarios scenarios[1].emplace_back(parallelPhase({}), true, "parallel Soroban - no txs"); - scenarios[1].emplace_back(parallelPhase({}, true, 1000), true, + scenarios[1].emplace_back(parallelPhase({}, true, 1000), + baseFeeWithoutTxsValid, "parallel Soroban - no txs, fee discount"); + scenarios[1].emplace_back( + parallelPhase({}, true, 0), baseFeeWithoutTxsValid, + "parallel Soroban - no txs, zero fee discount"); scenarios[1].emplace_back(parallelPhase({{10}}), true, "parallel Soroban - 1 stage, 1 cluster"); scenarios[1].emplace_back( @@ -458,10 +468,17 @@ TEST_CASE("generalized tx set XDR validation", "[txset]") bool valid = classicIsValid && sorobanIsValid; if (valid) { - REQUIRE(txSet->prepareForApply( - *app, app->getLedgerManager() - .getLastClosedLedgerHeader() - .header) != nullptr); + auto applicableTxSet = txSet->prepareForApply( + *app, app->getLedgerManager() + .getLastClosedLedgerHeader() + .header); + REQUIRE(applicableTxSet != nullptr); + // Every structurally valid tx set must be canonical, i.e. + // the frame we interpret it as is encoded exactly as the + // original tx set. + REQUIRE(applicableTxSet->toWireTxSetFrame() + ->getContentsHash() == + txSet->getContentsHash()); } else { diff --git a/src/history/test/HistoryTests.cpp b/src/history/test/HistoryTests.cpp index 867339dbff..ea86204578 100644 --- a/src/history/test/HistoryTests.cpp +++ b/src/history/test/HistoryTests.cpp @@ -1170,7 +1170,7 @@ TEST_CASE("History catchup over empty-tx-set ledgers", "[history][catchup]") } } -TEST_CASE("History catchup rejects empty-tx-set ledgers with transactions", +TEST_CASE("History catchup rejects forged tx sets for empty-tx-set ledgers", "[history][catchup]") { CatchupSimulation catchupSimulation{}; @@ -1180,6 +1180,8 @@ TEST_CASE("History catchup rejects empty-tx-set ledgers with transactions", { catchupSimulation.generateRandomLedger(); } + Hash const prevLedgerHash = + simApp.getLedgerManager().getLastClosedLedgerHeader().hash; catchupSimulation.generateEmptyTxSetLedger(); uint32_t const emptyTxSetSeq = simApp.getLedgerManager().getLastClosedLedgerNum(); @@ -1187,7 +1189,7 @@ TEST_CASE("History catchup rejects empty-tx-set ledgers with transactions", auto checkpointLedger = catchupSimulation.getLastCheckpointLedger(1); catchupSimulation.ensureOfflineCatchupPossible(checkpointLedger); - // Forge a non-empty TransactionHistoryEntry for the empty-tx-set ledger + // Forge a TransactionHistoryEntry for the empty-tx-set ledger // into the published transactions file, keeping the file sorted by // ledgerSeq. std::string archiveDir = @@ -1214,9 +1216,49 @@ TEST_CASE("History catchup rejects empty-tx-set ledgers with transactions", REQUIRE(!txs.empty()); REQUIRE(std::filesystem::remove(nonGzPath)); - // Copy an existing non-empty entry and retarget it at the empty-tx-set - // ledger - TransactionHistoryEntry forged = txs.front(); + // Honest archives never contain an entry for an empty-tx-set ledger, + // as it has no transactions. Nothing in the header binds such an + // entry, so replay must reject any of them. + TransactionHistoryEntry forged; + SECTION("tx set with transactions") + { + // Copy an existing non-empty entry + forged = txs.front(); + } + SECTION("legacy tx set without transactions") + { + // Applying this would crash the node when populating the + // (generalized) tx set in the ledger close meta. + forged.txSet.previousLedgerHash = prevLedgerHash; + } + SECTION("non-canonical tx set without transactions") + { + // No transactions, but the parallel Soroban phase carries a base + // fee, so this doesn't hash like `makeEmpty` + forged.ext.v(1); + auto& xdr = forged.ext.generalizedTxSet(); + xdr.v(1); + xdr.v1TxSet().previousLedgerHash = prevLedgerHash; + xdr.v1TxSet().phases.emplace_back(0); + xdr.v1TxSet() + .phases.emplace_back(1) + .parallelTxsComponent() + .baseFee.activate() = 12345; + } + SECTION("canonical tx set with legacy transactions") + { + // Both the legacy and the generalized tx set are populated, + // which is malformed and rejected before it's looked at (so the + // legacy transaction contents don't matter). + forged.ext.v(1); + TxSetXDRFrame::makeEmpty(prevLedgerHash, + simApp.getLedgerManager() + .getLastClosedLedgerHeader() + .header.ledgerVersion) + ->toXDR(forged.ext.generalizedTxSet()); + forged.txSet.txs.emplace_back(); + } + // Retarget the entry at the empty-tx-set ledger forged.ledgerSeq = emptyTxSetSeq; auto insertAt = std::find_if(txs.begin(), txs.end(), [&](auto const& e) { @@ -1255,7 +1297,7 @@ TEST_CASE("History catchup rejects empty-tx-set ledgers with transactions", auto work = appWm.executeWork( tmpDir, range, lastApplied, /*waitForPublish=*/true, nullptr); REQUIRE(work->getState() == BasicWork::State::WORK_FAILURE); - // Replay stops exactly at the skip ledger whose forged entry was rejected + // Replay stops exactly at the ledger whose forged entry was rejected REQUIRE(app->getLedgerManager().getLastClosedLedgerNum() == emptyTxSetSeq - 1); } diff --git a/src/util/ProtocolVersion.h b/src/util/ProtocolVersion.h index 811f2e0791..403371d164 100644 --- a/src/util/ProtocolVersion.h +++ b/src/util/ProtocolVersion.h @@ -94,4 +94,18 @@ constexpr ProtocolVersion LOWER_MAX_MESSAGE_SIZE_PROTOCOL_VERSION = constexpr ProtocolVersion MS_CLOSE_TIME_PROTOCOL_VERSION = ProtocolVersion::V_30; +// Starting from this protocol, a parallel Soroban phase without execution +// stages must not specify a base fee. Without this rule a tx set with no +// transactions can take many forms and hash to many different values. The +// problem is, we don't actually upload empty phases to history, we just rebuild +// them on the fly assuming the base fee is 0. A non-zero base fee with empty +// execution stages is essentially lost in history, blocking catchup. +// +// The protocol gate only protects the upgrade period, during which validators +// on older builds still accept such tx sets. Once the network is on protocol +// 30 it can likely be removed (i.e. the rule enforced for all protocols), as +// long as no pre-30 ledger contains such a phase. +constexpr ProtocolVersion CANONICAL_EMPTY_SOROBAN_PHASE_PROTOCOL_VERSION = + ProtocolVersion::V_30; + } // namespace stellar