2020-07-24 13:54:03 +00:00
|
|
|
package innerring
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
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"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/invoke"
|
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"
|
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-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/innerring/timers"
|
|
|
|
"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-01-26 10:33:28 +00:00
|
|
|
balanceWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance/wrapper"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
2021-01-26 10:33:28 +00:00
|
|
|
netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/subscriber"
|
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/pkg/errors"
|
|
|
|
"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-01-22 15:49:52 +00:00
|
|
|
blockTimers []*timers.BlockTimer
|
2021-01-22 15:01:44 +00:00
|
|
|
epochTimer *timers.BlockTimer
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
// global state
|
2020-10-12 09:13:19 +00:00
|
|
|
morphClient *client.Client
|
|
|
|
mainnetClient *client.Client
|
|
|
|
epochCounter atomic.Uint64
|
|
|
|
innerRingIndex atomic.Int32
|
2020-12-18 12:52:27 +00:00
|
|
|
innerRingSize atomic.Int32
|
2020-10-27 10:49:34 +00:00
|
|
|
precision precision.Fixed8Converter
|
2020-12-22 00:28:42 +00:00
|
|
|
auditClient *auditWrapper.ClientWrapper
|
2021-01-26 10:33:28 +00:00
|
|
|
balanceClient *balanceWrapper.Wrapper
|
2020-07-24 13:54:03 +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
|
|
|
|
predefinedValidators []keys.PublicKey
|
2020-12-22 00:28:42 +00:00
|
|
|
|
|
|
|
workers []func(context.Context)
|
2021-01-26 10:33:28 +00:00
|
|
|
|
|
|
|
auditSettlement *auditSettlement.Calculator
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contracts struct {
|
2020-12-29 15:50:41 +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
|
|
|
|
gas util.Uint160 // native contract in both chains
|
2020-10-12 09:29:27 +00:00
|
|
|
|
|
|
|
alphabet [alphabetContractsN]util.Uint160 // 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"
|
2020-10-12 09:29:27 +00:00
|
|
|
|
|
|
|
alphabetContractsN = 7 // az, buky, vedi, glagoli, dobro, jest, zhivete
|
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 {
|
2020-10-13 14:25:25 +00:00
|
|
|
err := s.initConfigFromBlockchain()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
intError <- errors.Wrap(err, "sidechain")
|
|
|
|
case err := <-mainnnetErr:
|
|
|
|
intError <- errors.Wrap(err, "mainnet")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-01-25 14:23:37 +00:00
|
|
|
// BUG: https://github.com/nspcc-dev/neofs-node/issues/346
|
|
|
|
s.mainnetListener.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 {
|
|
|
|
return errors.Wrap(err, "could not start block timers")
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return nil, errors.Wrap(err, "ir: can't create private key")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return nil, errors.Wrap(err, "ir: can't parse predefined validators list")
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
gas: server.contracts.gas,
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
mainnetChain := morphChain
|
|
|
|
mainnetChain.name = mainnetPrefix
|
|
|
|
|
|
|
|
// create mainnet listener
|
|
|
|
server.mainnetListener, err = createListener(ctx, mainnetChain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create mainnet client
|
|
|
|
server.mainnetClient, err = createClient(ctx, mainnetChain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-22 00:28:42 +00:00
|
|
|
server.pubKey = crypto.MarshalPublicKey(&server.key.PublicKey)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-22 11:33:39 +00:00
|
|
|
server.auditClient, err = invoke.NewNoFeeAuditClient(server.morphClient, server.contracts.audit)
|
2020-12-22 00:28:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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,
|
2020-12-22 12:14:50 +00:00
|
|
|
ClientCache: clientCache,
|
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-22 15:01:44 +00:00
|
|
|
var netmapProcessor *netmap.Processor
|
|
|
|
|
|
|
|
server.epochTimer = timers.NewBlockTimer(
|
|
|
|
timers.StaticBlockMeter(cfg.GetUint32("timers.epoch")),
|
|
|
|
func() {
|
|
|
|
netmapProcessor.HandleNewEpochTick(timers.NewEpochTick{})
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-01-22 15:49:52 +00:00
|
|
|
server.addBlockTimer(server.epochTimer)
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// create netmap processor
|
2021-01-22 15:01:44 +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-22 15:01:44 +00:00
|
|
|
EpochTimer: (*blockTimerWrapper)(server.epochTimer),
|
2020-10-29 16:09:45 +00:00
|
|
|
MorphClient: server.morphClient,
|
|
|
|
EpochState: server,
|
|
|
|
ActiveState: server,
|
|
|
|
CleanupEnabled: cfg.GetBool("netmap_cleaner.enabled"),
|
|
|
|
CleanupThreshold: cfg.GetUint64("netmap_cleaner.threshold"),
|
2020-12-18 09:27:19 +00:00
|
|
|
HandleAudit: auditProcessor.StartAuditHandler(),
|
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,
|
|
|
|
ActiveState: server,
|
|
|
|
})
|
|
|
|
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,
|
|
|
|
ActiveState: server,
|
2020-10-27 10:49:34 +00:00
|
|
|
Converter: &server.precision,
|
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{
|
2020-11-03 11:16:32 +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,
|
|
|
|
ActiveState: server,
|
|
|
|
Converter: &server.precision,
|
|
|
|
MintEmitCacheSize: cfg.GetInt("emit.mint.cache_size"),
|
|
|
|
MintEmitThreshold: cfg.GetUint64("emit.mint.threshold"),
|
2020-12-11 08:33:27 +00:00
|
|
|
MintEmitValue: fixedn.Fixed8(cfg.GetInt64("emit.mint.value")),
|
2020-07-24 13:54:03 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bindMainnetProcessor(neofsProcessor, server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-22 15:49:52 +00:00
|
|
|
var alphabetProcessor *alphabet.Processor
|
|
|
|
|
|
|
|
server.addBlockTimer(timers.NewBlockTimer(
|
|
|
|
timers.StaticBlockMeter(cfg.GetUint32("timers.emit")),
|
|
|
|
func() {
|
|
|
|
alphabetProcessor.HandleGasEmission(timers.NewAlphabetEmitTick{})
|
|
|
|
},
|
|
|
|
))
|
|
|
|
|
2020-10-12 10:26:05 +00:00
|
|
|
// create alphabet processor
|
2021-01-22 15:49:52 +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-01-26 10:33:28 +00:00
|
|
|
cnrClient, err := invoke.NewNoFeeContainerClient(server.morphClient, server.contracts.container)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nmClient, err := invoke.NewNoFeeNetmapClient(server.morphClient, server.contracts.netmap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
balClient, err := invoke.NewNoFeeBalanceClient(server.morphClient, server.contracts.balance)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
auditCalcDeps := &auditSettlementDeps{
|
|
|
|
log: server.log,
|
|
|
|
cnrSrc: cnrClient,
|
|
|
|
auditClient: server.auditClient,
|
|
|
|
nmSrc: nmClient,
|
|
|
|
clientCache: clientCache,
|
|
|
|
balanceClient: balClient,
|
|
|
|
}
|
|
|
|
|
|
|
|
server.auditSettlement = auditSettlement.NewCalculator(
|
|
|
|
&auditSettlement.CalculatorPrm{
|
|
|
|
ResultStorage: auditCalcDeps,
|
|
|
|
ContainerStorage: auditCalcDeps,
|
|
|
|
PlacementCalculator: auditCalcDeps,
|
|
|
|
SGStorage: auditCalcDeps,
|
|
|
|
AccountStorage: auditCalcDeps,
|
|
|
|
Exchanger: auditCalcDeps,
|
|
|
|
},
|
|
|
|
auditSettlement.WithLogger(server.log),
|
|
|
|
)
|
|
|
|
|
|
|
|
server.subscribeNewEpoch(func(e netmapEvent.NewEpoch) {
|
|
|
|
server.auditSettlement.Calculate(&auditSettlement.CalculatePrm{
|
|
|
|
Epoch: e.EpochNumber(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// todo: create vivid id component
|
|
|
|
|
|
|
|
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"),
|
|
|
|
DialTimeout: p.cfg.GetDuration(p.name + ".dial_timeouts"),
|
|
|
|
})
|
|
|
|
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),
|
|
|
|
client.WithDialTimeout(p.cfg.GetDuration(p.name+".dial_timeouts")),
|
|
|
|
client.WithGasContract(p.gas),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
nativeGasContractStr := cfg.GetString("contracts.gas")
|
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")
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
result.netmap, err = util.Uint160DecodeStringLE(netmapContractStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "ir: can't read netmap script-hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
result.neofs, err = util.Uint160DecodeStringLE(neofsContractStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "ir: can't read neofs script-hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
result.balance, err = util.Uint160DecodeStringLE(balanceContractStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "ir: can't read balance script-hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
result.gas, err = util.Uint160DecodeStringLE(nativeGasContractStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "ir: can't read native gas script-hash")
|
|
|
|
}
|
|
|
|
|
2020-08-11 12:14:08 +00:00
|
|
|
result.container, err = util.Uint160DecodeStringLE(containerContractStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "ir: can't read container script-hash")
|
|
|
|
}
|
|
|
|
|
2020-12-18 09:27:19 +00:00
|
|
|
result.audit, err = util.Uint160DecodeStringLE(auditContractStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "ir: can't read audit script-hash")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-13 11:47:23 +00:00
|
|
|
func parsePredefinedValidators(cfg *viper.Viper) ([]keys.PublicKey, error) {
|
|
|
|
publicKeyStrings := cfg.GetStringSlice("morph.validators")
|
|
|
|
|
|
|
|
return ParsePublicKeysFromStrings(publicKeyStrings)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParsePublicKeysFromStrings returns slice of neo public keys from slice
|
|
|
|
// of hex encoded strings.
|
|
|
|
func ParsePublicKeysFromStrings(pubKeys []string) ([]keys.PublicKey, error) {
|
|
|
|
publicKeys := make([]keys.PublicKey, 0, len(pubKeys))
|
|
|
|
|
|
|
|
for i := range pubKeys {
|
|
|
|
key, err := keys.NewPublicKeyFromString(pubKeys[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't decode public key")
|
|
|
|
}
|
|
|
|
|
|
|
|
publicKeys = append(publicKeys, *key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return publicKeys, nil
|
|
|
|
}
|
|
|
|
|
2020-10-12 09:29:27 +00:00
|
|
|
func parseAlphabetContracts(cfg *viper.Viper) (res [7]util.Uint160, err error) {
|
|
|
|
// list of glagolic script letters that represent alphabet contracts
|
|
|
|
glagolic := []string{"az", "buky", "vedi", "glagoli", "dobro", "jest", "zhivete"}
|
|
|
|
|
|
|
|
for i, letter := range glagolic {
|
|
|
|
contractStr := cfg.GetString("contracts.alphabet." + letter)
|
|
|
|
|
|
|
|
res[i], err = util.Uint160DecodeStringLE(contractStr)
|
|
|
|
if err != nil {
|
|
|
|
return res, errors.Wrapf(err, "ir: can't read alphabet %s contract", letter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-10-13 14:25:25 +00:00
|
|
|
func (s *Server) initConfigFromBlockchain() error {
|
2020-07-24 13:54:03 +00:00
|
|
|
// get current epoch
|
2020-10-13 14:25:25 +00:00
|
|
|
epoch, err := invoke.Epoch(s.morphClient, s.contracts.netmap)
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
2020-09-24 07:39:06 +00:00
|
|
|
return errors.Wrap(err, "can't read epoch")
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 14:25:25 +00:00
|
|
|
key := &s.key.PublicKey
|
|
|
|
|
2020-10-12 09:13:19 +00:00
|
|
|
// check if node inside inner ring list and what index it has
|
2020-12-18 12:52:27 +00:00
|
|
|
index, size, err := invoke.InnerRingIndex(s.mainnetClient, s.contracts.neofs, key)
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
2020-09-24 07:39:06 +00:00
|
|
|
return errors.Wrap(err, "can't read inner ring list")
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 10:49:34 +00:00
|
|
|
// get balance precision
|
|
|
|
balancePrecision, err := invoke.BalancePrecision(s.morphClient, s.contracts.balance)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "can't read balance contract precision")
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
s.epochCounter.Store(uint64(epoch))
|
2020-12-18 12:52:27 +00:00
|
|
|
s.innerRingSize.Store(size)
|
2020-10-12 09:13:19 +00:00
|
|
|
s.innerRingIndex.Store(index)
|
2020-10-27 10:49:34 +00:00
|
|
|
s.precision.SetBalancePrecision(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()),
|
2020-07-24 13:54:03 +00:00
|
|
|
zap.Int64("epoch", epoch),
|
2020-10-27 10:49:34 +00:00
|
|
|
zap.Uint32("precision", balancePrecision),
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-22 15:49:52 +00:00
|
|
|
|
|
|
|
func (s *Server) addBlockTimer(t *timers.BlockTimer) {
|
|
|
|
s.blockTimers = append(s.blockTimers, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) startBlockTimers() error {
|
|
|
|
for i := range s.blockTimers {
|
|
|
|
if err := s.blockTimers[i].Reset(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) tickTimers() {
|
|
|
|
for i := range s.blockTimers {
|
|
|
|
s.blockTimers[i].Tick()
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 10:33:28 +00:00
|
|
|
|
|
|
|
func (s *Server) subscribeNewEpoch(f func(netmapEvent.NewEpoch)) {
|
|
|
|
hi := event.HandlerInfo{}
|
|
|
|
|
|
|
|
// TODO: replace and share
|
|
|
|
const newEpochNotification = "NewEpoch"
|
|
|
|
|
|
|
|
hi.SetType(event.TypeFromString(newEpochNotification))
|
|
|
|
hi.SetScriptHash(s.contracts.netmap)
|
|
|
|
hi.SetHandler(s.onlyActiveEventHandler(func(ev event.Event) {
|
|
|
|
f(ev.(netmapEvent.NewEpoch))
|
|
|
|
}))
|
|
|
|
|
|
|
|
s.morphListener.RegisterHandler(hi)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) onlyActiveEventHandler(f event.Handler) event.Handler {
|
|
|
|
return func(ev event.Event) {
|
|
|
|
if s.IsActive() {
|
|
|
|
f(ev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|