|
| 1 | +package sweepbatcher |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/btcsuite/btcd/chaincfg" |
| 10 | + "github.com/btcsuite/btcd/wire" |
| 11 | + "github.com/lightninglabs/loop/loopdb" |
| 12 | + "github.com/lightninglabs/loop/loopdb/sqlc" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +// TestFetchUnconfirmedSweepBatchesRejectsInvalidBatchTxID verifies that |
| 17 | +// malformed persisted batch txids are rejected during batch loading. |
| 18 | +func TestFetchUnconfirmedSweepBatchesRejectsInvalidBatchTxID(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + tests := []struct { |
| 22 | + name string |
| 23 | + txid string |
| 24 | + errMsg string |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "short", |
| 28 | + txid: "abcd", |
| 29 | + errMsg: "invalid batch txid", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "non-hex", |
| 33 | + txid: strings.Repeat("z", 64), |
| 34 | + errMsg: "invalid batch txid", |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + for _, test := range tests { |
| 39 | + t.Run(test.name, func(t *testing.T) { |
| 40 | + t.Parallel() |
| 41 | + |
| 42 | + ctxb := t.Context() |
| 43 | + testDb := loopdb.NewTestDB(t) |
| 44 | + defer testDb.Close() |
| 45 | + |
| 46 | + store := NewSQLStore( |
| 47 | + loopdb.NewTypedStore[Querier](testDb), |
| 48 | + &chaincfg.RegressionNetParams, |
| 49 | + ) |
| 50 | + |
| 51 | + _, err := testDb.Queries.InsertBatch( |
| 52 | + ctxb, sqlc.InsertBatchParams{ |
| 53 | + BatchTxID: sql.NullString{ |
| 54 | + String: test.txid, |
| 55 | + Valid: true, |
| 56 | + }, |
| 57 | + BatchPkScript: []byte{0x00}, |
| 58 | + LastRbfHeight: sql.NullInt32{ |
| 59 | + Int32: 1, |
| 60 | + Valid: true, |
| 61 | + }, |
| 62 | + LastRbfSatPerKw: sql.NullInt32{ |
| 63 | + Int32: 1000, |
| 64 | + Valid: true, |
| 65 | + }, |
| 66 | + MaxTimeoutDistance: 1, |
| 67 | + }, |
| 68 | + ) |
| 69 | + require.NoError(t, err) |
| 70 | + |
| 71 | + _, err = store.FetchUnconfirmedSweepBatches(ctxb) |
| 72 | + require.ErrorContains(t, err, test.errMsg) |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// TestFetchUnconfirmedSweepBatchesAllowsEmptyBatchTxID verifies that empty |
| 78 | +// persisted batch txids are tolerated for recovery robustness. |
| 79 | +func TestFetchUnconfirmedSweepBatchesAllowsEmptyBatchTxID(t *testing.T) { |
| 80 | + t.Parallel() |
| 81 | + |
| 82 | + ctxb := t.Context() |
| 83 | + testDb := loopdb.NewTestDB(t) |
| 84 | + defer testDb.Close() |
| 85 | + |
| 86 | + store := NewSQLStore( |
| 87 | + loopdb.NewTypedStore[Querier](testDb), |
| 88 | + &chaincfg.RegressionNetParams, |
| 89 | + ) |
| 90 | + |
| 91 | + _, err := testDb.Queries.InsertBatch( |
| 92 | + ctxb, sqlc.InsertBatchParams{ |
| 93 | + BatchTxID: sql.NullString{ |
| 94 | + String: "", |
| 95 | + Valid: true, |
| 96 | + }, |
| 97 | + BatchPkScript: []byte{0x00}, |
| 98 | + LastRbfHeight: sql.NullInt32{ |
| 99 | + Int32: 1, |
| 100 | + Valid: true, |
| 101 | + }, |
| 102 | + LastRbfSatPerKw: sql.NullInt32{ |
| 103 | + Int32: 1000, |
| 104 | + Valid: true, |
| 105 | + }, |
| 106 | + MaxTimeoutDistance: 1, |
| 107 | + }, |
| 108 | + ) |
| 109 | + require.NoError(t, err) |
| 110 | + |
| 111 | + _, err = store.FetchUnconfirmedSweepBatches(ctxb) |
| 112 | + require.NoError(t, err) |
| 113 | +} |
| 114 | + |
| 115 | +// parentBatchDB is a test double that overrides GetParentBatch while reusing |
| 116 | +// the rest of the SQL store interface from an embedded BaseDB. |
| 117 | +type parentBatchDB struct { |
| 118 | + BaseDB |
| 119 | + |
| 120 | + batch sqlc.SweepBatch |
| 121 | + err error |
| 122 | +} |
| 123 | + |
| 124 | +// GetParentBatch returns the preconfigured batch row for store tests. |
| 125 | +func (s parentBatchDB) GetParentBatch(ctx context.Context, |
| 126 | + outpoint string) (sqlc.SweepBatch, error) { |
| 127 | + |
| 128 | + return s.batch, s.err |
| 129 | +} |
| 130 | + |
| 131 | +// TestGetParentBatchRejectsInvalidBatchTxID verifies that malformed persisted |
| 132 | +// batch txids are rejected through the GetParentBatch path as well. |
| 133 | +func TestGetParentBatchRejectsInvalidBatchTxID(t *testing.T) { |
| 134 | + t.Parallel() |
| 135 | + |
| 136 | + store := NewSQLStore(parentBatchDB{ |
| 137 | + batch: sqlc.SweepBatch{ |
| 138 | + BatchTxID: sql.NullString{ |
| 139 | + String: "abcd", |
| 140 | + Valid: true, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, &chaincfg.RegressionNetParams) |
| 144 | + |
| 145 | + _, err := store.GetParentBatch(t.Context(), wire.OutPoint{}) |
| 146 | + require.ErrorContains(t, err, "invalid batch txid") |
| 147 | +} |
0 commit comments