2020-08-11 12:10:01 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2021-08-25 11:35:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/mempoolevent"
|
2022-01-31 13:34:01 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client/container"
|
2022-01-31 11:04:59 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid"
|
2021-12-01 12:45:35 +00:00
|
|
|
morphsubnet "github.com/nspcc-dev/neofs-node/pkg/morph/client/subnet"
|
2020-08-11 12:10:01 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
|
|
|
containerEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/container"
|
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2021-03-23 15:20:44 +00:00
|
|
|
// AlphabetState is a callback interface for inner ring global state.
|
|
|
|
AlphabetState interface {
|
|
|
|
IsAlphabet() bool
|
2020-08-11 12:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Processor of events produced by container contract in morph chain.
|
|
|
|
Processor struct {
|
2021-09-15 10:50:07 +00:00
|
|
|
log *zap.Logger
|
|
|
|
pool *ants.Pool
|
|
|
|
alphabetState AlphabetState
|
2022-01-31 13:34:01 +00:00
|
|
|
cnrClient *container.Client // notary must be enabled
|
2022-01-31 11:04:59 +00:00
|
|
|
idClient *neofsid.Client
|
2021-12-01 12:45:35 +00:00
|
|
|
subnetClient *morphsubnet.Client
|
2021-09-15 10:50:07 +00:00
|
|
|
netState NetworkState
|
|
|
|
notaryDisabled bool
|
2020-08-11 12:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Params of the processor constructor.
|
|
|
|
Params struct {
|
2021-09-15 10:50:07 +00:00
|
|
|
Log *zap.Logger
|
|
|
|
PoolSize int
|
|
|
|
AlphabetState AlphabetState
|
2022-01-31 13:34:01 +00:00
|
|
|
ContainerClient *container.Client
|
2022-01-31 11:04:59 +00:00
|
|
|
NeoFSIDClient *neofsid.Client
|
2021-12-01 12:45:35 +00:00
|
|
|
SubnetClient *morphsubnet.Client
|
2021-09-15 10:50:07 +00:00
|
|
|
NetworkState NetworkState
|
|
|
|
NotaryDisabled bool
|
2020-08-11 12:10:01 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-06-04 15:02:25 +00:00
|
|
|
// NetworkState is an interface of a component
|
|
|
|
// that provides access to network state.
|
|
|
|
type NetworkState interface {
|
|
|
|
// Epoch must return number of the current epoch.
|
|
|
|
//
|
|
|
|
// Must return any error encountered
|
|
|
|
// which did not allow reading the value.
|
|
|
|
Epoch() (uint64, error)
|
|
|
|
}
|
|
|
|
|
2020-08-11 12:10:01 +00:00
|
|
|
const (
|
2020-09-02 16:04:29 +00:00
|
|
|
putNotification = "containerPut"
|
|
|
|
deleteNotification = "containerDelete"
|
2021-05-19 12:46:45 +00:00
|
|
|
|
|
|
|
setEACLNotification = "setEACL"
|
2020-08-11 12:10:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// New creates container contract processor instance.
|
|
|
|
func New(p *Params) (*Processor, error) {
|
|
|
|
switch {
|
|
|
|
case p.Log == nil:
|
|
|
|
return nil, errors.New("ir/container: logger is not set")
|
2021-03-23 15:20:44 +00:00
|
|
|
case p.AlphabetState == nil:
|
2020-08-11 12:10:01 +00:00
|
|
|
return nil, errors.New("ir/container: global state is not set")
|
2021-05-19 12:46:45 +00:00
|
|
|
case p.ContainerClient == nil:
|
|
|
|
return nil, errors.New("ir/container: Container client is not set")
|
|
|
|
case p.NeoFSIDClient == nil:
|
|
|
|
return nil, errors.New("ir/container: NeoFS ID client is not set")
|
2021-06-04 15:02:25 +00:00
|
|
|
case p.NetworkState == nil:
|
|
|
|
return nil, errors.New("ir/container: network state is not set")
|
2021-12-01 12:45:35 +00:00
|
|
|
case p.SubnetClient == nil:
|
|
|
|
return nil, errors.New("ir/container: subnet client is not set")
|
2020-08-11 12:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.Log.Debug("container 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/container: can't create worker pool: %w", err)
|
2020-08-11 12:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Processor{
|
2021-09-15 10:50:07 +00:00
|
|
|
log: p.Log,
|
|
|
|
pool: pool,
|
|
|
|
alphabetState: p.AlphabetState,
|
|
|
|
cnrClient: p.ContainerClient,
|
|
|
|
idClient: p.NeoFSIDClient,
|
|
|
|
netState: p.NetworkState,
|
|
|
|
notaryDisabled: p.NotaryDisabled,
|
2021-12-01 12:45:35 +00:00
|
|
|
subnetClient: p.SubnetClient,
|
2020-08-11 12:10:01 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
// ListenerNotificationParsers for the 'event.Listener' event producer.
|
|
|
|
func (cp *Processor) ListenerNotificationParsers() []event.NotificationParserInfo {
|
2021-09-08 08:17:19 +00:00
|
|
|
if !cp.notaryDisabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-19 12:32:19 +00:00
|
|
|
var (
|
2021-08-12 15:24:17 +00:00
|
|
|
parsers = make([]event.NotificationParserInfo, 0, 3)
|
2021-05-19 12:32:19 +00:00
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
p event.NotificationParserInfo
|
2021-05-19 12:32:19 +00:00
|
|
|
)
|
|
|
|
|
2021-09-15 10:50:07 +00:00
|
|
|
p.SetScriptHash(cp.cnrClient.ContractAddress())
|
2021-05-19 12:32:19 +00:00
|
|
|
|
2021-09-08 08:17:19 +00:00
|
|
|
// container put
|
|
|
|
p.SetType(event.TypeFromString(putNotification))
|
|
|
|
p.SetParser(containerEvent.ParsePut)
|
|
|
|
parsers = append(parsers, p)
|
2021-05-19 12:32:19 +00:00
|
|
|
|
|
|
|
// container delete
|
|
|
|
p.SetType(event.TypeFromString(deleteNotification))
|
|
|
|
p.SetParser(containerEvent.ParseDelete)
|
|
|
|
parsers = append(parsers, p)
|
2020-08-11 12:10:01 +00:00
|
|
|
|
2021-05-19 12:46:45 +00:00
|
|
|
// set eACL
|
|
|
|
p.SetType(event.TypeFromString(setEACLNotification))
|
|
|
|
p.SetParser(containerEvent.ParseSetEACL)
|
|
|
|
parsers = append(parsers, p)
|
|
|
|
|
2020-08-11 12:10:01 +00:00
|
|
|
return parsers
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
// ListenerNotificationHandlers for the 'event.Listener' event producer.
|
|
|
|
func (cp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
|
2021-09-08 08:17:19 +00:00
|
|
|
if !cp.notaryDisabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-19 12:32:19 +00:00
|
|
|
var (
|
2021-08-12 15:24:17 +00:00
|
|
|
handlers = make([]event.NotificationHandlerInfo, 0, 3)
|
2021-05-19 12:32:19 +00:00
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
h event.NotificationHandlerInfo
|
2021-05-19 12:32:19 +00:00
|
|
|
)
|
|
|
|
|
2021-09-15 10:50:07 +00:00
|
|
|
h.SetScriptHash(cp.cnrClient.ContractAddress())
|
2021-05-19 12:32:19 +00:00
|
|
|
|
2021-09-08 08:17:19 +00:00
|
|
|
// container put
|
|
|
|
h.SetType(event.TypeFromString(putNotification))
|
|
|
|
h.SetHandler(cp.handlePut)
|
|
|
|
handlers = append(handlers, h)
|
2021-05-19 12:32:19 +00:00
|
|
|
|
|
|
|
// container delete
|
|
|
|
h.SetType(event.TypeFromString(deleteNotification))
|
|
|
|
h.SetHandler(cp.handleDelete)
|
|
|
|
handlers = append(handlers, h)
|
2020-08-11 12:10:01 +00:00
|
|
|
|
2021-05-19 12:46:45 +00:00
|
|
|
// set eACL
|
|
|
|
h.SetType(event.TypeFromString(setEACLNotification))
|
|
|
|
h.SetHandler(cp.handleSetEACL)
|
|
|
|
handlers = append(handlers, h)
|
|
|
|
|
2020-08-11 12:10:01 +00:00
|
|
|
return handlers
|
|
|
|
}
|
|
|
|
|
2021-08-25 11:35:17 +00:00
|
|
|
// ListenerNotaryParsers for the 'event.Listener' notary event producer.
|
|
|
|
func (cp *Processor) ListenerNotaryParsers() []event.NotaryParserInfo {
|
2021-09-08 08:17:19 +00:00
|
|
|
var (
|
|
|
|
p event.NotaryParserInfo
|
|
|
|
|
2021-10-12 14:56:17 +00:00
|
|
|
pp = make([]event.NotaryParserInfo, 0, 4)
|
2021-09-08 08:17:19 +00:00
|
|
|
)
|
2021-08-25 11:35:17 +00:00
|
|
|
|
|
|
|
p.SetMempoolType(mempoolevent.TransactionAdded)
|
2021-09-15 10:50:07 +00:00
|
|
|
p.SetScriptHash(cp.cnrClient.ContractAddress())
|
2021-09-08 08:17:19 +00:00
|
|
|
|
|
|
|
// container put
|
|
|
|
p.SetRequestType(containerEvent.PutNotaryEvent)
|
2021-08-25 11:35:17 +00:00
|
|
|
p.SetParser(containerEvent.ParsePutNotary)
|
2021-09-08 08:17:19 +00:00
|
|
|
pp = append(pp, p)
|
|
|
|
|
2021-10-12 14:56:17 +00:00
|
|
|
// container named put
|
|
|
|
p.SetRequestType(containerEvent.PutNamedNotaryEvent)
|
|
|
|
p.SetParser(containerEvent.ParsePutNamedNotary)
|
|
|
|
pp = append(pp, p)
|
|
|
|
|
2021-09-08 08:17:19 +00:00
|
|
|
// container delete
|
|
|
|
p.SetRequestType(containerEvent.DeleteNotaryEvent)
|
|
|
|
p.SetParser(containerEvent.ParseDeleteNotary)
|
|
|
|
pp = append(pp, p)
|
2021-08-25 11:35:17 +00:00
|
|
|
|
2021-09-08 08:20:11 +00:00
|
|
|
// set EACL
|
|
|
|
p.SetRequestType(containerEvent.SetEACLNotaryEvent)
|
|
|
|
p.SetParser(containerEvent.ParseSetEACLNotary)
|
|
|
|
pp = append(pp, p)
|
|
|
|
|
2021-09-08 08:17:19 +00:00
|
|
|
return pp
|
2021-08-25 11:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerNotaryHandlers for the 'event.Listener' notary event producer.
|
|
|
|
func (cp *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo {
|
2021-09-08 08:17:19 +00:00
|
|
|
var (
|
|
|
|
h event.NotaryHandlerInfo
|
|
|
|
|
2021-10-12 14:56:17 +00:00
|
|
|
hh = make([]event.NotaryHandlerInfo, 0, 4)
|
2021-09-08 08:17:19 +00:00
|
|
|
)
|
2021-08-25 11:35:17 +00:00
|
|
|
|
2021-09-15 10:50:07 +00:00
|
|
|
h.SetScriptHash(cp.cnrClient.ContractAddress())
|
2021-08-25 11:35:17 +00:00
|
|
|
h.SetMempoolType(mempoolevent.TransactionAdded)
|
2021-09-08 08:17:19 +00:00
|
|
|
|
|
|
|
// container put
|
2021-08-25 11:35:17 +00:00
|
|
|
h.SetRequestType(containerEvent.PutNotaryEvent)
|
|
|
|
h.SetHandler(cp.handlePut)
|
2021-09-08 08:17:19 +00:00
|
|
|
hh = append(hh, h)
|
|
|
|
|
2021-10-12 14:56:17 +00:00
|
|
|
// container named put (same handler)
|
|
|
|
h.SetRequestType(containerEvent.PutNamedNotaryEvent)
|
|
|
|
hh = append(hh, h)
|
|
|
|
|
2021-09-08 08:17:19 +00:00
|
|
|
// container delete
|
|
|
|
h.SetRequestType(containerEvent.DeleteNotaryEvent)
|
|
|
|
h.SetHandler(cp.handleDelete)
|
|
|
|
hh = append(hh, h)
|
2021-08-25 11:35:17 +00:00
|
|
|
|
2021-09-08 08:20:11 +00:00
|
|
|
// set eACL
|
|
|
|
h.SetRequestType(containerEvent.SetEACLNotaryEvent)
|
|
|
|
h.SetHandler(cp.handleSetEACL)
|
|
|
|
hh = append(hh, h)
|
|
|
|
|
2021-09-08 08:17:19 +00:00
|
|
|
return hh
|
2021-08-25 11:35:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 12:10:01 +00:00
|
|
|
// TimersHandlers for the 'Timers' event producer.
|
2021-08-12 15:24:17 +00:00
|
|
|
func (cp *Processor) TimersHandlers() []event.NotificationHandlerInfo {
|
2020-08-11 12:10:01 +00:00
|
|
|
return nil
|
|
|
|
}
|