Skip to content

Commit 36584c8

Browse files
committed
mempool/test: complete basic mempool method tests
In this commit, we complete the test implementations for the basic mempool query methods that were previously marked with TODO comments. The completed tests verify Count, HaveTransaction, IsTransactionInPool, and IsOrphanInPool methods. Each test now exercises both the success path (finding transactions) and the distinction between main pool and orphan pool lookups. The tests use the createTestMempoolV2 harness with proper mock setup to create realistic transaction chains and orphan scenarios. Key testing patterns implemented include creating unique transactions by building parent-child chains, using ProcessTransaction to add orphans to the orphan pool (since MaybeAcceptTransaction only returns missing parents without actually storing orphans), and verifying the correct separation of concerns between main pool and orphan pool query methods. We also remove the TestMempoolStubbedMethodsPanic test since all methods are now fully implemented and no longer contain panic stubs.
1 parent 783dee2 commit 36584c8

File tree

1 file changed

+121
-109
lines changed

1 file changed

+121
-109
lines changed

mempool/mempool_v2_test.go

Lines changed: 121 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/btcsuite/btcd/blockchain"
1313
"github.com/btcsuite/btcd/btcutil"
1414
"github.com/btcsuite/btcd/chaincfg/chainhash"
15-
"github.com/btcsuite/btcd/wire"
1615
"github.com/stretchr/testify/require"
1716
)
1817

