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
}
func (s *boltSwapStore) fetchSwaps(bucketKey []byte,
callback func([]byte, Loop) error) error {
// FetchLoopOutSwaps returns all loop out swaps currently in the store.
//
// 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.
rootBucket := tx.Bucket(bucketKey)
rootBucket := tx.Bucket(loopOutBucketKey)
if rootBucket == nil {
return errors.New("bucket does not exist")
}
@ -180,22 +183,40 @@ func (s *boltSwapStore) fetchSwaps(bucketKey []byte,
return errors.New("contract not found")
}
contract, err := deserializeLoopOutContract(
contractBytes, s.chainParams,
)
if err != nil {
return err
}
updates, err := deserializeUpdates(swapBucket)
if err != nil {
return err
}
var hash lntypes.Hash
copy(hash[:], swapHash)
loop := LoopOut{
Loop: Loop{
Events: updates,
},
Contract: contract,
}
loop := Loop{
Hash: hash,
Events: updates,
loop.Hash, err = lntypes.MakeHash(swapHash)
if err != nil {
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
@ -227,44 +248,43 @@ func deserializeUpdates(swapBucket *bbolt.Bucket) ([]*LoopEvent, error) {
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.
func (s *boltSwapStore) FetchLoopOutSwaps() ([]*LoopOut, error) {
var swaps []*LoopOut
err := s.fetchSwaps(loopOutBucketKey,
func(contractBytes []byte, loop Loop) error {
contract, err := deserializeLoopOutContract(
contractBytes, s.chainParams,
)
if err != nil {
return err
}
func (s *boltSwapStore) FetchLoopInSwaps() ([]*LoopIn, error) {
var swaps []*LoopIn
swaps = append(swaps, &LoopOut{
Contract: contract,
Loop: loop,
})
err := s.db.View(func(tx *bbolt.Tx) error {
// First, we'll grab our main loop in bucket key.
rootBucket := tx.Bucket(loopInBucketKey)
if rootBucket == nil {
return errors.New("bucket does not exist")
}
return nil
},
)
if err != nil {
return nil, err
}
// 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 {
// Only go into things that we know are sub-bucket
// 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.
//
// NOTE: Part of the loopdb.SwapStore interface.
func (s *boltSwapStore) FetchLoopInSwaps() ([]*LoopIn, error) {
var swaps []*LoopIn
// With the main swap bucket obtained, we'll grab the
// raw swap contract bytes and decode it.
contractBytes := swapBucket.Get(contractKey)
if contractBytes == nil {
return errors.New("contract not found")
}
err := s.fetchSwaps(loopInBucketKey,
func(contractBytes []byte, loop Loop) error {
contract, err := deserializeLoopInContract(
contractBytes,
)
@ -272,14 +292,28 @@ func (s *boltSwapStore) FetchLoopInSwaps() ([]*LoopIn, error) {
return err
}
swaps = append(swaps, &LoopIn{
updates, err := deserializeUpdates(swapBucket)
if err != nil {
return err
}
loop := LoopIn{
Loop: Loop{
Events: updates,
},
Contract: contract,
Loop: loop,
})
}
loop.Hash, err = lntypes.MakeHash(swapHash)
if err != nil {
return err
}
swaps = append(swaps, &loop)
return nil
},
)
})
})
if err != nil {
return nil, err
}

Loading…
Cancel
Save