Skip to content

Commit

Permalink
Improve testing
Browse files Browse the repository at this point in the history
  • Loading branch information
swift1337 committed Oct 3, 2024
1 parent 142778b commit 3f8f989
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 18 deletions.
5 changes: 5 additions & 0 deletions pkg/chains/chains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions zetaclient/chains/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
52 changes: 34 additions & 18 deletions zetaclient/chains/ton/liteapi/client_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
82 changes: 82 additions & 0 deletions zetaclient/chains/ton/liteapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit 3f8f989

Please sign in to comment.