Skip to content

Commit e0d8595

Browse files
committed
multi: move StoreMock to loopdb
1 parent 99608ad commit e0d8595

File tree

8 files changed

+402
-375
lines changed

8 files changed

+402
-375
lines changed

client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,13 @@ func testLoopOutSuccess(ctx *testContext, amt btcutil.Amount, hash lntypes.Hash,
340340
// preimage before sweeping in order for the server to trust us with
341341
// our MuSig2 signing attempts.
342342
if scriptVersion == swap.HtlcV3 {
343-
ctx.assertPreimagePush(ctx.store.loopOutSwaps[hash].Preimage)
343+
ctx.assertPreimagePush(ctx.store.LoopOutSwaps[hash].Preimage)
344344

345345
// Try MuSig2 signing first and fail it so that we go for a
346346
// normal sweep.
347347
for i := 0; i < maxMusigSweepRetries; i++ {
348348
ctx.expiryChan <- testTime
349-
ctx.assertPreimagePush(ctx.store.loopOutSwaps[hash].Preimage)
349+
ctx.assertPreimagePush(ctx.store.LoopOutSwaps[hash].Preimage)
350350
}
351351
<-ctx.Context.Lnd.SignOutputRawChannel
352352
}

loopdb/store_mock.go

Lines changed: 339 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,339 @@
1+
package loopdb
2+
3+
import (
4+
"context"
5+
"errors"
6+
"testing"
7+
"time"
8+
9+
"github.com/lightninglabs/loop/test"
10+
"github.com/lightningnetwork/lnd/lntypes"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
// StoreMock implements a mock client swap store.
15+
type StoreMock struct {
16+
LoopOutSwaps map[lntypes.Hash]*LoopOutContract
17+
LoopOutUpdates map[lntypes.Hash][]SwapStateData
18+
loopOutStoreChan chan LoopOutContract
19+
loopOutUpdateChan chan SwapStateData
20+
21+
LoopInSwaps map[lntypes.Hash]*LoopInContract
22+
LoopInUpdates map[lntypes.Hash][]SwapStateData
23+
loopInStoreChan chan LoopInContract
24+
loopInUpdateChan chan SwapStateData
25+
26+
t *testing.T
27+
}
28+
29+
// NewStoreMock instantiates a new mock store.
30+
func NewStoreMock(t *testing.T) *StoreMock {
31+
return &StoreMock{
32+
loopOutStoreChan: make(chan LoopOutContract, 1),
33+
loopOutUpdateChan: make(chan SwapStateData, 1),
34+
LoopOutSwaps: make(map[lntypes.Hash]*LoopOutContract),
35+
LoopOutUpdates: make(map[lntypes.Hash][]SwapStateData),
36+
37+
loopInStoreChan: make(chan LoopInContract, 1),
38+
loopInUpdateChan: make(chan SwapStateData, 1),
39+
LoopInSwaps: make(map[lntypes.Hash]*LoopInContract),
40+
LoopInUpdates: make(map[lntypes.Hash][]SwapStateData),
41+
t: t,
42+
}
43+
}
44+
45+
// FetchLoopOutSwaps returns all swaps currently in the store.
46+
//
47+
// NOTE: Part of the SwapStore interface.
48+
func (s *StoreMock) FetchLoopOutSwaps(ctx context.Context) ([]*LoopOut, error) {
49+
result := []*LoopOut{}
50+
51+
for hash, contract := range s.LoopOutSwaps {
52+
updates := s.LoopOutUpdates[hash]
53+
events := make([]*LoopEvent, len(updates))
54+
for i, u := range updates {
55+
events[i] = &LoopEvent{
56+
SwapStateData: u,
57+
}
58+
}
59+
60+
swap := &LoopOut{
61+
Loop: Loop{
62+
Hash: hash,
63+
Events: events,
64+
},
65+
Contract: contract,
66+
}
67+
result = append(result, swap)
68+
}
69+
70+
return result, nil
71+
}
72+
73+
// FetchLoopOutSwaps returns all swaps currently in the store.
74+
//
75+
// NOTE: Part of the SwapStore interface.
76+
func (s *StoreMock) FetchLoopOutSwap(ctx context.Context,
77+
hash lntypes.Hash) (*LoopOut, error) {
78+
79+
contract, ok := s.LoopOutSwaps[hash]
80+
if !ok {
81+
return nil, errors.New("swap not found")
82+
}
83+
84+
updates := s.LoopOutUpdates[hash]
85+
events := make([]*LoopEvent, len(updates))
86+
for i, u := range updates {
87+
events[i] = &LoopEvent{
88+
SwapStateData: u,
89+
}
90+
}
91+
92+
swap := &LoopOut{
93+
Loop: Loop{
94+
Hash: hash,
95+
Events: events,
96+
},
97+
Contract: contract,
98+
}
99+
100+
return swap, nil
101+
}
102+
103+
// CreateLoopOut adds an initiated swap to the store.
104+
//
105+
// NOTE: Part of the SwapStore interface.
106+
func (s *StoreMock) CreateLoopOut(ctx context.Context, hash lntypes.Hash,
107+
swap *LoopOutContract) error {
108+
109+
_, ok := s.LoopOutSwaps[hash]
110+
if ok {
111+
return errors.New("swap already exists")
112+
}
113+
114+
s.LoopOutSwaps[hash] = swap
115+
s.LoopOutUpdates[hash] = []SwapStateData{}
116+
s.loopOutStoreChan <- *swap
117+
118+
return nil
119+
}
120+
121+
// FetchLoopInSwaps returns all in swaps currently in the store.
122+
func (s *StoreMock) FetchLoopInSwaps(ctx context.Context) ([]*LoopIn,
123+
error) {
124+
125+
result := []*LoopIn{}
126+
127+
for hash, contract := range s.LoopInSwaps {
128+
updates := s.LoopInUpdates[hash]
129+
events := make([]*LoopEvent, len(updates))
130+
for i, u := range updates {
131+
events[i] = &LoopEvent{
132+
SwapStateData: u,
133+
}
134+
}
135+
136+
swap := &LoopIn{
137+
Loop: Loop{
138+
Hash: hash,
139+
Events: events,
140+
},
141+
Contract: contract,
142+
}
143+
result = append(result, swap)
144+
}
145+
146+
return result, nil
147+
}
148+
149+
// CreateLoopIn adds an initiated loop in swap to the store.
150+
//
151+
// NOTE: Part of the SwapStore interface.
152+
func (s *StoreMock) CreateLoopIn(ctx context.Context, hash lntypes.Hash,
153+
swap *LoopInContract) error {
154+
155+
_, ok := s.LoopInSwaps[hash]
156+
if ok {
157+
return errors.New("swap already exists")
158+
}
159+
160+
s.LoopInSwaps[hash] = swap
161+
s.LoopInUpdates[hash] = []SwapStateData{}
162+
s.loopInStoreChan <- *swap
163+
164+
return nil
165+
}
166+
167+
// UpdateLoopOut stores a new event for a target loop out swap. This appends to
168+
// the event log for a particular swap as it goes through the various stages in
169+
// its lifetime.
170+
//
171+
// NOTE: Part of the SwapStore interface.
172+
func (s *StoreMock) UpdateLoopOut(ctx context.Context, hash lntypes.Hash,
173+
time time.Time, state SwapStateData) error {
174+
175+
updates, ok := s.LoopOutUpdates[hash]
176+
if !ok {
177+
return errors.New("swap does not exists")
178+
}
179+
180+
updates = append(updates, state)
181+
s.LoopOutUpdates[hash] = updates
182+
s.loopOutUpdateChan <- state
183+
184+
return nil
185+
}
186+
187+
// UpdateLoopIn stores a new event for a target loop in swap. This appends to
188+
// the event log for a particular swap as it goes through the various stages in
189+
// its lifetime.
190+
//
191+
// NOTE: Part of the SwapStore interface.
192+
func (s *StoreMock) UpdateLoopIn(ctx context.Context, hash lntypes.Hash,
193+
time time.Time, state SwapStateData) error {
194+
195+
updates, ok := s.LoopInUpdates[hash]
196+
if !ok {
197+
return errors.New("swap does not exists")
198+
}
199+
200+
updates = append(updates, state)
201+
s.LoopInUpdates[hash] = updates
202+
s.loopInUpdateChan <- state
203+
204+
return nil
205+
}
206+
207+
// PutLiquidityParams writes the serialized `manager.Parameters` bytes into the
208+
// bucket.
209+
//
210+
// NOTE: Part of the SwapStore interface.
211+
func (s *StoreMock) PutLiquidityParams(ctx context.Context,
212+
params []byte) error {
213+
214+
return nil
215+
}
216+
217+
// FetchLiquidityParams reads the serialized `manager.Parameters` bytes from
218+
// the bucket.
219+
//
220+
// NOTE: Part of the SwapStore interface.
221+
func (s *StoreMock) FetchLiquidityParams(ctx context.Context) ([]byte, error) {
222+
return nil, nil
223+
}
224+
225+
// Close closes the store.
226+
func (s *StoreMock) Close() error {
227+
return nil
228+
}
229+
230+
// isDone asserts that the store mock has no pending operations.
231+
func (s *StoreMock) IsDone() error {
232+
select {
233+
case <-s.loopOutStoreChan:
234+
return errors.New("storeChan not empty")
235+
default:
236+
}
237+
238+
select {
239+
case <-s.loopOutUpdateChan:
240+
return errors.New("updateChan not empty")
241+
default:
242+
}
243+
return nil
244+
}
245+
246+
// AssertLoopOutStored asserts that a swap is stored.
247+
func (s *StoreMock) AssertLoopOutStored() {
248+
s.t.Helper()
249+
250+
select {
251+
case <-s.loopOutStoreChan:
252+
case <-time.After(test.Timeout):
253+
s.t.Fatalf("expected swap to be stored")
254+
}
255+
}
256+
257+
// AssertLoopOutState asserts that a specified state transition is persisted to
258+
// disk.
259+
func (s *StoreMock) AssertLoopOutState(expectedState SwapState) {
260+
s.t.Helper()
261+
262+
select {
263+
case state := <-s.loopOutUpdateChan:
264+
require.Equal(s.t, expectedState, state.State)
265+
case <-time.After(test.Timeout):
266+
s.t.Fatalf("expected swap state to be stored")
267+
}
268+
}
269+
270+
// AssertLoopInStored asserts that a loop-in swap is stored.
271+
func (s *StoreMock) AssertLoopInStored() {
272+
s.t.Helper()
273+
274+
select {
275+
case <-s.loopInStoreChan:
276+
case <-time.After(test.Timeout):
277+
s.t.Fatalf("expected swap to be stored")
278+
}
279+
}
280+
281+
// assertLoopInState asserts that a specified state transition is persisted to
282+
// disk.
283+
func (s *StoreMock) AssertLoopInState(
284+
expectedState SwapState) SwapStateData {
285+
286+
s.t.Helper()
287+
288+
state := <-s.loopInUpdateChan
289+
require.Equal(s.t, expectedState, state.State)
290+
291+
return state
292+
}
293+
294+
// AssertStorePreimageReveal asserts that a swap is marked as preimage revealed.
295+
func (s *StoreMock) AssertStorePreimageReveal() {
296+
s.t.Helper()
297+
298+
select {
299+
case state := <-s.loopOutUpdateChan:
300+
require.Equal(s.t, StatePreimageRevealed, state.State)
301+
302+
case <-time.After(test.Timeout):
303+
s.t.Fatalf("expected swap to be marked as preimage revealed")
304+
}
305+
}
306+
307+
// AssertStoreFinished asserts that a swap is marked as finished.
308+
func (s *StoreMock) AssertStoreFinished(expectedResult SwapState) {
309+
s.t.Helper()
310+
311+
select {
312+
case state := <-s.loopOutUpdateChan:
313+
require.Equal(s.t, expectedResult, state.State)
314+
315+
case <-time.After(test.Timeout):
316+
s.t.Fatalf("expected swap to be finished")
317+
}
318+
}
319+
320+
// BatchCreateLoopOut creates many loop out swaps in a batch.
321+
func (b *StoreMock) BatchCreateLoopOut(ctx context.Context,
322+
swaps map[lntypes.Hash]*LoopOutContract) error {
323+
324+
return errors.New("not implemented")
325+
}
326+
327+
// BatchCreateLoopIn creates many loop in swaps in a batch.
328+
func (b *StoreMock) BatchCreateLoopIn(ctx context.Context,
329+
swaps map[lntypes.Hash]*LoopInContract) error {
330+
331+
return errors.New("not implemented")
332+
}
333+
334+
// BatchInsertUpdate inserts many updates for a swap in a batch.
335+
func (b *StoreMock) BatchInsertUpdate(ctx context.Context,
336+
updateData map[lntypes.Hash][]BatchInsertUpdateData) error {
337+
338+
return errors.New("not implemented")
339+
}

0 commit comments

Comments
 (0)