@@ -276,36 +276,54 @@ func (s *boltSwapStore) FetchLoopInSwaps() ([]*LoopIn, error) {
276276 return swaps , nil
277277}
278278
279- // createLoop creates a swap in the store. It requires that the contract is
280- // already serialized to be able to use this function for both in and out swaps.
281- func (s * boltSwapStore ) createLoop (bucketKey []byte , hash lntypes.Hash ,
282- contractBytes []byte ) error {
279+ // createLoopBucket creates the bucket for a particular swap.
280+ func createLoopBucket (tx * bbolt.Tx , swapTypeKey []byte , hash lntypes.Hash ) (
281+ * bbolt.Bucket , error ) {
282+
283+ // First, we'll grab the root bucket that houses all of our
284+ // swaps of this type.
285+ swapTypeBucket , err := tx .CreateBucketIfNotExists (swapTypeKey )
286+ if err != nil {
287+ return nil , err
288+ }
289+
290+ // If the swap already exists, then we'll exit as we don't want
291+ // to override a swap.
292+ if swapTypeBucket .Get (hash [:]) != nil {
293+ return nil , fmt .Errorf ("swap %v already exists" , hash )
294+ }
295+
296+ // From the swap type bucket, we'll make a new sub swap bucket using the
297+ // swap hash to store the individual swap.
298+ return swapTypeBucket .CreateBucket (hash [:])
299+ }
300+
301+ // CreateLoopOut adds an initiated swap to the store.
302+ //
303+ // NOTE: Part of the loopdb.SwapStore interface.
304+ func (s * boltSwapStore ) CreateLoopOut (hash lntypes.Hash ,
305+ swap * LoopOutContract ) error {
306+
307+ // If the hash doesn't match the pre-image, then this is an invalid
308+ // swap so we'll bail out early.
309+ if hash != swap .Preimage .Hash () {
310+ return errors .New ("hash and preimage do not match" )
311+ }
283312
284313 // Otherwise, we'll create a new swap within the database.
285314 return s .db .Update (func (tx * bbolt.Tx ) error {
286- // First, we'll grab the root bucket that houses all of our
287- // main swaps.
288- rootBucket , err := tx .CreateBucketIfNotExists (
289- bucketKey ,
290- )
315+ // Create the swap bucket.
316+ swapBucket , err := createLoopBucket (tx , loopOutBucketKey , hash )
291317 if err != nil {
292318 return err
293319 }
294320
295- // If the swap already exists, then we'll exit as we don't want
296- // to override a swap.
297- if rootBucket .Get (hash [:]) != nil {
298- return fmt .Errorf ("swap %v already exists" , hash )
299- }
300-
301- // From the root bucket, we'll make a new sub swap bucket using
302- // the swap hash.
303- swapBucket , err := rootBucket .CreateBucket (hash [:])
321+ // With the swap bucket created, we'll store the swap itself.
322+ contractBytes , err := serializeLoopOutContract (swap )
304323 if err != nil {
305324 return err
306325 }
307326
308- // With the swap bucket created, we'll store the swap itself.
309327 err = swapBucket .Put (contractKey , contractBytes )
310328 if err != nil {
311329 return err
@@ -318,26 +336,6 @@ func (s *boltSwapStore) createLoop(bucketKey []byte, hash lntypes.Hash,
318336 })
319337}
320338
321- // CreateLoopOut adds an initiated swap to the store.
322- //
323- // NOTE: Part of the loopdb.SwapStore interface.
324- func (s * boltSwapStore ) CreateLoopOut (hash lntypes.Hash ,
325- swap * LoopOutContract ) error {
326-
327- // If the hash doesn't match the pre-image, then this is an invalid
328- // swap so we'll bail out early.
329- if hash != swap .Preimage .Hash () {
330- return errors .New ("hash and preimage do not match" )
331- }
332-
333- contractBytes , err := serializeLoopOutContract (swap )
334- if err != nil {
335- return err
336- }
337-
338- return s .createLoop (loopOutBucketKey , hash , contractBytes )
339- }
340-
341339// CreateLoopIn adds an initiated swap to the store.
342340//
343341// NOTE: Part of the loopdb.SwapStore interface.
@@ -350,12 +348,30 @@ func (s *boltSwapStore) CreateLoopIn(hash lntypes.Hash,
350348 return errors .New ("hash and preimage do not match" )
351349 }
352350
353- contractBytes , err := serializeLoopInContract (swap )
354- if err != nil {
355- return err
356- }
351+ // Otherwise, we'll create a new swap within the database.
352+ return s .db .Update (func (tx * bbolt.Tx ) error {
353+ // Create the swap bucket.
354+ swapBucket , err := createLoopBucket (tx , loopInBucketKey , hash )
355+ if err != nil {
356+ return err
357+ }
358+
359+ // With the swap bucket created, we'll store the swap itself.
360+ contractBytes , err := serializeLoopInContract (swap )
361+ if err != nil {
362+ return err
363+ }
364+
365+ err = swapBucket .Put (contractKey , contractBytes )
366+ if err != nil {
367+ return err
368+ }
357369
358- return s .createLoop (loopInBucketKey , hash , contractBytes )
370+ // Finally, we'll create an empty updates bucket for this swap
371+ // to track any future updates to the swap itself.
372+ _ , err = swapBucket .CreateBucket (updatesBucketKey )
373+ return err
374+ })
359375}
360376
361377// updateLoop saves a new swap state transition to the store. It takes in a
0 commit comments