lightning-terminal/rules/manager_set.go

103 lines
2.6 KiB
Go
Raw Normal View History

2022-07-08 14:07:38 +02:00
package rules
import (
2025-01-13 07:02:42 +02:00
"context"
2022-07-08 14:07:38 +02:00
"encoding/json"
"fmt"
"github.com/lightninglabs/lightning-terminal/litrpc"
)
// ErrUnknownRule indicates that LiT is unaware of a values name.
var ErrUnknownRule = fmt.Errorf("unknown rule")
// LndConnIdLen is the length of the random connection ID used to create
// unique request identifiers.
const LndConnIdLen = 16
2022-07-08 14:07:38 +02:00
// ManagerSet is a map from a rule name to a rule Manager.
type ManagerSet map[string]Manager
// NewRuleManagerSet creates a new map of the supported rule ManagerSet.
func NewRuleManagerSet() ManagerSet {
2022-07-08 14:11:11 +02:00
return map[string]Manager{
2022-07-08 14:13:12 +02:00
RateLimitName: &RateLimitMgr{},
ChanPolicyBoundsName: &ChanPolicyBoundsMgr{},
2022-07-08 14:13:54 +02:00
HistoryLimitName: &HistoryLimitMgr{},
OnChainBudgetName: &OnChainBudgetMgr{},
2022-09-08 12:19:47 +02:00
ChannelRestrictName: NewChannelRestrictMgr(),
2022-09-08 12:20:03 +02:00
PeersRestrictName: NewPeerRestrictMgr(),
ChanConstraintName: &ChanConstraintMgr{},
2022-07-08 14:11:11 +02:00
}
2022-07-08 14:07:38 +02:00
}
// InitEnforcer gets the appropriate rule Manager for the given name and uses it
// to create an appropriate rule Enforcer.
2025-01-13 07:02:42 +02:00
func (m ManagerSet) InitEnforcer(ctx context.Context, cfg Config, name string,
2022-07-08 14:07:38 +02:00
values Values) (Enforcer, error) {
mgr, ok := m[name]
if !ok {
return nil, fmt.Errorf("%w %s, please upgrade", ErrUnknownRule,
name)
2022-07-08 14:07:38 +02:00
}
2025-01-13 07:02:42 +02:00
return mgr.NewEnforcer(ctx, cfg, values)
2022-07-08 14:07:38 +02:00
}
// GetAllRules returns a map of names of all the rules supported by rule
// ManagerSet.
func (m ManagerSet) GetAllRules() map[string]bool {
rules := make(map[string]bool, len(m))
for name := range m {
rules[name] = true
}
return rules
}
// UnmarshalRuleValues identifies the appropriate rule Manager based on the
// given rule name and uses that to parse the proto value into a Value object.
func (m ManagerSet) UnmarshalRuleValues(name string, proto *litrpc.RuleValue) (
Values, error) {
mgr, ok := m[name]
if !ok {
return nil, fmt.Errorf("%w %s, please upgrade", ErrUnknownRule,
name)
2022-07-08 14:07:38 +02:00
}
return mgr.NewValueFromProto(proto)
}
// InitRuleValues can be used to construct a Values object given raw rule
// value bytes along with the name of the appropriate rule.
func (m ManagerSet) InitRuleValues(name string, valueBytes []byte) (Values,
error) {
mgr, ok := m[name]
if !ok {
return nil, fmt.Errorf("%w %s, please upgrade", ErrUnknownRule,
name)
2022-07-08 14:07:38 +02:00
}
v := mgr.EmptyValue()
if err := json.Unmarshal(valueBytes, v); err != nil {
return nil, err
}
return v, nil
}
// Stop stops all the managers in the set.
func (m ManagerSet) Stop() error {
var returnErr error
for _, mgr := range m {
err := mgr.Stop()
if err != nil {
returnErr = err
}
}
return returnErr
}