2020-07-24 13:54:03 +00:00
|
|
|
package innerring
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-02-09 14:57:51 +00:00
|
|
|
"io"
|
2023-05-19 15:06:20 +00:00
|
|
|
"sync/atomic"
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2024-06-24 14:12:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/metrics"
|
2024-10-14 10:51:21 +00:00
|
|
|
internalNet "git.frostfs.info/TrueCloudLab/frostfs-node/internal/net"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/config"
|
2023-05-12 07:41:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/processors/alphabet"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/processors/governance"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/processors/netmap"
|
|
|
|
timerEvent "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/timers"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
balanceClient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/balance"
|
2023-10-16 15:15:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/container"
|
2023-03-07 13:38:26 +00:00
|
|
|
nmClient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/subscriber"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/timer"
|
|
|
|
control "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control/ir"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/precision"
|
2023-12-05 14:05:34 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/sdnotify"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/state"
|
2021-01-22 15:01:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2021-08-05 14:17:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-11-13 11:47:23 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2023-03-09 13:19:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Server is the inner ring application structure, that contains all event
|
|
|
|
// processors, shared variables and event handlers.
|
|
|
|
Server struct {
|
2022-09-28 07:41:01 +00:00
|
|
|
log *logger.Logger
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
// event producers
|
|
|
|
morphListener event.Listener
|
|
|
|
mainnetListener event.Listener
|
2021-04-15 13:50:34 +00:00
|
|
|
blockTimers []*timer.BlockTimer
|
|
|
|
epochTimer *timer.BlockTimer
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
// global state
|
2023-10-16 15:15:04 +00:00
|
|
|
morphClient *client.Client
|
|
|
|
mainnetClient *client.Client
|
|
|
|
epochCounter atomic.Uint64
|
|
|
|
epochDuration atomic.Uint64
|
|
|
|
statusIndex *innerRingIndexer
|
|
|
|
precision precision.Fixed8Converter
|
|
|
|
healthStatus atomic.Int32
|
|
|
|
balanceClient *balanceClient.Client
|
|
|
|
netmapClient *nmClient.Client
|
|
|
|
persistate *state.PersistentStorage
|
|
|
|
containerClient *container.Client
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2021-12-27 12:24:05 +00:00
|
|
|
// metrics
|
2023-06-08 13:37:46 +00:00
|
|
|
irMetrics *metrics.InnerRingServiceMetrics
|
2021-12-27 12:24:05 +00:00
|
|
|
|
2021-05-04 08:50:13 +00:00
|
|
|
// notary configuration
|
|
|
|
feeConfig *config.FeeConfig
|
|
|
|
mainNotaryConfig *notaryConfig
|
2021-02-24 15:46:01 +00:00
|
|
|
|
2020-10-13 14:25:25 +00:00
|
|
|
// internal variables
|
2021-07-21 15:15:56 +00:00
|
|
|
key *keys.PrivateKey
|
|
|
|
contracts *contracts
|
|
|
|
predefinedValidators keys.PublicKeys
|
|
|
|
initialEpochTickDelta uint32
|
2021-09-06 12:58:40 +00:00
|
|
|
withoutMainNet bool
|
2023-12-05 14:05:34 +00:00
|
|
|
sdNotify bool
|
2021-07-21 15:15:56 +00:00
|
|
|
|
|
|
|
// runtime processors
|
2023-05-12 07:41:04 +00:00
|
|
|
netmapProcessor *netmap.Processor
|
|
|
|
alphabetProcessor *alphabet.Processor
|
2020-12-22 00:28:42 +00:00
|
|
|
|
|
|
|
workers []func(context.Context)
|
2021-02-09 14:57:51 +00:00
|
|
|
|
|
|
|
// Set of local resources that must be
|
|
|
|
// initialized at the very beginning of
|
|
|
|
// Server's work, (e.g. opening files).
|
|
|
|
//
|
|
|
|
// If any starter returns an error, Server's
|
|
|
|
// starting fails immediately.
|
|
|
|
starters []func() error
|
|
|
|
|
|
|
|
// Set of local resources that must be
|
|
|
|
// released at Server's work completion
|
|
|
|
// (e.g closing files).
|
|
|
|
//
|
|
|
|
// Closer's wrong outcome shouldn't be critical.
|
|
|
|
//
|
|
|
|
// Errors are logged.
|
|
|
|
closers []func() error
|
2021-06-09 15:49:10 +00:00
|
|
|
|
|
|
|
// Set of component runners which
|
|
|
|
// should report start errors
|
|
|
|
// to the application.
|
2022-07-30 04:53:35 +00:00
|
|
|
runners []func(chan<- error) error
|
2024-04-04 08:05:25 +00:00
|
|
|
|
2024-09-23 07:51:05 +00:00
|
|
|
// cmode used for upgrade scenario.
|
|
|
|
// nolint:unused
|
2024-04-04 08:05:25 +00:00
|
|
|
cmode *atomic.Bool
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chainParams struct {
|
2023-05-26 09:15:50 +00:00
|
|
|
log *logger.Logger
|
|
|
|
cfg *viper.Viper
|
|
|
|
key *keys.PrivateKey
|
|
|
|
name string
|
|
|
|
sgn *transaction.Signer
|
|
|
|
from uint32 // block height
|
|
|
|
morphCacheMetric metrics.MorphCacheMetrics
|
2024-10-15 11:46:00 +00:00
|
|
|
multinetMetrics metrics.MultinetMetrics
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
morphPrefix = "morph"
|
|
|
|
mainnetPrefix = "mainnet"
|
2021-02-24 15:46:01 +00:00
|
|
|
|
|
|
|
// extra blocks to overlap two deposits, we do that to make sure that
|
2021-04-01 17:17:53 +00:00
|
|
|
// there won't be any blocks without deposited assets in notary contract;
|
|
|
|
// make sure it is bigger than any extra rounding value in notary client.
|
|
|
|
notaryExtraBlocks = 300
|
2021-02-25 16:53:30 +00:00
|
|
|
// amount of tries before notary deposit timeout.
|
|
|
|
notaryDepositTimeout = 100
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-04-29 13:50:46 +00:00
|
|
|
errDepositTimeout = errors.New("notary deposit didn't appear in the network")
|
2021-04-27 11:07:06 +00:00
|
|
|
errDepositFail = errors.New("notary tx has faulted")
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Start runs all event providers.
|
2021-06-09 15:45:59 +00:00
|
|
|
func (s *Server) Start(ctx context.Context, intError chan<- error) (err error) {
|
|
|
|
s.setHealthStatus(control.HealthStatus_STARTING)
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
s.setHealthStatus(control.HealthStatus_READY)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-03-29 09:20:22 +00:00
|
|
|
err = s.launchStarters()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-02-09 14:57:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 15:49:10 +00:00
|
|
|
err = s.initConfigFromBlockchain()
|
2020-10-13 14:25:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-31 09:51:12 +00:00
|
|
|
if s.IsAlphabet() {
|
|
|
|
err = s.initMainNotary(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-24 15:46:01 +00:00
|
|
|
|
2023-03-31 09:51:12 +00:00
|
|
|
err = s.initSideNotary(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-25 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 11:05:51 +00:00
|
|
|
prm := governance.VoteValidatorPrm{}
|
|
|
|
prm.Validators = s.predefinedValidators
|
|
|
|
|
2020-11-13 11:47:23 +00:00
|
|
|
// vote for sidechain validator if it is prepared in config
|
2021-11-10 11:05:51 +00:00
|
|
|
err = s.voteForSidechainValidator(prm)
|
2020-11-13 11:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
// we don't stop inner ring execution on this error
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log.Warn(ctx, logs.InnerringCantVoteForPreparedValidators,
|
2020-11-13 11:47:23 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
|
2023-03-29 09:20:22 +00:00
|
|
|
s.tickInitialExpoch()
|
2021-07-21 15:15:56 +00:00
|
|
|
|
2020-10-13 15:40:35 +00:00
|
|
|
morphErr := make(chan error)
|
|
|
|
mainnnetErr := make(chan error)
|
|
|
|
|
|
|
|
// anonymous function to multiplex error channels
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case err := <-morphErr:
|
2021-05-18 08:12:51 +00:00
|
|
|
intError <- fmt.Errorf("sidechain: %w", err)
|
2020-10-13 15:40:35 +00:00
|
|
|
case err := <-mainnnetErr:
|
2021-05-18 08:12:51 +00:00
|
|
|
intError <- fmt.Errorf("mainnet: %w", err)
|
2020-10-13 15:40:35 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-03-29 09:20:22 +00:00
|
|
|
s.registerMorphNewBlockEventHandler()
|
|
|
|
s.registerMainnetNewBlockEventHandler()
|
|
|
|
|
|
|
|
if err := s.startRunners(intError); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go s.morphListener.ListenWithError(ctx, morphErr) // listen for neo:morph events
|
|
|
|
go s.mainnetListener.ListenWithError(ctx, mainnnetErr) // listen for neo:mainnet events
|
|
|
|
|
|
|
|
if err := s.startBlockTimers(); err != nil {
|
|
|
|
return fmt.Errorf("could not start block timers: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.startWorkers(ctx)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) registerMorphNewBlockEventHandler() {
|
2021-02-09 11:18:14 +00:00
|
|
|
s.morphListener.RegisterBlockHandler(func(b *block.Block) {
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log.Debug(context.Background(), logs.InnerringNewBlock,
|
2021-01-22 15:01:44 +00:00
|
|
|
zap.Uint32("index", b.Index),
|
|
|
|
)
|
|
|
|
|
2023-03-29 09:20:22 +00:00
|
|
|
err := s.persistate.SetUInt32(persistateSideChainLastBlockKey, b.Index)
|
2021-09-06 13:01:50 +00:00
|
|
|
if err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log.Warn(context.Background(), logs.InnerringCantUpdatePersistentState,
|
2021-09-06 13:01:50 +00:00
|
|
|
zap.String("chain", "side"),
|
|
|
|
zap.Uint32("block_index", b.Index))
|
|
|
|
}
|
|
|
|
|
2022-03-30 10:16:41 +00:00
|
|
|
s.tickTimers(b.Index)
|
2021-01-22 15:01:44 +00:00
|
|
|
})
|
2023-03-29 09:20:22 +00:00
|
|
|
}
|
2021-01-22 15:01:44 +00:00
|
|
|
|
2023-03-29 09:20:22 +00:00
|
|
|
func (s *Server) registerMainnetNewBlockEventHandler() {
|
2021-09-06 13:01:50 +00:00
|
|
|
if !s.withoutMainNet {
|
|
|
|
s.mainnetListener.RegisterBlockHandler(func(b *block.Block) {
|
2023-03-29 09:20:22 +00:00
|
|
|
err := s.persistate.SetUInt32(persistateMainChainLastBlockKey, b.Index)
|
2021-09-06 13:01:50 +00:00
|
|
|
if err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log.Warn(context.Background(), logs.InnerringCantUpdatePersistentState,
|
2021-09-06 13:01:50 +00:00
|
|
|
zap.String("chain", "main"),
|
|
|
|
zap.Uint32("block_index", b.Index))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-03-29 09:20:22 +00:00
|
|
|
}
|
2021-09-06 13:01:50 +00:00
|
|
|
|
2023-03-29 09:20:22 +00:00
|
|
|
func (s *Server) startRunners(errCh chan<- error) error {
|
2021-06-09 15:49:10 +00:00
|
|
|
for _, runner := range s.runners {
|
2023-03-29 09:20:22 +00:00
|
|
|
if err := runner(errCh); err != nil {
|
2022-07-30 04:53:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-06-09 15:49:10 +00:00
|
|
|
}
|
2023-03-29 09:20:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-06-09 15:49:10 +00:00
|
|
|
|
2023-03-29 09:20:22 +00:00
|
|
|
func (s *Server) launchStarters() error {
|
|
|
|
for _, starter := range s.starters {
|
|
|
|
if err := starter(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-22 15:01:44 +00:00
|
|
|
}
|
2023-03-29 09:20:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-22 15:01:44 +00:00
|
|
|
|
2023-03-29 09:20:22 +00:00
|
|
|
func (s *Server) initMainNotary(ctx context.Context) error {
|
|
|
|
if !s.mainNotaryConfig.disabled {
|
|
|
|
return s.initNotary(ctx,
|
|
|
|
s.depositMainNotary,
|
|
|
|
s.awaitMainNotaryDeposit,
|
|
|
|
"waiting to accept main notary deposit",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-22 00:28:42 +00:00
|
|
|
|
2023-03-29 09:20:22 +00:00
|
|
|
func (s *Server) initSideNotary(ctx context.Context) error {
|
2023-05-17 14:56:11 +00:00
|
|
|
return s.initNotary(ctx,
|
|
|
|
s.depositSideNotary,
|
|
|
|
s.awaitSideNotaryDeposit,
|
|
|
|
"waiting to accept side notary deposit",
|
|
|
|
)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 09:20:22 +00:00
|
|
|
func (s *Server) tickInitialExpoch() {
|
|
|
|
initialEpochTicker := timer.NewOneTickTimer(
|
|
|
|
timer.StaticBlockMeter(s.initialEpochTickDelta),
|
|
|
|
func() {
|
|
|
|
s.netmapProcessor.HandleNewEpochTick(timerEvent.NewEpochTick{})
|
|
|
|
})
|
|
|
|
s.addBlockTimer(initialEpochTicker)
|
|
|
|
}
|
|
|
|
|
2020-12-22 00:28:42 +00:00
|
|
|
func (s *Server) startWorkers(ctx context.Context) {
|
|
|
|
for _, w := range s.workers {
|
|
|
|
go w(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// Stop closes all subscription channels.
|
|
|
|
func (s *Server) Stop() {
|
2021-06-09 15:45:59 +00:00
|
|
|
s.setHealthStatus(control.HealthStatus_SHUTTING_DOWN)
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
go s.morphListener.Stop()
|
2022-04-05 10:20:23 +00:00
|
|
|
go s.mainnetListener.Stop()
|
2021-02-09 14:57:51 +00:00
|
|
|
|
|
|
|
for _, c := range s.closers {
|
|
|
|
if err := c(); err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log.Warn(context.Background(), logs.InnerringCloserError,
|
2021-02-09 14:57:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 06:51:06 +00:00
|
|
|
func (s *Server) registerNoErrCloser(c func()) {
|
|
|
|
s.registerCloser(func() error {
|
|
|
|
c()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:51 +00:00
|
|
|
func (s *Server) registerIOCloser(c io.Closer) {
|
|
|
|
s.registerCloser(c.Close)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) registerCloser(f func() error) {
|
|
|
|
s.closers = append(s.closers, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) registerStarter(f func() error) {
|
|
|
|
s.starters = append(s.starters, f)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates instance of inner ring sever structure.
|
2023-10-08 09:32:00 +00:00
|
|
|
func New(ctx context.Context, log *logger.Logger, cfg *viper.Viper, errChan chan<- error,
|
2024-06-18 09:40:38 +00:00
|
|
|
metrics *metrics.InnerRingServiceMetrics, cmode *atomic.Bool, audit *atomic.Bool,
|
2023-10-31 11:56:55 +00:00
|
|
|
) (*Server, error) {
|
2020-10-13 14:25:25 +00:00
|
|
|
var err error
|
2023-06-01 07:07:48 +00:00
|
|
|
server := &Server{
|
2023-06-08 13:37:46 +00:00
|
|
|
log: log,
|
2023-10-08 09:32:00 +00:00
|
|
|
irMetrics: metrics,
|
2024-04-04 08:05:25 +00:00
|
|
|
cmode: cmode,
|
2023-06-01 07:07:48 +00:00
|
|
|
}
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2023-12-05 14:05:34 +00:00
|
|
|
server.sdNotify, err = server.initSdNotify(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-09 15:45:59 +00:00
|
|
|
server.setHealthStatus(control.HealthStatus_HEALTH_STATUS_UNDEFINED)
|
|
|
|
|
2021-04-29 13:50:46 +00:00
|
|
|
// parse notary support
|
2021-05-04 08:50:13 +00:00
|
|
|
server.feeConfig = config.NewFeeConfig(cfg)
|
2021-04-29 13:50:46 +00:00
|
|
|
|
2023-03-29 13:54:15 +00:00
|
|
|
err = server.initKey(cfg)
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
2023-03-29 13:54:15 +00:00
|
|
|
return nil, err
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 13:01:50 +00:00
|
|
|
server.persistate, err = initPersistentStateStorage(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
server.registerCloser(server.persistate.Close)
|
|
|
|
|
2023-03-29 13:54:15 +00:00
|
|
|
var morphChain *chainParams
|
|
|
|
morphChain, err = server.initMorph(ctx, cfg, errChan)
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-29 13:54:15 +00:00
|
|
|
err = server.initMainnet(ctx, cfg, morphChain, errChan)
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-06 13:01:50 +00:00
|
|
|
|
2023-04-26 08:24:40 +00:00
|
|
|
server.initNotaryConfig()
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2023-03-29 13:54:15 +00:00
|
|
|
err = server.initContracts(cfg)
|
2021-07-23 14:37:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-29 13:54:15 +00:00
|
|
|
err = server.enableNotarySupport()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-07-23 14:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse default validators
|
|
|
|
server.predefinedValidators, err = parsePredefinedValidators(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("ir: can't parse predefined validators list: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-03-29 13:54:15 +00:00
|
|
|
var morphClients *serverMorphClients
|
|
|
|
morphClients, err = server.initClientsFromMorph()
|
2020-10-12 10:26:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-12 07:41:04 +00:00
|
|
|
err = server.initProcessors(cfg, morphClients)
|
2020-10-12 10:26:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-11-06 12:25:02 +00:00
|
|
|
server.initTimers(cfg)
|
2021-03-31 15:41:47 +00:00
|
|
|
|
2024-06-18 09:40:38 +00:00
|
|
|
err = server.initGRPCServer(cfg, log, audit)
|
2021-03-31 15:41:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
2023-12-05 14:05:34 +00:00
|
|
|
func (s *Server) initSdNotify(cfg *viper.Viper) (bool, error) {
|
|
|
|
if cfg.GetBool("systemdnotify.enabled") {
|
|
|
|
return true, sdnotify.InitSocket()
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-02-10 16:32:09 +00:00
|
|
|
func createListener(ctx context.Context, cli *client.Client, p *chainParams) (event.Listener, error) {
|
2021-09-15 15:57:43 +00:00
|
|
|
var (
|
|
|
|
sub subscriber.Subscriber
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2022-02-10 16:32:09 +00:00
|
|
|
sub, err = subscriber.New(ctx, &subscriber.Params{
|
|
|
|
Log: p.log,
|
|
|
|
StartFromBlock: p.from,
|
|
|
|
Client: cli,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listener, err := event.NewListener(event.ListenerParams{
|
2024-10-21 07:22:54 +00:00
|
|
|
Logger: p.log.With(zap.String("chain", p.name)),
|
2023-04-03 07:44:05 +00:00
|
|
|
Subscriber: sub,
|
2020-07-24 13:54:03 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return listener, err
|
|
|
|
}
|
|
|
|
|
2022-03-31 12:46:28 +00:00
|
|
|
func createClient(ctx context.Context, p *chainParams, errChan chan<- error) (*client.Client, error) {
|
2021-08-26 07:59:02 +00:00
|
|
|
// config name left unchanged for compatibility, may be its better to rename it to "endpoints" or "clients"
|
2022-07-18 13:41:35 +00:00
|
|
|
var endpoints []client.Endpoint
|
|
|
|
|
2022-08-02 11:31:21 +00:00
|
|
|
// defaultPriority is a default endpoint priority
|
|
|
|
const defaultPriority = 1
|
|
|
|
|
2022-07-18 13:41:35 +00:00
|
|
|
section := p.name + ".endpoint.client"
|
|
|
|
for i := 0; ; i++ {
|
2022-07-22 08:54:21 +00:00
|
|
|
addr := p.cfg.GetString(fmt.Sprintf("%s.%d.%s", section, i, "address"))
|
2022-07-18 13:41:35 +00:00
|
|
|
if addr == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-08-02 11:31:21 +00:00
|
|
|
priority := p.cfg.GetInt(section + ".priority")
|
|
|
|
if priority <= 0 {
|
|
|
|
priority = defaultPriority
|
|
|
|
}
|
|
|
|
|
2024-06-11 12:47:37 +00:00
|
|
|
var mtlsConfig *client.MTLSConfig
|
|
|
|
rootCAs := p.cfg.GetStringSlice(fmt.Sprintf("%s.%d.trusted_ca_list", section, i))
|
|
|
|
if len(rootCAs) != 0 {
|
|
|
|
mtlsConfig = &client.MTLSConfig{
|
|
|
|
TrustedCAList: rootCAs,
|
|
|
|
KeyFile: p.cfg.GetString(fmt.Sprintf("%s.%d.key", section, i)),
|
|
|
|
CertFile: p.cfg.GetString(fmt.Sprintf("%s.%d.certificate", section, i)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 13:41:35 +00:00
|
|
|
endpoints = append(endpoints, client.Endpoint{
|
2024-06-11 12:47:37 +00:00
|
|
|
Address: addr,
|
|
|
|
Priority: priority,
|
|
|
|
MTLSConfig: mtlsConfig,
|
2022-07-18 13:41:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-26 07:59:02 +00:00
|
|
|
if len(endpoints) == 0 {
|
|
|
|
return nil, fmt.Errorf("%s chain client endpoints not provided", p.name)
|
|
|
|
}
|
|
|
|
|
2024-10-15 11:46:00 +00:00
|
|
|
nc := parseMultinetConfig(p.cfg, p.multinetMetrics)
|
2024-10-14 10:51:21 +00:00
|
|
|
ds, err := internalNet.NewDialerSource(nc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("dialer source: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
return client.New(
|
2023-04-05 13:58:32 +00:00
|
|
|
ctx,
|
2021-05-31 08:55:40 +00:00
|
|
|
p.key,
|
2020-07-24 13:54:03 +00:00
|
|
|
client.WithLogger(p.log),
|
2021-03-13 15:22:27 +00:00
|
|
|
client.WithDialTimeout(p.cfg.GetDuration(p.name+".dial_timeout")),
|
2021-08-05 14:17:07 +00:00
|
|
|
client.WithSigner(p.sgn),
|
2022-07-18 13:41:35 +00:00
|
|
|
client.WithEndpoints(endpoints...),
|
2022-03-31 12:46:28 +00:00
|
|
|
client.WithConnLostCallback(func() {
|
|
|
|
errChan <- fmt.Errorf("%s chain connection has been lost", p.name)
|
|
|
|
}),
|
2022-10-12 17:13:57 +00:00
|
|
|
client.WithSwitchInterval(p.cfg.GetDuration(p.name+".switch_interval")),
|
2023-05-26 09:15:50 +00:00
|
|
|
client.WithMorphCacheMetrics(p.morphCacheMetric),
|
2024-10-14 10:51:21 +00:00
|
|
|
client.WithDialerSource(ds),
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-25 07:34:27 +00:00
|
|
|
func parsePredefinedValidators(cfg *viper.Viper) (keys.PublicKeys, error) {
|
2020-11-13 11:47:23 +00:00
|
|
|
publicKeyStrings := cfg.GetStringSlice("morph.validators")
|
|
|
|
|
|
|
|
return ParsePublicKeysFromStrings(publicKeyStrings)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParsePublicKeysFromStrings returns slice of neo public keys from slice
|
|
|
|
// of hex encoded strings.
|
2021-03-25 07:34:27 +00:00
|
|
|
func ParsePublicKeysFromStrings(pubKeys []string) (keys.PublicKeys, error) {
|
|
|
|
publicKeys := make(keys.PublicKeys, 0, len(pubKeys))
|
2020-11-13 11:47:23 +00:00
|
|
|
|
|
|
|
for i := range pubKeys {
|
|
|
|
key, err := keys.NewPublicKeyFromString(pubKeys[i])
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("can't decode public key: %w", err)
|
2020-11-13 11:47:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 07:34:27 +00:00
|
|
|
publicKeys = append(publicKeys, key)
|
2020-11-13 11:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return publicKeys, nil
|
|
|
|
}
|
|
|
|
|
2023-03-09 13:19:39 +00:00
|
|
|
// parseWalletAddressesFromStrings returns a slice of util.Uint160 from a slice
|
|
|
|
// of strings.
|
|
|
|
func parseWalletAddressesFromStrings(wallets []string) ([]util.Uint160, error) {
|
|
|
|
if len(wallets) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
extraWallets := make([]util.Uint160, len(wallets))
|
|
|
|
for i := range wallets {
|
|
|
|
extraWallets[i], err = address.StringToUint160(wallets[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return extraWallets, nil
|
|
|
|
}
|
|
|
|
|
2024-10-15 11:46:00 +00:00
|
|
|
func parseMultinetConfig(cfg *viper.Viper, m metrics.MultinetMetrics) internalNet.Config {
|
2024-10-14 10:51:21 +00:00
|
|
|
nc := internalNet.Config{
|
|
|
|
Enabled: cfg.GetBool("multinet.enabled"),
|
|
|
|
Balancer: cfg.GetString("multinet.balancer"),
|
|
|
|
Restrict: cfg.GetBool("multinet.restrict"),
|
|
|
|
FallbackDelay: cfg.GetDuration("multinet.fallback_delay"),
|
2024-10-15 11:46:00 +00:00
|
|
|
Metrics: m,
|
2024-10-14 10:51:21 +00:00
|
|
|
}
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
mask := cfg.GetString(fmt.Sprintf("multinet.subnets.%d.mask", i))
|
|
|
|
if mask == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
sourceIPs := cfg.GetStringSlice(fmt.Sprintf("multinet.subnets.%d.source_ips", i))
|
|
|
|
nc.Subnets = append(nc.Subnets, internalNet.Subnet{
|
|
|
|
Prefix: mask,
|
|
|
|
SourceIPs: sourceIPs,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nc
|
|
|
|
}
|
|
|
|
|
2020-10-13 14:25:25 +00:00
|
|
|
func (s *Server) initConfigFromBlockchain() error {
|
2020-07-24 13:54:03 +00:00
|
|
|
// get current epoch
|
2021-05-31 11:50:11 +00:00
|
|
|
epoch, err := s.netmapClient.Epoch()
|
2021-05-21 07:45:15 +00:00
|
|
|
if err != nil {
|
2021-10-11 17:09:52 +00:00
|
|
|
return fmt.Errorf("can't read epoch number: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get current epoch duration
|
|
|
|
epochDuration, err := s.netmapClient.EpochDuration()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't read epoch duration: %w", err)
|
2021-05-21 07:45:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 10:49:34 +00:00
|
|
|
// get balance precision
|
2021-05-31 11:50:11 +00:00
|
|
|
balancePrecision, err := s.balanceClient.Decimals()
|
2020-10-27 10:49:34 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("can't read balance contract precision: %w", err)
|
2020-10-27 10:49:34 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 11:04:09 +00:00
|
|
|
s.epochCounter.Store(epoch)
|
|
|
|
s.epochDuration.Store(epochDuration)
|
|
|
|
s.precision.SetBalancePrecision(balancePrecision)
|
|
|
|
|
2021-07-21 15:15:56 +00:00
|
|
|
// get next epoch delta tick
|
|
|
|
s.initialEpochTickDelta, err = s.nextEpochBlockDelta()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log.Debug(context.Background(), logs.InnerringReadConfigFromBlockchain,
|
2020-10-12 09:13:19 +00:00
|
|
|
zap.Bool("active", s.IsActive()),
|
2021-03-23 15:20:44 +00:00
|
|
|
zap.Bool("alphabet", s.IsAlphabet()),
|
2021-05-31 11:50:11 +00:00
|
|
|
zap.Uint64("epoch", epoch),
|
|
|
|
zap.Uint32("precision", balancePrecision),
|
2021-07-21 15:15:56 +00:00
|
|
|
zap.Uint32("init_epoch_tick_delta", s.initialEpochTickDelta),
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-22 15:49:52 +00:00
|
|
|
|
2021-07-21 15:15:56 +00:00
|
|
|
func (s *Server) nextEpochBlockDelta() (uint32, error) {
|
|
|
|
epochBlock, err := s.netmapClient.LastEpochBlock()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("can't read last epoch block: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
blockHeight, err := s.morphClient.BlockCount()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("can't get side chain height: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-10-14 11:04:09 +00:00
|
|
|
delta := uint32(s.epochDuration.Load()) + epochBlock
|
2021-07-21 15:15:56 +00:00
|
|
|
if delta < blockHeight {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return delta - blockHeight, nil
|
|
|
|
}
|
|
|
|
|
2021-03-23 15:20:44 +00:00
|
|
|
// onlyAlphabet wrapper around event handler that executes it
|
|
|
|
// only if inner ring node is alphabet node.
|
|
|
|
func (s *Server) onlyAlphabetEventHandler(f event.Handler) event.Handler {
|
|
|
|
return func(ev event.Event) {
|
|
|
|
if s.IsAlphabet() {
|
|
|
|
f(ev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 17:10:45 +00:00
|
|
|
|
2021-11-01 15:55:25 +00:00
|
|
|
func (s *Server) newEpochTickHandlers() []newEpochHandler {
|
2021-10-11 17:10:45 +00:00
|
|
|
newEpochHandlers := []newEpochHandler{
|
|
|
|
func() {
|
|
|
|
s.netmapProcessor.HandleNewEpochTick(timerEvent.NewEpochTick{})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return newEpochHandlers
|
|
|
|
}
|
2023-05-12 07:41:04 +00:00
|
|
|
|
|
|
|
func (s *Server) SetExtraWallets(cfg *viper.Viper) error {
|
|
|
|
parsedWallets, err := parseWalletAddressesFromStrings(cfg.GetStringSlice("emit.extra_wallets"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.alphabetProcessor.SetParsedWallets(parsedWallets)
|
|
|
|
return nil
|
|
|
|
}
|