2020-08-22 15:20:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-01-12 11:05:08 +00:00
|
|
|
"context"
|
2021-08-25 10:49:59 +00:00
|
|
|
"errors"
|
2020-10-21 09:26:16 +00:00
|
|
|
"fmt"
|
2022-10-06 15:30:55 +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"
|
2022-09-07 11:32:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-10-21 09:26:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-06-01 14:58:40 +00:00
|
|
|
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"
|
2022-01-31 11:58:55 +00:00
|
|
|
nmClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
|
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
|
|
|
)
|
|
|
|
|
2021-08-25 10:49:59 +00:00
|
|
|
const (
|
|
|
|
newEpochNotification = "NewEpoch"
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// notaryDepositExtraBlocks is the amount of extra blocks to overlap two deposits,
|
2021-08-25 10:49:59 +00:00
|
|
|
// we do that to make sure that there won't be any blocks without deposited
|
2022-04-21 11:28:05 +00:00
|
|
|
// assets in a notary contract; make sure it is bigger than any extra rounding
|
|
|
|
// value in a notary client.
|
2021-08-25 10:49:59 +00:00
|
|
|
notaryDepositExtraBlocks = 300
|
|
|
|
|
|
|
|
// amount of tries(blocks) before notary deposit timeout.
|
|
|
|
notaryDepositRetriesAmount
|
|
|
|
)
|
2020-10-21 09:26:16 +00:00
|
|
|
|
2020-08-22 15:20:47 +00:00
|
|
|
func initMorphComponents(c *cfg) {
|
|
|
|
var err error
|
|
|
|
|
2022-02-07 14:37:06 +00:00
|
|
|
addresses := morphconfig.RPCEndpoint(c.appCfg)
|
2021-08-26 07:59:02 +00:00
|
|
|
|
2022-07-18 13:41:35 +00:00
|
|
|
// Morph client stable-sorts endpoints by priority. Shuffle here to randomize
|
|
|
|
// order of endpoints with the same priority.
|
2022-02-07 14:37:06 +00:00
|
|
|
rand.Shuffle(len(addresses), func(i, j int) {
|
|
|
|
addresses[i], addresses[j] = addresses[j], addresses[i]
|
|
|
|
})
|
2021-01-12 13:25:30 +00:00
|
|
|
|
2022-07-18 13:41:35 +00:00
|
|
|
cli, err := client.New(c.key,
|
2022-02-07 14:37:06 +00:00
|
|
|
client.WithDialTimeout(morphconfig.DialTimeout(c.appCfg)),
|
|
|
|
client.WithLogger(c.log),
|
2022-07-18 13:41:35 +00:00
|
|
|
client.WithEndpoints(addresses...),
|
2022-03-31 12:46:28 +00:00
|
|
|
client.WithConnLostCallback(func() {
|
|
|
|
c.internalErr <- errors.New("morph connection has been lost")
|
|
|
|
}),
|
2022-10-12 16:47:33 +00:00
|
|
|
client.WithSwitchInterval(morphconfig.SwitchInterval(c.appCfg)),
|
2022-02-07 14:37:06 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2021-08-26 07:59:02 +00:00
|
|
|
c.log.Info("failed to create neo RPC client",
|
|
|
|
zap.Any("endpoints", addresses),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
2021-05-11 10:24:13 +00:00
|
|
|
fatalOnErr(err)
|
2021-01-12 13:25:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 16:13:00 +00:00
|
|
|
c.onShutdown(cli.Close)
|
|
|
|
|
2022-02-07 14:37:06 +00:00
|
|
|
if err := cli.SetGroupSignerScope(); err != nil {
|
|
|
|
c.log.Info("failed to set group signer scope, continue with Global", zap.Error(err))
|
|
|
|
}
|
2021-02-09 17:52:10 +00:00
|
|
|
|
2022-02-07 14:37:06 +00:00
|
|
|
c.cfgMorph.client = cli
|
|
|
|
c.cfgMorph.notaryEnabled = cli.ProbeNotary()
|
2021-08-31 14:02:42 +00:00
|
|
|
|
2022-02-07 14:37:06 +00:00
|
|
|
lookupScriptHashesInNNS(c) // smart contract auto negotiation
|
2021-08-25 10:49:59 +00:00
|
|
|
|
2022-02-07 14:37:06 +00:00
|
|
|
if c.cfgMorph.notaryEnabled {
|
|
|
|
err = c.cfgMorph.client.EnableNotarySupport(
|
|
|
|
client.WithProxyContract(
|
|
|
|
c.cfgMorph.proxyScriptHash,
|
|
|
|
),
|
2021-08-23 11:15:08 +00:00
|
|
|
)
|
2022-02-07 14:37:06 +00:00
|
|
|
fatalOnErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.log.Debug("notary support",
|
|
|
|
zap.Bool("sidechain_enabled", c.cfgMorph.notaryEnabled),
|
|
|
|
)
|
2020-09-24 07:46:47 +00:00
|
|
|
|
2022-01-31 11:58:55 +00:00
|
|
|
wrap, err := nmClient.NewFromMorph(c.cfgMorph.client, c.cfgNetmap.scriptHash, 0, nmClient.TryNotary())
|
2020-09-24 07:46:47 +00:00
|
|
|
fatalOnErr(err)
|
|
|
|
|
2021-07-29 15:57:53 +00:00
|
|
|
var netmapSource netmap.Source
|
|
|
|
|
2022-08-16 11:59:30 +00:00
|
|
|
c.cfgMorph.cacheTTL = morphconfig.CacheTTL(c.appCfg)
|
2021-07-29 15:57:53 +00:00
|
|
|
|
2022-10-06 15:30:55 +00:00
|
|
|
if c.cfgMorph.cacheTTL == 0 {
|
|
|
|
msPerBlock, err := c.cfgMorph.client.MsPerBlock()
|
|
|
|
fatalOnErr(err)
|
|
|
|
c.cfgMorph.cacheTTL = time.Duration(msPerBlock) * time.Millisecond
|
|
|
|
c.log.Debug("morph.cache_ttl fetched from network", zap.Duration("value", c.cfgMorph.cacheTTL))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.cfgMorph.cacheTTL < 0 {
|
2021-07-29 15:57:53 +00:00
|
|
|
netmapSource = wrap
|
|
|
|
} else {
|
|
|
|
// use RPC node as source of netmap (with caching)
|
|
|
|
netmapSource = newCachedNetmapStorage(c.cfgNetmap.state, wrap)
|
|
|
|
}
|
|
|
|
|
2022-03-29 13:52:12 +00:00
|
|
|
c.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
|
|
|
|
2021-08-25 10:49:59 +00:00
|
|
|
func makeAndWaitNotaryDeposit(c *cfg) {
|
|
|
|
// skip notary deposit in non-notary environments
|
|
|
|
if !c.cfgMorph.notaryEnabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := makeNotaryDeposit(c)
|
|
|
|
fatalOnErr(err)
|
|
|
|
|
|
|
|
err = waitNotaryDeposit(c, tx)
|
|
|
|
fatalOnErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeNotaryDeposit(c *cfg) (util.Uint256, error) {
|
2021-09-30 19:11:20 +00:00
|
|
|
const (
|
|
|
|
// gasMultiplier defines how many times more the notary
|
|
|
|
// balance must be compared to the GAS balance of the node:
|
|
|
|
// notaryBalance = GASBalance * gasMultiplier
|
|
|
|
gasMultiplier = 3
|
|
|
|
|
|
|
|
// gasDivisor defines what part of GAS balance (1/gasDivisor)
|
|
|
|
// should be transferred to the notary service
|
|
|
|
gasDivisor = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
depositAmount, err := client.CalculateNotaryDepositAmount(c.cfgMorph.client, gasMultiplier, gasDivisor)
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint256{}, fmt.Errorf("could not calculate notary deposit: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-10-12 12:57:57 +00:00
|
|
|
epochDur, err := c.cfgNetmap.wrapper.EpochDuration()
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint256{}, fmt.Errorf("could not get current epoch duration: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-08-25 10:49:59 +00:00
|
|
|
return c.cfgMorph.client.DepositNotary(
|
2021-09-30 19:11:20 +00:00
|
|
|
depositAmount,
|
2021-10-12 12:57:57 +00:00
|
|
|
uint32(epochDur)+notaryDepositExtraBlocks,
|
2021-08-25 10:49:59 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
errNotaryDepositFail = errors.New("notary deposit tx has faulted")
|
|
|
|
errNotaryDepositTimeout = errors.New("notary deposit tx has not appeared in the network")
|
|
|
|
)
|
|
|
|
|
|
|
|
func waitNotaryDeposit(c *cfg, tx util.Uint256) error {
|
|
|
|
for i := 0; i < notaryDepositRetriesAmount; i++ {
|
|
|
|
select {
|
|
|
|
case <-c.ctx.Done():
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, err := c.cfgMorph.client.TxHalt(tx)
|
|
|
|
if err == nil {
|
|
|
|
if ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errNotaryDepositFail
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.cfgMorph.client.Wait(c.ctx, 1)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not wait for one block in chain: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errNotaryDepositTimeout
|
|
|
|
}
|
|
|
|
|
2020-10-21 09:26:16 +00:00
|
|
|
func listenMorphNotifications(c *cfg) {
|
2022-04-01 14:18:14 +00:00
|
|
|
// listenerPoolCap is a capacity of a
|
|
|
|
// worker pool inside the listener. It
|
|
|
|
// is used to prevent blocking in neo-go:
|
|
|
|
// the client cannot make RPC requests if
|
|
|
|
// the notification channel is not being
|
|
|
|
// read by another goroutine.
|
|
|
|
const listenerPoolCap = 10
|
|
|
|
|
2021-01-12 13:25:30 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
subs subscriber.Subscriber
|
|
|
|
)
|
|
|
|
|
2021-09-06 13:05:45 +00:00
|
|
|
fromSideChainBlock, err := c.persistate.UInt32(persistateSideChainLastBlockKey)
|
|
|
|
if err != nil {
|
|
|
|
fromSideChainBlock = 0
|
|
|
|
c.log.Warn("can't get last processed side chain block number", zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
|
2022-02-10 16:32:09 +00:00
|
|
|
subs, err = subscriber.New(c.ctx, &subscriber.Params{
|
|
|
|
Log: c.log,
|
|
|
|
StartFromBlock: fromSideChainBlock,
|
|
|
|
Client: c.cfgMorph.client,
|
|
|
|
})
|
2020-10-21 09:26:16 +00:00
|
|
|
fatalOnErr(err)
|
|
|
|
|
|
|
|
lis, err := event.NewListener(event.ListenerParams{
|
2022-04-01 14:18:14 +00:00
|
|
|
Logger: c.log,
|
|
|
|
Subscriber: subs,
|
|
|
|
WorkerPoolCapacity: listenerPoolCap,
|
2020-10-21 09:26:16 +00:00
|
|
|
})
|
|
|
|
fatalOnErr(err)
|
|
|
|
|
2021-01-12 11:05:08 +00:00
|
|
|
c.workers = append(c.workers, newWorkerFromFunc(func(ctx context.Context) {
|
2022-05-25 16:18:36 +00:00
|
|
|
runAndLog(c, "morph notification", false, func(c *cfg) {
|
|
|
|
lis.ListenWithError(ctx, c.internalErr)
|
|
|
|
})
|
2021-01-12 11:05:08 +00:00
|
|
|
}))
|
2020-10-21 09:26:16 +00:00
|
|
|
|
2022-09-07 11:32:16 +00:00
|
|
|
setNetmapNotificationParser(c, newEpochNotification, func(src *state.ContainedNotificationEvent) (event.Event, error) {
|
|
|
|
res, err := netmapEvent.ParseNewEpoch(src)
|
|
|
|
if err == nil {
|
|
|
|
c.log.Info("new epoch event from sidechain",
|
|
|
|
zap.Uint64("number", res.(netmapEvent.NewEpoch).EpochNumber()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
})
|
2020-10-21 09:26:16 +00:00
|
|
|
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))
|
2021-09-06 13:05:45 +00:00
|
|
|
|
|
|
|
err = c.persistate.SetUInt32(persistateSideChainLastBlockKey, block.Index)
|
|
|
|
if err != nil {
|
|
|
|
c.log.Warn("can't update persistent state",
|
|
|
|
zap.String("chain", "side"),
|
|
|
|
zap.Uint32("block_index", block.Index))
|
|
|
|
}
|
|
|
|
|
2021-04-15 14:57:29 +00:00
|
|
|
tickBlockTimers(c)
|
|
|
|
})
|
2020-10-21 09:26:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
func registerNotificationHandlers(scHash util.Uint160, lis event.Listener, parsers map[event.Type]event.NotificationParser,
|
2020-10-21 09:26:16 +00:00
|
|
|
subs map[event.Type][]event.Handler) {
|
|
|
|
for typ, handlers := range subs {
|
2021-08-12 15:24:17 +00:00
|
|
|
pi := event.NotificationParserInfo{}
|
2020-10-21 09:26:16 +00:00
|
|
|
pi.SetType(typ)
|
|
|
|
pi.SetScriptHash(scHash)
|
|
|
|
|
|
|
|
p, ok := parsers[typ]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("missing parser for event %s", typ))
|
|
|
|
}
|
|
|
|
|
|
|
|
pi.SetParser(p)
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
lis.SetNotificationParser(pi)
|
2020-10-21 09:26:16 +00:00
|
|
|
|
|
|
|
for _, h := range handlers {
|
2021-08-12 15:24:17 +00:00
|
|
|
hi := event.NotificationHandlerInfo{}
|
2020-10-21 09:26:16 +00:00
|
|
|
hi.SetType(typ)
|
|
|
|
hi.SetScriptHash(scHash)
|
|
|
|
hi.SetHandler(h)
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
lis.RegisterNotificationHandler(hi)
|
2020-10-21 09:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-15 14:57:29 +00:00
|
|
|
|
|
|
|
func registerBlockHandler(lis event.Listener, handler event.BlockHandler) {
|
|
|
|
lis.RegisterBlockHandler(handler)
|
|
|
|
}
|
2021-08-31 14:02:42 +00:00
|
|
|
|
|
|
|
// lookupScriptHashesInNNS looks up for contract script hashes in NNS contract of side
|
|
|
|
// chain if they were not specified in config file.
|
|
|
|
func lookupScriptHashesInNNS(c *cfg) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
|
|
|
|
emptyHash = util.Uint160{}
|
|
|
|
targets = [...]struct {
|
|
|
|
h *util.Uint160
|
|
|
|
nnsName string
|
|
|
|
}{
|
|
|
|
{&c.cfgNetmap.scriptHash, client.NNSNetmapContractName},
|
|
|
|
{&c.cfgAccounting.scriptHash, client.NNSBalanceContractName},
|
|
|
|
{&c.cfgContainer.scriptHash, client.NNSContainerContractName},
|
|
|
|
{&c.cfgReputation.scriptHash, client.NNSReputationContractName},
|
|
|
|
{&c.cfgMorph.proxyScriptHash, client.NNSProxyContractName},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, t := range targets {
|
2021-09-27 12:55:28 +00:00
|
|
|
if t.nnsName == client.NNSProxyContractName && !c.cfgMorph.notaryEnabled {
|
|
|
|
continue // ignore proxy contract if notary disabled
|
|
|
|
}
|
|
|
|
|
2021-08-31 14:02:42 +00:00
|
|
|
if emptyHash.Equals(*t.h) {
|
|
|
|
*t.h, err = c.cfgMorph.client.NNSContractAddress(t.nnsName)
|
|
|
|
fatalOnErrDetails(fmt.Sprintf("can't resolve %s in NNS", t.nnsName), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|