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"
|
2022-10-17 10:31:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/rpcevent"
|
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
|
2023-01-17 14:10:10 +00:00
|
|
|
// be closed, so make sure to handle this. Make sure you're not changing the
|
|
|
|
// received notifications, as it may affect the functionality of other
|
|
|
|
// notification receivers.
|
2022-10-25 12:11:24 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please, use custom channels with ReceiveBlocks, ReceiveTransactions,
|
|
|
|
// ReceiveExecutionNotifications, ReceiveExecutions, ReceiveNotaryRequests
|
|
|
|
// methods to subscribe for notifications. This field will be removed in future
|
|
|
|
// versions.
|
2020-05-12 08:18:44 +00:00
|
|
|
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
|
2022-10-17 10:31:24 +00:00
|
|
|
subscriptions map[string]notificationReceiver
|
2022-10-25 12:11:24 +00:00
|
|
|
// receivers is a mapping from receiver channel to a set of corresponding subscription IDs.
|
|
|
|
// It must be accessed with subscriptionsLock taken. Its keys must be used to deliver
|
|
|
|
// notifications, if channel is not in the receivers list and corresponding subscription
|
|
|
|
// still exists, notification must not be sent.
|
|
|
|
receivers map[interface{}][]string
|
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-10-25 12:11:24 +00:00
|
|
|
// notificationReceiver is an interface aimed to provide WS subscriber functionality
|
|
|
|
// for different types of subscriptions.
|
|
|
|
type notificationReceiver interface {
|
|
|
|
// Comparator provides notification filtering functionality.
|
|
|
|
rpcevent.Comparator
|
|
|
|
// Receiver returns notification receiver channel.
|
|
|
|
Receiver() interface{}
|
|
|
|
// TrySend checks whether notification passes receiver filter and sends it
|
|
|
|
// to the underlying channel if so.
|
|
|
|
TrySend(ntf Notification) bool
|
|
|
|
// Close closes underlying receiver channel.
|
|
|
|
Close()
|
2022-10-17 10:31:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// blockReceiver stores information about block events subscriber.
|
|
|
|
type blockReceiver struct {
|
|
|
|
filter *neorpc.BlockFilter
|
|
|
|
ch chan<- *block.Block
|
2022-10-17 10:31:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// EventID implements neorpc.Comparator interface.
|
|
|
|
func (r *blockReceiver) EventID() neorpc.EventID {
|
|
|
|
return neorpc.BlockEventID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter implements neorpc.Comparator interface.
|
|
|
|
func (r *blockReceiver) Filter() interface{} {
|
|
|
|
if r.filter == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return *r.filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receiver implements notificationReceiver interface.
|
|
|
|
func (r *blockReceiver) Receiver() interface{} {
|
|
|
|
return r.ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrySend implements notificationReceiver interface.
|
|
|
|
func (r *blockReceiver) TrySend(ntf Notification) bool {
|
|
|
|
if rpcevent.Matches(r, ntf) {
|
|
|
|
r.ch <- ntf.Value.(*block.Block)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements notificationReceiver interface.
|
|
|
|
func (r *blockReceiver) Close() {
|
|
|
|
close(r.ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// txReceiver stores information about transaction events subscriber.
|
|
|
|
type txReceiver struct {
|
|
|
|
filter *neorpc.TxFilter
|
|
|
|
ch chan<- *transaction.Transaction
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventID implements neorpc.Comparator interface.
|
|
|
|
func (r *txReceiver) EventID() neorpc.EventID {
|
|
|
|
return neorpc.TransactionEventID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter implements neorpc.Comparator interface.
|
|
|
|
func (r *txReceiver) Filter() interface{} {
|
|
|
|
if r.filter == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return *r.filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receiver implements notificationReceiver interface.
|
|
|
|
func (r *txReceiver) Receiver() interface{} {
|
|
|
|
return r.ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrySend implements notificationReceiver interface.
|
|
|
|
func (r *txReceiver) TrySend(ntf Notification) bool {
|
|
|
|
if rpcevent.Matches(r, ntf) {
|
|
|
|
r.ch <- ntf.Value.(*transaction.Transaction)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements notificationReceiver interface.
|
|
|
|
func (r *txReceiver) Close() {
|
|
|
|
close(r.ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// executionNotificationReceiver stores information about execution notifications subscriber.
|
|
|
|
type executionNotificationReceiver struct {
|
|
|
|
filter *neorpc.NotificationFilter
|
|
|
|
ch chan<- *state.ContainedNotificationEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventID implements neorpc.Comparator interface.
|
|
|
|
func (r *executionNotificationReceiver) EventID() neorpc.EventID {
|
|
|
|
return neorpc.NotificationEventID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter implements neorpc.Comparator interface.
|
|
|
|
func (r *executionNotificationReceiver) Filter() interface{} {
|
|
|
|
if r.filter == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return *r.filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receiver implements notificationReceiver interface.
|
|
|
|
func (r *executionNotificationReceiver) Receiver() interface{} {
|
|
|
|
return r.ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrySend implements notificationReceiver interface.
|
|
|
|
func (r *executionNotificationReceiver) TrySend(ntf Notification) bool {
|
|
|
|
if rpcevent.Matches(r, ntf) {
|
|
|
|
r.ch <- ntf.Value.(*state.ContainedNotificationEvent)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements notificationReceiver interface.
|
|
|
|
func (r *executionNotificationReceiver) Close() {
|
|
|
|
close(r.ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// executionReceiver stores information about application execution results subscriber.
|
|
|
|
type executionReceiver struct {
|
|
|
|
filter *neorpc.ExecutionFilter
|
|
|
|
ch chan<- *state.AppExecResult
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventID implements neorpc.Comparator interface.
|
|
|
|
func (r *executionReceiver) EventID() neorpc.EventID {
|
|
|
|
return neorpc.ExecutionEventID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter implements neorpc.Comparator interface.
|
|
|
|
func (r *executionReceiver) Filter() interface{} {
|
|
|
|
if r.filter == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return *r.filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receiver implements notificationReceiver interface.
|
|
|
|
func (r *executionReceiver) Receiver() interface{} {
|
|
|
|
return r.ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrySend implements notificationReceiver interface.
|
|
|
|
func (r *executionReceiver) TrySend(ntf Notification) bool {
|
|
|
|
if rpcevent.Matches(r, ntf) {
|
|
|
|
r.ch <- ntf.Value.(*state.AppExecResult)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements notificationReceiver interface.
|
|
|
|
func (r *executionReceiver) Close() {
|
|
|
|
close(r.ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// notaryRequestReceiver stores information about notary requests subscriber.
|
|
|
|
type notaryRequestReceiver struct {
|
|
|
|
filter *neorpc.TxFilter
|
|
|
|
ch chan<- *result.NotaryRequestEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventID implements neorpc.Comparator interface.
|
|
|
|
func (r *notaryRequestReceiver) EventID() neorpc.EventID {
|
|
|
|
return neorpc.NotaryRequestEventID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter implements neorpc.Comparator interface.
|
|
|
|
func (r *notaryRequestReceiver) Filter() interface{} {
|
|
|
|
if r.filter == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return *r.filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receiver implements notificationReceiver interface.
|
|
|
|
func (r *notaryRequestReceiver) Receiver() interface{} {
|
|
|
|
return r.ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrySend implements notificationReceiver interface.
|
|
|
|
func (r *notaryRequestReceiver) TrySend(ntf Notification) bool {
|
|
|
|
if rpcevent.Matches(r, ntf) {
|
|
|
|
r.ch <- ntf.Value.(*result.NotaryRequestEvent)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements notificationReceiver interface.
|
|
|
|
func (r *notaryRequestReceiver) Close() {
|
|
|
|
close(r.ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// naiveReceiver is a structure leaved for deprecated single channel based notifications
|
|
|
|
// delivering.
|
|
|
|
//
|
|
|
|
// Deprecated: this receiver must be removed after outdated subscriptions API removal.
|
|
|
|
type naiveReceiver struct {
|
|
|
|
eventID neorpc.EventID
|
|
|
|
filter interface{}
|
|
|
|
ch chan<- Notification
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventID implements neorpc.Comparator interface.
|
|
|
|
func (r *naiveReceiver) EventID() neorpc.EventID {
|
|
|
|
return r.eventID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter implements neorpc.Comparator interface.
|
|
|
|
func (r *naiveReceiver) Filter() interface{} {
|
2022-10-17 10:31:24 +00:00
|
|
|
return r.filter
|
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// Receiver implements notificationReceiver interface.
|
|
|
|
func (r *naiveReceiver) Receiver() interface{} {
|
|
|
|
return r.ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrySend implements notificationReceiver interface.
|
|
|
|
func (r *naiveReceiver) TrySend(ntf Notification) bool {
|
|
|
|
if rpcevent.Matches(r, ntf) {
|
|
|
|
r.ch <- ntf
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements notificationReceiver interface.
|
|
|
|
func (r *naiveReceiver) Close() {
|
|
|
|
r.ch <- Notification{
|
|
|
|
Type: neorpc.MissedEventID, // backwards-compatible behaviour
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Notification represents a server-generated notification for client subscriptions.
|
2022-10-17 06:12:33 +00:00
|
|
|
// Value can be one of *block.Block, *state.AppExecResult, *state.ContainedNotificationEvent
|
|
|
|
// *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
|
|
|
}
|
|
|
|
|
2022-10-17 10:31:24 +00:00
|
|
|
// EventID implements Container interface and returns notification ID.
|
|
|
|
func (n Notification) EventID() neorpc.EventID {
|
|
|
|
return n.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventPayload implements Container interface and returns notification
|
|
|
|
// object.
|
|
|
|
func (n Notification) EventPayload() interface{} {
|
|
|
|
return n.Value
|
|
|
|
}
|
|
|
|
|
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-10-25 12:11:24 +00:00
|
|
|
// ErrNilNotificationReceiver is returned when notification receiver channel is nil.
|
|
|
|
var ErrNilNotificationReceiver = errors.New("nil notification receiver")
|
|
|
|
|
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}
|
2022-09-02 09:25:51 +00:00
|
|
|
ws, resp, err := dialer.DialContext(ctx, endpoint, nil)
|
2022-09-02 09:21:24 +00:00
|
|
|
if resp != nil && resp.Body != nil { // Can be non-nil even with error returned.
|
|
|
|
defer resp.Body.Close() // Not exactly required by websocket, but let's do this for bodyclose checker.
|
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
if err != nil {
|
2022-11-23 08:25:05 +00:00
|
|
|
if resp != nil && resp.Body != nil {
|
|
|
|
var srvErr neorpc.HeaderAndError
|
|
|
|
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
decErr := dec.Decode(&srvErr)
|
|
|
|
if decErr == nil && srvErr.Error != nil {
|
|
|
|
err = srvErr.Error
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
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),
|
2022-10-17 10:31:24 +00:00
|
|
|
subscriptions: make(map[string]notificationReceiver),
|
2022-10-25 12:11:24 +00:00
|
|
|
receivers: make(map[interface{}][]string),
|
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)
|
2022-10-12 12:23:32 +00:00
|
|
|
// Call to cancel will send signal to all users of Context().
|
|
|
|
c.Client.ctxCancel()
|
2022-04-05 08:28:26 +00:00
|
|
|
}
|
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
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
if event == neorpc.MissedEventID {
|
|
|
|
c.subscriptionsLock.Lock()
|
|
|
|
for rcvr, ids := range c.receivers {
|
|
|
|
c.subscriptions[ids[0]].Close()
|
|
|
|
delete(c.receivers, rcvr)
|
|
|
|
}
|
|
|
|
c.subscriptionsLock.Unlock()
|
|
|
|
continue readloop
|
|
|
|
}
|
2022-10-17 10:31:24 +00:00
|
|
|
c.subscriptionsLock.RLock()
|
2022-10-25 12:11:24 +00:00
|
|
|
ntf := Notification{Type: event, Value: val}
|
|
|
|
for _, ids := range c.receivers {
|
|
|
|
for _, id := range ids {
|
|
|
|
if c.subscriptions[id].TrySend(ntf) {
|
|
|
|
break // strictly one notification per channel
|
|
|
|
}
|
2022-10-17 10:31:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c.subscriptionsLock.RUnlock()
|
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)
|
2022-10-12 12:23:32 +00:00
|
|
|
c.Client.ctxCancel()
|
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-10-17 10:31:24 +00:00
|
|
|
func (c *WSClient) performSubscription(params []interface{}, rcvr notificationReceiver) (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()
|
|
|
|
|
2022-10-17 10:31:24 +00:00
|
|
|
c.subscriptions[resp] = rcvr
|
2022-10-25 12:11:24 +00:00
|
|
|
ch := rcvr.Receiver()
|
|
|
|
c.receivers[ch] = append(c.receivers[ch], resp)
|
2020-05-12 08:18:44 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeForNewBlocks adds subscription for new block events to this instance
|
2022-10-26 10:22:12 +00:00
|
|
|
// of the client. It can be filtered by primary consensus node index, nil value doesn't
|
|
|
|
// add any filters.
|
2022-10-19 09:46:53 +00:00
|
|
|
//
|
2022-10-25 12:11:24 +00:00
|
|
|
// Deprecated: please, use ReceiveBlocks. This method will be removed in future versions.
|
2022-10-26 10:22:12 +00:00
|
|
|
func (c *WSClient) SubscribeForNewBlocks(primary *int) (string, error) {
|
2022-12-07 12:07:42 +00:00
|
|
|
var flt interface{}
|
2022-10-26 10:22:12 +00:00
|
|
|
if primary != nil {
|
2022-12-07 12:07:42 +00:00
|
|
|
flt = neorpc.BlockFilter{Primary: primary}
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
params := []interface{}{"block_added"}
|
|
|
|
if flt != nil {
|
2022-12-07 12:07:42 +00:00
|
|
|
params = append(params, flt)
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
r := &naiveReceiver{
|
|
|
|
eventID: neorpc.BlockEventID,
|
|
|
|
filter: flt,
|
|
|
|
ch: c.Notifications,
|
|
|
|
}
|
|
|
|
return c.performSubscription(params, r)
|
2022-10-17 10:31:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// ReceiveBlocks registers provided channel as a receiver for the new block events.
|
|
|
|
// Events can be filtered by the given BlockFilter, nil value doesn't add any filter.
|
|
|
|
// The receiver channel must be properly read and drained after usage in order not
|
2023-01-17 14:10:10 +00:00
|
|
|
// to block other notification receivers. Make sure you're not changing the received
|
|
|
|
// blocks, as it may affect the functionality of other notification receivers.
|
|
|
|
// If multiple subscriptions share the same receiver channel, then matching
|
|
|
|
// notification is only sent once per channel. The receiver channel will be closed
|
|
|
|
// by the WSClient immediately after MissedEvent is received from the server;
|
|
|
|
// no unsubscription is performed in this case, so it's the user responsibility
|
|
|
|
// to unsubscribe.
|
2022-10-25 12:11:24 +00:00
|
|
|
func (c *WSClient) ReceiveBlocks(flt *neorpc.BlockFilter, rcvr chan<- *block.Block) (string, error) {
|
|
|
|
if rcvr == nil {
|
|
|
|
return "", ErrNilNotificationReceiver
|
2022-10-19 09:46:53 +00:00
|
|
|
}
|
2022-07-07 17:03:10 +00:00
|
|
|
params := []interface{}{"block_added"}
|
2022-10-25 12:11:24 +00:00
|
|
|
if flt != nil {
|
|
|
|
params = append(params, *flt)
|
2020-05-13 10:16:42 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
r := &blockReceiver{
|
2022-10-17 10:31:24 +00:00
|
|
|
filter: flt,
|
2022-10-25 12:11:24 +00:00
|
|
|
ch: rcvr,
|
2022-10-17 10:31:24 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
return c.performSubscription(params, r)
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2022-10-19 09:46:53 +00:00
|
|
|
//
|
2022-10-25 12:11:24 +00:00
|
|
|
// Deprecated: please, use ReceiveTransactions. This method will be removed in future versions.
|
2020-07-29 16:57:38 +00:00
|
|
|
func (c *WSClient) SubscribeForNewTransactions(sender *util.Uint160, signer *util.Uint160) (string, error) {
|
2022-12-07 12:07:42 +00:00
|
|
|
var flt interface{}
|
2022-10-25 12:11:24 +00:00
|
|
|
if sender != nil || signer != nil {
|
2022-12-07 12:07:42 +00:00
|
|
|
flt = neorpc.TxFilter{Sender: sender, Signer: signer}
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
params := []interface{}{"transaction_added"}
|
|
|
|
if flt != nil {
|
2022-12-07 12:07:42 +00:00
|
|
|
params = append(params, flt)
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
r := &naiveReceiver{
|
|
|
|
eventID: neorpc.TransactionEventID,
|
|
|
|
filter: flt,
|
|
|
|
ch: c.Notifications,
|
|
|
|
}
|
|
|
|
return c.performSubscription(params, r)
|
2022-10-17 10:31:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// ReceiveTransactions registers provided channel as a receiver for new transaction
|
|
|
|
// events. Events can be filtered by the given TxFilter, nil value doesn't add any
|
|
|
|
// filter. The receiver channel must be properly read and drained after usage in
|
2023-01-17 14:10:10 +00:00
|
|
|
// order not to block other notification receivers. Make sure you're not changing
|
|
|
|
// the received transactions, as it may affect the functionality of other
|
|
|
|
// notification receivers.If multiple subscriptions share the same receiver channel,
|
|
|
|
// then matching notification is only sent once per channel. The receiver channel
|
|
|
|
// will be closed by the WSClient immediately after MissedEvent is received from
|
|
|
|
// the server; no unsubscription is performed in this case, so it's the user
|
|
|
|
// responsibility to unsubscribe.
|
2022-10-25 12:11:24 +00:00
|
|
|
func (c *WSClient) ReceiveTransactions(flt *neorpc.TxFilter, rcvr chan<- *transaction.Transaction) (string, error) {
|
|
|
|
if rcvr == nil {
|
|
|
|
return "", ErrNilNotificationReceiver
|
2022-10-19 09:46:53 +00:00
|
|
|
}
|
2022-07-07 17:03:10 +00:00
|
|
|
params := []interface{}{"transaction_added"}
|
2022-10-25 12:11:24 +00:00
|
|
|
if flt != nil {
|
2022-10-17 10:31:24 +00:00
|
|
|
params = append(params, *flt)
|
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
r := &txReceiver{
|
2022-10-17 10:31:24 +00:00
|
|
|
filter: flt,
|
2022-10-25 12:11:24 +00:00
|
|
|
ch: rcvr,
|
2020-05-13 10:16:42 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
return c.performSubscription(params, r)
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2022-10-19 09:46:53 +00:00
|
|
|
//
|
2022-10-25 12:11:24 +00:00
|
|
|
// Deprecated: please, use ReceiveExecutionNotifications. This method will be removed in future versions.
|
2020-08-04 13:24:32 +00:00
|
|
|
func (c *WSClient) SubscribeForExecutionNotifications(contract *util.Uint160, name *string) (string, error) {
|
2022-12-07 12:07:42 +00:00
|
|
|
var flt interface{}
|
2022-10-25 12:11:24 +00:00
|
|
|
if contract != nil || name != nil {
|
2022-12-07 12:07:42 +00:00
|
|
|
flt = neorpc.NotificationFilter{Contract: contract, Name: name}
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
params := []interface{}{"notification_from_execution"}
|
|
|
|
if flt != nil {
|
2022-12-07 12:07:42 +00:00
|
|
|
params = append(params, flt)
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
r := &naiveReceiver{
|
|
|
|
eventID: neorpc.NotificationEventID,
|
|
|
|
filter: flt,
|
|
|
|
ch: c.Notifications,
|
|
|
|
}
|
|
|
|
return c.performSubscription(params, r)
|
2022-10-17 10:31:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// ReceiveExecutionNotifications registers provided channel as a receiver for execution
|
|
|
|
// events. Events can be filtered by the given NotificationFilter, nil value doesn't add
|
|
|
|
// any filter. The receiver channel must be properly read and drained after usage in
|
2023-01-17 14:10:10 +00:00
|
|
|
// order not to block other notification receivers. Make sure you're not changing the
|
|
|
|
// received notification events, as it may affect the functionality of other
|
|
|
|
// notification receivers. If multiple subscriptions share the same receiver channel,
|
|
|
|
// then matching notification is only sent once per channel. The receiver channel will
|
|
|
|
// be closed by the WSClient immediately after MissedEvent is received from the server;
|
|
|
|
// no unsubscription is performed in this case, so it's the user responsibility to
|
|
|
|
// unsubscribe.
|
2022-10-25 12:11:24 +00:00
|
|
|
func (c *WSClient) ReceiveExecutionNotifications(flt *neorpc.NotificationFilter, rcvr chan<- *state.ContainedNotificationEvent) (string, error) {
|
|
|
|
if rcvr == nil {
|
|
|
|
return "", ErrNilNotificationReceiver
|
2022-10-19 09:46:53 +00:00
|
|
|
}
|
2022-07-07 17:03:10 +00:00
|
|
|
params := []interface{}{"notification_from_execution"}
|
2022-10-25 12:11:24 +00:00
|
|
|
if flt != nil {
|
2022-10-17 10:31:24 +00:00
|
|
|
params = append(params, *flt)
|
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
r := &executionNotificationReceiver{
|
2022-10-17 10:31:24 +00:00
|
|
|
filter: flt,
|
2022-10-25 12:11:24 +00:00
|
|
|
ch: rcvr,
|
2020-05-13 10:16:42 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
return c.performSubscription(params, r)
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2022-10-26 10:22:12 +00:00
|
|
|
// transactions, nil value means no filtering.
|
2022-10-19 09:46:53 +00:00
|
|
|
//
|
2022-10-25 12:11:24 +00:00
|
|
|
// Deprecated: please, use ReceiveExecutions. This method will be removed in future versions.
|
2022-10-26 10:22:12 +00:00
|
|
|
func (c *WSClient) SubscribeForTransactionExecutions(state *string) (string, error) {
|
2022-12-07 12:07:42 +00:00
|
|
|
var flt interface{}
|
2022-10-26 10:22:12 +00:00
|
|
|
if state != nil {
|
2022-12-07 12:07:42 +00:00
|
|
|
if *state != "HALT" && *state != "FAULT" {
|
|
|
|
return "", errors.New("bad state parameter")
|
|
|
|
}
|
|
|
|
flt = neorpc.ExecutionFilter{State: state}
|
2022-10-19 09:46:53 +00:00
|
|
|
}
|
2022-07-07 17:03:10 +00:00
|
|
|
params := []interface{}{"transaction_executed"}
|
2022-10-25 12:11:24 +00:00
|
|
|
if flt != nil {
|
2022-12-07 12:07:42 +00:00
|
|
|
params = append(params, flt)
|
2022-10-17 10:31:24 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
r := &naiveReceiver{
|
|
|
|
eventID: neorpc.ExecutionEventID,
|
|
|
|
filter: flt,
|
|
|
|
ch: c.Notifications,
|
|
|
|
}
|
|
|
|
return c.performSubscription(params, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReceiveExecutions registers provided channel as a receiver for
|
|
|
|
// application execution result events generated during transaction execution.
|
|
|
|
// Events can be filtered by the given ExecutionFilter, nil value doesn't add any filter.
|
|
|
|
// The receiver channel must be properly read and drained after usage in order not
|
2023-01-17 14:10:10 +00:00
|
|
|
// to block other notification receivers. Make sure you're not changing the received
|
|
|
|
// execution results, as it may affect the functionality of other notification
|
|
|
|
// receivers. If multiple subscriptions share the same receiver channel, then
|
|
|
|
// matching notification is only sent once per channel. The receiver channel will
|
|
|
|
// be closed by the WSClient immediately after MissedEvent is received from the
|
|
|
|
// server; no unsubscription is performed in this case, so it's the user responsibility
|
|
|
|
// to unsubscribe.
|
2022-10-25 12:11:24 +00:00
|
|
|
func (c *WSClient) ReceiveExecutions(flt *neorpc.ExecutionFilter, rcvr chan<- *state.AppExecResult) (string, error) {
|
|
|
|
if rcvr == nil {
|
|
|
|
return "", ErrNilNotificationReceiver
|
|
|
|
}
|
|
|
|
params := []interface{}{"transaction_executed"}
|
|
|
|
if flt != nil {
|
|
|
|
if flt.State != nil {
|
|
|
|
if *flt.State != "HALT" && *flt.State != "FAULT" {
|
|
|
|
return "", errors.New("bad state parameter")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
params = append(params, *flt)
|
|
|
|
}
|
|
|
|
r := &executionReceiver{
|
2022-10-17 10:31:24 +00:00
|
|
|
filter: flt,
|
2022-10-25 12:11:24 +00:00
|
|
|
ch: rcvr,
|
2020-05-13 10:16:42 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
return c.performSubscription(params, r)
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2022-10-19 09:46:53 +00:00
|
|
|
//
|
2022-10-25 12:11:24 +00:00
|
|
|
// Deprecated: please, use ReceiveNotaryRequests. This method will be removed in future versions.
|
2021-05-28 11:55:06 +00:00
|
|
|
func (c *WSClient) SubscribeForNotaryRequests(sender *util.Uint160, mainSigner *util.Uint160) (string, error) {
|
2022-12-07 12:07:42 +00:00
|
|
|
var flt interface{}
|
2022-10-25 12:11:24 +00:00
|
|
|
if sender != nil || mainSigner != nil {
|
2022-12-07 12:07:42 +00:00
|
|
|
flt = neorpc.TxFilter{Sender: sender, Signer: mainSigner}
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
params := []interface{}{"notary_request_event"}
|
|
|
|
if flt != nil {
|
2022-12-07 12:07:42 +00:00
|
|
|
params = append(params, flt)
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
r := &naiveReceiver{
|
|
|
|
eventID: neorpc.NotaryRequestEventID,
|
|
|
|
filter: flt,
|
|
|
|
ch: c.Notifications,
|
|
|
|
}
|
|
|
|
return c.performSubscription(params, r)
|
2022-10-17 10:31:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:11:24 +00:00
|
|
|
// ReceiveNotaryRequests registers provided channel as a receiver for notary request
|
|
|
|
// payload addition or removal events. Events can be filtered by the given TxFilter
|
|
|
|
// where sender corresponds to notary request sender (the second fallback transaction
|
|
|
|
// signer) and signer corresponds to main transaction signers. nil value doesn't add
|
|
|
|
// any filter. The receiver channel must be properly read and drained after usage in
|
2023-01-17 14:10:10 +00:00
|
|
|
// order not to block other notification receivers. Make sure you're not changing the
|
|
|
|
// received notary requests, as it may affect the functionality of other notification
|
|
|
|
// receivers. If multiple subscriptions share the same receiver channel, then matching
|
|
|
|
// notification is only sent once per channel. The receiver channel will be closed by
|
|
|
|
// the WSClient immediately after MissedEvent is received from the server; no
|
|
|
|
// unsubscription is performed in this case, so it's the user responsibility to
|
|
|
|
// unsubscribe.
|
2022-10-25 12:11:24 +00:00
|
|
|
func (c *WSClient) ReceiveNotaryRequests(flt *neorpc.TxFilter, rcvr chan<- *result.NotaryRequestEvent) (string, error) {
|
|
|
|
if rcvr == nil {
|
|
|
|
return "", ErrNilNotificationReceiver
|
2022-10-19 09:46:53 +00:00
|
|
|
}
|
2022-07-07 17:03:10 +00:00
|
|
|
params := []interface{}{"notary_request_event"}
|
2022-10-25 12:11:24 +00:00
|
|
|
if flt != nil {
|
2022-10-17 10:31:24 +00:00
|
|
|
params = append(params, *flt)
|
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
r := ¬aryRequestReceiver{
|
2022-10-17 10:31:24 +00:00
|
|
|
filter: flt,
|
2022-10-25 12:11:24 +00:00
|
|
|
ch: rcvr,
|
2021-05-28 11:55:06 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
return c.performSubscription(params, r)
|
2021-05-28 11:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 09:35:26 +00:00
|
|
|
// Unsubscribe removes subscription for the given event stream. It will return an
|
|
|
|
// error in case if there's no subscription with the provided ID. Call to Unsubscribe
|
|
|
|
// doesn't block notifications receive process for given subscriber, thus, ensure
|
|
|
|
// that subscriber channel is properly drained while unsubscription is being
|
|
|
|
// performed. You may probably need to run unsubscription process in a separate
|
|
|
|
// routine (in parallel with notification receiver routine) to avoid Client's
|
|
|
|
// notification dispatcher blocking.
|
2020-05-12 08:18:44 +00:00
|
|
|
func (c *WSClient) Unsubscribe(id string) error {
|
|
|
|
return c.performUnsubscription(id)
|
|
|
|
}
|
|
|
|
|
2022-11-16 09:35:26 +00:00
|
|
|
// UnsubscribeAll removes all active subscriptions of the current client. It copies
|
|
|
|
// the list of subscribers in order not to hold the lock for the whole execution
|
|
|
|
// time and tries to unsubscribe from us many feeds as possible returning the
|
|
|
|
// chunk of unsubscription errors afterwards. Call to UnsubscribeAll doesn't block
|
|
|
|
// notifications receive process for given subscribers, thus, ensure that subscribers
|
|
|
|
// channels are properly drained while unsubscription is being performed. You may
|
|
|
|
// probably need to run unsubscription process in a separate routine (in parallel
|
|
|
|
// with notification receiver routines) to avoid Client's notification dispatcher
|
|
|
|
// blocking.
|
2020-05-12 08:18:44 +00:00
|
|
|
func (c *WSClient) UnsubscribeAll() error {
|
2022-02-21 13:40:50 +00:00
|
|
|
c.subscriptionsLock.Lock()
|
2022-11-16 09:35:26 +00:00
|
|
|
subs := make([]string, 0, len(c.subscriptions))
|
2020-05-12 08:18:44 +00:00
|
|
|
for id := range c.subscriptions {
|
2022-11-16 09:35:26 +00:00
|
|
|
subs = append(subs, id)
|
|
|
|
}
|
|
|
|
c.subscriptionsLock.Unlock()
|
|
|
|
|
|
|
|
var resErr error
|
|
|
|
for _, id := range subs {
|
|
|
|
err := c.performUnsubscription(id)
|
2022-10-25 12:11:24 +00:00
|
|
|
if err != nil {
|
2022-11-16 09:35:26 +00:00
|
|
|
errFmt := "failed to unsubscribe from feed %d: %v"
|
|
|
|
errArgs := []interface{}{err}
|
|
|
|
if resErr != nil {
|
|
|
|
errFmt = "%w; " + errFmt
|
|
|
|
errArgs = append([]interface{}{resErr}, errArgs...)
|
|
|
|
}
|
|
|
|
resErr = fmt.Errorf(errFmt, errArgs...)
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
2022-11-16 09:35:26 +00:00
|
|
|
return resErr
|
2022-10-25 12:11:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 09:35:26 +00:00
|
|
|
// performUnsubscription is internal method that removes subscription with the given
|
|
|
|
// ID from the list of subscriptions and receivers. It takes the subscriptions lock
|
|
|
|
// after WS RPC unsubscription request is completed. Until then the subscriber channel
|
|
|
|
// may still receive WS notifications.
|
|
|
|
func (c *WSClient) performUnsubscription(id string) error {
|
2022-10-25 12:11:24 +00:00
|
|
|
var resp bool
|
|
|
|
if err := c.performRequest("unsubscribe", []interface{}{id}, &resp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !resp {
|
|
|
|
return errors.New("unsubscribe method returned false result")
|
|
|
|
}
|
2022-11-16 09:35:26 +00:00
|
|
|
|
|
|
|
c.subscriptionsLock.Lock()
|
|
|
|
defer c.subscriptionsLock.Unlock()
|
|
|
|
|
|
|
|
rcvr, ok := c.subscriptions[id]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no subscription with this ID")
|
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
ch := rcvr.Receiver()
|
|
|
|
ids := c.receivers[ch]
|
|
|
|
for i, rcvrID := range ids {
|
|
|
|
if rcvrID == id {
|
|
|
|
ids = append(ids[:i], ids[i+1:]...)
|
|
|
|
break
|
2022-02-21 13:40:50 +00:00
|
|
|
}
|
2020-05-12 08:18:44 +00:00
|
|
|
}
|
2022-10-25 12:11:24 +00:00
|
|
|
if len(ids) == 0 {
|
|
|
|
delete(c.receivers, ch)
|
|
|
|
} else {
|
|
|
|
c.receivers[ch] = ids
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2022-10-12 12:23:32 +00:00
|
|
|
|
|
|
|
// Context returns WSClient Cancel context that will be terminated on Client shutdown.
|
|
|
|
func (c *WSClient) Context() context.Context {
|
|
|
|
return c.Client.ctx
|
|
|
|
}
|