Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: inbound and outbound vote processing #1853

Merged
merged 44 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
466b3b8
refactor inbound vote
kingpinXD Mar 5, 2024
46366f8
add unit tests for ProcessZEVMDeposit
kingpinXD Mar 6, 2024
398ee79
creating mocking functions for test refactor
kingpinXD Mar 6, 2024
595fd36
add tests for Process message passing
kingpinXD Mar 6, 2024
842fcb1
move cointype to cctx struct and refactor tests to accomodate the change
kingpinXD Mar 6, 2024
822d4d4
add defer to recover from panic if config is already set
kingpinXD Mar 6, 2024
b228a28
add more unit tests
kingpinXD Mar 7, 2024
f285fe4
add validate
kingpinXD Mar 7, 2024
806a6f5
add validate function for cctx
kingpinXD Mar 8, 2024
d816489
add validate function for cctx
kingpinXD Mar 8, 2024
be6dc4e
add validate unit tests
kingpinXD Mar 8, 2024
639f2a4
add migration test
kingpinXD Mar 9, 2024
1c1fea6
set module version
kingpinXD Mar 9, 2024
4cdfaf8
fix unit tests
kingpinXD Mar 9, 2024
8d17e2d
remove unnecessary test
kingpinXD Mar 9, 2024
eb51c89
update upgrade docker file
kingpinXD Mar 11, 2024
ce158e1
rebase develop
kingpinXD Mar 11, 2024
77ae684
generate files
kingpinXD Mar 11, 2024
550f454
Merge branch 'develop' into refactor-inbound-vote
kingpinXD Mar 12, 2024
ea1812e
modify logs
kingpinXD Mar 12, 2024
4080792
ignore address validation for zetachain
kingpinXD Mar 12, 2024
5f4f37b
ignore address validation for zetachain
kingpinXD Mar 13, 2024
117fe62
ignore tx hash check
kingpinXD Mar 13, 2024
f59f1a6
Merge branch 'develop' into refactor-inbound-vote
kingpinXD Mar 18, 2024
152a3de
refactor: outboud vote (#1886)
kingpinXD Mar 18, 2024
69e3f33
remove changes to cctx struct
kingpinXD Mar 18, 2024
267dbce
modify changelog
kingpinXD Mar 18, 2024
8e35f5b
Merge branch 'develop' into refactor-inbound-vote
kingpinXD Mar 19, 2024
34da80f
add tests for types package
kingpinXD Mar 20, 2024
d432e40
add comments
kingpinXD Mar 20, 2024
071ea29
resolve comments
kingpinXD Mar 21, 2024
b79405d
modify comments based on suggestions
kingpinXD Mar 22, 2024
b67126a
add error message for add revert
kingpinXD Mar 22, 2024
2c856b0
replace broken eth rpc
kingpinXD Mar 22, 2024
6af6ba2
directly modify fungible state in SetupStateForProcessLogsZetaSent
kingpinXD Mar 22, 2024
effde3f
Rebase develop
kingpinXD Mar 22, 2024
861b635
set withdrawals to pending outbound
kingpinXD Mar 25, 2024
3390b3d
append status message if not empty
kingpinXD Mar 25, 2024
b868315
rebase develop 2
kingpinXD Mar 25, 2024
f1cdbf9
Merge branch 'develop' into refactor-inbound-vote
lumtis Mar 26, 2024
a5a43e9
Update changelog.md
kingpinXD Mar 26, 2024
40c8aac
add issue https://github.com/zeta-chain/node/issues/1951
kingpinXD Mar 26, 2024
7e2ac61
adjust changelog
kingpinXD Mar 28, 2024
a89f2ba
rebase develop 3
kingpinXD Apr 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
// 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 @@ -52,6 +53,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 @@ -69,6 +84,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
Loading