loopdb: extract update deserialization

Preparation to prevent code duplication in future refactoring.
pull/205/head
Joost Jager 4 years ago
parent ba5577748b
commit 9927139dd3
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -180,25 +180,7 @@ func (s *boltSwapStore) fetchSwaps(bucketKey []byte,
return errors.New("contract not found")
}
// 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")
}
// De serialize and collect each swap update into our
// slice of swap events.
var updates []*LoopEvent
err := stateBucket.ForEach(func(k, v []byte) error {
event, err := deserializeLoopEvent(v)
if err != nil {
return err
}
updates = append(updates, event)
return nil
})
updates, err := deserializeUpdates(swapBucket)
if err != nil {
return err
}
@ -216,6 +198,35 @@ func (s *boltSwapStore) fetchSwaps(bucketKey []byte,
})
}
// 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 nil, errors.New("updates bucket not found")
}
// Deserialize and collect each swap update into our slice of swap
// events.
var updates []*LoopEvent
err := stateBucket.ForEach(func(_, v []byte) error {
event, err := deserializeLoopEvent(v)
if err != nil {
return err
}
updates = append(updates, event)
return nil
})
if err != nil {
return nil, err
}
return updates, nil
}
// FetchLoopOutSwaps returns all loop out swaps currently in the store.
//
// NOTE: Part of the loopdb.SwapStore interface.

Loading…
Cancel
Save