-
Notifications
You must be signed in to change notification settings - Fork 431
Expand file tree
/
Copy pathblockSubscribe_rewards_test.go
More file actions
107 lines (92 loc) · 3.06 KB
/
blockSubscribe_rewards_test.go
File metadata and controls
107 lines (92 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package ws
import (
"strconv"
"testing"
"time"
"github.com/gagliardetto/solana-go/rpc"
"github.com/stretchr/testify/require"
)
// readSubscribeParams reads one subscribe request from the mock server and
// returns the params map of the second element (the options object), responding
// to the client so the subscribe call can return.
func readSubscribeParams(t *testing.T, m *mockWSServer, wsSubID uint64) map[string]any {
t.Helper()
select {
case msg := <-m.incoming:
var req struct {
ID uint64 `json:"id"`
Method string `json:"method"`
Params []any `json:"params"`
}
require.NoError(t, json.Unmarshal(msg, &req))
resp := `{"jsonrpc":"2.0","result":` + strconv.FormatUint(wsSubID, 10) + `,"id":` + strconv.FormatUint(req.ID, 10) + `}`
m.send(t, resp)
// params[0] is the filter ("all"), params[1] is the opts object
require.GreaterOrEqual(t, len(req.Params), 2, "expected opts object in params[1]: %s", string(msg))
opts, ok := req.Params[1].(map[string]any)
require.True(t, ok, "expected map opts in params[1], got %T", req.Params[1])
return opts
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for subscribe request")
return nil
}
}
// TestBlockSubscribeUsesShowRewardsField pins that BlockSubscribe sends the
// Solana-spec field "showRewards" (not "rewards") in its params object.
//
// See https://solana.com/docs/rpc/websocket/blocksubscribe for the spec, and
// issue #208 for the bug report.
func TestBlockSubscribeUsesShowRewardsField(t *testing.T) {
m := newMockWSServer(t)
defer m.stop()
c := connectClient(t, m)
defer c.Close()
rewards := false
opts := &BlockSubscribeOpts{
Commitment: rpc.CommitmentConfirmed,
Rewards: &rewards,
}
type subResult struct {
err error
}
ch := make(chan subResult, 1)
go func() {
_, err := c.BlockSubscribe(NewBlockSubscribeFilterAll(), opts)
ch <- subResult{err}
}()
params := readSubscribeParams(t, m, 1)
require.NoError(t, (<-ch).err)
_, hasOldKey := params["rewards"]
require.False(t, hasOldKey, "params must not include legacy 'rewards' key: %v", params)
v, ok := params["showRewards"]
require.True(t, ok, "params must include 'showRewards' key: %v", params)
require.Equal(t, false, v)
}
// TestParsedBlockSubscribeUsesShowRewardsField pins the same fix for the
// parsed-block subscription path.
func TestParsedBlockSubscribeUsesShowRewardsField(t *testing.T) {
m := newMockWSServer(t)
defer m.stop()
c := connectClient(t, m)
defer c.Close()
rewards := true
opts := &BlockSubscribeOpts{
Commitment: rpc.CommitmentConfirmed,
Rewards: &rewards,
}
type subResult struct {
err error
}
ch := make(chan subResult, 1)
go func() {
_, err := c.ParsedBlockSubscribe(NewBlockSubscribeFilterAll(), opts)
ch <- subResult{err}
}()
params := readSubscribeParams(t, m, 2)
require.NoError(t, (<-ch).err)
_, hasOldKey := params["rewards"]
require.False(t, hasOldKey, "params must not include legacy 'rewards' key: %v", params)
v, ok := params["showRewards"]
require.True(t, ok, "params must include 'showRewards' key: %v", params)
require.Equal(t, true, v)
}