Skip to content

Commit

Permalink
add validation before setting observer_set during add and update
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Aug 9, 2024
1 parent 415d354 commit 9208295
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
11 changes: 6 additions & 5 deletions x/observer/keeper/observer_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ func (k Keeper) IsAddressPartOfObserverSet(ctx sdk.Context, address string) bool

func (k Keeper) AddObserverToSet(ctx sdk.Context, address string) error {
observerSet, found := k.GetObserverSet(ctx)
if !found {
k.SetObserverSet(ctx, types.ObserverSet{
switch {
case !found:
observerSet = types.ObserverSet{
ObserverList: []string{address},
})
return nil
}
default:
observerSet.ObserverList = append(observerSet.ObserverList, address)
}
observerSet.ObserverList = append(observerSet.ObserverList, address)
err := observerSet.Validate()
if err != nil {
return err
Expand Down
12 changes: 5 additions & 7 deletions x/observer/keeper/observer_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,16 @@ func TestKeeper_AddObserverToSet(t *testing.T) {
t.Run("add observer to set if set doesn't exist", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)
newObserver := sample.AccAddress()
k.AddObserverToSet(ctx, newObserver)
err := k.AddObserverToSet(ctx, newObserver)
require.NoError(t, err)
require.True(t, k.IsAddressPartOfObserverSet(ctx, newObserver))
osNew, found := k.GetObserverSet(ctx)
require.True(t, found)
require.Len(t, osNew.ObserverList, 1)

// add same address again, len doesn't change
k.AddObserverToSet(ctx, newObserver)
require.True(t, k.IsAddressPartOfObserverSet(ctx, newObserver))
osNew, found = k.GetObserverSet(ctx)
require.True(t, found)
require.Len(t, osNew.ObserverList, 1)
// cannot add same address again
err = k.AddObserverToSet(ctx, newObserver)
require.ErrorIs(t, err, types.ErrDuplicateObserver)
})
}

Expand Down

0 comments on commit 9208295

Please sign in to comment.