2021-08-26 07:59:02 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2022-07-18 13:41:35 +00:00
|
|
|
"sort"
|
|
|
|
|
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-08-29 15:32:54 +00:00
|
|
|
cli, act, gas, 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
|
|
|
c.client = cli
|
2022-08-29 15:32:54 +00:00
|
|
|
c.rpcActor = act
|
|
|
|
c.gasToken = gas
|
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))
|
|
|
|
|
|
|
|
if !c.restoreSubscriptions(newEndpoint) {
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.cfg.ctx.Done():
|
|
|
|
_ = c.UnsubscribeAll()
|
|
|
|
c.close()
|
|
|
|
|
|
|
|
return
|
|
|
|
case <-c.closeChan:
|
|
|
|
_ = c.UnsubscribeAll()
|
|
|
|
c.close()
|
|
|
|
|
|
|
|
return
|
|
|
|
case n, ok := <-c.client.Notifications:
|
|
|
|
// notification channel is used as a connection
|
|
|
|
// state: if it is closed, the connection is
|
|
|
|
// considered to be lost
|
|
|
|
if !ok {
|
2022-06-09 19:22:02 +00:00
|
|
|
var closeReason string
|
|
|
|
if closeErr := c.client.GetError(); closeErr != nil {
|
|
|
|
closeReason = closeErr.Error()
|
|
|
|
} else {
|
|
|
|
closeReason = "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.Warn("switching to the next RPC node",
|
|
|
|
zap.String("reason", closeReason),
|
|
|
|
)
|
2021-02-09 17:52:10 +00:00
|
|
|
|
|
|
|
if !c.switchRPC() {
|
|
|
|
c.logger.Error("could not establish connection to any RPC node")
|
|
|
|
|
|
|
|
// could not connect to all endpoints =>
|
|
|
|
// switch client to inactive mode
|
|
|
|
c.inactiveMode()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c.notifications <- n
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
2021-08-26 07:59:02 +00:00
|
|
|
|
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
|
|
|
}
|