2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-09 19:10:47 +00:00

loop: add peer rules to set rule command

This commit is contained in:
carla 2021-02-16 13:31:52 +02:00
parent 949e76bb2a
commit b9aae4f8f9
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91
2 changed files with 38 additions and 12 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"bytes"
"context" "context"
"errors" "errors"
"fmt" "fmt"
@ -8,6 +9,7 @@ import (
"github.com/lightninglabs/loop/liquidity" "github.com/lightninglabs/loop/liquidity"
"github.com/lightninglabs/loop/looprpc" "github.com/lightninglabs/loop/looprpc"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/urfave/cli" "github.com/urfave/cli"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
@ -42,9 +44,9 @@ func getParams(ctx *cli.Context) error {
var setLiquidityRuleCommand = cli.Command{ var setLiquidityRuleCommand = cli.Command{
Name: "setrule", Name: "setrule",
Usage: "set liquidity manager rule for a channel", Usage: "set liquidity manager rule for a channel/peer",
Description: "Update or remove the liquidity rule for a channel.", Description: "Update or remove the liquidity rule for a channel/peer.",
ArgsUsage: "shortchanid", ArgsUsage: "{shortchanid | peerpubkey}",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.IntFlag{ cli.IntFlag{
Name: "incoming_threshold", Name: "incoming_threshold",
@ -59,7 +61,8 @@ var setLiquidityRuleCommand = cli.Command{
}, },
cli.BoolFlag{ cli.BoolFlag{
Name: "clear", Name: "clear",
Usage: "remove the rule currently set for the channel.", Usage: "remove the rule currently set for the " +
"channel/peer.",
}, },
}, },
Action: setRule, Action: setRule,
@ -68,13 +71,22 @@ var setLiquidityRuleCommand = cli.Command{
func setRule(ctx *cli.Context) error { func setRule(ctx *cli.Context) error {
// We require that a channel ID is set for this rule update. // We require that a channel ID is set for this rule update.
if ctx.NArg() != 1 { if ctx.NArg() != 1 {
return fmt.Errorf("please set a channel id for the rule " + return fmt.Errorf("please set a channel id or peer pubkey " +
"update") "for the rule update")
} }
var (
pubkey route.Vertex
pubkeyRule bool
)
chanID, err := strconv.ParseUint(ctx.Args().First(), 10, 64) chanID, err := strconv.ParseUint(ctx.Args().First(), 10, 64)
if err != nil { if err != nil {
return fmt.Errorf("could not parse channel ID: %v", err) pubkey, err = route.NewVertexFromStr(ctx.Args().First())
if err != nil {
return fmt.Errorf("please provide a valid pubkey: "+
"%v, or short channel ID", err)
}
pubkeyRule = true
} }
client, cleanup, err := getClient(ctx) client, cleanup, err := getClient(ctx)
@ -101,11 +113,20 @@ func setRule(ctx *cli.Context) error {
) )
// Run through our current set of rules and check whether we have a rule // Run through our current set of rules and check whether we have a rule
// currently set for this channel. We also track a slice containing all // currently set for this channel or peer. We also track a slice
// of the rules we currently have set for other channels, because we // containing all of the rules we currently have set for other channels,
// want to leave these rules untouched. // and peers because we want to leave these rules untouched.
for _, rule := range params.Rules { for _, rule := range params.Rules {
if rule.ChannelId == chanID { var (
channelRuleSet = rule.ChannelId != 0 &&
rule.ChannelId == chanID
peerRuleSet = rule.Pubkey != nil && bytes.Equal(
rule.Pubkey, pubkey[:],
)
)
if channelRuleSet || peerRuleSet {
ruleSet = true ruleSet = true
} else { } else {
otherRules = append(otherRules, rule) otherRules = append(otherRules, rule)
@ -149,6 +170,10 @@ func setRule(ctx *cli.Context) error {
Type: looprpc.LiquidityRuleType_THRESHOLD, Type: looprpc.LiquidityRuleType_THRESHOLD,
} }
if pubkeyRule {
newRule.Pubkey = pubkey[:]
}
if inboundSet { if inboundSet {
newRule.IncomingThreshold = uint32( newRule.IncomingThreshold = uint32(
ctx.Int("incoming_threshold"), ctx.Int("incoming_threshold"),

View File

@ -609,6 +609,7 @@ func (s *swapClientServer) GetLiquidityParams(_ context.Context,
} }
for peer, rule := range cfg.PeerRules { for peer, rule := range cfg.PeerRules {
peer := peer
rpcRule := newRPCRule(0, peer[:], rule) rpcRule := newRPCRule(0, peer[:], rule)
rpcCfg.Rules = append(rpcCfg.Rules, rpcRule) rpcCfg.Rules = append(rpcCfg.Rules, rpcRule)
} }