From f11ae1035dc9aa2640af4983be24d43d47a96a2b Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 8 Sep 2020 12:01:38 +0300 Subject: [PATCH] [#21] ir: Add config relay processor Signed-off-by: Alex Vanin --- pkg/innerring/processors/neofs/handlers.go | 17 ++++++++++++ .../processors/neofs/process_config.go | 26 +++++++++++++++++++ pkg/innerring/processors/neofs/processor.go | 15 +++++++++++ 3 files changed, 58 insertions(+) create mode 100644 pkg/innerring/processors/neofs/process_config.go diff --git a/pkg/innerring/processors/neofs/handlers.go b/pkg/innerring/processors/neofs/handlers.go index a64f6e771..d956295cf 100644 --- a/pkg/innerring/processors/neofs/handlers.go +++ b/pkg/innerring/processors/neofs/handlers.go @@ -55,3 +55,20 @@ func (np *Processor) handleCheque(ev event.Event) { zap.Int("capacity", np.pool.Cap())) } } + +func (np *Processor) handleConfig(ev event.Event) { + cfg := ev.(neofsEvent.Config) // todo: check panic in production + np.log.Info("notification", + zap.String("type", "set config"), + zap.String("key", hex.EncodeToString(cfg.Key())), + zap.String("value", hex.EncodeToString(cfg.Value()))) + + // send event to the worker pool + + err := np.pool.Submit(func() { np.processConfig(&cfg) }) + if err != nil { + // todo: move into controlled degradation stage + np.log.Warn("neofs processor worker pool drained", + zap.Int("capacity", np.pool.Cap())) + } +} diff --git a/pkg/innerring/processors/neofs/process_config.go b/pkg/innerring/processors/neofs/process_config.go new file mode 100644 index 000000000..407884640 --- /dev/null +++ b/pkg/innerring/processors/neofs/process_config.go @@ -0,0 +1,26 @@ +package neofs + +import ( + "github.com/nspcc-dev/neofs-node/pkg/innerring/invoke" + neofsEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/neofs" + "go.uber.org/zap" +) + +// Process config event by setting configuration value from main chain in +// side chain. +func (np *Processor) processConfig(config *neofsEvent.Config) { + if !np.activeState.IsActive() { + np.log.Info("passive mode, ignore deposit") + return + } + + err := invoke.SetConfig(np.morphClient, np.netmapContract, + &invoke.SetConfigArgs{ + Key: config.Key(), + Value: config.Value(), + }, + ) + if err != nil { + np.log.Error("can't relay set config event", zap.Error(err)) + } +} diff --git a/pkg/innerring/processors/neofs/processor.go b/pkg/innerring/processors/neofs/processor.go index b4eca268a..9541d0189 100644 --- a/pkg/innerring/processors/neofs/processor.go +++ b/pkg/innerring/processors/neofs/processor.go @@ -50,6 +50,7 @@ const ( depositNotification = "Deposit" withdrawNotification = "Withdraw" chequeNotification = "Cheque" + configNotification = "SetConfig" ) // New creates neofs mainnet contract processor instance. @@ -109,6 +110,13 @@ func (np *Processor) ListenerParsers() []event.ParserInfo { cheque.SetParser(neofsEvent.ParseCheque) parsers = append(parsers, cheque) + // config event + config := event.ParserInfo{} + config.SetType(configNotification) + config.SetScriptHash(np.neofsContract) + config.SetParser(neofsEvent.ParseConfig) + parsers = append(parsers, config) + return parsers } @@ -137,6 +145,13 @@ func (np *Processor) ListenerHandlers() []event.HandlerInfo { cheque.SetHandler(np.handleCheque) handlers = append(handlers, cheque) + // config handler + config := event.HandlerInfo{} + config.SetType(configNotification) + config.SetScriptHash(np.neofsContract) + config.SetHandler(np.handleConfig) + handlers = append(handlers, config) + return handlers }