mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-17 04:30:03 +02:00
internal/config: adding blacklist field to Ship config
This commit is contained in:
parent
93a816cb2c
commit
6d14591f6b
4 changed files with 75 additions and 6 deletions
|
|
@ -2,9 +2,13 @@ package config
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/eosswedenorg/thalos/internal/types"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/spf13/viper"
|
||||
|
|
@ -48,6 +52,7 @@ func NewBuilder() *Builder {
|
|||
"ship.irreversible_only": "irreversible-only",
|
||||
"ship.max_messages_in_flight": "max-msg-in-flight",
|
||||
"ship.chain": "chain",
|
||||
"ship.blacklist": "blacklist",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
@ -110,6 +115,33 @@ func (b *Builder) Build() (*Config, error) {
|
|||
mapstructure.TextUnmarshallerHookFunc(),
|
||||
mapstructure.StringToTimeDurationHookFunc(),
|
||||
mapstructure.StringToSliceHookFunc(","),
|
||||
func(f reflect.Type, t reflect.Type, in interface{}) (interface{}, error) {
|
||||
if t == reflect.TypeOf(types.Blacklist{}) && f.Kind() == reflect.Slice {
|
||||
if v, ok := in.([]string); ok {
|
||||
list := types.Blacklist{}
|
||||
for _, i := range v {
|
||||
var action string
|
||||
parts := strings.SplitN(i, ":", 2)
|
||||
|
||||
if len(parts) < 2 {
|
||||
action = "*"
|
||||
} else {
|
||||
action = parts[1]
|
||||
}
|
||||
|
||||
list.Add(parts[0], action)
|
||||
}
|
||||
|
||||
if len(list) < 1 {
|
||||
list = nil
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
return nil, fmt.Errorf("Must be a string slice")
|
||||
}
|
||||
|
||||
return in, nil
|
||||
},
|
||||
)
|
||||
|
||||
err := v.Unmarshal(&conf, viper.DecodeHook(decoders))
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/eosswedenorg/thalos/internal/log"
|
||||
"github.com/eosswedenorg/thalos/internal/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
|
|
@ -27,6 +28,10 @@ func TestBuilder(t *testing.T) {
|
|||
EndBlockNum: 23872222,
|
||||
IrreversibleOnly: true,
|
||||
MaxMessagesInFlight: 1337,
|
||||
Blacklist: types.Blacklist{
|
||||
"eosio": {"noop"},
|
||||
"contract": {"skip1", "skip2"},
|
||||
},
|
||||
},
|
||||
Telegram: TelegramConfig{
|
||||
Id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
|
||||
|
|
@ -58,6 +63,11 @@ ship:
|
|||
max_messages_in_flight: 1337
|
||||
start_block_num: 23671836
|
||||
end_block_num: 23872222
|
||||
blacklist:
|
||||
eosio: ["noop"]
|
||||
contract:
|
||||
- skip1
|
||||
- skip2
|
||||
telegram:
|
||||
id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw"
|
||||
channel: -123456789
|
||||
|
|
@ -175,6 +185,7 @@ func TestBuilder_Flags(t *testing.T) {
|
|||
require.NoError(t, flags.Set("irreversible-only", "true"))
|
||||
require.NoError(t, flags.Set("max-msg-in-flight", "98"))
|
||||
require.NoError(t, flags.Set("chain", "wax"))
|
||||
require.NoError(t, flags.Set("blacklist", "contract:action1,contract:action2,contract2:action1"))
|
||||
|
||||
cfg, err := NewBuilder().
|
||||
SetSource(bytes.NewReader([]byte(``))).
|
||||
|
|
@ -196,6 +207,10 @@ func TestBuilder_Flags(t *testing.T) {
|
|||
MaxMessagesInFlight: 98,
|
||||
IrreversibleOnly: true,
|
||||
Chain: "wax",
|
||||
Blacklist: types.Blacklist{
|
||||
"contract": {"action1", "action2"},
|
||||
"contract2": {"action1"},
|
||||
},
|
||||
},
|
||||
Telegram: TelegramConfig{
|
||||
Id: "72983126312982618",
|
||||
|
|
@ -213,3 +228,21 @@ func TestBuilder_Flags(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
require.Equal(t, &expected, cfg)
|
||||
}
|
||||
|
||||
func TestBuilder_BlacklistFlag(t *testing.T) {
|
||||
flags := GetFlags()
|
||||
|
||||
require.NoError(t, flags.Set("blacklist", "contract,contract:action2"))
|
||||
|
||||
conf, err := NewBuilder().
|
||||
SetSource(bytes.NewReader([]byte(``))).
|
||||
SetFlags(flags).
|
||||
Build()
|
||||
|
||||
expected := types.Blacklist{
|
||||
"contract": {"*", "action2"},
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expected, conf.Ship.Blacklist)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,5 +43,7 @@ func GetFlags() *pflag.FlagSet {
|
|||
flags.Int("max-msg-in-flight", 10, "Maximum messages that can be sent from SHIP without acknowledgement")
|
||||
flags.String("chain", "", "ChainID used in channel namespace, can be any string (default from api)")
|
||||
|
||||
flags.StringSlice("blacklist", []string{}, "Define a list of 'contract:action' pairs that will be blacklisted (thalos will not process those actions)")
|
||||
|
||||
return &flags
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package config
|
|||
|
||||
import (
|
||||
"github.com/eosswedenorg/thalos/internal/log"
|
||||
"github.com/eosswedenorg/thalos/internal/types"
|
||||
)
|
||||
|
||||
type RedisConfig struct {
|
||||
|
|
@ -18,12 +19,13 @@ type TelegramConfig struct {
|
|||
}
|
||||
|
||||
type ShipConfig struct {
|
||||
Url string `yaml:"url" mapstructure:"url"`
|
||||
IrreversibleOnly bool `yaml:"irreversible_only" mapstructure:"irreversible_only"`
|
||||
MaxMessagesInFlight uint32 `yaml:"max_messages_in_flight" mapstructure:"max_messages_in_flight"`
|
||||
StartBlockNum uint32 `yaml:"start_block_num" mapstructure:"start_block_num"`
|
||||
EndBlockNum uint32 `yaml:"end_block_num" mapstructure:"end_block_num"`
|
||||
Chain string `yaml:"chain" mapstructure:"chain"`
|
||||
Url string `yaml:"url" mapstructure:"url"`
|
||||
IrreversibleOnly bool `yaml:"irreversible_only" mapstructure:"irreversible_only"`
|
||||
MaxMessagesInFlight uint32 `yaml:"max_messages_in_flight" mapstructure:"max_messages_in_flight"`
|
||||
StartBlockNum uint32 `yaml:"start_block_num" mapstructure:"start_block_num"`
|
||||
EndBlockNum uint32 `yaml:"end_block_num" mapstructure:"end_block_num"`
|
||||
Chain string `yaml:"chain" mapstructure:"chain"`
|
||||
Blacklist types.Blacklist `yaml:"blacklist" mapstructure:"blacklist"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue