2022-12-23 17:35:35 +00:00
|
|
|
package frostfs
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-11-03 11:16:32 +00:00
|
|
|
"sync"
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/morph/client/balance"
|
2023-01-10 13:01:54 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/morph/client/frostfsid"
|
2022-12-23 17:35:35 +00:00
|
|
|
nmClient "github.com/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/morph/event"
|
2023-01-10 12:50:17 +00:00
|
|
|
frostfsEvent "github.com/TrueCloudLab/frostfs-node/pkg/morph/event/frostfs"
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/util/logger"
|
2022-12-30 09:51:49 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru/v2"
|
2020-12-11 08:33:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2022-10-17 12:03:55 +00:00
|
|
|
// EpochState is a callback interface for inner ring global state.
|
2020-07-24 13:54:03 +00:00
|
|
|
EpochState interface {
|
|
|
|
EpochCounter() uint64
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:03:55 +00:00
|
|
|
// AlphabetState is a callback interface for inner ring global state.
|
2021-03-23 15:20:44 +00:00
|
|
|
AlphabetState interface {
|
|
|
|
IsAlphabet() bool
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 10:49:09 +00:00
|
|
|
// PrecisionConverter converts balance amount values.
|
|
|
|
PrecisionConverter interface {
|
|
|
|
ToBalancePrecision(int64) int64
|
|
|
|
}
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
// Processor of events produced by frostfs contract in main net.
|
2020-07-24 13:54:03 +00:00
|
|
|
Processor struct {
|
2022-09-28 07:41:01 +00:00
|
|
|
log *logger.Logger
|
2021-03-18 17:42:35 +00:00
|
|
|
pool *ants.Pool
|
2022-12-23 17:35:35 +00:00
|
|
|
frostfsContract util.Uint160
|
2022-01-31 09:24:25 +00:00
|
|
|
balanceClient *balance.Client
|
2022-01-31 11:58:55 +00:00
|
|
|
netmapClient *nmClient.Client
|
2021-03-18 17:42:35 +00:00
|
|
|
morphClient *client.Client
|
|
|
|
epochState EpochState
|
2021-03-23 15:20:44 +00:00
|
|
|
alphabetState AlphabetState
|
2021-03-18 17:42:35 +00:00
|
|
|
converter PrecisionConverter
|
|
|
|
mintEmitLock *sync.Mutex
|
2022-12-30 09:51:49 +00:00
|
|
|
mintEmitCache *lru.Cache[string, uint64]
|
2021-03-18 17:42:35 +00:00
|
|
|
mintEmitThreshold uint64
|
|
|
|
mintEmitValue fixedn.Fixed8
|
|
|
|
gasBalanceThreshold int64
|
2021-05-31 18:14:30 +00:00
|
|
|
|
2023-01-10 13:01:54 +00:00
|
|
|
frostfsIDClient *frostfsid.Client
|
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
|
2021-03-18 17:42:35 +00:00
|
|
|
PoolSize int
|
2023-01-10 13:07:47 +00:00
|
|
|
FrostFSContract util.Uint160
|
|
|
|
FrostFSIDClient *frostfsid.Client
|
2022-01-31 09:24:25 +00:00
|
|
|
BalanceClient *balance.Client
|
2022-01-31 11:58:55 +00:00
|
|
|
NetmapClient *nmClient.Client
|
2021-03-18 17:42:35 +00:00
|
|
|
MorphClient *client.Client
|
|
|
|
EpochState EpochState
|
2021-03-23 15:20:44 +00:00
|
|
|
AlphabetState AlphabetState
|
2021-03-18 17:42:35 +00:00
|
|
|
Converter PrecisionConverter
|
|
|
|
MintEmitCacheSize int
|
|
|
|
MintEmitThreshold uint64 // in epochs
|
|
|
|
MintEmitValue fixedn.Fixed8
|
|
|
|
GasBalanceThreshold int64
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
depositNotification = "Deposit"
|
|
|
|
withdrawNotification = "Withdraw"
|
|
|
|
chequeNotification = "Cheque"
|
2020-09-08 09:01:38 +00:00
|
|
|
configNotification = "SetConfig"
|
2021-05-31 18:14:30 +00:00
|
|
|
bindNotification = "Bind"
|
|
|
|
unbindNotification = "Unbind"
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
// New creates frostfs mainnet contract processor instance.
|
2020-07-24 13:54:03 +00:00
|
|
|
func New(p *Params) (*Processor, error) {
|
|
|
|
switch {
|
|
|
|
case p.Log == nil:
|
2022-12-23 17:35:35 +00:00
|
|
|
return nil, errors.New("ir/frostfs: logger is not set")
|
2020-07-24 13:54:03 +00:00
|
|
|
case p.MorphClient == nil:
|
2022-12-23 17:35:35 +00:00
|
|
|
return nil, errors.New("ir/frostfs: neo:morph client is not set")
|
2020-07-24 13:54:03 +00:00
|
|
|
case p.EpochState == nil:
|
2022-12-23 17:35:35 +00:00
|
|
|
return nil, errors.New("ir/frostfs: global state is not set")
|
2021-03-23 15:20:44 +00:00
|
|
|
case p.AlphabetState == nil:
|
2022-12-23 17:35:35 +00:00
|
|
|
return nil, errors.New("ir/frostfs: global state is not set")
|
2020-10-27 10:49:09 +00:00
|
|
|
case p.Converter == nil:
|
2022-12-23 17:35:35 +00:00
|
|
|
return nil, errors.New("ir/frostfs: balance precision converter is not set")
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
p.Log.Debug("frostfs worker pool", 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 {
|
2022-12-23 17:35:35 +00:00
|
|
|
return nil, fmt.Errorf("ir/frostfs: can't create worker pool: %w", err)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 09:51:49 +00:00
|
|
|
lruCache, err := lru.New[string, uint64](p.MintEmitCacheSize)
|
2020-11-03 11:16:32 +00:00
|
|
|
if err != nil {
|
2022-12-23 17:35:35 +00:00
|
|
|
return nil, fmt.Errorf("ir/frostfs: can't create LRU cache for gas emission: %w", err)
|
2020-11-03 11:16:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
return &Processor{
|
2021-03-18 17:42:35 +00:00
|
|
|
log: p.Log,
|
|
|
|
pool: pool,
|
2023-01-10 13:07:47 +00:00
|
|
|
frostfsContract: p.FrostFSContract,
|
2021-05-31 11:50:11 +00:00
|
|
|
balanceClient: p.BalanceClient,
|
|
|
|
netmapClient: p.NetmapClient,
|
2021-03-18 17:42:35 +00:00
|
|
|
morphClient: p.MorphClient,
|
|
|
|
epochState: p.EpochState,
|
2021-03-23 15:20:44 +00:00
|
|
|
alphabetState: p.AlphabetState,
|
2021-03-18 17:42:35 +00:00
|
|
|
converter: p.Converter,
|
|
|
|
mintEmitLock: new(sync.Mutex),
|
|
|
|
mintEmitCache: lruCache,
|
|
|
|
mintEmitThreshold: p.MintEmitThreshold,
|
|
|
|
mintEmitValue: p.MintEmitValue,
|
|
|
|
gasBalanceThreshold: p.GasBalanceThreshold,
|
2021-05-31 18:14:30 +00:00
|
|
|
|
2023-01-10 13:07:47 +00:00
|
|
|
frostfsIDClient: p.FrostFSIDClient,
|
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-05-31 18:14:30 +00:00
|
|
|
var (
|
2021-08-12 15:24:17 +00:00
|
|
|
parsers = make([]event.NotificationParserInfo, 0, 6)
|
2021-05-31 18:14:30 +00:00
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
p event.NotificationParserInfo
|
2021-05-31 18:14:30 +00:00
|
|
|
)
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
p.SetScriptHash(np.frostfsContract)
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
// deposit event
|
2021-05-31 18:14:30 +00:00
|
|
|
p.SetType(event.TypeFromString(depositNotification))
|
2022-12-23 17:35:35 +00:00
|
|
|
p.SetParser(frostfsEvent.ParseDeposit)
|
2021-05-31 18:14:30 +00:00
|
|
|
parsers = append(parsers, p)
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
// withdraw event
|
2021-05-31 18:14:30 +00:00
|
|
|
p.SetType(event.TypeFromString(withdrawNotification))
|
2022-12-23 17:35:35 +00:00
|
|
|
p.SetParser(frostfsEvent.ParseWithdraw)
|
2021-05-31 18:14:30 +00:00
|
|
|
parsers = append(parsers, p)
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
// cheque event
|
2021-05-31 18:14:30 +00:00
|
|
|
p.SetType(event.TypeFromString(chequeNotification))
|
2022-12-23 17:35:35 +00:00
|
|
|
p.SetParser(frostfsEvent.ParseCheque)
|
2021-05-31 18:14:30 +00:00
|
|
|
parsers = append(parsers, p)
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2020-09-08 09:01:38 +00:00
|
|
|
// config event
|
2021-05-31 18:14:30 +00:00
|
|
|
p.SetType(event.TypeFromString(configNotification))
|
2022-12-23 17:35:35 +00:00
|
|
|
p.SetParser(frostfsEvent.ParseConfig)
|
2021-05-31 18:14:30 +00:00
|
|
|
parsers = append(parsers, p)
|
|
|
|
|
|
|
|
// bind event
|
|
|
|
p.SetType(event.TypeFromString(bindNotification))
|
2022-12-23 17:35:35 +00:00
|
|
|
p.SetParser(frostfsEvent.ParseBind)
|
2021-05-31 18:14:30 +00:00
|
|
|
parsers = append(parsers, p)
|
|
|
|
|
|
|
|
// unbind event
|
|
|
|
p.SetType(event.TypeFromString(unbindNotification))
|
2022-12-23 17:35:35 +00:00
|
|
|
p.SetParser(frostfsEvent.ParseUnbind)
|
2021-05-31 18:14:30 +00:00
|
|
|
parsers = append(parsers, p)
|
2020-09-08 09:01:38 +00:00
|
|
|
|
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-05-31 18:14:30 +00:00
|
|
|
var (
|
2021-08-12 15:24:17 +00:00
|
|
|
handlers = make([]event.NotificationHandlerInfo, 0, 6)
|
2021-05-31 18:14:30 +00:00
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
h event.NotificationHandlerInfo
|
2021-05-31 18:14:30 +00:00
|
|
|
)
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
h.SetScriptHash(np.frostfsContract)
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
// deposit handler
|
2021-05-31 18:14:30 +00:00
|
|
|
h.SetType(event.TypeFromString(depositNotification))
|
|
|
|
h.SetHandler(np.handleDeposit)
|
|
|
|
handlers = append(handlers, h)
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
// withdraw handler
|
2021-05-31 18:14:30 +00:00
|
|
|
h.SetType(event.TypeFromString(withdrawNotification))
|
|
|
|
h.SetHandler(np.handleWithdraw)
|
|
|
|
handlers = append(handlers, h)
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
// cheque handler
|
2021-05-31 18:14:30 +00:00
|
|
|
h.SetType(event.TypeFromString(chequeNotification))
|
|
|
|
h.SetHandler(np.handleCheque)
|
|
|
|
handlers = append(handlers, h)
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2020-09-08 09:01:38 +00:00
|
|
|
// config handler
|
2021-05-31 18:14:30 +00:00
|
|
|
h.SetType(event.TypeFromString(configNotification))
|
|
|
|
h.SetHandler(np.handleConfig)
|
|
|
|
handlers = append(handlers, h)
|
|
|
|
|
|
|
|
// bind handler
|
|
|
|
h.SetType(event.TypeFromString(bindNotification))
|
|
|
|
h.SetHandler(np.handleBind)
|
|
|
|
handlers = append(handlers, h)
|
|
|
|
|
|
|
|
// unbind handler
|
|
|
|
h.SetType(event.TypeFromString(unbindNotification))
|
|
|
|
h.SetHandler(np.handleUnbind)
|
|
|
|
handlers = append(handlers, h)
|
2020-09-08 09:01:38 +00:00
|
|
|
|
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 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerNotaryHandlers for the 'event.Listener' event producer.
|
|
|
|
func (np *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// TimersHandlers for the 'Timers' event producer.
|
2021-08-12 15:24:17 +00:00
|
|
|
func (np *Processor) TimersHandlers() []event.NotificationHandlerInfo {
|
2020-07-24 13:54:03 +00:00
|
|
|
return nil
|
|
|
|
}
|