2020-07-24 13:54:03 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
2024-10-21 07:22:54 +00:00
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-05-26 10:24:41 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/metrics"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/processors/netmap/nodevalidation/state"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
|
|
|
|
netmapEvent "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
2021-09-08 13:54:45 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/mempoolevent"
|
2023-04-27 14:57:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// EpochTimerReseter is a callback interface for tickers component.
|
|
|
|
EpochTimerReseter interface {
|
2022-04-04 07:07:33 +00:00
|
|
|
ResetEpochTimer(uint32) 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-10-11 17:09:52 +00:00
|
|
|
SetEpochDuration(uint64)
|
|
|
|
EpochDuration() uint64
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-10-14 15:05:55 +00:00
|
|
|
// VerifyAndUpdate must verify and optionally update NodeInfo structure.
|
2021-02-09 14:36:43 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-04-27 14:57:27 +00:00
|
|
|
Client interface {
|
|
|
|
MorphNotaryInvoke(contract util.Uint160, fee fixedn.Fixed8, nonce uint32, vub *uint32, method string, args ...any) error
|
|
|
|
ContractAddress() util.Uint160
|
|
|
|
EpochDuration() (uint64, error)
|
|
|
|
MorphTxHeight(h util.Uint256) (res uint32, err error)
|
|
|
|
NetMap() (*netmap.NetMap, error)
|
2023-11-08 09:05:03 +00:00
|
|
|
NewEpoch(epoch uint64) error
|
2023-04-27 14:57:27 +00:00
|
|
|
MorphIsValidScript(script []byte, signers []transaction.Signer) (valid bool, err error)
|
|
|
|
MorphNotarySignAndInvokeTX(mainTx *transaction.Transaction) 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 {
|
2022-09-28 07:41:01 +00:00
|
|
|
log *logger.Logger
|
2023-05-26 10:24:41 +00:00
|
|
|
metrics metrics.Register
|
2021-09-15 10:50:07 +00:00
|
|
|
pool *ants.Pool
|
|
|
|
epochTimer EpochTimerReseter
|
|
|
|
epochState EpochState
|
|
|
|
alphabetState AlphabetState
|
2020-10-29 15:57:55 +00:00
|
|
|
|
2023-04-27 14:57:27 +00:00
|
|
|
netmapClient Client
|
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
|
|
|
|
2023-04-25 06:16:14 +00:00
|
|
|
handleAlphabetSync event.Handler
|
|
|
|
handleNotaryDeposit event.Handler
|
2021-02-09 14:36:43 +00:00
|
|
|
|
|
|
|
nodeValidator NodeValidator
|
2021-09-08 13:54:45 +00:00
|
|
|
|
2022-09-19 16:34:53 +00:00
|
|
|
nodeStateSettings state.NetworkSettings
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Params of the processor constructor.
|
|
|
|
Params struct {
|
2022-09-28 07:41:01 +00:00
|
|
|
Log *logger.Logger
|
2023-05-26 10:24:41 +00:00
|
|
|
Metrics metrics.Register
|
2021-09-15 10:50:07 +00:00
|
|
|
PoolSize int
|
2023-04-27 14:57:27 +00:00
|
|
|
NetmapClient Client
|
2020-10-29 15:57:55 +00:00
|
|
|
EpochTimer EpochTimerReseter
|
|
|
|
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-28 19:51:41 +00:00
|
|
|
|
2023-04-25 06:16:14 +00:00
|
|
|
AlphabetSyncHandler event.Handler
|
|
|
|
NotaryDepositHandler event.Handler
|
2021-02-09 14:36:43 +00:00
|
|
|
|
|
|
|
NodeValidator NodeValidator
|
2021-09-08 13:54:45 +00:00
|
|
|
|
2022-09-19 16:34:53 +00:00
|
|
|
NodeStateSettings state.NetworkSettings
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-05-11 15:17:06 +00:00
|
|
|
newEpochNotification = "NewEpoch"
|
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.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")
|
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-11-01 15:55:25 +00:00
|
|
|
case p.NotaryDepositHandler == nil:
|
|
|
|
return nil, errors.New("ir/netmap: notary deposit handler 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")
|
2022-09-19 16:34:53 +00:00
|
|
|
case p.NodeStateSettings == nil:
|
|
|
|
return nil, errors.New("ir/netmap: node state settings is not set")
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
p.Log.Debug(context.Background(), logs.NetmapNetmapWorkerPool, zap.Int("size", p.PoolSize))
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-26 10:24:41 +00:00
|
|
|
metricsRegister := p.Metrics
|
|
|
|
if metricsRegister == nil {
|
|
|
|
metricsRegister = metrics.DefaultRegister{}
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
return &Processor{
|
|
|
|
log: p.Log,
|
2023-05-26 10:24:41 +00:00
|
|
|
metrics: metricsRegister,
|
2020-07-24 13:54:03 +00:00
|
|
|
pool: pool,
|
|
|
|
epochTimer: p.EpochTimer,
|
|
|
|
epochState: p.EpochState,
|
2021-03-23 15:20:44 +00:00
|
|
|
alphabetState: p.AlphabetState,
|
2021-05-31 11:50:11 +00:00
|
|
|
netmapClient: p.NetmapClient,
|
2020-10-29 15:57:55 +00:00
|
|
|
netmapSnapshot: newCleanupTable(p.CleanupEnabled, p.CleanupThreshold),
|
2021-02-09 14:36:43 +00:00
|
|
|
|
2021-03-25 13:24:44 +00:00
|
|
|
handleAlphabetSync: p.AlphabetSyncHandler,
|
|
|
|
|
2021-11-01 15:55:25 +00:00
|
|
|
handleNotaryDeposit: p.NotaryDepositHandler,
|
|
|
|
|
2021-02-09 14:36:43 +00:00
|
|
|
nodeValidator: p.NodeValidator,
|
2021-09-08 13:54:45 +00:00
|
|
|
|
2022-09-19 16:34:53 +00:00
|
|
|
nodeStateSettings: p.NodeStateSettings,
|
2020-07-24 13:54:03 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
// ListenerNotificationParsers for the 'event.Listener' event producer.
|
|
|
|
func (np *Processor) ListenerNotificationParsers() []event.NotificationParserInfo {
|
2021-09-08 13:54:45 +00:00
|
|
|
parsers := make([]event.NotificationParserInfo, 0, 3)
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2021-09-15 10:50:07 +00:00
|
|
|
var p event.NotificationParserInfo
|
|
|
|
|
|
|
|
p.SetScriptHash(np.netmapClient.ContractAddress())
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// new epoch event
|
2021-09-15 10:50:07 +00:00
|
|
|
p.SetType(newEpochNotification)
|
|
|
|
p.SetParser(netmapEvent.ParseNewEpoch)
|
|
|
|
parsers = append(parsers, p)
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
return parsers
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
// ListenerNotificationHandlers for the 'event.Listener' event producer.
|
|
|
|
func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
|
2021-09-08 13:54:45 +00:00
|
|
|
handlers := make([]event.NotificationHandlerInfo, 0, 3)
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2021-09-15 10:50:07 +00:00
|
|
|
var i event.NotificationHandlerInfo
|
|
|
|
|
|
|
|
i.SetScriptHash(np.netmapClient.ContractAddress())
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// new epoch handler
|
2021-09-15 10:50:07 +00:00
|
|
|
i.SetType(newEpochNotification)
|
|
|
|
i.SetHandler(np.handleNewEpoch)
|
|
|
|
handlers = append(handlers, i)
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
return handlers
|
|
|
|
}
|
|
|
|
|
2021-09-08 08:28:41 +00:00
|
|
|
// ListenerNotaryParsers for the 'event.Listener' event producer.
|
|
|
|
func (np *Processor) ListenerNotaryParsers() []event.NotaryParserInfo {
|
2021-09-08 13:54:45 +00:00
|
|
|
var (
|
|
|
|
p event.NotaryParserInfo
|
|
|
|
|
|
|
|
pp = make([]event.NotaryParserInfo, 0, 2)
|
|
|
|
)
|
|
|
|
|
|
|
|
p.SetMempoolType(mempoolevent.TransactionAdded)
|
2021-09-15 10:50:07 +00:00
|
|
|
p.SetScriptHash(np.netmapClient.ContractAddress())
|
2021-09-08 13:54:45 +00:00
|
|
|
|
|
|
|
// new peer
|
|
|
|
p.SetRequestType(netmapEvent.AddPeerNotaryEvent)
|
|
|
|
p.SetParser(netmapEvent.ParseAddPeerNotary)
|
|
|
|
pp = append(pp, p)
|
|
|
|
|
2021-09-09 11:55:01 +00:00
|
|
|
// update state
|
|
|
|
p.SetRequestType(netmapEvent.UpdateStateNotaryEvent)
|
|
|
|
p.SetParser(netmapEvent.ParseUpdatePeerNotary)
|
|
|
|
pp = append(pp, p)
|
|
|
|
|
2021-09-08 13:54:45 +00:00
|
|
|
return pp
|
2021-09-08 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerNotaryHandlers for the 'event.Listener' event producer.
|
|
|
|
func (np *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo {
|
2021-09-08 13:54:45 +00:00
|
|
|
var (
|
|
|
|
h event.NotaryHandlerInfo
|
|
|
|
|
|
|
|
hh = make([]event.NotaryHandlerInfo, 0, 2)
|
|
|
|
)
|
|
|
|
|
|
|
|
h.SetMempoolType(mempoolevent.TransactionAdded)
|
2021-09-15 10:50:07 +00:00
|
|
|
h.SetScriptHash(np.netmapClient.ContractAddress())
|
2021-09-08 13:54:45 +00:00
|
|
|
|
|
|
|
// new peer
|
|
|
|
h.SetRequestType(netmapEvent.AddPeerNotaryEvent)
|
|
|
|
h.SetHandler(np.handleAddPeer)
|
|
|
|
hh = append(hh, h)
|
|
|
|
|
2021-09-09 11:55:01 +00:00
|
|
|
// update state
|
|
|
|
h.SetRequestType(netmapEvent.UpdateStateNotaryEvent)
|
|
|
|
h.SetHandler(np.handleUpdateState)
|
|
|
|
hh = append(hh, h)
|
|
|
|
|
2021-09-08 13:54:45 +00:00
|
|
|
return hh
|
2021-09-08 08:28:41 +00:00
|
|
|
}
|