2020-07-24 13:54:03 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-02-09 14:36:43 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2021-04-29 13:40:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/config"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2021-01-29 07:48:47 +00:00
|
|
|
container "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
|
|
|
netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
|
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// EpochTimerReseter is a callback interface for tickers component.
|
|
|
|
EpochTimerReseter interface {
|
2021-01-22 15:01:44 +00:00
|
|
|
ResetEpochTimer() error
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EpochState is a callback interface for inner ring global state.
|
|
|
|
EpochState interface {
|
|
|
|
SetEpochCounter(uint64)
|
|
|
|
EpochCounter() uint64
|
|
|
|
}
|
|
|
|
|
2021-03-23 15:20:44 +00:00
|
|
|
// AlphabetState is a callback interface for inner ring global state.
|
|
|
|
AlphabetState interface {
|
|
|
|
IsAlphabet() bool
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:36:43 +00:00
|
|
|
// NodeValidator wraps basic method of checking the correctness
|
|
|
|
// of information about the node and its finalization for adding
|
|
|
|
// to the network map.
|
|
|
|
NodeValidator interface {
|
|
|
|
// Must verify and update NodeInfo structure.
|
|
|
|
//
|
|
|
|
// Must return an error if NodeInfo input is invalid.
|
|
|
|
// Must return an error if it is not possible to correctly
|
|
|
|
// change the structure for sending to the network map.
|
|
|
|
//
|
|
|
|
// If no error occurs, the parameter must point to the
|
|
|
|
// ready-made NodeInfo structure.
|
|
|
|
VerifyAndUpdate(*netmap.NodeInfo) error
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// Processor of events produced by network map contract
|
|
|
|
// and new epoch ticker, because it is related to contract.
|
|
|
|
Processor struct {
|
|
|
|
log *zap.Logger
|
|
|
|
pool *ants.Pool
|
|
|
|
netmapContract util.Uint160
|
|
|
|
epochTimer EpochTimerReseter
|
|
|
|
epochState EpochState
|
2021-03-23 15:20:44 +00:00
|
|
|
alphabetState AlphabetState
|
2020-10-29 15:57:55 +00:00
|
|
|
|
2021-01-29 07:48:47 +00:00
|
|
|
morphClient *client.Client
|
|
|
|
containerWrp *container.Wrapper
|
2020-12-18 09:27:19 +00:00
|
|
|
|
2021-01-29 07:48:47 +00:00
|
|
|
netmapSnapshot cleanupTable
|
2021-01-28 19:51:41 +00:00
|
|
|
|
2021-01-29 07:48:47 +00:00
|
|
|
handleNewAudit event.Handler
|
2021-01-28 19:51:41 +00:00
|
|
|
handleAuditSettlements event.Handler
|
2021-03-25 13:24:44 +00:00
|
|
|
handleAlphabetSync event.Handler
|
2021-02-09 14:36:43 +00:00
|
|
|
|
|
|
|
nodeValidator NodeValidator
|
2021-04-29 13:40:34 +00:00
|
|
|
|
|
|
|
feeProvider *config.FeeConfig
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Params of the processor constructor.
|
|
|
|
Params struct {
|
2020-10-29 15:57:55 +00:00
|
|
|
Log *zap.Logger
|
|
|
|
PoolSize int
|
|
|
|
NetmapContract util.Uint160
|
|
|
|
EpochTimer EpochTimerReseter
|
|
|
|
MorphClient *client.Client
|
|
|
|
EpochState EpochState
|
2021-03-23 15:20:44 +00:00
|
|
|
AlphabetState AlphabetState
|
2020-10-29 15:57:55 +00:00
|
|
|
CleanupEnabled bool
|
|
|
|
CleanupThreshold uint64 // in epochs
|
2021-01-29 07:48:47 +00:00
|
|
|
ContainerWrapper *container.Wrapper
|
2021-01-28 19:51:41 +00:00
|
|
|
|
2021-01-29 07:48:47 +00:00
|
|
|
HandleAudit event.Handler
|
2021-01-28 19:51:41 +00:00
|
|
|
AuditSettlementsHandler event.Handler
|
2021-03-25 13:24:44 +00:00
|
|
|
AlphabetSyncHandler event.Handler
|
2021-02-09 14:36:43 +00:00
|
|
|
|
|
|
|
NodeValidator NodeValidator
|
2021-04-29 13:40:34 +00:00
|
|
|
|
|
|
|
FeeProvider *config.FeeConfig
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-09-03 14:55:11 +00:00
|
|
|
newEpochNotification = "NewEpoch"
|
|
|
|
addPeerNotification = "AddPeer"
|
|
|
|
updatePeerStateNotification = "UpdateState"
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// New creates network map contract processor instance.
|
|
|
|
func New(p *Params) (*Processor, error) {
|
|
|
|
switch {
|
|
|
|
case p.Log == nil:
|
|
|
|
return nil, errors.New("ir/netmap: logger is not set")
|
|
|
|
case p.MorphClient == nil:
|
|
|
|
return nil, errors.New("ir/netmap: morph client is not set")
|
|
|
|
case p.EpochTimer == nil:
|
|
|
|
return nil, errors.New("ir/netmap: epoch itmer is not set")
|
|
|
|
case p.EpochState == nil:
|
|
|
|
return nil, errors.New("ir/netmap: global state is not set")
|
2021-03-23 15:20:44 +00:00
|
|
|
case p.AlphabetState == nil:
|
2020-07-24 13:54:03 +00:00
|
|
|
return nil, errors.New("ir/netmap: global state is not set")
|
2020-12-18 09:27:19 +00:00
|
|
|
case p.HandleAudit == nil:
|
|
|
|
return nil, errors.New("ir/netmap: audit handler is not set")
|
2021-01-28 19:51:41 +00:00
|
|
|
case p.AuditSettlementsHandler == nil:
|
|
|
|
return nil, errors.New("ir/netmap: audit settlement handler is not set")
|
2021-03-25 13:24:44 +00:00
|
|
|
case p.AlphabetSyncHandler == nil:
|
|
|
|
return nil, errors.New("ir/netmap: alphabet sync handler is not set")
|
2021-01-29 07:48:47 +00:00
|
|
|
case p.ContainerWrapper == nil:
|
|
|
|
return nil, errors.New("ir/netmap: container contract wrapper is not set")
|
2021-02-09 14:36:43 +00:00
|
|
|
case p.NodeValidator == nil:
|
|
|
|
return nil, errors.New("ir/netmap: node validator is not set")
|
2021-04-29 13:40:34 +00:00
|
|
|
case p.FeeProvider == nil:
|
|
|
|
return nil, errors.New("ir/netmap: fee provider is not set")
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.Log.Debug("netmap worker pool", zap.Int("size", p.PoolSize))
|
|
|
|
|
|
|
|
pool, err := ants.NewPool(p.PoolSize, ants.WithNonblocking(true))
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("ir/netmap: can't create worker pool: %w", err)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Processor{
|
|
|
|
log: p.Log,
|
|
|
|
pool: pool,
|
|
|
|
netmapContract: p.NetmapContract,
|
|
|
|
epochTimer: p.EpochTimer,
|
|
|
|
epochState: p.EpochState,
|
2021-03-23 15:20:44 +00:00
|
|
|
alphabetState: p.AlphabetState,
|
2020-07-24 13:54:03 +00:00
|
|
|
morphClient: p.MorphClient,
|
2021-01-29 07:48:47 +00:00
|
|
|
containerWrp: p.ContainerWrapper,
|
2020-10-29 15:57:55 +00:00
|
|
|
netmapSnapshot: newCleanupTable(p.CleanupEnabled, p.CleanupThreshold),
|
2020-12-18 09:27:19 +00:00
|
|
|
handleNewAudit: p.HandleAudit,
|
2021-01-28 19:51:41 +00:00
|
|
|
|
|
|
|
handleAuditSettlements: p.AuditSettlementsHandler,
|
2021-02-09 14:36:43 +00:00
|
|
|
|
2021-03-25 13:24:44 +00:00
|
|
|
handleAlphabetSync: p.AlphabetSyncHandler,
|
|
|
|
|
2021-02-09 14:36:43 +00:00
|
|
|
nodeValidator: p.NodeValidator,
|
2021-04-29 13:40:34 +00:00
|
|
|
|
|
|
|
feeProvider: p.FeeProvider,
|
2020-07-24 13:54:03 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerParsers for the 'event.Listener' event producer.
|
|
|
|
func (np *Processor) ListenerParsers() []event.ParserInfo {
|
|
|
|
var parsers []event.ParserInfo
|
|
|
|
|
|
|
|
// new epoch event
|
|
|
|
newEpoch := event.ParserInfo{}
|
|
|
|
newEpoch.SetType(newEpochNotification)
|
|
|
|
newEpoch.SetScriptHash(np.netmapContract)
|
|
|
|
newEpoch.SetParser(netmapEvent.ParseNewEpoch)
|
|
|
|
parsers = append(parsers, newEpoch)
|
|
|
|
|
2020-09-03 14:55:11 +00:00
|
|
|
// new peer event
|
|
|
|
addPeer := event.ParserInfo{}
|
|
|
|
addPeer.SetType(addPeerNotification)
|
|
|
|
addPeer.SetScriptHash(np.netmapContract)
|
|
|
|
addPeer.SetParser(netmapEvent.ParseAddPeer)
|
|
|
|
parsers = append(parsers, addPeer)
|
|
|
|
|
|
|
|
// update peer event
|
|
|
|
updatePeer := event.ParserInfo{}
|
|
|
|
updatePeer.SetType(updatePeerStateNotification)
|
|
|
|
updatePeer.SetScriptHash(np.netmapContract)
|
|
|
|
updatePeer.SetParser(netmapEvent.ParseUpdatePeer)
|
|
|
|
parsers = append(parsers, updatePeer)
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
return parsers
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerHandlers for the 'event.Listener' event producer.
|
|
|
|
func (np *Processor) ListenerHandlers() []event.HandlerInfo {
|
|
|
|
var handlers []event.HandlerInfo
|
|
|
|
|
|
|
|
// new epoch handler
|
|
|
|
newEpoch := event.HandlerInfo{}
|
|
|
|
newEpoch.SetType(newEpochNotification)
|
|
|
|
newEpoch.SetScriptHash(np.netmapContract)
|
|
|
|
newEpoch.SetHandler(np.handleNewEpoch)
|
|
|
|
handlers = append(handlers, newEpoch)
|
|
|
|
|
2020-09-03 14:55:11 +00:00
|
|
|
// new peer handler
|
|
|
|
addPeer := event.HandlerInfo{}
|
|
|
|
addPeer.SetType(addPeerNotification)
|
|
|
|
addPeer.SetScriptHash(np.netmapContract)
|
|
|
|
addPeer.SetHandler(np.handleAddPeer)
|
|
|
|
handlers = append(handlers, addPeer)
|
|
|
|
|
|
|
|
// update peer handler
|
|
|
|
updatePeer := event.HandlerInfo{}
|
|
|
|
updatePeer.SetType(updatePeerStateNotification)
|
|
|
|
updatePeer.SetScriptHash(np.netmapContract)
|
|
|
|
updatePeer.SetHandler(np.handleUpdateState)
|
|
|
|
handlers = append(handlers, updatePeer)
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
return handlers
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimersHandlers for the 'Timers' event producer.
|
|
|
|
func (np *Processor) TimersHandlers() []event.HandlerInfo {
|
2021-01-22 15:01:44 +00:00
|
|
|
return nil
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|