Skip to content

Commit

Permalink
Add abci tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Mar 28, 2024
1 parent 0759af2 commit 62a04a5
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
4 changes: 0 additions & 4 deletions x/observer/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
return
}
totalObserverCountCurrentBlock := allObservers.LenUint()
if totalObserverCountCurrentBlock < 0 {
ctx.Logger().Error("TotalObserverCount is negative at height", ctx.BlockHeight())
return
}
// #nosec G701 always in range
if totalObserverCountCurrentBlock == lastBlockObserverCount.Count {
return
Expand Down
99 changes: 99 additions & 0 deletions x/observer/abci_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package observer_test

import (
"math"
"testing"

"github.com/stretchr/testify/require"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/testutil/sample"
"github.com/zeta-chain/zetacore/x/observer"
"github.com/zeta-chain/zetacore/x/observer/types"
)

func TestBeginBlocker(t *testing.T) {
t.Run("should not update LastObserverCount if not found", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

observer.BeginBlocker(ctx, *k)

_, found := k.GetLastObserverCount(ctx)
require.False(t, found)

_, found = k.GetKeygen(ctx)
require.False(t, found)
})

t.Run("should not update LastObserverCount if observer set not found", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

count := 1
k.SetLastObserverCount(ctx, &types.LastObserverCount{
Count: uint64(count),
})

observer.BeginBlocker(ctx, *k)

lastObserverCount, found := k.GetLastObserverCount(ctx)
require.True(t, found)
require.Equal(t, uint64(count), lastObserverCount.Count)
require.Equal(t, int64(0), lastObserverCount.LastChangeHeight)

_, found = k.GetKeygen(ctx)
require.False(t, found)
})

t.Run("should not update LastObserverCount if observer set count equal last observed count", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

count := 1
os := sample.ObserverSet(count)
k.SetObserverSet(ctx, os)
k.SetLastObserverCount(ctx, &types.LastObserverCount{
Count: uint64(count),
})

observer.BeginBlocker(ctx, *k)

lastObserverCount, found := k.GetLastObserverCount(ctx)
require.True(t, found)
require.Equal(t, uint64(count), lastObserverCount.Count)
require.Equal(t, int64(0), lastObserverCount.LastChangeHeight)

_, found = k.GetKeygen(ctx)
require.False(t, found)
})

t.Run("should update LastObserverCount", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

observeSetLen := 10
count := 1
os := sample.ObserverSet(observeSetLen)
k.SetObserverSet(ctx, os)
k.SetLastObserverCount(ctx, &types.LastObserverCount{
Count: uint64(count),
})

keygen, found := k.GetKeygen(ctx)
require.False(t, found)
require.Equal(t, types.Keygen{}, keygen)

observer.BeginBlocker(ctx, *k)

keygen, found = k.GetKeygen(ctx)
require.True(t, found)
require.Empty(t, keygen.GranteePubkeys)
require.Equal(t, types.KeygenStatus_PendingKeygen, keygen.Status)
require.Equal(t, int64(math.MaxInt64), keygen.BlockNumber)

inboundEnabled := k.IsInboundEnabled(ctx)
require.False(t, inboundEnabled)

lastObserverCount, found := k.GetLastObserverCount(ctx)
require.True(t, found)
require.Equal(t, uint64(observeSetLen), lastObserverCount.Count)
require.Equal(t, ctx.BlockHeight(), lastObserverCount.LastChangeHeight)

})
}

0 comments on commit 62a04a5

Please sign in to comment.