Skip to content

Commit

Permalink
refactor(crosschain): define interface for fungible keeper reference (
Browse files Browse the repository at this point in the history
#905)

* fix crosschain test keeper

* fungible expected keeper

* use new type

* genesis rename import

* fix keeper_test

* fix test files

---------

Co-authored-by: brewmaster012 <88689859+brewmaster012@users.noreply.github.com>
  • Loading branch information
lumtis and brewmaster012 authored Aug 4, 2023
1 parent da2ead6 commit b77c02f
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 47 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func New(
app.AccountKeeper,
app.BankKeeper,
app.ZetaObserverKeeper,
app.FungibleKeeper,
&app.FungibleKeeper,
)
app.GroupKeeper = groupkeeper.NewKeeper(keys[group.StoreKey], appCodec, app.MsgServiceRouter(), app.AccountKeeper, group.Config{
MaxExecutionPeriod: 2 * time.Hour, // Two hours.
Expand Down
34 changes: 17 additions & 17 deletions testutil/keeper/zetacore.go → testutil/keeper/crosschain.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package keeper

import (
authkeeper2 "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper2 "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/zeta-chain/zetacore/x/crosschain/keeper"
"testing"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmdb "github.com/tendermint/tm-db"
"github.com/zeta-chain/zetacore/x/crosschain/types"
fungibleKeeper "github.com/zeta-chain/zetacore/x/fungible/keeper"
zetaobserverKeeper "github.com/zeta-chain/zetacore/x/observer/keeper"

typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/zeta-chain/zetacore/x/crosschain/keeper"
"github.com/zeta-chain/zetacore/x/crosschain/types"
fungiblekeeper "github.com/zeta-chain/zetacore/x/fungible/keeper"
observerkeeper "github.com/zeta-chain/zetacore/x/observer/keeper"
)

func ZetacoreKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
func CrosschainKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
storeKey := sdk.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

Expand All @@ -36,28 +36,28 @@ func ZetacoreKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)

paramsSubspace := typesparams.NewSubspace(cdc,
paramsSubspace := paramstypes.NewSubspace(cdc,
types.Amino,
storeKey,
memStoreKey,
"ZetacoreParams",
)

bankkeeper := bankkeeper2.BaseKeeper{}
authkeeper := authkeeper2.AccountKeeper{}
zetaobserverKeeper := zetaobserverKeeper.Keeper{}
fungibleKeeper := fungibleKeeper.Keeper{}
bankKeeper := bankkeeper.BaseKeeper{}
authKeeper := authkeeper.AccountKeeper{}
observerKeeper := observerkeeper.Keeper{}
fungibleKeeper := fungiblekeeper.Keeper{}

k := keeper.NewKeeper(
codec.NewProtoCodec(registry),
storeKey,
memStoreKey,
stakingkeeper.Keeper{}, // custom
paramsSubspace,
authkeeper,
bankkeeper,
zetaobserverKeeper,
fungibleKeeper,
authKeeper,
bankKeeper,
observerKeeper,
&fungibleKeeper,
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
4 changes: 2 additions & 2 deletions x/crosschain/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package crosschain_test

import (
"github.com/zeta-chain/zetacore/x/crosschain"
"testing"

"github.com/stretchr/testify/require"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/testutil/nullify"
"github.com/zeta-chain/zetacore/x/crosschain"
"github.com/zeta-chain/zetacore/x/crosschain/types"
)

Expand All @@ -33,7 +33,7 @@ func TestGenesis(t *testing.T) {
//},
}

k, ctx := keepertest.ZetacoreKeeper(t)
k, ctx := keepertest.CrosschainKeeper(t)
crosschain.InitGenesis(ctx, *k, genesisState)
got := crosschain.ExportGenesis(ctx, *k)
require.NotNil(t, got)
Expand Down
4 changes: 2 additions & 2 deletions x/crosschain/keeper/grpc_query_in_tx_hash_to_cctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
var _ = strconv.IntSize

func TestInTxHashToCctxQuerySingle(t *testing.T) {
keeper, ctx := keepertest.ZetacoreKeeper(t)
keeper, ctx := keepertest.CrosschainKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
msgs := createNInTxHashToCctx(keeper, ctx, 2)
for _, tc := range []struct {
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestInTxHashToCctxQuerySingle(t *testing.T) {
}

func TestInTxHashToCctxQueryPaginated(t *testing.T) {
keeper, ctx := keepertest.ZetacoreKeeper(t)
keeper, ctx := keepertest.CrosschainKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
msgs := createNInTxHashToCctx(keeper, ctx, 5)

Expand Down
6 changes: 3 additions & 3 deletions x/crosschain/keeper/in_tx_hash_to_cctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func createNInTxHashToCctx(keeper *keeper.Keeper, ctx sdk.Context, n int) []type
}

func TestInTxHashToCctxGet(t *testing.T) {
keeper, ctx := keepertest.ZetacoreKeeper(t)
keeper, ctx := keepertest.CrosschainKeeper(t)
items := createNInTxHashToCctx(keeper, ctx, 10)
for _, item := range items {
rst, found := keeper.GetInTxHashToCctx(ctx,
Expand All @@ -40,7 +40,7 @@ func TestInTxHashToCctxGet(t *testing.T) {
}
}
func TestInTxHashToCctxRemove(t *testing.T) {
keeper, ctx := keepertest.ZetacoreKeeper(t)
keeper, ctx := keepertest.CrosschainKeeper(t)
items := createNInTxHashToCctx(keeper, ctx, 10)
for _, item := range items {
keeper.RemoveInTxHashToCctx(ctx,
Expand All @@ -54,7 +54,7 @@ func TestInTxHashToCctxRemove(t *testing.T) {
}

func TestInTxHashToCctxGetAll(t *testing.T) {
keeper, ctx := keepertest.ZetacoreKeeper(t)
keeper, ctx := keepertest.CrosschainKeeper(t)
items := createNInTxHashToCctx(keeper, ctx, 10)
require.ElementsMatch(t,
nullify.Fill(items),
Expand Down
5 changes: 2 additions & 3 deletions x/crosschain/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
fungibleModuleKeeper "github.com/zeta-chain/zetacore/x/fungible/keeper"

"github.com/tendermint/tendermint/libs/log"

Expand All @@ -24,7 +23,7 @@ type (
authKeeper types.AccountKeeper
bankKeeper types.BankKeeper
zetaObserverKeeper types.ZetaObserverKeeper
fungibleKeeper fungibleModuleKeeper.Keeper
fungibleKeeper types.FungibleKeeper
}
)

Expand All @@ -37,7 +36,7 @@ func NewKeeper(
authKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
zetaObserverKeeper types.ZetaObserverKeeper,
fungibleKeeper fungibleModuleKeeper.Keeper,
fungibleKeeper types.FungibleKeeper,
) *Keeper {
// ensure governance module account is set
// FIXME: enable this check! (disabled for now to avoid unit test panic)
Expand Down
6 changes: 3 additions & 3 deletions x/crosschain/keeper/keeper_out_tx_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func createNOutTxTracker(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.
}

func TestOutTxTrackerGet(t *testing.T) {
keeper, ctx := keepertest.ZetacoreKeeper(t)
keeper, ctx := keepertest.CrosschainKeeper(t)
items := createNOutTxTracker(keeper, ctx, 10)
for _, item := range items {
rst, found := keeper.GetOutTxTracker(ctx,
Expand All @@ -45,7 +45,7 @@ func TestOutTxTrackerGet(t *testing.T) {
}
}
func TestOutTxTrackerRemove(t *testing.T) {
k, ctx := keepertest.ZetacoreKeeper(t)
k, ctx := keepertest.CrosschainKeeper(t)
items := createNOutTxTracker(k, ctx, 10)
for _, item := range items {
k.RemoveOutTxTracker(ctx,
Expand All @@ -61,7 +61,7 @@ func TestOutTxTrackerRemove(t *testing.T) {
}

func TestOutTxTrackerGetAll(t *testing.T) {
keeper, ctx := keepertest.ZetacoreKeeper(t)
keeper, ctx := keepertest.CrosschainKeeper(t)
items := createNOutTxTracker(keeper, ctx, 10)
require.ElementsMatch(t,
nullify.Fill(items),
Expand Down
28 changes: 14 additions & 14 deletions x/crosschain/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package keeper

import (
authkeeper2 "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper2 "github.com/cosmos/cosmos-sdk/x/bank/keeper"
fungibleKeeper "github.com/zeta-chain/zetacore/x/fungible/keeper"
"github.com/zeta-chain/zetacore/x/observer/keeper"
"testing"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmdb "github.com/tendermint/tm-db"
"github.com/zeta-chain/zetacore/x/crosschain/types"

typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/zeta-chain/zetacore/x/crosschain/types"
fungiblekeeper "github.com/zeta-chain/zetacore/x/fungible/keeper"
"github.com/zeta-chain/zetacore/x/observer/keeper"
)

func setupKeeper(t testing.TB) (*Keeper, sdk.Context) {
Expand All @@ -41,21 +41,21 @@ func setupKeeper(t testing.TB) (*Keeper, sdk.Context) {
memStoreKey,
"ZetacoreParams",
)
bankkeeper := bankkeeper2.BaseKeeper{}
authkeeper := authkeeper2.AccountKeeper{}
zetaobserverKeeper := keeper.Keeper{}
fungibleKeeper := fungibleKeeper.Keeper{}
bankKeeper := bankkeeper.BaseKeeper{}
authKeeper := authkeeper.AccountKeeper{}
observerKeeper := keeper.Keeper{}
fungibleKeeper := fungiblekeeper.Keeper{}

k := NewKeeper(
codec.NewProtoCodec(registry),
storeKey,
memStoreKey,
stakingkeeper.Keeper{}, // custom
paramsSubspace,
authkeeper,
bankkeeper,
zetaobserverKeeper,
fungibleKeeper,
authKeeper,
bankKeeper,
observerKeeper,
&fungibleKeeper,
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
49 changes: 47 additions & 2 deletions x/crosschain/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package types

import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
eth "github.com/ethereum/go-ethereum/common"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/zeta-chain/zetacore/common"

fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types"
zetaObserverTypes "github.com/zeta-chain/zetacore/x/observer/types"
)

Expand Down Expand Up @@ -54,8 +60,47 @@ type ZetaObserverKeeper interface {
SetKeygen(ctx sdk.Context, keygen zetaObserverTypes.Keygen)
SetPermissionFlags(ctx sdk.Context, permissionFlags zetaObserverTypes.PermissionFlags)
SetLastObserverCount(ctx sdk.Context, lbc *zetaObserverTypes.LastObserverCount)
AddVoteToBallot(ctx sdk.Context, ballot zetaObserverTypes.Ballot, address string, observationType zetaObserverTypes.VoteType) (zetaObserverTypes.Ballot, error)
AddVoteToBallot(
ctx sdk.Context,
ballot zetaObserverTypes.Ballot,
address string,
observationType zetaObserverTypes.VoteType,
) (zetaObserverTypes.Ballot, error)
CheckIfFinalizingVote(ctx sdk.Context, ballot zetaObserverTypes.Ballot) (zetaObserverTypes.Ballot, bool)
IsAuthorized(ctx sdk.Context, address string, chain *common.Chain) (bool, error)
FindBallot(ctx sdk.Context, index string, chain *common.Chain, observationType zetaObserverTypes.ObservationType) (ballot zetaObserverTypes.Ballot, isNew bool, err error)
FindBallot(
ctx sdk.Context,
index string,
chain *common.Chain,
observationType zetaObserverTypes.ObservationType,
) (ballot zetaObserverTypes.Ballot, isNew bool, err error)
}

type FungibleKeeper interface {
GetForeignCoins(ctx sdk.Context, zrc20Addr string) (val fungibletypes.ForeignCoins, found bool)
GetAllForeignCoinsForChain(ctx sdk.Context, foreignChainID int64) (list []fungibletypes.ForeignCoins)
GetSystemContract(ctx sdk.Context) (val fungibletypes.SystemContract, found bool)
QuerySystemContractGasCoinZRC20(ctx sdk.Context, chainID *big.Int) (eth.Address, error)
QueryUniswapv2RouterGetAmountsIn(ctx sdk.Context, amountOut *big.Int, outZRC4 eth.Address) (*big.Int, error)
SetGasPrice(ctx sdk.Context, chainID *big.Int, gasPrice *big.Int) (uint64, error)
DepositCoinZeta(ctx sdk.Context, to eth.Address, amount *big.Int) error
ZRC20DepositAndCallContract(
ctx sdk.Context,
to eth.Address,
amount *big.Int,
senderChain *common.Chain,
message string,
contract eth.Address,
data []byte,
coinType common.CoinType,
asset string,
) (*evmtypes.MsgEthereumTxResponse, error)
CallUniswapv2RouterSwapExactETHForToken(
ctx sdk.Context,
sender eth.Address,
to eth.Address,
amountIn *big.Int,
outZRC4 eth.Address,
) ([]*big.Int, error)
CallZRC20Burn(ctx sdk.Context, sender eth.Address, zrc20address eth.Address, amount *big.Int) error
}

0 comments on commit b77c02f

Please sign in to comment.