1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-08-23 19:48:12 +02:00

internal/types/blacklist.go: add isWhitelist field

This commit is contained in:
Henrik Hautakoski 2024-07-15 23:02:29 +02:00
parent 0aee0a97aa
commit 4f27307c70
4 changed files with 146 additions and 58 deletions

View file

@ -116,30 +116,9 @@ func (b *Builder) Build() (*Config, error) {
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")
if t == reflect.TypeOf(types.Blacklist{}) {
return decodeIntoBlacklist(in)
}
return in, nil
},
)
@ -151,3 +130,61 @@ func (b *Builder) Build() (*Config, error) {
return &conf, nil
}
// Decode a generic structure into types.Blacklist
func decodeIntoBlacklist(in any) (*types.Blacklist, error) {
switch v := in.(type) {
// Standard map structure.
case map[string]any:
return blacklistParseMap(v)
// slice of "contract:action" pairs. Usually from CLI
case []string:
return blacklistParseSlice(v)
// Sometimes we have a slice of interfaces.
// Need to convert it to a slice of strings.
case []any:
sv := make([]string, len(v))
for i, j := range v {
sv[i] = j.(string)
}
return blacklistParseSlice(sv)
}
return nil, fmt.Errorf("Must be a string slice")
}
// Blacklist map parser
func blacklistParseMap(in map[string]any) (*types.Blacklist, error) {
list := &types.Blacklist{}
for k, v := range in {
switch v := v.(type) {
case []any:
for _, v := range v {
list.Add(k, v.(string))
}
case any:
list.Add(k, v.(string))
}
}
return list, nil
}
// Blacklist slice parser
func blacklistParseSlice(in []string) (*types.Blacklist, error) {
list := &types.Blacklist{}
for _, i := range in {
var action string
parts := strings.SplitN(i, ":", 2)
if len(parts) < 2 {
action = "*"
} else {
action = parts[1]
}
list.Add(parts[0], action)
}
return list, nil
}

View file

@ -28,10 +28,10 @@ func TestBuilder(t *testing.T) {
EndBlockNum: 23872222,
IrreversibleOnly: true,
MaxMessagesInFlight: 1337,
Blacklist: types.Blacklist{
Blacklist: *types.NewBlacklist(map[string][]string{
"eosio": {"noop"},
"contract": {"skip1", "skip2"},
},
}),
},
Telegram: TelegramConfig{
Id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
@ -64,7 +64,7 @@ ship:
start_block_num: 23671836
end_block_num: 23872222
blacklist:
eosio: ["noop"]
eosio: noop
contract:
- skip1
- skip2
@ -207,10 +207,10 @@ func TestBuilder_Flags(t *testing.T) {
MaxMessagesInFlight: 98,
IrreversibleOnly: true,
Chain: "wax",
Blacklist: types.Blacklist{
Blacklist: *types.NewBlacklist(map[string][]string{
"contract": {"action1", "action2"},
"contract2": {"action1"},
},
}),
},
Telegram: TelegramConfig{
Id: "72983126312982618",
@ -229,20 +229,28 @@ func TestBuilder_Flags(t *testing.T) {
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"},
func TestBuilder_BlacklistSlice(t *testing.T) {
expected := Config{
Ship: ShipConfig{
Blacklist: *types.NewBlacklist(map[string][]string{
"contract": {"action"},
"contract2": {"action2"},
"contract3": {"*"},
}),
},
}
builder := NewBuilder()
builder.SetSource(bytes.NewBuffer([]byte(`
ship:
blacklist:
- "contract:action"
- "contract2:action2"
- contract3
`)))
cfg, err := builder.Build()
require.NoError(t, err)
require.Equal(t, expected, conf.Ship.Blacklist)
require.Equal(t, &expected, cfg)
}