Skip to content

Commit f0f64f8

Browse files
committed
sweepbatcher: fix race in store_mock
1 parent e86ccb9 commit f0f64f8

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

sweepbatcher/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
"sort"
7+
"sync"
78

89
"github.com/btcsuite/btcd/btcutil"
910
"github.com/lightningnetwork/lnd/lntypes"
@@ -13,6 +14,7 @@ import (
1314
type StoreMock struct {
1415
batches map[int32]dbBatch
1516
sweeps map[lntypes.Hash]dbSweep
17+
mu sync.Mutex
1618
}
1719

1820
// NewStoreMock instantiates a new mock store.
@@ -28,6 +30,9 @@ func NewStoreMock() *StoreMock {
2830
func (s *StoreMock) FetchUnconfirmedSweepBatches(ctx context.Context) (
2931
[]*dbBatch, error) {
3032

33+
s.mu.Lock()
34+
defer s.mu.Unlock()
35+
3136
result := []*dbBatch{}
3237
for _, batch := range s.batches {
3338
batch := batch
@@ -44,6 +49,9 @@ func (s *StoreMock) FetchUnconfirmedSweepBatches(ctx context.Context) (
4449
func (s *StoreMock) InsertSweepBatch(ctx context.Context,
4550
batch *dbBatch) (int32, error) {
4651

52+
s.mu.Lock()
53+
defer s.mu.Unlock()
54+
4755
var id int32
4856

4957
if len(s.batches) == 0 {
@@ -66,12 +74,18 @@ func (s *StoreMock) DropBatch(ctx context.Context, id int32) error {
6674
func (s *StoreMock) UpdateSweepBatch(ctx context.Context,
6775
batch *dbBatch) error {
6876

77+
s.mu.Lock()
78+
defer s.mu.Unlock()
79+
6980
s.batches[batch.ID] = *batch
7081
return nil
7182
}
7283

7384
// ConfirmBatch confirms a batch.
7485
func (s *StoreMock) ConfirmBatch(ctx context.Context, id int32) error {
86+
s.mu.Lock()
87+
defer s.mu.Unlock()
88+
7589
batch, ok := s.batches[id]
7690
if !ok {
7791
return errors.New("batch not found")
@@ -87,6 +101,9 @@ func (s *StoreMock) ConfirmBatch(ctx context.Context, id int32) error {
87101
func (s *StoreMock) FetchBatchSweeps(ctx context.Context,
88102
id int32) ([]*dbSweep, error) {
89103

104+
s.mu.Lock()
105+
defer s.mu.Unlock()
106+
90107
result := []*dbSweep{}
91108
for _, sweep := range s.sweeps {
92109
sweep := sweep
@@ -104,14 +121,21 @@ func (s *StoreMock) FetchBatchSweeps(ctx context.Context,
104121

105122
// UpsertSweep inserts a sweep into the database, or updates an existing sweep.
106123
func (s *StoreMock) UpsertSweep(ctx context.Context, sweep *dbSweep) error {
124+
s.mu.Lock()
125+
defer s.mu.Unlock()
126+
107127
s.sweeps[sweep.SwapHash] = *sweep
128+
108129
return nil
109130
}
110131

111132
// GetSweepStatus returns the status of a sweep.
112133
func (s *StoreMock) GetSweepStatus(ctx context.Context,
113134
swapHash lntypes.Hash) (bool, error) {
114135

136+
s.mu.Lock()
137+
defer s.mu.Unlock()
138+
115139
sweep, ok := s.sweeps[swapHash]
116140
if !ok {
117141
return false, nil
@@ -127,6 +151,9 @@ func (s *StoreMock) Close() error {
127151

128152
// AssertSweepStored asserts that a sweep is stored.
129153
func (s *StoreMock) AssertSweepStored(id lntypes.Hash) bool {
154+
s.mu.Lock()
155+
defer s.mu.Unlock()
156+
130157
_, ok := s.sweeps[id]
131158
return ok
132159
}
@@ -135,6 +162,9 @@ func (s *StoreMock) AssertSweepStored(id lntypes.Hash) bool {
135162
func (s *StoreMock) GetParentBatch(ctx context.Context, swapHash lntypes.Hash) (
136163
*dbBatch, error) {
137164

165+
s.mu.Lock()
166+
defer s.mu.Unlock()
167+
138168
for _, sweep := range s.sweeps {
139169
if sweep.SwapHash == swapHash {
140170
batch, ok := s.batches[sweep.BatchID]
@@ -153,6 +183,9 @@ func (s *StoreMock) GetParentBatch(ctx context.Context, swapHash lntypes.Hash) (
153183
func (s *StoreMock) TotalSweptAmount(ctx context.Context, batchID int32) (
154184
btcutil.Amount, error) {
155185

186+
s.mu.Lock()
187+
defer s.mu.Unlock()
188+
156189
batch, ok := s.batches[batchID]
157190
if !ok {
158191
return 0, errors.New("batch not found")

0 commit comments

Comments
 (0)