Skip to content

Commit

Permalink
renamed unit test package as evm_test
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Mar 6, 2024
1 parent 2526747 commit 6383662
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 46 deletions.
6 changes: 6 additions & 0 deletions zetaclient/evm/evm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ func (ob *ChainClient) WithZetaClient(bridge *zetabridge.ZetaCoreBridge) {
ob.zetaClient = bridge
}

func (ob *ChainClient) WithBlockCache(cache *lru.Cache) {
ob.Mu.Lock()
defer ob.Mu.Unlock()
ob.blockCache = cache
}

func (ob *ChainClient) SetChainParams(params observertypes.ChainParams) {
ob.Mu.Lock()
defer ob.Mu.Unlock()
Expand Down
44 changes: 22 additions & 22 deletions zetaclient/evm/evm_client_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
package evm
package evm_test

import (
"math/big"
"sync"
"testing"

"cosmossdk.io/math"
ethtypes "github.com/ethereum/go-ethereum/core/types"
lru "github.com/hashicorp/golang-lru"
"github.com/onrik/ethrpc"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/x/crosschain/types"
"github.com/zeta-chain/zetacore/zetaclient/evm"
"github.com/zeta-chain/zetacore/zetaclient/testutils"
)

func TestEVM_BlockCache(t *testing.T) {
// create client
blockCache, err := lru.New(1000)
require.NoError(t, err)
ob := ChainClient{
blockCache: blockCache,
}
ob := &evm.ChainClient{Mu: &sync.Mutex{}}
ob.WithBlockCache(blockCache)

// delete non-existing block should not panic
blockNumber := int64(10388180)
// #nosec G701 possible nummber
ob.RemoveCachedBlock(uint64(blockNumber))
blockNumber := uint64(10388180)
ob.RemoveCachedBlock(blockNumber)

// add a block
header := &ethtypes.Header{
Number: big.NewInt(blockNumber),
block := &ethrpc.Block{
// #nosec G701 always in range
Number: int(blockNumber),
}
block := ethtypes.NewBlock(header, nil, nil, nil, nil)
ob.blockCache.Add(blockNumber, block)
blockCache.Add(blockNumber, block)
ob.WithBlockCache(blockCache)

// block should be in cache
_, ok := ob.blockCache.Get(blockNumber)
require.True(t, ok)
_, err = ob.GetBlockByNumberCached(blockNumber)
require.NoError(t, err)

// delete the block should not panic
ob.RemoveCachedBlock(uint64(blockNumber))
ob.RemoveCachedBlock(blockNumber)
}

func TestEVM_CheckTxInclusion(t *testing.T) {
Expand All @@ -57,12 +57,11 @@ func TestEVM_CheckTxInclusion(t *testing.T) {
// create client
blockCache, err := lru.New(1000)
require.NoError(t, err)
ob := ChainClient{
blockCache: blockCache,
}
ob := &evm.ChainClient{Mu: &sync.Mutex{}}

// save block to cache
ob.blockCache.Add(blockNumber, block)
blockCache.Add(blockNumber, block)
ob.WithBlockCache(blockCache)

t.Run("should pass for archived outtx", func(t *testing.T) {
err := ob.CheckTxInclusion(tx, receipt)
Expand All @@ -80,14 +79,15 @@ func TestEVM_CheckTxInclusion(t *testing.T) {
// change the tx at position 'receipt.TransactionIndex' to a different tx
priorTx := block.Transactions[receipt.TransactionIndex-1]
block.Transactions[receipt.TransactionIndex] = priorTx
ob.blockCache.Add(blockNumber, block)
blockCache.Add(blockNumber, block)
ob.WithBlockCache(blockCache)

// check inclusion should fail
err := ob.CheckTxInclusion(tx, receipt)
require.ErrorContains(t, err, "has different hash")

// wrong block should be removed from cache
_, ok := ob.blockCache.Get(blockNumber)
_, ok := blockCache.Get(blockNumber)
require.False(t, ok)
})
}
Expand Down
50 changes: 26 additions & 24 deletions zetaclient/evm/inbounds_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package evm
package evm_test

import (
"encoding/hex"
Expand All @@ -12,6 +12,7 @@ import (
"github.com/zeta-chain/zetacore/common"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"github.com/zeta-chain/zetacore/zetaclient/config"
"github.com/zeta-chain/zetacore/zetaclient/evm"
"github.com/zeta-chain/zetacore/zetaclient/interfaces"
"github.com/zeta-chain/zetacore/zetaclient/testutils"
clienttypes "github.com/zeta-chain/zetacore/zetaclient/types"
Expand All @@ -22,15 +23,16 @@ func MockEVMClient(
chain common.Chain,
tss interfaces.TSSSigner,
lastBlock uint64,
params observertypes.ChainParams) *ChainClient {
return &ChainClient{
chain: chain,
Tss: tss,
Mu: &sync.Mutex{},
zetaClient: testutils.MockCoreBridge(),
lastBlock: lastBlock,
chainParams: params,
params observertypes.ChainParams) *evm.ChainClient {
client := &evm.ChainClient{
Tss: tss,
Mu: &sync.Mutex{},
}
client.WithChain(chain)
client.WithZetaClient(testutils.MockCoreBridge())
client.SetLastBlockHeight(lastBlock)
client.SetChainParams(params)
return client
}

func TestEVM_CheckAndVoteInboundTokenZeta(t *testing.T) {
Expand All @@ -42,7 +44,7 @@ func TestEVM_CheckAndVoteInboundTokenZeta(t *testing.T) {

t.Run("should pass for archived intx, receipt and cctx", func(t *testing.T) {
tx, receipt, cctx := testutils.LoadEVMIntxNReceiptNCctx(t, chainID, intxHash, common.CoinType_Zeta)
require.NoError(t, ValidateEvmTransaction(tx))
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMClient(common.EthChain(), testutils.NewMockTSSMainnet(), lastBlock, testutils.MockChainParams(chainID, confirmation))
Expand All @@ -52,7 +54,7 @@ func TestEVM_CheckAndVoteInboundTokenZeta(t *testing.T) {
})
t.Run("should fail on unconfirmed intx", func(t *testing.T) {
tx, receipt, _ := testutils.LoadEVMIntxNReceiptNCctx(t, chainID, intxHash, common.CoinType_Zeta)
require.NoError(t, ValidateEvmTransaction(tx))
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation - 1

ob := MockEVMClient(common.EthChain(), testutils.NewMockTSSMainnet(), lastBlock, testutils.MockChainParams(chainID, confirmation))
Expand All @@ -62,7 +64,7 @@ func TestEVM_CheckAndVoteInboundTokenZeta(t *testing.T) {
t.Run("should not act if no ZetaSent event", func(t *testing.T) {
tx, receipt, _ := testutils.LoadEVMIntxNReceiptNCctx(t, chainID, intxHash, common.CoinType_Zeta)
receipt.Logs = receipt.Logs[:2] // remove ZetaSent event
require.NoError(t, ValidateEvmTransaction(tx))
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMClient(common.EthChain(), testutils.NewMockTSSMainnet(), lastBlock, testutils.MockChainParams(chainID, confirmation))
Expand All @@ -73,7 +75,7 @@ func TestEVM_CheckAndVoteInboundTokenZeta(t *testing.T) {
t.Run("should not act if emitter is not ZetaConnector", func(t *testing.T) {
tx, receipt, _ := testutils.LoadEVMIntxNReceiptNCctx(t, chainID, intxHash, common.CoinType_Zeta)
chainID = 56 // use BSC chain connector
require.NoError(t, ValidateEvmTransaction(tx))
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMClient(common.EthChain(), testutils.NewMockTSSMainnet(), lastBlock, testutils.MockChainParams(chainID, confirmation))
Expand All @@ -91,7 +93,7 @@ func TestEVM_CheckAndVoteInboundTokenERC20(t *testing.T) {

t.Run("should pass for archived intx, receipt and cctx", func(t *testing.T) {
tx, receipt, cctx := testutils.LoadEVMIntxNReceiptNCctx(t, chainID, intxHash, common.CoinType_ERC20)
require.NoError(t, ValidateEvmTransaction(tx))
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMClient(common.EthChain(), testutils.NewMockTSSMainnet(), lastBlock, testutils.MockChainParams(chainID, confirmation))
Expand All @@ -101,7 +103,7 @@ func TestEVM_CheckAndVoteInboundTokenERC20(t *testing.T) {
})
t.Run("should fail on unconfirmed intx", func(t *testing.T) {
tx, receipt, _ := testutils.LoadEVMIntxNReceiptNCctx(t, chainID, intxHash, common.CoinType_ERC20)
require.NoError(t, ValidateEvmTransaction(tx))
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation - 1

ob := MockEVMClient(common.EthChain(), testutils.NewMockTSSMainnet(), lastBlock, testutils.MockChainParams(chainID, confirmation))
Expand All @@ -111,7 +113,7 @@ func TestEVM_CheckAndVoteInboundTokenERC20(t *testing.T) {
t.Run("should not act if no Deposit event", func(t *testing.T) {
tx, receipt, _ := testutils.LoadEVMIntxNReceiptNCctx(t, chainID, intxHash, common.CoinType_ERC20)
receipt.Logs = receipt.Logs[:1] // remove Deposit event
require.NoError(t, ValidateEvmTransaction(tx))
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMClient(common.EthChain(), testutils.NewMockTSSMainnet(), lastBlock, testutils.MockChainParams(chainID, confirmation))
Expand All @@ -122,7 +124,7 @@ func TestEVM_CheckAndVoteInboundTokenERC20(t *testing.T) {
t.Run("should not act if emitter is not ERC20 Custody", func(t *testing.T) {
tx, receipt, _ := testutils.LoadEVMIntxNReceiptNCctx(t, chainID, intxHash, common.CoinType_ERC20)
chainID = 56 // use BSC chain ERC20 custody
require.NoError(t, ValidateEvmTransaction(tx))
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMClient(common.EthChain(), testutils.NewMockTSSMainnet(), lastBlock, testutils.MockChainParams(chainID, confirmation))
Expand All @@ -140,7 +142,7 @@ func TestEVM_CheckAndVoteInboundTokenGas(t *testing.T) {

t.Run("should pass for archived intx, receipt and cctx", func(t *testing.T) {
tx, receipt, cctx := testutils.LoadEVMIntxNReceiptNCctx(t, chainID, intxHash, common.CoinType_Gas)
require.NoError(t, ValidateEvmTransaction(tx))
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMClient(common.EthChain(), testutils.NewMockTSSMainnet(), lastBlock, testutils.MockChainParams(chainID, confirmation))
Expand All @@ -150,7 +152,7 @@ func TestEVM_CheckAndVoteInboundTokenGas(t *testing.T) {
})
t.Run("should fail on unconfirmed intx", func(t *testing.T) {
tx, receipt, _ := testutils.LoadEVMIntxNReceiptNCctx(t, chainID, intxHash, common.CoinType_Gas)
require.NoError(t, ValidateEvmTransaction(tx))
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation - 1

ob := MockEVMClient(common.EthChain(), testutils.NewMockTSSMainnet(), lastBlock, testutils.MockChainParams(chainID, confirmation))
Expand All @@ -160,7 +162,7 @@ func TestEVM_CheckAndVoteInboundTokenGas(t *testing.T) {
t.Run("should not act if receiver is not TSS", func(t *testing.T) {
tx, receipt, _ := testutils.LoadEVMIntxNReceiptNCctx(t, chainID, intxHash, common.CoinType_Gas)
tx.To = testutils.OtherAddress // use other address
require.NoError(t, ValidateEvmTransaction(tx))
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMClient(common.EthChain(), testutils.NewMockTSSMainnet(), lastBlock, testutils.MockChainParams(chainID, confirmation))
Expand All @@ -171,7 +173,7 @@ func TestEVM_CheckAndVoteInboundTokenGas(t *testing.T) {
t.Run("should not act if transaction failed", func(t *testing.T) {
tx, receipt, _ := testutils.LoadEVMIntxNReceiptNCctx(t, chainID, intxHash, common.CoinType_Gas)
receipt.Status = ethtypes.ReceiptStatusFailed
require.NoError(t, ValidateEvmTransaction(tx))
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMClient(common.EthChain(), testutils.NewMockTSSMainnet(), lastBlock, testutils.MockChainParams(chainID, confirmation))
Expand All @@ -182,7 +184,7 @@ func TestEVM_CheckAndVoteInboundTokenGas(t *testing.T) {
t.Run("should not act on nil message", func(t *testing.T) {
tx, receipt, _ := testutils.LoadEVMIntxNReceiptNCctx(t, chainID, intxHash, common.CoinType_Gas)
tx.Input = hex.EncodeToString([]byte(common.DonationMessage)) // donation will result in nil message
require.NoError(t, ValidateEvmTransaction(tx))
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMClient(common.EthChain(), testutils.NewMockTSSMainnet(), lastBlock, testutils.MockChainParams(chainID, confirmation))
Expand Down Expand Up @@ -288,14 +290,14 @@ func TestEVM_BuildInboundVoteMsgForTokenSentToTSS(t *testing.T) {
chainID := int64(1)
intxHash := "0xeaec67d5dd5d85f27b21bef83e01cbdf59154fd793ea7a22c297f7c3a722c532"
tx, receipt := testutils.LoadEVMIntxNReceipt(t, chainID, intxHash, common.CoinType_Gas)
require.NoError(t, ValidateEvmTransaction(tx))
require.NoError(t, evm.ValidateEvmTransaction(tx))
cctx := testutils.LoadEVMIntxCctx(t, chainID, intxHash, common.CoinType_Gas)

// load archived gas token donation to TSS
// https://etherscan.io/tx/0x52f214cf7b10be71f4d274193287d47bc9632b976e69b9d2cdeb527c2ba32155
inTxHashDonation := "0x52f214cf7b10be71f4d274193287d47bc9632b976e69b9d2cdeb527c2ba32155"
txDonation, receiptDonation := testutils.LoadEVMIntxNReceiptDonation(t, chainID, inTxHashDonation, common.CoinType_Gas)
require.NoError(t, ValidateEvmTransaction(txDonation))
require.NoError(t, evm.ValidateEvmTransaction(txDonation))

// create test compliance config
ob := MockEVMClient(common.EthChain(), nil, 1, testutils.MockChainParams(1, 1))
Expand Down

0 comments on commit 6383662

Please sign in to comment.