Skip to content

Commit 634fbbc

Browse files
authored
Merge pull request #819 from bhandras/store-mock-mx
loopdb: properly lock store mock for concurrent access
2 parents 8978afe + bd54183 commit 634fbbc

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

loopdb/store_mock.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
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.
1617
type 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.
5255
func (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) {
8086
func (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,
110119
func (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,
126138
func (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,
156171
func (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,
176194
func (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,
196217
func (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,
347371
func (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,
367394
func (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) (
376406
func (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

Comments
 (0)