forked from TrueCloudLab/frostfs-node
121 lines
3.3 KiB
Go
121 lines
3.3 KiB
Go
package balance
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
|
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"
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
"github.com/panjf2000/ants/v2"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type (
|
|
// AlphabetState is a callback interface for inner ring global state.
|
|
AlphabetState interface {
|
|
IsAlphabet() bool
|
|
}
|
|
|
|
// PrecisionConverter converts balance amount values.
|
|
PrecisionConverter interface {
|
|
ToFixed8(int64) int64
|
|
}
|
|
|
|
FrostFSClient interface {
|
|
Cheque(p frostfscontract.ChequePrm) error
|
|
}
|
|
|
|
// Processor of events produced by balance contract in the morphchain.
|
|
Processor struct {
|
|
log *logger.Logger
|
|
pool *ants.Pool
|
|
frostfsClient FrostFSClient
|
|
balanceSC util.Uint160
|
|
alphabetState AlphabetState
|
|
converter PrecisionConverter
|
|
}
|
|
|
|
// Params of the processor constructor.
|
|
Params struct {
|
|
Log *logger.Logger
|
|
PoolSize int
|
|
FrostFSClient FrostFSClient
|
|
BalanceSC util.Uint160
|
|
AlphabetState AlphabetState
|
|
Converter PrecisionConverter
|
|
}
|
|
)
|
|
|
|
const (
|
|
lockNotification = "Lock"
|
|
)
|
|
|
|
// New creates a balance contract processor instance.
|
|
func New(p *Params) (*Processor, error) {
|
|
switch {
|
|
case p.Log == nil:
|
|
return nil, errors.New("ir/balance: logger is not set")
|
|
case p.AlphabetState == nil:
|
|
return nil, errors.New("ir/balance: global state is not set")
|
|
case p.Converter == nil:
|
|
return nil, errors.New("ir/balance: balance precision converter is not set")
|
|
}
|
|
|
|
p.Log.Debug(logs.BalanceBalanceWorkerPool, zap.Int("size", p.PoolSize))
|
|
|
|
pool, err := ants.NewPool(p.PoolSize, ants.WithNonblocking(true))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ir/balance: can't create worker pool: %w", err)
|
|
}
|
|
|
|
return &Processor{
|
|
log: p.Log,
|
|
pool: pool,
|
|
frostfsClient: p.FrostFSClient,
|
|
balanceSC: p.BalanceSC,
|
|
alphabetState: p.AlphabetState,
|
|
converter: p.Converter,
|
|
}, nil
|
|
}
|
|
|
|
// ListenerNotificationParsers for the 'event.Listener' event producer.
|
|
func (bp *Processor) ListenerNotificationParsers() []event.NotificationParserInfo {
|
|
var parsers []event.NotificationParserInfo
|
|
|
|
// new lock event
|
|
lock := event.NotificationParserInfo{}
|
|
lock.SetType(lockNotification)
|
|
lock.SetScriptHash(bp.balanceSC)
|
|
lock.SetParser(balanceEvent.ParseLock)
|
|
parsers = append(parsers, lock)
|
|
|
|
return parsers
|
|
}
|
|
|
|
// ListenerNotificationHandlers for the 'event.Listener' event producer.
|
|
func (bp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
|
|
var handlers []event.NotificationHandlerInfo
|
|
|
|
// lock handler
|
|
lock := event.NotificationHandlerInfo{}
|
|
lock.SetType(lockNotification)
|
|
lock.SetScriptHash(bp.balanceSC)
|
|
lock.SetHandler(bp.handleLock)
|
|
handlers = append(handlers, lock)
|
|
|
|
return handlers
|
|
}
|
|
|
|
// 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
|
|
}
|