Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Sep 9, 2023
1 parent fdda86d commit b989fe4
Show file tree
Hide file tree
Showing 16 changed files with 1,092 additions and 275 deletions.
2 changes: 2 additions & 0 deletions proto/crosschain/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "crosschain/chain_nonces.proto";
import "crosschain/cross_chain_tx.proto";
import "crosschain/gas_price.proto";
import "crosschain/in_tx_hash_to_cctx.proto";
import "crosschain/in_tx_tracker.proto";
import "crosschain/last_block_height.proto";
import "crosschain/out_tx_tracker.proto";
import "crosschain/params.proto";
Expand All @@ -25,4 +26,5 @@ message GenesisState {
repeated LastBlockHeight lastBlockHeightList = 8;
repeated InTxHashToCctx inTxHashToCctxList = 9 [(gogoproto.nullable) = false];
repeated TSS tss_history = 10 [(gogoproto.nullable) = false];
repeated InTxTracker in_tx_tracker_list = 11 [(gogoproto.nullable) = false];
}
9 changes: 9 additions & 0 deletions proto/crosschain/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ service Query {
rpc InTxTrackerAllByChain(QueryAllInTxTrackerByChainRequest) returns (QueryAllInTxTrackerByChainResponse) {
option (google.api.http).get = "/zeta-chain/crosschain/inTxTrackerByChain/{chain_id}";
}
rpc InTxTrackerAll(QueryAllInTxTrackersRequest) returns (QueryAllInTxTrackersResponse) {
option (google.api.http).get = "/zeta-chain/crosschain/inTxTrackers";
}

// Queries a InTxHashToCctx by index.
rpc InTxHashToCctx(QueryGetInTxHashToCctxRequest) returns (QueryGetInTxHashToCctxResponse) {
Expand Down Expand Up @@ -191,6 +194,12 @@ message QueryAllInTxTrackerByChainResponse {
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryAllInTxTrackersRequest {}

message QueryAllInTxTrackersResponse {
repeated InTxTracker inTxTracker = 1 [(gogoproto.nullable) = false];
}

message QueryGetInTxHashToCctxRequest {
string inTxHash = 1;
}
Expand Down
10 changes: 10 additions & 0 deletions testutil/network/genesis_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ func AddCrosschainData(t *testing.T, n int, genesisState map[string]json.RawMess
state.OutTxTrackerList = append(state.OutTxTrackerList, outTxTracker)
}

for i := 0; i < n; i++ {
inTxTracker := types.InTxTracker{
ChainId: 5,
TxHash: fmt.Sprintf("txHash-%d", i),
CoinType: common.CoinType_Gas,
}
nullify.Fill(&inTxTracker)
state.InTxTrackerList = append(state.InTxTrackerList, inTxTracker)
}

for i := 0; i < n; i++ {
inTxHashToCctx := types.InTxHashToCctx{
InTxHash: strconv.Itoa(i),
Expand Down
54 changes: 54 additions & 0 deletions x/crosschain/client/cli/cli_in_tx_tracker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"context"
"strconv"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -44,3 +45,56 @@ func CmdAddToInTxTracker() *cobra.Command {

return cmd
}

func CmdListInTxTrackerByChain() *cobra.Command {
cmd := &cobra.Command{
Use: "list-in-tx-tracker [chainId]",
Short: "shows a list of in tx tracker by chainId",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := types.NewQueryClient(clientCtx)
argChain, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return err
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
params := &types.QueryAllInTxTrackerByChainRequest{
ChainId: argChain,
Pagination: pageReq,
}
res, err := queryClient.InTxTrackerAllByChain(context.Background(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, cmd.Use)
return cmd
}

func CmdListInTxTrackers() *cobra.Command {
cmd := &cobra.Command{
Use: "list-all-tx-trackers",
Short: "shows all inTxTrackers",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryAllInTxTrackersRequest{}
res, err := queryClient.InTxTrackerAll(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
2 changes: 2 additions & 0 deletions x/crosschain/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
CmdQueryParams(),
CmdGetTssAddress(),
CmdListTssHistory(),
CmdListInTxTrackerByChain(),
CmdListInTxTrackers(),
)

return cmd
Expand Down
1 change: 1 addition & 0 deletions x/crosschain/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func GetTxCmd() *cobra.Command {
CmdCCTXInboundVoter(),
CmdRemoveFromWatchList(),
CmdUpdateTss(),
CmdAddToInTxTracker(),
)

return cmd
Expand Down
101 changes: 101 additions & 0 deletions x/crosschain/client/querytests/in_tx_tracker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package querytests

import (
"fmt"

"github.com/cosmos/cosmos-sdk/client/flags"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/zeta-chain/zetacore/testutil/nullify"
"github.com/zeta-chain/zetacore/x/crosschain/client/cli"
"github.com/zeta-chain/zetacore/x/crosschain/types"
)

func (s *CliTestSuite) TestListInTxTrackers() {
ctx := s.network.Validators[0].ClientCtx
objs := s.crosschainState.InTxTrackerList
s.Run("List all trackers", func() {
args := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListInTxTrackers(), args)
s.Require().NoError(err)
var resp types.QueryAllInTxTrackersResponse
s.Require().NoError(s.network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
s.Require().Equal(len(objs), len(resp.InTxTracker))
s.Require().ElementsMatch(nullify.Fill(objs), nullify.Fill(resp.InTxTracker))
})
}

func (s *CliTestSuite) TestListInTxTrackersByChain() {
ctx := s.network.Validators[0].ClientCtx
objs := s.crosschainState.InTxTrackerList
request := func(next []byte, offset, limit uint64, total bool, chainID int) []string {
args := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
if next == nil {
args = append(args, fmt.Sprintf("--%s=%d", flags.FlagOffset, offset))
} else {
args = append(args, fmt.Sprintf("--%s=%s", flags.FlagPageKey, next))
}
args = append(args, fmt.Sprintf("--%s=%d", flags.FlagLimit, limit))
if total {
args = append(args, fmt.Sprintf("--%s", flags.FlagCountTotal))
}
args = append(args, fmt.Sprintf("%d", chainID))
return args
}
s.Run("ByOffset", func() {
step := 2
for i := 0; i < len(objs); i += step {
args := request(nil, uint64(i), uint64(step), false, 5)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListInTxTrackerByChain(), args)
s.Require().NoError(err)
var resp types.QueryAllInTxTrackerByChainResponse
s.Require().NoError(s.network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
s.Require().LessOrEqual(len(resp.InTxTracker), step)
s.Require().Subset(nullify.Fill(objs),
nullify.Fill(resp.InTxTracker),
)
}
})
s.Run("ByKey", func() {
step := 2
var next []byte
for i := 0; i < len(objs); i += step {
args := request(next, 0, uint64(step), false, 5)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListInTxTrackerByChain(), args)
s.Require().NoError(err)
var resp types.QueryAllInTxTrackerByChainResponse
s.Require().NoError(s.network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
s.Require().LessOrEqual(len(resp.InTxTracker), step)
s.Require().Subset(
nullify.Fill(objs),
nullify.Fill(resp.InTxTracker),
)
next = resp.Pagination.NextKey
}
})
s.Run("Total", func() {
args := request(nil, 0, uint64(len(objs)), true, 5)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListInTxTrackerByChain(), args)
s.Require().NoError(err)
var resp types.QueryAllInTxTrackerByChainResponse
s.Require().NoError(s.network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
s.Require().NoError(err)
s.Require().Equal(len(objs), int(resp.Pagination.Total))
s.Require().ElementsMatch(nullify.Fill(objs),
nullify.Fill(resp.InTxTracker),
)
})
s.Run("Incorrect Chain ID ", func() {
args := request(nil, 0, uint64(len(objs)), true, 15)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListInTxTrackerByChain(), args)
s.Require().NoError(err)
var resp types.QueryAllInTxTrackerByChainResponse
s.Require().NoError(s.network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
s.Require().NoError(err)
s.Require().Equal(0, int(resp.Pagination.Total))
})
}
6 changes: 6 additions & 0 deletions x/crosschain/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
k.SetOutTxTracker(ctx, elem)
}

// Set all the inTxTracker
for _, elem := range genState.InTxTrackerList {
k.SetInTxTracker(ctx, elem)
}

// Set all the inTxHashToCctx
for _, elem := range genState.InTxHashToCctxList {
k.SetInTxHashToCctx(ctx, elem)
Expand Down Expand Up @@ -76,6 +81,7 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis.Params = k.GetParams(ctx)
genesis.OutTxTrackerList = k.GetAllOutTxTracker(ctx)
genesis.InTxHashToCctxList = k.GetAllInTxHashToCctx(ctx)
genesis.InTxTrackerList = k.GetAllInTxTracker(ctx)

// Get tss
tss, found := k.GetTSS(ctx)
Expand Down
18 changes: 16 additions & 2 deletions x/crosschain/keeper/grpc_query_in_tx_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@ import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/zeta-chain/zetacore/x/crosschain/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (k Keeper) InTxTrackerAllByChain(goCtx context.Context, request *types.QueryAllInTxTrackerByChainRequest) (*types.QueryAllInTxTrackerByChainResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
inTxTrackers := k.GetAllInTxTrackerForChain(ctx, request.ChainId)
return &types.QueryAllInTxTrackerByChainResponse{InTxTracker: inTxTrackers}, nil
var inTxTrackers []types.InTxTracker
inTxTrackers, pageRes, err := k.GetAllInTxTrackerForChainPaginated(ctx, request.ChainId, request.Pagination)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryAllInTxTrackerByChainResponse{InTxTracker: inTxTrackers, Pagination: pageRes}, nil
}

func (k Keeper) InTxTrackerAll(goCtx context.Context, _ *types.QueryAllInTxTrackersRequest) (*types.QueryAllInTxTrackersResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
var inTxTrackers []types.InTxTracker
inTxTrackers = k.GetAllInTxTracker(ctx)
return &types.QueryAllInTxTrackersResponse{InTxTracker: inTxTrackers}, nil
}
32 changes: 29 additions & 3 deletions x/crosschain/keeper/keeper_in_tx_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@ import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/zeta-chain/zetacore/x/crosschain/types"
zetaObserverTypes "github.com/zeta-chain/zetacore/x/observer/types"
)

func getOutTrackerKey(chainID int64, txHash string) string {
func getInTrackerKey(chainID int64, txHash string) string {
return fmt.Sprintf("%d-%s", chainID, txHash)
}

// SetInTxTracker set a specific InTxTracker in the store from its index
func (k Keeper) SetInTxTracker(ctx sdk.Context, InTxTracker types.InTxTracker) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InTxTrackerKeyPrefix))
b := k.cdc.MustMarshal(&InTxTracker)
key := types.KeyPrefix(getOutTrackerKey(InTxTracker.ChainId, InTxTracker.TxHash))
key := types.KeyPrefix(getInTrackerKey(InTxTracker.ChainId, InTxTracker.TxHash))
store.Set(key, b)
}

// GetInTxTracker returns a InTxTracker from its index
func (k Keeper) GetInTxTracker(ctx sdk.Context, chainID int64, txHash string) (val types.InTxTracker, found bool) {
key := getOutTrackerKey(chainID, txHash)
key := getInTrackerKey(chainID, txHash)
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InTxTrackerKeyPrefix))
b := store.Get(types.KeyPrefix(key))
if b == nil {
Expand All @@ -34,6 +35,17 @@ func (k Keeper) GetInTxTracker(ctx sdk.Context, chainID int64, txHash string) (v
k.cdc.MustUnmarshal(b, &val)
return val, true
}
func (k Keeper) GetAllInTxTracker(ctx sdk.Context) (list []types.InTxTracker) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InTxTrackerKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.InTxTracker
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return list
}

func (k Keeper) GetAllInTxTrackerForChain(ctx sdk.Context, chainId int64) (list []types.InTxTracker) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InTxTrackerKeyPrefix))
Expand All @@ -47,6 +59,20 @@ func (k Keeper) GetAllInTxTrackerForChain(ctx sdk.Context, chainId int64) (list
return list
}

func (k Keeper) GetAllInTxTrackerForChainPaginated(ctx sdk.Context, chainId int64, pagination *query.PageRequest) (inTxTrackers []types.InTxTracker, pageRes *query.PageResponse, err error) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(fmt.Sprintf("%s", types.InTxTrackerKeyPrefix)))
chainStore := prefix.NewStore(store, types.KeyPrefix(fmt.Sprintf("%d-", chainId)))
pageRes, err = query.Paginate(chainStore, pagination, func(key []byte, value []byte) error {
var inTxTracker types.InTxTracker
if err := k.cdc.Unmarshal(value, &inTxTracker); err != nil {
return err
}
inTxTrackers = append(inTxTrackers, inTxTracker)
return nil
})
return
}

func (k msgServer) AddToInTxTracker(goCtx context.Context, msg *types.MsgAddToInTxTracker) (*types.MsgAddToInTxTrackerResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
chain := k.zetaObserverKeeper.GetParams(ctx).GetChainFromChainID(msg.ChainId)
Expand Down
Loading

0 comments on commit b989fe4

Please sign in to comment.