Skip to content

Commit

Permalink
added test
Browse files Browse the repository at this point in the history
  • Loading branch information
insumity committed Jan 31, 2024
1 parent ca4d558 commit 4b7b4d9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
8 changes: 2 additions & 6 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1155,14 +1155,10 @@ func (k Keeper) SetTopN(
func (k Keeper) GetTopN(
ctx sdk.Context,
chainID string,
) (uint32, bool) {
) uint32 {
store := ctx.KVStore(k.storeKey)
buf := store.Get(types.TopNKey(chainID))
if buf == nil {
return 0, false
} else {
}
return binary.BigEndian.Uint32(buf), true
return binary.BigEndian.Uint32(buf)
}

// IsTopN returns true if chain with `chainID` is a Top N chain (i.e., enforces at least one validator to validate chain `chainID`)
Expand Down
19 changes: 19 additions & 0 deletions x/ccv/provider/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,22 @@ func TestGetAllProposedConsumerChainIDs(t *testing.T) {
}
}
}

// TestTopN tests `SetTopN`, `GetTopN`, `IsTopN`, and `IsOptIn` methods
func TestTopN(t *testing.T) {
providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()

providerKeeper.SetTopN(ctx, "TopNChainID1", 50)
require.Equal(t, uint32(50), providerKeeper.GetTopN(ctx, "TopNChainID1"))
require.True(t, providerKeeper.IsTopN(ctx, "TopNChainID1"))
require.False(t, providerKeeper.IsOptIn(ctx, "TopNChainID1"))

providerKeeper.SetTopN(ctx, "TopNChainID2", 100)
require.Equal(t, uint32(100), providerKeeper.GetTopN(ctx, "TopNChainID2"))

providerKeeper.SetTopN(ctx, "OptInChain", 0)
require.Equal(t, uint32(0), providerKeeper.GetTopN(ctx, "OptInChain"))
require.False(t, providerKeeper.IsTopN(ctx, "OptInChain"))
require.True(t, providerKeeper.IsOptIn(ctx, "OptInChain"))
}
8 changes: 1 addition & 7 deletions x/ccv/provider/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ func (k Keeper) HandleConsumerAdditionProposal(ctx sdk.Context, p *types.Consume
return err
}

// you should not be able
if _, found := k.GetTopN(ctx, p.ChainId); found {
return errorsmod.Wrap(types.ErrDuplicateConsumerChain,
fmt.Sprint("cannot add a chain "))
}
k.SetTopN(ctx, p.ChainId, p.Top_N)

k.SetPendingConsumerAdditionProp(ctx, p)

k.Logger(ctx).Info("consumer addition proposal enqueued",
Expand Down Expand Up @@ -372,6 +365,7 @@ func (k Keeper) BeginBlockInit(ctx sdk.Context) {
ctx.Logger().Info("consumer client could not be created: %w", err)
continue
}
k.SetTopN(ctx, prop.ChainId, prop.Top_N)
// The cached context is created with a new EventManager so we merge the event
// into the original context
ctx.EventManager().EmitEvents(cachedCtx.EventManager().Events())
Expand Down

0 comments on commit 4b7b4d9

Please sign in to comment.