[#502] innerring: synchronize validators on mainnet alphabet update

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-05-18 10:40:21 +03:00 committed by Alex Vanin
parent 7eab752923
commit 2e31cd34e6
6 changed files with 98 additions and 4 deletions

View file

@ -490,6 +490,10 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
} }
} else { } else {
alphaSync = governanceProcessor.HandleAlphabetSync alphaSync = governanceProcessor.HandleAlphabetSync
err = bindMainnetProcessor(governanceProcessor, server)
if err != nil {
return nil, err
}
} }
// create netmap processor // create netmap processor

View file

@ -1,12 +1,29 @@
package governance package governance
import ( import (
"github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
"github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/nspcc-dev/neofs-node/pkg/morph/event"
"github.com/nspcc-dev/neofs-node/pkg/morph/event/rolemanagement"
"go.uber.org/zap" "go.uber.org/zap"
) )
func (gp *Processor) HandleAlphabetSync(_ event.Event) { func (gp *Processor) HandleAlphabetSync(e event.Event) {
gp.log.Info("new event", zap.String("type", "sync")) var typ string
switch et := e.(type) {
case Sync:
typ = "sync"
case rolemanagement.Designate:
if et.Role != noderoles.NeoFSAlphabet {
return
}
typ = native.DesignationEventName
default:
return
}
gp.log.Info("new event", zap.String("type", typ))
// send event to the worker pool // send event to the worker pool

View file

@ -4,11 +4,13 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neofs-node/pkg/innerring/config" "github.com/nspcc-dev/neofs-node/pkg/innerring/config"
"github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/nspcc-dev/neofs-node/pkg/morph/event"
"github.com/nspcc-dev/neofs-node/pkg/morph/event/rolemanagement"
"github.com/panjf2000/ants/v2" "github.com/panjf2000/ants/v2"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -111,12 +113,20 @@ func New(p *Params) (*Processor, error) {
// ListenerParsers for the 'event.Listener' event producer. // ListenerParsers for the 'event.Listener' event producer.
func (gp *Processor) ListenerParsers() []event.ParserInfo { func (gp *Processor) ListenerParsers() []event.ParserInfo {
return nil var pi event.ParserInfo
pi.SetScriptHash(gp.mainnetClient.GetDesignateHash())
pi.SetType(event.TypeFromString(native.DesignationEventName))
pi.SetParser(rolemanagement.ParseDesignate)
return []event.ParserInfo{pi}
} }
// ListenerHandlers for the 'event.Listener' event producer. // ListenerHandlers for the 'event.Listener' event producer.
func (gp *Processor) ListenerHandlers() []event.HandlerInfo { func (gp *Processor) ListenerHandlers() []event.HandlerInfo {
return nil var hi event.HandlerInfo
hi.SetScriptHash(gp.mainnetClient.GetDesignateHash())
hi.SetType(event.TypeFromString(native.DesignationEventName))
hi.SetHandler(gp.HandleAlphabetSync)
return []event.HandlerInfo{hi}
} }
// TimersHandlers for the 'Timers' event producer. // TimersHandlers for the 'Timers' event producer.

View file

@ -236,6 +236,11 @@ func (c *Client) NeoFSAlphabetList() (keys.PublicKeys, error) {
return list, nil return list, nil
} }
// GetDesignateHash returns hash of the native `RoleManagement` contract.
func (c *Client) GetDesignateHash() util.Uint160 {
return c.designate
}
func (c *Client) roleList(r noderoles.Role) (keys.PublicKeys, error) { func (c *Client) roleList(r noderoles.Role) (keys.PublicKeys, error) {
height, err := c.client.GetBlockCount() height, err := c.client.GetBlockCount()
if err != nil { if err != nil {

View file

@ -0,0 +1,31 @@
package rolemanagement
import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
)
// Designate represents designation event of the mainnet RoleManagement contract.
type Designate struct {
Role noderoles.Role
}
// MorphEvent implements Neo:Morph Event interface.
func (Designate) MorphEvent() {}
// ParseDesignate from notification into container event structure.
func ParseDesignate(params []stackitem.Item) (event.Event, error) {
if len(params) != 2 {
return nil, event.WrongNumberOfParameters(2, len(params))
}
bi, err := params[0].TryInteger()
if err != nil {
return nil, fmt.Errorf("invalid stackitem type: %w", err)
}
return Designate{Role: noderoles.Role(bi.Int64())}, nil
}

View file

@ -0,0 +1,27 @@
package rolemanagement
import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
func TestParseRoleUpdate(t *testing.T) {
t.Run("wrong number of arguments", func(t *testing.T) {
_, err := ParseDesignate([]stackitem.Item{})
require.Error(t, err)
})
t.Run("invalid item type", func(t *testing.T) {
args := []stackitem.Item{stackitem.NewMap(), stackitem.Make(123)}
_, err := ParseDesignate(args)
require.Error(t, err)
})
t.Run("good", func(t *testing.T) {
args := []stackitem.Item{stackitem.Make(int(noderoles.NeoFSAlphabet)), stackitem.Make(123)}
e, err := ParseDesignate(args)
require.NoError(t, err)
require.Equal(t, noderoles.NeoFSAlphabet, e.(Designate).Role)
})
}