From 6d14591f6bf521388993806d4d10d2c64c093232 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 23 Jun 2024 14:52:05 +0200 Subject: [PATCH] internal/config: adding blacklist field to Ship config --- internal/config/builder.go | 32 ++++++++++++++++++++++++++++++++ internal/config/builder_test.go | 33 +++++++++++++++++++++++++++++++++ internal/config/cli.go | 2 ++ internal/config/config.go | 14 ++++++++------ 4 files changed, 75 insertions(+), 6 deletions(-) diff --git a/internal/config/builder.go b/internal/config/builder.go index 3bcbbd3..026dd33 100644 --- a/internal/config/builder.go +++ b/internal/config/builder.go @@ -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)) diff --git a/internal/config/builder_test.go b/internal/config/builder_test.go index 24f7751..d226a30 100644 --- a/internal/config/builder_test.go +++ b/internal/config/builder_test.go @@ -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) +} diff --git a/internal/config/cli.go b/internal/config/cli.go index bace210..38439a5 100644 --- a/internal/config/cli.go +++ b/internal/config/cli.go @@ -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 } diff --git a/internal/config/config.go b/internal/config/config.go index 41777df..5e84072 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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 {