From 4aee3de24e9f6f3ad71d9f9ef89ed81e98aec8b1 Mon Sep 17 00:00:00 2001
From: Alex Vanin <alexey@nspcc.ru>
Date: Tue, 8 Sep 2020 12:06:34 +0300
Subject: [PATCH] [#21] ir: Add inner ring list relay processor

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
---
 pkg/innerring/processors/neofs/handlers.go    | 16 +++++++++++++
 .../processors/neofs/process_update.go        | 23 +++++++++++++++++++
 pkg/innerring/processors/neofs/processor.go   | 15 ++++++++++++
 3 files changed, 54 insertions(+)
 create mode 100644 pkg/innerring/processors/neofs/process_update.go

diff --git a/pkg/innerring/processors/neofs/handlers.go b/pkg/innerring/processors/neofs/handlers.go
index d956295cf..487b02cac 100644
--- a/pkg/innerring/processors/neofs/handlers.go
+++ b/pkg/innerring/processors/neofs/handlers.go
@@ -72,3 +72,19 @@ func (np *Processor) handleConfig(ev event.Event) {
 			zap.Int("capacity", np.pool.Cap()))
 	}
 }
+
+func (np *Processor) handleUpdateInnerRing(ev event.Event) {
+	updIR := ev.(neofsEvent.UpdateInnerRing) // todo: check panic in production
+	np.log.Info("notification",
+		zap.String("type", "update inner ring"),
+	)
+
+	// send event to the worker pool
+
+	err := np.pool.Submit(func() { np.processUpdateInnerRing(&updIR) })
+	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_update.go b/pkg/innerring/processors/neofs/process_update.go
new file mode 100644
index 000000000..c5a562021
--- /dev/null
+++ b/pkg/innerring/processors/neofs/process_update.go
@@ -0,0 +1,23 @@
+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 update inner ring event by setting inner ring list value from
+// main chain in side chain.
+func (np *Processor) processUpdateInnerRing(list *neofsEvent.UpdateInnerRing) {
+	if !np.activeState.IsActive() {
+		np.log.Info("passive mode, ignore deposit")
+		return
+	}
+
+	err := invoke.UpdateInnerRing(np.morphClient, np.netmapContract,
+		list.Keys(),
+	)
+	if err != nil {
+		np.log.Error("can't relay update inner ring event", zap.Error(err))
+	}
+}
diff --git a/pkg/innerring/processors/neofs/processor.go b/pkg/innerring/processors/neofs/processor.go
index 9541d0189..5d2d08818 100644
--- a/pkg/innerring/processors/neofs/processor.go
+++ b/pkg/innerring/processors/neofs/processor.go
@@ -51,6 +51,7 @@ const (
 	withdrawNotification = "Withdraw"
 	chequeNotification   = "Cheque"
 	configNotification   = "SetConfig"
+	updateIRNotification = "InnerRingUpdate"
 )
 
 // New creates neofs mainnet contract processor instance.
@@ -117,6 +118,13 @@ func (np *Processor) ListenerParsers() []event.ParserInfo {
 	config.SetParser(neofsEvent.ParseConfig)
 	parsers = append(parsers, config)
 
+	// update inner ring event
+	updateIR := event.ParserInfo{}
+	updateIR.SetType(updateIRNotification)
+	updateIR.SetScriptHash(np.neofsContract)
+	updateIR.SetParser(neofsEvent.ParseUpdateInnerRing)
+	parsers = append(parsers, updateIR)
+
 	return parsers
 }
 
@@ -152,6 +160,13 @@ func (np *Processor) ListenerHandlers() []event.HandlerInfo {
 	config.SetHandler(np.handleConfig)
 	handlers = append(handlers, config)
 
+	// updateIR handler
+	updateIR := event.HandlerInfo{}
+	updateIR.SetType(updateIRNotification)
+	updateIR.SetScriptHash(np.neofsContract)
+	updateIR.SetHandler(np.handleUpdateInnerRing)
+	handlers = append(handlers, updateIR)
+
 	return handlers
 }