From 2de851dc020b68814a952b1c60a533cc93593676 Mon Sep 17 00:00:00 2001 From: Dmitry S <11892559+swift1337@users.noreply.github.com> Date: Fri, 4 Oct 2024 14:55:30 +0200 Subject: [PATCH] Simplify E2E --- e2e/e2etests/test_ton_deposit.go | 28 ++-------- e2e/e2etests/test_ton_deposit_and_call.go | 23 ++------- e2e/runner/runner.go | 4 +- e2e/runner/setup_ton.go | 3 +- e2e/runner/ton.go | 59 ++++++++++++++++++++++ e2e/runner/zeta.go | 21 ++++---- x/observer/keeper/msg_server_vote_blame.go | 6 +-- 7 files changed, 83 insertions(+), 61 deletions(-) create mode 100644 e2e/runner/ton.go diff --git a/e2e/e2etests/test_ton_deposit.go b/e2e/e2etests/test_ton_deposit.go index bc4b5c970d..b4c7a32168 100644 --- a/e2e/e2etests/test_ton_deposit.go +++ b/e2e/e2etests/test_ton_deposit.go @@ -10,17 +10,10 @@ import ( "github.com/zeta-chain/node/e2e/runner" "github.com/zeta-chain/node/e2e/runner/ton" "github.com/zeta-chain/node/pkg/chains" - toncontracts "github.com/zeta-chain/node/pkg/contracts/ton" "github.com/zeta-chain/node/testutil/sample" - crosschainTypes "github.com/zeta-chain/node/x/crosschain/types" + cctypes "github.com/zeta-chain/node/x/crosschain/types" ) -// we need to use this send mode due to how wallet V5 works -// -// https://github.com/tonkeeper/w5/blob/main/contracts/wallet_v5.fc#L82 -// https://docs.ton.org/develop/smart-contracts/guidelines/message-modes-cookbook -const tonDepositSendCode = toncontracts.SendFlagSeparateFees + toncontracts.SendFlagIgnoreErrors - func TestTONDeposit(r *runner.E2ERunner, args []string) { require.Len(r, args, 1) @@ -34,9 +27,6 @@ func TestTONDeposit(r *runner.E2ERunner, args []string) { // (will be optimized & dynamic in the future) depositFee := math.NewUint(10_000_000) - // Given TON Gateway - gw := toncontracts.NewGateway(r.TONGateway) - // Given sample wallet with a balance of 50 TON sender, err := deployer.CreateWallet(ctx, ton.TONCoins(50)) require.NoError(r, err) @@ -45,28 +35,18 @@ func TestTONDeposit(r *runner.E2ERunner, args []string) { recipient := sample.EthAddress() // ACT - r.Logger.Info( - "Sending deposit of %s TON from %s to zEVM %s", - amount.String(), - sender.GetAddress().ToRaw(), - recipient.Hex(), - ) - - err = gw.SendDeposit(ctx, sender, amount, recipient, tonDepositSendCode) + err = r.TONDeposit(sender, amount, recipient) // ASSERT require.NoError(r, err) // Wait for CCTX mining - filter := func(cctx *crosschainTypes.CrossChainTx) bool { + filter := func(cctx *cctypes.CrossChainTx) bool { return cctx.InboundParams.SenderChainId == chain.ChainId && cctx.InboundParams.Sender == sender.GetAddress().ToRaw() } - cctxs := r.WaitForSpecificCCTX(filter, time.Minute) - require.Len(r, cctxs, 1) - - cctx := r.WaitForMinedCCTXFromIndex(cctxs[0].Index) + cctx := r.WaitForSpecificCCTX(filter, time.Minute) // Check CCTX expectedDeposit := amount.Sub(depositFee) diff --git a/e2e/e2etests/test_ton_deposit_and_call.go b/e2e/e2etests/test_ton_deposit_and_call.go index a612794e9a..b385b19f49 100644 --- a/e2e/e2etests/test_ton_deposit_and_call.go +++ b/e2e/e2etests/test_ton_deposit_and_call.go @@ -11,9 +11,8 @@ import ( "github.com/zeta-chain/node/e2e/runner/ton" "github.com/zeta-chain/node/e2e/utils" "github.com/zeta-chain/node/pkg/chains" - toncontracts "github.com/zeta-chain/node/pkg/contracts/ton" testcontract "github.com/zeta-chain/node/testutil/contracts" - crosschainTypes "github.com/zeta-chain/node/x/crosschain/types" + cctypes "github.com/zeta-chain/node/x/crosschain/types" ) func TestTONDepositAndCall(r *runner.E2ERunner, args []string) { @@ -29,9 +28,6 @@ func TestTONDepositAndCall(r *runner.E2ERunner, args []string) { // (will be optimized & dynamic in the future) depositFee := math.NewUint(10_000_000) - // Given TON Gateway - gw := toncontracts.NewGateway(r.TONGateway) - // Given sample wallet with a balance of 50 TON sender, err := deployer.CreateWallet(ctx, ton.TONCoins(50)) require.NoError(r, err) @@ -45,29 +41,18 @@ func TestTONDepositAndCall(r *runner.E2ERunner, args []string) { callData := []byte("hello from TON!") // ACT - r.Logger.Info( - "Sending deposit of %s TON from %s to zEVM %s and calling contract with %q", - amount.String(), - sender.GetAddress().ToRaw(), - contractAddr.Hex(), - string(callData), - ) - - err = gw.SendDepositAndCall(ctx, sender, amount, contractAddr, callData, tonDepositSendCode) + err = r.TONDepositAndCall(sender, amount, contractAddr, callData) // ASSERT require.NoError(r, err) // Wait for CCTX mining - filter := func(cctx *crosschainTypes.CrossChainTx) bool { + filter := func(cctx *cctypes.CrossChainTx) bool { return cctx.InboundParams.SenderChainId == chain.ChainId && cctx.InboundParams.Sender == sender.GetAddress().ToRaw() } - cctxs := r.WaitForSpecificCCTX(filter, time.Minute) - require.Len(r, cctxs, 1) - - r.WaitForMinedCCTXFromIndex(cctxs[0].Index) + r.WaitForSpecificCCTX(filter, time.Minute) expectedDeposit := amount.Sub(depositFee) diff --git a/e2e/runner/runner.go b/e2e/runner/runner.go index ca3e633a55..03cafb6fc4 100644 --- a/e2e/runner/runner.go +++ b/e2e/runner/runner.go @@ -18,7 +18,6 @@ import ( "github.com/ethereum/go-ethereum/ethclient" "github.com/gagliardetto/solana-go" "github.com/gagliardetto/solana-go/rpc" - "github.com/tonkeeper/tongo/ton" "github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/evm/erc20custody.sol" zetaeth "github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/evm/zeta.eth.sol" zetaconnectoreth "github.com/zeta-chain/protocol-contracts/v1/pkg/contracts/evm/zetaconnector.eth.sol" @@ -40,6 +39,7 @@ import ( "github.com/zeta-chain/node/e2e/txserver" "github.com/zeta-chain/node/e2e/utils" "github.com/zeta-chain/node/pkg/contracts/testdappv2" + toncontracts "github.com/zeta-chain/node/pkg/contracts/ton" authoritytypes "github.com/zeta-chain/node/x/authority/types" crosschaintypes "github.com/zeta-chain/node/x/crosschain/types" fungibletypes "github.com/zeta-chain/node/x/fungible/types" @@ -73,7 +73,7 @@ type E2ERunner struct { BTCDeployerAddress *btcutil.AddressWitnessPubKeyHash SolanaDeployerAddress solana.PublicKey TONDeployer *tonrunner.Deployer - TONGateway ton.AccountID + TONGateway *toncontracts.Gateway // all clients. // a reference to this type is required to enable creating a new E2ERunner. diff --git a/e2e/runner/setup_ton.go b/e2e/runner/setup_ton.go index 19e91d2608..62d89c3329 100644 --- a/e2e/runner/setup_ton.go +++ b/e2e/runner/setup_ton.go @@ -10,6 +10,7 @@ import ( "github.com/zeta-chain/node/e2e/utils" "github.com/zeta-chain/node/pkg/chains" "github.com/zeta-chain/node/pkg/constant" + toncontracts "github.com/zeta-chain/node/pkg/contracts/ton" observertypes "github.com/zeta-chain/node/x/observer/types" ) @@ -65,7 +66,7 @@ func (r *E2ERunner) SetupTON() error { } r.TONDeployer = deployer - r.TONGateway = gwAccount.ID + r.TONGateway = toncontracts.NewGateway(gwAccount.ID) return r.ensureTONChainParams(gwAccount) } diff --git a/e2e/runner/ton.go b/e2e/runner/ton.go new file mode 100644 index 0000000000..8746e25977 --- /dev/null +++ b/e2e/runner/ton.go @@ -0,0 +1,59 @@ +package runner + +import ( + "cosmossdk.io/math" + eth "github.com/ethereum/go-ethereum/common" + "github.com/stretchr/testify/require" + "github.com/tonkeeper/tongo/wallet" + + toncontracts "github.com/zeta-chain/node/pkg/contracts/ton" +) + +// we need to use this send mode due to how wallet V5 works +// +// https://github.com/tonkeeper/w5/blob/main/contracts/wallet_v5.fc#L82 +// https://docs.ton.org/develop/smart-contracts/guidelines/message-modes-cookbook +const tonDepositSendCode = toncontracts.SendFlagSeparateFees + toncontracts.SendFlagIgnoreErrors + +// TONDeposit deposit TON to Gateway contract +func (r *E2ERunner) TONDeposit(sender *wallet.Wallet, amount math.Uint, zevmRecipient eth.Address) error { + require.NotNil(r, r.TONGateway, "TON Gateway is not initialized") + + require.NotNil(r, sender, "Sender wallet is nil") + require.False(r, amount.IsZero()) + require.NotEqual(r, (eth.Address{}).String(), zevmRecipient.String()) + + r.Logger.Info( + "Sending deposit of %s TON from %s to zEVM %s", + amount.String(), + sender.GetAddress().ToRaw(), + zevmRecipient.Hex(), + ) + + return r.TONGateway.SendDeposit(r.Ctx, sender, amount, zevmRecipient, tonDepositSendCode) +} + +// TONDepositAndCall deposit TON to Gateway contract with call data. +func (r *E2ERunner) TONDepositAndCall( + sender *wallet.Wallet, + amount math.Uint, + zevmRecipient eth.Address, + callData []byte, +) error { + require.NotNil(r, r.TONGateway, "TON Gateway is not initialized") + + require.NotNil(r, sender, "Sender wallet is nil") + require.False(r, amount.IsZero()) + require.NotEqual(r, (eth.Address{}).String(), zevmRecipient.String()) + require.NotEmpty(r, callData) + + r.Logger.Info( + "Sending deposit of %s TON from %s to zEVM %s and calling contract with %q", + amount.String(), + sender.GetAddress().ToRaw(), + zevmRecipient.Hex(), + string(callData), + ) + + return r.TONGateway.SendDepositAndCall(r.Ctx, sender, amount, zevmRecipient, callData, tonDepositSendCode) +} diff --git a/e2e/runner/zeta.go b/e2e/runner/zeta.go index 126f718ef3..5b977a9a31 100644 --- a/e2e/runner/zeta.go +++ b/e2e/runner/zeta.go @@ -6,6 +6,7 @@ import ( "time" "github.com/cenkalti/backoff/v4" + query "github.com/cosmos/cosmos-sdk/types/query" ethcommon "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/stretchr/testify/require" @@ -106,32 +107,30 @@ func (r *E2ERunner) WaitForMinedCCTXFromIndex(index string) *types.CrossChainTx return cctx } +// WaitForSpecificCCTX scans for cctx by filters and ensures it's mined func (r *E2ERunner) WaitForSpecificCCTX( filter func(*types.CrossChainTx) bool, timeout time.Duration, -) []types.CrossChainTx { +) *types.CrossChainTx { var ( - ctx = r.Ctx - start = time.Now() - query = &types.QueryAllCctxRequest{} - out []types.CrossChainTx + ctx = r.Ctx + start = time.Now() + reqQuery = &types.QueryAllCctxRequest{ + Pagination: &query.PageRequest{Reverse: true}, + } ) for time.Since(start) < timeout { - res, err := r.CctxClient.CctxAll(ctx, query) + res, err := r.CctxClient.CctxAll(ctx, reqQuery) require.NoError(r, err) for i := range res.CrossChainTx { tx := res.CrossChainTx[i] if filter(tx) { - out = append(out, *tx) + return r.WaitForMinedCCTXFromIndex(tx.Index) } } - if len(out) > 0 { - return out - } - time.Sleep(time.Second) } diff --git a/x/observer/keeper/msg_server_vote_blame.go b/x/observer/keeper/msg_server_vote_blame.go index 74a3654657..d320ff54db 100644 --- a/x/observer/keeper/msg_server_vote_blame.go +++ b/x/observer/keeper/msg_server_vote_blame.go @@ -6,7 +6,7 @@ import ( sdkerrors "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" - crosschainTypes "github.com/zeta-chain/node/x/crosschain/types" + cctypes "github.com/zeta-chain/node/x/crosschain/types" "github.com/zeta-chain/node/x/observer/types" ) @@ -21,9 +21,7 @@ func (k msgServer) VoteBlame( // GetChainFromChainID makes sure we are getting only supported chains , if a chain support has been turned on using gov proposal, this function returns nil observationChain, found := k.GetSupportedChainFromChainID(ctx, msg.ChainId) if !found { - return nil, sdkerrors.Wrapf( - crosschainTypes.ErrUnsupportedChain, - "%s, ChainID %d", voteBlameID, msg.ChainId) + return nil, sdkerrors.Wrapf(cctypes.ErrUnsupportedChain, "%s, ChainID %d", voteBlameID, msg.ChainId) } if ok := k.IsNonTombstonedObserver(ctx, msg.Creator); !ok {