Skip to content

Commit 3e419da

Browse files
sputn1ckguggero
authored andcommitted
recoverloopin: add sqlite option
This commit will allow to recover loop ins that have been made with sqlite.
1 parent 6a81614 commit 3e419da

File tree

1 file changed

+78
-16
lines changed

1 file changed

+78
-16
lines changed

cmd/chantools/recoverloopin.go

Lines changed: 78 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package main
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/hex"
7+
"errors"
68
"fmt"
9+
"path/filepath"
10+
"time"
711

812
"github.com/btcsuite/btcd/btcutil"
913
"github.com/btcsuite/btcd/chaincfg/chainhash"
@@ -15,10 +19,15 @@ import (
1519
"github.com/lightninglabs/loop/swap"
1620
"github.com/lightningnetwork/lnd/input"
1721
"github.com/lightningnetwork/lnd/keychain"
22+
"github.com/lightningnetwork/lnd/lnrpc"
1823
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
1924
"github.com/spf13/cobra"
2025
)
2126

27+
var (
28+
errSwapNotFound = fmt.Errorf("loop in swap not found")
29+
)
30+
2231
type recoverLoopInCommand struct {
2332
TxID string
2433
Vout uint32
@@ -32,7 +41,8 @@ type recoverLoopInCommand struct {
3241
APIURL string
3342
Publish bool
3443

35-
LoopDbDir string
44+
LoopDbDir string
45+
SqliteFile string
3646

3747
rootKey *rootKey
3848
cmd *cobra.Command
@@ -97,6 +107,11 @@ func newRecoverLoopInCommand() *cobra.Command {
97107
cc.cmd.Flags().Uint64Var(
98108
&cc.OutputAmt, "output_amt", 0, "amount of the output to sweep",
99109
)
110+
cc.cmd.Flags().StringVar(
111+
&cc.SqliteFile, "sqlite_file", "", "optional path to the loop "+
112+
"sqlite database file, if not specified, the default "+
113+
"location will be loaded from --loop_db_dir",
114+
)
100115

101116
cc.rootKey = newRootKey(cc.cmd, "deriving starting key")
102117

@@ -130,32 +145,62 @@ func (c *recoverLoopInCommand) Execute(_ *cobra.Command, _ []string) error {
130145
}
131146

132147
api := newExplorerAPI(c.APIURL)
148+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
149+
defer cancel()
150+
133151
signer := &lnd.Signer{
134152
ExtendedKey: extendedKey,
135153
ChainParams: chainParams,
136154
}
137155

138-
// Try to fetch the swap from the database.
139-
store, err := loopdb.NewBoltSwapStore(c.LoopDbDir, chainParams)
140-
if err != nil {
141-
return err
142-
}
143-
defer store.Close()
156+
// Try to fetch the swap from the boltdb.
157+
var (
158+
store loopdb.SwapStore
159+
loopIn *loopdb.LoopIn
160+
)
144161

145-
swaps, err := store.FetchLoopInSwaps()
146-
if err != nil {
147-
return err
162+
// First check if a boltdb file exists.
163+
if lnrpc.FileExists(filepath.Join(c.LoopDbDir, "loop.db")) {
164+
store, err = loopdb.NewBoltSwapStore(c.LoopDbDir, chainParams)
165+
if err != nil {
166+
return err
167+
}
168+
defer store.Close()
169+
170+
loopIn, err = findLoopInSwap(ctx, store, c.SwapHash)
171+
if err != nil && !errors.Is(err, errSwapNotFound) {
172+
return err
173+
}
148174
}
149175

150-
var loopIn *loopdb.LoopIn
151-
for _, s := range swaps {
152-
if s.Hash.String() == c.SwapHash {
153-
loopIn = s
154-
break
176+
// If the loopin is not found yet, try to fetch it from the sqlite db.
177+
if loopIn == nil {
178+
if c.SqliteFile == "" {
179+
c.SqliteFile = filepath.Join(
180+
c.LoopDbDir, "loop_sqlite.db",
181+
)
182+
}
183+
184+
sqliteDb, err := loopdb.NewSqliteStore(
185+
&loopdb.SqliteConfig{
186+
DatabaseFileName: c.SqliteFile,
187+
SkipMigrations: true,
188+
}, chainParams,
189+
)
190+
if err != nil {
191+
return err
192+
}
193+
defer sqliteDb.Close()
194+
195+
loopIn, err = findLoopInSwap(ctx, sqliteDb, c.SwapHash)
196+
if err != nil && !errors.Is(err, errSwapNotFound) {
197+
return err
155198
}
156199
}
200+
201+
// If the loopin is still not found, return an error.
157202
if loopIn == nil {
158-
return fmt.Errorf("swap not found")
203+
return errSwapNotFound
159204
}
160205

161206
// If the swap is an external htlc, we require the output amount to be
@@ -350,6 +395,23 @@ func getSignedTx(signer *lnd.Signer, sweepTx *wire.MsgTx, htlc *swap.Htlc,
350395
return rawTx, nil
351396
}
352397

398+
func findLoopInSwap(ctx context.Context, store loopdb.SwapStore,
399+
swapHash string) (*loopdb.LoopIn, error) {
400+
401+
swaps, err := store.FetchLoopInSwaps(ctx)
402+
if err != nil {
403+
return nil, err
404+
}
405+
406+
for _, s := range swaps {
407+
if s.Hash.String() == swapHash {
408+
return s, nil
409+
}
410+
}
411+
412+
return nil, errSwapNotFound
413+
}
414+
353415
// encodeTx encodes a tx to raw bytes.
354416
func encodeTx(tx *wire.MsgTx) ([]byte, error) {
355417
var buffer bytes.Buffer

0 commit comments

Comments
 (0)