2021-11-26 12:19:20 +00:00
|
|
|
package innerring
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
2022-05-13 18:39:20 +00:00
|
|
|
"encoding/hex"
|
2021-11-26 12:19:20 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/mempoolevent"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-05-13 18:39:20 +00:00
|
|
|
neogoutil "github.com/nspcc-dev/neo-go/pkg/util"
|
2021-11-26 12:19:20 +00:00
|
|
|
irsubnet "github.com/nspcc-dev/neofs-node/pkg/innerring/processors/subnet"
|
2022-05-13 18:39:20 +00:00
|
|
|
netmapclient "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
|
2021-11-26 12:19:20 +00:00
|
|
|
morphsubnet "github.com/nspcc-dev/neofs-node/pkg/morph/client/subnet"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
|
|
|
subnetevents "github.com/nspcc-dev/neofs-node/pkg/morph/event/subnet"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util"
|
2022-05-13 18:39:20 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2021-11-26 12:19:20 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/subnet"
|
|
|
|
subnetid "github.com/nspcc-dev/neofs-sdk-go/subnet/id"
|
2022-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2021-11-26 12:19:20 +00:00
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IR server's component to handle Subnet contract notifications.
|
|
|
|
type subnetHandler struct {
|
|
|
|
workerPool util.WorkerPool
|
|
|
|
|
|
|
|
morphClient morphsubnet.Client
|
|
|
|
|
|
|
|
putValidator irsubnet.PutValidator
|
|
|
|
}
|
|
|
|
|
|
|
|
// configuration of subnet component.
|
|
|
|
type subnetConfig struct {
|
|
|
|
queueSize uint32
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// makes IR server to catch Subnet notifications from the sidechain listener,
|
|
|
|
// and to release the corresponding processing queue on stop.
|
2021-11-26 12:19:20 +00:00
|
|
|
func (s *Server) initSubnet(cfg subnetConfig) {
|
|
|
|
s.registerStarter(func() error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// initialize queue for processing of the events from Subnet contract
|
|
|
|
s.subnetHandler.workerPool, err = ants.NewPool(int(cfg.queueSize), ants.WithNonblocking(true))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("subnet queue initialization: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize morph client of Subnet contract
|
|
|
|
clientMode := morphsubnet.NotaryAlphabet
|
|
|
|
|
|
|
|
if s.sideNotaryConfig.disabled {
|
|
|
|
clientMode = morphsubnet.NonNotary
|
|
|
|
}
|
|
|
|
|
|
|
|
var initPrm morphsubnet.InitPrm
|
|
|
|
|
|
|
|
initPrm.SetBaseClient(s.morphClient)
|
|
|
|
initPrm.SetContractAddress(s.contracts.subnet)
|
|
|
|
initPrm.SetMode(clientMode)
|
|
|
|
|
|
|
|
err = s.subnetHandler.morphClient.Init(initPrm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("init morph subnet client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.listenSubnet()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
s.registerCloser(func() error {
|
|
|
|
s.stopSubnet()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// releases the Subnet contract notification processing queue.
|
|
|
|
func (s *Server) stopSubnet() {
|
|
|
|
s.workerPool.Release()
|
|
|
|
}
|
|
|
|
|
|
|
|
// names of listened notification events from Subnet contract.
|
|
|
|
const (
|
|
|
|
// subnet creation
|
2021-12-01 11:21:48 +00:00
|
|
|
subnetCreateEvName = "Put"
|
2021-11-26 12:19:20 +00:00
|
|
|
// subnet removal
|
2021-12-01 11:21:48 +00:00
|
|
|
subnetRemoveEvName = "Delete"
|
2021-11-30 17:32:52 +00:00
|
|
|
// subnet creation (notary)
|
2021-12-01 11:21:48 +00:00
|
|
|
notarySubnetCreateEvName = "put"
|
2021-11-26 12:19:20 +00:00
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// makes the IR server to listen to notifications of Subnet contract.
|
2021-11-26 12:19:20 +00:00
|
|
|
// All required resources must be initialized before (initSubnet).
|
2022-04-21 11:28:05 +00:00
|
|
|
// It works in one of two modes (configured): notary and non-notary.
|
2021-11-26 12:19:20 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// All handlers are executed only if the local node is an alphabet one.
|
2021-11-26 12:19:20 +00:00
|
|
|
//
|
|
|
|
// Events (notary):
|
2022-08-15 16:20:20 +00:00
|
|
|
// - put (parser: subnetevents.ParseNotaryPut, handler: catchSubnetCreation);
|
|
|
|
// - Delete (parser: subnetevents.ParseDelete, handler: catchSubnetCreation).
|
2021-11-26 12:19:20 +00:00
|
|
|
//
|
|
|
|
// Events (non-notary):
|
2022-08-15 16:20:20 +00:00
|
|
|
// - Put (parser: subnetevents.ParsePut, handler: catchSubnetCreation);
|
|
|
|
// - Delete (parser: subnetevents.ParseDelete, handler: catchSubnetCreation).
|
2021-11-26 12:19:20 +00:00
|
|
|
func (s *Server) listenSubnet() {
|
|
|
|
if s.sideNotaryConfig.disabled {
|
|
|
|
s.listenSubnetWithoutNotary()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
parserInfo event.NotaryParserInfo
|
|
|
|
handlerInfo event.NotaryHandlerInfo
|
|
|
|
)
|
|
|
|
|
|
|
|
parserInfo.SetScriptHash(s.contracts.subnet)
|
|
|
|
handlerInfo.SetScriptHash(s.contracts.subnet)
|
|
|
|
|
2022-05-13 18:37:59 +00:00
|
|
|
listenNotaryEvent := func(notifyName string, parser event.NotaryParser, handler event.Handler) {
|
2021-11-26 12:19:20 +00:00
|
|
|
notifyTyp := event.NotaryTypeFromString(notifyName)
|
|
|
|
|
|
|
|
parserInfo.SetMempoolType(mempoolevent.TransactionAdded)
|
|
|
|
handlerInfo.SetMempoolType(mempoolevent.TransactionAdded)
|
|
|
|
|
|
|
|
parserInfo.SetParser(parser)
|
|
|
|
handlerInfo.SetHandler(handler)
|
|
|
|
|
|
|
|
parserInfo.SetRequestType(notifyTyp)
|
|
|
|
handlerInfo.SetRequestType(notifyTyp)
|
|
|
|
|
|
|
|
s.morphListener.SetNotaryParser(parserInfo)
|
|
|
|
s.morphListener.RegisterNotaryHandler(handlerInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// subnet creation
|
2022-05-13 18:37:59 +00:00
|
|
|
listenNotaryEvent(notarySubnetCreateEvName, subnetevents.ParseNotaryPut, s.onlyAlphabetEventHandler(s.catchSubnetCreation))
|
|
|
|
// subnet removal
|
|
|
|
listenNotifySubnetEvent(s, subnetRemoveEvName, subnetevents.ParseDelete, s.onlyAlphabetEventHandler(s.catchSubnetRemoval))
|
2021-11-26 12:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) listenSubnetWithoutNotary() {
|
2022-05-13 18:37:59 +00:00
|
|
|
// subnet creation
|
|
|
|
listenNotifySubnetEvent(s, subnetCreateEvName, subnetevents.ParsePut, s.onlyAlphabetEventHandler(s.catchSubnetCreation))
|
|
|
|
// subnet removal
|
|
|
|
listenNotifySubnetEvent(s, subnetRemoveEvName, subnetevents.ParseDelete, s.onlyAlphabetEventHandler(s.catchSubnetRemoval))
|
|
|
|
}
|
|
|
|
|
|
|
|
func listenNotifySubnetEvent(s *Server, notifyName string, parser event.NotificationParser, handler event.Handler) {
|
2021-11-26 12:19:20 +00:00
|
|
|
var (
|
|
|
|
parserInfo event.NotificationParserInfo
|
|
|
|
handlerInfo event.NotificationHandlerInfo
|
|
|
|
)
|
|
|
|
|
|
|
|
parserInfo.SetScriptHash(s.contracts.subnet)
|
|
|
|
handlerInfo.SetScriptHash(s.contracts.subnet)
|
|
|
|
|
2022-05-13 18:37:59 +00:00
|
|
|
notifyTyp := event.TypeFromString(notifyName)
|
2021-11-26 12:19:20 +00:00
|
|
|
|
2022-05-13 18:37:59 +00:00
|
|
|
parserInfo.SetType(notifyTyp)
|
|
|
|
handlerInfo.SetType(notifyTyp)
|
2021-11-26 12:19:20 +00:00
|
|
|
|
2022-05-13 18:37:59 +00:00
|
|
|
parserInfo.SetParser(parser)
|
|
|
|
handlerInfo.SetHandler(handler)
|
2021-11-26 12:19:20 +00:00
|
|
|
|
2022-05-13 18:37:59 +00:00
|
|
|
s.morphListener.SetNotificationParser(parserInfo)
|
|
|
|
s.morphListener.RegisterNotificationHandler(handlerInfo)
|
2021-11-26 12:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// catchSubnetCreation catches event of subnet creation from listener and queues the processing.
|
|
|
|
func (s *Server) catchSubnetCreation(e event.Event) {
|
|
|
|
err := s.subnetHandler.workerPool.Submit(func() {
|
|
|
|
s.handleSubnetCreation(e)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("subnet creation queue failure",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// implements irsubnet.Put event interface required by irsubnet.PutValidator.
|
|
|
|
type putSubnetEvent struct {
|
|
|
|
ev subnetevents.Put
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ReadID unmarshals the subnet ID from a binary NeoFS API protocol's format.
|
2021-11-26 12:19:20 +00:00
|
|
|
func (x putSubnetEvent) ReadID(id *subnetid.ID) error {
|
|
|
|
return id.Unmarshal(x.ev.ID())
|
|
|
|
}
|
|
|
|
|
|
|
|
var errMissingSubnetOwner = errors.New("missing subnet owner")
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ReadCreator unmarshals the subnet creator from a binary NeoFS API protocol's format.
|
|
|
|
// Returns an error if the byte array is empty.
|
2022-05-17 13:59:46 +00:00
|
|
|
func (x putSubnetEvent) ReadCreator(id *user.ID) error {
|
2021-11-26 12:19:20 +00:00
|
|
|
data := x.ev.Owner()
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
return errMissingSubnetOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := keys.NewPublicKeyFromBytes(data, elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
user.IDFromKey(id, (ecdsa.PublicKey)(*key))
|
2021-11-26 12:19:20 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ReadInfo unmarshal the subnet info from a binary NeoFS API protocol's format.
|
2021-11-26 12:19:20 +00:00
|
|
|
func (x putSubnetEvent) ReadInfo(info *subnet.Info) error {
|
|
|
|
return info.Unmarshal(x.ev.Info())
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// handleSubnetCreation handles an event of subnet creation parsed via subnetevents.ParsePut.
|
2021-11-26 12:19:20 +00:00
|
|
|
//
|
|
|
|
// Validates the event using irsubnet.PutValidator. Logs message about (dis)agreement.
|
|
|
|
func (s *Server) handleSubnetCreation(e event.Event) {
|
|
|
|
putEv := e.(subnetevents.Put) // panic occurs only if we registered handler incorrectly
|
|
|
|
|
|
|
|
err := s.subnetHandler.putValidator.Assert(putSubnetEvent{
|
|
|
|
ev: putEv,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.log.Info("discard subnet creation",
|
|
|
|
zap.String("reason", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
notaryMainTx := putEv.NotaryMainTx()
|
|
|
|
|
|
|
|
isNotary := notaryMainTx != nil
|
|
|
|
if isNotary {
|
|
|
|
// re-sign notary request
|
|
|
|
err = s.morphClient.NotarySignAndInvokeTX(notaryMainTx)
|
|
|
|
} else {
|
|
|
|
// send new transaction
|
|
|
|
var prm morphsubnet.PutPrm
|
|
|
|
|
|
|
|
prm.SetID(putEv.ID())
|
|
|
|
prm.SetOwner(putEv.Owner())
|
|
|
|
prm.SetInfo(putEv.Info())
|
|
|
|
prm.SetTxHash(putEv.TxHash())
|
|
|
|
|
|
|
|
_, err = s.subnetHandler.morphClient.Put(prm)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("approve subnet creation",
|
|
|
|
zap.Bool("notary", isNotary),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// catchSubnetRemoval catches an event of subnet removal from listener and queues the processing.
|
2021-11-26 12:19:20 +00:00
|
|
|
func (s *Server) catchSubnetRemoval(e event.Event) {
|
|
|
|
err := s.subnetHandler.workerPool.Submit(func() {
|
2022-01-29 13:35:28 +00:00
|
|
|
s.handleSubnetRemoval(e)
|
2021-11-26 12:19:20 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-05-27 09:35:41 +00:00
|
|
|
s.log.Error("subnet removal handling failure",
|
2021-11-26 12:19:20 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleSubnetRemoval handles event of subnet removal parsed via subnetevents.ParseDelete.
|
|
|
|
func (s *Server) handleSubnetRemoval(e event.Event) {
|
|
|
|
delEv := e.(subnetevents.Delete) // panic occurs only if we registered handler incorrectly
|
|
|
|
|
2022-05-13 18:39:20 +00:00
|
|
|
// handle subnet changes in netmap
|
|
|
|
|
|
|
|
candidates, err := s.netmapClient.GetCandidates()
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("getting netmap candidates",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var removedID subnetid.ID
|
|
|
|
err = removedID.Unmarshal(delEv.ID())
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("unmarshalling removed subnet ID",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
candidateNodes := candidates.Nodes()
|
|
|
|
|
|
|
|
for i := range candidateNodes {
|
|
|
|
s.processCandidate(delEv.TxHash(), removedID, candidateNodes[i])
|
2022-05-13 18:39:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func (s *Server) processCandidate(txHash neogoutil.Uint256, removedID subnetid.ID, c netmap.NodeInfo) {
|
2022-05-13 18:39:20 +00:00
|
|
|
removeSubnet := false
|
|
|
|
log := s.log.With(
|
2022-06-08 23:18:26 +00:00
|
|
|
zap.String("public_key", hex.EncodeToString(c.PublicKey())),
|
2022-05-13 18:39:20 +00:00
|
|
|
zap.String("removed_subnet", removedID.String()),
|
|
|
|
)
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
err := c.IterateSubnets(func(id subnetid.ID) error {
|
2022-06-15 08:08:10 +00:00
|
|
|
if removedID.Equals(id) {
|
2022-05-13 18:39:20 +00:00
|
|
|
removeSubnet = true
|
|
|
|
return netmap.ErrRemoveSubnet
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error("iterating node's subnets", zap.Error(err))
|
|
|
|
log.Debug("removing node from netmap candidates")
|
|
|
|
|
|
|
|
var updateStatePrm netmapclient.UpdatePeerPrm
|
2022-06-08 23:18:26 +00:00
|
|
|
updateStatePrm.SetKey(c.PublicKey())
|
2022-05-13 18:39:20 +00:00
|
|
|
updateStatePrm.SetHash(txHash)
|
|
|
|
|
|
|
|
err = s.netmapClient.UpdatePeerState(updateStatePrm)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("removing node from candidates",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove subnet from node's information
|
|
|
|
// if it contains removed subnet
|
|
|
|
if removeSubnet {
|
|
|
|
log.Debug("removing subnet from the node")
|
|
|
|
|
|
|
|
var addPeerPrm netmapclient.AddPeerPrm
|
2022-06-08 23:18:26 +00:00
|
|
|
addPeerPrm.SetNodeInfo(c)
|
2022-05-13 18:39:20 +00:00
|
|
|
addPeerPrm.SetHash(txHash)
|
|
|
|
|
|
|
|
err = s.netmapClient.AddPeer(addPeerPrm)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("updating subnet info",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 12:19:20 +00:00
|
|
|
}
|