From 3f8f98958819c72911d6a78660c10490c14e72c6 Mon Sep 17 00:00:00 2001 From: Dmitry S <11892559+swift1337@users.noreply.github.com> Date: Thu, 3 Oct 2024 20:32:17 +0200 Subject: [PATCH] Improve testing --- pkg/chains/chains_test.go | 5 ++ zetaclient/chains/interfaces/interfaces.go | 8 ++ .../chains/ton/liteapi/client_live_test.go | 52 ++++++++---- zetaclient/chains/ton/liteapi/client_test.go | 82 +++++++++++++++++++ 4 files changed, 129 insertions(+), 18 deletions(-) diff --git a/pkg/chains/chains_test.go b/pkg/chains/chains_test.go index 270a26b045..73c3521ffa 100644 --- a/pkg/chains/chains_test.go +++ b/pkg/chains/chains_test.go @@ -135,6 +135,11 @@ func TestChainListByNetwork(t *testing.T) { chains.Network_solana, []chains.Chain{chains.SolanaMainnet, chains.SolanaDevnet, chains.SolanaLocalnet}, }, + { + "TON", + chains.Network_ton, + []chains.Chain{chains.TONMainnet, chains.TONTestnet, chains.TONLocalnet}, + }, } for _, lt := range listTests { diff --git a/zetaclient/chains/interfaces/interfaces.go b/zetaclient/chains/interfaces/interfaces.go index 0e1b938c64..c89a77e4b8 100644 --- a/zetaclient/chains/interfaces/interfaces.go +++ b/zetaclient/chains/interfaces/interfaces.go @@ -39,12 +39,20 @@ const ( // ChainObserver is the interface for chain observer type ChainObserver interface { + // Start starts the observer Start(ctx context.Context) + + // Stop stops the observer Stop() + // ChainParams returns observer chain params (might be out of date with zetacore) ChainParams() observertypes.ChainParams + + // SetChainParams sets observer chain params SetChainParams(observertypes.ChainParams) + // VoteOutboundIfConfirmed checks outbound status and returns (continueKeySign, error) + // todo we should make this simpler. VoteOutboundIfConfirmed(ctx context.Context, cctx *crosschaintypes.CrossChainTx) (bool, error) } diff --git a/zetaclient/chains/ton/liteapi/client_live_test.go b/zetaclient/chains/ton/liteapi/client_live_test.go index 40c37dce71..acf211736b 100644 --- a/zetaclient/chains/ton/liteapi/client_live_test.go +++ b/zetaclient/chains/ton/liteapi/client_live_test.go @@ -28,32 +28,48 @@ func TestClient(t *testing.T) { ) t.Run("GetFirstTransaction", func(t *testing.T) { - // ARRANGE - // Given sample account id (a dev wallet) - // https://tonviewer.com/UQCVlMcZ7EyV9maDsvscoLCd5KQfb7CHukyNJluWpMzlD0vr?section=transactions - accountID, err := ton.ParseAccountID("UQCVlMcZ7EyV9maDsvscoLCd5KQfb7CHukyNJluWpMzlD0vr") - require.NoError(t, err) + t.Run("Account doesn't exist", func(t *testing.T) { + // ARRANGE + accountID, err := ton.ParseAccountID("0:55798cb7b87168251a7c39f6806b8c202f6caa0f617a76f4070b3fdacfd056a2") + require.NoError(t, err) - // Given expected hash for the first tx - const expect = "b73df4853ca02a040df46f56635d6b8f49b554d5f556881ab389111bbfce4498" + // ACT + tx, scrolled, err := client.GetFirstTransaction(ctx, accountID) - // as of 2024-09-18 - const expectedTransactions = 23 + // ASSERT + require.ErrorContains(t, err, "account is not active") + require.Zero(t, scrolled) + require.Nil(t, tx) + }) - start := time.Now() + t.Run("All good", func(t *testing.T) { + // ARRANGE + // Given sample account id (a dev wallet) + // https://tonviewer.com/UQCVlMcZ7EyV9maDsvscoLCd5KQfb7CHukyNJluWpMzlD0vr?section=transactions + accountID, err := ton.ParseAccountID("UQCVlMcZ7EyV9maDsvscoLCd5KQfb7CHukyNJluWpMzlD0vr") + require.NoError(t, err) - // ACT - tx, scrolled, err := client.GetFirstTransaction(ctx, accountID) + // Given expected hash for the first tx + const expect = "b73df4853ca02a040df46f56635d6b8f49b554d5f556881ab389111bbfce4498" - finish := time.Since(start) + // as of 2024-09-18 + const expectedTransactions = 23 - // ASSERT - require.NoError(t, err) + start := time.Now() + + // ACT + tx, scrolled, err := client.GetFirstTransaction(ctx, accountID) + + finish := time.Since(start) + + // ASSERT + require.NoError(t, err) - assert.GreaterOrEqual(t, scrolled, expectedTransactions) - assert.Equal(t, expect, tx.Hash().Hex()) + assert.GreaterOrEqual(t, scrolled, expectedTransactions) + assert.Equal(t, expect, tx.Hash().Hex()) - t.Logf("Time taken %s; transactions scanned: %d", finish.String(), scrolled) + t.Logf("Time taken %s; transactions scanned: %d", finish.String(), scrolled) + }) }) t.Run("GetTransactionsUntil", func(t *testing.T) { diff --git a/zetaclient/chains/ton/liteapi/client_test.go b/zetaclient/chains/ton/liteapi/client_test.go index 3ab56422a5..a1148540be 100644 --- a/zetaclient/chains/ton/liteapi/client_test.go +++ b/zetaclient/chains/ton/liteapi/client_test.go @@ -16,3 +16,85 @@ func TestHashes(t *testing.T) { require.Equal(t, "e02b8c7cec103e08175ade8106619a8908707623c31451df2a68497c7d23d15a", hash.Hex()) require.Equal(t, sample, TransactionHashToString(lt, hash)) } + +func TestTransactionHashFromString(t *testing.T) { + for _, tt := range []struct { + name string + raw string + error bool + lt uint64 + hash string + }{ + { + name: "real example", + raw: "163000003:d0415f655644db6ee1260b1fa48e9f478e938823e8b293054fbae1f3511b77c5", + lt: 163000003, + hash: "d0415f655644db6ee1260b1fa48e9f478e938823e8b293054fbae1f3511b77c5", + }, + { + name: "zero lt", + raw: "0:0000000000000000000000000000000000000000000000000000000000000000", + lt: 0, + hash: "0000000000000000000000000000000000000000000000000000000000000000", + }, + { + name: "big lt", + raw: "999999999999:fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0", + lt: 999_999_999_999, + hash: "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0", + }, + { + name: "missing colon", + raw: "123456abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef", + error: true, + }, + { + name: "missing logical time", + raw: ":abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef", + error: true, + }, + { + name: "hash length", + raw: "123456:abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcde", + error: true, + }, + { + name: "non-numeric logical time", + raw: "notanumber:abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef", + error: true, + }, + { + name: "non-hex hash", + raw: "123456:xyz123xyz123xyz123xyz123xyz123xyz123xyz123xyz123xyz123xyz123xyz123", + error: true, + }, + { + name: "empty string", + raw: "", + error: true, + }, + { + name: "Invalid - only logical time, no hash", + raw: "123456:", + error: true, + }, + { + name: "Invalid - too many parts (extra colon)", + raw: "123456:abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef:extra", + error: true, + }, + } { + t.Run(tt.name, func(t *testing.T) { + lt, hash, err := TransactionHashFromString(tt.raw) + + if tt.error { + require.Error(t, err) + return + } + + require.NoError(t, err) + require.Equal(t, tt.lt, lt) + require.Equal(t, hash.Hex(), tt.hash) + }) + } +}