Skip to content

Commit a8139ed

Browse files
committed
Check envelope types in test + remove outer db txn
1 parent d457d0e commit a8139ed

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

src/herder/HerderImpl.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,6 @@ HerderImpl::processExternalized(uint64 slotIndex, StellarValue const& value,
308308
if (mApp.getConfig().MODE_STORES_HISTORY_MISC)
309309
{
310310
ZoneNamedN(updateSCPHistoryZone, "update SCP history", true);
311-
312-
// NOTE: Consolidating the two saveSCPHistory calls into one transaction
313-
// provides modest performance increases for sqlite and insignificant
314-
// performance increases for postgres.
315-
soci::transaction txScope(mApp.getDatabase().getSession());
316311
if (slotIndex != 0)
317312
{
318313
// Save any new SCP messages received about the previous ledger.
@@ -328,7 +323,6 @@ HerderImpl::processExternalized(uint64 slotIndex, StellarValue const& value,
328323
static_cast<uint32>(slotIndex),
329324
getSCP().getExternalizingState(slotIndex),
330325
mPendingEnvelopes.getCurrentlyTrackedQuorum());
331-
txScope.commit();
332326
}
333327

334328
// reflect upgrades with the ones included in this SCP round

src/herder/test/HerderTests.cpp

+49-27
Original file line numberDiff line numberDiff line change
@@ -5848,27 +5848,46 @@ TEST_CASE("SCP message capture from previous ledger", "[herder]")
58485848
},
58495849
2 * Herder::EXP_LEDGER_TIMESPAN_SECONDS, false);
58505850

5851-
// Return the number of entries in a node's scphistory table for the given
5852-
// ledger number
5853-
auto getNumSCPHistoryEntries = [&](Application::pointer node,
5854-
uint32_t ledgerNum) {
5855-
auto& db = node->getDatabase();
5856-
auto prep = db.getPreparedStatement(
5857-
"SELECT COUNT(*) FROM scphistory WHERE ledgerseq = :l");
5858-
auto& st = prep.statement();
5859-
st.exchange(soci::use(ledgerNum));
5860-
uint32_t count;
5861-
st.exchange(soci::into(count));
5862-
st.define_and_bind();
5863-
st.execute(true);
5864-
return count;
5865-
};
5851+
// Check that a node's scphistory table for a given ledger has the correct
5852+
// number of entries of each type in `expectedTypes`
5853+
auto checkSCPHistoryEntries =
5854+
[&](Application::pointer node, uint32_t ledgerNum,
5855+
UnorderedMap<SCPStatementType, size_t> const& expectedTypes) {
5856+
// Prepare query
5857+
auto& db = node->getDatabase();
5858+
auto prep = db.getPreparedStatement(
5859+
"SELECT envelope FROM scphistory WHERE ledgerseq = :l");
5860+
auto& st = prep.statement();
5861+
st.exchange(soci::use(ledgerNum));
5862+
std::string envStr;
5863+
st.exchange(soci::into(envStr));
5864+
st.define_and_bind();
5865+
st.execute(false);
5866+
5867+
// Count the number of entries of each type
5868+
UnorderedMap<SCPStatementType, size_t> actualTypes;
5869+
while (st.fetch())
5870+
{
5871+
Value v;
5872+
decoder::decode_b64(envStr, v);
5873+
SCPEnvelope env;
5874+
xdr::xdr_from_opaque(v, env);
5875+
++actualTypes[env.statement.pledges.type()];
5876+
}
5877+
5878+
REQUIRE(actualTypes == expectedTypes);
5879+
};
5880+
5881+
// A has 1 CONFIRM and 1 EXTERNALIZE in its scphistory table for ledger 2.
5882+
checkSCPHistoryEntries(A, 2,
5883+
{{SCPStatementType::SCP_ST_CONFIRM, 1},
5884+
{SCPStatementType::SCP_ST_EXTERNALIZE, 1}});
5885+
5886+
// B has 2 EXTERNALIZEs in its scphistory table for ledger 2.
5887+
checkSCPHistoryEntries(B, 2, {{SCPStatementType::SCP_ST_EXTERNALIZE, 2}});
58665888

5867-
// A and B should have 2 entries in their scphistory table for ledger 2. C
5868-
// should have 0.
5869-
REQUIRE(getNumSCPHistoryEntries(A, 2) == 2);
5870-
REQUIRE(getNumSCPHistoryEntries(B, 2) == 2);
5871-
REQUIRE(getNumSCPHistoryEntries(C, 2) == 0);
5889+
// C has no entries in its scphistory table for ledger 2.
5890+
checkSCPHistoryEntries(C, 2, {});
58725891

58735892
// Get messages from A and B
58745893
HerderImpl& herderA = dynamic_cast<HerderImpl&>(A->getHerder());
@@ -5910,9 +5929,12 @@ TEST_CASE("SCP message capture from previous ledger", "[herder]")
59105929
},
59115930
2 * Herder::EXP_LEDGER_TIMESPAN_SECONDS, false);
59125931

5913-
// A and B should now have 3 entries in their scphistory table for ledger 2.
5914-
REQUIRE(getNumSCPHistoryEntries(A, 2) == 3);
5915-
REQUIRE(getNumSCPHistoryEntries(B, 2) == 3);
5932+
// A and B should now each have 3 EXTERNALIZEs in their scphistory table for
5933+
// ledger 2. A's CONFIRM entry has been replaced with an EXTERNALIZE.
5934+
UnorderedMap<SCPStatementType, size_t> const expectedTypes = {
5935+
{SCPStatementType::SCP_ST_EXTERNALIZE, 3}};
5936+
checkSCPHistoryEntries(A, 2, expectedTypes);
5937+
checkSCPHistoryEntries(B, 2, expectedTypes);
59165938

59175939
// Connect C to B and crank C to catch up with A and B
59185940
simulation->addConnection(validatorCKey.getPublicKey(),
@@ -5921,8 +5943,8 @@ TEST_CASE("SCP message capture from previous ledger", "[herder]")
59215943
[&]() { return C->getLedgerManager().getLastClosedLedgerNum() == 3; },
59225944
2 * Herder::EXP_LEDGER_TIMESPAN_SECONDS, false);
59235945

5924-
// C should have 3 entries in its scphistory table for ledger 2. This check
5925-
// ensures that C does not double count messages from ledger 2 when closing
5926-
// ledger 3.
5927-
REQUIRE(getNumSCPHistoryEntries(C, 2) == 3);
5946+
// C should have 3 EXTERNALIZEs in its scphistory table for ledger 2. This
5947+
// check ensures that C does not double count messages from ledger 2 when
5948+
// closing ledger 3.
5949+
checkSCPHistoryEntries(C, 2, expectedTypes);
59285950
}

0 commit comments

Comments
 (0)