Skip to content

Commit

Permalink
test: add crosschain and observer operations (#3207)
Browse files Browse the repository at this point in the history
* add crosschain decoders

* decoders test

* add oeprations

* add fungible deploy contracts

* add fungible deploy contracts

* add simulation for observer module

* add comments for crosschain operations

* add comments for observer and fungible operations

* generate files

* debug import export test

* fix import export tests

* fix import export tests

* fix app determinism test

* update codecov.yml

* reduce weight for DeployedSystemContracts operation

* add new function to generate eth address from provided randomness

* Update x/fungible/simulation/decoders.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update testutil/sample/sample.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* add validations

* add validations

* add randmoness to tss

* make requested changes 1

* update state.go file

* update state.go file

* change chains.IsEVMChain to  chains.IsEthereumChain in deposit tests

* add basic structure for outbound vote message

* add randomised outbound message

* add cointype randomisation to inbound message

* add cointype gas

* add cointype erc20

* add outbound tracker

* improve outbound tracker nonce selection

* remove block limit for outbound tracker

* add operation remove outbound tracker

* add operation whitelist erc20

* fix unit tests

* fix unit tests

* fix Abort CCTX test

* add operation refund aborted cctx

* add operation update rate limiter flags

* add operation UpdateErc20PauseStatus

* add additional checks to whitelist erc20

* add default gas prices to the state

* add default gas prices to the state

* update zeta accounting genesis

* update operation whitelist erc20 to not try whitelisting duplicate assets

* add check for aborted status when finalizing an outbound

* add check for aborted status when finalizing an outbound

* reduce errors for RefundAbortedCCTX

* reduce errors for RefundAbortedCCTX

* reafactor update TSS to use existing cctx

* remove setting nonce to cctx twice

* set observe count for even is observer set is empty

* add updte keygen message

* add update chain params

* add SimulateMsgResetChainNonces

* add more operations from observer module

* add helper functions

* format code

* add additional check to voting messages to for existing ballots

* add msg vote tss

* add msg vote tss

* remove isPending check for aborted cctx

* fix unit tests

* add comments to simulation_test.go

* add comments and unit tests

* add comments to operations

* refactor based on comments

* fix formating

* improve formating for decoders.go

* remove overflow check when not needed

* resolve comments 2

* add protocol v2

* generate files 2

* reduce weight of message enable CCTX

* add solana address from rand

* make changes based on feedback

* refacctor updateCrossChainState to updateCrosschainState

* rename Maxed to MaxReached

* change test names for tests under TestCoinType_SupportsRefund

* rename functions updateState to extractState

* rebase develop

* revert format for makefile

* generate files

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
kingpinXD and coderabbitai[bot] authored Jan 10, 2025
1 parent 47c9444 commit 2edfa9c
Show file tree
Hide file tree
Showing 66 changed files with 3,841 additions and 547 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ test-sim-fullappsimulation:
$(call run-sim-test,"TestFullAppSimulation",TestFullAppSimulation,100,200,30m)

test-sim-import-export:
$(call run-sim-test,"test-import-export",TestAppImportExport,50,100,30m)
$(call run-sim-test,"test-import-export",TestAppImportExport,100,200,30m)

test-sim-after-import:
$(call run-sim-test,"test-sim-after-import",TestAppSimulationAfterImport,100,200,30m)
Expand All @@ -430,6 +430,12 @@ test-sim-after-import-long: runsim
@echo "Running application simulation-after-import. This may take several minute"
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 500 50 TestAppSimulationAfterImport

# Use to run all simulation tests quickly (for example, before a creating a PR)
test-sim-quick:
$(call run-sim-test,"test-full-app-sim",TestFullAppSimulation,10,20,30m)
$(call run-sim-test,"test-import-export",TestAppImportExport,10,20,30m)
$(call run-sim-test,"test-sim-after-import",TestAppSimulationAfterImport,10,20,30m)

.PHONY: \
test-sim-nondeterminism \
test-sim-fullappsimulation \
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [3254](https://github.com/zeta-chain/node/pull/3254) - rename v2 E2E tests as evm tests and rename old evm tests as legacy
* [3095](https://github.com/zeta-chain/node/pull/3095) - initialize simulation tests for custom zetachain modules
* [3276](https://github.com/zeta-chain/node/pull/3276) - add Solana E2E performance tests and improve Solana outbounds performance
* [3207](https://github.com/zeta-chain/node/pull/3207) - add simulation test operations for all messages in crosschain and observer module

### Refactor

Expand Down
5 changes: 5 additions & 0 deletions pkg/chains/chain_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func FilterByConsensus(cs Consensus) ChainFilter {
return func(chain Chain) bool { return chain.Consensus == cs }
}

// FilterByVM filters chains by VM type
func FilterByVM(vm Vm) ChainFilter {
return func(chain Chain) bool { return chain.Vm == vm }
}

// FilterChains applies a list of filters to a list of chains
func FilterChains(chainList []Chain, filters ...ChainFilter) []Chain {
// Apply each filter to the list of supported chains
Expand Down
33 changes: 24 additions & 9 deletions pkg/chains/chain_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,57 @@ func TestFilterChains(t *testing.T) {
expected func() []chains.Chain
}{
{
name: "Filter external chains",
name: "filter external chains",
filters: []chains.ChainFilter{chains.FilterExternalChains},
expected: func() []chains.Chain {
return chains.ExternalChainList([]chains.Chain{})
},
},
{
name: "Filter gateway observer chains",
name: "filter gateway observer chains",
filters: []chains.ChainFilter{chains.FilterByGateway(chains.CCTXGateway_observers)},
expected: func() []chains.Chain {
return chains.ChainListByGateway(chains.CCTXGateway_observers, []chains.Chain{})
},
},
{
name: "Filter consensus ethereum chains",
name: "filter consensus ethereum chains",
filters: []chains.ChainFilter{chains.FilterByConsensus(chains.Consensus_ethereum)},
expected: func() []chains.Chain {
return chains.ChainListByConsensus(chains.Consensus_ethereum, []chains.Chain{})
},
},
{
name: "Filter consensus bitcoin chains",
name: "filter consensus bitcoin chains",
filters: []chains.ChainFilter{chains.FilterByConsensus(chains.Consensus_bitcoin)},
expected: func() []chains.Chain {
return chains.ChainListByConsensus(chains.Consensus_bitcoin, []chains.Chain{})
},
},
{
name: "Filter consensus solana chains",
name: "filter consensus solana chains",
filters: []chains.ChainFilter{chains.FilterByConsensus(chains.Consensus_solana_consensus)},
expected: func() []chains.Chain {
return chains.ChainListByConsensus(chains.Consensus_solana_consensus, []chains.Chain{})
},
},
{
name: "Apply multiple filters external chains and gateway observer",
name: "filter evm chains",
filters: []chains.ChainFilter{
chains.FilterByVM(chains.Vm_evm),
},
expected: func() []chains.Chain {
var chainList []chains.Chain
for _, chain := range chains.ExternalChainList([]chains.Chain{}) {
if chain.Vm == chains.Vm_evm {
chainList = append(chainList, chain)
}
}
return chainList
},
},
{
name: "apply multiple filters external chains and gateway observer",
filters: []chains.ChainFilter{
chains.FilterExternalChains,
chains.FilterByGateway(chains.CCTXGateway_observers),
Expand All @@ -66,7 +81,7 @@ func TestFilterChains(t *testing.T) {
},
},
{
name: "Apply multiple filters external chains with gateway observer and consensus ethereum",
name: "apply multiple filters external chains with gateway observer and consensus ethereum",
filters: []chains.ChainFilter{
chains.FilterExternalChains,
chains.FilterByGateway(chains.CCTXGateway_observers),
Expand All @@ -85,7 +100,7 @@ func TestFilterChains(t *testing.T) {
},
},
{
name: "Apply multiple filters external chains with gateway observer and consensus bitcoin",
name: "apply multiple filters external chains with gateway observer and consensus bitcoin",
filters: []chains.ChainFilter{
chains.FilterExternalChains,
chains.FilterByGateway(chains.CCTXGateway_observers),
Expand Down Expand Up @@ -116,7 +131,7 @@ func TestFilterChains(t *testing.T) {
},
},
{
name: "Test multiple filters in random order",
name: "test multiple filters in random order",
filters: []chains.ChainFilter{
chains.FilterByGateway(chains.CCTXGateway_observers),
chains.FilterByConsensus(chains.Consensus_ethereum),
Expand Down
4 changes: 4 additions & 0 deletions pkg/coin/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ func GetAzetaDecFromAmountInZeta(zetaAmount string) (sdk.Dec, error) {
zetaToAzetaConvertionFactor := sdk.NewDecFromInt(sdk.NewInt(1000000000000000000))
return zetaDec.Mul(zetaToAzetaConvertionFactor), nil
}

func (c CoinType) SupportsRefund() bool {
return c == CoinType_ERC20 || c == CoinType_Gas || c == CoinType_Zeta
}
42 changes: 32 additions & 10 deletions pkg/coin/coin_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package coin
package coin_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/node/pkg/coin"
)

func Test_AzetaPerZeta(t *testing.T) {
require.Equal(t, sdk.NewDec(1e18), AzetaPerZeta())
require.Equal(t, sdk.NewDec(1e18), coin.AzetaPerZeta())
}

func Test_GetAzetaDecFromAmountInZeta(t *testing.T) {
Expand Down Expand Up @@ -57,7 +58,7 @@ func Test_GetAzetaDecFromAmountInZeta(t *testing.T) {
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
azeta, err := GetAzetaDecFromAmountInZeta(tc.zetaAmount)
azeta, err := coin.GetAzetaDecFromAmountInZeta(tc.zetaAmount)
tc.err(t, err)
if err == nil {
require.Equal(t, tc.azetaAmount, azeta)
Expand All @@ -71,31 +72,31 @@ func TestGetCoinType(t *testing.T) {
tests := []struct {
name string
coin string
want CoinType
want coin.CoinType
wantErr bool
}{
{
name: "valid coin type 0",
coin: "0",
want: CoinType(0),
want: coin.CoinType(0),
wantErr: false,
},
{
name: "valid coin type 1",
coin: "1",
want: CoinType(1),
want: coin.CoinType(1),
wantErr: false,
},
{
name: "valid coin type 2",
coin: "2",
want: CoinType(2),
want: coin.CoinType(2),
wantErr: false,
},
{
name: "valid coin type 3",
coin: "3",
want: CoinType(3),
want: coin.CoinType(3),
wantErr: false,
},
{
Expand All @@ -106,7 +107,7 @@ func TestGetCoinType(t *testing.T) {
{
name: "invalid coin type large number",
coin: "4",
want: CoinType(4),
want: coin.CoinType(4),
},
{
name: "invalid coin type non-integer",
Expand All @@ -117,7 +118,7 @@ func TestGetCoinType(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetCoinType(tt.coin)
got, err := coin.GetCoinType(tt.coin)
if tt.wantErr {
require.Error(t, err)
} else {
Expand All @@ -127,3 +128,24 @@ func TestGetCoinType(t *testing.T) {
})
}
}

func TestCoinType_SupportsRefund(t *testing.T) {
tests := []struct {
name string
c coin.CoinType
want bool
}{
{"should support refund for ERC20", coin.CoinType_ERC20, true},
{"should support refund forGas", coin.CoinType_Gas, true},
{"should support refund forZeta", coin.CoinType_Zeta, true},
{"should not support refund forCmd", coin.CoinType_Cmd, false},
{"should not support refund forUnknown", coin.CoinType(100), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.c.SupportsRefund(); got != tt.want {
t.Errorf("CoinType.SupportsRefund() = %v, want %v", got, tt.want)
}
})
}
}
25 changes: 25 additions & 0 deletions pkg/memo/memo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package memo_test

import (
"encoding/hex"
mathrand "math/rand"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/node/pkg/memo"
"github.com/zeta-chain/node/testutil/sample"
crosschaintypes "github.com/zeta-chain/node/x/crosschain/types"
)

Expand Down Expand Up @@ -306,6 +308,9 @@ func Test_Memo_DecodeFromBytes(t *testing.T) {

func Test_DecodeLegacyMemoHex(t *testing.T) {
expectedShortMsgResult, err := hex.DecodeString("1a2b3c4d5e6f708192a3b4c5d6e7f808")
r := mathrand.New(mathrand.NewSource(42))
address, data, memoHex := sample.MemoFromRand(r)

require.NoError(t, err)
tests := []struct {
name string
Expand All @@ -324,6 +329,8 @@ func Test_DecodeLegacyMemoHex(t *testing.T) {
{"empty msg", "", common.Address{}, nil, false},
{"invalid hex", "invalidHex", common.Address{}, nil, true},
{"short msg", "1a2b3c4d5e6f708192a3b4c5d6e7f808", common.Address{}, expectedShortMsgResult, false},
{"random message", sample.EthAddress().String(), common.Address{}, nil, true},
{"random message with hex encoding", memoHex, address, data, false},
}

for _, tt := range tests {
Expand All @@ -339,3 +346,21 @@ func Test_DecodeLegacyMemoHex(t *testing.T) {
})
}
}

func Test_DecodeLegacyMemoHex_Random(t *testing.T) {
r := mathrand.New(mathrand.NewSource(42))

// Generate a random memo hex
randomMemo := common.BytesToAddress([]byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78}).
Hex()
randomData := []byte(sample.StringRandom(r, 10))
randomMemoHex := hex.EncodeToString(append(common.FromHex(randomMemo), randomData...))

// Decode the random memo hex
addr, data, err := memo.DecodeLegacyMemoHex(randomMemoHex)

// Validate the results
require.NoError(t, err)
require.Equal(t, common.HexToAddress(randomMemo), addr)
require.Equal(t, randomData, data)
}
Loading

0 comments on commit 2edfa9c

Please sign in to comment.