diff --git a/x/observer/abci.go b/x/observer/abci.go index f219bb833d..41121ced19 100644 --- a/x/observer/abci.go +++ b/x/observer/abci.go @@ -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 diff --git a/x/observer/abci_test.go b/x/observer/abci_test.go new file mode 100644 index 0000000000..618dad9054 --- /dev/null +++ b/x/observer/abci_test.go @@ -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) + + }) +}