Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset Autoloop [2/x] looprpc: add asset id to params requests #878

Closed
wants to merge 2 commits into from
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
20 changes: 18 additions & 2 deletions cmd/loop/liquidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/lightninglabs/loop/liquidity"
"github.com/lightninglabs/loop/looprpc"
"github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/urfave/cli"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -106,7 +107,7 @@ func setRule(ctx *cli.Context) error {
// We need to set the full set of current parameters every time we call
// SetParameters. To allow users to set only individual fields on the
// cli, we lookup our current params, then update individual values.
params, err := client.GetLiquidityParams(
paramsResponse, err := client.GetLiquidityParams(
context.Background(), &looprpc.GetLiquidityParamsRequest{},
)
if err != nil {
Expand All @@ -120,6 +121,12 @@ func setRule(ctx *cli.Context) error {
otherRules []*looprpc.LiquidityRule
)

// We're only interested in the btc rules for now.
params, ok := paramsResponse.Rules[swap.DefaultBtcAssetID]
if !ok {
return errors.New("no rules set for BTC")
}

// Run through our current set of rules and check whether we have a rule
// currently set for this channel or peer. We also track a slice
// containing all of the rules we currently have set for other channels,
Expand Down Expand Up @@ -160,6 +167,7 @@ func setRule(ctx *cli.Context) error {
context.Background(),
&looprpc.SetLiquidityParamsRequest{
Parameters: params,
AssetId: swap.DefaultBtcAssetID,
},
)
return err
Expand Down Expand Up @@ -216,6 +224,7 @@ func setRule(ctx *cli.Context) error {
context.Background(),
&looprpc.SetLiquidityParamsRequest{
Parameters: params,
AssetId: swap.DefaultBtcAssetID,
},
)

Expand Down Expand Up @@ -364,13 +373,19 @@ func setParams(ctx *cli.Context) error {
// We need to set the full set of current parameters every time we call
// SetParameters. To allow users to set only individual fields on the
// cli, we lookup our current params, then update individual values.
params, err := client.GetLiquidityParams(
paramsResponse, err := client.GetLiquidityParams(
context.Background(), &looprpc.GetLiquidityParamsRequest{},
)
if err != nil {
return err
}

// We're only interested in the btc rules for now.
params, ok := paramsResponse.Rules[swap.DefaultBtcAssetID]
if !ok {
return errors.New("no rules set for BTC")
}

var flagSet, categoriesSet, feePercentSet bool

// Update our existing parameters with the values provided by cli flags.
Expand Down Expand Up @@ -556,6 +571,7 @@ func setParams(ctx *cli.Context) error {
_, err = client.SetLiquidityParams(
context.Background(), &looprpc.SetLiquidityParamsRequest{
Parameters: params,
AssetId: swap.DefaultBtcAssetID,
},
)

Expand Down
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 {
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
10 changes: 7 additions & 3 deletions loopd/swapclient_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1224,8 +1224,8 @@ func (s *swapClientServer) GetInfo(ctx context.Context,

// GetLiquidityParams gets our current liquidity manager's parameters.
func (s *swapClientServer) GetLiquidityParams(_ context.Context,
_ *looprpc.GetLiquidityParamsRequest) (*looprpc.LiquidityParameters,
error) {
_ *looprpc.GetLiquidityParamsRequest) (
*looprpc.GetLiquidityParamsResponse, error) {

cfg := s.liquidityMgr.GetParameters()

Expand All @@ -1234,7 +1234,11 @@ func (s *swapClientServer) GetLiquidityParams(_ context.Context,
return nil, err
}

return rpcCfg, nil
return &looprpc.GetLiquidityParamsResponse{
Rules: map[string]*looprpc.LiquidityParameters{
swap.DefaultBtcAssetID: rpcCfg,
},
}, nil
}

// SetLiquidityParams attempts to set our current liquidity manager's
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
Loading
Loading