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,17 +180,38 @@ func (s *boltSwapStore) fetchSwaps(bucketKey []byte,
return errors.New("contract not found") 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 // Once we have the raw swap, we'll also need to decode
// each of the past updates to the swap itself. // each of the past updates to the swap itself.
stateBucket := swapBucket.Bucket(updatesBucketKey) stateBucket := swapBucket.Bucket(updatesBucketKey)
if stateBucket == nil { 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 // Deserialize and collect each swap update into our slice of swap
// slice of swap events. // events.
var updates []*LoopEvent var updates []*LoopEvent
err := stateBucket.ForEach(func(k, v []byte) error { err := stateBucket.ForEach(func(_, v []byte) error {
event, err := deserializeLoopEvent(v) event, err := deserializeLoopEvent(v)
if err != nil { if err != nil {
return err return err
@ -200,20 +221,10 @@ func (s *boltSwapStore) fetchSwaps(bucketKey []byte,
return nil return nil
}) })
if err != nil { if err != nil {
return err return nil, err
}
var hash lntypes.Hash
copy(hash[:], swapHash)
loop := Loop{
Hash: hash,
Events: updates,
} }
return callback(contractBytes, loop) return updates, nil
})
})
} }
// FetchLoopOutSwaps returns all loop out swaps currently in the store. // FetchLoopOutSwaps returns all loop out swaps currently in the store.

Loading…
Cancel
Save