2020-08-22 15:20:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-01-12 11:05:08 +00:00
|
|
|
"context"
|
2020-10-21 09:26:16 +00:00
|
|
|
"fmt"
|
2021-05-11 10:24:13 +00:00
|
|
|
"time"
|
2020-10-21 09:26:16 +00:00
|
|
|
|
2021-04-15 14:57:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-10-21 09:26:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-06-01 14:58:40 +00:00
|
|
|
mainchainconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/mainchain"
|
|
|
|
morphconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/morph"
|
2021-07-29 15:57:53 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2020-08-22 15:20:47 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2020-09-08 13:14:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper"
|
2020-10-21 09:26:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
|
|
|
netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/subscriber"
|
2021-01-12 13:25:30 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/rand"
|
|
|
|
"go.uber.org/zap"
|
2020-08-22 15:20:47 +00:00
|
|
|
)
|
|
|
|
|
2020-10-21 09:26:16 +00:00
|
|
|
const newEpochNotification = "NewEpoch"
|
|
|
|
|
2020-08-22 15:20:47 +00:00
|
|
|
func initMorphComponents(c *cfg) {
|
|
|
|
var err error
|
|
|
|
|
2021-06-01 14:58:40 +00:00
|
|
|
fn := func(addresses []string, dialTimeout time.Duration, handler func(*client.Client)) {
|
2021-05-11 10:24:13 +00:00
|
|
|
crand := rand.New() // math/rand with cryptographic source
|
|
|
|
crand.Shuffle(len(addresses), func(i, j int) {
|
|
|
|
addresses[i], addresses[j] = addresses[j], addresses[i]
|
|
|
|
})
|
|
|
|
|
|
|
|
for i := range addresses {
|
2021-05-31 08:55:40 +00:00
|
|
|
cli, err := client.New(c.key, addresses[i], client.WithDialTimeout(dialTimeout))
|
2021-05-11 10:24:13 +00:00
|
|
|
if err == nil {
|
|
|
|
c.log.Info("neo RPC connection established",
|
|
|
|
zap.String("endpoint", addresses[i]))
|
2021-01-12 13:25:30 +00:00
|
|
|
|
2021-05-11 10:24:13 +00:00
|
|
|
handler(cli)
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
c.log.Info("failed to establish neo RPC connection, trying another",
|
|
|
|
zap.String("endpoint", addresses[i]),
|
|
|
|
zap.String("error", err.Error()))
|
2021-01-12 13:25:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 10:24:13 +00:00
|
|
|
fatalOnErr(err)
|
2021-01-12 13:25:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 10:24:13 +00:00
|
|
|
// replace to a separate initialing block during refactoring
|
|
|
|
// since current function initializes sidechain components
|
2021-06-01 14:58:40 +00:00
|
|
|
fn(mainchainconfig.RPCEndpoint(c.appCfg), mainchainconfig.DialTimeout(c.appCfg), func(cli *client.Client) {
|
2021-05-11 10:24:13 +00:00
|
|
|
c.mainChainClient = cli
|
2021-08-23 11:15:08 +00:00
|
|
|
|
|
|
|
c.log.Debug("notary support",
|
|
|
|
zap.Bool("mainchain_enabled", cli.ProbeNotary()),
|
|
|
|
)
|
2021-06-01 14:58:40 +00:00
|
|
|
})
|
2021-05-11 10:24:13 +00:00
|
|
|
|
2021-06-01 14:58:40 +00:00
|
|
|
fn(morphconfig.RPCEndpoint(c.appCfg), morphconfig.DialTimeout(c.appCfg), func(cli *client.Client) {
|
2021-05-11 10:24:13 +00:00
|
|
|
c.cfgMorph.client = cli
|
2021-08-23 11:15:08 +00:00
|
|
|
|
|
|
|
c.log.Debug("notary support",
|
|
|
|
zap.Bool("sidechain_enabled", cli.ProbeNotary()),
|
|
|
|
)
|
2021-06-01 14:58:40 +00:00
|
|
|
})
|
2020-09-24 07:46:47 +00:00
|
|
|
|
2021-05-21 11:02:46 +00:00
|
|
|
wrap, err := wrapper.NewFromMorph(c.cfgMorph.client, c.cfgNetmap.scriptHash, 0)
|
2020-09-24 07:46:47 +00:00
|
|
|
fatalOnErr(err)
|
|
|
|
|
2021-07-29 15:57:53 +00:00
|
|
|
var netmapSource netmap.Source
|
|
|
|
|
|
|
|
c.cfgMorph.disableCache = morphconfig.DisableCache(c.appCfg)
|
|
|
|
|
|
|
|
if c.cfgMorph.disableCache {
|
|
|
|
netmapSource = wrap
|
|
|
|
} else {
|
|
|
|
// use RPC node as source of netmap (with caching)
|
|
|
|
netmapSource = newCachedNetmapStorage(c.cfgNetmap.state, wrap)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.cfgObject.netMapSource = netmapSource
|
2020-09-24 07:46:47 +00:00
|
|
|
c.cfgNetmap.wrapper = wrap
|
2020-08-22 15:20:47 +00:00
|
|
|
}
|
2020-10-21 09:26:16 +00:00
|
|
|
|
|
|
|
func listenMorphNotifications(c *cfg) {
|
2021-01-12 13:25:30 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
subs subscriber.Subscriber
|
|
|
|
)
|
|
|
|
|
2021-06-01 14:58:40 +00:00
|
|
|
endpoints := morphconfig.NotificationEndpoint(c.appCfg)
|
|
|
|
timeout := morphconfig.DialTimeout(c.appCfg)
|
2021-01-12 13:25:30 +00:00
|
|
|
|
|
|
|
crand := rand.New() // math/rand with cryptographic source
|
|
|
|
crand.Shuffle(len(endpoints), func(i, j int) {
|
|
|
|
endpoints[i], endpoints[j] = endpoints[j], endpoints[i]
|
2020-10-21 09:26:16 +00:00
|
|
|
})
|
2021-01-12 13:25:30 +00:00
|
|
|
|
|
|
|
for i := range endpoints {
|
|
|
|
subs, err = subscriber.New(c.ctx, &subscriber.Params{
|
|
|
|
Log: c.log,
|
|
|
|
Endpoint: endpoints[i],
|
|
|
|
DialTimeout: timeout,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
c.log.Info("websocket neo event listener established",
|
|
|
|
zap.String("endpoint", endpoints[i]))
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
c.log.Info("failed to establish websocket neo event listener, trying another",
|
|
|
|
zap.String("endpoint", endpoints[i]),
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
|
2020-10-21 09:26:16 +00:00
|
|
|
fatalOnErr(err)
|
|
|
|
|
|
|
|
lis, err := event.NewListener(event.ListenerParams{
|
|
|
|
Logger: c.log,
|
|
|
|
Subscriber: subs,
|
|
|
|
})
|
|
|
|
fatalOnErr(err)
|
|
|
|
|
2021-01-12 11:05:08 +00:00
|
|
|
c.workers = append(c.workers, newWorkerFromFunc(func(ctx context.Context) {
|
|
|
|
lis.ListenWithError(ctx, c.internalErr)
|
|
|
|
}))
|
2020-10-21 09:26:16 +00:00
|
|
|
|
|
|
|
setNetmapNotificationParser(c, newEpochNotification, netmapEvent.ParseNewEpoch)
|
|
|
|
registerNotificationHandlers(c.cfgNetmap.scriptHash, lis, c.cfgNetmap.parsers, c.cfgNetmap.subscribers)
|
2021-02-01 12:33:58 +00:00
|
|
|
registerNotificationHandlers(c.cfgContainer.scriptHash, lis, c.cfgContainer.parsers, c.cfgContainer.subscribers)
|
2021-04-15 14:57:29 +00:00
|
|
|
|
|
|
|
registerBlockHandler(lis, func(block *block.Block) {
|
|
|
|
c.log.Debug("new block", zap.Uint32("index", block.Index))
|
|
|
|
tickBlockTimers(c)
|
|
|
|
})
|
2020-10-21 09:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func registerNotificationHandlers(scHash util.Uint160, lis event.Listener, parsers map[event.Type]event.Parser,
|
|
|
|
subs map[event.Type][]event.Handler) {
|
|
|
|
for typ, handlers := range subs {
|
|
|
|
pi := event.ParserInfo{}
|
|
|
|
pi.SetType(typ)
|
|
|
|
pi.SetScriptHash(scHash)
|
|
|
|
|
|
|
|
p, ok := parsers[typ]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("missing parser for event %s", typ))
|
|
|
|
}
|
|
|
|
|
|
|
|
pi.SetParser(p)
|
|
|
|
|
|
|
|
lis.SetParser(pi)
|
|
|
|
|
|
|
|
for _, h := range handlers {
|
|
|
|
hi := event.HandlerInfo{}
|
|
|
|
hi.SetType(typ)
|
|
|
|
hi.SetScriptHash(scHash)
|
|
|
|
hi.SetHandler(h)
|
|
|
|
|
|
|
|
lis.RegisterHandler(hi)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-15 14:57:29 +00:00
|
|
|
|
|
|
|
func registerBlockHandler(lis event.Listener, handler event.BlockHandler) {
|
|
|
|
lis.RegisterBlockHandler(handler)
|
|
|
|
}
|