Skip to content

Commit 676ba60

Browse files
authored
Merge pull request #113 from sputn1ck/recoverloopin_sqlite
`recoverloopin`: Sqlite option
2 parents 450c277 + 3e419da commit 676ba60

File tree

4 files changed

+938
-105
lines changed

4 files changed

+938
-105
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

cmd/chantools/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const (
3838

3939
// lndVersion is the current version of lnd that we support. This is
4040
// shown in some commands that affect the database and its migrations.
41-
lndVersion = "v0.17.0-beta"
41+
lndVersion = "v0.17.4-beta"
4242

4343
Commit = ""
4444
)

go.mod

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@ module github.com/lightninglabs/chantools
33
go 1.21
44

55
require (
6-
github.com/btcsuite/btcd v0.23.5-0.20230905170901-80f5a0ffdf36
6+
github.com/btcsuite/btcd v0.24.1-0.20240123000108-62e6af035ec5
77
github.com/btcsuite/btcd/btcec/v2 v2.3.2
8-
github.com/btcsuite/btcd/btcutil v1.1.4-0.20230904040416-d4f519f5dc05
8+
github.com/btcsuite/btcd/btcutil v1.1.5
99
github.com/btcsuite/btcd/btcutil/psbt v1.1.8
10-
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2
10+
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0
1111
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
12-
github.com/btcsuite/btcwallet v0.16.10-0.20230804184612-07be54bc22cf
12+
github.com/btcsuite/btcwallet v0.16.10-0.20240127010340-16b422a2e8bf
1313
github.com/btcsuite/btcwallet/wallet/txrules v1.2.0
1414
github.com/btcsuite/btcwallet/walletdb v1.4.0
1515
github.com/coreos/bbolt v1.3.3
1616
github.com/davecgh/go-spew v1.1.1
1717
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1
1818
github.com/gogo/protobuf v1.3.2 // indirect
1919
github.com/hasura/go-graphql-client v0.9.1
20-
github.com/lightninglabs/loop v0.23.0-beta
20+
github.com/lightninglabs/loop v0.26.6-beta
2121
github.com/lightninglabs/pool v0.6.2-beta.0.20230329135228-c3bffb52df3a
2222
// The current version of lnd we are compatible with, mostly affects the
2323
// commands that touch the channel DB and has an impact on the DB schema.
2424
// NOTE: When updating this version, make sure to also update the string in
2525
// cmd/chantools/root.go.
26-
github.com/lightningnetwork/lnd v0.17.0-beta
26+
github.com/lightningnetwork/lnd v0.17.4-beta
2727
github.com/lightningnetwork/lnd/kvdb v1.4.4
2828
github.com/lightningnetwork/lnd/queue v1.1.1
2929
github.com/lightningnetwork/lnd/ticker v1.1.1
@@ -38,10 +38,13 @@ require (
3838
require github.com/tv42/zbase32 v0.0.0-20220222190657-f76a9fc892fa
3939

4040
require (
41+
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
42+
github.com/Microsoft/go-winio v0.6.1 // indirect
43+
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
4144
github.com/Yawning/aez v0.0.0-20211027044916-e49e68abd344 // indirect
4245
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
4346
github.com/aead/siphash v1.0.1 // indirect
44-
github.com/andybalholm/brotli v1.0.3 // indirect
47+
github.com/andybalholm/brotli v1.0.4 // indirect
4548
github.com/beorn7/perks v1.0.1 // indirect
4649
github.com/btcsuite/btcwallet/wallet/txauthor v1.3.2 // indirect
4750
github.com/btcsuite/btcwallet/wallet/txsizes v1.2.3 // indirect
@@ -51,28 +54,38 @@ require (
5154
github.com/btcsuite/winsvc v1.0.0 // indirect
5255
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
5356
github.com/cespare/xxhash/v2 v2.2.0 // indirect
57+
github.com/containerd/continuity v0.3.0 // indirect
5458
github.com/coreos/go-semver v0.3.0 // indirect
5559
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
5660
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
5761
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
5862
github.com/decred/dcrd/lru v1.0.0 // indirect
63+
github.com/docker/cli v20.10.17+incompatible // indirect
64+
github.com/docker/docker v24.0.7+incompatible // indirect
65+
github.com/docker/go-connections v0.4.0 // indirect
66+
github.com/docker/go-units v0.5.0 // indirect
5967
github.com/dsnet/compress v0.0.1 // indirect
6068
github.com/dustin/go-humanize v1.0.0 // indirect
6169
github.com/fergusstrange/embedded-postgres v1.10.0 // indirect
6270
github.com/go-errors/errors v1.0.1 // indirect
6371
github.com/go-logr/logr v1.3.0 // indirect
6472
github.com/go-logr/stdr v1.2.2 // indirect
6573
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
74+
github.com/golang-migrate/migrate/v4 v4.16.1 // indirect
6675
github.com/golang/mock v1.6.0 // indirect
6776
github.com/golang/protobuf v1.5.3 // indirect
6877
github.com/golang/snappy v0.0.4 // indirect
6978
github.com/google/btree v1.0.1 // indirect
79+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
7080
github.com/google/uuid v1.3.1 // indirect
71-
github.com/gorilla/websocket v1.4.2 // indirect
81+
github.com/gorilla/websocket v1.5.0 // indirect
7282
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
7383
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
7484
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
75-
github.com/grpc-ecosystem/grpc-gateway/v2 v2.5.0 // indirect
85+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 // indirect
86+
github.com/hashicorp/errwrap v1.1.0 // indirect
87+
github.com/hashicorp/go-multierror v1.1.1 // indirect
88+
github.com/imdario/mergo v0.3.13 // indirect
7689
github.com/inconshreveable/mousetrap v1.0.0 // indirect
7790
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
7891
github.com/jackc/pgconn v1.14.3 // indirect
@@ -86,17 +99,17 @@ require (
8699
github.com/jessevdk/go-flags v1.4.0 // indirect
87100
github.com/jonboulle/clockwork v0.2.2 // indirect
88101
github.com/jrick/logrotate v1.0.0 // indirect
89-
github.com/json-iterator/go v1.1.11 // indirect
102+
github.com/json-iterator/go v1.1.12 // indirect
90103
github.com/juju/loggo v0.0.0-20210728185423-eebad3a902c4 // indirect
91104
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
92105
github.com/kkdai/bstream v1.0.0 // indirect
93106
github.com/klauspost/compress v1.16.0 // indirect
94107
github.com/klauspost/pgzip v1.2.5 // indirect
95-
github.com/lib/pq v1.10.3 // indirect
96-
github.com/lightninglabs/aperture v0.1.20-beta // indirect
108+
github.com/lib/pq v1.10.7 // indirect
109+
github.com/lightninglabs/aperture v0.1.21-beta.0.20230705004936-87bb996a4030 // indirect
97110
github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf // indirect
98-
github.com/lightninglabs/lndclient v0.16.0-10 // indirect
99-
github.com/lightninglabs/loop/swapserverrpc v1.0.4 // indirect
111+
github.com/lightninglabs/lndclient v0.17.4-1 // indirect
112+
github.com/lightninglabs/loop/swapserverrpc v1.0.5 // indirect
100113
github.com/lightninglabs/neutrino v0.16.0 // indirect
101114
github.com/lightninglabs/neutrino/cache v1.1.1 // indirect
102115
github.com/lightninglabs/pool/auctioneerrpc v1.0.7 // indirect
@@ -105,19 +118,26 @@ require (
105118
github.com/lightningnetwork/lnd/healthcheck v1.2.3 // indirect
106119
github.com/lightningnetwork/lnd/tlv v1.1.1 // indirect
107120
github.com/ltcsuite/ltcd v0.0.0-20191228044241-92166e412499 // indirect
108-
github.com/mattn/go-isatty v0.0.16 // indirect
109-
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
121+
github.com/mattn/go-isatty v0.0.17 // indirect
122+
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
110123
github.com/mholt/archiver/v3 v3.5.0 // indirect
111124
github.com/miekg/dns v1.1.43 // indirect
125+
github.com/mitchellh/mapstructure v1.4.1 // indirect
126+
github.com/moby/term v0.5.0 // indirect
112127
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
113-
github.com/modern-go/reflect2 v1.0.1 // indirect
128+
github.com/modern-go/reflect2 v1.0.2 // indirect
114129
github.com/nwaples/rardecode v1.1.2 // indirect
115-
github.com/pierrec/lz4/v4 v4.1.8 // indirect
130+
github.com/opencontainers/go-digest v1.0.0 // indirect
131+
github.com/opencontainers/image-spec v1.0.2 // indirect
132+
github.com/opencontainers/runc v1.1.12 // indirect
133+
github.com/ory/dockertest/v3 v3.10.0 // indirect
134+
github.com/pierrec/lz4/v4 v4.1.15 // indirect
135+
github.com/pkg/errors v0.9.1 // indirect
116136
github.com/pmezard/go-difflib v1.0.0 // indirect
117137
github.com/prometheus/client_golang v1.11.1 // indirect
118-
github.com/prometheus/client_model v0.2.0 // indirect
119-
github.com/prometheus/common v0.26.0 // indirect
120-
github.com/prometheus/procfs v0.6.0 // indirect
138+
github.com/prometheus/client_model v0.3.0 // indirect
139+
github.com/prometheus/common v0.30.0 // indirect
140+
github.com/prometheus/procfs v0.7.3 // indirect
121141
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
122142
github.com/rogpeppe/fastuuid v1.2.0 // indirect
123143
github.com/russross/blackfriday/v2 v2.0.1 // indirect
@@ -129,6 +149,9 @@ require (
129149
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
130150
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 // indirect
131151
github.com/ulikunitz/xz v0.5.11 // indirect
152+
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
153+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
154+
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
132155
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
133156
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
134157
gitlab.com/yawning/bsaes.git v0.0.0-20190805113838-0a714cd429ec // indirect
@@ -141,12 +164,13 @@ require (
141164
go.etcd.io/etcd/server/v3 v3.5.7 // indirect
142165
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.0 // indirect
143166
go.opentelemetry.io/otel v1.20.0 // indirect
144-
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.0.1 // indirect
145-
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.0.1 // indirect
167+
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.3.0 // indirect
168+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.3.0 // indirect
169+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.3.0 // indirect
146170
go.opentelemetry.io/otel/metric v1.20.0 // indirect
147-
go.opentelemetry.io/otel/sdk v1.0.1 // indirect
171+
go.opentelemetry.io/otel/sdk v1.3.0 // indirect
148172
go.opentelemetry.io/otel/trace v1.20.0 // indirect
149-
go.opentelemetry.io/proto/otlp v0.9.0 // indirect
173+
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
150174
go.uber.org/atomic v1.7.0 // indirect
151175
go.uber.org/multierr v1.6.0 // indirect
152176
go.uber.org/zap v1.17.0 // indirect
@@ -157,7 +181,7 @@ require (
157181
golang.org/x/sys v0.17.0 // indirect
158182
golang.org/x/term v0.17.0 // indirect
159183
golang.org/x/text v0.14.0 // indirect
160-
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
184+
golang.org/x/time v0.3.0 // indirect
161185
golang.org/x/tools v0.9.1 // indirect
162186
google.golang.org/appengine v1.6.7 // indirect
163187
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect
@@ -166,7 +190,7 @@ require (
166190
google.golang.org/grpc v1.59.0 // indirect
167191
google.golang.org/protobuf v1.31.0 // indirect
168192
gopkg.in/errgo.v1 v1.0.1 // indirect
169-
gopkg.in/macaroon-bakery.v2 v2.0.1 // indirect
193+
gopkg.in/macaroon-bakery.v2 v2.1.0 // indirect
170194
gopkg.in/macaroon.v2 v2.1.0 // indirect
171195
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
172196
gopkg.in/yaml.v2 v2.4.0 // indirect

0 commit comments

Comments
 (0)