[#72] Init inner ring configuration before run

This way we can re-initialize application in any given
moment, which is useful for reconnects.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-10-13 17:25:25 +03:00 committed by Alex Vanin
parent a9d296faa8
commit d3d8f00757

View file

@ -40,7 +40,9 @@ type (
epochCounter atomic.Uint64 epochCounter atomic.Uint64
innerRingIndex atomic.Int32 innerRingIndex atomic.Int32
// todo: export error channel // internal variables
key *ecdsa.PrivateKey
contracts *contracts
} }
contracts struct { contracts struct {
@ -74,6 +76,11 @@ const (
// Start runs all event providers. // Start runs all event providers.
func (s *Server) Start(ctx context.Context) error { func (s *Server) Start(ctx context.Context) error {
err := s.initConfigFromBlockchain()
if err != nil {
return err
}
s.localTimers.Start(ctx) // local timers start ticking s.localTimers.Start(ctx) // local timers start ticking
go s.morphListener.Listen(ctx) // listen for neo:morph events go s.morphListener.Listen(ctx) // listen for neo:morph events
@ -90,16 +97,17 @@ func (s *Server) Stop() {
// New creates instance of inner ring sever structure. // New creates instance of inner ring sever structure.
func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error) { func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error) {
var err error
server := &Server{log: log} server := &Server{log: log}
// prepare inner ring node private key // prepare inner ring node private key
key, err := crypto.LoadPrivateKey(cfg.GetString("key")) server.key, err = crypto.LoadPrivateKey(cfg.GetString("key"))
if err != nil { if err != nil {
return nil, errors.Wrap(err, "ir: can't create private key") return nil, errors.Wrap(err, "ir: can't create private key")
} }
// get all script hashes of contracts // get all script hashes of contracts
contracts, err := parseContracts(cfg) server.contracts, err = parseContracts(cfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -114,8 +122,8 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
morphChain := &chainParams{ morphChain := &chainParams{
log: log, log: log,
cfg: cfg, cfg: cfg,
key: key, key: server.key,
gas: contracts.gas, gas: server.contracts.gas,
name: morphPrefix, name: morphPrefix,
} }
@ -150,7 +158,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
netmapProcessor, err := netmap.New(&netmap.Params{ netmapProcessor, err := netmap.New(&netmap.Params{
Log: log, Log: log,
PoolSize: cfg.GetInt("workers.netmap"), PoolSize: cfg.GetInt("workers.netmap"),
NetmapContract: contracts.netmap, NetmapContract: server.contracts.netmap,
EpochTimer: server.localTimers, EpochTimer: server.localTimers,
MorphClient: server.morphClient, MorphClient: server.morphClient,
EpochState: server, EpochState: server,
@ -169,7 +177,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
containerProcessor, err := container.New(&container.Params{ containerProcessor, err := container.New(&container.Params{
Log: log, Log: log,
PoolSize: cfg.GetInt("workers.container"), PoolSize: cfg.GetInt("workers.container"),
ContainerContract: contracts.container, ContainerContract: server.contracts.container,
MorphClient: server.morphClient, MorphClient: server.morphClient,
ActiveState: server, ActiveState: server,
}) })
@ -186,8 +194,8 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
balanceProcessor, err := balance.New(&balance.Params{ balanceProcessor, err := balance.New(&balance.Params{
Log: log, Log: log,
PoolSize: cfg.GetInt("workers.balance"), PoolSize: cfg.GetInt("workers.balance"),
NeoFSContract: contracts.neofs, NeoFSContract: server.contracts.neofs,
BalanceContract: contracts.balance, BalanceContract: server.contracts.balance,
MainnetClient: server.mainnetClient, MainnetClient: server.mainnetClient,
ActiveState: server, ActiveState: server,
}) })
@ -206,9 +214,9 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
neofsProcessor, err := neofs.New(&neofs.Params{ neofsProcessor, err := neofs.New(&neofs.Params{
Log: log, Log: log,
PoolSize: cfg.GetInt("workers.neofs"), PoolSize: cfg.GetInt("workers.neofs"),
NeoFSContract: contracts.neofs, NeoFSContract: server.contracts.neofs,
BalanceContract: contracts.balance, BalanceContract: server.contracts.balance,
NetmapContract: contracts.netmap, NetmapContract: server.contracts.netmap,
MorphClient: server.morphClient, MorphClient: server.morphClient,
EpochState: server, EpochState: server,
ActiveState: server, ActiveState: server,
@ -226,7 +234,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
alphabetProcessor, err := alphabet.New(&alphabet.Params{ alphabetProcessor, err := alphabet.New(&alphabet.Params{
Log: log, Log: log,
PoolSize: cfg.GetInt("workers.alphabet"), PoolSize: cfg.GetInt("workers.alphabet"),
AlphabetContracts: contracts.alphabet, AlphabetContracts: server.contracts.alphabet,
MorphClient: server.morphClient, MorphClient: server.morphClient,
IRList: server, IRList: server,
}) })
@ -242,11 +250,6 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
// todo: create vivid id component // todo: create vivid id component
// todo: create audit scheduler // todo: create audit scheduler
err = initConfigFromBlockchain(server, contracts, &key.PublicKey)
if err != nil {
return nil, errors.Wrap(err, "initializing error")
}
return server, nil return server, nil
} }
@ -344,15 +347,17 @@ func parseAlphabetContracts(cfg *viper.Viper) (res [7]util.Uint160, err error) {
return res, nil return res, nil
} }
func initConfigFromBlockchain(s *Server, c *contracts, key *ecdsa.PublicKey) error { func (s *Server) initConfigFromBlockchain() error {
// get current epoch // get current epoch
epoch, err := invoke.Epoch(s.morphClient, c.netmap) epoch, err := invoke.Epoch(s.morphClient, s.contracts.netmap)
if err != nil { if err != nil {
return errors.Wrap(err, "can't read epoch") return errors.Wrap(err, "can't read epoch")
} }
key := &s.key.PublicKey
// check if node inside inner ring list and what index it has // check if node inside inner ring list and what index it has
index, err := invoke.InnerRingIndex(s.mainnetClient, c.neofs, key) index, err := invoke.InnerRingIndex(s.mainnetClient, s.contracts.neofs, key)
if err != nil { if err != nil {
return errors.Wrap(err, "can't read inner ring list") return errors.Wrap(err, "can't read inner ring list")
} }