Skip to content

Commit 9927139

Browse files
committed
loopdb: extract update deserialization
Preparation to prevent code duplication in future refactoring.
1 parent ba55777 commit 9927139

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

loopdb/store.go

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -180,25 +180,7 @@ func (s *boltSwapStore) fetchSwaps(bucketKey []byte,
180180
return errors.New("contract not found")
181181
}
182182

183-
// Once we have the raw swap, we'll also need to decode
184-
// each of the past updates to the swap itself.
185-
stateBucket := swapBucket.Bucket(updatesBucketKey)
186-
if stateBucket == nil {
187-
return errors.New("updates bucket not found")
188-
}
189-
190-
// De serialize and collect each swap update into our
191-
// slice of swap events.
192-
var updates []*LoopEvent
193-
err := stateBucket.ForEach(func(k, v []byte) error {
194-
event, err := deserializeLoopEvent(v)
195-
if err != nil {
196-
return err
197-
}
198-
199-
updates = append(updates, event)
200-
return nil
201-
})
183+
updates, err := deserializeUpdates(swapBucket)
202184
if err != nil {
203185
return err
204186
}
@@ -216,6 +198,35 @@ func (s *boltSwapStore) fetchSwaps(bucketKey []byte,
216198
})
217199
}
218200

201+
// deserializeUpdates deserializes the list of swap updates that are stored as a
202+
// key of the given bucket.
203+
func deserializeUpdates(swapBucket *bbolt.Bucket) ([]*LoopEvent, error) {
204+
// Once we have the raw swap, we'll also need to decode
205+
// each of the past updates to the swap itself.
206+
stateBucket := swapBucket.Bucket(updatesBucketKey)
207+
if stateBucket == nil {
208+
return nil, errors.New("updates bucket not found")
209+
}
210+
211+
// Deserialize and collect each swap update into our slice of swap
212+
// events.
213+
var updates []*LoopEvent
214+
err := stateBucket.ForEach(func(_, v []byte) error {
215+
event, err := deserializeLoopEvent(v)
216+
if err != nil {
217+
return err
218+
}
219+
220+
updates = append(updates, event)
221+
return nil
222+
})
223+
if err != nil {
224+
return nil, err
225+
}
226+
227+
return updates, nil
228+
}
229+
219230
// FetchLoopOutSwaps returns all loop out swaps currently in the store.
220231
//
221232
// NOTE: Part of the loopdb.SwapStore interface.

0 commit comments

Comments
 (0)