status-go/cmd/statusd/topics/topics.go

53 lines
1.2 KiB
Go
Raw Normal View History

2018-04-09 06:26:06 +00:00
package topics
2018-04-10 06:44:09 +00:00
import (
"errors"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/p2p/discv5"
"github.com/status-im/status-go/params"
2018-04-10 06:44:09 +00:00
)
2018-04-09 06:26:06 +00:00
// TopicFlag used to parse discv5 topics.
type TopicFlag []discv5.Topic
2018-04-10 06:44:09 +00:00
2018-04-09 06:26:06 +00:00
func (f *TopicFlag) String() string {
2018-04-10 06:44:09 +00:00
return "discv5 topics"
}
2018-04-09 06:26:06 +00:00
// Set parses single topic.
func (f *TopicFlag) Set(value string) error {
2018-04-10 06:44:09 +00:00
*f = append(*f, discv5.Topic(strings.TrimSpace(value)))
return nil
}
2018-04-09 06:26:06 +00:00
// TopicLimitsFlag used to parse limits for discv5 topics.
type TopicLimitsFlag map[discv5.Topic]params.Limits
2018-04-10 06:44:09 +00:00
2018-04-09 06:26:06 +00:00
func (f *TopicLimitsFlag) String() string {
2018-04-10 06:44:09 +00:00
return "disv5 topics to limits map"
}
2018-04-09 06:26:06 +00:00
// Set parses single limit for a topic.
func (f *TopicLimitsFlag) Set(value string) error {
2018-04-10 06:44:09 +00:00
parts := strings.Split(strings.TrimSpace(value), "=")
if len(parts) != 2 {
return errors.New("topic must be separated by '=' from limits, e.g. 'topic1=1,1'")
}
limits := strings.Split(parts[1], ",")
if len(limits) != 2 {
return errors.New("min and max limit must be set, e.g. 'topic1=1,1'")
}
minL, err := strconv.Atoi(limits[0])
if err != nil {
return err
}
maxL, err := strconv.Atoi(limits[1])
if err != nil {
return err
}
2018-04-20 07:39:31 +00:00
(*f)[discv5.Topic(parts[0])] = params.NewLimits(minL, maxL)
2018-04-10 06:44:09 +00:00
return nil
}