2022-07-21 19:39:53 +00:00
|
|
|
package rpcclient
|
2020-04-29 19:51:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-05 14:56:17 +00:00
|
|
|
"encoding/json"
|
2021-03-17 13:40:24 +00:00
|
|
|
"fmt"
|
2020-05-12 08:18:44 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2022-02-22 13:18:52 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
2020-05-18 13:23:51 +00:00
|
|
|
"strings"
|
2022-02-22 13:18:52 +00:00
|
|
|
"sync"
|
2023-10-11 10:24:16 +00:00
|
|
|
"sync/atomic"
|
2020-04-29 19:51:43 +00:00
|
|
|
"testing"
|
2020-05-12 08:18:44 +00:00
|
|
|
"time"
|
2020-04-29 19:51:43 +00:00
|
|
|
|
2020-05-12 08:18:44 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2020-06-18 06:43:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2022-10-25 12:11:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2022-05-23 10:47:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2022-07-22 16:09:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc"
|
2022-10-25 12:11:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2022-05-23 10:47:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
2022-07-21 13:21:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/services/rpcsrv/params"
|
2020-05-13 10:16:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2022-10-25 12:11:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-04-29 19:51:43 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWSClientClose(t *testing.T) {
|
2023-04-18 16:36:57 +00:00
|
|
|
srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "result": "55aaff00"}`)
|
2023-04-25 08:26:36 +00:00
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{})
|
2020-04-29 19:51:43 +00:00
|
|
|
require.NoError(t, err)
|
2023-04-18 16:36:57 +00:00
|
|
|
wsc.getNextRequestID = getTestRequestID
|
|
|
|
bCh := make(chan *block.Block)
|
|
|
|
_, err = wsc.ReceiveBlocks(nil, bCh)
|
2020-04-29 19:51:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
wsc.Close()
|
2023-04-18 16:36:57 +00:00
|
|
|
// Subscriber channel must be closed by server.
|
|
|
|
_, ok := <-bCh
|
|
|
|
require.False(t, ok)
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
|
|
|
|
func TestWSClientSubscription(t *testing.T) {
|
2022-10-25 12:11:24 +00:00
|
|
|
bCh := make(chan *block.Block)
|
|
|
|
txCh := make(chan *transaction.Transaction)
|
|
|
|
aerCh := make(chan *state.AppExecResult)
|
|
|
|
ntfCh := make(chan *state.ContainedNotificationEvent)
|
|
|
|
ntrCh := make(chan *result.NotaryRequestEvent)
|
2020-05-12 08:18:44 +00:00
|
|
|
var cases = map[string]func(*WSClient) (string, error){
|
2020-05-13 10:16:42 +00:00
|
|
|
"blocks": func(wsc *WSClient) (string, error) {
|
2022-10-25 12:11:24 +00:00
|
|
|
return wsc.ReceiveBlocks(nil, bCh)
|
2022-10-17 10:31:24 +00:00
|
|
|
},
|
2020-05-13 10:16:42 +00:00
|
|
|
"transactions": func(wsc *WSClient) (string, error) {
|
2022-10-25 12:11:24 +00:00
|
|
|
return wsc.ReceiveTransactions(nil, txCh)
|
2022-10-17 10:31:24 +00:00
|
|
|
},
|
2020-05-13 10:16:42 +00:00
|
|
|
"notifications": func(wsc *WSClient) (string, error) {
|
2022-10-25 12:11:24 +00:00
|
|
|
return wsc.ReceiveExecutionNotifications(nil, ntfCh)
|
2022-10-17 10:31:24 +00:00
|
|
|
},
|
2020-05-13 10:16:42 +00:00
|
|
|
"executions": func(wsc *WSClient) (string, error) {
|
2022-10-25 12:11:24 +00:00
|
|
|
return wsc.ReceiveExecutions(nil, aerCh)
|
2020-05-13 10:16:42 +00:00
|
|
|
},
|
2022-10-25 12:11:24 +00:00
|
|
|
"notary requests": func(wsc *WSClient) (string, error) {
|
|
|
|
return wsc.ReceiveNotaryRequests(nil, ntrCh)
|
2022-10-17 10:31:24 +00:00
|
|
|
},
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
t.Run("good", func(t *testing.T) {
|
|
|
|
for name, f := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "result": "55aaff00"}`)
|
2023-04-25 08:26:36 +00:00
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{})
|
2020-05-12 08:18:44 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-18 17:28:13 +00:00
|
|
|
wsc.getNextRequestID = getTestRequestID
|
2020-10-14 15:13:20 +00:00
|
|
|
require.NoError(t, wsc.Init())
|
2020-05-12 08:18:44 +00:00
|
|
|
id, err := f(wsc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "55aaff00", id)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("bad", func(t *testing.T) {
|
|
|
|
for name, f := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "error":{"code":-32602,"message":"Invalid Params"}}`)
|
2023-04-25 08:26:36 +00:00
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{})
|
2020-05-12 08:18:44 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-18 17:28:13 +00:00
|
|
|
wsc.getNextRequestID = getTestRequestID
|
2020-10-14 15:13:20 +00:00
|
|
|
require.NoError(t, wsc.Init())
|
2020-05-12 08:18:44 +00:00
|
|
|
_, err = f(wsc)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWSClientUnsubscription(t *testing.T) {
|
|
|
|
type responseCheck struct {
|
|
|
|
response string
|
|
|
|
code func(*testing.T, *WSClient)
|
|
|
|
}
|
|
|
|
var cases = map[string]responseCheck{
|
|
|
|
"good": {`{"jsonrpc": "2.0", "id": 1, "result": true}`, func(t *testing.T, wsc *WSClient) {
|
|
|
|
// We can't really subscribe using this stub server, so set up wsc internals.
|
2022-10-25 12:11:24 +00:00
|
|
|
wsc.subscriptions["0"] = &blockReceiver{}
|
2020-05-12 08:18:44 +00:00
|
|
|
err := wsc.Unsubscribe("0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}},
|
|
|
|
"all": {`{"jsonrpc": "2.0", "id": 1, "result": true}`, func(t *testing.T, wsc *WSClient) {
|
|
|
|
// We can't really subscribe using this stub server, so set up wsc internals.
|
2022-10-25 12:11:24 +00:00
|
|
|
wsc.subscriptions["0"] = &blockReceiver{}
|
2020-05-12 08:18:44 +00:00
|
|
|
err := wsc.UnsubscribeAll()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(wsc.subscriptions))
|
|
|
|
}},
|
|
|
|
"not subscribed": {`{"jsonrpc": "2.0", "id": 1, "result": true}`, func(t *testing.T, wsc *WSClient) {
|
|
|
|
err := wsc.Unsubscribe("0")
|
|
|
|
require.Error(t, err)
|
|
|
|
}},
|
|
|
|
"error returned": {`{"jsonrpc": "2.0", "id": 1, "error":{"code":-32602,"message":"Invalid Params"}}`, func(t *testing.T, wsc *WSClient) {
|
|
|
|
// We can't really subscribe using this stub server, so set up wsc internals.
|
2022-10-25 12:11:24 +00:00
|
|
|
wsc.subscriptions["0"] = &blockReceiver{}
|
2020-05-12 08:18:44 +00:00
|
|
|
err := wsc.Unsubscribe("0")
|
|
|
|
require.Error(t, err)
|
|
|
|
}},
|
|
|
|
"false returned": {`{"jsonrpc": "2.0", "id": 1, "result": false}`, func(t *testing.T, wsc *WSClient) {
|
|
|
|
// We can't really subscribe using this stub server, so set up wsc internals.
|
2022-10-25 12:11:24 +00:00
|
|
|
wsc.subscriptions["0"] = &blockReceiver{}
|
2020-05-12 08:18:44 +00:00
|
|
|
err := wsc.Unsubscribe("0")
|
|
|
|
require.Error(t, err)
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
for name, rc := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
srv := initTestServer(t, rc.response)
|
2023-04-25 08:26:36 +00:00
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{})
|
2020-05-12 08:18:44 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-18 17:28:13 +00:00
|
|
|
wsc.getNextRequestID = getTestRequestID
|
2020-10-14 15:13:20 +00:00
|
|
|
require.NoError(t, wsc.Init())
|
2020-05-12 08:18:44 +00:00
|
|
|
rc.code(t, wsc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWSClientEvents(t *testing.T) {
|
|
|
|
var ok bool
|
2022-04-20 18:30:09 +00:00
|
|
|
// Events from RPC server testchain.
|
2020-05-12 08:18:44 +00:00
|
|
|
var events = []string{
|
2020-11-12 10:06:11 +00:00
|
|
|
`{"jsonrpc":"2.0","method":"transaction_executed","params":[{"container":"0xe1cd5e57e721d2a2e05fb1f08721b12057b25ab1dd7fd0f33ee1639932fdfad7","trigger":"Application","vmstate":"HALT","gasconsumed":"22910000","stack":[],"notifications":[{"contract":"0x1b4357bff5a01bdf2a6581247cf9ed1e24629176","eventname":"contract call","state":{"type":"Array","value":[{"type":"ByteString","value":"dHJhbnNmZXI="},{"type":"Array","value":[{"type":"ByteString","value":"dpFiJB7t+XwkgWUq3xug9b9XQxs="},{"type":"ByteString","value":"MW6FEDkBnTnfwsN9bD/uGf1YCYc="},{"type":"Integer","value":"1000"}]}]}},{"contract":"0x1b4357bff5a01bdf2a6581247cf9ed1e24629176","eventname":"transfer","state":{"type":"Array","value":[{"type":"ByteString","value":"dpFiJB7t+XwkgWUq3xug9b9XQxs="},{"type":"ByteString","value":"MW6FEDkBnTnfwsN9bD/uGf1YCYc="},{"type":"Integer","value":"1000"}]}}]}]}`,
|
2021-09-24 09:15:25 +00:00
|
|
|
`{"jsonrpc":"2.0","method":"notification_from_execution","params":[{"container":"0xe1cd5e57e721d2a2e05fb1f08721b12057b25ab1dd7fd0f33ee1639932fdfad7","contract":"0x1b4357bff5a01bdf2a6581247cf9ed1e24629176","eventname":"contract call","state":{"type":"Array","value":[{"type":"ByteString","value":"dHJhbnNmZXI="},{"type":"Array","value":[{"type":"ByteString","value":"dpFiJB7t+XwkgWUq3xug9b9XQxs="},{"type":"ByteString","value":"MW6FEDkBnTnfwsN9bD/uGf1YCYc="},{"type":"Integer","value":"1000"}]}]}}]}`,
|
2020-11-12 10:06:11 +00:00
|
|
|
`{"jsonrpc":"2.0","method":"transaction_executed","params":[{"container":"0xf97a72b7722c109f909a8bc16c22368c5023d85828b09b127b237aace33cf099","trigger":"Application","vmstate":"HALT","gasconsumed":"6042610","stack":[],"notifications":[{"contract":"0xe65ff7b3a02d207b584a5c27057d4e9862ef01da","eventname":"contract call","state":{"type":"Array","value":[{"type":"ByteString","value":"dHJhbnNmZXI="},{"type":"Array","value":[{"type":"ByteString","value":"MW6FEDkBnTnfwsN9bD/uGf1YCYc="},{"type":"ByteString","value":"IHKCdK+vw29DoHHTKM+j5inZy7A="},{"type":"Integer","value":"123"}]}]}},{"contract":"0xe65ff7b3a02d207b584a5c27057d4e9862ef01da","eventname":"transfer","state":{"type":"Array","value":[{"type":"ByteString","value":"MW6FEDkBnTnfwsN9bD/uGf1YCYc="},{"type":"ByteString","value":"IHKCdK+vw29DoHHTKM+j5inZy7A="},{"type":"Integer","value":"123"}]}}]}]}`,
|
2021-03-17 13:40:24 +00:00
|
|
|
fmt.Sprintf(`{"jsonrpc":"2.0","method":"block_added","params":[%s]}`, b1Verbose),
|
2022-10-25 12:11:24 +00:00
|
|
|
`{"jsonrpc":"2.0","method":"event_missed","params":[]}`, // the last one, will trigger receiver channels closing.
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.URL.Path == "/ws" && req.Method == "GET" {
|
|
|
|
var upgrader = websocket.Upgrader{}
|
|
|
|
ws, err := upgrader.Upgrade(w, req, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
for _, event := range events {
|
2021-05-12 18:34:07 +00:00
|
|
|
err = ws.SetWriteDeadline(time.Now().Add(2 * time.Second))
|
|
|
|
require.NoError(t, err)
|
2020-05-12 08:18:44 +00:00
|
|
|
err = ws.WriteMessage(1, []byte(event))
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}))
|
2023-04-25 08:26:36 +00:00
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{})
|
2022-10-25 12:11:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
wsc.getNextRequestID = getTestRequestID
|
|
|
|
wsc.cacheLock.Lock()
|
|
|
|
wsc.cache.initDone = true // Our server mock is restricted, so perform initialisation manually.
|
|
|
|
wsc.cache.network = netmode.UnitTestNet
|
|
|
|
wsc.cacheLock.Unlock()
|
2020-05-12 08:18:44 +00:00
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// Our server mock is restricted, so perform subscriptions manually with default notifications channel.
|
|
|
|
bCh1 := make(chan *block.Block)
|
|
|
|
bCh2 := make(chan *block.Block)
|
|
|
|
aerCh1 := make(chan *state.AppExecResult)
|
|
|
|
aerCh2 := make(chan *state.AppExecResult)
|
|
|
|
aerCh3 := make(chan *state.AppExecResult)
|
|
|
|
ntfCh := make(chan *state.ContainedNotificationEvent)
|
|
|
|
halt := "HALT"
|
|
|
|
fault := "FAULT"
|
|
|
|
wsc.subscriptionsLock.Lock()
|
|
|
|
wsc.subscriptions["0"] = &blockReceiver{ch: bCh1}
|
|
|
|
wsc.receivers[chan<- *block.Block(bCh1)] = []string{"0"}
|
|
|
|
wsc.subscriptions["1"] = &blockReceiver{ch: bCh2} // two different channels subscribed for same notifications
|
|
|
|
wsc.receivers[chan<- *block.Block(bCh2)] = []string{"1"}
|
2022-10-17 10:31:24 +00:00
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
wsc.subscriptions["2"] = &executionNotificationReceiver{ch: ntfCh}
|
|
|
|
wsc.subscriptions["3"] = &executionNotificationReceiver{ch: ntfCh} // check duplicating subscriptions
|
|
|
|
wsc.receivers[chan<- *state.ContainedNotificationEvent(ntfCh)] = []string{"2", "3"}
|
2022-10-17 10:31:24 +00:00
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
wsc.subscriptions["4"] = &executionReceiver{ch: aerCh1}
|
|
|
|
wsc.receivers[chan<- *state.AppExecResult(aerCh1)] = []string{"4"}
|
|
|
|
wsc.subscriptions["5"] = &executionReceiver{filter: &neorpc.ExecutionFilter{State: &halt}, ch: aerCh2}
|
|
|
|
wsc.receivers[chan<- *state.AppExecResult(aerCh2)] = []string{"5"}
|
|
|
|
wsc.subscriptions["6"] = &executionReceiver{filter: &neorpc.ExecutionFilter{State: &fault}, ch: aerCh3}
|
|
|
|
wsc.receivers[chan<- *state.AppExecResult(aerCh3)] = []string{"6"}
|
|
|
|
// MissedEvent must close the channels above.
|
|
|
|
|
|
|
|
wsc.subscriptionsLock.Unlock()
|
|
|
|
|
|
|
|
var (
|
|
|
|
b1Cnt, b2Cnt int
|
|
|
|
aer1Cnt, aer2Cnt, aer3Cnt int
|
|
|
|
ntfCnt int
|
2023-10-09 19:43:49 +00:00
|
|
|
expectedb1Cnt, expectedb2Cnt = 1, 1 // single Block event
|
|
|
|
expectedaer1Cnt, expectedaer2Cnt, expectedaer3Cnt = 2, 2, 0 // two HALTED AERs
|
|
|
|
expectedntfCnt = 1 // single notification event
|
2022-10-25 12:11:24 +00:00
|
|
|
aer *state.AppExecResult
|
|
|
|
)
|
|
|
|
for b1Cnt+b2Cnt+
|
|
|
|
aer1Cnt+aer2Cnt+aer3Cnt+
|
2023-10-09 19:43:49 +00:00
|
|
|
ntfCnt !=
|
2022-10-25 12:11:24 +00:00
|
|
|
expectedb1Cnt+expectedb2Cnt+
|
|
|
|
expectedaer1Cnt+expectedaer2Cnt+expectedaer3Cnt+
|
2023-10-09 19:43:49 +00:00
|
|
|
expectedntfCnt {
|
2022-10-17 10:31:24 +00:00
|
|
|
select {
|
2022-10-25 12:11:24 +00:00
|
|
|
case _, ok = <-bCh1:
|
|
|
|
if ok {
|
|
|
|
b1Cnt++
|
|
|
|
}
|
|
|
|
case _, ok = <-bCh2:
|
|
|
|
if ok {
|
|
|
|
b2Cnt++
|
|
|
|
}
|
|
|
|
case _, ok = <-aerCh1:
|
|
|
|
if ok {
|
|
|
|
aer1Cnt++
|
|
|
|
}
|
|
|
|
case aer, ok = <-aerCh2:
|
|
|
|
if ok {
|
|
|
|
require.Equal(t, vmstate.Halt, aer.VMState)
|
|
|
|
aer2Cnt++
|
|
|
|
}
|
|
|
|
case _, ok = <-aerCh3:
|
|
|
|
if ok {
|
|
|
|
aer3Cnt++
|
|
|
|
}
|
|
|
|
case _, ok = <-ntfCh:
|
|
|
|
if ok {
|
|
|
|
ntfCnt++
|
|
|
|
}
|
2022-10-17 10:31:24 +00:00
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("timeout waiting for event")
|
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
assert.Equal(t, expectedb1Cnt, b1Cnt)
|
|
|
|
assert.Equal(t, expectedb2Cnt, b2Cnt)
|
|
|
|
assert.Equal(t, expectedaer1Cnt, aer1Cnt)
|
|
|
|
assert.Equal(t, expectedaer2Cnt, aer2Cnt)
|
|
|
|
assert.Equal(t, expectedaer3Cnt, aer3Cnt)
|
|
|
|
assert.Equal(t, expectedntfCnt, ntfCnt)
|
|
|
|
|
|
|
|
// Channels must be closed by server
|
|
|
|
_, ok = <-bCh1
|
|
|
|
require.False(t, ok)
|
|
|
|
_, ok = <-bCh2
|
|
|
|
require.False(t, ok)
|
|
|
|
_, ok = <-aerCh1
|
|
|
|
require.False(t, ok)
|
|
|
|
_, ok = <-aerCh2
|
|
|
|
require.False(t, ok)
|
|
|
|
_, ok = <-aerCh3
|
|
|
|
require.False(t, ok)
|
|
|
|
_, ok = <-ntfCh
|
|
|
|
require.False(t, ok)
|
|
|
|
_, ok = <-ntfCh
|
|
|
|
require.False(t, ok)
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
2020-05-13 10:16:42 +00:00
|
|
|
|
2023-04-21 14:14:47 +00:00
|
|
|
func TestWSClientNonBlockingEvents(t *testing.T) {
|
|
|
|
// Use buffered channel as a receiver to check it will be closed by WSClient
|
|
|
|
// after overflow if CloseNotificationChannelIfFull option is enabled.
|
|
|
|
const chCap = 3
|
|
|
|
bCh := make(chan *block.Block, chCap)
|
|
|
|
|
|
|
|
// Events from RPC server testchain. Require events len to be larger than chCap to reach
|
|
|
|
// subscriber's chanel overflow.
|
|
|
|
var events = []string{
|
|
|
|
fmt.Sprintf(`{"jsonrpc":"2.0","method":"block_added","params":[%s]}`, b1Verbose),
|
|
|
|
fmt.Sprintf(`{"jsonrpc":"2.0","method":"block_added","params":[%s]}`, b1Verbose),
|
|
|
|
fmt.Sprintf(`{"jsonrpc":"2.0","method":"block_added","params":[%s]}`, b1Verbose),
|
|
|
|
fmt.Sprintf(`{"jsonrpc":"2.0","method":"block_added","params":[%s]}`, b1Verbose),
|
|
|
|
fmt.Sprintf(`{"jsonrpc":"2.0","method":"block_added","params":[%s]}`, b1Verbose),
|
|
|
|
}
|
|
|
|
require.True(t, chCap < len(events))
|
|
|
|
|
|
|
|
var blocksSent atomic.Bool
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.URL.Path == "/ws" && req.Method == "GET" {
|
|
|
|
var upgrader = websocket.Upgrader{}
|
|
|
|
ws, err := upgrader.Upgrade(w, req, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
for _, event := range events {
|
|
|
|
err = ws.SetWriteDeadline(time.Now().Add(2 * time.Second))
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = ws.WriteMessage(1, []byte(event))
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
blocksSent.Store(true)
|
|
|
|
ws.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{CloseNotificationChannelIfFull: true})
|
|
|
|
require.NoError(t, err)
|
|
|
|
wsc.getNextRequestID = getTestRequestID
|
|
|
|
wsc.cacheLock.Lock()
|
|
|
|
wsc.cache.initDone = true // Our server mock is restricted, so perform initialisation manually.
|
|
|
|
wsc.cache.network = netmode.UnitTestNet
|
|
|
|
wsc.cacheLock.Unlock()
|
|
|
|
|
|
|
|
// Our server mock is restricted, so perform subscriptions manually.
|
|
|
|
wsc.subscriptionsLock.Lock()
|
|
|
|
wsc.subscriptions["0"] = &blockReceiver{ch: bCh}
|
|
|
|
wsc.subscriptions["1"] = &blockReceiver{ch: bCh}
|
|
|
|
wsc.receivers[chan<- *block.Block(bCh)] = []string{"0", "1"}
|
|
|
|
wsc.subscriptionsLock.Unlock()
|
|
|
|
|
|
|
|
// Check that events are sent to WSClient.
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
return blocksSent.Load()
|
|
|
|
}, time.Second, 100*time.Millisecond)
|
|
|
|
|
|
|
|
// Check that block receiver channel was removed from the receivers list due to overflow.
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
wsc.subscriptionsLock.RLock()
|
|
|
|
defer wsc.subscriptionsLock.RUnlock()
|
|
|
|
return len(wsc.receivers) == 0
|
|
|
|
}, 2*time.Second, 200*time.Millisecond)
|
|
|
|
|
|
|
|
// Check that subscriptions are still there and waiting for the call to Unsubscribe()
|
|
|
|
// to be excluded from the subscriptions map.
|
|
|
|
wsc.subscriptionsLock.RLock()
|
|
|
|
require.True(t, len(wsc.subscriptions) == 2)
|
|
|
|
wsc.subscriptionsLock.RUnlock()
|
|
|
|
|
|
|
|
// Check that receiver was closed after overflow.
|
|
|
|
for i := 0; i < chCap; i++ {
|
|
|
|
_, ok := <-bCh
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case _, ok := <-bCh:
|
|
|
|
require.False(t, ok)
|
|
|
|
default:
|
|
|
|
t.Fatal("channel wasn't closed by WSClient")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 10:16:42 +00:00
|
|
|
func TestWSExecutionVMStateCheck(t *testing.T) {
|
|
|
|
// Will answer successfully if request slips through.
|
|
|
|
srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "result": "55aaff00"}`)
|
2023-04-25 08:26:36 +00:00
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{})
|
2020-05-13 10:16:42 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-18 17:28:13 +00:00
|
|
|
wsc.getNextRequestID = getTestRequestID
|
2020-10-14 15:13:20 +00:00
|
|
|
require.NoError(t, wsc.Init())
|
2020-05-13 10:16:42 +00:00
|
|
|
filter := "NONE"
|
2022-10-25 12:11:24 +00:00
|
|
|
_, err = wsc.ReceiveExecutions(&neorpc.ExecutionFilter{State: &filter}, make(chan *state.AppExecResult))
|
2020-05-13 10:16:42 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
wsc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWSFilteredSubscriptions(t *testing.T) {
|
|
|
|
var cases = []struct {
|
|
|
|
name string
|
|
|
|
clientCode func(*testing.T, *WSClient)
|
2022-07-07 14:41:01 +00:00
|
|
|
serverCode func(*testing.T, *params.Params)
|
2020-05-13 10:16:42 +00:00
|
|
|
}{
|
2022-10-18 12:09:30 +00:00
|
|
|
{"blocks primary",
|
2020-05-13 10:16:42 +00:00
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
primary := 3
|
2023-04-18 16:36:57 +00:00
|
|
|
_, err := wsc.ReceiveBlocks(&neorpc.BlockFilter{Primary: &primary}, make(chan *block.Block))
|
2020-05-13 10:16:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
2022-07-07 14:41:01 +00:00
|
|
|
func(t *testing.T, p *params.Params) {
|
2020-06-04 11:58:47 +00:00
|
|
|
param := p.Value(1)
|
2022-07-22 16:09:29 +00:00
|
|
|
filt := new(neorpc.BlockFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2022-10-18 12:09:30 +00:00
|
|
|
require.Equal(t, 3, *filt.Primary)
|
|
|
|
require.Equal(t, (*uint32)(nil), filt.Since)
|
2022-10-19 04:31:27 +00:00
|
|
|
require.Equal(t, (*uint32)(nil), filt.Till)
|
2022-10-18 12:09:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{"blocks since",
|
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
var since uint32 = 3
|
2023-04-18 16:36:57 +00:00
|
|
|
_, err := wsc.ReceiveBlocks(&neorpc.BlockFilter{Since: &since}, make(chan *block.Block))
|
2022-10-18 12:09:30 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
func(t *testing.T, p *params.Params) {
|
|
|
|
param := p.Value(1)
|
|
|
|
filt := new(neorpc.BlockFilter)
|
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
|
|
|
require.Equal(t, (*int)(nil), filt.Primary)
|
|
|
|
require.Equal(t, uint32(3), *filt.Since)
|
2022-10-19 04:31:27 +00:00
|
|
|
require.Equal(t, (*uint32)(nil), filt.Till)
|
2022-10-18 12:09:30 +00:00
|
|
|
},
|
|
|
|
},
|
2022-10-19 04:31:27 +00:00
|
|
|
{"blocks till",
|
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
var till uint32 = 3
|
2023-04-18 16:36:57 +00:00
|
|
|
_, err := wsc.ReceiveBlocks(&neorpc.BlockFilter{Till: &till}, make(chan *block.Block))
|
2022-10-19 04:31:27 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
func(t *testing.T, p *params.Params) {
|
|
|
|
param := p.Value(1)
|
|
|
|
filt := new(neorpc.BlockFilter)
|
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
|
|
|
require.Equal(t, (*int)(nil), filt.Primary)
|
|
|
|
require.Equal(t, (*uint32)(nil), filt.Since)
|
|
|
|
require.Equal(t, (uint32)(3), *filt.Till)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"blocks primary, since and till",
|
2022-10-18 12:09:30 +00:00
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
var (
|
|
|
|
since uint32 = 3
|
|
|
|
primary = 2
|
2022-10-19 04:31:27 +00:00
|
|
|
till uint32 = 5
|
2022-10-18 12:09:30 +00:00
|
|
|
)
|
2022-10-25 12:11:24 +00:00
|
|
|
_, err := wsc.ReceiveBlocks(&neorpc.BlockFilter{
|
|
|
|
Primary: &primary,
|
|
|
|
Since: &since,
|
|
|
|
Till: &till,
|
2023-04-18 16:36:57 +00:00
|
|
|
}, make(chan *block.Block))
|
2022-10-18 12:09:30 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
func(t *testing.T, p *params.Params) {
|
|
|
|
param := p.Value(1)
|
|
|
|
filt := new(neorpc.BlockFilter)
|
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
|
|
|
require.Equal(t, 2, *filt.Primary)
|
|
|
|
require.Equal(t, uint32(3), *filt.Since)
|
2022-10-19 04:31:27 +00:00
|
|
|
require.Equal(t, uint32(5), *filt.Till)
|
2020-05-13 10:16:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{"transactions sender",
|
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
sender := util.Uint160{1, 2, 3, 4, 5}
|
2023-04-18 16:36:57 +00:00
|
|
|
_, err := wsc.ReceiveTransactions(&neorpc.TxFilter{Sender: &sender}, make(chan *transaction.Transaction))
|
2020-05-13 10:16:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
2022-07-07 14:41:01 +00:00
|
|
|
func(t *testing.T, p *params.Params) {
|
2020-06-04 11:58:47 +00:00
|
|
|
param := p.Value(1)
|
2022-07-22 16:09:29 +00:00
|
|
|
filt := new(neorpc.TxFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2020-05-13 10:16:42 +00:00
|
|
|
require.Equal(t, util.Uint160{1, 2, 3, 4, 5}, *filt.Sender)
|
2020-07-29 16:57:38 +00:00
|
|
|
require.Nil(t, filt.Signer)
|
2020-05-13 10:16:42 +00:00
|
|
|
},
|
|
|
|
},
|
2020-07-29 16:57:38 +00:00
|
|
|
{"transactions signer",
|
2020-05-13 10:16:42 +00:00
|
|
|
func(t *testing.T, wsc *WSClient) {
|
2020-07-29 16:57:38 +00:00
|
|
|
signer := util.Uint160{0, 42}
|
2023-04-18 16:36:57 +00:00
|
|
|
_, err := wsc.ReceiveTransactions(&neorpc.TxFilter{Signer: &signer}, make(chan *transaction.Transaction))
|
2020-05-13 10:16:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
2022-07-07 14:41:01 +00:00
|
|
|
func(t *testing.T, p *params.Params) {
|
2020-06-04 11:58:47 +00:00
|
|
|
param := p.Value(1)
|
2022-07-22 16:09:29 +00:00
|
|
|
filt := new(neorpc.TxFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2020-05-13 10:16:42 +00:00
|
|
|
require.Nil(t, filt.Sender)
|
2020-07-29 16:57:38 +00:00
|
|
|
require.Equal(t, util.Uint160{0, 42}, *filt.Signer)
|
2020-05-13 10:16:42 +00:00
|
|
|
},
|
|
|
|
},
|
2020-07-29 16:57:38 +00:00
|
|
|
{"transactions sender and signer",
|
2020-05-13 10:16:42 +00:00
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
sender := util.Uint160{1, 2, 3, 4, 5}
|
2020-07-29 16:57:38 +00:00
|
|
|
signer := util.Uint160{0, 42}
|
2023-04-18 16:36:57 +00:00
|
|
|
_, err := wsc.ReceiveTransactions(&neorpc.TxFilter{Sender: &sender, Signer: &signer}, make(chan *transaction.Transaction))
|
2020-05-13 10:16:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
2022-07-07 14:41:01 +00:00
|
|
|
func(t *testing.T, p *params.Params) {
|
2020-06-04 11:58:47 +00:00
|
|
|
param := p.Value(1)
|
2022-07-22 16:09:29 +00:00
|
|
|
filt := new(neorpc.TxFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2020-05-13 10:16:42 +00:00
|
|
|
require.Equal(t, util.Uint160{1, 2, 3, 4, 5}, *filt.Sender)
|
2020-07-29 16:57:38 +00:00
|
|
|
require.Equal(t, util.Uint160{0, 42}, *filt.Signer)
|
2020-05-13 10:16:42 +00:00
|
|
|
},
|
|
|
|
},
|
2020-08-04 13:24:32 +00:00
|
|
|
{"notifications contract hash",
|
2020-05-13 10:16:42 +00:00
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
contract := util.Uint160{1, 2, 3, 4, 5}
|
2023-04-18 16:36:57 +00:00
|
|
|
_, err := wsc.ReceiveExecutionNotifications(&neorpc.NotificationFilter{Contract: &contract}, make(chan *state.ContainedNotificationEvent))
|
2020-05-13 10:16:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
2022-07-07 14:41:01 +00:00
|
|
|
func(t *testing.T, p *params.Params) {
|
2020-06-04 11:58:47 +00:00
|
|
|
param := p.Value(1)
|
2022-07-22 16:09:29 +00:00
|
|
|
filt := new(neorpc.NotificationFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2020-08-04 13:24:32 +00:00
|
|
|
require.Equal(t, util.Uint160{1, 2, 3, 4, 5}, *filt.Contract)
|
|
|
|
require.Nil(t, filt.Name)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"notifications name",
|
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
name := "my_pretty_notification"
|
2023-04-18 16:36:57 +00:00
|
|
|
_, err := wsc.ReceiveExecutionNotifications(&neorpc.NotificationFilter{Name: &name}, make(chan *state.ContainedNotificationEvent))
|
2020-08-04 13:24:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
2022-07-07 14:41:01 +00:00
|
|
|
func(t *testing.T, p *params.Params) {
|
2020-08-04 13:24:32 +00:00
|
|
|
param := p.Value(1)
|
2022-07-22 16:09:29 +00:00
|
|
|
filt := new(neorpc.NotificationFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2020-08-04 13:24:32 +00:00
|
|
|
require.Equal(t, "my_pretty_notification", *filt.Name)
|
|
|
|
require.Nil(t, filt.Contract)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"notifications contract hash and name",
|
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
contract := util.Uint160{1, 2, 3, 4, 5}
|
|
|
|
name := "my_pretty_notification"
|
2023-04-18 16:36:57 +00:00
|
|
|
_, err := wsc.ReceiveExecutionNotifications(&neorpc.NotificationFilter{Contract: &contract, Name: &name}, make(chan *state.ContainedNotificationEvent))
|
2020-08-04 13:24:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
2022-07-07 14:41:01 +00:00
|
|
|
func(t *testing.T, p *params.Params) {
|
2020-08-04 13:24:32 +00:00
|
|
|
param := p.Value(1)
|
2022-07-22 16:09:29 +00:00
|
|
|
filt := new(neorpc.NotificationFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2020-08-04 13:24:32 +00:00
|
|
|
require.Equal(t, util.Uint160{1, 2, 3, 4, 5}, *filt.Contract)
|
|
|
|
require.Equal(t, "my_pretty_notification", *filt.Name)
|
2020-05-13 10:16:42 +00:00
|
|
|
},
|
|
|
|
},
|
2022-10-18 12:20:55 +00:00
|
|
|
{"executions state",
|
2020-05-13 10:16:42 +00:00
|
|
|
func(t *testing.T, wsc *WSClient) {
|
2023-04-18 16:36:57 +00:00
|
|
|
vmstate := "FAULT"
|
|
|
|
_, err := wsc.ReceiveExecutions(&neorpc.ExecutionFilter{State: &vmstate}, make(chan *state.AppExecResult))
|
2020-05-13 10:16:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
2022-07-07 14:41:01 +00:00
|
|
|
func(t *testing.T, p *params.Params) {
|
2020-06-04 11:58:47 +00:00
|
|
|
param := p.Value(1)
|
2022-07-22 16:09:29 +00:00
|
|
|
filt := new(neorpc.ExecutionFilter)
|
2021-10-28 11:10:18 +00:00
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
2022-10-18 12:20:55 +00:00
|
|
|
require.Equal(t, "FAULT", *filt.State)
|
|
|
|
require.Equal(t, (*util.Uint256)(nil), filt.Container)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"executions container",
|
|
|
|
func(t *testing.T, wsc *WSClient) {
|
|
|
|
container := util.Uint256{1, 2, 3}
|
2023-04-18 16:36:57 +00:00
|
|
|
_, err := wsc.ReceiveExecutions(&neorpc.ExecutionFilter{Container: &container}, make(chan *state.AppExecResult))
|
2022-10-18 12:20:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
func(t *testing.T, p *params.Params) {
|
|
|
|
param := p.Value(1)
|
|
|
|
filt := new(neorpc.ExecutionFilter)
|
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
|
|
|
require.Equal(t, (*string)(nil), filt.State)
|
|
|
|
require.Equal(t, util.Uint256{1, 2, 3}, *filt.Container)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"executions state and container",
|
|
|
|
func(t *testing.T, wsc *WSClient) {
|
2023-04-18 16:36:57 +00:00
|
|
|
vmstate := "FAULT"
|
2022-10-18 12:20:55 +00:00
|
|
|
container := util.Uint256{1, 2, 3}
|
2023-04-18 16:36:57 +00:00
|
|
|
_, err := wsc.ReceiveExecutions(&neorpc.ExecutionFilter{State: &vmstate, Container: &container}, make(chan *state.AppExecResult))
|
2022-10-18 12:20:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
func(t *testing.T, p *params.Params) {
|
|
|
|
param := p.Value(1)
|
|
|
|
filt := new(neorpc.ExecutionFilter)
|
|
|
|
require.NoError(t, json.Unmarshal(param.RawMessage, filt))
|
|
|
|
require.Equal(t, "FAULT", *filt.State)
|
|
|
|
require.Equal(t, util.Uint256{1, 2, 3}, *filt.Container)
|
2020-05-13 10:16:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.URL.Path == "/ws" && req.Method == "GET" {
|
|
|
|
var upgrader = websocket.Upgrader{}
|
|
|
|
ws, err := upgrader.Upgrade(w, req, nil)
|
|
|
|
require.NoError(t, err)
|
2021-05-12 18:34:07 +00:00
|
|
|
err = ws.SetReadDeadline(time.Now().Add(2 * time.Second))
|
|
|
|
require.NoError(t, err)
|
2022-07-07 14:41:01 +00:00
|
|
|
req := params.In{}
|
2020-05-13 10:16:42 +00:00
|
|
|
err = ws.ReadJSON(&req)
|
|
|
|
require.NoError(t, err)
|
2022-07-07 14:41:01 +00:00
|
|
|
params := params.Params(req.RawParams)
|
2021-10-28 11:10:18 +00:00
|
|
|
c.serverCode(t, ¶ms)
|
2021-05-12 18:34:07 +00:00
|
|
|
err = ws.SetWriteDeadline(time.Now().Add(2 * time.Second))
|
|
|
|
require.NoError(t, err)
|
2020-05-13 10:16:42 +00:00
|
|
|
err = ws.WriteMessage(1, []byte(`{"jsonrpc": "2.0", "id": 1, "result": "0"}`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
ws.Close()
|
|
|
|
}
|
|
|
|
}))
|
2023-04-25 08:26:36 +00:00
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{})
|
2020-05-13 10:16:42 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-18 17:28:13 +00:00
|
|
|
wsc.getNextRequestID = getTestRequestID
|
2022-02-21 10:13:39 +00:00
|
|
|
wsc.cache.network = netmode.UnitTestNet
|
2020-05-13 10:16:42 +00:00
|
|
|
c.clientCode(t, wsc)
|
|
|
|
wsc.Close()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-05-18 13:23:51 +00:00
|
|
|
|
|
|
|
func TestNewWS(t *testing.T) {
|
|
|
|
srv := initTestServer(t, "")
|
|
|
|
|
|
|
|
t.Run("good", func(t *testing.T) {
|
2023-04-25 08:26:36 +00:00
|
|
|
c, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{})
|
2020-05-18 13:23:51 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-18 17:28:13 +00:00
|
|
|
c.getNextRequestID = getTestRequestID
|
|
|
|
c.cache.network = netmode.UnitTestNet
|
2020-10-14 15:13:20 +00:00
|
|
|
require.NoError(t, c.Init())
|
2020-05-18 13:23:51 +00:00
|
|
|
})
|
|
|
|
t.Run("bad URL", func(t *testing.T) {
|
2023-04-25 08:26:36 +00:00
|
|
|
_, err := NewWS(context.TODO(), strings.TrimPrefix(srv.URL, "http://"), WSOptions{})
|
2020-05-18 13:23:51 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
2022-02-22 13:18:52 +00:00
|
|
|
|
|
|
|
func TestWSConcurrentAccess(t *testing.T) {
|
|
|
|
var ids struct {
|
|
|
|
lock sync.RWMutex
|
|
|
|
m map[int]struct{}
|
|
|
|
}
|
|
|
|
ids.m = make(map[int]struct{})
|
|
|
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.URL.Path == "/ws" && req.Method == "GET" {
|
|
|
|
var upgrader = websocket.Upgrader{}
|
|
|
|
ws, err := upgrader.Upgrade(w, req, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
for {
|
|
|
|
err = ws.SetReadDeadline(time.Now().Add(2 * time.Second))
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, p, err := ws.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2022-07-07 14:41:01 +00:00
|
|
|
r := params.NewIn()
|
2022-02-22 13:18:52 +00:00
|
|
|
err = json.Unmarshal(p, r)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot decode request body: %s", req.Body)
|
|
|
|
}
|
|
|
|
i, err := strconv.Atoi(string(r.RawID))
|
|
|
|
require.NoError(t, err)
|
|
|
|
ids.lock.Lock()
|
|
|
|
ids.m[i] = struct{}{}
|
|
|
|
ids.lock.Unlock()
|
|
|
|
var response string
|
|
|
|
// Different responses to catch possible unmarshalling errors connected with invalid IDs distribution.
|
|
|
|
switch r.Method {
|
|
|
|
case "getblockcount":
|
|
|
|
response = fmt.Sprintf(`{"id":%s,"jsonrpc":"2.0","result":123}`, r.RawID)
|
|
|
|
case "getversion":
|
|
|
|
response = fmt.Sprintf(`{"id":%s,"jsonrpc":"2.0","result":{"network":42,"tcpport":20332,"wsport":20342,"nonce":2153672787,"useragent":"/NEO-GO:0.73.1-pre-273-ge381358/"}}`, r.RawID)
|
|
|
|
case "getblockhash":
|
|
|
|
response = fmt.Sprintf(`{"id":%s,"jsonrpc":"2.0","result":"0x157ca5e5b8cf8f84c9660502a3270b346011612bded1514a6847f877c433a9bb"}`, r.RawID)
|
|
|
|
}
|
|
|
|
err = ws.SetWriteDeadline(time.Now().Add(2 * time.Second))
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = ws.WriteMessage(1, []byte(response))
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
t.Cleanup(srv.Close)
|
|
|
|
|
2023-04-25 08:26:36 +00:00
|
|
|
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{})
|
2022-02-22 13:18:52 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
batchCount := 100
|
2023-10-11 10:24:16 +00:00
|
|
|
completed := &atomic.Int32{}
|
2022-02-22 13:18:52 +00:00
|
|
|
for i := 0; i < batchCount; i++ {
|
|
|
|
go func() {
|
|
|
|
_, err := wsc.GetBlockCount()
|
|
|
|
require.NoError(t, err)
|
2023-10-11 10:24:16 +00:00
|
|
|
completed.Add(1)
|
2022-02-22 13:18:52 +00:00
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
_, err := wsc.GetBlockHash(123)
|
|
|
|
require.NoError(t, err)
|
2023-10-11 10:24:16 +00:00
|
|
|
completed.Add(1)
|
2022-02-22 13:18:52 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err := wsc.GetVersion()
|
|
|
|
require.NoError(t, err)
|
2023-10-11 10:24:16 +00:00
|
|
|
completed.Add(1)
|
2022-02-22 13:18:52 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
return int(completed.Load()) == batchCount*3
|
|
|
|
}, time.Second, 100*time.Millisecond)
|
|
|
|
|
|
|
|
ids.lock.RLock()
|
|
|
|
require.True(t, len(ids.m) > batchCount)
|
|
|
|
idsList := make([]int, 0, len(ids.m))
|
|
|
|
for i := range ids.m {
|
|
|
|
idsList = append(idsList, i)
|
|
|
|
}
|
|
|
|
ids.lock.RUnlock()
|
|
|
|
|
|
|
|
sort.Ints(idsList)
|
|
|
|
require.Equal(t, 1, idsList[0])
|
|
|
|
require.Less(t, idsList[len(idsList)-1],
|
|
|
|
batchCount*3+1) // batchCount*requestsPerBatch+1
|
|
|
|
wsc.Close()
|
|
|
|
}
|
2022-04-05 08:28:26 +00:00
|
|
|
|
|
|
|
func TestWSDoubleClose(t *testing.T) {
|
|
|
|
srv := initTestServer(t, "")
|
|
|
|
|
2023-04-25 08:26:36 +00:00
|
|
|
c, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{})
|
2022-04-05 08:28:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NotPanics(t, func() {
|
|
|
|
c.Close()
|
|
|
|
c.Close()
|
|
|
|
})
|
|
|
|
}
|
rpc: avoid panic during request after WS connection is closed
Fix the following panic:
```
panic: assignment to entry in nil map
goroutine 131 [running]:
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*WSClient).registerRespChannel(0xc00033c240, 0x0, 0xc00003e2a0)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/wsclient.go:244 +0x96
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*WSClient).makeWsRequest(0xc00033c240, 0xc002080000)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/wsclient.go:264 +0x69
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).performRequest(0xc00033c240, {0xc9f173, 0xd}, {{0x13d09d0, 0x0, 0x0}}, {0xb44120, 0xc00147a000})
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/client.go:186 +0x15d
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).GetBlockCount(0xc001fb5440)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/rpc.go:73 +0x69
...
```
2022-04-26 14:19:51 +00:00
|
|
|
|
|
|
|
func TestWS_RequestAfterClose(t *testing.T) {
|
|
|
|
srv := initTestServer(t, "")
|
|
|
|
|
2023-04-25 08:26:36 +00:00
|
|
|
c, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{})
|
rpc: avoid panic during request after WS connection is closed
Fix the following panic:
```
panic: assignment to entry in nil map
goroutine 131 [running]:
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*WSClient).registerRespChannel(0xc00033c240, 0x0, 0xc00003e2a0)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/wsclient.go:244 +0x96
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*WSClient).makeWsRequest(0xc00033c240, 0xc002080000)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/wsclient.go:264 +0x69
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).performRequest(0xc00033c240, {0xc9f173, 0xd}, {{0x13d09d0, 0x0, 0x0}}, {0xb44120, 0xc00147a000})
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/client.go:186 +0x15d
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).GetBlockCount(0xc001fb5440)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/rpc.go:73 +0x69
...
```
2022-04-26 14:19:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
c.Close()
|
|
|
|
|
|
|
|
require.NotPanics(t, func() {
|
|
|
|
_, err = c.GetBlockCount()
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
2023-05-04 14:03:06 +00:00
|
|
|
require.ErrorIs(t, err, ErrWSConnLost)
|
rpc: avoid panic during request after WS connection is closed
Fix the following panic:
```
panic: assignment to entry in nil map
goroutine 131 [running]:
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*WSClient).registerRespChannel(0xc00033c240, 0x0, 0xc00003e2a0)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/wsclient.go:244 +0x96
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*WSClient).makeWsRequest(0xc00033c240, 0xc002080000)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/wsclient.go:264 +0x69
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).performRequest(0xc00033c240, {0xc9f173, 0xd}, {{0x13d09d0, 0x0, 0x0}}, {0xb44120, 0xc00147a000})
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/client.go:186 +0x15d
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).GetBlockCount(0xc001fb5440)
/home/denis/go/pkg/mod/github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/rpc.go:73 +0x69
...
```
2022-04-26 14:19:51 +00:00
|
|
|
}
|
2022-05-23 10:47:52 +00:00
|
|
|
|
|
|
|
func TestWSClient_ConnClosedError(t *testing.T) {
|
|
|
|
t.Run("standard closing", func(t *testing.T) {
|
2022-06-10 14:20:45 +00:00
|
|
|
srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "result": 123}`)
|
2023-04-25 08:26:36 +00:00
|
|
|
c, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{})
|
2022-05-23 10:47:52 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-10 14:20:45 +00:00
|
|
|
// Check client is working.
|
|
|
|
_, err = c.GetBlockCount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = c.GetError()
|
|
|
|
require.NoError(t, err)
|
2022-05-23 10:47:52 +00:00
|
|
|
|
2022-06-10 14:20:45 +00:00
|
|
|
c.Close()
|
2022-05-23 10:47:52 +00:00
|
|
|
err = c.GetError()
|
2022-06-10 14:20:45 +00:00
|
|
|
require.NoError(t, err)
|
2022-05-23 10:47:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("malformed request", func(t *testing.T) {
|
2022-06-10 14:20:45 +00:00
|
|
|
srv := initTestServer(t, "")
|
2023-04-25 08:26:36 +00:00
|
|
|
c, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{})
|
2022-05-23 10:47:52 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
defaultMaxBlockSize := 262144
|
|
|
|
_, err = c.SubmitP2PNotaryRequest(&payload.P2PNotaryRequest{
|
|
|
|
MainTransaction: &transaction.Transaction{
|
|
|
|
Script: make([]byte, defaultMaxBlockSize*3),
|
|
|
|
},
|
|
|
|
FallbackTransaction: &transaction.Transaction{},
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
err = c.GetError()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, strings.Contains(err.Error(), "failed to read JSON response (timeout/connection loss/malformed response)"), err.Error())
|
|
|
|
})
|
|
|
|
}
|