Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions liquidity/autoloop_testcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/loopdb/sqlc"
"github.com/lightninglabs/loop/swap"
"github.com/lightninglabs/loop/test"
"github.com/lightningnetwork/lnd/clock"
Expand Down Expand Up @@ -188,10 +189,14 @@ func newAutoloopTestCtx(t *testing.T, parameters Parameters,
MinimumConfirmations: loop.DefaultSweepConfTarget,
Lnd: &testCtx.lnd.LndServices,
Clock: testCtx.testClock,
PutLiquidityParams: func(_ context.Context, _ []byte) error {
PutLiquidityParams: func(_ context.Context, _ string,
_ []byte) error {

return nil
},
FetchLiquidityParams: func(context.Context) ([]byte, error) {
FetchLiquidityParams: func(context.Context) (
[]sqlc.LiquidityParam, error) {

return nil, nil
},
}
Expand Down
21 changes: 15 additions & 6 deletions liquidity/liquidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/labels"
"github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/loopdb/sqlc"
clientrpc "github.com/lightninglabs/loop/looprpc"
"github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/clock"
Expand Down Expand Up @@ -229,13 +230,15 @@ type Config struct {
//
// NOTE: the params are encoded using `proto.Marshal` over an RPC
// request.
PutLiquidityParams func(ctx context.Context, params []byte) error
PutLiquidityParams func(ctx context.Context, assetId string,
params []byte) error

// FetchLiquidityParams reads the serialized `Parameters` from db.
//
// NOTE: the params are decoded using `proto.Unmarshal` over a
// serialized RPC request.
FetchLiquidityParams func(ctx context.Context) ([]byte, error)
FetchLiquidityParams func(ctx context.Context) ([]sqlc.LiquidityParam,
error)
}

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

// Save the params on disk.
if err := m.cfg.PutLiquidityParams(ctx, paramsBytes); err != nil {
err = m.cfg.PutLiquidityParams(ctx, swap.DefaultBtcAssetID, paramsBytes)
if err != nil {
return fmt.Errorf("failed to save params: %v", err)
}

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

paramsBytes, err := m.cfg.FetchLiquidityParams(ctx)
params, err := m.cfg.FetchLiquidityParams(ctx)
if err != nil {
return nil, fmt.Errorf("failed to read params: %v", err)
}

// Return early if there's nothing saved.
if paramsBytes == nil {
if params == nil {
return nil, nil
}

if len(params) != 1 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can move this above to the other param check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think emptiness check should still come first (just as it is now).

return nil, fmt.Errorf("expected 1 param, got %v", len(params))
}

// Unmarshal the params.
req := &clientrpc.LiquidityParameters{}
err = proto.Unmarshal(paramsBytes, req)

err = proto.Unmarshal(params[0].Params, req)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal params: %v", err)
}
Expand Down
16 changes: 12 additions & 4 deletions liquidity/liquidity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/labels"
"github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/loopdb/sqlc"
clientrpc "github.com/lightninglabs/loop/looprpc"
"github.com/lightninglabs/loop/swap"
"github.com/lightninglabs/loop/test"
Expand Down Expand Up @@ -268,10 +269,12 @@ func TestPersistParams(t *testing.T) {

ctxb := context.Background()

var paramsBytes []byte
var paramsBytes []sqlc.LiquidityParam

// Mock the read method to return empty data.
manager.cfg.FetchLiquidityParams = func(context.Context) ([]byte, error) {
manager.cfg.FetchLiquidityParams = func(context.Context) (
[]sqlc.LiquidityParam, error) {

return paramsBytes, nil
}

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

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

paramsBytes = data
paramsBytes = []sqlc.LiquidityParam{
{
AssetID: swap.DefaultBtcAssetID,
Params: data,
},
}
return nil
}

Expand Down
6 changes: 4 additions & 2 deletions loopdb/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"github.com/lightninglabs/loop/loopdb/sqlc"
"github.com/lightningnetwork/lnd/lntypes"
)

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

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

// BatchUpdateLoopOutSwapCosts updates the swap costs for a batch of
// loop out swaps.
Expand Down
18 changes: 12 additions & 6 deletions loopdb/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"sort"

"github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -184,7 +185,9 @@ func (m *MigratorManager) migrateLiquidityParams(ctx context.Context) error {
}

// Put the liquidity parameters in the toStore.
err = m.toStore.PutLiquidityParams(ctx, params)
err = m.toStore.PutLiquidityParams(
ctx, swap.DefaultBtcAssetID, params[0].Params,
)
if err != nil {
return err
}
Expand Down Expand Up @@ -295,11 +298,14 @@ func (m *MigratorManager) checkLiquidityParams(ctx context.Context) error {
return err
}

// Check that the liquidity parameters are the same.
if !bytes.Equal(fromParams, toParams) {
return NewMigrationError(
fmt.Errorf("from: %v, to: %v", fromParams, toParams),
)
for i, fromParam := range fromParams {
// Check that the liquidity parameters are the same.
if !bytes.Equal(fromParam.Params, toParams[i].Params) {
return NewMigrationError(
fmt.Errorf("from: %v, to: %v", fromParams,
toParams),
)
}
}

return nil
Expand Down
15 changes: 10 additions & 5 deletions loopdb/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,15 @@ func (db *BaseDB) UpdateLoopIn(ctx context.Context, hash lntypes.Hash,
//
// NOTE: it's the caller's responsibility to encode the param. Atm,
// it's encoding using the proto package's `Marshal` method.
func (db *BaseDB) PutLiquidityParams(ctx context.Context,
func (db *BaseDB) PutLiquidityParams(ctx context.Context, assetId string,
params []byte) error {

err := db.Queries.UpsertLiquidityParams(ctx, params)
err := db.Queries.UpsertLiquidityParams(
ctx, sqlc.UpsertLiquidityParamsParams{
AssetID: assetId,
Params: params,
},
)
if err != nil {
return err
}
Expand All @@ -358,10 +363,10 @@ func (db *BaseDB) PutLiquidityParams(ctx context.Context,
//
// NOTE: it's the caller's responsibility to decode the param. Atm,
// it's decoding using the proto package's `Unmarshal` method.
func (db *BaseDB) FetchLiquidityParams(ctx context.Context) ([]byte,
error) {
func (db *BaseDB) FetchLiquidityParams(ctx context.Context) (
[]sqlc.LiquidityParam, error) {

var params []byte
var params []sqlc.LiquidityParam
params, err := db.Queries.FetchLiquidityParams(ctx)
if errors.Is(err, sql.ErrNoRows) {
return params, nil
Expand Down
7 changes: 4 additions & 3 deletions loopdb/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,17 @@ func TestSqliteLiquidityParams(t *testing.T) {
require.Empty(t, params, "expect empty bytes")
require.Nil(t, params, "expected nil byte array")

params = []byte("test")
insertParams := []byte("test")

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

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

// TestSqliteTypeConversion is a small test that checks that we can safely
Expand Down
47 changes: 34 additions & 13 deletions loopdb/sqlc/liquidity_params.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions loopdb/sqlc/migrations/000013_liquidity_assets.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ALTER TABLE liquidity_params RENAME TO liquidity_params_assets_backup;

CREATE TABLE liquidity_params (
id INTEGER PRIMARY KEY,
params BLOB
);

INSERT INTO liquidity_params (id, params)
SELECT 1, params
FROM liquidity_params_assets_backup
WHERE asset_id = 'btc';

DROP TABLE liquidity_params_assets_backup;
15 changes: 15 additions & 0 deletions loopdb/sqlc/migrations/000013_liquidity_assets.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Create a new table with the desired schema
CREATE TABLE new_liquidity_params (
asset_id TEXT NOT NULL PRIMARY KEY,
params BLOB
);

-- Copy data from the old table to the new table
INSERT INTO new_liquidity_params (asset_id, params)
SELECT 'btc', params FROM liquidity_params;

-- Drop the old table
DROP TABLE liquidity_params;

-- Rename the new table to the old table name
ALTER TABLE new_liquidity_params RENAME TO liquidity_params;
4 changes: 2 additions & 2 deletions loopdb/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions loopdb/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions loopdb/sqlc/queries/liquidity_params.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
-- name: UpsertLiquidityParams :exec
INSERT INTO liquidity_params (
id, params
asset_id, params
) VALUES (
1, $1
) ON CONFLICT (id) DO UPDATE SET
params = excluded.params;
$1, $2
) ON CONFLICT (asset_id) DO UPDATE SET
params = $2;

-- name: FetchLiquidityParams :one
SELECT params FROM liquidity_params WHERE id = 1;
-- name: FetchLiquidityParams :many
SELECT * FROM liquidity_params;
Loading
Loading