2020-07-24 13:54:03 +00:00
|
|
|
package innerring
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-02-09 14:57:51 +00:00
|
|
|
"io"
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2021-01-22 15:01:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-11-13 11:47:23 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-12-11 08:33:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
2021-02-02 11:12:41 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/config"
|
2020-10-12 10:26:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/alphabet"
|
2020-12-18 09:27:19 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/audit"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/balance"
|
2020-08-11 12:14:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/container"
|
2021-03-25 13:24:44 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/governance"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/neofs"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/netmap"
|
2021-03-31 15:41:47 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/reputation"
|
2021-01-28 19:51:41 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement"
|
2021-01-26 10:33:28 +00:00
|
|
|
auditSettlement "github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement/audit"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2020-12-22 00:28:42 +00:00
|
|
|
auditWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit/wrapper"
|
2021-05-21 10:39:01 +00:00
|
|
|
balanceWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance/wrapper"
|
|
|
|
cntWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper"
|
2021-05-19 12:46:45 +00:00
|
|
|
neofsid "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid/wrapper"
|
2021-05-21 10:39:01 +00:00
|
|
|
nmWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper"
|
|
|
|
repWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation/wrapper"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/subscriber"
|
2021-04-15 13:50:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/timer"
|
2020-12-22 00:28:42 +00:00
|
|
|
audittask "github.com/nspcc-dev/neofs-node/pkg/services/audit/taskmanager"
|
2020-12-23 16:53:11 +00:00
|
|
|
util2 "github.com/nspcc-dev/neofs-node/pkg/util"
|
2020-10-27 10:49:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/precision"
|
2020-12-22 00:28:42 +00:00
|
|
|
"github.com/panjf2000/ants/v2"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.uber.org/atomic"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Server is the inner ring application structure, that contains all event
|
|
|
|
// processors, shared variables and event handlers.
|
|
|
|
Server struct {
|
|
|
|
log *zap.Logger
|
|
|
|
|
|
|
|
// 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
|
2021-03-23 12:44:09 +00:00
|
|
|
morphClient *client.Client
|
|
|
|
mainnetClient *client.Client
|
|
|
|
epochCounter atomic.Uint64
|
|
|
|
statusIndex *innerRingIndexer
|
|
|
|
precision precision.Fixed8Converter
|
|
|
|
auditClient *auditWrapper.ClientWrapper
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2021-05-04 08:50:13 +00:00
|
|
|
// notary configuration
|
|
|
|
feeConfig *config.FeeConfig
|
|
|
|
mainNotaryConfig *notaryConfig
|
|
|
|
sideNotaryConfig *notaryConfig
|
2021-02-24 15:46:01 +00:00
|
|
|
|
2020-10-13 14:25:25 +00:00
|
|
|
// internal variables
|
2020-11-13 11:47:23 +00:00
|
|
|
key *ecdsa.PrivateKey
|
2020-12-22 00:28:42 +00:00
|
|
|
pubKey []byte
|
2020-11-13 11:47:23 +00:00
|
|
|
contracts *contracts
|
2021-03-25 07:34:27 +00:00
|
|
|
predefinedValidators keys.PublicKeys
|
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
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contracts struct {
|
2021-03-31 15:41:47 +00:00
|
|
|
neofs util.Uint160 // in mainnet
|
|
|
|
netmap util.Uint160 // in morph
|
|
|
|
balance util.Uint160 // in morph
|
|
|
|
container util.Uint160 // in morph
|
|
|
|
audit util.Uint160 // in morph
|
|
|
|
proxy util.Uint160 // in morph
|
2021-04-20 15:16:40 +00:00
|
|
|
processing util.Uint160 // in mainnet
|
2021-03-31 15:41:47 +00:00
|
|
|
reputation util.Uint160 // in morph
|
2021-05-19 12:46:45 +00:00
|
|
|
neofsID util.Uint160 // in morph
|
2020-10-12 09:29:27 +00:00
|
|
|
|
2021-02-21 06:53:18 +00:00
|
|
|
alphabet alphabetContracts // in morph
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chainParams struct {
|
|
|
|
log *zap.Logger
|
|
|
|
cfg *viper.Viper
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
name string
|
|
|
|
gas util.Uint160
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2021-05-21 07:45:15 +00:00
|
|
|
|
|
|
|
precisionMethod = "decimals"
|
|
|
|
// netmap
|
|
|
|
getEpochMethod = "epoch"
|
2021-02-25 16:53:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
2020-10-13 15:40:35 +00:00
|
|
|
func (s *Server) Start(ctx context.Context, intError chan<- error) error {
|
2021-02-09 14:57:51 +00:00
|
|
|
for _, starter := range s.starters {
|
|
|
|
if err := starter(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 14:25:25 +00:00
|
|
|
err := s.initConfigFromBlockchain()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-04 08:50:13 +00:00
|
|
|
if !s.mainNotaryConfig.disabled {
|
|
|
|
err = s.initNotary(ctx,
|
|
|
|
s.depositMainNotary,
|
|
|
|
s.awaitMainNotaryDeposit,
|
|
|
|
"waiting to accept main notary deposit",
|
|
|
|
)
|
2021-04-29 13:50:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-04 08:50:13 +00:00
|
|
|
}
|
2021-02-24 15:46:01 +00:00
|
|
|
|
2021-05-04 08:50:13 +00:00
|
|
|
if !s.sideNotaryConfig.disabled {
|
|
|
|
err = s.initNotary(ctx,
|
|
|
|
s.depositSideNotary,
|
|
|
|
s.awaitSideNotaryDeposit,
|
|
|
|
"waiting to accept side notary deposit",
|
|
|
|
)
|
2021-04-29 13:50:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-25 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 11:47:23 +00:00
|
|
|
// vote for sidechain validator if it is prepared in config
|
|
|
|
err = s.voteForSidechainValidator(s.predefinedValidators)
|
|
|
|
if err != nil {
|
|
|
|
// we don't stop inner ring execution on this error
|
|
|
|
s.log.Warn("can't vote for prepared validators",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-02-09 11:18:14 +00:00
|
|
|
s.morphListener.RegisterBlockHandler(func(b *block.Block) {
|
2021-01-25 12:04:00 +00:00
|
|
|
s.log.Debug("new block",
|
2021-01-22 15:01:44 +00:00
|
|
|
zap.Uint32("index", b.Index),
|
|
|
|
)
|
|
|
|
|
2021-01-22 15:49:52 +00:00
|
|
|
s.tickTimers()
|
2021-01-22 15:01:44 +00:00
|
|
|
})
|
|
|
|
|
2021-01-25 13:47:01 +00:00
|
|
|
go s.morphListener.ListenWithError(ctx, morphErr) // listen for neo:morph events
|
|
|
|
go s.mainnetListener.ListenWithError(ctx, mainnnetErr) // listen for neo:mainnet events
|
|
|
|
|
2021-01-22 15:49:52 +00:00
|
|
|
if err := s.startBlockTimers(); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not start block timers: %w", err)
|
2021-01-22 15:01:44 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 00:28:42 +00:00
|
|
|
s.startWorkers(ctx)
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
go s.morphListener.Stop()
|
|
|
|
go s.mainnetListener.Stop()
|
2021-02-09 14:57:51 +00:00
|
|
|
|
|
|
|
for _, c := range s.closers {
|
|
|
|
if err := c(); err != nil {
|
|
|
|
s.log.Warn("closer error",
|
|
|
|
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.
|
|
|
|
func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error) {
|
2020-10-13 14:25:25 +00:00
|
|
|
var err error
|
2020-07-24 13:54:03 +00:00
|
|
|
server := &Server{log: log}
|
|
|
|
|
2021-04-29 13:50:46 +00:00
|
|
|
// parse notary support
|
2021-05-04 08:50:13 +00:00
|
|
|
server.feeConfig = config.NewFeeConfig(cfg)
|
|
|
|
server.mainNotaryConfig, server.sideNotaryConfig = parseNotaryConfigs(cfg)
|
2021-04-29 13:50:46 +00:00
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// prepare inner ring node private key
|
2020-10-13 14:25:25 +00:00
|
|
|
server.key, err = crypto.LoadPrivateKey(cfg.GetString("key"))
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("ir: can't create private key: %w", err)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get all script hashes of contracts
|
2020-10-13 14:25:25 +00:00
|
|
|
server.contracts, err = parseContracts(cfg)
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-13 11:47:23 +00:00
|
|
|
// parse default validators
|
|
|
|
server.predefinedValidators, err = parsePredefinedValidators(cfg)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("ir: can't parse predefined validators list: %w", err)
|
2020-11-13 11:47:23 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
morphChain := &chainParams{
|
|
|
|
log: log,
|
|
|
|
cfg: cfg,
|
2020-10-13 14:25:25 +00:00
|
|
|
key: server.key,
|
2020-07-24 13:54:03 +00:00
|
|
|
name: morphPrefix,
|
|
|
|
}
|
|
|
|
|
|
|
|
// create morph listener
|
|
|
|
server.morphListener, err = createListener(ctx, morphChain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create morph client
|
|
|
|
server.morphClient, err = createClient(ctx, morphChain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-20 15:16:40 +00:00
|
|
|
// enable notary support in the client
|
2021-05-04 08:50:13 +00:00
|
|
|
if !server.sideNotaryConfig.disabled {
|
2021-04-29 13:50:46 +00:00
|
|
|
err = server.morphClient.EnableNotarySupport(server.contracts.proxy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-20 10:44:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 10:47:20 +00:00
|
|
|
withoutMainNet := cfg.GetBool("without_mainnet")
|
|
|
|
|
|
|
|
if withoutMainNet {
|
2021-02-21 09:26:18 +00:00
|
|
|
// This works as long as event Listener starts listening loop once,
|
|
|
|
// otherwise Server.Start will run two similar routines.
|
|
|
|
// This behavior most likely will not change.
|
|
|
|
server.mainnetListener = server.morphListener
|
|
|
|
server.mainnetClient = server.morphClient
|
|
|
|
} else {
|
|
|
|
mainnetChain := morphChain
|
|
|
|
mainnetChain.name = mainnetPrefix
|
|
|
|
|
|
|
|
// create mainnet listener
|
|
|
|
server.mainnetListener, err = createListener(ctx, mainnetChain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2021-02-21 09:26:18 +00:00
|
|
|
// create mainnet client
|
|
|
|
server.mainnetClient, err = createClient(ctx, mainnetChain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-20 15:16:40 +00:00
|
|
|
|
|
|
|
// enable notary support in the client
|
2021-05-04 08:50:13 +00:00
|
|
|
if !server.mainNotaryConfig.disabled {
|
2021-04-29 13:50:46 +00:00
|
|
|
err = server.mainnetClient.EnableNotarySupport(
|
|
|
|
server.contracts.processing,
|
|
|
|
client.WithAlphabetSource(server.morphClient.Committee),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-20 15:16:40 +00:00
|
|
|
}
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 00:28:42 +00:00
|
|
|
server.pubKey = crypto.MarshalPublicKey(&server.key.PublicKey)
|
|
|
|
|
2021-03-23 12:44:09 +00:00
|
|
|
server.statusIndex = newInnerRingIndexer(
|
|
|
|
server.morphClient,
|
|
|
|
&server.key.PublicKey,
|
|
|
|
cfg.GetDuration("indexer.cache_timeout"),
|
|
|
|
)
|
|
|
|
|
2020-12-24 09:47:20 +00:00
|
|
|
auditPool, err := ants.NewPool(cfg.GetInt("audit.task.exec_pool_size"))
|
2020-12-22 00:28:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-21 10:39:01 +00:00
|
|
|
fee := server.feeConfig.SideChainFee()
|
|
|
|
|
|
|
|
server.auditClient, err = auditWrapper.NewFromMorph(server.morphClient, server.contracts.audit, fee)
|
2020-12-22 00:28:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-25 12:19:22 +00:00
|
|
|
cnrClient, err := cntWrapper.NewFromMorph(server.morphClient, server.contracts.container, fee, cntWrapper.TryNotary())
|
2021-01-28 19:51:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-21 10:39:01 +00:00
|
|
|
nmClient, err := nmWrapper.NewFromMorph(server.morphClient, server.contracts.netmap, fee)
|
2021-01-28 19:51:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-21 10:39:01 +00:00
|
|
|
balClient, err := balanceWrapper.NewFromMorph(server.morphClient, server.contracts.balance, fee)
|
2021-01-28 19:51:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-21 10:39:01 +00:00
|
|
|
repClient, err := repWrapper.NewFromMorph(server.morphClient, server.contracts.reputation, fee)
|
2021-03-31 15:41:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-19 12:46:45 +00:00
|
|
|
neofsIDClient, err := neofsid.NewFromMorph(server.morphClient, server.contracts.neofsID, fee)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:12:41 +00:00
|
|
|
// create global runtime config reader
|
|
|
|
globalConfig := config.NewGlobalConfigReader(cfg, nmClient)
|
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
clientCache := newClientCache(&clientCacheParams{
|
|
|
|
Log: log,
|
|
|
|
Key: server.key,
|
|
|
|
SGTimeout: cfg.GetDuration("audit.timeout.get"),
|
|
|
|
HeadTimeout: cfg.GetDuration("audit.timeout.head"),
|
|
|
|
RangeTimeout: cfg.GetDuration("audit.timeout.rangehash"),
|
|
|
|
})
|
2020-12-22 12:14:50 +00:00
|
|
|
|
2021-05-31 06:51:06 +00:00
|
|
|
server.registerNoErrCloser(clientCache.cache.CloseAll)
|
|
|
|
|
2020-12-23 16:53:11 +00:00
|
|
|
pdpPoolSize := cfg.GetInt("audit.pdp.pairs_pool_size")
|
2020-12-25 07:45:59 +00:00
|
|
|
porPoolSize := cfg.GetInt("audit.por.pool_size")
|
2020-12-23 16:53:11 +00:00
|
|
|
|
2021-01-29 07:42:40 +00:00
|
|
|
// create audit processor dependencies
|
2020-12-22 00:28:42 +00:00
|
|
|
auditTaskManager := audittask.New(
|
|
|
|
audittask.WithQueueCapacity(cfg.GetUint32("audit.task.queue_capacity")),
|
|
|
|
audittask.WithWorkerPool(auditPool),
|
|
|
|
audittask.WithLogger(log),
|
2020-12-22 12:14:50 +00:00
|
|
|
audittask.WithContainerCommunicator(clientCache),
|
2020-12-23 16:53:11 +00:00
|
|
|
audittask.WithMaxPDPSleepInterval(cfg.GetDuration("audit.pdp.max_sleep_interval")),
|
|
|
|
audittask.WithPDPWorkerPoolGenerator(func() (util2.WorkerPool, error) {
|
2020-12-24 09:47:20 +00:00
|
|
|
return ants.NewPool(pdpPoolSize)
|
2020-12-23 16:53:11 +00:00
|
|
|
}),
|
2020-12-24 11:24:25 +00:00
|
|
|
audittask.WithPoRWorkerPoolGenerator(func() (util2.WorkerPool, error) {
|
|
|
|
return ants.NewPool(porPoolSize)
|
|
|
|
}),
|
2020-12-22 00:28:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
server.workers = append(server.workers, auditTaskManager.Listen)
|
|
|
|
|
2020-12-18 09:27:19 +00:00
|
|
|
// create audit processor
|
|
|
|
auditProcessor, err := audit.New(&audit.Params{
|
|
|
|
Log: log,
|
2020-12-21 08:40:30 +00:00
|
|
|
NetmapContract: server.contracts.netmap,
|
2020-12-18 09:27:19 +00:00
|
|
|
ContainerContract: server.contracts.container,
|
|
|
|
AuditContract: server.contracts.audit,
|
|
|
|
MorphClient: server.morphClient,
|
|
|
|
IRList: server,
|
2021-04-29 13:50:46 +00:00
|
|
|
FeeProvider: server.feeConfig,
|
2020-12-22 12:14:50 +00:00
|
|
|
ClientCache: clientCache,
|
2021-03-13 15:22:21 +00:00
|
|
|
Key: server.key,
|
2020-12-23 15:07:10 +00:00
|
|
|
RPCSearchTimeout: cfg.GetDuration("audit.timeout.search"),
|
2020-12-22 00:28:42 +00:00
|
|
|
TaskManager: auditTaskManager,
|
|
|
|
Reporter: server,
|
2020-12-18 09:27:19 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-29 07:42:40 +00:00
|
|
|
// create settlement processor dependencies
|
2021-02-01 12:40:07 +00:00
|
|
|
settlementDeps := &settlementDeps{
|
2021-04-29 13:50:46 +00:00
|
|
|
globalConfig: globalConfig,
|
|
|
|
log: server.log,
|
2021-05-19 16:32:25 +00:00
|
|
|
cnrSrc: cntWrapper.AsContainerSource(cnrClient),
|
2021-04-29 13:50:46 +00:00
|
|
|
auditClient: server.auditClient,
|
|
|
|
nmSrc: nmClient,
|
|
|
|
clientCache: clientCache,
|
|
|
|
balanceClient: balClient,
|
2021-05-04 08:50:13 +00:00
|
|
|
notaryDisabled: server.sideNotaryConfig.disabled,
|
2021-01-28 19:51:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 12:40:07 +00:00
|
|
|
auditCalcDeps := &auditSettlementDeps{
|
|
|
|
settlementDeps: settlementDeps,
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:20:33 +00:00
|
|
|
basicSettlementDeps := &basicIncomeSettlementDeps{
|
|
|
|
settlementDeps: settlementDeps,
|
|
|
|
cnrClient: cnrClient,
|
|
|
|
}
|
|
|
|
|
2021-01-28 19:51:41 +00:00
|
|
|
auditSettlementCalc := auditSettlement.NewCalculator(
|
|
|
|
&auditSettlement.CalculatorPrm{
|
|
|
|
ResultStorage: auditCalcDeps,
|
|
|
|
ContainerStorage: auditCalcDeps,
|
|
|
|
PlacementCalculator: auditCalcDeps,
|
|
|
|
SGStorage: auditCalcDeps,
|
|
|
|
AccountStorage: auditCalcDeps,
|
|
|
|
Exchanger: auditCalcDeps,
|
2021-04-07 10:53:13 +00:00
|
|
|
AuditFeeFetcher: auditCalcDeps,
|
2021-01-28 19:51:41 +00:00
|
|
|
},
|
|
|
|
auditSettlement.WithLogger(server.log),
|
|
|
|
)
|
|
|
|
|
2021-01-29 07:42:40 +00:00
|
|
|
// create settlement processor
|
2021-01-28 19:51:41 +00:00
|
|
|
settlementProcessor := settlement.New(
|
|
|
|
settlement.Prm{
|
|
|
|
AuditProcessor: (*auditSettlementCalculator)(auditSettlementCalc),
|
2021-02-01 16:20:33 +00:00
|
|
|
BasicIncome: &basicSettlementConstructor{dep: basicSettlementDeps},
|
|
|
|
State: server,
|
2021-01-28 19:51:41 +00:00
|
|
|
},
|
|
|
|
settlement.WithLogger(server.log),
|
|
|
|
)
|
|
|
|
|
2021-02-09 15:03:43 +00:00
|
|
|
locodeValidator, err := server.newLocodeValidator(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:24:44 +00:00
|
|
|
// create governance processor
|
|
|
|
governanceProcessor, err := governance.New(&governance.Params{
|
2021-04-29 13:50:46 +00:00
|
|
|
Log: log,
|
|
|
|
NeoFSContract: server.contracts.neofs,
|
|
|
|
NetmapContract: server.contracts.netmap,
|
|
|
|
AlphabetState: server,
|
|
|
|
EpochState: server,
|
|
|
|
Voter: server,
|
|
|
|
MorphClient: server.morphClient,
|
|
|
|
MainnetClient: server.mainnetClient,
|
2021-05-04 08:50:13 +00:00
|
|
|
NotaryDisabled: server.sideNotaryConfig.disabled,
|
2021-04-29 13:50:46 +00:00
|
|
|
FeeProvider: server.feeConfig,
|
2021-03-25 13:24:44 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-04 10:47:20 +00:00
|
|
|
var alphaSync event.Handler
|
|
|
|
|
|
|
|
if withoutMainNet {
|
|
|
|
alphaSync = func(event.Event) {
|
|
|
|
log.Debug("alphabet keys sync is disabled")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
alphaSync = governanceProcessor.HandleAlphabetSync
|
2021-05-18 07:40:21 +00:00
|
|
|
err = bindMainnetProcessor(governanceProcessor, server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-04 10:47:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// create netmap processor
|
2021-01-29 07:42:40 +00:00
|
|
|
netmapProcessor, err := netmap.New(&netmap.Params{
|
2020-10-29 16:09:45 +00:00
|
|
|
Log: log,
|
|
|
|
PoolSize: cfg.GetInt("workers.netmap"),
|
|
|
|
NetmapContract: server.contracts.netmap,
|
2021-01-29 07:42:40 +00:00
|
|
|
EpochTimer: server,
|
2020-10-29 16:09:45 +00:00
|
|
|
MorphClient: server.morphClient,
|
|
|
|
EpochState: server,
|
2021-03-23 15:20:44 +00:00
|
|
|
AlphabetState: server,
|
2020-10-29 16:09:45 +00:00
|
|
|
CleanupEnabled: cfg.GetBool("netmap_cleaner.enabled"),
|
|
|
|
CleanupThreshold: cfg.GetUint64("netmap_cleaner.threshold"),
|
2021-01-29 07:48:47 +00:00
|
|
|
ContainerWrapper: cnrClient,
|
2021-01-29 07:42:40 +00:00
|
|
|
HandleAudit: server.onlyActiveEventHandler(
|
|
|
|
auditProcessor.StartAuditHandler(),
|
|
|
|
),
|
2021-03-23 15:20:44 +00:00
|
|
|
AuditSettlementsHandler: server.onlyAlphabetEventHandler(
|
2021-01-29 07:42:40 +00:00
|
|
|
settlementProcessor.HandleAuditEvent,
|
|
|
|
),
|
2021-05-04 10:47:20 +00:00
|
|
|
AlphabetSyncHandler: alphaSync,
|
2021-03-25 13:24:44 +00:00
|
|
|
NodeValidator: locodeValidator,
|
2021-05-04 08:50:13 +00:00
|
|
|
NotaryDisabled: server.sideNotaryConfig.disabled,
|
2021-04-29 13:50:46 +00:00
|
|
|
FeeProvider: server.feeConfig,
|
2020-07-24 13:54:03 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bindMorphProcessor(netmapProcessor, server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-11 12:14:08 +00:00
|
|
|
// container processor
|
|
|
|
containerProcessor, err := container.New(&container.Params{
|
|
|
|
Log: log,
|
|
|
|
PoolSize: cfg.GetInt("workers.container"),
|
2020-10-13 14:25:25 +00:00
|
|
|
ContainerContract: server.contracts.container,
|
2020-08-11 12:14:08 +00:00
|
|
|
MorphClient: server.morphClient,
|
2021-03-23 15:20:44 +00:00
|
|
|
AlphabetState: server,
|
2021-04-29 13:50:46 +00:00
|
|
|
FeeProvider: server.feeConfig,
|
2021-05-19 12:46:45 +00:00
|
|
|
ContainerClient: cnrClient,
|
|
|
|
NeoFSIDClient: neofsIDClient,
|
2020-08-11 12:14:08 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bindMorphProcessor(containerProcessor, server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
// create balance processor
|
|
|
|
balanceProcessor, err := balance.New(&balance.Params{
|
|
|
|
Log: log,
|
|
|
|
PoolSize: cfg.GetInt("workers.balance"),
|
2020-10-13 14:25:25 +00:00
|
|
|
NeoFSContract: server.contracts.neofs,
|
|
|
|
BalanceContract: server.contracts.balance,
|
2020-07-24 13:54:03 +00:00
|
|
|
MainnetClient: server.mainnetClient,
|
2021-03-23 15:20:44 +00:00
|
|
|
AlphabetState: server,
|
2020-10-27 10:49:34 +00:00
|
|
|
Converter: &server.precision,
|
2021-04-29 13:50:46 +00:00
|
|
|
FeeProvider: server.feeConfig,
|
2020-07-24 13:54:03 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bindMorphProcessor(balanceProcessor, server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: create reputation processor
|
|
|
|
|
|
|
|
// create mainnnet neofs processor
|
|
|
|
neofsProcessor, err := neofs.New(&neofs.Params{
|
2021-03-18 17:42:35 +00:00
|
|
|
Log: log,
|
|
|
|
PoolSize: cfg.GetInt("workers.neofs"),
|
|
|
|
NeoFSContract: server.contracts.neofs,
|
|
|
|
BalanceContract: server.contracts.balance,
|
|
|
|
NetmapContract: server.contracts.netmap,
|
|
|
|
MorphClient: server.morphClient,
|
|
|
|
EpochState: server,
|
2021-03-23 15:20:44 +00:00
|
|
|
AlphabetState: server,
|
2021-03-18 17:42:35 +00:00
|
|
|
Converter: &server.precision,
|
2021-04-29 13:50:46 +00:00
|
|
|
FeeProvider: server.feeConfig,
|
2021-03-18 17:42:35 +00:00
|
|
|
MintEmitCacheSize: cfg.GetInt("emit.mint.cache_size"),
|
|
|
|
MintEmitThreshold: cfg.GetUint64("emit.mint.threshold"),
|
|
|
|
MintEmitValue: fixedn.Fixed8(cfg.GetInt64("emit.mint.value")),
|
|
|
|
GasBalanceThreshold: cfg.GetInt64("emit.gas.balance_threshold"),
|
2020-07-24 13:54:03 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bindMainnetProcessor(neofsProcessor, server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-12 10:26:05 +00:00
|
|
|
// create alphabet processor
|
2021-01-29 07:42:40 +00:00
|
|
|
alphabetProcessor, err := alphabet.New(&alphabet.Params{
|
2020-10-12 10:26:05 +00:00
|
|
|
Log: log,
|
|
|
|
PoolSize: cfg.GetInt("workers.alphabet"),
|
2020-10-13 14:25:25 +00:00
|
|
|
AlphabetContracts: server.contracts.alphabet,
|
2020-11-03 08:12:39 +00:00
|
|
|
NetmapContract: server.contracts.netmap,
|
2020-10-12 10:26:05 +00:00
|
|
|
MorphClient: server.morphClient,
|
|
|
|
IRList: server,
|
2020-11-03 08:12:39 +00:00
|
|
|
StorageEmission: cfg.GetUint64("emit.storage.amount"),
|
2020-10-12 10:26:05 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bindMorphProcessor(alphabetProcessor, server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-31 15:41:47 +00:00
|
|
|
// create reputation processor
|
|
|
|
reputationProcessor, err := reputation.New(&reputation.Params{
|
|
|
|
Log: log,
|
|
|
|
PoolSize: cfg.GetInt("workers.reputation"),
|
2021-05-04 08:50:13 +00:00
|
|
|
NotaryDisabled: server.sideNotaryConfig.disabled,
|
2021-03-31 15:41:47 +00:00
|
|
|
ReputationContract: server.contracts.reputation,
|
|
|
|
EpochState: server,
|
|
|
|
AlphabetState: server,
|
|
|
|
ReputationWrapper: repClient,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bindMorphProcessor(reputationProcessor, server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// todo: create vivid id component
|
|
|
|
|
2021-01-29 07:42:40 +00:00
|
|
|
// initialize epoch timers
|
|
|
|
server.epochTimer = newEpochTimer(&epochTimerArgs{
|
2021-02-02 14:17:51 +00:00
|
|
|
l: server.log,
|
2021-05-04 08:50:13 +00:00
|
|
|
notaryDisabled: server.sideNotaryConfig.disabled,
|
2021-02-02 14:17:51 +00:00
|
|
|
nm: netmapProcessor,
|
|
|
|
cnrWrapper: cnrClient,
|
|
|
|
epoch: server,
|
2021-04-15 06:43:49 +00:00
|
|
|
epochDuration: globalConfig.EpochDuration,
|
2021-02-02 14:17:51 +00:00
|
|
|
stopEstimationDMul: cfg.GetUint32("timers.stop_estimation.mul"),
|
|
|
|
stopEstimationDDiv: cfg.GetUint32("timers.stop_estimation.div"),
|
|
|
|
collectBasicIncome: subEpochEventHandler{
|
|
|
|
handler: settlementProcessor.HandleIncomeCollectionEvent,
|
|
|
|
durationMul: cfg.GetUint32("timers.collect_basic_income.mul"),
|
|
|
|
durationDiv: cfg.GetUint32("timers.collect_basic_income.div"),
|
|
|
|
},
|
|
|
|
distributeBasicIncome: subEpochEventHandler{
|
|
|
|
handler: settlementProcessor.HandleIncomeDistributionEvent,
|
|
|
|
durationMul: cfg.GetUint32("timers.distribute_basic_income.mul"),
|
|
|
|
durationDiv: cfg.GetUint32("timers.distribute_basic_income.div"),
|
|
|
|
},
|
2021-01-29 07:42:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
server.addBlockTimer(server.epochTimer)
|
|
|
|
|
|
|
|
// initialize emission timer
|
|
|
|
emissionTimer := newEmissionTimer(&emitTimerArgs{
|
|
|
|
ap: alphabetProcessor,
|
|
|
|
emitDuration: cfg.GetUint32("timers.emit"),
|
|
|
|
})
|
|
|
|
|
|
|
|
server.addBlockTimer(emissionTimer)
|
|
|
|
|
2021-05-04 08:50:13 +00:00
|
|
|
// initialize notary timers
|
|
|
|
if !server.mainNotaryConfig.disabled {
|
|
|
|
mainNotaryTimer := newNotaryDepositTimer(¬aryDepositArgs{
|
|
|
|
l: log,
|
|
|
|
depositor: server.depositMainNotary,
|
|
|
|
notaryDuration: server.mainNotaryConfig.duration,
|
|
|
|
})
|
|
|
|
|
|
|
|
server.addBlockTimer(mainNotaryTimer)
|
|
|
|
}
|
2021-02-24 15:46:01 +00:00
|
|
|
|
2021-05-04 08:50:13 +00:00
|
|
|
if !server.sideNotaryConfig.disabled {
|
|
|
|
sideNotaryTimer := newNotaryDepositTimer(¬aryDepositArgs{
|
2021-04-29 13:50:46 +00:00
|
|
|
l: log,
|
2021-05-04 08:50:13 +00:00
|
|
|
depositor: server.depositSideNotary,
|
|
|
|
notaryDuration: server.sideNotaryConfig.duration,
|
2021-04-29 13:50:46 +00:00
|
|
|
})
|
2021-02-24 15:46:01 +00:00
|
|
|
|
2021-05-04 08:50:13 +00:00
|
|
|
server.addBlockTimer(sideNotaryTimer)
|
2021-04-29 13:50:46 +00:00
|
|
|
}
|
2021-02-24 15:46:01 +00:00
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createListener(ctx context.Context, p *chainParams) (event.Listener, error) {
|
|
|
|
sub, err := subscriber.New(ctx, &subscriber.Params{
|
|
|
|
Log: p.log,
|
|
|
|
Endpoint: p.cfg.GetString(p.name + ".endpoint.notification"),
|
2021-03-13 15:22:27 +00:00
|
|
|
DialTimeout: p.cfg.GetDuration(p.name + ".dial_timeout"),
|
2020-07-24 13:54:03 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
listener, err := event.NewListener(event.ListenerParams{
|
|
|
|
Logger: p.log,
|
|
|
|
Subscriber: sub,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return listener, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func createClient(ctx context.Context, p *chainParams) (*client.Client, error) {
|
|
|
|
return client.New(
|
|
|
|
p.key,
|
|
|
|
p.cfg.GetString(p.name+".endpoint.client"),
|
|
|
|
client.WithContext(ctx),
|
|
|
|
client.WithLogger(p.log),
|
2021-03-13 15:22:27 +00:00
|
|
|
client.WithDialTimeout(p.cfg.GetDuration(p.name+".dial_timeout")),
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseContracts(cfg *viper.Viper) (*contracts, error) {
|
|
|
|
var (
|
|
|
|
result = new(contracts)
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
netmapContractStr := cfg.GetString("contracts.netmap")
|
|
|
|
neofsContractStr := cfg.GetString("contracts.neofs")
|
|
|
|
balanceContractStr := cfg.GetString("contracts.balance")
|
2020-08-11 12:14:08 +00:00
|
|
|
containerContractStr := cfg.GetString("contracts.container")
|
2020-12-18 09:27:19 +00:00
|
|
|
auditContractStr := cfg.GetString("contracts.audit")
|
2021-02-20 10:44:51 +00:00
|
|
|
proxyContractStr := cfg.GetString("contracts.proxy")
|
2021-04-20 15:16:40 +00:00
|
|
|
processingContractStr := cfg.GetString("contracts.processing")
|
2021-03-31 15:41:47 +00:00
|
|
|
reputationContractStr := cfg.GetString("contracts.reputation")
|
2021-05-19 12:46:45 +00:00
|
|
|
neofsIDContractStr := cfg.GetString("contracts.neofsid")
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
result.netmap, err = util.Uint160DecodeStringLE(netmapContractStr)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("ir: can't read netmap script-hash: %w", err)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result.neofs, err = util.Uint160DecodeStringLE(neofsContractStr)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("ir: can't read neofs script-hash: %w", err)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result.balance, err = util.Uint160DecodeStringLE(balanceContractStr)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("ir: can't read balance script-hash: %w", err)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 12:14:08 +00:00
|
|
|
result.container, err = util.Uint160DecodeStringLE(containerContractStr)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("ir: can't read container script-hash: %w", err)
|
2020-08-11 12:14:08 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 09:27:19 +00:00
|
|
|
result.audit, err = util.Uint160DecodeStringLE(auditContractStr)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("ir: can't read audit script-hash: %w", err)
|
2020-12-18 09:27:19 +00:00
|
|
|
}
|
|
|
|
|
2021-02-20 10:44:51 +00:00
|
|
|
result.proxy, err = util.Uint160DecodeStringLE(proxyContractStr)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("ir: can't read proxy script-hash: %w", err)
|
2021-02-20 10:44:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 15:16:40 +00:00
|
|
|
result.processing, err = util.Uint160DecodeStringLE(processingContractStr)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("ir: can't read processing script-hash: %w", err)
|
2021-04-20 15:16:40 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 15:41:47 +00:00
|
|
|
result.reputation, err = util.Uint160DecodeStringLE(reputationContractStr)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("ir: can't read reputation script-hash: %w", err)
|
2021-03-31 15:41:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 12:46:45 +00:00
|
|
|
result.neofsID, err = util.Uint160DecodeStringLE(neofsIDContractStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("ir: can't read NeoFS ID script-hash: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-10-12 09:29:27 +00:00
|
|
|
result.alphabet, err = parseAlphabetContracts(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-21 06:53:18 +00:00
|
|
|
func parseAlphabetContracts(cfg *viper.Viper) (alphabetContracts, error) {
|
|
|
|
num := glagoliticLetter(cfg.GetUint("contracts.alphabet.amount"))
|
|
|
|
alpha := newAlphabetContracts()
|
|
|
|
|
|
|
|
if num > lastLetterNum {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("amount of alphabet contracts overflows glagolitsa %d > %d", num, lastLetterNum)
|
2021-02-21 06:53:18 +00:00
|
|
|
}
|
2020-10-12 09:29:27 +00:00
|
|
|
|
2021-02-21 06:53:18 +00:00
|
|
|
for letter := az; letter < num; letter++ {
|
|
|
|
contractStr := cfg.GetString("contracts.alphabet." + letter.configString())
|
2020-10-12 09:29:27 +00:00
|
|
|
|
2021-02-21 06:53:18 +00:00
|
|
|
contractHash, err := util.Uint160DecodeStringLE(contractStr)
|
2020-10-12 09:29:27 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("invalid alphabet %s contract: %s: %w", letter.configString(), contractStr, err)
|
2020-10-12 09:29:27 +00:00
|
|
|
}
|
2021-02-21 06:53:18 +00:00
|
|
|
|
|
|
|
alpha.set(letter, contractHash)
|
2020-10-12 09:29:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-21 06:53:18 +00:00
|
|
|
return alpha, nil
|
2020-10-12 09:29:27 +00:00
|
|
|
}
|
|
|
|
|
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-21 07:45:15 +00:00
|
|
|
val, err := s.morphClient.TestInvoke(s.contracts.netmap, getEpochMethod)
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("can't read epoch: %w", err)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 07:45:15 +00:00
|
|
|
epoch, err := client.IntFromStackItem(val[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't parse epoch: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-10-27 10:49:34 +00:00
|
|
|
// get balance precision
|
2021-05-21 07:45:15 +00:00
|
|
|
v, err := s.morphClient.TestInvoke(s.contracts.balance, precisionMethod)
|
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-05-21 07:45:15 +00:00
|
|
|
balancePrecision, err := client.IntFromStackItem(v[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't parse balance contract precision: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
s.epochCounter.Store(uint64(epoch))
|
2021-05-21 07:45:15 +00:00
|
|
|
s.precision.SetBalancePrecision(uint32(balancePrecision))
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
s.log.Debug("read config from blockchain",
|
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()),
|
2020-07-24 13:54:03 +00:00
|
|
|
zap.Int64("epoch", epoch),
|
2021-05-21 07:45:15 +00:00
|
|
|
zap.Uint32("precision", uint32(balancePrecision)),
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-22 15:49:52 +00:00
|
|
|
|
2021-01-29 07:42:40 +00:00
|
|
|
// onlyActiveHandler wrapper around event handler that executes it
|
|
|
|
// only if inner ring node state is active.
|
2021-01-26 10:33:28 +00:00
|
|
|
func (s *Server) onlyActiveEventHandler(f event.Handler) event.Handler {
|
|
|
|
return func(ev event.Event) {
|
|
|
|
if s.IsActive() {
|
|
|
|
f(ev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-24 15:46:01 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|