Skip to content

Commit

Permalink
add msg_server test
Browse files Browse the repository at this point in the history
  • Loading branch information
happycoder9345 committed Sep 25, 2024
1 parent 6e0271c commit 096beb6
Show file tree
Hide file tree
Showing 10 changed files with 600 additions and 15 deletions.
1 change: 1 addition & 0 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ var (
agent.AppModuleBasic{},
plan.AppModuleBasic{},
token.AppModuleBasic{},
ccev.AppModuleBasic{},
)

// module account permissions
Expand Down
9 changes: 7 additions & 2 deletions x/ccev/keeper/contract.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
errorsmod "cosmossdk.io/errors"
"github.com/Lorenzo-Protocol/lorenzo/v3/x/ccev/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -14,14 +15,18 @@ func (k Keeper) UploadContract(
address string,
eventName string,
abi []byte,
) {
) error {
if !k.hasClient(ctx, chainID) {
return errorsmod.Wrapf(types.ErrNotFoundClient, "client %d not found", chainID)
}
contract := &types.Contract{
ChainId: chainID,
Address: address,
EventName: eventName,
Abi: abi,
}
k.setContract(ctx, contract)
return nil
}

func (k Keeper) setContract(ctx sdk.Context, contract *types.Contract) {
Expand All @@ -47,7 +52,7 @@ func (k Keeper) getContract(
return &contract
}

func (k Keeper) getAllContracts(ctx sdk.Context,chainID uint32) (contracts []*types.Contract) {
func (k Keeper) getAllContracts(ctx sdk.Context, chainID uint32) (contracts []*types.Contract) {
store := k.clientStore(ctx, chainID)

it := sdk.KVStorePrefixIterator(store, types.KeyPrefixCrossChainContract)
Expand Down
3 changes: 3 additions & 0 deletions x/ccev/keeper/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func (k Keeper) UploadHeaders(ctx sdk.Context, chainID uint32, headers []types.T
}

for _, header := range headers {
if k.HasHeader(ctx, chainID, header.Number) {
return errorsmod.Wrapf(types.ErrDuplicateHeader, "header %d already exists", header.Number)
}
k.setHeader(ctx, chainID, &header)

Check failure on line 54 in x/ccev/keeper/header.go

View workflow job for this annotation

GitHub Actions / golangci-lint

G601: Implicit memory aliasing in for loop. (gosec)
}
k.setLatestNumber(ctx, chainID, headers[len(headers)-1].Number)
Expand Down
67 changes: 67 additions & 0 deletions x/ccev/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package keeper_test

import (
"encoding/json"
"testing"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"

"github.com/Lorenzo-Protocol/lorenzo/v3/app"
"github.com/Lorenzo-Protocol/lorenzo/v3/x/ccev/keeper"
"github.com/Lorenzo-Protocol/lorenzo/v3/x/ccev/types"
)

var testAccounts = app.CreateTestAddrs(2)

type KeeperTestSuite struct {
suite.Suite

ctx sdk.Context
keeper keeper.Keeper

msgServer types.MsgServer
queryClient types.QueryClient
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}

func (suite *KeeperTestSuite) SetupTest() {
suite.T().Log("setting up keeper test suite")
merge := func(cdc codec.Codec, state map[string]json.RawMessage) {
genesis := &types.GenesisState{
Params: &types.Params{
AllowList: []string{
testAccounts[0].String(),
},
},
}
state[types.ModuleName] = cdc.MustMarshalJSON(genesis)
}

lorenzoApp := app.SetupWithGenesisMergeFn(suite.T(), merge)
suite.ctx = lorenzoApp.BaseApp.NewContext(false, tmproto.Header{})
suite.keeper = lorenzoApp.CCEVkeeper

queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, lorenzoApp.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, keeper.NewQuerier(suite.keeper))
// types.RegisterMsgServer(queryHelper, keeper.NewMsgServerImpl(suite.keeper))

queryClient := types.NewQueryClient(queryHelper)
suite.msgServer = keeper.NewMsgServerImpl(suite.keeper)
suite.queryClient = queryClient
}

func (suite *KeeperTestSuite) CreateClient(chainID uint32, chainName string, initialBlock types.TinyHeader) {
err := suite.keeper.CreateClient(suite.ctx, &types.Client{
ChainId: chainID,
ChainName: chainName,
InitialBlock: initialBlock,
})
suite.Require().NoError(err)
}
6 changes: 4 additions & 2 deletions x/ccev/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ func (m msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParam
// UploadContract implements types.MsgServer.
func (m msgServer) UploadContract(goCtx context.Context, msg *types.MsgUploadContract) (*types.MsgUploadContractResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if !m.Allow(ctx, msg.Address) {
if !m.Allow(ctx, msg.Sender) {
return nil, errorsmod.Wrapf(types.ErrUnauthorized, "address %s is not in allowlist", msg.Address)
}

m.Keeper.UploadContract(ctx, msg.ChainId, msg.Address, msg.EventName, msg.Abi)
if err := m.Keeper.UploadContract(ctx, msg.ChainId, msg.Address, msg.EventName, msg.Abi); err != nil {
return nil, err
}
return &types.MsgUploadContractResponse{}, nil
}

Expand Down
Loading

0 comments on commit 096beb6

Please sign in to comment.