frostfs-node/pkg/innerring/processors/balance/processor.go

122 lines
3.2 KiB
Go
Raw Normal View History

2020-07-24 13:54:03 +00:00
package balance
import (
"errors"
"fmt"
"github.com/nspcc-dev/neo-go/pkg/util"
neofscontract "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs"
2020-07-24 13:54:03 +00:00
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
balanceEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/balance"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
2020-07-24 13:54:03 +00:00
"github.com/panjf2000/ants/v2"
"go.uber.org/zap"
)
type (
// AlphabetState is a callback interface for inner ring global state.
AlphabetState interface {
IsAlphabet() bool
2020-07-24 13:54:03 +00:00
}
// PrecisionConverter converts balance amount values.
PrecisionConverter interface {
ToFixed8(int64) int64
}
// Processor of events produced by balance contract in the morphchain.
2020-07-24 13:54:03 +00:00
Processor struct {
log *logger.Logger
pool *ants.Pool
neofsClient *neofscontract.Client
balanceSC util.Uint160
alphabetState AlphabetState
converter PrecisionConverter
2020-07-24 13:54:03 +00:00
}
// Params of the processor constructor.
Params struct {
Log *logger.Logger
PoolSize int
NeoFSClient *neofscontract.Client
BalanceSC util.Uint160
AlphabetState AlphabetState
Converter PrecisionConverter
2020-07-24 13:54:03 +00:00
}
)
const (
lockNotification = "Lock"
)
// 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")
case p.AlphabetState == nil:
2020-07-24 13:54:03 +00:00
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")
2020-07-24 13:54:03 +00:00
}
p.Log.Debug("balance worker pool", 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)
2020-07-24 13:54:03 +00:00
}
return &Processor{
log: p.Log,
pool: pool,
neofsClient: p.NeoFSClient,
balanceSC: p.BalanceSC,
alphabetState: p.AlphabetState,
converter: p.Converter,
2020-07-24 13:54:03 +00:00
}, nil
}
// 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
lock := event.NotificationParserInfo{}
2020-07-24 13:54:03 +00:00
lock.SetType(lockNotification)
lock.SetScriptHash(bp.balanceSC)
2020-07-24 13:54:03 +00:00
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
2020-07-24 13:54:03 +00:00
// lock handler
lock := event.NotificationHandlerInfo{}
2020-07-24 13:54:03 +00:00
lock.SetType(lockNotification)
lock.SetScriptHash(bp.balanceSC)
2020-07-24 13:54:03 +00:00
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
}
2020-07-24 13:54:03 +00:00
// TimersHandlers for the 'Timers' event producer.
func (bp *Processor) TimersHandlers() []event.NotificationHandlerInfo {
2020-07-24 13:54:03 +00:00
return nil
}