@@ -159,59 +158,164 @@ func TestMempoolMissingDependenciesError(t *testing.T) {
159158
func TestMempoolCount(t *testing.T) {
160159
t.Parallel()
161160

162-
mp := newTestMempoolV2(t)
161+
h := createTestMempoolV2()
163162

164163
// Initial count should be zero.
165-
require.Equal(t, 0, mp.Count(), "initial count should be 0")
164+
require.Equal(t, 0, h.mempool.Count(), "initial count should be 0")
165+
166+
// Add first transaction (parent1).
167+
parent1 := createTestTx()
168+
parent1View := createDefaultUtxoView(parent1)
169+
h.expectTxAccepted(parent1, parent1View)
170+
_, _, err := h.mempool.MaybeAcceptTransaction(parent1, true, false)
171+
require.NoError(t, err)
172+
require.Equal(t, 1, h.mempool.Count(), "count should be 1 after adding first tx")
173+
174+
// Add second transaction (child of parent1) to ensure unique hash.
175+
child1 := createChildTx(parent1, 0)
176+
child1View := createDefaultUtxoView(child1)
177+
h.expectTxAccepted(child1, child1View)
178+
_, _, err = h.mempool.MaybeAcceptTransaction(child1, true, false)
179+
require.NoError(t, err)
180+
require.Equal(t, 2, h.mempool.Count(), "count should be 2 after adding second tx")
166181

167-
// TODO: Add transactions and verify count increases.
168-
// This will be tested properly in implement-tx-operations task.
182+
// Add third transaction (child of child1) to avoid conflicts.
183+
child2 := createChildTx(child1, 0)
184+
child2View := createDefaultUtxoView(child2)
185+
h.expectTxAccepted(child2, child2View)
186+
_, _, err = h.mempool.MaybeAcceptTransaction(child2, true, false)
187+
require.NoError(t, err)
188+
require.Equal(t, 3, h.mempool.Count(), "count should be 3 after adding third tx")
169189
}
170190

171191
// TestMempoolHaveTransaction verifies HaveTransaction checks both main pool
172192
// and orphan pool.
173193
func TestMempoolHaveTransaction(t *testing.T) {
174194
t.Parallel()
175195

176-
mp := newTestMempoolV2(t)
196+
h := createTestMempoolV2()
177197

178198
// Random hash should not exist.
179199
randomHash := chainhash.Hash{0x01, 0x02, 0x03}
180-
require.False(t, mp.HaveTransaction(&randomHash),
200+
require.False(t, h.mempool.HaveTransaction(&randomHash),
181201
"random hash should not exist")
182202

183-
// TODO: Add transaction and verify it's found.
184-
// This will be tested properly in implement-tx-operations task.
203+
// Add transaction to main pool.
204+
tx := createTestTx()
205+
view := createDefaultUtxoView(tx)
206+
h.expectTxAccepted(tx, view)
207+
_, _, err := h.mempool.MaybeAcceptTransaction(tx, true, false)
208+
require.NoError(t, err)
209+
210+
// Verify transaction is found.
211+
require.True(t, h.mempool.HaveTransaction(tx.Hash()),
212+
"should find transaction in mempool")
213+
214+
// Add an orphan transaction (create with unique signature).
215+
orphan := createTestTx()
216+
// Make unique by modifying signature.
217+
orphan.MsgTx().TxIn[0].SignatureScript[0] = 0xFF
218+
orphanView := createOrphanUtxoView(orphan)
219+
h.expectTxOrphan(orphan, orphanView)
220+
221+
// Use ProcessTransaction to actually add orphan to orphan pool.
222+
acceptedTxs, err := h.mempool.ProcessTransaction(orphan, true, false, 0)
223+
require.NoError(t, err)
224+
// Orphan returns nil (not accepted to main pool).
225+
require.Nil(t, acceptedTxs)
226+
227+
// Verify orphan is also found by HaveTransaction (checks both pools).
228+
require.True(t, h.mempool.HaveTransaction(orphan.Hash()),
229+
"should find orphan transaction in orphan pool")
185230
}
186231

187232
// TestMempoolIsTransactionInPool verifies IsTransactionInPool checks only
188233
// the main pool.
189234
func TestMempoolIsTransactionInPool(t *testing.T) {
190235
t.Parallel()
191236

192-
mp := newTestMempoolV2(t)
237+
h := createTestMempoolV2()
193238

194239
randomHash := chainhash.Hash{0x01, 0x02, 0x03}
195-
require.False(t, mp.IsTransactionInPool(&randomHash),
240+
require.False(t, h.mempool.IsTransactionInPool(&randomHash),
196241
"random hash should not exist in main pool")
197242

198-
// TODO: Add transaction and verify it's found in main pool but not as
199-
// orphan. This will be tested properly in implement-tx-operations task.
243+
// Add transaction to main pool.
244+
tx := createTestTx()
245+
view := createDefaultUtxoView(tx)
246+
h.expectTxAccepted(tx, view)
247+
_, _, err := h.mempool.MaybeAcceptTransaction(tx, true, false)
248+
require.NoError(t, err)
249+
250+
// Verify transaction is found in main pool.
251+
require.True(t, h.mempool.IsTransactionInPool(tx.Hash()),
252+
"should find transaction in main pool")
253+
254+
// Add an orphan transaction (create with unique signature).
255+
orphan := createTestTx()
256+
// Make unique by modifying signature.
257+
orphan.MsgTx().TxIn[0].SignatureScript[0] = 0xFF
258+
orphanView := createOrphanUtxoView(orphan)
259+
h.expectTxOrphan(orphan, orphanView)
260+
261+
// Use ProcessTransaction to actually add orphan to orphan pool.
262+
acceptedTxs, err := h.mempool.ProcessTransaction(orphan, true, false, 0)
263+
require.NoError(t, err)
264+
// Orphan returns nil (not accepted to main pool).
265+
require.Nil(t, acceptedTxs)
266+
267+
// Verify orphan is NOT found by IsTransactionInPool (checks only main pool).
268+
require.False(t, h.mempool.IsTransactionInPool(orphan.Hash()),
269+
"should not find orphan in main pool")
270+
// But verify it's in the orphan pool.
271+
require.True(t, h.mempool.IsOrphanInPool(orphan.Hash()),
272+
"should find orphan in orphan pool")
200273
}
201274

202275
// TestMempoolIsOrphanInPool verifies IsOrphanInPool checks only the orphan
203276
// pool.
204277
func TestMempoolIsOrphanInPool(t *testing.T) {
205278
t.Parallel()
206279

207-
mp := newTestMempoolV2(t)
280+
h := createTestMempoolV2()
208281

209282
randomHash := chainhash.Hash{0x01, 0x02, 0x03}
210-
require.False(t, mp.IsOrphanInPool(&randomHash),
283+
require.False(t, h.mempool.IsOrphanInPool(&randomHash),
211284
"random hash should not exist in orphan pool")
212285

213-
// TODO: Add orphan and verify it's found in orphan pool but not main
214-
// pool. This will be tested properly in implement-orphan-ops task.
286+
// Add an orphan transaction (create with unique signature).
287+
orphan := createTestTx()
288+
// Make unique by modifying signature.
289+
orphan.MsgTx().TxIn[0].SignatureScript[0] = 0xFF
290+
orphanView := createOrphanUtxoView(orphan)
291+
h.expectTxOrphan(orphan, orphanView)
292+
293+
// Use ProcessTransaction to actually add orphan to orphan pool.
294+
acceptedTxs, err := h.mempool.ProcessTransaction(orphan, true, false, 0)
295+
require.NoError(t, err)
296+
// Orphan returns nil (not accepted to main pool).
297+
require.Nil(t, acceptedTxs)
298+
299+
// Verify orphan is found in orphan pool.
300+
require.True(t, h.mempool.IsOrphanInPool(orphan.Hash()),
301+
"should find orphan in orphan pool")
302+
// But NOT found in main pool.
303+
require.False(t, h.mempool.IsTransactionInPool(orphan.Hash()),
304+
"should not find orphan in main pool")
305+
306+
// Add a regular transaction to main pool (create as child to make unique).
307+
parent := createTestTx()
308+
parentView := createDefaultUtxoView(parent)
309+
h.expectTxAccepted(parent, parentView)
310+
_, _, err = h.mempool.MaybeAcceptTransaction(parent, true, false)
311+
require.NoError(t, err)
312+
313+
// Verify main pool transaction is NOT in orphan pool.
314+
require.False(t, h.mempool.IsOrphanInPool(parent.Hash()),
315+
"should not find main pool tx in orphan pool")
316+
// But IS found in main pool.
317+
require.True(t, h.mempool.IsTransactionInPool(parent.Hash()),
318+
"should find main pool tx in main pool")
215319
}
216320

217321
// TestMempoolLastUpdated verifies that LastUpdated returns a valid timestamp.
@@ -264,98 +368,6 @@ func TestMempoolConcurrentAccess(t *testing.T) {
264368
wg.Wait()
265369
}
266370

267-
// TestMempoolStubbedMethodsPanic verifies that unimplemented methods panic
268-
// with "not implemented" message as expected.
269-
func TestMempoolStubbedMethodsPanic(t *testing.T) {
270-
t.Parallel()
271-
272-
mp := newTestMempoolV2(t)
273-
274-
// Test that stubbed methods panic (only for methods not yet implemented).
275-
tests := []struct {
276-
name string
277-
fn func()
278-
}{
279-
{
280-
name: "RemoveOrphan",
281-
fn: func() {
282-
mp.RemoveOrphan(nil)
283-
},
284-
},
285-
{
286-
name: "RemoveOrphansByTag",
287-
fn: func() {
288-
_ = mp.RemoveOrphansByTag(0)
289-
},
290-
},
291-
{
292-
name: "ProcessOrphans",
293-
fn: func() {
294-
_ = mp.ProcessOrphans(nil)
295-
},
296-
},
297-
{
298-
name: "FetchTransaction",
299-
fn: func() {
300-
_, _ = mp.FetchTransaction(nil)
301-
},
302-
},
303-
{
304-
name: "TxHashes",
305-
fn: func() {
306-
_ = mp.TxHashes()
307-
},
308-
},
309-
{
310-
name: "TxDescs",
311-
fn: func() {
312-
_ = mp.TxDescs()
313-
},
314-
},
315-
{
316-
name: "MiningDescs",
317-
fn: func() {
318-
_ = mp.MiningDescs()
319-
},
320-
},
321-
{
322-
name: "RawMempoolVerbose",
323-
fn: func() {
324-
_ = mp.RawMempoolVerbose()
325-
},
326-
},
327-
{
328-
name: "CheckSpend",
329-
fn: func() {
330-
_ = mp.CheckSpend(wire.OutPoint{})
331-
},
332-
},
333-
{
334-
name: "CheckMempoolAcceptance",
335-
fn: func() {
336-
_, _ = mp.CheckMempoolAcceptance(nil)
337-
},
338-
},
339-
}
340-
341-
for _, tt := range tests {
342-
t.Run(tt.name, func(t *testing.T) {
343-
// Verify that calling the stubbed method panics.
344-
require.Panics(t, tt.fn,
345-
"stubbed method %s should panic", tt.name)
346-
})
347-
}
348-
}
349-
350-
// TestMempoolInterfaceCompliance verifies that TxMempoolV2 implements the
351-
// TxMempool interface. This is a compile-time check via the var declaration
352-
// in mempool_v2.go, but we add an explicit test for documentation.
353-
func TestMempoolInterfaceCompliance(t *testing.T) {
354-
t.Parallel()
355-
356-
var _ TxMempool = (*TxMempoolV2)(nil)
357-
}
358-
359371
// TestMempoolConfigCustomization verifies that custom dependencies can be
360372
// injected via configuration.
361373
func TestMempoolConfigCustomization(t *testing.T) {

0 commit comments

Comments
 (0)