2022-12-23 17:35:35 +00:00
|
|
|
package frostfs
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
import (
|
2024-10-21 07:22:54 +00:00
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-11-03 11:16:32 +00:00
|
|
|
"sync"
|
|
|
|
|
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/morph/client/balance"
|
|
|
|
nmClient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
|
|
|
|
frostfsEvent "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event/frostfs"
|
|
|
|
"git.frostfs.info/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
|
|
|
|
}
|
|
|
|
|
2023-04-26 13:25:50 +00:00
|
|
|
BalanceClient interface {
|
|
|
|
Mint(p balance.MintPrm) error
|
|
|
|
Lock(p balance.LockPrm) error
|
|
|
|
Burn(p balance.BurnPrm) error
|
|
|
|
}
|
|
|
|
|
|
|
|
NetmapClient interface {
|
|
|
|
SetConfig(p nmClient.SetConfigPrm) error
|
|
|
|
}
|
|
|
|
|
|
|
|
MorphClient interface {
|
|
|
|
GasBalance() (res int64, err error)
|
|
|
|
TransferGas(receiver util.Uint160, amount fixedn.Fixed8) error
|
|
|
|
}
|
|
|
|
|
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
|
2023-05-26 10:24:41 +00:00
|
|
|
metrics metrics.Register
|
2021-03-18 17:42:35 +00:00
|
|
|
pool *ants.Pool
|
2022-12-23 17:35:35 +00:00
|
|
|
frostfsContract util.Uint160
|
2023-04-26 13:25:50 +00:00
|
|
|
balanceClient BalanceClient
|
|
|
|
netmapClient NetmapClient
|
|
|
|
morphClient MorphClient
|
2021-03-18 17:42:35 +00:00
|
|
|
epochState EpochState
|
2021-03-23 15:20:44 +00:00
|
|
|
alphabetState AlphabetState
|
2021-03-18 17:42:35 +00:00
|
|
|
converter PrecisionConverter
|
2023-05-30 07:14:37 +00:00
|
|
|
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
|
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-03-18 17:42:35 +00:00
|
|
|
PoolSize int
|
2023-01-10 13:07:47 +00:00
|
|
|
FrostFSContract util.Uint160
|
2023-04-26 13:25:50 +00:00
|
|
|
BalanceClient BalanceClient
|
|
|
|
NetmapClient NetmapClient
|
|
|
|
MorphClient MorphClient
|
2021-03-18 17:42:35 +00:00
|
|
|
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"
|
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
|
|
|
}
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
p.Log.Debug(context.Background(), logs.FrostFSFrostfsWorkerPool, 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
|
|
|
}
|
|
|
|
|
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{
|
2021-03-18 17:42:35 +00:00
|
|
|
log: p.Log,
|
2023-05-26 10:24:41 +00:00
|
|
|
metrics: metricsRegister,
|
2021-03-18 17:42:35 +00:00
|
|
|
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,
|
|
|
|
mintEmitCache: lruCache,
|
|
|
|
mintEmitThreshold: p.MintEmitThreshold,
|
|
|
|
mintEmitValue: p.MintEmitValue,
|
|
|
|
gasBalanceThreshold: p.GasBalanceThreshold,
|
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)
|
|
|
|
|
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)
|
|
|
|
|
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
|
|
|
|
}
|