Skip to content
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
21 changes: 21 additions & 0 deletions src/catchup/ApplyCheckpointWork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Comment thread
SirTyson marked this conversation as resolved.
Resolved
{
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());
}
Expand Down Expand Up @@ -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())
{
Expand Down
10 changes: 10 additions & 0 deletions src/herder/HerderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1774,6 +1774,16 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger,
mTransactionQueue.ban(
invalidTxPhases[static_cast<size_t>(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(
Expand Down
5 changes: 5 additions & 0 deletions src/herder/HerderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ class HerderImpl : public Herder
{
mFlowControlExtraBuffer = std::make_optional<uint32_t>(bytes);
}

// When set, this node nominates the tx set returned by this function
// (given the LCL) instead of the one it builds.
std::function<TxSetXDRFrameConstPtr(LedgerHeaderHistoryEntry const&)>
mNominatedTxSetOverride;
#endif
void sendSCPStateToPeer(uint32 ledgerSeq, Peer::pointer peer) override;

Expand Down
24 changes: 19 additions & 5 deletions src/herder/TxSetFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Comment thread
SirTyson marked this conversation as resolved.
Resolved
{
CLOG_DEBUG(Herder, "Got bad txSet: base fee without stages");
return false;
}
for (auto const& stage : component.executionStages)
{
if (stage.empty())
Expand All @@ -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;

Expand Down Expand Up @@ -164,7 +177,8 @@ validateTxSetXDRStructure(GeneralizedTransactionSet const& txSet)
phase.v());
return false;
}
if (!validateParallelComponent(phase.parallelTxsComponent()))
if (!validateParallelComponent(phase.parallelTxsComponent(),
ledgerVersion))
{
return false;
}
Expand Down Expand Up @@ -1133,7 +1147,7 @@ TxSetXDRFrame::prepareForApply(Application& app,
if (isGeneralizedTxSet())
{
auto const& xdrTxSet = std::get<GeneralizedTransactionSet>(mXDRTxSet);
if (!validateTxSetXDRStructure(xdrTxSet))
if (!validateTxSetXDRStructure(xdrTxSet, lclHeader.ledgerVersion))
{
CLOG_DEBUG(Herder,
"Got bad generalized txSet with invalid XDR structure");
Expand Down
89 changes: 89 additions & 0 deletions src/herder/test/HerderTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<HerderImpl&>(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<uint32_t>(
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]")
Expand Down
27 changes: 22 additions & 5 deletions src/herder/test/TxSetTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
{
Expand Down
54 changes: 48 additions & 6 deletions src/history/test/HistoryTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand All @@ -1180,14 +1180,16 @@ 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();

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 =
Expand All @@ -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) {
Expand Down Expand Up @@ -1255,7 +1297,7 @@ TEST_CASE("History catchup rejects empty-tx-set ledgers with transactions",
auto work = appWm.executeWork<DownloadApplyTxsWork>(
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);
}
Expand Down
14 changes: 14 additions & 0 deletions src/util/ProtocolVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading