Skip to content

Commit a180f2a

Browse files
committed
loopdb: modify liquidity to use asset ids
1 parent bb859c5 commit a180f2a

19 files changed

+170
-65
lines changed

liquidity/autoloop_testcontext_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/lightninglabs/lndclient"
1111
"github.com/lightninglabs/loop"
1212
"github.com/lightninglabs/loop/loopdb"
13+
"github.com/lightninglabs/loop/loopdb/sqlc"
1314
"github.com/lightninglabs/loop/swap"
1415
"github.com/lightninglabs/loop/test"
1516
"github.com/lightningnetwork/lnd/clock"
@@ -188,10 +189,14 @@ func newAutoloopTestCtx(t *testing.T, parameters Parameters,
188189
MinimumConfirmations: loop.DefaultSweepConfTarget,
189190
Lnd: &testCtx.lnd.LndServices,
190191
Clock: testCtx.testClock,
191-
PutLiquidityParams: func(_ context.Context, _ []byte) error {
192+
PutLiquidityParams: func(_ context.Context, _ string,
193+
_ []byte) error {
194+
192195
return nil
193196
},
194-
FetchLiquidityParams: func(context.Context) ([]byte, error) {
197+
FetchLiquidityParams: func(context.Context) (
198+
[]sqlc.LiquidityParam, error) {
199+
195200
return nil, nil
196201
},
197202
}

liquidity/liquidity.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"github.com/lightninglabs/loop"
4747
"github.com/lightninglabs/loop/labels"
4848
"github.com/lightninglabs/loop/loopdb"
49+
"github.com/lightninglabs/loop/loopdb/sqlc"
4950
clientrpc "github.com/lightninglabs/loop/looprpc"
5051
"github.com/lightninglabs/loop/swap"
5152
"github.com/lightningnetwork/lnd/clock"
@@ -229,13 +230,15 @@ type Config struct {
229230
//
230231
// NOTE: the params are encoded using `proto.Marshal` over an RPC
231232
// request.
232-
PutLiquidityParams func(ctx context.Context, params []byte) error
233+
PutLiquidityParams func(ctx context.Context, assetId string,
234+
params []byte) error
233235

234236
// FetchLiquidityParams reads the serialized `Parameters` from db.
235237
//
236238
// NOTE: the params are decoded using `proto.Unmarshal` over a
237239
// serialized RPC request.
238-
FetchLiquidityParams func(ctx context.Context) ([]byte, error)
240+
FetchLiquidityParams func(ctx context.Context) ([]sqlc.LiquidityParam,
241+
error)
239242
}
240243

241244
// Manager contains a set of desired liquidity rules for our channel
@@ -392,7 +395,8 @@ func (m *Manager) saveParams(ctx context.Context, req proto.Message) error {
392395
}
393396

394397
// Save the params on disk.
395-
if err := m.cfg.PutLiquidityParams(ctx, paramsBytes); err != nil {
398+
err = m.cfg.PutLiquidityParams(ctx, swap.DefaultBtcAssetID, paramsBytes)
399+
if err != nil {
396400
return fmt.Errorf("failed to save params: %v", err)
397401
}
398402

@@ -404,19 +408,24 @@ func (m *Manager) saveParams(ctx context.Context, req proto.Message) error {
404408
func (m *Manager) loadParams(ctx context.Context) (
405409
*clientrpc.LiquidityParameters, error) {
406410

407-
paramsBytes, err := m.cfg.FetchLiquidityParams(ctx)
411+
params, err := m.cfg.FetchLiquidityParams(ctx)
408412
if err != nil {
409413
return nil, fmt.Errorf("failed to read params: %v", err)
410414
}
411415

412416
// Return early if there's nothing saved.
413-
if paramsBytes == nil {
417+
if params == nil {
414418
return nil, nil
415419
}
416420

421+
if len(params) != 1 {
422+
return nil, fmt.Errorf("expected 1 param, got %v", len(params))
423+
}
424+
417425
// Unmarshal the params.
418426
req := &clientrpc.LiquidityParameters{}
419-
err = proto.Unmarshal(paramsBytes, req)
427+
428+
err = proto.Unmarshal(params[0].Params, req)
420429
if err != nil {
421430
return nil, fmt.Errorf("failed to unmarshal params: %v", err)
422431
}

liquidity/liquidity_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/lightninglabs/loop"
1111
"github.com/lightninglabs/loop/labels"
1212
"github.com/lightninglabs/loop/loopdb"
13+
"github.com/lightninglabs/loop/loopdb/sqlc"
1314
clientrpc "github.com/lightninglabs/loop/looprpc"
1415
"github.com/lightninglabs/loop/swap"
1516
"github.com/lightninglabs/loop/test"
@@ -268,10 +269,12 @@ func TestPersistParams(t *testing.T) {
268269

269270
ctxb := context.Background()
270271

271-
var paramsBytes []byte
272+
var paramsBytes []sqlc.LiquidityParam
272273

273274
// Mock the read method to return empty data.
274-
manager.cfg.FetchLiquidityParams = func(context.Context) ([]byte, error) {
275+
manager.cfg.FetchLiquidityParams = func(context.Context) (
276+
[]sqlc.LiquidityParam, error) {
277+
275278
return paramsBytes, nil
276279
}
277280

@@ -282,9 +285,14 @@ func TestPersistParams(t *testing.T) {
282285

283286
// Mock the write method to return no error.
284287
manager.cfg.PutLiquidityParams = func(ctx context.Context,
285-
data []byte) error {
288+
assetId string, data []byte) error {
286289

287-
paramsBytes = data
290+
paramsBytes = []sqlc.LiquidityParam{
291+
{
292+
AssetID: swap.DefaultBtcAssetID,
293+
Params: data,
294+
},
295+
}
288296
return nil
289297
}
290298

loopdb/interface.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"time"
66

7+
"github.com/lightninglabs/loop/loopdb/sqlc"
78
"github.com/lightningnetwork/lnd/lntypes"
89
)
910

@@ -61,14 +62,15 @@ type SwapStore interface {
6162
//
6263
// NOTE: it's the caller's responsibility to encode the param. Atm,
6364
// it's encoding using the proto package's `Marshal` method.
64-
PutLiquidityParams(ctx context.Context, params []byte) error
65+
PutLiquidityParams(ctx context.Context, assetId string,
66+
params []byte) error
6567

6668
// FetchLiquidityParams reads the serialized `manager.Parameters` bytes
6769
// from the bucket.
6870
//
6971
// NOTE: it's the caller's responsibility to decode the param. Atm,
7072
// it's decoding using the proto package's `Unmarshal` method.
71-
FetchLiquidityParams(ctx context.Context) ([]byte, error)
73+
FetchLiquidityParams(ctx context.Context) ([]sqlc.LiquidityParam, error)
7274

7375
// BatchUpdateLoopOutSwapCosts updates the swap costs for a batch of
7476
// loop out swaps.

loopdb/migrate.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"sort"
99

10+
"github.com/lightninglabs/loop/swap"
1011
"github.com/lightningnetwork/lnd/lntypes"
1112
"github.com/stretchr/testify/require"
1213
)
@@ -184,7 +185,9 @@ func (m *MigratorManager) migrateLiquidityParams(ctx context.Context) error {
184185
}
185186

186187
// Put the liquidity parameters in the toStore.
187-
err = m.toStore.PutLiquidityParams(ctx, params)
188+
err = m.toStore.PutLiquidityParams(
189+
ctx, swap.DefaultBtcAssetID, params[0].Params,
190+
)
188191
if err != nil {
189192
return err
190193
}
@@ -295,11 +298,14 @@ func (m *MigratorManager) checkLiquidityParams(ctx context.Context) error {
295298
return err
296299
}
297300

298-
// Check that the liquidity parameters are the same.
299-
if !bytes.Equal(fromParams, toParams) {
300-
return NewMigrationError(
301-
fmt.Errorf("from: %v, to: %v", fromParams, toParams),
302-
)
301+
for i, fromParam := range fromParams {
302+
// Check that the liquidity parameters are the same.
303+
if !bytes.Equal(fromParam.Params, toParams[i].Params) {
304+
return NewMigrationError(
305+
fmt.Errorf("from: %v, to: %v", fromParams,
306+
toParams),
307+
)
308+
}
303309
}
304310

305311
return nil

loopdb/sql_store.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,15 @@ func (db *BaseDB) UpdateLoopIn(ctx context.Context, hash lntypes.Hash,
342342
//
343343
// NOTE: it's the caller's responsibility to encode the param. Atm,
344344
// it's encoding using the proto package's `Marshal` method.
345-
func (db *BaseDB) PutLiquidityParams(ctx context.Context,
345+
func (db *BaseDB) PutLiquidityParams(ctx context.Context, assetId string,
346346
params []byte) error {
347347

348-
err := db.Queries.UpsertLiquidityParams(ctx, params)
348+
err := db.Queries.UpsertLiquidityParams(
349+
ctx, sqlc.UpsertLiquidityParamsParams{
350+
AssetID: assetId,
351+
Params: params,
352+
},
353+
)
349354
if err != nil {
350355
return err
351356
}
@@ -358,10 +363,10 @@ func (db *BaseDB) PutLiquidityParams(ctx context.Context,
358363
//
359364
// NOTE: it's the caller's responsibility to decode the param. Atm,
360365
// it's decoding using the proto package's `Unmarshal` method.
361-
func (db *BaseDB) FetchLiquidityParams(ctx context.Context) ([]byte,
362-
error) {
366+
func (db *BaseDB) FetchLiquidityParams(ctx context.Context) (
367+
[]sqlc.LiquidityParam, error) {
363368

364-
var params []byte
369+
var params []sqlc.LiquidityParam
365370
params, err := db.Queries.FetchLiquidityParams(ctx)
366371
if errors.Is(err, sql.ErrNoRows) {
367372
return params, nil

loopdb/sql_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,16 +317,17 @@ func TestSqliteLiquidityParams(t *testing.T) {
317317
require.Empty(t, params, "expect empty bytes")
318318
require.Nil(t, params, "expected nil byte array")
319319

320-
params = []byte("test")
320+
insertParams := []byte("test")
321321

322322
// Test we can save the params.
323-
err = store.PutLiquidityParams(ctxb, params)
323+
err = store.PutLiquidityParams(ctxb, "test", insertParams)
324324
require.NoError(t, err, "failed to put params")
325325

326326
// Now fetch the db again should return the above saved bytes.
327327
paramsRead, err := store.FetchLiquidityParams(ctxb)
328328
require.NoError(t, err, "failed to fetch params")
329-
require.Equal(t, params, paramsRead, "unexpected return value")
329+
require.Equal(t, insertParams, paramsRead[0].Params,
330+
"unexpected return value")
330331
}
331332

332333
// TestSqliteTypeConversion is a small test that checks that we can safely

loopdb/sqlc/liquidity_params.sql.go

Lines changed: 34 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
ALTER TABLE liquidity_params RENAME TO liquidity_params_assets_backup;
2+
3+
CREATE TABLE liquidity_params (
4+
id INTEGER PRIMARY KEY,
5+
params BLOB
6+
);
7+
8+
INSERT INTO liquidity_params (id, params)
9+
SELECT 1, params
10+
FROM liquidity_params_assets_backup
11+
WHERE asset_id = 'btc';
12+
13+
DROP TABLE liquidity_params_assets_backup;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Create a new table with the desired schema
2+
CREATE TABLE new_liquidity_params (
3+
asset_id TEXT NOT NULL PRIMARY KEY,
4+
params BLOB
5+
);
6+
7+
-- Copy data from the old table to the new table
8+
INSERT INTO new_liquidity_params (asset_id, params)
9+
SELECT 'btc', params FROM liquidity_params;
10+
11+
-- Drop the old table
12+
DROP TABLE liquidity_params;
13+
14+
-- Rename the new table to the old table name
15+
ALTER TABLE new_liquidity_params RENAME TO liquidity_params;

0 commit comments

Comments
 (0)