Skip to content

Commit

Permalink
Simplify E2E
Browse files Browse the repository at this point in the history
  • Loading branch information
swift1337 committed Oct 4, 2024
1 parent f5a4701 commit 2de851d
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 61 deletions.
28 changes: 4 additions & 24 deletions e2e/e2etests/test_ton_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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)
Expand Down
23 changes: 4 additions & 19 deletions e2e/e2etests/test_ton_deposit_and_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions e2e/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion e2e/runner/setup_ton.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down
59 changes: 59 additions & 0 deletions e2e/runner/ton.go
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 10 additions & 11 deletions e2e/runner/zeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 2 additions & 4 deletions x/observer/keeper/msg_server_vote_blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand Down

0 comments on commit 2de851d

Please sign in to comment.