From 3fe5962e926c165fdd07febf1b11717d2a643952 Mon Sep 17 00:00:00 2001
From: Alex Vanin <alexey@nspcc.ru>
Date: Thu, 25 Mar 2021 16:24:44 +0300
Subject: [PATCH] [#447] innerring: Use governance processor in inner ring

Network map processors creates sync event once per epoch.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
---
 pkg/innerring/innerring.go                     | 18 +++++++++++++++++-
 .../processors/netmap/process_epoch.go         |  2 ++
 pkg/innerring/processors/netmap/processor.go   |  6 ++++++
 pkg/innerring/state.go                         | 11 ++++++++++-
 4 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go
index 5f6cb9491..fdfa8d8c9 100644
--- a/pkg/innerring/innerring.go
+++ b/pkg/innerring/innerring.go
@@ -16,6 +16,7 @@ import (
 	"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/audit"
 	"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/balance"
 	"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/container"
+	"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/governance"
 	"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/neofs"
 	"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/netmap"
 	"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement"
@@ -423,6 +424,20 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
 		return nil, err
 	}
 
+	// create governance processor
+	governanceProcessor, err := governance.New(&governance.Params{
+		Log:           log,
+		NeoFSContract: server.contracts.neofs,
+		AlphabetState: server,
+		EpochState:    server,
+		Voter:         server,
+		MorphClient:   server.morphClient,
+		MainnetClient: server.mainnetClient,
+	})
+	if err != nil {
+		return nil, err
+	}
+
 	// create netmap processor
 	netmapProcessor, err := netmap.New(&netmap.Params{
 		Log:              log,
@@ -441,7 +456,8 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
 		AuditSettlementsHandler: server.onlyAlphabetEventHandler(
 			settlementProcessor.HandleAuditEvent,
 		),
-		NodeValidator: locodeValidator,
+		AlphabetSyncHandler: governanceProcessor.HandleAlphabetSync,
+		NodeValidator:       locodeValidator,
 	})
 	if err != nil {
 		return nil, err
diff --git a/pkg/innerring/processors/netmap/process_epoch.go b/pkg/innerring/processors/netmap/process_epoch.go
index a1f6517f8..ac2bb6b87 100644
--- a/pkg/innerring/processors/netmap/process_epoch.go
+++ b/pkg/innerring/processors/netmap/process_epoch.go
@@ -3,6 +3,7 @@ package netmap
 import (
 	"github.com/nspcc-dev/neofs-node/pkg/innerring/invoke"
 	"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/audit"
+	"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/governance"
 	"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement"
 	"go.uber.org/zap"
 )
@@ -38,6 +39,7 @@ func (np *Processor) processNewEpoch(epoch uint64) {
 	np.handleCleanupTick(netmapCleanupTick{epoch: epoch})
 	np.handleNewAudit(audit.NewAuditStartEvent(epoch))
 	np.handleAuditSettlements(settlement.NewAuditEvent(epoch))
+	np.handleAlphabetSync(governance.NewSyncEvent())
 }
 
 // Process new epoch tick by invoking new epoch method in network map contract.
diff --git a/pkg/innerring/processors/netmap/processor.go b/pkg/innerring/processors/netmap/processor.go
index 9cc62d0b7..2f480665d 100644
--- a/pkg/innerring/processors/netmap/processor.go
+++ b/pkg/innerring/processors/netmap/processor.go
@@ -61,6 +61,7 @@ type (
 
 		handleNewAudit         event.Handler
 		handleAuditSettlements event.Handler
+		handleAlphabetSync     event.Handler
 
 		nodeValidator NodeValidator
 	}
@@ -80,6 +81,7 @@ type (
 
 		HandleAudit             event.Handler
 		AuditSettlementsHandler event.Handler
+		AlphabetSyncHandler     event.Handler
 
 		NodeValidator NodeValidator
 	}
@@ -108,6 +110,8 @@ func New(p *Params) (*Processor, error) {
 		return nil, errors.New("ir/netmap: audit handler is not set")
 	case p.AuditSettlementsHandler == nil:
 		return nil, errors.New("ir/netmap: audit settlement handler is not set")
+	case p.AlphabetSyncHandler == nil:
+		return nil, errors.New("ir/netmap: alphabet sync handler is not set")
 	case p.ContainerWrapper == nil:
 		return nil, errors.New("ir/netmap: container contract wrapper is not set")
 	case p.NodeValidator == nil:
@@ -135,6 +139,8 @@ func New(p *Params) (*Processor, error) {
 
 		handleAuditSettlements: p.AuditSettlementsHandler,
 
+		handleAlphabetSync: p.AlphabetSyncHandler,
+
 		nodeValidator: p.NodeValidator,
 	}, nil
 }
diff --git a/pkg/innerring/state.go b/pkg/innerring/state.go
index 612a958b7..cafa3e6c2 100644
--- a/pkg/innerring/state.go
+++ b/pkg/innerring/state.go
@@ -1,6 +1,8 @@
 package innerring
 
 import (
+	"sort"
+
 	"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
 	"github.com/nspcc-dev/neo-go/pkg/util"
 	"github.com/nspcc-dev/neofs-node/pkg/innerring/invoke"
@@ -95,13 +97,20 @@ func (s *Server) voteForSidechainValidator(validators keys.PublicKeys) error {
 
 // InitAndVoteForSidechainValidator is a public function to use outside of
 // inner ring daemon execution. It initialize inner ring structure with data
-// from blockchain and then calls vote method on corresponding alphabet contract.
+// from blockchain and then calls vote method on alphabet contracts.
 func (s *Server) InitAndVoteForSidechainValidator(validators keys.PublicKeys) error {
 	err := s.initConfigFromBlockchain()
 	if err != nil {
 		return err
 	}
 
+	return s.VoteForSidechainValidator(validators)
+}
+
+// VoteForSidechainValidator calls vote method on alphabet contracts with
+// provided list of keys.
+func (s *Server) VoteForSidechainValidator(validators keys.PublicKeys) error {
+	sort.Sort(validators)
 	return s.voteForSidechainValidator(validators)
 }