Skip to content

Commit

Permalink
Add client test
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanTinianov committed Sep 24, 2024
1 parent 8080fed commit 05524a1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 46 deletions.
52 changes: 32 additions & 20 deletions pkg/solana/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ func TestSolanaChain_GetClient(t *testing.T) {

ch := solcfg.Chain{}
ch.SetDefaults()
mn := solcfg.MultiNode{}
mn.SetDefaults(false)
cfg := &solcfg.TOMLConfig{
ChainID: ptr("devnet"),
Chain: ch,
ChainID: ptr("devnet"),
Chain: ch,
MultiNode: mn,
}
testChain := chain{
id: "devnet",
Expand Down Expand Up @@ -117,24 +120,28 @@ func TestSolanaChain_GetClient(t *testing.T) {
assert.NoError(t, err)
}

func TestSolanaChain_MultiNode(t *testing.T) {
// TODO: Set up chain with MultiNode enabled
func TestSolanaChain_MultiNode_GetClient(t *testing.T) {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
out := fmt.Sprintf(TestSolanaGenesisHashTemplate, client.MainnetGenesisHash) // mainnet genesis hash
if !strings.Contains(r.URL.Path, "/mismatch") {
// devnet gensis hash
out = fmt.Sprintf(TestSolanaGenesisHashTemplate, client.DevnetGenesisHash)
}
_, err := w.Write([]byte(out))
require.NoError(t, err)
}))
defer mockServer.Close()

ch := solcfg.Chain{}
ch.SetDefaults()
mn := solcfg.MultiNode{}
mn.SetDefaults(true)

cfg := &solcfg.TOMLConfig{
ChainID: ptr("devnet"),
Chain: ch,
ChainID: ptr("devnet"),
Chain: ch,
MultiNode: mn,
}
testChain := chain{
id: "devnet",
cfg: cfg,
lggr: logger.Test(t),
clientCache: map[string]*verifiedCachedClient{},
}



cfg.Nodes = []*solcfg.Node{
{
Name: ptr("devnet"),
Expand All @@ -145,14 +152,19 @@ func TestSolanaChain_MultiNode(t *testing.T) {
URL: config.MustParseURL(mockServer.URL + "/2"),
},
}
_, err := testChain.getClient()
assert.NoError(t, err)

// TODO: Start MultiNode and ensure we can call getClient()
testChain, err := newChain("devnet", cfg, nil, logger.Test(t))
require.NoError(t, err)

mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = testChain.multiNode.Start(tests.Context(t))
assert.NoError(t, err)

}
selectedClient, err := testChain.getClient()
assert.NoError(t, err)

id, err := selectedClient.ChainID(tests.Context(t))
assert.NoError(t, err)
assert.Equal(t, "devnet", id.String())
}

func TestSolanaChain_VerifiedClient(t *testing.T) {
Expand Down
31 changes: 8 additions & 23 deletions pkg/solana/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func NewClient(endpoint string, cfg *config.TOMLConfig, requestTimeout time.Dura
contextDuration: requestTimeout,
log: log,
requestGroup: &singleflight.Group{},
pollInterval: cfg.PollInterval(),
finalizedBlockPollInterval: cfg.FinalizedBlockPollInterval(),
pollInterval: cfg.MultiNode.PollInterval(),
finalizedBlockPollInterval: cfg.MultiNode.FinalizedBlockPollInterval(),
}, nil
}

Expand All @@ -117,8 +117,7 @@ func (h *Head) BlockNumber() int64 {
}

func (h *Head) BlockDifficulty() *big.Int {
// TODO: Is difficulty relevant for Solana?
// TODO: If not, then remove changes to it in latestBlockInfo
// TODO: Not relevant for Solana?
return nil
}

Expand All @@ -130,16 +129,7 @@ var _ mn.RPCClient[mn.StringID, *Head] = (*Client)(nil)
var _ mn.SendTxRPCClient[*solana.Transaction] = (*Client)(nil)

func (c *Client) Dial(ctx context.Context) error {
// TODO: is there any work to do here? Doesn't seem like we have to dial anything
// TODO: Could maybe do a version check here?
/* TODO: Should we use this health check?
health, err := c.rpc.GetHealth(ctx)
if err != nil {
return false, err
}
return health == rpc.HealthOk, nil
*/
panic("implement me")
return nil
}

func (c *Client) SubscribeToHeads(ctx context.Context) (<-chan *Head, mn.Subscription, error) {
Expand Down Expand Up @@ -187,22 +177,17 @@ func (c *Client) LatestFinalizedBlock(ctx context.Context) (*Head, error) {
// capture chStopInFlight to ensure we are not updating chainInfo with observations related to previous life cycle
//ctx, cancel, chStopInFlight, _, _ := c.acquireQueryCtx(ctx, c.rpcTimeout)

// TODO: Is this really the way to implement this?
latestBH, err := c.rpc.GetLatestBlockhash(ctx, c.commitment)
finalizedBlockHeight, err := c.rpc.GetBlockHeight(ctx, rpc.CommitmentFinalized)
if err != nil {
return nil, err
}

var finalityDepth uint64 = 1 // TODO: Get value from config

latestFinalizedBH := latestBH.Value.LastValidBlockHeight - finalityDepth // TODO: subtract finality depth?

resp, err := c.rpc.GetBlock(ctx, latestFinalizedBH)
block, err := c.rpc.GetBlock(ctx, finalizedBlockHeight)
if err != nil {
return nil, err
}

head := &Head{GetBlockResult: *resp}
head := &Head{GetBlockResult: *block}
c.onNewFinalizedHead(ctx, c.chStopInFlight, head)
return head, nil
}
Expand Down Expand Up @@ -254,7 +239,7 @@ func (c *Client) Ping(ctx context.Context) error {
}

func (c *Client) IsSyncing(ctx context.Context) (bool, error) {
// TODO: is this relevant?
// Not relevant for Solana
return false, nil
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/solana/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,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
Expand Down Expand Up @@ -340,6 +341,7 @@ func TestClient_Subscriptions_Integration(t *testing.T) {
wg.Wait()
}
*/

func TestClientLatency(t *testing.T) {
c := Client{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/solana/config/multinode.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func (c *MultiNode) FinalityTagEnabled() bool { return *c.finalityTagEnabled }

func (c *MultiNode) FinalizedBlockOffset() uint32 { return *c.finalizedBlockOffset }

func (c *MultiNode) SetDefaults() {
c.multiNodeEnabled = ptr(false)
func (c *MultiNode) SetDefaults(enabled bool) {
c.multiNodeEnabled = ptr(enabled)

// Node Configs
c.pollFailureThreshold = ptr(uint32(5))
Expand Down
2 changes: 1 addition & 1 deletion pkg/solana/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,6 @@ func (c *TOMLConfig) MultiNodeConfig() *MultiNode {
func NewDefault() *TOMLConfig {
cfg := &TOMLConfig{}
cfg.Chain.SetDefaults()
cfg.MultiNode.SetDefaults()
cfg.MultiNode.SetDefaults(false)
return cfg
}

0 comments on commit 05524a1

Please sign in to comment.