Skip to content

Commit

Permalink
only access results() of tx results with code txFAILED, move skip mod…
Browse files Browse the repository at this point in the history
…e to ifdef BUILDTEST
  • Loading branch information
ThomasBrady committed Nov 22, 2024
1 parent 3c2e52b commit c451463
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/catchup/ApplyCheckpointWork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ ApplyCheckpointWork::openInputFiles()
mTxIn.open(ti.localPath_nogz());
mTxHistoryEntry = TransactionHistoryEntry();
mHeaderHistoryEntry = LedgerHeaderHistoryEntry();
#ifdef BUILD_TESTS
if (mApp.getConfig().CATCHUP_SKIP_KNOWN_RESULTS_FOR_TESTING)
{
mTxResultIn = std::make_optional<XDRInputFileStream>();
Expand All @@ -107,6 +108,7 @@ ApplyCheckpointWork::openInputFiles()
mTxHistoryResultEntry =
std::make_optional<TransactionHistoryResultEntry>();
}
#endif
mFilesOpen = true;
}

Expand Down Expand Up @@ -152,6 +154,7 @@ ApplyCheckpointWork::getCurrentTxSet()
return TxSetXDRFrame::makeEmpty(lm.getLastClosedLedgerHeader());
}

#ifdef BUILD_TESTS
std::optional<TransactionResultSet>
ApplyCheckpointWork::getCurrentTxResultSet()
{
Expand Down Expand Up @@ -187,6 +190,7 @@ ApplyCheckpointWork::getCurrentTxResultSet()
CLOG_DEBUG(History, "No txresultset for ledger {}", seq);
return std::nullopt;
}
#endif // BUILD_TESTS

std::shared_ptr<LedgerCloseData>
ApplyCheckpointWork::getNextLedgerCloseData()
Expand Down Expand Up @@ -267,10 +271,12 @@ ApplyCheckpointWork::getNextLedgerCloseData()
txset->sizeTxTotal());

std::optional<TransactionResultSet> txres = std::nullopt;
#ifdef BUILD_TESTS
if (mApp.getConfig().CATCHUP_SKIP_KNOWN_RESULTS_FOR_TESTING)
{
txres = getCurrentTxResultSet();
}
#endif

// We've verified the ledgerHeader (in the "trusted part of history"
// sense) in CATCHUP_VERIFY phase; we now need to check that the
Expand Down
6 changes: 5 additions & 1 deletion src/catchup/ApplyCheckpointWork.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ class ApplyCheckpointWork : public BasicWork

XDRInputFileStream mHdrIn;
XDRInputFileStream mTxIn;
std::optional<XDRInputFileStream> mTxResultIn;
TransactionHistoryEntry mTxHistoryEntry;
#ifdef BUILD_TESTS
std::optional<XDRInputFileStream> mTxResultIn;
std::optional<TransactionHistoryResultEntry> mTxHistoryResultEntry;
#endif // BUILD_TESTS
LedgerHeaderHistoryEntry mHeaderHistoryEntry;
OnFailureCallback mOnFailure;

Expand All @@ -63,7 +65,9 @@ class ApplyCheckpointWork : public BasicWork
std::shared_ptr<ConditionalWork> mConditionalWork;

TxSetXDRFrameConstPtr getCurrentTxSet();
#ifdef BUILD_TESTS
std::optional<TransactionResultSet> getCurrentTxResultSet();
#endif // BUILD_TESTS
void openInputFiles();

std::shared_ptr<LedgerCloseData> getNextLedgerCloseData();
Expand Down
3 changes: 2 additions & 1 deletion src/catchup/DownloadApplyTxsWork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ DownloadApplyTxsWork::yieldMoreWork()
std::vector<std::shared_ptr<BasicWork>> seq{getAndUnzip};
std::vector<FileTransferInfo> filesToTransfer{ft};
std::vector<std::shared_ptr<BasicWork>> optionalDownloads;

