Skip to content

Commit 61c0b45

Browse files
authored
Merge pull request #829 from bhandras/cost-migration-rpc-batchsize
loopd: allow setting RPC batch size when running cost migration
2 parents 5be1322 + 0e2a360 commit 61c0b45

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

cost_migration.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ import (
1616
const (
1717
// costMigrationID is the identifier for the cost migration.
1818
costMigrationID = "cost_migration"
19-
20-
// paymentBatchSize is the maximum number of payments we'll fetch in
21-
// one go.
22-
paymentBatchSize = 1000
2319
)
2420

2521
// CalculateLoopOutCost calculates the total cost of a loop out swap. It will
@@ -112,7 +108,7 @@ func CalculateLoopOutCost(params *chaincfg.Params, loopOutSwap *loopdb.LoopOut,
112108
// MigrateLoopOutCosts will calculate the correct cost for all loop out swaps
113109
// and override the cost values of the last update in the database.
114110
func MigrateLoopOutCosts(ctx context.Context, lnd lndclient.LndServices,
115-
db loopdb.SwapStore) error {
111+
paymentBatchSize int, db loopdb.SwapStore) error {
116112

117113
migrationDone, err := db.HasMigration(ctx, costMigrationID)
118114
if err != nil {
@@ -145,7 +141,7 @@ func MigrateLoopOutCosts(ctx context.Context, lnd lndclient.LndServices,
145141
payments, err := lnd.Client.ListPayments(
146142
ctx, lndclient.ListPaymentsRequest{
147143
Offset: offset,
148-
MaxPayments: paymentBatchSize,
144+
MaxPayments: uint64(paymentBatchSize),
149145
},
150146
)
151147
if err != nil {

cost_migration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func TestCostMigration(t *testing.T) {
158158
}
159159

160160
// Now we can run the migration.
161-
err = MigrateLoopOutCosts(context.Background(), lnd.LndServices, store)
161+
err = MigrateLoopOutCosts(context.Background(), lnd.LndServices, 1, store)
162162
require.NoError(t, err)
163163

164164
// Finally check that the swap cost has been updated correctly.
@@ -179,6 +179,6 @@ func TestCostMigration(t *testing.T) {
179179
// Now run the migration again to make sure it doesn't fail. This also
180180
// indicates that the migration did not run the second time as
181181
// otherwise the store mocks SetMigration function would fail.
182-
err = MigrateLoopOutCosts(context.Background(), lnd.LndServices, store)
182+
err = MigrateLoopOutCosts(context.Background(), lnd.LndServices, 1, store)
183183
require.NoError(t, err)
184184
}

loopd/config.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ var (
4848
defaultTotalPaymentTimeout = time.Minute * 60
4949
defaultMaxPaymentRetries = 3
5050

51+
// defaultRPCBatchSize is the default batch size to use for RPC calls
52+
// we make to LND during migrations. If operations on the LND side are
53+
// too slow a too large batch size may prevent the migration from
54+
// completing.
55+
defaultRPCBatchSize = 1000
56+
5157
// DefaultTLSCertFilename is the default file name for the autogenerated
5258
// TLS certificate.
5359
DefaultTLSCertFilename = "tls.cert"
@@ -179,6 +185,8 @@ type Config struct {
179185

180186
EnableExperimental bool `long:"experimental" description:"Enable experimental features: reservations"`
181187

188+
MigrationRPCBatchSize int `long:"migrationrpcbatchsize" description:"The RPC batch size to use during migrations."`
189+
182190
Lnd *lndConfig `group:"lnd" namespace:"lnd"`
183191

184192
Server *loopServerConfig `group:"server" namespace:"server"`
@@ -207,20 +215,21 @@ func DefaultConfig() Config {
207215
Sqlite: &loopdb.SqliteConfig{
208216
DatabaseFileName: defaultSqliteDatabasePath,
209217
},
210-
LogDir: defaultLogDir,
211-
MaxLogFiles: defaultMaxLogFiles,
212-
MaxLogFileSize: defaultMaxLogFileSize,
213-
DebugLevel: defaultLogLevel,
214-
TLSCertPath: DefaultTLSCertPath,
215-
TLSKeyPath: DefaultTLSKeyPath,
216-
TLSValidity: DefaultAutogenValidity,
217-
MacaroonPath: DefaultMacaroonPath,
218-
MaxL402Cost: l402.DefaultMaxCostSats,
219-
MaxL402Fee: l402.DefaultMaxRoutingFeeSats,
220-
LoopOutMaxParts: defaultLoopOutMaxParts,
221-
TotalPaymentTimeout: defaultTotalPaymentTimeout,
222-
MaxPaymentRetries: defaultMaxPaymentRetries,
223-
EnableExperimental: false,
218+
LogDir: defaultLogDir,
219+
MaxLogFiles: defaultMaxLogFiles,
220+
MaxLogFileSize: defaultMaxLogFileSize,
221+
DebugLevel: defaultLogLevel,
222+
TLSCertPath: DefaultTLSCertPath,
223+
TLSKeyPath: DefaultTLSKeyPath,
224+
TLSValidity: DefaultAutogenValidity,
225+
MacaroonPath: DefaultMacaroonPath,
226+
MaxL402Cost: l402.DefaultMaxCostSats,
227+
MaxL402Fee: l402.DefaultMaxRoutingFeeSats,
228+
LoopOutMaxParts: defaultLoopOutMaxParts,
229+
TotalPaymentTimeout: defaultTotalPaymentTimeout,
230+
MaxPaymentRetries: defaultMaxPaymentRetries,
231+
EnableExperimental: false,
232+
MigrationRPCBatchSize: defaultRPCBatchSize,
224233
Lnd: &lndConfig{
225234
Host: "localhost:10009",
226235
MacaroonPath: DefaultLndMacaroonPath,
@@ -367,6 +376,10 @@ func Validate(cfg *Config) error {
367376
return fmt.Errorf("TLS certificate minimum validity period is 24h")
368377
}
369378

379+
if cfg.MigrationRPCBatchSize <= 0 {
380+
return fmt.Errorf("migrationrpcbatchsize must be greater than 0")
381+
}
382+
370383
return nil
371384
}
372385

loopd/daemon.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,10 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
410410
}
411411

412412
// Run the costs migration.
413-
err = loop.MigrateLoopOutCosts(d.mainCtx, d.lnd.LndServices, swapDb)
413+
err = loop.MigrateLoopOutCosts(
414+
d.mainCtx, d.lnd.LndServices, d.cfg.MigrationRPCBatchSize,
415+
swapDb,
416+
)
414417
if err != nil {
415418
log.Errorf("Cost migration failed: %v", err)
416419

0 commit comments

Comments
 (0)