Skip to content

Commit

Permalink
Merge branch 'develop' into test-crosschain-module
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Apr 2, 2024
2 parents 032d446 + a607ebb commit c8de223
Show file tree
Hide file tree
Showing 68 changed files with 2,826 additions and 1,838 deletions.
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# CHANGELOG

## Unreleased

### Breaking Changes

* Admin policies have been moved from `observer` to a new module `authority`.
Expand All @@ -22,6 +21,8 @@
* [1914](https://github.com/zeta-chain/node/pull/1914) - move crosschain flags to core context in zetaclient
* [1948](https://github.com/zeta-chain/node/pull/1948) - remove deprecated GetTSSAddress query in crosschain module
* [1936](https://github.com/zeta-chain/node/pull/1936) - refactor common package into subpackages and rename to pkg
* [1853](https://github.com/zeta-chain/node/pull/1853) - refactor vote inbound tx and vote outbound tx


### Features

Expand Down
2 changes: 2 additions & 0 deletions proto/crosschain/cross_chain_tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package zetachain.zetacore.crosschain;
import "gogoproto/gogo.proto";
import "pkg/coin/coin.proto";

//TODO : fix the descriptor numbers for the fields
// https://github.com/zeta-chain/node/issues/1951
option go_package = "github.com/zeta-chain/zetacore/x/crosschain/types";

enum CctxStatus {
Expand Down
11 changes: 11 additions & 0 deletions testutil/keeper/authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,14 @@ func AuthorityKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
func MockIsAuthorized(m *mock.Mock, address string, policyType types.PolicyType, isAuthorized bool) {
m.On("IsAuthorized", mock.Anything, address, policyType).Return(isAuthorized).Once()
}

func SetAdminPolices(ctx sdk.Context, ak *keeper.Keeper) string {
admin := sample.AccAddress()
ak.SetPolicies(ctx, types.Policies{Items: []*types.Policy{
{
Address: admin,
PolicyType: types.PolicyType_groupAdmin,
},
}})
return admin
}
116 changes: 116 additions & 0 deletions testutil/keeper/crosschain.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package keeper

import (
"math/big"
"testing"

"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
ethcommon "github.com/ethereum/go-ethereum/common"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
tmdb "github.com/tendermint/tm-db"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/coin"
crosschainmocks "github.com/zeta-chain/zetacore/testutil/keeper/mocks/crosschain"
"github.com/zeta-chain/zetacore/testutil/sample"
"github.com/zeta-chain/zetacore/x/crosschain/keeper"
"github.com/zeta-chain/zetacore/x/crosschain/types"
fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
)

type CrosschainMockOptions struct {
Expand Down Expand Up @@ -183,3 +192,110 @@ func GetCrosschainFungibleMock(t testing.TB, keeper *keeper.Keeper) *crosschainm
require.True(t, ok)
return cfk
}

func MockGetSupportedChainFromChainID(m *crosschainmocks.CrosschainObserverKeeper, senderChain *chains.Chain) {
m.On("GetSupportedChainFromChainID", mock.Anything, senderChain.ChainId).
Return(senderChain).Once()

}
func MockGetRevertGasLimitForERC20(m *crosschainmocks.CrosschainFungibleKeeper, asset string, senderChain chains.Chain) {
m.On("GetForeignCoinFromAsset", mock.Anything, asset, senderChain.ChainId).
Return(fungibletypes.ForeignCoins{
Zrc20ContractAddress: sample.EthAddress().String(),
}, true).Once()
m.On("QueryGasLimit", mock.Anything, mock.Anything).
Return(big.NewInt(100), nil).Once()

}
func MockPayGasAndUpdateCCTX(m *crosschainmocks.CrosschainFungibleKeeper, m2 *crosschainmocks.CrosschainObserverKeeper, ctx sdk.Context, k keeper.Keeper, senderChain chains.Chain, asset string) {
m2.On("GetSupportedChainFromChainID", mock.Anything, senderChain.ChainId).
Return(&senderChain).Twice()
m.On("GetForeignCoinFromAsset", mock.Anything, asset, senderChain.ChainId).
Return(fungibletypes.ForeignCoins{
Zrc20ContractAddress: sample.EthAddress().String(),
}, true).Once()
m.On("QuerySystemContractGasCoinZRC20", mock.Anything, mock.Anything).
Return(ethcommon.Address{}, nil).Once()
m.On("QueryGasLimit", mock.Anything, mock.Anything).
Return(big.NewInt(100), nil).Once()
m.On("QueryProtocolFlatFee", mock.Anything, mock.Anything).
Return(big.NewInt(1), nil).Once()
k.SetGasPrice(ctx, types.GasPrice{
ChainId: senderChain.ChainId,
MedianIndex: 0,
Prices: []uint64{1},
})

m.On("QueryUniswapV2RouterGetZRC4ToZRC4AmountsIn", mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(big.NewInt(0), nil).Once()
m.On("DepositZRC20", mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(&evmtypes.MsgEthereumTxResponse{}, nil)
m.On("GetUniswapV2Router02Address", mock.Anything).
Return(ethcommon.Address{}, nil).Once()
m.On("CallZRC20Approve", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(nil).Once()
m.On("CallUniswapV2RouterSwapExactTokensForTokens", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return([]*big.Int{big.NewInt(0), big.NewInt(1), big.NewInt(1000)}, nil).Once()
m.On("CallZRC20Burn", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(nil).Once()

}

func MockUpdateNonce(m *crosschainmocks.CrosschainObserverKeeper, senderChain chains.Chain) (nonce uint64) {
nonce = uint64(1)
tss := sample.Tss()
m.On("GetSupportedChainFromChainID", mock.Anything, senderChain.ChainId).
Return(senderChain)
m.On("GetChainNonces", mock.Anything, senderChain.ChainName.String()).
Return(observertypes.ChainNonces{Nonce: nonce}, true)
m.On("GetTSS", mock.Anything).
Return(tss, true)
m.On("GetPendingNonces", mock.Anything, tss.TssPubkey, mock.Anything).
Return(observertypes.PendingNonces{NonceHigh: int64(nonce)}, true)
m.On("SetChainNonces", mock.Anything, mock.Anything)
m.On("SetPendingNonces", mock.Anything, mock.Anything)
return
}

func MockRevertForHandleEVMDeposit(m *crosschainmocks.CrosschainFungibleKeeper, receiver ethcommon.Address, amount *big.Int, senderChainID int64, errDeposit error) {
m.On(
"ZRC20DepositAndCallContract",
mock.Anything,
mock.Anything,
receiver,
amount,
senderChainID,
mock.Anything,
coin.CoinType_ERC20,
mock.Anything,
).Return(&evmtypes.MsgEthereumTxResponse{VmError: "reverted"}, false, errDeposit)
}

func MockVoteOnOutboundSuccessBallot(m *crosschainmocks.CrosschainObserverKeeper, ctx sdk.Context, cctx *types.CrossChainTx, senderChain chains.Chain, observer string) {
m.On("VoteOnOutboundBallot", ctx, mock.Anything, cctx.GetCurrentOutTxParam().ReceiverChainId, chains.ReceiveStatus_Success, observer).
Return(true, true, observertypes.Ballot{BallotStatus: observertypes.BallotStatus_BallotFinalized_SuccessObservation}, senderChain.ChainName.String(), nil).Once()
}

func MockVoteOnOutboundFailedBallot(m *crosschainmocks.CrosschainObserverKeeper, ctx sdk.Context, cctx *types.CrossChainTx, senderChain chains.Chain, observer string) {
m.On("VoteOnOutboundBallot", ctx, mock.Anything, cctx.GetCurrentOutTxParam().ReceiverChainId, chains.ReceiveStatus_Failed, observer).
Return(true, true, observertypes.Ballot{BallotStatus: observertypes.BallotStatus_BallotFinalized_FailureObservation}, senderChain.ChainName.String(), nil).Once()
}

func MockGetOutBound(m *crosschainmocks.CrosschainObserverKeeper, ctx sdk.Context) {
m.On("GetTSS", ctx).Return(observertypes.TSS{}, true).Once()
}

func MockSaveOutBound(m *crosschainmocks.CrosschainObserverKeeper, ctx sdk.Context, cctx *types.CrossChainTx, tss observertypes.TSS) {
m.On("RemoveFromPendingNonces",
ctx, tss.TssPubkey, cctx.GetCurrentOutTxParam().ReceiverChainId, mock.Anything).
Return().Once()
m.On("GetTSS", ctx).Return(observertypes.TSS{}, true)
}

func MockSaveOutBoundNewRevertCreated(m *crosschainmocks.CrosschainObserverKeeper, ctx sdk.Context, cctx *types.CrossChainTx, tss observertypes.TSS) {
m.On("RemoveFromPendingNonces",
ctx, tss.TssPubkey, cctx.GetCurrentOutTxParam().ReceiverChainId, mock.Anything).
Return().Once()
m.On("GetTSS", ctx).Return(observertypes.TSS{}, true)
m.On("SetNonceToCctx", mock.Anything, mock.Anything).Return().Once()
}
31 changes: 31 additions & 0 deletions testutil/sample/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/coin"
"github.com/zeta-chain/zetacore/x/crosschain/types"
)
Expand Down Expand Up @@ -62,6 +63,20 @@ func InboundTxParams(r *rand.Rand) *types.InboundTxParams {
}
}

func InboundTxParamsValidChainID(r *rand.Rand) *types.InboundTxParams {
return &types.InboundTxParams{
Sender: EthAddress().String(),
SenderChainId: chains.GoerliChain().ChainId,
TxOrigin: EthAddress().String(),
Asset: StringRandom(r, 32),
Amount: math.NewUint(uint64(r.Int63())),
InboundTxObservedHash: StringRandom(r, 32),
InboundTxObservedExternalHeight: r.Uint64(),
InboundTxBallotIndex: StringRandom(r, 32),
InboundTxFinalizedZetaHeight: r.Uint64(),
}
}

func OutboundTxParams(r *rand.Rand) *types.OutboundTxParams {
return &types.OutboundTxParams{
Receiver: EthAddress().String(),
Expand All @@ -79,6 +94,22 @@ func OutboundTxParams(r *rand.Rand) *types.OutboundTxParams {
}
}

func OutboundTxParamsValidChainID(r *rand.Rand) *types.OutboundTxParams {
return &types.OutboundTxParams{
Receiver: EthAddress().String(),
ReceiverChainId: chains.GoerliChain().ChainId,
Amount: math.NewUint(uint64(r.Int63())),
OutboundTxTssNonce: r.Uint64(),
OutboundTxGasLimit: r.Uint64(),
OutboundTxGasPrice: math.NewUint(uint64(r.Int63())).String(),
OutboundTxHash: StringRandom(r, 32),
OutboundTxBallotIndex: StringRandom(r, 32),
OutboundTxObservedExternalHeight: r.Uint64(),
OutboundTxGasUsed: r.Uint64(),
OutboundTxEffectiveGasPrice: math.NewInt(r.Int63()),
}
}

func Status(t *testing.T, index string) *types.Status {
r := newRandFromStringSeed(t, index)

Expand Down
7 changes: 7 additions & 0 deletions testutil/sample/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

sdkmath "cosmossdk.io/math"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/zeta-chain/zetacore/cmd/zetacored/config"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
Expand Down Expand Up @@ -111,6 +112,12 @@ func Hash() ethcommon.Hash {
return EthAddress().Hash()
}

func ZetaIndex(t *testing.T) string {
msg := CrossChainTx(t, "foo")
hash := ethcrypto.Keccak256Hash([]byte(msg.String()))
return hash.Hex()
}

// Bytes returns a sample byte array
func Bytes() []byte {
return []byte("sample")
Expand Down
2 changes: 1 addition & 1 deletion x/authority/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestGenesisState_Validate(t *testing.T) {
setConfig()
setConfig(t)

tests := []struct {
name string
Expand Down
11 changes: 8 additions & 3 deletions x/authority/types/policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ import (
)

// setConfig sets the global config to use zeta chain's bech32 prefixes
func setConfig() {
func setConfig(t *testing.T) {
defer func(t *testing.T) {
if r := recover(); r != nil {
t.Log("config is already sealed", r)
}
}(t)
cfg := sdk.GetConfig()
cfg.SetBech32PrefixForAccount(app.Bech32PrefixAccAddr, app.Bech32PrefixAccPub)
cfg.Seal()
}

func TestPolicies_Validate(t *testing.T) {
setConfig()

setConfig(t)
// use table driven tests to test the validation of policies
tests := []struct {
name string
Expand Down
Loading

0 comments on commit c8de223

Please sign in to comment.