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"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
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/result"
|
2020-07-10 14:17:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2023-02-27 14:17:55 +00:00
|
|
|
NotificationChannels struct {
|
|
|
|
BlockCh <-chan *block.Block
|
|
|
|
NotificationsCh <-chan *state.ContainedNotificationEvent
|
|
|
|
NotaryRequestsCh <-chan *result.NotaryRequestEvent
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:17:51 +00:00
|
|
|
// Subscriber is an interface of the NotificationEvent listener.
|
|
|
|
Subscriber interface {
|
2023-02-27 14:17:55 +00:00
|
|
|
SubscribeForNotification(...util.Uint160) error
|
|
|
|
BlockNotifications() error
|
|
|
|
SubscribeForNotaryRequests(mainTXSigner util.Uint160) error
|
|
|
|
|
|
|
|
NotificationChannels() NotificationChannels
|
|
|
|
|
2021-08-09 15:27:04 +00:00
|
|
|
Close()
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
subChannels struct {
|
|
|
|
NotifyChan chan *state.ContainedNotificationEvent
|
|
|
|
BlockChan chan *block.Block
|
|
|
|
NotaryChan chan *result.NotaryRequestEvent
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:17:51 +00:00
|
|
|
subscriber struct {
|
2023-05-30 07:14:37 +00:00
|
|
|
sync.RWMutex
|
2022-09-28 07:41:01 +00:00
|
|
|
log *logger.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
|
2023-05-11 14:05:24 +00:00
|
|
|
blockChan chan *block.Block
|
|
|
|
notaryChan chan *result.NotaryRequestEvent
|
2021-01-20 17:05:52 +00:00
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
current subChannels
|
2021-08-09 15:27:04 +00:00
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
// cached subscription information
|
|
|
|
subscribedEvents map[util.Uint160]bool
|
|
|
|
subscribedNotaryEvents map[util.Uint160]bool
|
|
|
|
subscribedToNewBlocks bool
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Params is a group of Subscriber constructor parameters.
|
|
|
|
Params struct {
|
2022-09-28 07:41:01 +00:00
|
|
|
Log *logger.Logger
|
2021-09-06 12:52:17 +00:00
|
|
|
StartFromBlock uint32
|
2022-02-10 16:32:09 +00:00
|
|
|
Client *client.Client
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-02-27 14:17:55 +00:00
|
|
|
func (s *subscriber) NotificationChannels() NotificationChannels {
|
|
|
|
return NotificationChannels{
|
|
|
|
BlockCh: s.blockChan,
|
|
|
|
NotificationsCh: s.notifyChan,
|
|
|
|
NotaryRequestsCh: s.notaryChan,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2023-02-27 14:17:55 +00:00
|
|
|
func (s *subscriber) SubscribeForNotification(contracts ...util.Uint160) error {
|
2020-07-10 14:17:51 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
notifyIDs := make([]string, 0, len(contracts))
|
2020-07-10 14:17:51 +00:00
|
|
|
|
|
|
|
for i := range contracts {
|
2023-05-11 14:05:24 +00:00
|
|
|
if s.subscribedEvents[contracts[i]] {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-10 14:17:51 +00:00
|
|
|
// subscribe to contract notifications
|
2023-05-11 14:05:24 +00:00
|
|
|
id, err := s.client.ReceiveExecutionNotifications(contracts[i], s.current.NotifyChan)
|
2020-07-10 14:17:51 +00:00
|
|
|
if err != nil {
|
|
|
|
// if there is some error, undo all subscriptions and return error
|
2023-05-11 14:05:24 +00:00
|
|
|
for _, id := range notifyIDs {
|
|
|
|
_ = s.client.Unsubscribe(id)
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:17:55 +00:00
|
|
|
return err
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// save notification id
|
2023-05-11 14:05:24 +00:00
|
|
|
notifyIDs = append(notifyIDs, id)
|
|
|
|
}
|
|
|
|
for i := range contracts {
|
|
|
|
s.subscribedEvents[contracts[i]] = true
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:17:55 +00:00
|
|
|
return nil
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
func (s *subscriber) Close() {
|
|
|
|
s.client.Close()
|
|
|
|
}
|
|
|
|
|
2023-02-27 14:17:55 +00:00
|
|
|
func (s *subscriber) BlockNotifications() error {
|
2023-05-11 14:05:24 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
if s.subscribedToNewBlocks {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if _, err := s.client.ReceiveBlocks(s.current.BlockChan); err != nil {
|
2023-02-27 14:17:55 +00:00
|
|
|
return fmt.Errorf("could not subscribe for new block events: %w", err)
|
2021-01-20 17:08:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
s.subscribedToNewBlocks = true
|
|
|
|
|
2023-02-27 14:17:55 +00:00
|
|
|
return nil
|
2021-01-20 17:05:52 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:17:55 +00:00
|
|
|
func (s *subscriber) SubscribeForNotaryRequests(mainTXSigner util.Uint160) error {
|
2023-05-11 14:05:24 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
if s.subscribedNotaryEvents[mainTXSigner] {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if _, err := s.client.ReceiveNotaryRequests(mainTXSigner, s.current.NotaryChan); err != nil {
|
2023-02-27 14:17:55 +00:00
|
|
|
return fmt.Errorf("could not subscribe for notary request events: %w", err)
|
2021-08-09 15:27:04 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
s.subscribedNotaryEvents[mainTXSigner] = true
|
2023-02-27 14:17:55 +00:00
|
|
|
return nil
|
2021-08-09 15:27:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 14:17:51 +00:00
|
|
|
// 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
|
|
|
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
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
current: newSubChannels(),
|
|
|
|
|
|
|
|
subscribedEvents: make(map[util.Uint160]bool),
|
|
|
|
subscribedNotaryEvents: make(map[util.Uint160]bool),
|
|
|
|
}
|
|
|
|
// Worker listens all events from temporary NeoGo channel and puts them
|
|
|
|
// into corresponding permanent channels.
|
2020-07-10 14:17:51 +00:00
|
|
|
go sub.routeNotifications(ctx)
|
|
|
|
|
|
|
|
return sub, nil
|
|
|
|
}
|
2021-09-06 12:52:17 +00:00
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
func (s *subscriber) routeNotifications(ctx context.Context) {
|
|
|
|
var (
|
|
|
|
restoreCh = make(chan bool)
|
|
|
|
restoreInProgress bool
|
|
|
|
)
|
|
|
|
|
|
|
|
routeloop:
|
|
|
|
for {
|
|
|
|
var connLost bool
|
|
|
|
s.RLock()
|
|
|
|
curr := s.current
|
|
|
|
s.RUnlock()
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break routeloop
|
|
|
|
case ev, ok := <-curr.NotifyChan:
|
|
|
|
if ok {
|
2023-06-08 13:37:46 +00:00
|
|
|
s.client.Metrics().IncNotificationCount("notify")
|
2023-05-11 14:05:24 +00:00
|
|
|
s.notifyChan <- ev
|
|
|
|
} else {
|
|
|
|
connLost = true
|
|
|
|
}
|
|
|
|
case ev, ok := <-curr.BlockChan:
|
|
|
|
if ok {
|
2023-06-08 13:37:46 +00:00
|
|
|
s.client.Metrics().IncNotificationCount("block")
|
|
|
|
s.client.Metrics().SetLastBlock(ev.Index)
|
2023-05-11 14:05:24 +00:00
|
|
|
s.blockChan <- ev
|
|
|
|
} else {
|
|
|
|
connLost = true
|
|
|
|
}
|
|
|
|
case ev, ok := <-curr.NotaryChan:
|
|
|
|
if ok {
|
2023-06-08 13:37:46 +00:00
|
|
|
s.client.Metrics().IncNotificationCount("notary")
|
2023-05-11 14:05:24 +00:00
|
|
|
s.notaryChan <- ev
|
|
|
|
} else {
|
|
|
|
connLost = true
|
|
|
|
}
|
|
|
|
case ok := <-restoreCh:
|
|
|
|
restoreInProgress = false
|
|
|
|
if !ok {
|
|
|
|
connLost = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if connLost {
|
|
|
|
if !restoreInProgress {
|
2023-10-24 09:34:30 +00:00
|
|
|
restoreInProgress = s.switchEndpoint(ctx, restoreCh)
|
2023-05-11 14:05:24 +00:00
|
|
|
if !restoreInProgress {
|
|
|
|
break routeloop
|
|
|
|
}
|
|
|
|
curr.drain()
|
|
|
|
} else { // Avoid getting additional !ok events.
|
|
|
|
s.Lock()
|
|
|
|
s.current.NotifyChan = nil
|
|
|
|
s.current.BlockChan = nil
|
|
|
|
s.current.NotaryChan = nil
|
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(s.notifyChan)
|
|
|
|
close(s.blockChan)
|
|
|
|
close(s.notaryChan)
|
|
|
|
}
|
|
|
|
|
2023-10-24 09:34:30 +00:00
|
|
|
func (s *subscriber) switchEndpoint(ctx context.Context, finishCh chan<- bool) bool {
|
2023-08-09 12:53:13 +00:00
|
|
|
s.log.Info(logs.RPConnectionLost)
|
2023-05-11 14:05:24 +00:00
|
|
|
if !s.client.SwitchRPC(ctx) {
|
2023-08-09 12:53:13 +00:00
|
|
|
s.log.Error(logs.RPCNodeSwitchFailure)
|
2023-10-24 09:34:30 +00:00
|
|
|
return false
|
2023-05-11 14:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.Lock()
|
|
|
|
chs := newSubChannels()
|
|
|
|
go func() {
|
|
|
|
finishCh <- s.restoreSubscriptions(chs.NotifyChan, chs.BlockChan, chs.NotaryChan)
|
|
|
|
}()
|
|
|
|
s.current = chs
|
|
|
|
s.Unlock()
|
|
|
|
|
2023-06-08 13:37:46 +00:00
|
|
|
s.client.Metrics().IncSwitchCount()
|
2023-10-24 09:34:30 +00:00
|
|
|
return true
|
2023-05-11 14:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newSubChannels() subChannels {
|
|
|
|
return subChannels{
|
|
|
|
NotifyChan: make(chan *state.ContainedNotificationEvent),
|
|
|
|
BlockChan: make(chan *block.Block),
|
|
|
|
NotaryChan: make(chan *result.NotaryRequestEvent),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subChannels) drain() {
|
|
|
|
drainloop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case _, ok := <-s.NotifyChan:
|
|
|
|
if !ok {
|
|
|
|
s.NotifyChan = nil
|
|
|
|
}
|
|
|
|
case _, ok := <-s.BlockChan:
|
|
|
|
if !ok {
|
|
|
|
s.BlockChan = nil
|
|
|
|
}
|
|
|
|
case _, ok := <-s.NotaryChan:
|
|
|
|
if !ok {
|
|
|
|
s.NotaryChan = nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break drainloop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// restoreSubscriptions restores subscriptions according to
|
|
|
|
// cached information about them.
|
|
|
|
func (s *subscriber) restoreSubscriptions(notifCh chan<- *state.ContainedNotificationEvent,
|
|
|
|
blCh chan<- *block.Block, notaryCh chan<- *result.NotaryRequestEvent) bool {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// new block events restoration
|
|
|
|
if s.subscribedToNewBlocks {
|
|
|
|
_, err = s.client.ReceiveBlocks(blCh)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error(logs.ClientCouldNotRestoreBlockSubscriptionAfterRPCSwitch, zap.Error(err))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// notification events restoration
|
|
|
|
for contract := range s.subscribedEvents {
|
|
|
|
contract := contract // See https://github.com/nspcc-dev/neo-go/issues/2890
|
|
|
|
_, err = s.client.ReceiveExecutionNotifications(contract, notifCh)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error(logs.ClientCouldNotRestoreNotificationSubscriptionAfterRPCSwitch, zap.Error(err))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// notary notification events restoration
|
|
|
|
for signer := range s.subscribedNotaryEvents {
|
|
|
|
signer := signer // See https://github.com/nspcc-dev/neo-go/issues/2890
|
|
|
|
_, err = s.client.ReceiveNotaryRequests(signer, notaryCh)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error(logs.ClientCouldNotRestoreNotaryNotificationSubscriptionAfterRPCSwitch, zap.Error(err))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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
|
2023-02-05 15:59:38 +00:00
|
|
|
// FrostFS 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
|
|
|
}
|