2020-07-24 13:54:03 +00:00
|
|
|
package balance
|
|
|
|
|
|
|
|
import (
|
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-03-07 13:38:26 +00:00
|
|
|
frostfscontract "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/frostfs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
|
|
|
|
balanceEvent "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event/balance"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
2022-07-28 15:04:07 +00:00
|
|
|
"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 (
|
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 {
|
|
|
|
ToFixed8(int64) int64
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Processor of events produced by balance contract in the morphchain.
|
2020-07-24 13:54:03 +00:00
|
|
|
Processor struct {
|
2022-09-28 07:41:01 +00:00
|
|
|
log *logger.Logger
|
2021-09-15 10:50:07 +00:00
|
|
|
pool *ants.Pool
|
2023-01-10 13:07:47 +00:00
|
|
|
frostfsClient *frostfscontract.Client
|
2022-07-28 15:04:07 +00:00
|
|
|
balanceSC util.Uint160
|
2021-09-15 10:50:07 +00:00
|
|
|
alphabetState AlphabetState
|
|
|
|
converter PrecisionConverter
|
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-09-15 10:50:07 +00:00
|
|
|
PoolSize int
|
2023-01-10 13:07:47 +00:00
|
|
|
FrostFSClient *frostfscontract.Client
|
2022-07-28 15:04:07 +00:00
|
|
|
BalanceSC util.Uint160
|
2021-09-15 10:50:07 +00:00
|
|
|
AlphabetState AlphabetState
|
|
|
|
Converter PrecisionConverter
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
lockNotification = "Lock"
|
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// New creates a balance contract processor instance.
|
2020-07-24 13:54:03 +00:00
|
|
|
func New(p *Params) (*Processor, error) {
|
|
|
|
switch {
|
|
|
|
case p.Log == nil:
|
|
|
|
return nil, errors.New("ir/balance: logger 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/balance: global state is not set")
|
2020-10-27 10:49:09 +00:00
|
|
|
case p.Converter == nil:
|
|
|
|
return nil, errors.New("ir/balance: balance precision converter is not set")
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
p.Log.Debug(logs.BalanceBalanceWorkerPool, 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/balance: can't create worker pool: %w", err)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Processor{
|
2021-09-15 10:50:07 +00:00
|
|
|
log: p.Log,
|
|
|
|
pool: pool,
|
2023-01-10 13:07:47 +00:00
|
|
|
frostfsClient: p.FrostFSClient,
|
2022-07-28 15:04:07 +00:00
|
|
|
balanceSC: p.BalanceSC,
|
2021-09-15 10:50:07 +00:00
|
|
|
alphabetState: p.AlphabetState,
|
|
|
|
converter: p.Converter,
|
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 (bp *Processor) ListenerNotificationParsers() []event.NotificationParserInfo {
|
|
|
|
var parsers []event.NotificationParserInfo
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
// new lock event
|
2021-08-12 15:24:17 +00:00
|
|
|
lock := event.NotificationParserInfo{}
|
2020-07-24 13:54:03 +00:00
|
|
|
lock.SetType(lockNotification)
|
2022-07-28 15:04:07 +00:00
|
|
|
lock.SetScriptHash(bp.balanceSC)
|
2020-07-24 13:54:03 +00:00
|
|
|
lock.SetParser(balanceEvent.ParseLock)
|
|
|
|
parsers = append(parsers, lock)
|
|
|
|
|
|
|
|
return parsers
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
// ListenerNotificationHandlers for the 'event.Listener' event producer.
|
|
|
|
func (bp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
|
|
|
|
var handlers []event.NotificationHandlerInfo
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
// lock handler
|
2021-08-12 15:24:17 +00:00
|
|
|
lock := event.NotificationHandlerInfo{}
|
2020-07-24 13:54:03 +00:00
|
|
|
lock.SetType(lockNotification)
|
2022-07-28 15:04:07 +00:00
|
|
|
lock.SetScriptHash(bp.balanceSC)
|
2020-07-24 13:54:03 +00:00
|
|
|
lock.SetHandler(bp.handleLock)
|
|
|
|
handlers = append(handlers, lock)
|
|
|
|
|
|
|
|
return handlers
|
|
|
|
}
|
|
|
|
|
2021-09-08 08:28:41 +00:00
|
|
|
// ListenerNotaryParsers for the 'event.Listener' event producer.
|
|
|
|
func (bp *Processor) ListenerNotaryParsers() []event.NotaryParserInfo {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerNotaryHandlers for the 'event.Listener' event producer.
|
|
|
|
func (bp *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 (bp *Processor) TimersHandlers() []event.NotificationHandlerInfo {
|
2020-07-24 13:54:03 +00:00
|
|
|
return nil
|
|
|
|
}
|