2020-07-24 13:54:03 +00:00
|
|
|
package balance
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-04-29 13:40:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/config"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
|
|
|
balanceEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/balance"
|
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2021-03-23 15:20:44 +00:00
|
|
|
// AlphabetState is a callback interface for inner ring global state
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// Processor of events produced by balance contract in morph chain.
|
|
|
|
Processor struct {
|
|
|
|
log *zap.Logger
|
|
|
|
pool *ants.Pool
|
|
|
|
neofsContract util.Uint160
|
|
|
|
balanceContract util.Uint160
|
|
|
|
mainnetClient *client.Client
|
2021-03-23 15:20:44 +00:00
|
|
|
alphabetState AlphabetState
|
2020-10-27 10:49:09 +00:00
|
|
|
converter PrecisionConverter
|
2021-04-29 13:40:34 +00:00
|
|
|
feeProvider *config.FeeConfig
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Params of the processor constructor.
|
|
|
|
Params struct {
|
|
|
|
Log *zap.Logger
|
|
|
|
PoolSize int
|
|
|
|
NeoFSContract util.Uint160
|
|
|
|
BalanceContract util.Uint160
|
|
|
|
MainnetClient *client.Client
|
2021-03-23 15:20:44 +00:00
|
|
|
AlphabetState AlphabetState
|
2020-10-27 10:49:09 +00:00
|
|
|
Converter PrecisionConverter
|
2021-04-29 13:40:34 +00:00
|
|
|
FeeProvider *config.FeeConfig
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
lockNotification = "Lock"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New creates 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.MainnetClient == nil:
|
|
|
|
return nil, errors.New("ir/balance: neo:mainnet client 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")
|
2021-04-29 13:40:34 +00:00
|
|
|
case p.FeeProvider == nil:
|
|
|
|
return nil, errors.New("ir/balance: fee provider 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 {
|
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{
|
|
|
|
log: p.Log,
|
|
|
|
pool: pool,
|
|
|
|
neofsContract: p.NeoFSContract,
|
|
|
|
balanceContract: p.BalanceContract,
|
|
|
|
mainnetClient: p.MainnetClient,
|
2021-03-23 15:20:44 +00:00
|
|
|
alphabetState: p.AlphabetState,
|
2020-10-27 10:49:09 +00:00
|
|
|
converter: p.Converter,
|
2021-04-29 13:40:34 +00:00
|
|
|
feeProvider: p.FeeProvider,
|
2020-07-24 13:54:03 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerParsers for the 'event.Listener' event producer.
|
|
|
|
func (bp *Processor) ListenerParsers() []event.ParserInfo {
|
|
|
|
var parsers []event.ParserInfo
|
|
|
|
|
|
|
|
// new lock event
|
|
|
|
lock := event.ParserInfo{}
|
|
|
|
lock.SetType(lockNotification)
|
|
|
|
lock.SetScriptHash(bp.balanceContract)
|
|
|
|
lock.SetParser(balanceEvent.ParseLock)
|
|
|
|
parsers = append(parsers, lock)
|
|
|
|
|
|
|
|
return parsers
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerHandlers for the 'event.Listener' event producer.
|
|
|
|
func (bp *Processor) ListenerHandlers() []event.HandlerInfo {
|
|
|
|
var handlers []event.HandlerInfo
|
|
|
|
|
|
|
|
// lock handler
|
|
|
|
lock := event.HandlerInfo{}
|
|
|
|
lock.SetType(lockNotification)
|
|
|
|
lock.SetScriptHash(bp.balanceContract)
|
|
|
|
lock.SetHandler(bp.handleLock)
|
|
|
|
handlers = append(handlers, lock)
|
|
|
|
|
|
|
|
return handlers
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimersHandlers for the 'Timers' event producer.
|
|
|
|
func (bp *Processor) TimersHandlers() []event.HandlerInfo {
|
|
|
|
return nil
|
|
|
|
}
|