2020-07-10 14:17:51 +00:00
|
|
|
package subscriber
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-07-10 14:17:51 +00:00
|
|
|
"sync"
|
|
|
|
|
2021-01-20 17:05:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2022-07-28 16:22:32 +00:00
|
|
|
"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"
|
2020-07-10 14:17:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2022-02-10 16:32:09 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2020-07-10 14:17:51 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Subscriber is an interface of the NotificationEvent listener.
|
|
|
|
Subscriber interface {
|
2022-07-28 16:22:32 +00:00
|
|
|
SubscribeForNotification(...util.Uint160) (<-chan *state.ContainedNotificationEvent, error)
|
2020-07-10 14:17:51 +00:00
|
|
|
UnsubscribeForNotification()
|
2021-01-20 17:08:06 +00:00
|
|
|
BlockNotifications() (<-chan *block.Block, error)
|
2022-07-28 16:22:32 +00:00
|
|
|
SubscribeForNotaryRequests(mainTXSigner util.Uint160) (<-chan *result.NotaryRequestEvent, error)
|
2021-08-09 15:27:04 +00:00
|
|
|
Close()
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
subscriber struct {
|
|
|
|
*sync.RWMutex
|
|
|
|
log *zap.Logger
|
2022-02-10 16:32:09 +00:00
|
|
|
client *client.Client
|
2020-07-10 14:17:51 +00:00
|
|
|
|
2022-07-28 16:22:32 +00:00
|
|
|
notifyChan chan *state.ContainedNotificationEvent
|
2021-01-20 17:05:52 +00:00
|
|
|
|
|
|
|
blockChan chan *block.Block
|
2021-08-09 15:27:04 +00:00
|
|
|
|
2022-07-28 16:22:32 +00:00
|
|
|
notaryChan chan *result.NotaryRequestEvent
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Params is a group of Subscriber constructor parameters.
|
|
|
|
Params struct {
|
2021-09-06 12:52:17 +00:00
|
|
|
Log *zap.Logger
|
|
|
|
StartFromBlock uint32
|
2022-02-10 16:32:09 +00:00
|
|
|
Client *client.Client
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errNilParams = errors.New("chain/subscriber: config was not provided to the constructor")
|
|
|
|
|
|
|
|
errNilLogger = errors.New("chain/subscriber: logger was not provided to the constructor")
|
2022-02-10 16:32:09 +00:00
|
|
|
|
|
|
|
errNilClient = errors.New("chain/subscriber: client was not provided to the constructor")
|
2020-07-10 14:17:51 +00:00
|
|
|
)
|
|
|
|
|
2022-07-28 16:22:32 +00:00
|
|
|
func (s *subscriber) SubscribeForNotification(contracts ...util.Uint160) (<-chan *state.ContainedNotificationEvent, error) {
|
2020-07-10 14:17:51 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2022-02-10 16:32:09 +00:00
|
|
|
notifyIDs := make(map[util.Uint160]struct{}, len(contracts))
|
2020-07-10 14:17:51 +00:00
|
|
|
|
|
|
|
for i := range contracts {
|
|
|
|
// subscribe to contract notifications
|
2022-02-10 16:32:09 +00:00
|
|
|
err := s.client.SubscribeForExecutionNotifications(contracts[i])
|
2020-07-10 14:17:51 +00:00
|
|
|
if err != nil {
|
|
|
|
// if there is some error, undo all subscriptions and return error
|
2022-02-10 16:32:09 +00:00
|
|
|
for hash := range notifyIDs {
|
|
|
|
_ = s.client.UnsubscribeContract(hash)
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// save notification id
|
2022-02-10 16:32:09 +00:00
|
|
|
notifyIDs[contracts[i]] = struct{}{}
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 15:27:04 +00:00
|
|
|
return s.notifyChan, nil
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) UnsubscribeForNotification() {
|
2022-02-10 16:32:09 +00:00
|
|
|
err := s.client.UnsubscribeAll()
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("unsubscribe for notification",
|
|
|
|
zap.Error(err))
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
func (s *subscriber) Close() {
|
|
|
|
s.client.Close()
|
|
|
|
}
|
|
|
|
|
2021-01-20 17:08:06 +00:00
|
|
|
func (s *subscriber) BlockNotifications() (<-chan *block.Block, error) {
|
2022-02-10 16:32:09 +00:00
|
|
|
if err := s.client.SubscribeForNewBlocks(); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not subscribe for new block events: %w", err)
|
2021-01-20 17:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.blockChan, nil
|
2021-01-20 17:05:52 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 16:22:32 +00:00
|
|
|
func (s *subscriber) SubscribeForNotaryRequests(mainTXSigner util.Uint160) (<-chan *result.NotaryRequestEvent, error) {
|
2022-02-10 16:32:09 +00:00
|
|
|
if err := s.client.SubscribeForNotaryRequests(mainTXSigner); err != nil {
|
2021-08-09 15:27:04 +00:00
|
|
|
return nil, fmt.Errorf("could not subscribe for notary request events: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.notaryChan, nil
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:17:51 +00:00
|
|
|
func (s *subscriber) routeNotifications(ctx context.Context) {
|
2022-02-10 16:32:09 +00:00
|
|
|
notificationChan := s.client.NotificationChannel()
|
|
|
|
|
2020-07-10 14:17:51 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2022-02-10 16:32:09 +00:00
|
|
|
case notification, ok := <-notificationChan:
|
2020-10-13 15:36:13 +00:00
|
|
|
if !ok {
|
2021-08-09 15:27:04 +00:00
|
|
|
s.log.Warn("remote notification channel has been closed")
|
|
|
|
close(s.notifyChan)
|
2021-01-20 17:05:52 +00:00
|
|
|
close(s.blockChan)
|
2021-08-09 15:27:04 +00:00
|
|
|
close(s.notaryChan)
|
2020-10-13 15:36:13 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:17:51 +00:00
|
|
|
switch notification.Type {
|
2022-07-28 16:22:32 +00:00
|
|
|
case neorpc.NotificationEventID:
|
|
|
|
notifyEvent, ok := notification.Value.(*state.ContainedNotificationEvent)
|
2020-07-10 14:17:51 +00:00
|
|
|
if !ok {
|
2021-10-14 09:39:21 +00:00
|
|
|
s.log.Error("can't cast notify event value to the notify struct",
|
|
|
|
zap.String("received type", fmt.Sprintf("%T", notification.Value)),
|
|
|
|
)
|
2020-07-10 14:17:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-09-07 10:20:31 +00:00
|
|
|
s.log.Debug("new notification event from sidechain",
|
|
|
|
zap.String("name", notifyEvent.Name),
|
|
|
|
)
|
|
|
|
|
2021-10-21 15:09:26 +00:00
|
|
|
s.notifyChan <- notifyEvent
|
2022-07-28 16:22:32 +00:00
|
|
|
case neorpc.BlockEventID:
|
2021-01-20 17:05:52 +00:00
|
|
|
b, ok := notification.Value.(*block.Block)
|
|
|
|
if !ok {
|
2021-10-14 09:39:21 +00:00
|
|
|
s.log.Error("can't cast block event value to block",
|
|
|
|
zap.String("received type", fmt.Sprintf("%T", notification.Value)),
|
|
|
|
)
|
2021-01-20 17:05:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s.blockChan <- b
|
2022-07-28 16:22:32 +00:00
|
|
|
case neorpc.NotaryRequestEventID:
|
|
|
|
notaryRequest, ok := notification.Value.(*result.NotaryRequestEvent)
|
2021-08-09 15:27:04 +00:00
|
|
|
if !ok {
|
2021-10-14 09:39:21 +00:00
|
|
|
s.log.Error("can't cast notify event value to the notary request struct",
|
|
|
|
zap.String("received type", fmt.Sprintf("%T", notification.Value)),
|
|
|
|
)
|
2021-08-09 15:27:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s.notaryChan <- notaryRequest
|
2020-07-10 14:17:51 +00:00
|
|
|
default:
|
|
|
|
s.log.Debug("unsupported notification from the chain",
|
|
|
|
zap.Uint8("type", uint8(notification.Type)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New is a constructs Neo:Morph event listener and returns Subscriber interface.
|
|
|
|
func New(ctx context.Context, p *Params) (Subscriber, error) {
|
|
|
|
switch {
|
|
|
|
case p == nil:
|
|
|
|
return nil, errNilParams
|
|
|
|
case p.Log == nil:
|
|
|
|
return nil, errNilLogger
|
2022-02-10 16:32:09 +00:00
|
|
|
case p.Client == nil:
|
|
|
|
return nil, errNilClient
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 16:32:09 +00:00
|
|
|
err := awaitHeight(p.Client, p.StartFromBlock)
|
2021-09-06 12:52:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:17:51 +00:00
|
|
|
sub := &subscriber{
|
2021-08-09 15:27:04 +00:00
|
|
|
RWMutex: new(sync.RWMutex),
|
|
|
|
log: p.Log,
|
2022-02-10 16:32:09 +00:00
|
|
|
client: p.Client,
|
2022-07-28 16:22:32 +00:00
|
|
|
notifyChan: make(chan *state.ContainedNotificationEvent),
|
2021-08-09 15:27:04 +00:00
|
|
|
blockChan: make(chan *block.Block),
|
2022-07-28 16:22:32 +00:00
|
|
|
notaryChan: make(chan *result.NotaryRequestEvent),
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Worker listens all events from neo-go websocket and puts them
|
|
|
|
// into corresponding channel. It may be notifications, transactions,
|
|
|
|
// new blocks. For now only notifications.
|
|
|
|
go sub.routeNotifications(ctx)
|
|
|
|
|
|
|
|
return sub, nil
|
|
|
|
}
|
2021-09-06 12:52:17 +00:00
|
|
|
|
|
|
|
// awaitHeight checks if remote client has least expected block height and
|
|
|
|
// returns error if it is not reached that height after timeout duration.
|
|
|
|
// This function is required to avoid connections to unsynced RPC nodes, because
|
|
|
|
// they can produce events from the past that should not be processed by
|
|
|
|
// NeoFS nodes.
|
2022-02-10 16:32:09 +00:00
|
|
|
func awaitHeight(cli *client.Client, startFrom uint32) error {
|
2021-09-06 12:52:17 +00:00
|
|
|
if startFrom == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-10 16:32:09 +00:00
|
|
|
height, err := cli.BlockCount()
|
2021-09-07 10:15:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get block height: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if height < startFrom {
|
|
|
|
return fmt.Errorf("RPC block counter %d didn't reach expected height %d", height, startFrom)
|
2021-09-06 12:52:17 +00:00
|
|
|
}
|
2021-09-07 10:15:56 +00:00
|
|
|
|
|
|
|
return nil
|
2021-09-06 12:52:17 +00:00
|
|
|
}
|