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

112 lines
3 KiB
Go
Raw Normal View History

2020-07-24 13:54:03 +00:00
package balance
import (
"context"
"errors"
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/metrics"
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"
2020-07-24 13:54:03 +00:00
"github.com/panjf2000/ants/v2"
)
type (
// AlphabetState is a callback interface for inner ring global state.
AlphabetState interface {
IsAlphabet(context.Context) bool
2020-07-24 13:54:03 +00:00
}
// PrecisionConverter converts balance amount values.
PrecisionConverter interface {
ToFixed8(int64) int64
}
FrostFSClient interface {
Cheque(ctx context.Context, p frostfscontract.ChequePrm) error
}
// Processor of events produced by balance contract in the morphchain.
2020-07-24 13:54:03 +00:00
Processor struct {
log *logger.Logger
metrics metrics.Register
pool *ants.Pool
frostfsClient FrostFSClient
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
Metrics metrics.Register
PoolSize int
FrostFSClient FrostFSClient
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
}
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
}
metricsRegister := p.Metrics
if metricsRegister == nil {
metricsRegister = metrics.DefaultRegister{}
}
2020-07-24 13:54:03 +00:00
return &Processor{
log: p.Log,
metrics: metricsRegister,
pool: pool,
frostfsClient: p.FrostFSClient,
balanceSC: p.BalanceSC,
alphabetState: p.AlphabetState,
converter: p.Converter,
2020-07-24 13:54:03 +00:00
}, nil
}
// ListenerNotificationHandlers for the 'event.Listener' event producer.
func (bp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
return []event.NotificationHandlerInfo{
{
Contract: bp.balanceSC,
Type: lockNotification,
Parser: balanceEvent.ParseLock,
Handlers: []event.Handler{bp.handleLock},
},
}
2020-07-24 13:54:03 +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
}