2022-07-21 19:39:53 +00:00
|
|
|
package rpcclient
|
2020-04-29 19:51:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-05-23 10:47:52 +00:00
|
|
|
"fmt"
|
2022-02-18 17:28:13 +00:00
|
|
|
"strconv"
|
2022-02-21 13:40:50 +00:00
|
|
|
"sync"
|
2020-04-29 19:51:43 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2020-05-12 08:18:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-09-03 16:58:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-05-12 08:18:44 +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-07-22 18:26:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2020-05-13 10:16:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2022-04-05 08:28:26 +00:00
|
|
|
"go.uber.org/atomic"
|
2020-04-29 19:51:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// WSClient is a websocket-enabled RPC client that can be used with appropriate
|
|
|
|
// servers. It's supposed to be faster than Client because it has persistent
|
2022-04-20 18:30:09 +00:00
|
|
|
// connection to the server and at the same time it exposes some functionality
|
2020-04-29 19:51:43 +00:00
|
|
|
// that is only provided via websockets (like event subscription mechanism).
|
2022-02-22 13:49:38 +00:00
|
|
|
// WSClient is thread-safe and can be used from multiple goroutines to perform
|
|
|
|
// RPC requests.
|
2020-04-29 19:51:43 +00:00
|
|
|
type WSClient struct {
|
|
|
|
Client
|
2020-05-12 08:18:44 +00:00
|
|
|
// Notifications is a channel that is used to send events received from
|
2022-04-20 18:30:09 +00:00
|
|
|
// the server. Client's code is supposed to be reading from this channel if
|
|
|
|
// it wants to use subscription mechanism. Failing to do so will cause
|
2020-05-12 08:18:44 +00:00
|
|
|
// WSClient to block even regular requests. This channel is not buffered.
|
2022-04-20 18:30:09 +00:00
|
|
|
// In case of protocol error or upon connection closure, this channel will
|
2020-05-12 08:18:44 +00:00
|
|
|
// be closed, so make sure to handle this.
|
|
|
|
Notifications chan Notification
|
|
|
|
|
2022-04-05 08:28:26 +00:00
|
|
|
ws *websocket.Conn
|
|
|
|
done chan struct{}
|
2022-07-22 16:09:29 +00:00
|
|
|
requests chan *neorpc.Request
|
2022-04-05 08:28:26 +00:00
|
|
|
shutdown chan struct{}
|
|
|
|
closeCalled atomic.Bool
|
2022-02-21 13:40:50 +00:00
|
|
|
|
2022-05-23 10:47:52 +00:00
|
|
|
closeErrLock sync.RWMutex
|
|
|
|
closeErr error
|
|
|
|
|
2022-02-21 13:40:50 +00:00
|
|
|
subscriptionsLock sync.RWMutex
|
|
|
|
subscriptions map[string]bool
|
2022-02-18 17:28:13 +00:00
|
|
|
|
|
|
|
respLock sync.RWMutex
|
2022-07-22 16:09:29 +00:00
|
|
|
respChannels map[uint64]chan *neorpc.Response
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Notification represents a server-generated notification for client subscriptions.
|
2022-07-22 18:17:23 +00:00
|
|
|
// Value can be one of block.Block, state.AppExecResult, state.ContainedNotificationEvent
|
2021-09-24 09:23:30 +00:00
|
|
|
// transaction.Transaction or subscriptions.NotaryRequestEvent based on Type.
|
2020-05-12 08:18:44 +00:00
|
|
|
type Notification struct {
|
2022-07-22 16:09:29 +00:00
|
|
|
Type neorpc.EventID
|
2020-05-12 08:18:44 +00:00
|
|
|
Value interface{}
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// requestResponse is a combined type for request and response since we can get
|
|
|
|
// any of them here.
|
|
|
|
type requestResponse struct {
|
2022-07-22 16:09:29 +00:00
|
|
|
neorpc.Response
|
2022-07-07 14:41:01 +00:00
|
|
|
Method string `json:"method"`
|
|
|
|
RawParams []json.RawMessage `json:"params,omitempty"`
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Message limit for receiving side.
|
|
|
|
wsReadLimit = 10 * 1024 * 1024
|
|
|
|
|
|
|
|
// Disconnection timeout.
|
|
|
|
wsPongLimit = 60 * time.Second
|
|
|
|
|
|
|
|
// Ping period for connection liveness check.
|
|
|
|
wsPingPeriod = wsPongLimit / 2
|
|
|
|
|
|
|
|
// Write deadline.
|
|
|
|
wsWriteLimit = wsPingPeriod / 2
|
|
|
|
)
|
|
|
|
|
2022-06-10 14:20:45 +00:00
|
|
|
// errConnClosedByUser is a WSClient error used iff the user calls (*WSClient).Close method by himself.
|
|
|
|
var errConnClosedByUser = errors.New("connection closed by user")
|
|
|
|
|
2020-04-29 19:51:43 +00:00
|
|
|
// NewWS returns a new WSClient ready to use (with established websocket
|
|
|
|
// connection). You need to use websocket URL for it like `ws://1.2.3.4/ws`.
|
2022-04-20 18:30:09 +00:00
|
|
|
// You should call Init method to initialize the network magic the client is
|
2020-10-14 15:13:20 +00:00
|
|
|
// operating on.
|
2020-04-29 19:51:43 +00:00
|
|
|
func NewWS(ctx context.Context, endpoint string, opts Options) (*WSClient, error) {
|
|
|
|
dialer := websocket.Dialer{HandshakeTimeout: opts.DialTimeout}
|
|
|
|
ws, _, err := dialer.Dial(endpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wsc := &WSClient{
|
2022-02-21 15:30:07 +00:00
|
|
|
Client: Client{},
|
2020-05-12 08:18:44 +00:00
|
|
|
Notifications: make(chan Notification),
|
|
|
|
|
|
|
|
ws: ws,
|
|
|
|
shutdown: make(chan struct{}),
|
|
|
|
done: make(chan struct{}),
|
2022-04-05 08:28:26 +00:00
|
|
|
closeCalled: *atomic.NewBool(false),
|
2022-07-22 16:09:29 +00:00
|
|
|
respChannels: make(map[uint64]chan *neorpc.Response),
|
|
|
|
requests: make(chan *neorpc.Request),
|
2020-05-12 08:18:44 +00:00
|
|
|
subscriptions: make(map[string]bool),
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
2022-02-21 15:30:07 +00:00
|
|
|
|
|
|
|
err = initClient(ctx, &wsc.Client, endpoint, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wsc.Client.cli = nil
|
|
|
|
|
2020-04-29 19:51:43 +00:00
|
|
|
go wsc.wsReader()
|
|
|
|
go wsc.wsWriter()
|
|
|
|
wsc.requestF = wsc.makeWsRequest
|
|
|
|
return wsc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes connection to the remote side rendering this client instance
|
|
|
|
// unusable.
|
|
|
|
func (c *WSClient) Close() {
|
2022-04-05 08:28:26 +00:00
|
|
|
if c.closeCalled.CAS(false, true) {
|
2022-06-10 14:20:45 +00:00
|
|
|
c.setCloseErr(errConnClosedByUser)
|
2022-04-20 18:30:09 +00:00
|
|
|
// Closing shutdown channel sends a signal to wsWriter to break out of the
|
2022-04-05 08:28:26 +00:00
|
|
|
// loop. In doing so it does ws.Close() closing the network connection
|
2022-04-20 18:30:09 +00:00
|
|
|
// which in turn makes wsReader receive an err from ws.ReadJSON() and also
|
2022-04-05 08:28:26 +00:00
|
|
|
// break out of the loop closing c.done channel in its shutdown sequence.
|
|
|
|
close(c.shutdown)
|
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
<-c.done
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *WSClient) wsReader() {
|
|
|
|
c.ws.SetReadLimit(wsReadLimit)
|
2022-05-23 10:47:52 +00:00
|
|
|
c.ws.SetPongHandler(func(string) error {
|
|
|
|
err := c.ws.SetReadDeadline(time.Now().Add(wsPongLimit))
|
|
|
|
if err != nil {
|
|
|
|
c.setCloseErr(fmt.Errorf("failed to set pong read deadline: %w", err))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
var connCloseErr error
|
2020-05-12 08:18:44 +00:00
|
|
|
readloop:
|
2020-04-29 19:51:43 +00:00
|
|
|
for {
|
|
|
|
rr := new(requestResponse)
|
2021-05-12 18:45:32 +00:00
|
|
|
err := c.ws.SetReadDeadline(time.Now().Add(wsPongLimit))
|
|
|
|
if err != nil {
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to set response read deadline: %w", err)
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop
|
2021-05-12 18:45:32 +00:00
|
|
|
}
|
|
|
|
err = c.ws.ReadJSON(rr)
|
2020-04-29 19:51:43 +00:00
|
|
|
if err != nil {
|
|
|
|
// Timeout/connection loss/malformed response.
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to read JSON response (timeout/connection loss/malformed response): %w", err)
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
2022-07-07 14:41:01 +00:00
|
|
|
if rr.ID == nil && rr.Method != "" {
|
2022-07-22 16:09:29 +00:00
|
|
|
event, err := neorpc.GetEventIDFromString(rr.Method)
|
2020-05-12 08:18:44 +00:00
|
|
|
if err != nil {
|
|
|
|
// Bad event received.
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to perse event ID from string %s: %w", rr.Method, err)
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
2022-07-22 16:09:29 +00:00
|
|
|
if event != neorpc.MissedEventID && len(rr.RawParams) != 1 {
|
2020-05-12 08:18:44 +00:00
|
|
|
// Bad event received.
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("bad event received: %s / %d", event, len(rr.RawParams))
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
var val interface{}
|
|
|
|
switch event {
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.BlockEventID:
|
2022-02-21 10:41:14 +00:00
|
|
|
sr, err := c.StateRootInHeader()
|
|
|
|
if err != nil {
|
2022-04-20 18:30:09 +00:00
|
|
|
// Client is not initialized.
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to fetch StateRootInHeader: %w", err)
|
|
|
|
break readloop
|
2022-02-21 10:41:14 +00:00
|
|
|
}
|
|
|
|
val = block.New(sr)
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.TransactionEventID:
|
2021-03-25 16:18:01 +00:00
|
|
|
val = &transaction.Transaction{}
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.NotificationEventID:
|
2022-07-22 18:17:23 +00:00
|
|
|
val = new(state.ContainedNotificationEvent)
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.ExecutionEventID:
|
2020-09-03 16:58:50 +00:00
|
|
|
val = new(state.AppExecResult)
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.NotaryRequestEventID:
|
2022-07-22 18:26:29 +00:00
|
|
|
val = new(result.NotaryRequestEvent)
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.MissedEventID:
|
2020-05-12 19:38:29 +00:00
|
|
|
// No value.
|
2020-05-12 08:18:44 +00:00
|
|
|
default:
|
|
|
|
// Bad event received.
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("unknown event received: %d", event)
|
2020-05-12 08:18:44 +00:00
|
|
|
break readloop
|
|
|
|
}
|
2022-07-22 16:09:29 +00:00
|
|
|
if event != neorpc.MissedEventID {
|
2022-07-07 14:41:01 +00:00
|
|
|
err = json.Unmarshal(rr.RawParams[0], val)
|
2020-05-12 19:38:29 +00:00
|
|
|
if err != nil {
|
|
|
|
// Bad event received.
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to unmarshal event of type %s from JSON: %w", event, err)
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop
|
2020-05-12 19:38:29 +00:00
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
c.Notifications <- Notification{event, val}
|
2022-07-07 14:41:01 +00:00
|
|
|
} else if rr.ID != nil && (rr.Error != nil || rr.Result != nil) {
|
2022-07-07 14:45:33 +00:00
|
|
|
id, err := strconv.ParseUint(string(rr.ID), 10, 64)
|
2022-02-18 17:28:13 +00:00
|
|
|
if err != nil {
|
2022-07-07 14:41:01 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to retrieve response ID from string %s: %w", string(rr.ID), err)
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop // Malformed response (invalid response ID).
|
2022-02-18 17:28:13 +00:00
|
|
|
}
|
2022-07-07 14:45:33 +00:00
|
|
|
ch := c.getResponseChannel(id)
|
2022-02-18 17:28:13 +00:00
|
|
|
if ch == nil {
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("unknown response channel for response %d", id)
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop // Unknown response (unexpected response ID).
|
2022-02-18 17:28:13 +00:00
|
|
|
}
|
2022-07-22 16:09:29 +00:00
|
|
|
ch <- &rr.Response
|
2020-04-29 19:51:43 +00:00
|
|
|
} else {
|
|
|
|
// Malformed response, neither valid request, nor valid response.
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("malformed response")
|
2022-06-10 14:20:45 +00:00
|
|
|
break readloop
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-23 10:47:52 +00:00
|
|
|
if connCloseErr != nil {
|
|
|
|
c.setCloseErr(connCloseErr)
|
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
close(c.done)
|
2022-02-18 17:28:13 +00:00
|
|
|
c.respLock.Lock()
|
|
|
|
for _, ch := range c.respChannels {
|
|
|
|
close(ch)
|
|
|
|
}
|
|
|
|
c.respChannels = nil
|
|
|
|
c.respLock.Unlock()
|
2020-05-12 08:18:44 +00:00
|
|
|
close(c.Notifications)
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *WSClient) wsWriter() {
|
|
|
|
pingTicker := time.NewTicker(wsPingPeriod)
|
|
|
|
defer c.ws.Close()
|
|
|
|
defer pingTicker.Stop()
|
2022-05-23 10:47:52 +00:00
|
|
|
var connCloseErr error
|
|
|
|
writeloop:
|
2020-04-29 19:51:43 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.shutdown:
|
|
|
|
return
|
|
|
|
case <-c.done:
|
|
|
|
return
|
|
|
|
case req, ok := <-c.requests:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-05-12 18:45:32 +00:00
|
|
|
if err := c.ws.SetWriteDeadline(time.Now().Add(c.opts.RequestTimeout)); err != nil {
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to set request write deadline: %w", err)
|
|
|
|
break writeloop
|
2021-05-12 18:45:32 +00:00
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
if err := c.ws.WriteJSON(req); err != nil {
|
2022-07-07 15:33:23 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to write JSON request (%s / %d): %w", req.Method, len(req.Params), err)
|
2022-05-23 10:47:52 +00:00
|
|
|
break writeloop
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
|
|
|
case <-pingTicker.C:
|
2021-05-12 18:45:32 +00:00
|
|
|
if err := c.ws.SetWriteDeadline(time.Now().Add(wsWriteLimit)); err != nil {
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to set ping write deadline: %w", err)
|
|
|
|
break writeloop
|
2021-05-12 18:45:32 +00:00
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
if err := c.ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
2022-05-23 10:47:52 +00:00
|
|
|
connCloseErr = fmt.Errorf("failed to write ping message: %w", err)
|
|
|
|
break writeloop
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-23 10:47:52 +00:00
|
|
|
if connCloseErr != nil {
|
|
|
|
c.setCloseErr(connCloseErr)
|
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 17:28:13 +00:00
|
|
|
func (c *WSClient) unregisterRespChannel(id uint64) {
|
|
|
|
c.respLock.Lock()
|
|
|
|
defer c.respLock.Unlock()
|
|
|
|
if ch, ok := c.respChannels[id]; ok {
|
|
|
|
delete(c.respChannels, id)
|
|
|
|
close(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 16:09:29 +00:00
|
|
|
func (c *WSClient) getResponseChannel(id uint64) chan *neorpc.Response {
|
2022-02-18 17:28:13 +00:00
|
|
|
c.respLock.RLock()
|
|
|
|
defer c.respLock.RUnlock()
|
|
|
|
return c.respChannels[id]
|
|
|
|
}
|
|
|
|
|
2022-07-22 16:09:29 +00:00
|
|
|
func (c *WSClient) makeWsRequest(r *neorpc.Request) (*neorpc.Response, error) {
|
|
|
|
ch := make(chan *neorpc.Response)
|
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
|
|
|
c.respLock.Lock()
|
|
|
|
select {
|
|
|
|
case <-c.done:
|
|
|
|
c.respLock.Unlock()
|
|
|
|
return nil, errors.New("connection lost before registering response channel")
|
|
|
|
default:
|
|
|
|
c.respChannels[r.ID] = ch
|
|
|
|
c.respLock.Unlock()
|
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
select {
|
|
|
|
case <-c.done:
|
2022-02-18 17:28:13 +00:00
|
|
|
return nil, errors.New("connection lost before sending the request")
|
2020-04-29 19:51:43 +00:00
|
|
|
case c.requests <- r:
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-c.done:
|
2022-02-18 17:28:13 +00:00
|
|
|
return nil, errors.New("connection lost while waiting for the response")
|
|
|
|
case resp := <-ch:
|
|
|
|
c.unregisterRespChannel(r.ID)
|
2020-04-29 19:51:43 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
|
2022-07-07 17:03:10 +00:00
|
|
|
func (c *WSClient) performSubscription(params []interface{}) (string, error) {
|
2020-05-12 08:18:44 +00:00
|
|
|
var resp string
|
|
|
|
|
|
|
|
if err := c.performRequest("subscribe", params, &resp); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-02-21 13:40:50 +00:00
|
|
|
|
|
|
|
c.subscriptionsLock.Lock()
|
|
|
|
defer c.subscriptionsLock.Unlock()
|
|
|
|
|
2020-05-12 08:18:44 +00:00
|
|
|
c.subscriptions[resp] = true
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *WSClient) performUnsubscription(id string) error {
|
|
|
|
var resp bool
|
|
|
|
|
2022-02-21 13:40:50 +00:00
|
|
|
c.subscriptionsLock.Lock()
|
|
|
|
defer c.subscriptionsLock.Unlock()
|
|
|
|
|
2020-05-12 08:18:44 +00:00
|
|
|
if !c.subscriptions[id] {
|
|
|
|
return errors.New("no subscription with this ID")
|
|
|
|
}
|
2022-07-07 17:03:10 +00:00
|
|
|
if err := c.performRequest("unsubscribe", []interface{}{id}, &resp); err != nil {
|
2020-05-12 08:18:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !resp {
|
|
|
|
return errors.New("unsubscribe method returned false result")
|
|
|
|
}
|
|
|
|
delete(c.subscriptions, id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeForNewBlocks adds subscription for new block events to this instance
|
2022-04-20 18:30:09 +00:00
|
|
|
// of the client. It can be filtered by primary consensus node index, nil value doesn't
|
2020-05-13 10:16:42 +00:00
|
|
|
// add any filters.
|
|
|
|
func (c *WSClient) SubscribeForNewBlocks(primary *int) (string, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
params := []interface{}{"block_added"}
|
2020-05-13 10:16:42 +00:00
|
|
|
if primary != nil {
|
2022-07-22 16:09:29 +00:00
|
|
|
params = append(params, neorpc.BlockFilter{Primary: *primary})
|
2020-05-13 10:16:42 +00:00
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
return c.performSubscription(params)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeForNewTransactions adds subscription for new transaction events to
|
2022-04-20 18:30:09 +00:00
|
|
|
// this instance of the client. It can be filtered by the sender and/or the signer, nil
|
2020-05-13 10:16:42 +00:00
|
|
|
// value is treated as missing filter.
|
2020-07-29 16:57:38 +00:00
|
|
|
func (c *WSClient) SubscribeForNewTransactions(sender *util.Uint160, signer *util.Uint160) (string, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
params := []interface{}{"transaction_added"}
|
2020-07-29 16:57:38 +00:00
|
|
|
if sender != nil || signer != nil {
|
2022-07-22 16:09:29 +00:00
|
|
|
params = append(params, neorpc.TxFilter{Sender: sender, Signer: signer})
|
2020-05-13 10:16:42 +00:00
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
return c.performSubscription(params)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeForExecutionNotifications adds subscription for notifications
|
2022-04-20 18:30:09 +00:00
|
|
|
// generated during transaction execution to this instance of the client. It can be
|
|
|
|
// filtered by the contract's hash (that emits notifications), nil value puts no such
|
2020-05-13 10:16:42 +00:00
|
|
|
// restrictions.
|
2020-08-04 13:24:32 +00:00
|
|
|
func (c *WSClient) SubscribeForExecutionNotifications(contract *util.Uint160, name *string) (string, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
params := []interface{}{"notification_from_execution"}
|
2020-08-04 13:24:32 +00:00
|
|
|
if contract != nil || name != nil {
|
2022-07-22 16:09:29 +00:00
|
|
|
params = append(params, neorpc.NotificationFilter{Contract: contract, Name: name})
|
2020-05-13 10:16:42 +00:00
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
return c.performSubscription(params)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeForTransactionExecutions adds subscription for application execution
|
2022-04-20 18:30:09 +00:00
|
|
|
// results generated during transaction execution to this instance of the client. It can
|
2020-05-13 10:16:42 +00:00
|
|
|
// be filtered by state (HALT/FAULT) to check for successful or failing
|
|
|
|
// transactions, nil value means no filtering.
|
|
|
|
func (c *WSClient) SubscribeForTransactionExecutions(state *string) (string, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
params := []interface{}{"transaction_executed"}
|
2020-05-13 10:16:42 +00:00
|
|
|
if state != nil {
|
|
|
|
if *state != "HALT" && *state != "FAULT" {
|
|
|
|
return "", errors.New("bad state parameter")
|
|
|
|
}
|
2022-07-22 16:09:29 +00:00
|
|
|
params = append(params, neorpc.ExecutionFilter{State: *state})
|
2020-05-13 10:16:42 +00:00
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
return c.performSubscription(params)
|
|
|
|
}
|
|
|
|
|
2021-05-28 11:55:06 +00:00
|
|
|
// SubscribeForNotaryRequests adds subscription for notary request payloads
|
|
|
|
// addition or removal events to this instance of client. It can be filtered by
|
|
|
|
// request sender's hash, or main tx signer's hash, nil value puts no such
|
|
|
|
// restrictions.
|
|
|
|
func (c *WSClient) SubscribeForNotaryRequests(sender *util.Uint160, mainSigner *util.Uint160) (string, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
params := []interface{}{"notary_request_event"}
|
2021-05-28 11:55:06 +00:00
|
|
|
if sender != nil {
|
2022-07-22 16:09:29 +00:00
|
|
|
params = append(params, neorpc.TxFilter{Sender: sender, Signer: mainSigner})
|
2021-05-28 11:55:06 +00:00
|
|
|
}
|
|
|
|
return c.performSubscription(params)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Unsubscribe removes subscription for the given event stream.
|
2020-05-12 08:18:44 +00:00
|
|
|
func (c *WSClient) Unsubscribe(id string) error {
|
|
|
|
return c.performUnsubscription(id)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnsubscribeAll removes all active subscriptions of the current client.
|
2020-05-12 08:18:44 +00:00
|
|
|
func (c *WSClient) UnsubscribeAll() error {
|
2022-02-21 13:40:50 +00:00
|
|
|
c.subscriptionsLock.Lock()
|
|
|
|
defer c.subscriptionsLock.Unlock()
|
|
|
|
|
2020-05-12 08:18:44 +00:00
|
|
|
for id := range c.subscriptions {
|
2022-02-21 13:40:50 +00:00
|
|
|
var resp bool
|
2022-07-07 17:03:10 +00:00
|
|
|
if err := c.performRequest("unsubscribe", []interface{}{id}, &resp); err != nil {
|
2020-05-12 08:18:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-02-21 13:40:50 +00:00
|
|
|
if !resp {
|
|
|
|
return errors.New("unsubscribe method returned false result")
|
|
|
|
}
|
|
|
|
delete(c.subscriptions, id)
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-23 10:47:52 +00:00
|
|
|
|
|
|
|
// setCloseErr is a thread-safe method setting closeErr in case if it's not yet set.
|
|
|
|
func (c *WSClient) setCloseErr(err error) {
|
|
|
|
c.closeErrLock.Lock()
|
|
|
|
defer c.closeErrLock.Unlock()
|
|
|
|
|
|
|
|
if c.closeErr == nil {
|
|
|
|
c.closeErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 14:20:45 +00:00
|
|
|
// GetError returns the reason of WS connection closing. It returns nil in case if connection
|
|
|
|
// was closed by the use via Close() method calling.
|
2022-05-23 10:47:52 +00:00
|
|
|
func (c *WSClient) GetError() error {
|
|
|
|
c.closeErrLock.RLock()
|
|
|
|
defer c.closeErrLock.RUnlock()
|
|
|
|
|
2022-06-10 14:20:45 +00:00
|
|
|
if c.closeErr != nil && errors.Is(c.closeErr, errConnClosedByUser) {
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-23 10:47:52 +00:00
|
|
|
return c.closeErr
|
|
|
|
}
|