-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathstore_test.go
219 lines (174 loc) · 5.1 KB
/
store_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package sweep
import (
"bytes"
"testing"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/stretchr/testify/require"
)
// TestStore asserts that the store persists the presented data to disk and is
// able to retrieve it again.
func TestStore(t *testing.T) {
// Create new store.
cdb, err := channeldb.MakeTestDB(t)
require.NoError(t, err)
var chain chainhash.Hash
store, err := NewSweeperStore(cdb, &chain)
require.NoError(t, err)
// Notify publication of tx1
tx1 := wire.MsgTx{}
tx1.AddTxIn(&wire.TxIn{
PreviousOutPoint: wire.OutPoint{
Index: 1,
},
})
tr1 := &TxRecord{
Txid: tx1.TxHash(),
}
err = store.StoreTx(tr1)
require.NoError(t, err)
// Notify publication of tx2
tx2 := wire.MsgTx{}
tx2.AddTxIn(&wire.TxIn{
PreviousOutPoint: wire.OutPoint{
Index: 2,
},
})
tr2 := &TxRecord{
Txid: tx2.TxHash(),
}
err = store.StoreTx(tr2)
require.NoError(t, err)
// Recreate the sweeper store
store, err = NewSweeperStore(cdb, &chain)
require.NoError(t, err)
// Assert that both txes are recognized as our own.
ours := store.IsOurTx(tx1.TxHash())
require.True(t, ours, "expected tx to be ours")
ours = store.IsOurTx(tx2.TxHash())
require.True(t, ours, "expected tx to be ours")
// An different hash should be reported as not being ours.
var unknownHash chainhash.Hash
ours = store.IsOurTx(unknownHash)
require.False(t, ours, "expected tx to not be ours")
txns, err := store.ListSweeps()
require.NoError(t, err, "unexpected error")
// Create a map containing the sweeps we expect to be returned by list
// sweeps.
expected := map[chainhash.Hash]bool{
tx1.TxHash(): true,
tx2.TxHash(): true,
}
require.Len(t, txns, len(expected))
for _, tx := range txns {
_, ok := expected[tx]
require.Truef(t, ok, "unexpected txid returned: %v", tx)
}
}
// TestTxRecord asserts that the serializeTxRecord and deserializeTxRecord
// behave as expected.
func TestTxRecord(t *testing.T) {
t.Parallel()
// Create a testing record.
//
// NOTE: Txid is omitted because it is not serialized.
tr := &TxRecord{
FeeRate: 1000,
Fee: 10000,
Published: true,
}
var b bytes.Buffer
// Assert we can serialize the record.
err := serializeTxRecord(&b, tr)
require.NoError(t, err)
// Assert we can deserialize the record.
result, err := deserializeTxRecord(&b)
require.NoError(t, err)
// Assert the deserialized record is equal to the original.
require.Equal(t, tr, result)
}
// TestGetTx asserts that the GetTx method behaves as expected.
func TestGetTx(t *testing.T) {
t.Parallel()
cdb, err := channeldb.MakeTestDB(t)
require.NoError(t, err)
// Create a testing store.
chain := chainhash.Hash{}
store, err := NewSweeperStore(cdb, &chain)
require.NoError(t, err)
// Create a testing record.
txid := chainhash.Hash{1, 2, 3}
tr := &TxRecord{
Txid: txid,
FeeRate: 1000,
Fee: 10000,
Published: true,
}
// Assert we can store this tx record.
err = store.StoreTx(tr)
require.NoError(t, err)
// Assert we can query the tx record.
result, err := store.GetTx(txid)
require.NoError(t, err)
require.Equal(t, tr, result)
// Assert we get an error when querying a non-existing tx.
_, err = store.GetTx(chainhash.Hash{4, 5, 6})
require.ErrorIs(t, ErrTxNotFound, err)
}
// TestGetTxCompatible asserts that when there's old tx record data in the
// database it can be successfully queried.
func TestGetTxCompatible(t *testing.T) {
t.Parallel()
cdb, err := channeldb.MakeTestDB(t)
require.NoError(t, err)
// Create a testing store.
chain := chainhash.Hash{}
store, err := NewSweeperStore(cdb, &chain)
require.NoError(t, err)
// Create a testing txid.
txid := chainhash.Hash{0, 1, 2, 3}
// Create a record using the old format "hash -> empty byte slice".
err = kvdb.Update(cdb, func(tx kvdb.RwTx) error {
txHashesBucket := tx.ReadWriteBucket(txHashesBucketKey)
return txHashesBucket.Put(txid[:], []byte{})
}, func() {})
require.NoError(t, err)
// Assert we can query the tx record.
result, err := store.GetTx(txid)
require.NoError(t, err)
require.Equal(t, txid, result.Txid)
// Assert the Published field is true.
require.True(t, result.Published)
}
// TestDeleteTx asserts that the DeleteTx method behaves as expected.
func TestDeleteTx(t *testing.T) {
t.Parallel()
cdb, err := channeldb.MakeTestDB(t)
require.NoError(t, err)
// Create a testing store.
chain := chainhash.Hash{}
store, err := NewSweeperStore(cdb, &chain)
require.NoError(t, err)
// Create a testing record.
txid := chainhash.Hash{1, 2, 3}
tr := &TxRecord{
Txid: txid,
FeeRate: 1000,
Fee: 10000,
Published: true,
}
// Assert we can store this tx record.
err = store.StoreTx(tr)
require.NoError(t, err)
// Assert we can delete the tx record.
err = store.DeleteTx(txid)
require.NoError(t, err)
// Query it again should give us an error.
_, err = store.GetTx(txid)
require.ErrorIs(t, ErrTxNotFound, err)
// Assert deleting a non-existing tx doesn't return an error.
err = store.DeleteTx(chainhash.Hash{4, 5, 6})
require.NoError(t, err)
}