@@ -146,12 +146,15 @@ func NewBoltSwapStore(dbPath string, chainParams *chaincfg.Params) (
146146 }, nil
147147}
148148
149- func (s * boltSwapStore ) fetchSwaps (bucketKey []byte ,
150- callback func ([]byte , Loop ) error ) error {
149+ // FetchLoopOutSwaps returns all loop out swaps currently in the store.
150+ //
151+ // NOTE: Part of the loopdb.SwapStore interface.
152+ func (s * boltSwapStore ) FetchLoopOutSwaps () ([]* LoopOut , error ) {
153+ var swaps []* LoopOut
151154
152- return s .db .View (func (tx * bbolt.Tx ) error {
155+ err := s .db .View (func (tx * bbolt.Tx ) error {
153156 // First, we'll grab our main loop in bucket key.
154- rootBucket := tx .Bucket (bucketKey )
157+ rootBucket := tx .Bucket (loopOutBucketKey )
155158 if rootBucket == nil {
156159 return errors .New ("bucket does not exist" )
157160 }
@@ -180,22 +183,40 @@ func (s *boltSwapStore) fetchSwaps(bucketKey []byte,
180183 return errors .New ("contract not found" )
181184 }
182185
186+ contract , err := deserializeLoopOutContract (
187+ contractBytes , s .chainParams ,
188+ )
189+ if err != nil {
190+ return err
191+ }
192+
183193 updates , err := deserializeUpdates (swapBucket )
184194 if err != nil {
185195 return err
186196 }
187197
188- var hash lntypes.Hash
189- copy (hash [:], swapHash )
198+ loop := LoopOut {
199+ Loop : Loop {
200+ Events : updates ,
201+ },
202+ Contract : contract ,
203+ }
190204
191- loop := Loop {
192- Hash : hash ,
193- Events : updates ,
205+ loop . Hash , err = lntypes . MakeHash ( swapHash )
206+ if err != nil {
207+ return err
194208 }
195209
196- return callback (contractBytes , loop )
210+ swaps = append (swaps , & loop )
211+
212+ return nil
197213 })
198214 })
215+ if err != nil {
216+ return nil , err
217+ }
218+
219+ return swaps , nil
199220}
200221
201222// deserializeUpdates deserializes the list of swap updates that are stored as a
@@ -227,59 +248,72 @@ func deserializeUpdates(swapBucket *bbolt.Bucket) ([]*LoopEvent, error) {
227248 return updates , nil
228249}
229250
230- // FetchLoopOutSwaps returns all loop out swaps currently in the store.
251+ // FetchLoopInSwaps returns all loop in swaps currently in the store.
231252//
232253// NOTE: Part of the loopdb.SwapStore interface.
233- func (s * boltSwapStore ) FetchLoopOutSwaps () ([]* LoopOut , error ) {
234- var swaps []* LoopOut
235-
236- err := s .fetchSwaps (loopOutBucketKey ,
237- func (contractBytes []byte , loop Loop ) error {
238- contract , err := deserializeLoopOutContract (
239- contractBytes , s .chainParams ,
240- )
241- if err != nil {
242- return err
243- }
254+ func (s * boltSwapStore ) FetchLoopInSwaps () ([]* LoopIn , error ) {
255+ var swaps []* LoopIn
244256
245- swaps = append (swaps , & LoopOut {
246- Contract : contract ,
247- Loop : loop ,
248- })
257+ err := s .db .View (func (tx * bbolt.Tx ) error {
258+ // First, we'll grab our main loop in bucket key.
259+ rootBucket := tx .Bucket (loopInBucketKey )
260+ if rootBucket == nil {
261+ return errors .New ("bucket does not exist" )
262+ }
249263
250- return nil
251- },
252- )
253- if err != nil {
254- return nil , err
255- }
264+ // We'll now traverse the root bucket for all active swaps. The
265+ // primary key is the swap hash itself.
266+ return rootBucket .ForEach (func (swapHash , v []byte ) error {
267+ // Only go into things that we know are sub-bucket
268+ // keys.
269+ if v != nil {
270+ return nil
271+ }
256272
257- return swaps , nil
258- }
273+ // From the root bucket, we'll grab the next swap
274+ // bucket for this swap from its swaphash.
275+ swapBucket := rootBucket .Bucket (swapHash )
276+ if swapBucket == nil {
277+ return fmt .Errorf ("swap bucket %x not found" ,
278+ swapHash )
279+ }
259280
260- // FetchLoopInSwaps returns all loop in swaps currently in the store.
261- //
262- // NOTE: Part of the loopdb.SwapStore interface.
263- func (s * boltSwapStore ) FetchLoopInSwaps () ([]* LoopIn , error ) {
264- var swaps []* LoopIn
281+ // With the main swap bucket obtained, we'll grab the
282+ // raw swap contract bytes and decode it.
283+ contractBytes := swapBucket .Get (contractKey )
284+ if contractBytes == nil {
285+ return errors .New ("contract not found" )
286+ }
265287
266- err := s .fetchSwaps (loopInBucketKey ,
267- func (contractBytes []byte , loop Loop ) error {
268288 contract , err := deserializeLoopInContract (
269289 contractBytes ,
270290 )
271291 if err != nil {
272292 return err
273293 }
274294
275- swaps = append (swaps , & LoopIn {
295+ updates , err := deserializeUpdates (swapBucket )
296+ if err != nil {
297+ return err
298+ }
299+
300+ loop := LoopIn {
301+ Loop : Loop {
302+ Events : updates ,
303+ },
276304 Contract : contract ,
277- Loop : loop ,
278- })
305+ }
306+
307+ loop .Hash , err = lntypes .MakeHash (swapHash )
308+ if err != nil {
309+ return err
310+ }
311+
312+ swaps = append (swaps , & loop )
279313
280314 return nil
281- },
282- )
315+ })
316+ } )
283317 if err != nil {
284318 return nil , err
285319 }
0 commit comments