Skip to content

Commit 032f003

Browse files
use fresh local test txs pool for each test
1 parent 9f9abbb commit 032f003

File tree

1 file changed

+62
-55
lines changed

1 file changed

+62
-55
lines changed

test/pending_txs_pool.cpp

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -72,69 +72,69 @@ class TestPendingTxsPool : public PendingTxsPool
7272

7373
return add(transaction);
7474
}
75-
};
7675

77-
TestPendingTxsPool pendingTxsPool;
76+
unsigned int addTickTransactions(unsigned int tick, unsigned long long seed, unsigned int maxTransactions)
77+
{
78+
// use pseudo-random sequence
79+
std::mt19937_64 gen64(seed);
7880

79-
unsigned int addTickTransactions(unsigned int tick, unsigned long long seed, unsigned int maxTransactions)
80-
{
81-
// use pseudo-random sequence
82-
std::mt19937_64 gen64(seed);
81+
unsigned int numTransactionsAdded = 0;
8382

84-
unsigned int numTransactionsAdded = 0;
83+
// add transactions of tick
84+
unsigned int transactionNum = gen64() % (maxTransactions + 1);
85+
for (unsigned int transaction = 0; transaction < transactionNum; ++transaction)
86+
{
87+
unsigned int inputSize = gen64() % MAX_INPUT_SIZE;
88+
long long amount = gen64() % MAX_AMOUNT;
89+
m256i srcPublicKey = m256i{ 0, 0, 0, (gen64() % NUM_INITIALIZED_ENTITIES) + 1 };
90+
if (addTransaction(tick, amount, inputSize, /*dest=*/nullptr, &srcPublicKey))
91+
numTransactionsAdded++;
92+
}
93+
checkStateConsistencyWithAssert();
8594

86-
// add transactions of tick
87-
unsigned int transactionNum = gen64() % (maxTransactions + 1);
88-
for (unsigned int transaction = 0; transaction < transactionNum; ++transaction)
89-
{
90-
unsigned int inputSize = gen64() % MAX_INPUT_SIZE;
91-
long long amount = gen64() % MAX_AMOUNT;
92-
m256i srcPublicKey = m256i{ 0, 0, 0, (gen64() % NUM_INITIALIZED_ENTITIES) + 1 };
93-
if (pendingTxsPool.addTransaction(tick, amount, inputSize, /*dest=*/nullptr, &srcPublicKey))
94-
numTransactionsAdded++;
95+
return numTransactionsAdded;
9596
}
96-
pendingTxsPool.checkStateConsistencyWithAssert();
97-
98-
return numTransactionsAdded;
99-
}
10097

101-
void checkTickTransactions(unsigned int tick, unsigned long long seed, unsigned int maxTransactions)
102-
{
103-
// use pseudo-random sequence
104-
std::mt19937_64 gen64(seed);
98+
void checkTickTransactions(unsigned int tick, unsigned long long seed, unsigned int maxTransactions)
99+
{
100+
// use pseudo-random sequence
101+
std::mt19937_64 gen64(seed);
105102

106-
// check transactions of tick
107-
unsigned int transactionNum = gen64() % (maxTransactions + 1);
103+
// check transactions of tick
104+
unsigned int transactionNum = gen64() % (maxTransactions + 1);
108105

109-
for (unsigned int transaction = 0; transaction < transactionNum; ++transaction)
110-
{
111-
unsigned int expectedInputSize = gen64() % MAX_INPUT_SIZE;
112-
long long expectedAmount = gen64() % MAX_AMOUNT;
113-
m256i expectedSrcPublicKey = m256i{ 0, 0, 0, (gen64() % NUM_INITIALIZED_ENTITIES) + 1 };
106+
for (unsigned int transaction = 0; transaction < transactionNum; ++transaction)
107+
{
108+
unsigned int expectedInputSize = gen64() % MAX_INPUT_SIZE;
109+
long long expectedAmount = gen64() % MAX_AMOUNT;
110+
m256i expectedSrcPublicKey = m256i{ 0, 0, 0, (gen64() % NUM_INITIALIZED_ENTITIES) + 1 };
114111

115-
Transaction* tp = pendingTxsPool.getTx(tick, transaction);
112+
Transaction* tp = getTx(tick, transaction);
116113

117-
ASSERT_NE(tp, nullptr);
114+
ASSERT_NE(tp, nullptr);
118115

119-
EXPECT_TRUE(tp->checkValidity());
120-
EXPECT_EQ(tp->tick, tick);
121-
EXPECT_EQ(static_cast<unsigned int>(tp->inputSize), expectedInputSize);
122-
EXPECT_EQ(tp->amount, expectedAmount);
123-
EXPECT_TRUE(tp->sourcePublicKey == expectedSrcPublicKey);
116+
EXPECT_TRUE(tp->checkValidity());
117+
EXPECT_EQ(tp->tick, tick);
118+
EXPECT_EQ(static_cast<unsigned int>(tp->inputSize), expectedInputSize);
119+
EXPECT_EQ(tp->amount, expectedAmount);
120+
EXPECT_TRUE(tp->sourcePublicKey == expectedSrcPublicKey);
124121

125-
m256i* digest = pendingTxsPool.getDigest(tick, transaction);
122+
m256i* digest = getDigest(tick, transaction);
126123

127-
ASSERT_NE(digest, nullptr);
124+
ASSERT_NE(digest, nullptr);
128125

129-
m256i tpDigest;
130-
KangarooTwelve(tp, tp->totalSize(), &tpDigest, 32);
131-
EXPECT_EQ(*digest, tpDigest);
126+
m256i tpDigest;
127+
KangarooTwelve(tp, tp->totalSize(), &tpDigest, 32);
128+
EXPECT_EQ(*digest, tpDigest);
129+
}
132130
}
133-
}
131+
};
134132

135133

136134
TEST(TestPendingTxsPool, EpochTransition)
137135
{
136+
TestPendingTxsPool pendingTxsPool;
137+
138138
unsigned long long seed = 42;
139139

140140
// use pseudo-random sequence
@@ -176,11 +176,11 @@ TEST(TestPendingTxsPool, EpochTransition)
176176

177177
// add ticks transactions
178178
for (int i = 0; i < firstEpochTicks; ++i)
179-
numAdded = addTickTransactions(firstEpochTick0 + i, firstEpochSeeds[i], maxTransactions);
179+
numAdded = pendingTxsPool.addTickTransactions(firstEpochTick0 + i, firstEpochSeeds[i], maxTransactions);
180180

181181
// check ticks transactions
182182
for (int i = 0; i < firstEpochTicks; ++i)
183-
checkTickTransactions(firstEpochTick0 + i, firstEpochSeeds[i], maxTransactions);
183+
pendingTxsPool.checkTickTransactions(firstEpochTick0 + i, firstEpochSeeds[i], maxTransactions);
184184

185185
pendingTxsPool.checkStateConsistencyWithAssert();
186186

@@ -192,14 +192,14 @@ TEST(TestPendingTxsPool, EpochTransition)
192192

193193
// add ticks transactions
194194
for (int i = 0; i < secondEpochTicks; ++i)
195-
numAdded = addTickTransactions(secondEpochTick0 + i, secondEpochSeeds[i], maxTransactions);
195+
numAdded = pendingTxsPool.addTickTransactions(secondEpochTick0 + i, secondEpochSeeds[i], maxTransactions);
196196

197197
// check ticks transactions
198198
for (int i = 0; i < secondEpochTicks; ++i)
199-
checkTickTransactions(secondEpochTick0 + i, secondEpochSeeds[i], maxTransactions);
199+
pendingTxsPool.checkTickTransactions(secondEpochTick0 + i, secondEpochSeeds[i], maxTransactions);
200200

201201
// add a transaction for the next epoch
202-
numAdded = addTickTransactions(thirdEpochTick0 + 1, thirdEpochSeeds[1], maxTransactions);
202+
numAdded = pendingTxsPool.addTickTransactions(thirdEpochTick0 + 1, thirdEpochSeeds[1], maxTransactions);
203203

204204
pendingTxsPool.checkStateConsistencyWithAssert();
205205

@@ -211,14 +211,14 @@ TEST(TestPendingTxsPool, EpochTransition)
211211

212212
// add ticks transactions
213213
for (int i = 2; i < thirdEpochTicks; ++i)
214-
numAdded = addTickTransactions(thirdEpochTick0 + i, thirdEpochSeeds[i], maxTransactions);
214+
numAdded = pendingTxsPool.addTickTransactions(thirdEpochTick0 + i, thirdEpochSeeds[i], maxTransactions);
215215

216216
// check ticks transactions
217217
for (int i = 1; i < thirdEpochTicks; ++i)
218-
checkTickTransactions(thirdEpochTick0 + i, thirdEpochSeeds[i], maxTransactions);
218+
pendingTxsPool.checkTickTransactions(thirdEpochTick0 + i, thirdEpochSeeds[i], maxTransactions);
219219

220220
// add a transaction for the next epoch
221-
numAdded = addTickTransactions(fourthEpochTick0 + 1, /*seed=*/42, maxTransactions);
221+
numAdded = pendingTxsPool.addTickTransactions(fourthEpochTick0 + 1, /*seed=*/42, maxTransactions);
222222

223223
pendingTxsPool.checkStateConsistencyWithAssert();
224224

@@ -234,6 +234,7 @@ TEST(TestPendingTxsPool, EpochTransition)
234234

235235
TEST(TestPendingTxsPool, TotalNumberOfPendingTxs)
236236
{
237+
TestPendingTxsPool pendingTxsPool;
237238
unsigned long long seed = 1337;
238239

239240
// use pseudo-random sequence
@@ -262,7 +263,7 @@ TEST(TestPendingTxsPool, TotalNumberOfPendingTxs)
262263
std::vector<unsigned short> numPendingTransactions(firstEpochTicks, 0);
263264
for (int i = firstEpochTicks - 1; i >= 0; --i)
264265
{
265-
numTransactionsAdded[i] = addTickTransactions(firstEpochTick0 + i, firstEpochSeeds[i], maxTransactions);
266+
numTransactionsAdded[i] = pendingTxsPool.addTickTransactions(firstEpochTick0 + i, firstEpochSeeds[i], maxTransactions);
266267
if (i > 0)
267268
{
268269
numPendingTransactions[i - 1] = numPendingTransactions[i] + numTransactionsAdded[i];
@@ -281,6 +282,7 @@ TEST(TestPendingTxsPool, TotalNumberOfPendingTxs)
281282

282283
TEST(TestPendingTxsPool, NumberOfPendingTickTxs)
283284
{
285+
TestPendingTxsPool pendingTxsPool;
284286
unsigned long long seed = 67534;
285287

286288
// use pseudo-random sequence
@@ -308,7 +310,7 @@ TEST(TestPendingTxsPool, NumberOfPendingTickTxs)
308310
std::vector<unsigned short> numTransactionsAdded(firstEpochTicks);
309311
for (int i = firstEpochTicks - 1; i >= 0; --i)
310312
{
311-
numTransactionsAdded[i] = addTickTransactions(firstEpochTick0 + i, firstEpochSeeds[i], maxTransactions);
313+
numTransactionsAdded[i] = pendingTxsPool.addTickTransactions(firstEpochTick0 + i, firstEpochSeeds[i], maxTransactions);
312314
}
313315

314316
EXPECT_EQ(pendingTxsPool.getNumberOfPendingTickTxs(firstEpochTick0 - 1), 0);
@@ -323,6 +325,7 @@ TEST(TestPendingTxsPool, NumberOfPendingTickTxs)
323325

324326
TEST(TestPendingTxsPool, IncrementFirstStoredTick)
325327
{
328+
TestPendingTxsPool pendingTxsPool;
326329
unsigned long long seed = 84129;
327330

328331
// use pseudo-random sequence
@@ -351,7 +354,7 @@ TEST(TestPendingTxsPool, IncrementFirstStoredTick)
351354
std::vector<unsigned short> numPendingTransactions(firstEpochTicks, 0);
352355
for (int i = firstEpochTicks - 1; i >= 0; --i)
353356
{
354-
numTransactionsAdded[i] = addTickTransactions(firstEpochTick0 + i, firstEpochSeeds[i], maxTransactions);
357+
numTransactionsAdded[i] = pendingTxsPool.addTickTransactions(firstEpochTick0 + i, firstEpochSeeds[i], maxTransactions);
355358
if (i > 0)
356359
{
357360
numPendingTransactions[i - 1] = numPendingTransactions[i] + numTransactionsAdded[i];
@@ -376,6 +379,7 @@ TEST(TestPendingTxsPool, IncrementFirstStoredTick)
376379

377380
TEST(TestPendingTxsPool, TxsPrioritizationMoreThanMaxTxs)
378381
{
382+
TestPendingTxsPool pendingTxsPool;
379383
unsigned long long seed = 9532;
380384

381385
// use pseudo-random sequence
@@ -418,6 +422,7 @@ TEST(TestPendingTxsPool, TxsPrioritizationMoreThanMaxTxs)
418422

419423
TEST(TestPendingTxsPool, TxsPrioritizationDuplicateTxs)
420424
{
425+
TestPendingTxsPool pendingTxsPool;
421426
unsigned long long seed = 9532;
422427

423428
// use pseudo-random sequence
@@ -457,6 +462,7 @@ TEST(TestPendingTxsPool, TxsPrioritizationDuplicateTxs)
457462

458463
TEST(TestPendingTxsPool, ProtocolLevelTxsMaxPriority)
459464
{
465+
TestPendingTxsPool pendingTxsPool;
460466
unsigned long long seed = 9532;
461467

462468
// use pseudo-random sequence
@@ -499,6 +505,7 @@ TEST(TestPendingTxsPool, ProtocolLevelTxsMaxPriority)
499505

500506
TEST(TestPendingTxsPool, TxsWithSrcBalance0AreRejected)
501507
{
508+
TestPendingTxsPool pendingTxsPool;
502509
unsigned long long seed = 3452;
503510

504511
// use pseudo-random sequence

0 commit comments

Comments
 (0)