4
4
"context"
5
5
"errors"
6
6
"sort"
7
+ "sync"
7
8
8
9
"github.com/btcsuite/btcd/btcutil"
9
10
"github.com/lightningnetwork/lnd/lntypes"
@@ -13,6 +14,7 @@ import (
13
14
type StoreMock struct {
14
15
batches map [int32 ]dbBatch
15
16
sweeps map [lntypes.Hash ]dbSweep
17
+ mu sync.Mutex
16
18
}
17
19
18
20
// NewStoreMock instantiates a new mock store.
@@ -28,6 +30,9 @@ func NewStoreMock() *StoreMock {
28
30
func (s * StoreMock ) FetchUnconfirmedSweepBatches (ctx context.Context ) (
29
31
[]* dbBatch , error ) {
30
32
33
+ s .mu .Lock ()
34
+ defer s .mu .Unlock ()
35
+
31
36
result := []* dbBatch {}
32
37
for _ , batch := range s .batches {
33
38
batch := batch
@@ -44,6 +49,9 @@ func (s *StoreMock) FetchUnconfirmedSweepBatches(ctx context.Context) (
44
49
func (s * StoreMock ) InsertSweepBatch (ctx context.Context ,
45
50
batch * dbBatch ) (int32 , error ) {
46
51
52
+ s .mu .Lock ()
53
+ defer s .mu .Unlock ()
54
+
47
55
var id int32
48
56
49
57
if len (s .batches ) == 0 {
@@ -66,12 +74,18 @@ func (s *StoreMock) DropBatch(ctx context.Context, id int32) error {
66
74
func (s * StoreMock ) UpdateSweepBatch (ctx context.Context ,
67
75
batch * dbBatch ) error {
68
76
77
+ s .mu .Lock ()
78
+ defer s .mu .Unlock ()
79
+
69
80
s .batches [batch .ID ] = * batch
70
81
return nil
71
82
}
72
83
73
84
// ConfirmBatch confirms a batch.
74
85
func (s * StoreMock ) ConfirmBatch (ctx context.Context , id int32 ) error {
86
+ s .mu .Lock ()
87
+ defer s .mu .Unlock ()
88
+
75
89
batch , ok := s .batches [id ]
76
90
if ! ok {
77
91
return errors .New ("batch not found" )
@@ -87,6 +101,9 @@ func (s *StoreMock) ConfirmBatch(ctx context.Context, id int32) error {
87
101
func (s * StoreMock ) FetchBatchSweeps (ctx context.Context ,
88
102
id int32 ) ([]* dbSweep , error ) {
89
103
104
+ s .mu .Lock ()
105
+ defer s .mu .Unlock ()
106
+
90
107
result := []* dbSweep {}
91
108
for _ , sweep := range s .sweeps {
92
109
sweep := sweep
@@ -104,14 +121,21 @@ func (s *StoreMock) FetchBatchSweeps(ctx context.Context,
104
121
105
122
// UpsertSweep inserts a sweep into the database, or updates an existing sweep.
106
123
func (s * StoreMock ) UpsertSweep (ctx context.Context , sweep * dbSweep ) error {
124
+ s .mu .Lock ()
125
+ defer s .mu .Unlock ()
126
+
107
127
s .sweeps [sweep .SwapHash ] = * sweep
128
+
108
129
return nil
109
130
}
110
131
111
132
// GetSweepStatus returns the status of a sweep.
112
133
func (s * StoreMock ) GetSweepStatus (ctx context.Context ,
113
134
swapHash lntypes.Hash ) (bool , error ) {
114
135
136
+ s .mu .Lock ()
137
+ defer s .mu .Unlock ()
138
+
115
139
sweep , ok := s .sweeps [swapHash ]
116
140
if ! ok {
117
141
return false , nil
@@ -127,6 +151,9 @@ func (s *StoreMock) Close() error {
127
151
128
152
// AssertSweepStored asserts that a sweep is stored.
129
153
func (s * StoreMock ) AssertSweepStored (id lntypes.Hash ) bool {
154
+ s .mu .Lock ()
155
+ defer s .mu .Unlock ()
156
+
130
157
_ , ok := s .sweeps [id ]
131
158
return ok
132
159
}
@@ -135,6 +162,9 @@ func (s *StoreMock) AssertSweepStored(id lntypes.Hash) bool {
135
162
func (s * StoreMock ) GetParentBatch (ctx context.Context , swapHash lntypes.Hash ) (
136
163
* dbBatch , error ) {
137
164
165
+ s .mu .Lock ()
166
+ defer s .mu .Unlock ()
167
+
138
168
for _ , sweep := range s .sweeps {
139
169
if sweep .SwapHash == swapHash {
140
170
batch , ok := s .batches [sweep .BatchID ]
@@ -153,6 +183,9 @@ func (s *StoreMock) GetParentBatch(ctx context.Context, swapHash lntypes.Hash) (
153
183
func (s * StoreMock ) TotalSweptAmount (ctx context.Context , batchID int32 ) (
154
184
btcutil.Amount , error ) {
155
185
186
+ s .mu .Lock ()
187
+ defer s .mu .Unlock ()
188
+
156
189
batch , ok := s .batches [batchID ]
157
190
if ! ok {
158
191
return 0 , errors .New ("batch not found" )
0 commit comments