loopdb: extract update deserialization
Preparation to prevent code duplication in future refactoring.
@@ -180,17 +180,38 @@ func (s *boltSwapStore) fetchSwaps(bucketKey []byte,
return errors.New("contract not found")
}
+ updates, err := deserializeUpdates(swapBucket)
+ if err != nil {
+ return err
+ }
+
+ var hash lntypes.Hash
+ copy(hash[:], swapHash)
+
+ loop := Loop{
+ Hash: hash,
+ Events: updates,
+ }
+
+ return callback(contractBytes, loop)
+ })
+ })
+}
+
+// deserializeUpdates deserializes the list of swap updates that are stored as a
+// key of the given bucket.
+func deserializeUpdates(swapBucket *bbolt.Bucket) ([]*LoopEvent, error) {
// Once we have the raw swap, we'll also need to decode
// each of the past updates to the swap itself.
stateBucket := swapBucket.Bucket(updatesBucketKey)
if stateBucket == nil {
- return errors.New("updates bucket not found")
+ return nil, errors.New("updates bucket not found")
}
- // De serialize and collect each swap update into our
- // slice of swap events.
+ // Deserialize and collect each swap update into our slice of swap
+ // events.
var updates []*LoopEvent
- err := stateBucket.ForEach(func(k, v []byte) error {
+ err := stateBucket.ForEach(func(_, v []byte) error {
event, err := deserializeLoopEvent(v)
if err != nil {
return err
@@ -200,20 +221,10 @@ func (s *boltSwapStore) fetchSwaps(bucketKey []byte,
return nil
})
if err != nil {
- return err
- }
-
- var hash lntypes.Hash
- copy(hash[:], swapHash)
-
- loop := Loop{
- Hash: hash,
- Events: updates,
+ return nil, err
}
- return callback(contractBytes, loop)
- })
- })
+ return updates, nil
}
// FetchLoopOutSwaps returns all loop out swaps currently in the store.
