2020-08-11 12:10:01 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2023-11-20 14:03:19 +00:00
|
|
|
frostfsidclient "git.frostfs.info/TrueCloudLab/frostfs-contract/frostfsid/client"
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-04-26 09:05:33 +00:00
|
|
|
containercore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
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/morph/event"
|
|
|
|
containerEvent "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
2021-08-25 11:35:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/mempoolevent"
|
2023-05-17 12:43:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2023-04-26 09:05:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-08-11 12:10:01 +00:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
2023-04-26 09:05:33 +00:00
|
|
|
ContClient interface {
|
|
|
|
ContractAddress() util.Uint160
|
|
|
|
Get(cid []byte) (*containercore.Container, error)
|
2023-05-17 12:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MorphClient interface {
|
|
|
|
NotarySignAndInvokeTX(mainTx *transaction.Transaction) error
|
2023-04-26 09:05:33 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 14:03:19 +00:00
|
|
|
FrostFSIDClient interface {
|
|
|
|
GetSubject(addr util.Uint160) (*frostfsidclient.Subject, error)
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Processor of events produced by container contract in the sidechain.
|
2020-08-11 12:10:01 +00:00
|
|
|
Processor struct {
|
2023-11-20 14:03:19 +00:00
|
|
|
log *logger.Logger
|
|
|
|
metrics metrics.Register
|
|
|
|
pool *ants.Pool
|
|
|
|
alphabetState AlphabetState
|
|
|
|
cnrClient ContClient // notary must be enabled
|
|
|
|
morphClient MorphClient
|
|
|
|
netState NetworkState
|
|
|
|
frostFSIDClient FrostFSIDClient
|
2020-08-11 12:10:01 +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
|
|
|
|
AlphabetState AlphabetState
|
2023-04-26 09:05:33 +00:00
|
|
|
ContainerClient ContClient
|
2023-05-17 12:43:56 +00:00
|
|
|
MorphClient MorphClient
|
2021-09-15 10:50:07 +00:00
|
|
|
NetworkState NetworkState
|
2023-11-20 14:03:19 +00:00
|
|
|
FrostFSIDClient FrostFSIDClient
|
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 {
|
2022-04-21 11:28:05 +00:00
|
|
|
// Epoch must return the number of the current epoch.
|
2021-06-04 15:02:25 +00:00
|
|
|
//
|
|
|
|
// Must return any error encountered
|
|
|
|
// which did not allow reading the value.
|
|
|
|
Epoch() (uint64, error)
|
2022-04-29 17:48:11 +00:00
|
|
|
|
|
|
|
// HomomorphicHashDisabled must return boolean that
|
|
|
|
// represents homomorphic network state:
|
|
|
|
// * true if hashing is disabled;
|
|
|
|
// * false if hashing is enabled.
|
|
|
|
//
|
|
|
|
// which did not allow reading the value.
|
|
|
|
HomomorphicHashDisabled() (bool, error)
|
2021-06-04 15:02:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// New creates a container contract processor instance.
|
2020-08-11 12:10:01 +00:00
|
|
|
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")
|
2023-05-17 12:43:56 +00:00
|
|
|
case p.MorphClient == nil:
|
|
|
|
return nil, errors.New("ir/container: Morph 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")
|
2023-11-20 14:03:19 +00:00
|
|
|
case p.FrostFSIDClient == nil:
|
|
|
|
return nil, errors.New("ir/container: FrostFSID client is not set")
|
2020-08-11 12:10:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
p.Log.Debug(logs.ContainerContainerWorkerPool, zap.Int("size", p.PoolSize))
|
2020-08-11 12:10:01 +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/container: can't create worker pool: %w", err)
|
2020-08-11 12:10:01 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 10:24:41 +00:00
|
|
|
metricsRegister := p.Metrics
|
|
|
|
if metricsRegister == nil {
|
|
|
|
metricsRegister = metrics.DefaultRegister{}
|
|
|
|
}
|
|
|
|
|
2020-08-11 12:10:01 +00:00
|
|
|
return &Processor{
|
2023-11-20 14:03:19 +00:00
|
|
|
log: p.Log,
|
|
|
|
metrics: metricsRegister,
|
|
|
|
pool: pool,
|
|
|
|
alphabetState: p.AlphabetState,
|
|
|
|
cnrClient: p.ContainerClient,
|
|
|
|
netState: p.NetworkState,
|
|
|
|
morphClient: p.MorphClient,
|
|
|
|
frostFSIDClient: p.FrostFSIDClient,
|
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 {
|
2023-05-11 15:14:00 +00:00
|
|
|
return nil
|
2020-08-11 12:10:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
// ListenerNotificationHandlers for the 'event.Listener' event producer.
|
|
|
|
func (cp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
|
2023-05-11 15:14:00 +00:00
|
|
|
return nil
|
2020-08-11 12:10:01 +00:00
|
|
|
}
|
|
|
|
|
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: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:17:19 +00:00
|
|
|
return hh
|
2021-08-25 11:35:17 +00:00
|
|
|
}
|