Skip to content

Commit

Permalink
add unit tests for aborted zeta amount keeper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Nov 14, 2023
1 parent c41f51a commit d3bd6d8
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 34 deletions.
1 change: 1 addition & 0 deletions proto/crosschain/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ message GenesisState {
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];
AbortedZetaAmount aborted_zeta_amount = 12 [(gogoproto.nullable) = false];
}
7 changes: 6 additions & 1 deletion typescript/crosschain/genesis_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { OutTxTracker } from "./out_tx_tracker_pb.js";
import type { TSS } from "./tss_pb.js";
import type { GasPrice } from "./gas_price_pb.js";
import type { ChainNonces } from "./chain_nonces_pb.js";
import type { CrossChainTx } from "./cross_chain_tx_pb.js";
import type { AbortedZetaAmount, CrossChainTx } from "./cross_chain_tx_pb.js";
import type { LastBlockHeight } from "./last_block_height_pb.js";
import type { InTxHashToCctx } from "./in_tx_hash_to_cctx_pb.js";
import type { InTxTracker } from "./in_tx_tracker_pb.js";
Expand Down Expand Up @@ -71,6 +71,11 @@ export declare class GenesisState extends Message<GenesisState> {
*/
inTxTrackerList: InTxTracker[];

/**
* @generated from field: zetachain.zetacore.crosschain.AbortedZetaAmount aborted_zeta_amount = 12;
*/
abortedZetaAmount?: AbortedZetaAmount;

constructor(data?: PartialMessage<GenesisState>);

static readonly runtime: typeof proto3;
Expand Down
1 change: 1 addition & 0 deletions x/crosschain/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func GetQueryCmd(_ string) *cobra.Command {
CmdPendingCctx(),
CmdListInTxTrackerByChain(),
CmdListInTxTrackers(),
CmdGetAbortedZetaAmount(),
)

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

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
"github.com/zeta-chain/zetacore/x/crosschain/types"
)

func CmdGetAbortedZetaAmount() *cobra.Command {
cmd := &cobra.Command{
Use: "get-aborted-zeta-amount",
Short: "Query current tss address",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryAbortedZetaAmountRequest{}
res, err := queryClient.AbortedZetaAmount(cmd.Context(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
2 changes: 2 additions & 0 deletions x/crosschain/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
// Params
k.SetParams(ctx, genState.Params)

k.SetAbortedZetaAmount(ctx, genState.AbortedZetaAmount)
// Set all the outTxTracker
for _, elem := range genState.OutTxTrackerList {
k.SetOutTxTracker(ctx, elem)
Expand Down Expand Up @@ -70,6 +71,7 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
k.SetTSSHistory(ctx, elem)
}
}

}

// ExportGenesis returns the crosschain module's exported genesis.
Expand Down
4 changes: 4 additions & 0 deletions x/crosschain/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package crosschain_test
import (
"testing"

sdkmath "cosmossdk.io/math"
"github.com/stretchr/testify/require"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/testutil/nullify"
Expand All @@ -14,6 +15,9 @@ import (
func TestGenesis(t *testing.T) {
genesisState := types.GenesisState{
Params: types.DefaultParams(),
AbortedZetaAmount: types.AbortedZetaAmount{
Amount: sdkmath.OneUint(),
},
OutTxTrackerList: []types.OutTxTracker{
sample.OutTxTracker(t, "0"),
sample.OutTxTracker(t, "1"),
Expand Down
49 changes: 49 additions & 0 deletions x/crosschain/keeper/abourted_zeta_amount_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package keeper_test

import (
"math/rand"
"testing"

sdkmath "cosmossdk.io/math"
"github.com/stretchr/testify/require"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/x/crosschain/types"
)

func TestKeeper_AddAbortedZetaAmount(t *testing.T) {

t.Run("should add aborted zeta amount", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
originalAmount := sdkmath.NewUint(rand.Uint64())
k.SetAbortedZetaAmount(ctx, types.AbortedZetaAmount{
originalAmount,
})
val, found := k.GetAbortedZetaAmount(ctx)
require.True(t, found)
require.Equal(t, originalAmount, val.Amount)
addAmount := sdkmath.NewUint(rand.Uint64())
k.AddAbortedZetaAmount(ctx, addAmount)
val, found = k.GetAbortedZetaAmount(ctx)
require.True(t, found)
require.Equal(t, originalAmount.Add(addAmount), val.Amount)
})

t.Run("cant find aborted amount", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
val, found := k.GetAbortedZetaAmount(ctx)
require.False(t, found)
require.Equal(t, types.AbortedZetaAmount{}, val)
})

t.Run("add very high zeta amount", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
highAmount := sdkmath.NewUintFromString("100000000000000000000000000000000000000000000000")
k.SetAbortedZetaAmount(ctx, types.AbortedZetaAmount{
highAmount,
})
val, found := k.GetAbortedZetaAmount(ctx)
require.True(t, found)
require.Equal(t, highAmount, val.Amount)
})

}
122 changes: 89 additions & 33 deletions x/crosschain/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zetaclient/zeta_supply_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ func ValidateZetaSupply(logger zerolog.Logger, abortedTxAmounts, zetaInTransit,
return true
}

// TODO : Add genesis state for Aborted amount in zeta
// TODO : Add tests for keeper functions
// TODO : Add cli commands for querying aborted amount
func (zs *ZetaSupplyChecker) AbortedTxAmount() (sdkmath.Int, error) {
amount, err := zs.zetaClient.GetAbortedZetaAmount()
if err != nil {
Expand Down

0 comments on commit d3bd6d8

Please sign in to comment.