#ifdef BUILD_TESTS
if (mApp.getConfig().CATCHUP_SKIP_KNOWN_RESULTS_FOR_TESTING)
{
CLOG_INFO(History,
Expand Down Expand Up @@ -128,6 +128,7 @@ DownloadApplyTxsWork::yieldMoreWork()

filesToTransfer.push_back(resultsFile);
}
#endif // BUILD_TESTS

auto maybeWaitForMerges = [](Application& app) {
if (app.getConfig().CATCHUP_WAIT_MERGES_TX_APPLY_FOR_TESTING)
Expand Down
2 changes: 2 additions & 0 deletions src/herder/LedgerCloseData.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ class LedgerCloseData
{
return mExpectedLedgerHash;
}
#ifdef BUILD_TESTS
std::optional<TransactionResultSet> const&
getExpectedResults() const
{
return mExpectedResults;
}
#endif // BUILD_TESTS

StoredDebugTransactionSet
toXDR() const
Expand Down
1 change: 1 addition & 0 deletions src/historywork/GetAndUnzipRemoteFileWork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "historywork/GetRemoteFileWork.h"
#include "historywork/GunzipFileWork.h"
#include "util/GlobalChecks.h"
#include "util/Logging.h"
#include <Tracy.hpp>
#include <fmt/format.h>

Expand Down
4 changes: 4 additions & 0 deletions src/ledger/LedgerManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,7 @@ LedgerManagerImpl::processFeesSeqNums(
auto header = ltx.loadHeader().current();
std::map<AccountID, SequenceNumber> accToMaxSeq;

#ifdef BUILD_TESTS
// If we have expected results, we assign them to the mutable tx results
// here.
std::optional<std::vector<TransactionResultPair>::const_iterator>
Expand All @@ -1373,6 +1374,7 @@ LedgerManagerImpl::processFeesSeqNums(
expectedResultsIter =
std::make_optional(expectedResults->results.begin());
}
#endif

bool mergeSeen = false;
for (auto tx : txs)
Expand All @@ -1381,6 +1383,7 @@ LedgerManagerImpl::processFeesSeqNums(

txResults.push_back(
tx->processFeeSeqNum(ltxTx, txSet.getTxBaseFee(tx, header)));
#ifdef BUILD_TESTS
if (expectedResultsIter)
{
releaseAssert(*expectedResultsIter !=
Expand All @@ -1391,6 +1394,7 @@ LedgerManagerImpl::processFeesSeqNums(
(*expectedResultsIter)->result);
++(*expectedResultsIter);
}
#endif // BUILD_TESTS

if (protocolVersionStartsFrom(
ltxTx.loadHeader().current().ledgerVersion,
Expand Down
3 changes: 2 additions & 1 deletion src/main/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,9 +878,10 @@ runCatchup(CommandLineArgs const& args)
config.RUN_STANDALONE = true;
config.MANUAL_CLOSE = true;
config.DISABLE_BUCKET_GC = disableBucketGC;

#ifdef BUILD_TESTS
// SET TO TRUE FOR TESTING PURPOSES, will be removed pre-merge.
config.CATCHUP_SKIP_KNOWN_RESULTS_FOR_TESTING = true;
#endif // BUILD_TESTS

if (config.AUTOMATIC_MAINTENANCE_PERIOD.count() > 0 &&
config.AUTOMATIC_MAINTENANCE_COUNT > 0)
Expand Down
2 changes: 2 additions & 0 deletions src/main/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1150,10 +1150,12 @@ Config::processConfig(std::shared_ptr<cpptoml::table> t)
CATCHUP_RECENT =
readInt<uint32_t>(item, 0, UINT32_MAX - 1);
}},
#ifdef BUILD_TESTS
{"CATCHUP_SKIP_KNOWN_RESULTS_FOR_TESTING",
[&]() {
CATCHUP_SKIP_KNOWN_RESULTS_FOR_TESTING = readBool(item);
}},
#endif // BUILD_TESTS
{"ARTIFICIALLY_GENERATE_LOAD_FOR_TESTING",
[&]() {
ARTIFICIALLY_GENERATE_LOAD_FOR_TESTING = readBool(item);
Expand Down
2 changes: 2 additions & 0 deletions src/main/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,12 @@ class Config : public std::enable_shared_from_this<Config>
// If you want, say, a week of history, set this to 120000.
uint32_t CATCHUP_RECENT;

#ifdef BUILD_TESTS
// Mode for "accelerated" catchup. If set to true, the node will skip
// application of failed transactions and will not verify signatures of
// successful transactions.
bool CATCHUP_SKIP_KNOWN_RESULTS_FOR_TESTING;
#endif // BUILD_TESTS

// Interval between automatic maintenance executions
std::chrono::seconds AUTOMATIC_MAINTENANCE_PERIOD;
Expand Down
4 changes: 4 additions & 0 deletions src/transactions/MutableTransactionResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ MutableTransactionResult::isSuccess() const
return getResult().result.code() == txSUCCESS;
}

#ifdef BUILD_TESTS
void
MutableTransactionResult::setReplayTransactionResult(
TransactionResult const& replayResult)
Expand All @@ -326,6 +327,7 @@ MutableTransactionResult::getReplayTransactionResult() const
{
return mReplayTransactionResult;
}
#endif // BUILD_TESTS

FeeBumpMutableTransactionResult::FeeBumpMutableTransactionResult(
MutableTxResultPtr innerTxResult)
Expand Down Expand Up @@ -461,6 +463,7 @@ FeeBumpMutableTransactionResult::isSuccess() const
return mTxResult->result.code() == txFEE_BUMP_INNER_SUCCESS;
}

#ifdef BUILD_TESTS
void
FeeBumpMutableTransactionResult::setReplayTransactionResult(
TransactionResult const& replayResult)
Expand All @@ -473,4 +476,5 @@ FeeBumpMutableTransactionResult::getReplayTransactionResult() const
{
return mReplayTransactionResult;
}
#endif // BUILD_TESTS
}
6 changes: 6 additions & 0 deletions src/transactions/MutableTransactionResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ class MutableTransactionResultBase : public NonMovableOrCopyable
virtual void refundSorobanFee(int64_t feeRefund,
uint32_t ledgerVersion) = 0;
virtual bool isSuccess() const = 0;
#ifdef BUILD_TESTS
virtual std::optional<TransactionResult> const&
getReplayTransactionResult() const = 0;
virtual void
setReplayTransactionResult(TransactionResult const& replayResult) = 0;
#endif
};

class MutableTransactionResult : public MutableTransactionResultBase
Expand All @@ -128,10 +130,12 @@ class MutableTransactionResult : public MutableTransactionResultBase
TransactionResult const& getResult() const override;
TransactionResultCode getResultCode() const override;
void setResultCode(TransactionResultCode code) override;
#ifdef BUILD_TESTS
void
setReplayTransactionResult(TransactionResult const& replayResult) override;
std::optional<TransactionResult> const&
getReplayTransactionResult() const override;
#endif // BUILD_TESTS

OperationResult& getOpResultAt(size_t index) override;
std::shared_ptr<SorobanTxData> getSorobanData() override;
Expand Down Expand Up @@ -186,9 +190,11 @@ class FeeBumpMutableTransactionResult : public MutableTransactionResultBase
void refundSorobanFee(int64_t feeRefund, uint32_t ledgerVersion) override;
bool isSuccess() const override;

#ifdef BUILD_TESTS
void
setReplayTransactionResult(TransactionResult const& replayResult) override;
std::optional<TransactionResult> const&
getReplayTransactionResult() const override;
#endif // BUILD_TESTS
};
}
2 changes: 2 additions & 0 deletions src/transactions/SignatureChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class SignatureChecker : public AbstractSignatureChecker
std::vector<bool> mUsedSignatures;
};

