2021-02-09 17:52:10 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2023-02-22 13:04:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2021-02-09 17:52:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Close closes connection to the remote side making
|
|
|
|
// this client instance unusable. Closes notification
|
|
|
|
// channel returned from Client.NotificationChannel(),
|
|
|
|
// Removes all subscription.
|
|
|
|
func (c *Client) Close() {
|
|
|
|
// closing should be done via the channel
|
|
|
|
// to prevent switching to another RPC node
|
|
|
|
// in the notification loop
|
2023-10-27 12:02:14 +00:00
|
|
|
if c.closed.CompareAndSwap(false, true) {
|
|
|
|
close(c.closeChan)
|
|
|
|
}
|
|
|
|
c.wg.Wait()
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
// ReceiveExecutionNotifications performs subscription for notifications
|
|
|
|
// generated during contract execution. Events are sent to the specified channel.
|
2021-02-09 17:52:10 +00:00
|
|
|
//
|
|
|
|
// Returns ErrConnectionLost if client has not been able to establish
|
|
|
|
// connection to any of passed RPC endpoints.
|
2023-05-11 14:05:24 +00:00
|
|
|
func (c *Client) ReceiveExecutionNotifications(contract util.Uint160, ch chan<- *state.ContainedNotificationEvent) (string, error) {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.Lock()
|
|
|
|
defer c.switchLock.Unlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
2023-05-11 14:05:24 +00:00
|
|
|
return "", ErrConnectionLost
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
return c.client.ReceiveExecutionNotifications(&neorpc.NotificationFilter{Contract: &contract}, ch)
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
// ReceiveBlocks performs subscription for new block events. Events are sent
|
|
|
|
// to the specified channel.
|
2021-02-09 17:52:10 +00:00
|
|
|
//
|
|
|
|
// Returns ErrConnectionLost if client has not been able to establish
|
|
|
|
// connection to any of passed RPC endpoints.
|
2023-05-11 14:05:24 +00:00
|
|
|
func (c *Client) ReceiveBlocks(ch chan<- *block.Block) (string, error) {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.Lock()
|
|
|
|
defer c.switchLock.Unlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
2023-05-11 14:05:24 +00:00
|
|
|
return "", ErrConnectionLost
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
return c.client.ReceiveBlocks(nil, ch)
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
// ReceiveNotaryRequests performsn subscription for notary request payloads
|
2021-02-09 17:52:10 +00:00
|
|
|
// addition or removal events to this instance of client. Passed txSigner is
|
|
|
|
// used as filter: subscription is only for the notary requests that must be
|
2023-05-11 14:05:24 +00:00
|
|
|
// signed by txSigner. Events are sent to the specified channel.
|
2021-02-09 17:52:10 +00:00
|
|
|
//
|
|
|
|
// Returns ErrConnectionLost if client has not been able to establish
|
|
|
|
// connection to any of passed RPC endpoints.
|
2023-05-11 14:05:24 +00:00
|
|
|
func (c *Client) ReceiveNotaryRequests(txSigner util.Uint160, ch chan<- *result.NotaryRequestEvent) (string, error) {
|
2021-02-09 17:52:10 +00:00
|
|
|
if c.notary == nil {
|
|
|
|
panic(notaryNotEnabledPanicMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.switchLock.Lock()
|
|
|
|
defer c.switchLock.Unlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
2023-05-11 14:05:24 +00:00
|
|
|
return "", ErrConnectionLost
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
return c.client.ReceiveNotaryRequests(&neorpc.TxFilter{Signer: &txSigner}, ch)
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
// Unsubscribe performs unsubscription for the given subscription ID.
|
2021-02-09 17:52:10 +00:00
|
|
|
//
|
|
|
|
// Returns ErrConnectionLost if client has not been able to establish
|
|
|
|
// connection to any of passed RPC endpoints.
|
2023-05-11 14:05:24 +00:00
|
|
|
func (c *Client) Unsubscribe(subID string) error {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.Lock()
|
|
|
|
defer c.switchLock.Unlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
|
|
|
return ErrConnectionLost
|
|
|
|
}
|
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
return c.client.Unsubscribe(subID)
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnsubscribeAll removes all active subscriptions of current client.
|
|
|
|
//
|
|
|
|
// Returns ErrConnectionLost if client has not been able to establish
|
|
|
|
// connection to any of passed RPC endpoints.
|
|
|
|
func (c *Client) UnsubscribeAll() error {
|
|
|
|
c.switchLock.Lock()
|
|
|
|
defer c.switchLock.Unlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
|
|
|
return ErrConnectionLost
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.client.UnsubscribeAll()
|
2023-08-14 11:06:17 +00:00
|
|
|
return err
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|