44 "context"
55 "errors"
66 "fmt"
7+ "sync"
78 "testing"
89 "time"
910
@@ -14,6 +15,8 @@ import (
1415
1516// StoreMock implements a mock client swap store.
1617type StoreMock struct {
18+ sync.RWMutex
19+
1720 LoopOutSwaps map [lntypes.Hash ]* LoopOutContract
1821 LoopOutUpdates map [lntypes.Hash ][]SwapStateData
1922 loopOutStoreChan chan LoopOutContract
@@ -50,6 +53,9 @@ func NewStoreMock(t *testing.T) *StoreMock {
5053//
5154// NOTE: Part of the SwapStore interface.
5255func (s * StoreMock ) FetchLoopOutSwaps (ctx context.Context ) ([]* LoopOut , error ) {
56+ s .RLock ()
57+ defer s .RUnlock ()
58+
5359 result := []* LoopOut {}
5460
5561 for hash , contract := range s .LoopOutSwaps {
@@ -80,6 +86,9 @@ func (s *StoreMock) FetchLoopOutSwaps(ctx context.Context) ([]*LoopOut, error) {
8086func (s * StoreMock ) FetchLoopOutSwap (ctx context.Context ,
8187 hash lntypes.Hash ) (* LoopOut , error ) {
8288
89+ s .RLock ()
90+ defer s .RUnlock ()
91+
8392 contract , ok := s .LoopOutSwaps [hash ]
8493 if ! ok {
8594 return nil , errors .New ("swap not found" )
@@ -110,6 +119,9 @@ func (s *StoreMock) FetchLoopOutSwap(ctx context.Context,
110119func (s * StoreMock ) CreateLoopOut (ctx context.Context , hash lntypes.Hash ,
111120 swap * LoopOutContract ) error {
112121
122+ s .Lock ()
123+ defer s .Unlock ()
124+
113125 _ , ok := s .LoopOutSwaps [hash ]
114126 if ok {
115127 return errors .New ("swap already exists" )
@@ -126,6 +138,9 @@ func (s *StoreMock) CreateLoopOut(ctx context.Context, hash lntypes.Hash,
126138func (s * StoreMock ) FetchLoopInSwaps (ctx context.Context ) ([]* LoopIn ,
127139 error ) {
128140
141+ s .RLock ()
142+ defer s .RUnlock ()
143+
129144 result := []* LoopIn {}
130145
131146 for hash , contract := range s .LoopInSwaps {
@@ -156,6 +171,9 @@ func (s *StoreMock) FetchLoopInSwaps(ctx context.Context) ([]*LoopIn,
156171func (s * StoreMock ) CreateLoopIn (ctx context.Context , hash lntypes.Hash ,
157172 swap * LoopInContract ) error {
158173
174+ s .Lock ()
175+ defer s .Unlock ()
176+
159177 _ , ok := s .LoopInSwaps [hash ]
160178 if ok {
161179 return errors .New ("swap already exists" )
@@ -176,6 +194,9 @@ func (s *StoreMock) CreateLoopIn(ctx context.Context, hash lntypes.Hash,
176194func (s * StoreMock ) UpdateLoopOut (ctx context.Context , hash lntypes.Hash ,
177195 time time.Time , state SwapStateData ) error {
178196
197+ s .Lock ()
198+ defer s .Unlock ()
199+
179200 updates , ok := s .LoopOutUpdates [hash ]
180201 if ! ok {
181202 return errors .New ("swap does not exists" )
@@ -196,6 +217,9 @@ func (s *StoreMock) UpdateLoopOut(ctx context.Context, hash lntypes.Hash,
196217func (s * StoreMock ) UpdateLoopIn (ctx context.Context , hash lntypes.Hash ,
197218 time time.Time , state SwapStateData ) error {
198219
220+ s .Lock ()
221+ defer s .Unlock ()
222+
199223 updates , ok := s .LoopInUpdates [hash ]
200224 if ! ok {
201225 return errors .New ("swap does not exists" )
@@ -347,6 +371,9 @@ func (b *StoreMock) BatchInsertUpdate(ctx context.Context,
347371func (s * StoreMock ) BatchUpdateLoopOutSwapCosts (ctx context.Context ,
348372 costs map [lntypes.Hash ]SwapCost ) error {
349373
374+ s .Lock ()
375+ defer s .Unlock ()
376+
350377 for hash , cost := range costs {
351378 if _ , ok := s .LoopOutUpdates [hash ]; ! ok {
352379 return fmt .Errorf ("swap has no updates: %v" , hash )
@@ -367,6 +394,9 @@ func (s *StoreMock) BatchUpdateLoopOutSwapCosts(ctx context.Context,
367394func (s * StoreMock ) HasMigration (ctx context.Context , migrationID string ) (
368395 bool , error ) {
369396
397+ s .RLock ()
398+ defer s .RUnlock ()
399+
370400 _ , ok := s .migrations [migrationID ]
371401
372402 return ok , nil
@@ -376,6 +406,9 @@ func (s *StoreMock) HasMigration(ctx context.Context, migrationID string) (
376406func (s * StoreMock ) SetMigration (ctx context.Context ,
377407 migrationID string ) error {
378408
409+ s .Lock ()
410+ defer s .Unlock ()
411+
379412 if _ , ok := s .migrations [migrationID ]; ok {
380413 return errors .New ("migration already done" )
381414 }
0 commit comments