2020-04-29 19:51:43 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
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"
|
2020-04-29 19:51:43 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/response"
|
2021-09-24 09:15:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions"
|
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{}
|
|
|
|
requests chan *request.Raw
|
|
|
|
shutdown chan struct{}
|
|
|
|
closeCalled atomic.Bool
|
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
|
|
|
|
respChannels map[uint64]chan *response.Raw
|
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.
|
2021-09-24 09:15:25 +00:00
|
|
|
// Value can be one of block.Block, state.AppExecResult, subscriptions.NotificationEvent
|
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 {
|
|
|
|
Type response.EventID
|
|
|
|
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 {
|
|
|
|
request.In
|
|
|
|
Error *response.Error `json:"error,omitempty"`
|
|
|
|
Result json.RawMessage `json:"result,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-02-18 17:28:13 +00:00
|
|
|
respChannels: make(map[uint64]chan *response.Raw),
|
2020-05-12 08:18:44 +00:00
|
|
|
requests: make(chan *request.Raw),
|
|
|
|
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-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)
|
2021-05-12 18:45:32 +00:00
|
|
|
c.ws.SetPongHandler(func(string) error { return c.ws.SetReadDeadline(time.Now().Add(wsPongLimit)) })
|
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 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = c.ws.ReadJSON(rr)
|
2020-04-29 19:51:43 +00:00
|
|
|
if err != nil {
|
|
|
|
// Timeout/connection loss/malformed response.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if rr.RawID == nil && rr.Method != "" {
|
2020-05-12 08:18:44 +00:00
|
|
|
event, err := response.GetEventIDFromString(rr.Method)
|
|
|
|
if err != nil {
|
|
|
|
// Bad event received.
|
|
|
|
break
|
|
|
|
}
|
2021-10-28 11:10:18 +00:00
|
|
|
if event != response.MissedEventID && len(rr.RawParams) != 1 {
|
2020-05-12 08:18:44 +00:00
|
|
|
// Bad event received.
|
|
|
|
break
|
2020-04-29 19:51:43 +00:00
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
var val interface{}
|
|
|
|
switch event {
|
|
|
|
case response.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-02-21 10:41:14 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
val = block.New(sr)
|
2020-05-12 08:18:44 +00:00
|
|
|
case response.TransactionEventID:
|
2021-03-25 16:18:01 +00:00
|
|
|
val = &transaction.Transaction{}
|
2020-05-12 08:18:44 +00:00
|
|
|
case response.NotificationEventID:
|
2021-09-24 09:15:25 +00:00
|
|
|
val = new(subscriptions.NotificationEvent)
|
2020-05-12 08:18:44 +00:00
|
|
|
case response.ExecutionEventID:
|
2020-09-03 16:58:50 +00:00
|
|
|
val = new(state.AppExecResult)
|
2021-05-28 11:55:06 +00:00
|
|
|
case response.NotaryRequestEventID:
|
2021-09-24 09:23:30 +00:00
|
|
|
val = new(subscriptions.NotaryRequestEvent)
|
2020-05-12 19:38:29 +00:00
|
|
|
case response.MissedEventID:
|
|
|
|
// No value.
|
2020-05-12 08:18:44 +00:00
|
|
|
default:
|
|
|
|
// Bad event received.
|
|
|
|
break readloop
|
|
|
|
}
|
2020-05-12 19:38:29 +00:00
|
|
|
if event != response.MissedEventID {
|
2021-10-28 11:10:18 +00:00
|
|
|
err = json.Unmarshal(rr.RawParams[0].RawMessage, val)
|
2020-05-12 19:38:29 +00:00
|
|
|
if err != nil {
|
|
|
|
// Bad event received.
|
|
|
|
break
|
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
c.Notifications <- Notification{event, val}
|
2020-04-29 19:51:43 +00:00
|
|
|
} else if rr.RawID != nil && (rr.Error != nil || rr.Result != nil) {
|
|
|
|
resp := new(response.Raw)
|
|
|
|
resp.ID = rr.RawID
|
|
|
|
resp.JSONRPC = rr.JSONRPC
|
|
|
|
resp.Error = rr.Error
|
|
|
|
resp.Result = rr.Result
|
2022-02-18 17:28:13 +00:00
|
|
|
id, err := strconv.Atoi(string(resp.ID))
|
|
|
|
if err != nil {
|
|
|
|
break // Malformed response (invalid response ID).
|
|
|
|
}
|
|
|
|
ch := c.getResponseChannel(uint64(id))
|
|
|
|
if ch == nil {
|
|
|
|
break // Unknown response (unexpected response ID).
|
|
|
|
}
|
|
|
|
ch <- resp
|
2020-04-29 19:51:43 +00:00
|
|
|
} else {
|
|
|
|
// Malformed response, neither valid request, nor valid response.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
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 {
|
|
|
|
return
|
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
if err := c.ws.WriteJSON(req); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-pingTicker.C:
|
2021-05-12 18:45:32 +00:00
|
|
|
if err := c.ws.SetWriteDeadline(time.Now().Add(wsWriteLimit)); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
if err := c.ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *WSClient) getResponseChannel(id uint64) chan *response.Raw {
|
|
|
|
c.respLock.RLock()
|
|
|
|
defer c.respLock.RUnlock()
|
|
|
|
return c.respChannels[id]
|
|
|
|
}
|
|
|
|
|
2020-04-29 19:51:43 +00:00
|
|
|
func (c *WSClient) makeWsRequest(r *request.Raw) (*response.Raw, error) {
|
2022-02-18 17:28:13 +00:00
|
|
|
ch := make(chan *response.Raw)
|
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
|
|
|
|
|
|
|
func (c *WSClient) performSubscription(params request.RawParams) (string, error) {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
if err := c.performRequest("unsubscribe", request.NewRawParams(id), &resp); err != nil {
|
|
|
|
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) {
|
2020-05-12 08:18:44 +00:00
|
|
|
params := request.NewRawParams("block_added")
|
2020-05-13 10:16:42 +00:00
|
|
|
if primary != nil {
|
|
|
|
params.Values = append(params.Values, request.BlockFilter{Primary: *primary})
|
|
|
|
}
|
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) {
|
2020-05-12 08:18:44 +00:00
|
|
|
params := request.NewRawParams("transaction_added")
|
2020-07-29 16:57:38 +00:00
|
|
|
if sender != nil || signer != nil {
|
|
|
|
params.Values = append(params.Values, request.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) {
|
2020-05-12 08:18:44 +00:00
|
|
|
params := request.NewRawParams("notification_from_execution")
|
2020-08-04 13:24:32 +00:00
|
|
|
if contract != nil || name != nil {
|
|
|
|
params.Values = append(params.Values, request.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) {
|
2020-05-12 08:18:44 +00:00
|
|
|
params := request.NewRawParams("transaction_executed")
|
2020-05-13 10:16:42 +00:00
|
|
|
if state != nil {
|
|
|
|
if *state != "HALT" && *state != "FAULT" {
|
|
|
|
return "", errors.New("bad state parameter")
|
|
|
|
}
|
|
|
|
params.Values = append(params.Values, request.ExecutionFilter{State: *state})
|
|
|
|
}
|
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) {
|
|
|
|
params := request.NewRawParams("notary_request_event")
|
|
|
|
if sender != nil {
|
|
|
|
params.Values = append(params.Values, request.TxFilter{Sender: sender, Signer: mainSigner})
|
|
|
|
}
|
|
|
|
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
|
|
|
|
if err := c.performRequest("unsubscribe", request.NewRawParams(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
|
|
|
|
}
|