55 "encoding/hex"
66 "encoding/json"
77 "fmt"
8- "strconv"
9- "strings"
108 "time"
119
1210 "github.com/lightninglabs/lightning-terminal/litrpc"
@@ -43,8 +41,9 @@ var addAutopilotSessionCmd = cli.Command{
4341 Description : `
4442 Initialize an Autopilot session.
4543
46- If set for any feature, configuration flags need to be repeated for each
47- feature that is registered, corresponding to the order of features.` ,
44+ If set for any feature, configuration flags and channel and peer
45+ restrict lists need to be repeated (empty if necessary) for each feature
46+ that is registered, corresponding to the order of features.` ,
4847 Action : initAutopilotSession ,
4948 Flags : []cli.Flag {
5049 labelFlag ,
@@ -55,19 +54,21 @@ var addAutopilotSessionCmd = cli.Command{
5554 Name : "feature" ,
5655 Required : true ,
5756 },
58- cli.StringFlag {
59- Name : "channel-restrict-list" ,
60- Usage : "list of channel IDs that the " +
61- "Autopilot server should not " +
62- "perform actions on. In the " +
63- "form of: chanID1,chanID2,..." ,
57+ cli.StringSliceFlag {
58+ Name : "feature-channel-restrict-list" ,
59+ Usage : `List of channel IDs that the ` +
60+ `Autopilot server should not ` +
61+ `perform actions on. In the ` +
62+ `form of: [chanID1,chanID2,...]. ` +
63+ `An empty list is set by using [].` ,
6464 },
65- cli.StringFlag {
66- Name : "peer-restrict-list" ,
67- Usage : "list of peer IDs that the " +
68- "Autopilot server should not " +
69- "perform actions on. In the " +
70- "form of: peerID1,peerID2,..." ,
65+ cli.StringSliceFlag {
66+ Name : "feature-peer-restrict-list" ,
67+ Usage : `List of peer IDs that the ` +
68+ `Autopilot server should not ` +
69+ `perform actions on. In the ` +
70+ `form of: ["peerID1","peerID2",...]. ` +
71+ `An empty list is set by using [].` ,
7172 },
7273 cli.StringFlag {
7374 Name : "group_id" ,
@@ -196,45 +197,77 @@ func initAutopilotSession(ctx *cli.Context) error {
196197 defer cleanup ()
197198 client := litrpc .NewAutopilotClient (clientConn )
198199
199- ruleMap := & litrpc.RulesMap {
200- Rules : make (map [string ]* litrpc.RuleValue ),
200+ features := ctx .StringSlice ("feature" )
201+
202+ perFeatureRules := make ([]* litrpc.RulesMap , len (features ))
203+ for i := range features {
204+ perFeatureRules [i ] = & litrpc.RulesMap {
205+ Rules : make (map [string ]* litrpc.RuleValue ),
206+ }
201207 }
202208
203- chanRestrictList := ctx .String ("channel-restrict-list" )
204- if chanRestrictList != "" {
209+ chanRestrictLists := ctx .StringSlice ("feature-channel-restrict-list" )
210+ if len (chanRestrictLists ) > 0 &&
211+ len (features ) != len (chanRestrictLists ) {
212+
213+ return fmt .Errorf ("number of features (%v) and channel " +
214+ "restrict list (%v) must match" , len (features ),
215+ len (chanRestrictLists ))
216+ }
217+
218+ for i , chanRestrictList := range chanRestrictLists {
205219 var chanIDs []uint64
206- chans := strings .Split (chanRestrictList , "," )
207- for _ , c := range chans {
208- i , err := strconv .ParseUint (c , 10 , 64 )
209- if err != nil {
210- return err
211- }
212- chanIDs = append (chanIDs , i )
220+ err := json .Unmarshal ([]byte (chanRestrictList ), & chanIDs )
221+ if err != nil {
222+ return fmt .Errorf ("could not parse " +
223+ "channel restrict list for feature %v: %v" ,
224+ features [i ], err )
213225 }
214226
215- ruleMap .Rules [rules .ChannelRestrictName ] = & litrpc.RuleValue {
216- Value : & litrpc.RuleValue_ChannelRestrict {
217- ChannelRestrict : & litrpc.ChannelRestrict {
218- ChannelIds : chanIDs ,
219- },
220- },
227+ if len (chanIDs ) == 0 {
228+ continue
221229 }
230+
231+ perFeatureRules [i ].Rules [rules .ChannelRestrictName ] =
232+ & litrpc.RuleValue {
233+ Value : & litrpc.RuleValue_ChannelRestrict {
234+ ChannelRestrict : & litrpc.ChannelRestrict {
235+ ChannelIds : chanIDs ,
236+ },
237+ },
238+ }
222239 }
223240
224- peerRestrictList := ctx .String ("peer-restrict-list" )
225- if peerRestrictList != "" {
226- peerIDs := strings .Split (peerRestrictList , "," )
241+ peerRestrictLists := ctx .StringSlice ("feature-peer-restrict-list" )
242+ if len (peerRestrictLists ) > 0 && len (features ) != len (peerRestrictLists ) {
243+ return fmt .Errorf ("number of features (%v) and peer " +
244+ "restrict list (%v) must match" , len (features ),
245+ len (peerRestrictLists ))
246+ }
227247
228- ruleMap .Rules [rules .PeersRestrictName ] = & litrpc.RuleValue {
229- Value : & litrpc.RuleValue_PeerRestrict {
230- PeerRestrict : & litrpc.PeerRestrict {
231- PeerIds : peerIDs ,
232- },
233- },
248+ for i , peerRestrictList := range peerRestrictLists {
249+ var peerIDs []string
250+ err := json .Unmarshal ([]byte (peerRestrictList ), & peerIDs )
251+ if err != nil {
252+ return fmt .Errorf ("could not parse " +
253+ "peer restrict list for feature %v: %v" ,
254+ features [i ], err )
234255 }
256+
257+ if len (peerIDs ) == 0 {
258+ continue
259+ }
260+
261+ perFeatureRules [i ].Rules [rules .PeersRestrictName ] =
262+ & litrpc.RuleValue {
263+ Value : & litrpc.RuleValue_PeerRestrict {
264+ PeerRestrict : & litrpc.PeerRestrict {
265+ PeerIds : peerIDs ,
266+ },
267+ },
268+ }
235269 }
236270
237- features := ctx .StringSlice ("feature" )
238271 configs := ctx .StringSlice ("feature-config" )
239272 if len (configs ) > 0 && len (features ) != len (configs ) {
240273 return fmt .Errorf ("number of features (%v) and configurations " +
@@ -262,7 +295,7 @@ func initAutopilotSession(ctx *cli.Context) error {
262295 }
263296
264297 featureMap [feature ] = & litrpc.FeatureConfig {
265- Rules : ruleMap ,
298+ Rules : perFeatureRules [ i ] ,
266299 Config : config ,
267300 }
268301 }
0 commit comments