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

Deprecate obsolete tx fee history table #4385

Merged
merged 1 commit into from
Jul 19, 2024
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
7 changes: 2 additions & 5 deletions src/database/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ bool Database::gDriversRegistered = false;

// smallest schema version supported
static unsigned long const MIN_SCHEMA_VERSION = 21;
static unsigned long const SCHEMA_VERSION = 21;
static unsigned long const SCHEMA_VERSION = 22;

// These should always match our compiled version precisely, since we are
// using a bundled version to get access to carray(). But in case someone
Expand Down Expand Up @@ -213,10 +213,7 @@ Database::applySchemaUpgrade(unsigned long vers)
switch (vers)
{
case 22:
// Add schema upgrades for version 21 -> 22 here.
// Currently, this is empty as the schema is already at version 21 and
// there are no upgrades to apply. However, this case is left here to
// allow for future upgrades to be added.
deprecateTransactionFeeHistory(*this);
break;
default:
throw std::runtime_error("Unknown DB schema version");
Expand Down
18 changes: 3 additions & 15 deletions src/ledger/LedgerManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,6 @@ LedgerManagerImpl::processFeesSeqNums(
{
LedgerTxn ltx(ltxOuter);
auto header = ltx.loadHeader().current();
auto ledgerSeq = header.ledgerSeq;
std::map<AccountID, SequenceNumber> accToMaxSeq;

bool mergeSeen = false;
Expand Down Expand Up @@ -1384,18 +1383,7 @@ LedgerManagerImpl::processFeesSeqNums(
ledgerCloseMeta->setLastTxProcessingFeeProcessingChanges(
changes);
}
// Note to future: when we eliminate the txhistory and txfeehistory
// tables, the following step can be removed.
//
// Also note: for historical reasons the history tables number
// txs counting from 1, not 0. We preserve this for the time being
// in case anyone depends on it.
++index;
if (mApp.getConfig().MODE_STORES_HISTORY_MISC)
{
storeTransactionFee(mApp.getDatabase(), ledgerSeq, tx, changes,
index);
}
ltxTx.commit();
}

Expand Down Expand Up @@ -1475,7 +1463,7 @@ LedgerManagerImpl::prefetchTransactionData(
}
else
{
tx->insertKeysForTxApply(classicKeys, {nullptr});
tx->insertKeysForTxApply(classicKeys, nullptr);
}
}
// Prefetch classic and soroban keys separately for greater visibility
Expand Down Expand Up @@ -1582,8 +1570,8 @@ LedgerManagerImpl::applyTransactions(
// Then finally store the results and meta into the txhistory table.
// if we're running in a mode that has one.
//
// Note to future: when we eliminate the txhistory and txfeehistory
// tables, the following step can be removed.
// Note to future: when we eliminate the txhistory for archiving, the
// next step can be removed.
//
// Also note: for historical reasons the history tables number
// txs counting from 1, not 0. We preserve this for the time being
Expand Down
45 changes: 12 additions & 33 deletions src/test/TxTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,15 +463,15 @@ checkLiquidityPool(Application& app, PoolID const& poolID, int64_t reserveA,
REQUIRE(cp.poolSharesTrustLineCount == poolSharesTrustLineCount);
}

TxSetResultMeta
TransactionResultSet
closeLedgerOn(Application& app, uint32 ledgerSeq, int day, int month, int year,
std::vector<TransactionFrameBasePtr> const& txs, bool strictOrder)
{
return closeLedgerOn(app, ledgerSeq, getTestDate(day, month, year), txs,
strictOrder);
}

TxSetResultMeta
TransactionResultSet
closeLedgerOn(Application& app, int day, int month, int year,
std::vector<TransactionFrameBasePtr> const& txs, bool strictOrder)
{
Expand All @@ -480,7 +480,7 @@ closeLedgerOn(Application& app, int day, int month, int year,
strictOrder);
}

TxSetResultMeta
TransactionResultSet
closeLedger(Application& app, std::vector<TransactionFrameBasePtr> const& txs,
bool strictOrder)
{
Expand All @@ -493,7 +493,7 @@ closeLedger(Application& app, std::vector<TransactionFrameBasePtr> const& txs,
return closeLedgerOn(app, nextLedgerSeq, lastCloseTime, txs, strictOrder);
}

TxSetResultMeta
TransactionResultSet
closeLedgerOn(Application& app, uint32 ledgerSeq, TimePoint closeTime,
std::vector<TransactionFrameBasePtr> const& txs, bool strictOrder)
{
Expand Down Expand Up @@ -544,23 +544,11 @@ closeLedgerOn(Application& app, uint32 ledgerSeq, TimePoint closeTime,
}
app.getHerder().externalizeValue(txSet.first, ledgerSeq, closeTime,
emptyUpgradeSteps);
auto z1 = getTransactionHistoryResults(app.getDatabase(), ledgerSeq);
auto z2 = getTransactionFeeMeta(app.getDatabase(), ledgerSeq);

REQUIRE(app.getLedgerManager().getLastClosedLedgerNum() == ledgerSeq);

TxSetResultMeta res;
std::transform(
z1.results.begin(), z1.results.end(), z2.begin(),
std::back_inserter(res),
[](TransactionResultPair const& r1, LedgerEntryChanges const& r2) {
return std::make_pair(r1, r2);
});

return res;
return getTransactionHistoryResults(app.getDatabase(), ledgerSeq);
}

TxSetResultMeta
TransactionResultSet
closeLedger(Application& app, TxSetXDRFrameConstPtr txSet)
{
auto lastCloseTime = app.getLedgerManager()
Expand All @@ -570,27 +558,18 @@ closeLedger(Application& app, TxSetXDRFrameConstPtr txSet)
return closeLedgerOn(app, nextLedgerSeq, lastCloseTime, txSet);
}

TxSetResultMeta
TransactionResultSet
closeLedgerOn(Application& app, uint32 ledgerSeq, time_t closeTime,
TxSetXDRFrameConstPtr txSet)
{
app.getHerder().externalizeValue(txSet, ledgerSeq, closeTime,
emptyUpgradeSteps);

auto z1 = getTransactionHistoryResults(app.getDatabase(), ledgerSeq);
auto z2 = getTransactionFeeMeta(app.getDatabase(), ledgerSeq);

REQUIRE(app.getLedgerManager().getLastClosedLedgerNum() == ledgerSeq);

TxSetResultMeta res;
std::transform(
z1.results.begin(), z1.results.end(), z2.begin(),
std::back_inserter(res),
[](TransactionResultPair const& r1, LedgerEntryChanges const& r2) {
return std::make_pair(r1, r2);
});

return res;
return z1;
}

SecretKey
Expand Down Expand Up @@ -1634,17 +1613,17 @@ getFirstResultCode(TransactionFrame const& tx)
}

void
checkTx(int index, TxSetResultMeta& r, TransactionResultCode expected)
checkTx(int index, TransactionResultSet& r, TransactionResultCode expected)
{
REQUIRE(r[index].first.result.result.code() == expected);
REQUIRE(r.results[index].result.result.code() == expected);
};

void
checkTx(int index, TxSetResultMeta& r, TransactionResultCode expected,
checkTx(int index, TransactionResultSet& r, TransactionResultCode expected,
OperationResultCode code)
{
checkTx(index, r, expected);
REQUIRE(r[index].first.result.result.results()[0].code() == code);
REQUIRE(r.results[index].result.result.results()[0].code() == code);
};

static void
Expand Down
23 changes: 11 additions & 12 deletions src/test/TxTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ class TestAccount;
namespace txtest
{

typedef std::vector<std::pair<TransactionResultPair, LedgerEntryChanges>>
TxSetResultMeta;

struct ExpectedOpResult
{
OperationResult mOperationResult;
Expand Down Expand Up @@ -86,27 +83,28 @@ void checkLiquidityPool(Application& app, PoolID const& poolID,
int64_t totalPoolShares,
int64_t poolSharesTrustLineCount);

TxSetResultMeta
TransactionResultSet
closeLedger(Application& app,
std::vector<TransactionFrameBasePtr> const& txs = {},
bool strictOrder = false);

TxSetResultMeta
TransactionResultSet
closeLedgerOn(Application& app, int day, int month, int year,
std::vector<TransactionFrameBasePtr> const& txs = {},
bool strictOrder = false);

TxSetResultMeta
TransactionResultSet
closeLedgerOn(Application& app, uint32 ledgerSeq, TimePoint closeTime,
std::vector<TransactionFrameBasePtr> const& txs = {},
bool strictOrder = false);

TxSetResultMeta closeLedger(Application& app, TxSetXDRFrameConstPtr txSet);
TransactionResultSet closeLedger(Application& app, TxSetXDRFrameConstPtr txSet);

TxSetResultMeta closeLedgerOn(Application& app, uint32 ledgerSeq,
time_t closeTime, TxSetXDRFrameConstPtr txSet);
TransactionResultSet closeLedgerOn(Application& app, uint32 ledgerSeq,
time_t closeTime,
TxSetXDRFrameConstPtr txSet);

TxSetResultMeta
TransactionResultSet
closeLedgerOn(Application& app, uint32 ledgerSeq, int day, int month, int year,
std::vector<TransactionFrameBasePtr> const& txs = {},
bool strictOrder = false);
Expand Down Expand Up @@ -287,9 +285,10 @@ OperationResult const& getFirstResult(TransactionFrame const& tx);
OperationResultCode getFirstResultCode(TransactionFrame const& tx);

// methods to check results based off meta data
void checkTx(int index, TxSetResultMeta& r, TransactionResultCode expected);
void checkTx(int index, TransactionResultSet& r,
TransactionResultCode expected);

void checkTx(int index, TxSetResultMeta& r, TransactionResultCode expected,
void checkTx(int index, TransactionResultSet& r, TransactionResultCode expected,
OperationResultCode code);

TransactionFrameBasePtr
Expand Down
83 changes: 7 additions & 76 deletions src/transactions/TransactionSQL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,38 +435,6 @@ storeTxSet(Database& db, uint32_t ledgerSeq, TxSetXDRFrame const& txSet)
}
}

void
storeTransactionFee(Database& db, uint32_t ledgerSeq,
TransactionFrameBasePtr const& tx,
LedgerEntryChanges const& changes, uint32_t txIndex)
{
ZoneScoped;
std::string txChanges = decoder::encode_b64(xdr::xdr_to_opaque(changes));

std::string txIDString = binToHex(tx->getContentsHash());

auto prep = db.getPreparedStatement(
"INSERT INTO txfeehistory "
"( txid, ledgerseq, txindex, txchanges) VALUES "
"(:id, :seq, :txindex, :txchanges)");

auto& st = prep.statement();
st.exchange(soci::use(txIDString));
st.exchange(soci::use(ledgerSeq));
st.exchange(soci::use(txIndex));
st.exchange(soci::use(txChanges));
st.define_and_bind();
{
auto timer = db.getInsertTimer("txfeehistory");
st.execute(true);
}

if (st.get_affected_rows() != 1)
{
throw std::runtime_error("Could not update data in SQL");
}
}

TransactionResultSet
getTransactionHistoryResults(Database& db, uint32 ledgerSeq)
{
Expand Down Expand Up @@ -498,35 +466,6 @@ getTransactionHistoryResults(Database& db, uint32 ledgerSeq)
return res;
}

std::vector<LedgerEntryChanges>
getTransactionFeeMeta(Database& db, uint32 ledgerSeq)
{
ZoneScoped;
std::vector<LedgerEntryChanges> res;
std::string changes64;
auto prep =
db.getPreparedStatement("SELECT txchanges FROM txfeehistory "
"WHERE ledgerseq = :lseq ORDER BY txindex ASC");
auto& st = prep.statement();

st.exchange(soci::into(changes64));
st.exchange(soci::use(ledgerSeq));
st.define_and_bind();
st.execute(true);
while (st.got_data())
{
std::vector<uint8_t> changesRaw;
decoder::decode_b64(changes64, changesRaw);

xdr::xdr_get g1(&changesRaw.front(), &changesRaw.back() + 1);
res.emplace_back();
xdr_argpack_archive(g1, res.back());

st.fetch();
}
return res;
}

size_t
copyTransactionsToStream(Application& app, soci::session& sess,
uint32_t ledgerSeq, uint32_t ledgerCount,
Expand Down Expand Up @@ -630,14 +569,19 @@ createTxSetHistoryTable(Database& db)
")";
}

void
deprecateTransactionFeeHistory(Database& db)
{
ZoneScoped;
db.getSession() << "DROP TABLE IF EXISTS txfeehistory";
}

void
dropTransactionHistory(Database& db, Config const& cfg)
{
ZoneScoped;
db.getSession() << "DROP TABLE IF EXISTS txhistory";

db.getSession() << "DROP TABLE IF EXISTS txfeehistory";

// txmeta only supported when BucketListDB is not enabled
std::string txMetaColumn =
cfg.isUsingBucketListDB() ? "" : "txmeta TEXT NOT NULL,";
Expand All @@ -654,15 +598,6 @@ dropTransactionHistory(Database& db, Config const& cfg)

db.getSession() << "CREATE INDEX histbyseq ON txhistory (ledgerseq);";

db.getSession() << "CREATE TABLE txfeehistory ("
"txid CHARACTER(64) NOT NULL,"
"ledgerseq INT NOT NULL CHECK (ledgerseq >= 0),"
"txindex INT NOT NULL,"
"txchanges TEXT NOT NULL,"
"PRIMARY KEY (ledgerseq, txindex)"
")";
db.getSession() << "CREATE INDEX histfeebyseq ON txfeehistory (ledgerseq);";

createTxSetHistoryTable(db);
}

Expand All @@ -675,8 +610,6 @@ deleteOldTransactionHistoryEntries(Database& db, uint32_t ledgerSeq,
"txhistory", "ledgerseq");
DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count,
"txsethistory", "ledgerseq");
DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count,
"txfeehistory", "ledgerseq");
}

void
Expand All @@ -687,8 +620,6 @@ deleteNewerTransactionHistoryEntries(Database& db, uint32_t ledgerSeq)
"txhistory", "ledgerseq");
DatabaseUtils::deleteNewerEntriesHelper(db.getSession(), ledgerSeq,
"txsethistory", "ledgerseq");
DatabaseUtils::deleteNewerEntriesHelper(db.getSession(), ledgerSeq,
"txfeehistory", "ledgerseq");
}

}
9 changes: 2 additions & 7 deletions src/transactions/TransactionSQL.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,18 @@ void storeTransaction(Database& db, uint32_t ledgerSeq,

void storeTxSet(Database& db, uint32_t ledgerSeq, TxSetXDRFrame const& txSet);

void storeTransactionFee(Database& db, uint32_t ledgerSeq,
TransactionFrameBasePtr const& tx,
LedgerEntryChanges const& changes, uint32_t txIndex);

TransactionResultSet getTransactionHistoryResults(Database& db,
uint32 ledgerSeq);

std::vector<LedgerEntryChanges> getTransactionFeeMeta(Database& db,
uint32 ledgerSeq);

size_t copyTransactionsToStream(Application& app, soci::session& sess,
uint32_t ledgerSeq, uint32_t ledgerCount,
XDROutputFileStream& txOut,
XDROutputFileStream& txResultOut);

void createTxSetHistoryTable(Database& db);

void deprecateTransactionFeeHistory(Database& db);

void dropTransactionHistory(Database& db, Config const& cfg);

void deleteOldTransactionHistoryEntries(Database& db, uint32_t ledgerSeq,
Expand Down
Loading
Loading