Skip to content

Commit b9aae4f

Browse files
committed
loop: add peer rules to set rule command
1 parent 949e76b commit b9aae4f

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

cmd/loop/liquidity.go

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package main
22

33
import (
4+
"bytes"
45
"context"
56
"errors"
67
"fmt"
78
"strconv"
89

910
"github.com/lightninglabs/loop/liquidity"
1011
"github.com/lightninglabs/loop/looprpc"
12+
"github.com/lightningnetwork/lnd/routing/route"
1113
"github.com/urfave/cli"
1214
"google.golang.org/grpc/codes"
1315
"google.golang.org/grpc/status"
@@ -42,9 +44,9 @@ func getParams(ctx *cli.Context) error {
4244

4345
var setLiquidityRuleCommand = cli.Command{
4446
Name: "setrule",
45-
Usage: "set liquidity manager rule for a channel",
46-
Description: "Update or remove the liquidity rule for a channel.",
47-
ArgsUsage: "shortchanid",
47+
Usage: "set liquidity manager rule for a channel/peer",
48+
Description: "Update or remove the liquidity rule for a channel/peer.",
49+
ArgsUsage: "{shortchanid | peerpubkey}",
4850
Flags: []cli.Flag{
4951
cli.IntFlag{
5052
Name: "incoming_threshold",
@@ -58,8 +60,9 @@ var setLiquidityRuleCommand = cli.Command{
5860
"that we do not want to drop below.",
5961
},
6062
cli.BoolFlag{
61-
Name: "clear",
62-
Usage: "remove the rule currently set for the channel.",
63+
Name: "clear",
64+
Usage: "remove the rule currently set for the " +
65+
"channel/peer.",
6366
},
6467
},
6568
Action: setRule,
@@ -68,13 +71,22 @@ var setLiquidityRuleCommand = cli.Command{
6871
func setRule(ctx *cli.Context) error {
6972
// We require that a channel ID is set for this rule update.
7073
if ctx.NArg() != 1 {
71-
return fmt.Errorf("please set a channel id for the rule " +
72-
"update")
74+
return fmt.Errorf("please set a channel id or peer pubkey " +
75+
"for the rule update")
7376
}
7477

78+
var (
79+
pubkey route.Vertex
80+
pubkeyRule bool
81+
)
7582
chanID, err := strconv.ParseUint(ctx.Args().First(), 10, 64)
7683
if err != nil {
77-
return fmt.Errorf("could not parse channel ID: %v", err)
84+
pubkey, err = route.NewVertexFromStr(ctx.Args().First())
85+
if err != nil {
86+
return fmt.Errorf("please provide a valid pubkey: "+
87+
"%v, or short channel ID", err)
88+
}
89+
pubkeyRule = true
7890
}
7991

8092
client, cleanup, err := getClient(ctx)
@@ -101,11 +113,20 @@ func setRule(ctx *cli.Context) error {
101113
)
102114

103115
// Run through our current set of rules and check whether we have a rule
104-
// currently set for this channel. We also track a slice containing all
105-
// of the rules we currently have set for other channels, because we
106-
// want to leave these rules untouched.
116+
// currently set for this channel or peer. We also track a slice
117+
// containing all of the rules we currently have set for other channels,
118+
// and peers because we want to leave these rules untouched.
107119
for _, rule := range params.Rules {
108-
if rule.ChannelId == chanID {
120+
var (
121+
channelRuleSet = rule.ChannelId != 0 &&
122+
rule.ChannelId == chanID
123+
124+
peerRuleSet = rule.Pubkey != nil && bytes.Equal(
125+
rule.Pubkey, pubkey[:],
126+
)
127+
)
128+
129+
if channelRuleSet || peerRuleSet {
109130
ruleSet = true
110131
} else {
111132
otherRules = append(otherRules, rule)
@@ -149,6 +170,10 @@ func setRule(ctx *cli.Context) error {
149170
Type: looprpc.LiquidityRuleType_THRESHOLD,
150171
}
151172

173+
if pubkeyRule {
174+
newRule.Pubkey = pubkey[:]
175+
}
176+
152177
if inboundSet {
153178
newRule.IncomingThreshold = uint32(
154179
ctx.Int("incoming_threshold"),

loopd/swapclient_server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ func (s *swapClientServer) GetLiquidityParams(_ context.Context,
609609
}
610610

611611
for peer, rule := range cfg.PeerRules {
612+
peer := peer
612613
rpcRule := newRPCRule(0, peer[:], rule)
613614
rpcCfg.Rules = append(rpcCfg.Rules, rpcRule)
614615
}

0 commit comments

Comments
 (0)