#ifdef BUILD_TESTS
class AlwaysValidSignatureChecker : public AbstractSignatureChecker
{
public:
Expand All @@ -62,4 +63,5 @@ class AlwaysValidSignatureChecker : public AbstractSignatureChecker
return true;
}
};
#endif // BUILD_TESTS
}
12 changes: 11 additions & 1 deletion src/transactions/TransactionFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,7 @@ TransactionFrame::applyOperations(AbstractSignatureChecker& signatureChecker,
Hash const& sorobanBasePrngSeed) const
{
ZoneScoped;
#ifdef BUILD_TESTS
auto const& result = txResult.getReplayTransactionResult();
if (result && result->result.code() != txSUCCESS)
{
Expand All @@ -1584,9 +1585,14 @@ TransactionFrame::applyOperations(AbstractSignatureChecker& signatureChecker,
CLOG_DEBUG(Tx, "Skipping replay of failed transaction: tx {}",
binToHex(getContentsHash()));
txResult.setResultCode(result->result.code());
txResult.getResult().result.results() = result->result.results();
// results field is only active if code is txFAILED or txSUCCESS
if (result->result.code() == txFAILED)
{
txResult.getResult().result.results() = result->result.results();
}
return false;
}
#endif

auto& internalErrorCounter = app.getMetrics().NewCounter(
{"ledger", "transaction", "internal-error"});
Expand Down Expand Up @@ -1800,6 +1806,7 @@ TransactionFrame::apply(AppConnector& app, AbstractLedgerTxn& ltx,
mCachedAccountPreProtocol8.reset();
uint32_t ledgerVersion = ltx.loadHeader().current().ledgerVersion;
std::unique_ptr<AbstractSignatureChecker> signatureChecker;
#ifdef BUILD_TESTS
// If the txResult has a replay result (catchup in skip mode is
// enabled),
// we do not perform signature verification.
Expand All @@ -1810,9 +1817,12 @@ TransactionFrame::apply(AppConnector& app, AbstractLedgerTxn& ltx,
}
else
{
#endif // BUILD_TESTS
signatureChecker = std::make_unique<SignatureChecker>(
ledgerVersion, getContentsHash(), getSignatures(mEnvelope));
#ifdef BUILD_TESTS
}
#endif // BUILD_TESTS

// when applying, a failure during tx validation means that
// we'll skip trying to apply operations but we'll still
Expand Down

0 comments on commit c451463

Please sign in to comment.