Skip to content

Commit

Permalink
Add subscription test
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanTinianov committed Sep 24, 2024
1 parent 05524a1 commit 6b92e70
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
8 changes: 4 additions & 4 deletions pkg/solana/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (h *Head) BlockNumber() int64 {
}

func (h *Head) BlockDifficulty() *big.Int {
// TODO: Not relevant for Solana?
// Not relevant for Solana
return nil
}

Expand Down Expand Up @@ -157,17 +157,17 @@ func (c *Client) SubscribeToFinalizedHeads(ctx context.Context) (<-chan *Head, m
}

func (c *Client) LatestBlock(ctx context.Context) (*Head, error) {
latestBlockHash, err := c.rpc.GetLatestBlockhash(ctx, c.commitment)
latestBlockHeight, err := c.rpc.GetBlockHeight(ctx, rpc.CommitmentConfirmed)
if err != nil {
return nil, err
}

latestBlock, err := c.rpc.GetBlock(ctx, latestBlockHash.Value.LastValidBlockHeight)
block, err := c.rpc.GetBlock(ctx, latestBlockHeight)
if err != nil {
return nil, err
}

head := &Head{GetBlockResult: *latestBlock}
head := &Head{GetBlockResult: *block}
c.onNewHead(ctx, c.chStopInFlight, head)
return head, nil
}
Expand Down
57 changes: 31 additions & 26 deletions pkg/solana/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"context"
"fmt"
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"
"math/rand"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -297,13 +298,7 @@ func TestClient_SendTxDuplicates_Integration(t *testing.T) {
assert.Equal(t, uint64(5_000), initBal-endBal)
}

/*
func TestClient_Subscriptions_Integration(t *testing.T) {
// TODO: Test subscribing to heads and finalized heads
// TODO: Ensure chain info is updated on new heads
// TODO: Test Dial, Close, IsSyncing, GetInterceptedChainInfo
// TODO: Create server for testing??
url := SetupLocalSolNode(t)
privKey, err := solana.NewRandomPrivateKey()
require.NoError(t, err)
Expand All @@ -313,35 +308,45 @@ func TestClient_Subscriptions_Integration(t *testing.T) {
requestTimeout := 5 * time.Second
lggr := logger.Test(t)
cfg := config.NewDefault()
// Enable MultiNode
cfg.MultiNode.SetDefaults(true)

ctx := context.Background()
c, err := NewClient(url, cfg, requestTimeout, lggr)
require.NoError(t, err)

ch, sub, err := c.SubscribeToHeads(ctx)
defer sub.Unsubscribe()
err = c.Ping(tests.Context(t))
require.NoError(t, err)

// TODO: How do we test this?
// check for new heads
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
for {
select {
case head := <-ch:
t.Logf("New head: %v", head)
wg.Done()
case <-ctx.Done():
return
}
}
}()
ch, sub, err := c.SubscribeToHeads(tests.Context(t))
require.NoError(t, err)
defer sub.Unsubscribe()

wg.Wait()
finalizedCh, finalizedSub, err := c.SubscribeToFinalizedHeads(tests.Context(t))
require.NoError(t, err)
defer finalizedSub.Unsubscribe()

require.NoError(t, err)
ctx, cancel := context.WithTimeout(tests.Context(t), time.Minute)
defer cancel()

select {
case head := <-ch:
require.NotEqual(t, solana.Hash{}, head.Blockhash)
latest, _ := c.GetInterceptedChainInfo()
require.Equal(t, head.BlockNumber(), latest.BlockNumber)
case <-ctx.Done():
t.Fatal("failed to receive head: ", ctx.Err())
}

select {
case finalizedHead := <-finalizedCh:
require.NotEqual(t, solana.Hash{}, finalizedHead.Blockhash)
latest, _ := c.GetInterceptedChainInfo()
require.Equal(t, finalizedHead.BlockNumber(), latest.FinalizedBlockNumber)
case <-ctx.Done():
t.Fatal("failed to receive finalized head: ", ctx.Err())
}
}
*/

func TestClientLatency(t *testing.T) {
c := Client{}
Expand Down

0 comments on commit 6b92e70

Please sign in to comment.