diff --git a/common/types.go b/common/types.go index 02c574e966..f518fdeb42 100644 --- a/common/types.go +++ b/common/types.go @@ -247,16 +247,18 @@ func (a UnprefixedAddress) MarshalText() ([]byte, error) { // Extract validators from byte array. func RemoveItemFromArray(array []Address, items []Address) []Address { - if items == nil { + if len(items) == 0 { return array } - for i, value := range array { - for _, item := range items { - if value == item { + + for _, item := range items { + for i := len(array) - 1; i >= 0; i-- { + if array[i] == item { array = append(array[:i], array[i+1:]...) } } } + return array } diff --git a/common/types_test.go b/common/types_test.go index 4b01bc275d..e481db96c3 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -151,10 +151,10 @@ func BenchmarkAddressHex(b *testing.B) { } func TestRemoveItemInArray(t *testing.T) { - array := []Address{HexToAddress("0x0000000"), HexToAddress("0x0000001"), HexToAddress("0x0000002")} - remove := []Address{HexToAddress("0x0000000"), HexToAddress("0x0000004"), HexToAddress("0x0000003")} + array := []Address{HexToAddress("0x0000003"),HexToAddress("0x0000001"), HexToAddress("0x0000002"),HexToAddress("0x0000003")} + remove := []Address{HexToAddress("0x0000002"), HexToAddress("0x0000004"), HexToAddress("0x0000003")} array = RemoveItemFromArray(array, remove) - if len(array) != 2 { + if len(array) != 1 { t.Error("fail remove item from array address") } }