2021-08-26 07:59:02 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2022-07-18 13:41:35 +00:00
|
|
|
"sort"
|
2022-08-22 16:36:41 +00:00
|
|
|
"time"
|
2022-07-18 13:41:35 +00:00
|
|
|
|
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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
2021-08-26 07:59:02 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-07-18 13:41:35 +00:00
|
|
|
// Endpoint represents morph endpoint together with its priority.
|
|
|
|
type Endpoint struct {
|
|
|
|
Address string
|
|
|
|
Priority int
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:52:10 +00:00
|
|
|
type endpoints struct {
|
|
|
|
curr int
|
2022-07-18 13:41:35 +00:00
|
|
|
list []Endpoint
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
2021-08-26 07:59:02 +00:00
|
|
|
|
2022-07-18 13:41:35 +00:00
|
|
|
func (e *endpoints) init(ee []Endpoint) {
|
|
|
|
sort.SliceStable(ee, func(i, j int) bool {
|
2022-08-02 11:31:21 +00:00
|
|
|
return ee[i].Priority < ee[j].Priority
|
2022-07-18 13:41:35 +00:00
|
|
|
})
|
|
|
|
|
2022-06-29 13:02:07 +00:00
|
|
|
e.curr = 0
|
|
|
|
e.list = ee
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 17:52:10 +00:00
|
|
|
func (c *Client) switchRPC() bool {
|
|
|
|
c.switchLock.Lock()
|
|
|
|
defer c.switchLock.Unlock()
|
|
|
|
|
|
|
|
c.client.Close()
|
|
|
|
|
2022-07-18 13:41:35 +00:00
|
|
|
// Iterate endpoints in the order of decreasing priority.
|
|
|
|
for c.endpoints.curr = range c.endpoints.list {
|
|
|
|
newEndpoint := c.endpoints.list[c.endpoints.curr].Address
|
2022-11-03 12:44:59 +00:00
|
|
|
cli, act, err := c.newCli(newEndpoint)
|
2021-02-09 17:52:10 +00:00
|
|
|
if err != nil {
|
|
|
|
c.logger.Warn("could not establish connection to the switched RPC node",
|
|
|
|
zap.String("endpoint", newEndpoint),
|
|
|
|
zap.Error(err),
|
2021-08-26 07:59:02 +00:00
|
|
|
)
|
|
|
|
|
2021-02-09 17:52:10 +00:00
|
|
|
continue
|
2021-08-31 10:27:12 +00:00
|
|
|
}
|
2021-08-26 07:59:02 +00:00
|
|
|
|
2022-03-31 13:12:42 +00:00
|
|
|
c.cache.invalidate()
|
2021-02-09 17:52:10 +00:00
|
|
|
|
2022-06-29 13:02:07 +00:00
|
|
|
c.logger.Info("connection to the new RPC node has been established",
|
|
|
|
zap.String("endpoint", newEndpoint))
|
|
|
|
|
2023-02-22 13:04:58 +00:00
|
|
|
subs, ok := c.restoreSubscriptions(cli, newEndpoint, false)
|
|
|
|
if !ok {
|
2022-06-29 13:02:07 +00:00
|
|
|
// new WS client does not allow
|
|
|
|
// restoring subscription, client
|
|
|
|
// could not work correctly =>
|
|
|
|
// closing connection to RPC node
|
|
|
|
// to switch to another one
|
|
|
|
cli.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-08-22 16:36:41 +00:00
|
|
|
c.client = cli
|
2022-11-03 12:44:59 +00:00
|
|
|
c.setActor(act)
|
2023-02-22 13:04:58 +00:00
|
|
|
c.subsInfo = subs
|
2022-08-22 16:36:41 +00:00
|
|
|
|
2022-10-12 16:47:33 +00:00
|
|
|
if c.cfg.switchInterval != 0 && !c.switchIsActive.Load() &&
|
2022-08-22 16:36:41 +00:00
|
|
|
c.endpoints.list[c.endpoints.curr].Priority != c.endpoints.list[0].Priority {
|
|
|
|
c.switchIsActive.Store(true)
|
|
|
|
go c.switchToMostPrioritized()
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:52:10 +00:00
|
|
|
return true
|
|
|
|
}
|
2022-07-18 13:41:35 +00:00
|
|
|
|
|
|
|
return false
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) notificationLoop() {
|
2023-02-22 13:04:58 +00:00
|
|
|
var e any
|
|
|
|
var ok bool
|
|
|
|
|
2021-02-09 17:52:10 +00:00
|
|
|
for {
|
2022-08-22 16:36:41 +00:00
|
|
|
c.switchLock.RLock()
|
2023-02-22 13:04:58 +00:00
|
|
|
bChan := c.blockRcv
|
|
|
|
nChan := c.notificationRcv
|
|
|
|
nrChan := c.notaryReqRcv
|
2022-08-22 16:36:41 +00:00
|
|
|
c.switchLock.RUnlock()
|
|
|
|
|
2021-02-09 17:52:10 +00:00
|
|
|
select {
|
|
|
|
case <-c.cfg.ctx.Done():
|
|
|
|
_ = c.UnsubscribeAll()
|
|
|
|
c.close()
|
|
|
|
|
|
|
|
return
|
|
|
|
case <-c.closeChan:
|
|
|
|
_ = c.UnsubscribeAll()
|
|
|
|
c.close()
|
|
|
|
|
|
|
|
return
|
2023-02-22 13:04:58 +00:00
|
|
|
case e, ok = <-bChan:
|
|
|
|
case e, ok = <-nChan:
|
|
|
|
case e, ok = <-nrChan:
|
|
|
|
}
|
2022-06-09 19:22:02 +00:00
|
|
|
|
2023-02-22 13:04:58 +00:00
|
|
|
if ok {
|
|
|
|
c.routeEvent(e)
|
|
|
|
continue
|
|
|
|
}
|
2021-02-09 17:52:10 +00:00
|
|
|
|
2023-02-22 13:04:58 +00:00
|
|
|
if !c.reconnect() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 17:52:10 +00:00
|
|
|
|
2023-02-22 13:04:58 +00:00
|
|
|
func (c *Client) routeEvent(e any) {
|
|
|
|
typedNotification := rpcclient.Notification{Value: e}
|
2021-02-09 17:52:10 +00:00
|
|
|
|
2023-02-22 13:04:58 +00:00
|
|
|
switch e.(type) {
|
|
|
|
case *block.Block:
|
|
|
|
typedNotification.Type = neorpc.BlockEventID
|
|
|
|
case *state.ContainedNotificationEvent:
|
|
|
|
typedNotification.Type = neorpc.NotificationEventID
|
|
|
|
case *result.NotaryRequestEvent:
|
|
|
|
typedNotification.Type = neorpc.NotaryRequestEventID
|
|
|
|
}
|
2021-02-09 17:52:10 +00:00
|
|
|
|
2023-02-22 13:04:58 +00:00
|
|
|
select {
|
|
|
|
case c.notifications <- typedNotification:
|
|
|
|
case <-c.cfg.ctx.Done():
|
|
|
|
_ = c.UnsubscribeAll()
|
|
|
|
c.close()
|
|
|
|
case <-c.closeChan:
|
|
|
|
_ = c.UnsubscribeAll()
|
|
|
|
c.close()
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 17:52:10 +00:00
|
|
|
|
2023-02-22 13:04:58 +00:00
|
|
|
func (c *Client) reconnect() bool {
|
|
|
|
if closeErr := c.client.GetError(); closeErr != nil {
|
|
|
|
c.logger.Warn("switching to the next RPC node",
|
|
|
|
zap.String("reason", closeErr.Error()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// neo-go client was closed by calling `Close`
|
|
|
|
// method, that happens only when a client has
|
|
|
|
// switched to the more prioritized RPC
|
|
|
|
return true
|
|
|
|
}
|
2023-01-27 15:12:03 +00:00
|
|
|
|
2023-02-22 13:04:58 +00:00
|
|
|
if !c.switchRPC() {
|
|
|
|
c.logger.Error("could not establish connection to any RPC node")
|
2023-01-27 15:12:03 +00:00
|
|
|
|
2023-02-22 13:04:58 +00:00
|
|
|
// could not connect to all endpoints =>
|
|
|
|
// switch client to inactive mode
|
|
|
|
c.inactiveMode()
|
|
|
|
|
|
|
|
return false
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|
2023-02-22 13:04:58 +00:00
|
|
|
|
|
|
|
// TODO(@carpawell): call here some callback retrieved in constructor
|
|
|
|
// of the client to allow checking chain state since during switch
|
|
|
|
// process some notification could be lost
|
|
|
|
|
|
|
|
return true
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
2021-08-26 07:59:02 +00:00
|
|
|
|
2022-08-22 16:36:41 +00:00
|
|
|
func (c *Client) switchToMostPrioritized() {
|
2022-10-12 17:12:35 +00:00
|
|
|
t := time.NewTicker(c.cfg.switchInterval)
|
2022-08-22 16:36:41 +00:00
|
|
|
defer t.Stop()
|
|
|
|
defer c.switchIsActive.Store(false)
|
|
|
|
|
|
|
|
mainLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.cfg.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-t.C:
|
|
|
|
c.switchLock.RLock()
|
2023-02-22 13:04:58 +00:00
|
|
|
|
2022-08-22 16:36:41 +00:00
|
|
|
endpointsCopy := make([]Endpoint, len(c.endpoints.list))
|
|
|
|
copy(endpointsCopy, c.endpoints.list)
|
|
|
|
currPriority := c.endpoints.list[c.endpoints.curr].Priority
|
|
|
|
highestPriority := c.endpoints.list[0].Priority
|
2023-02-22 13:04:58 +00:00
|
|
|
|
2022-08-22 16:36:41 +00:00
|
|
|
c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if currPriority == highestPriority {
|
|
|
|
// already connected to
|
|
|
|
// the most prioritized
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, e := range endpointsCopy {
|
|
|
|
if currPriority == e.Priority {
|
|
|
|
// a switch will not increase the priority
|
|
|
|
continue mainLoop
|
|
|
|
}
|
|
|
|
|
|
|
|
tryE := e.Address
|
|
|
|
|
2022-11-03 12:44:59 +00:00
|
|
|
cli, act, err := c.newCli(tryE)
|
2022-08-22 16:36:41 +00:00
|
|
|
if err != nil {
|
|
|
|
c.logger.Warn("could not create client to the higher priority node",
|
|
|
|
zap.String("endpoint", tryE),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-02-22 13:04:58 +00:00
|
|
|
if subs, ok := c.restoreSubscriptions(cli, tryE, true); ok {
|
2022-08-22 16:36:41 +00:00
|
|
|
c.switchLock.Lock()
|
|
|
|
|
|
|
|
// higher priority node could have been
|
|
|
|
// connected in the other goroutine
|
|
|
|
if e.Priority >= c.endpoints.list[c.endpoints.curr].Priority {
|
|
|
|
cli.Close()
|
|
|
|
c.switchLock.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.client.Close()
|
|
|
|
c.cache.invalidate()
|
|
|
|
c.client = cli
|
2022-11-03 12:44:59 +00:00
|
|
|
c.setActor(act)
|
2023-02-22 13:04:58 +00:00
|
|
|
c.subsInfo = subs
|
2022-08-22 16:36:41 +00:00
|
|
|
c.endpoints.curr = i
|
|
|
|
|
|
|
|
c.switchLock.Unlock()
|
|
|
|
|
|
|
|
c.logger.Info("switched to the higher priority RPC",
|
|
|
|
zap.String("endpoint", tryE))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.Warn("could not restore side chain subscriptions using node",
|
|
|
|
zap.String("endpoint", tryE),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:03:55 +00:00
|
|
|
// close closes notification channel and wrapped WS client.
|
2021-02-09 17:52:10 +00:00
|
|
|
func (c *Client) close() {
|
|
|
|
close(c.notifications)
|
|
|
|
c.client.Close()
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|