loopdb: unroll shared fetch logic

Split the fetch logic so that it is easier to add loop type-specific
serialization.
pull/205/head
Joost Jager 4 years ago
parent 9927139dd3
commit 503c83c29f
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -146,12 +146,15 @@ func NewBoltSwapStore(dbPath string, chainParams *chaincfg.Params) (
}, nil }, nil
} }
func (s *boltSwapStore) fetchSwaps(bucketKey []byte, // FetchLoopOutSwaps returns all loop out swaps currently in the store.
callback func([]byte, Loop) error) error { //
// NOTE: Part of the loopdb.SwapStore interface.
func (s *boltSwapStore) FetchLoopOutSwaps() ([]*LoopOut, error) {
var swaps []*LoopOut
return s.db.View(func(tx *bbolt.Tx) error { err := s.db.View(func(tx *bbolt.Tx) error {
// First, we'll grab our main loop in bucket key. // First, we'll grab our main loop in bucket key.
rootBucket := tx.Bucket(bucketKey) rootBucket := tx.Bucket(loopOutBucketKey)
if rootBucket == nil { if rootBucket == nil {
return errors.New("bucket does not exist") return errors.New("bucket does not exist")
} }
@ -180,22 +183,40 @@ func (s *boltSwapStore) fetchSwaps(bucketKey []byte,
return errors.New("contract not found") return errors.New("contract not found")
} }
contract, err := deserializeLoopOutContract(
contractBytes, s.chainParams,
)
if err != nil {
return err
}
updates, err := deserializeUpdates(swapBucket) updates, err := deserializeUpdates(swapBucket)
if err != nil { if err != nil {
return err return err
} }
var hash lntypes.Hash loop := LoopOut{
copy(hash[:], swapHash) Loop: Loop{
Events: updates,
},
Contract: contract,
}
loop := Loop{ loop.Hash, err = lntypes.MakeHash(swapHash)
Hash: hash, if err != nil {
Events: updates, return err
} }
return callback(contractBytes, loop) swaps = append(swaps, &loop)
return nil
}) })
}) })
if err != nil {
return nil, err
}
return swaps, nil
} }
// deserializeUpdates deserializes the list of swap updates that are stored as a // deserializeUpdates deserializes the list of swap updates that are stored as a
@ -227,44 +248,43 @@ func deserializeUpdates(swapBucket *bbolt.Bucket) ([]*LoopEvent, error) {
return updates, nil return updates, nil
} }
// FetchLoopOutSwaps returns all loop out swaps currently in the store. // FetchLoopInSwaps returns all loop in swaps currently in the store.
// //
// NOTE: Part of the loopdb.SwapStore interface. // NOTE: Part of the loopdb.SwapStore interface.
func (s *boltSwapStore) FetchLoopOutSwaps() ([]*LoopOut, error) { func (s *boltSwapStore) FetchLoopInSwaps() ([]*LoopIn, error) {
var swaps []*LoopOut var swaps []*LoopIn
err := s.fetchSwaps(loopOutBucketKey,
func(contractBytes []byte, loop Loop) error {
contract, err := deserializeLoopOutContract(
contractBytes, s.chainParams,
)
if err != nil {
return err
}
swaps = append(swaps, &LoopOut{ err := s.db.View(func(tx *bbolt.Tx) error {
Contract: contract, // First, we'll grab our main loop in bucket key.
Loop: loop, rootBucket := tx.Bucket(loopInBucketKey)
}) if rootBucket == nil {
return errors.New("bucket does not exist")
}
return nil // We'll now traverse the root bucket for all active swaps. The
}, // primary key is the swap hash itself.
) return rootBucket.ForEach(func(swapHash, v []byte) error {
if err != nil { // Only go into things that we know are sub-bucket
return nil, err // keys.
} if v != nil {
return nil
}
return swaps, nil // From the root bucket, we'll grab the next swap
} // bucket for this swap from its swaphash.
swapBucket := rootBucket.Bucket(swapHash)
if swapBucket == nil {
return fmt.Errorf("swap bucket %x not found",
swapHash)
}
// FetchLoopInSwaps returns all loop in swaps currently in the store. // With the main swap bucket obtained, we'll grab the
// // raw swap contract bytes and decode it.
// NOTE: Part of the loopdb.SwapStore interface. contractBytes := swapBucket.Get(contractKey)
func (s *boltSwapStore) FetchLoopInSwaps() ([]*LoopIn, error) { if contractBytes == nil {
var swaps []*LoopIn return errors.New("contract not found")
}
err := s.fetchSwaps(loopInBucketKey,
func(contractBytes []byte, loop Loop) error {
contract, err := deserializeLoopInContract( contract, err := deserializeLoopInContract(
contractBytes, contractBytes,
) )
@ -272,14 +292,28 @@ func (s *boltSwapStore) FetchLoopInSwaps() ([]*LoopIn, error) {
return err return err
} }
swaps = append(swaps, &LoopIn{ updates, err := deserializeUpdates(swapBucket)
if err != nil {
return err
}
loop := LoopIn{
Loop: Loop{
Events: updates,
},
Contract: contract, Contract: contract,
Loop: loop, }
})
loop.Hash, err = lntypes.MakeHash(swapHash)
if err != nil {
return err
}
swaps = append(swaps, &loop)
return nil return nil
}, })
) })
if err != nil { if err != nil {
return nil, err return nil, err
} }

Loading…
Cancel
Save