2020-10-12 10:17:40 +00:00
|
|
|
package alphabet
|
|
|
|
|
|
|
|
import (
|
2024-10-21 07:22:54 +00:00
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-05-12 07:41:04 +00:00
|
|
|
"sync"
|
2023-04-26 07:51:30 +00:00
|
|
|
"time"
|
2021-05-18 08:12:51 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-05-26 10:24:41 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/metrics"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
2023-04-26 07:51:30 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2020-10-12 10:17:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Indexer is a callback interface for inner ring global state.
|
|
|
|
Indexer interface {
|
2021-03-23 15:20:44 +00:00
|
|
|
AlphabetIndex() int
|
2020-10-12 10:17:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-21 06:53:18 +00:00
|
|
|
// Contracts is an interface of the storage
|
|
|
|
// of the alphabet contract addresses.
|
|
|
|
Contracts interface {
|
2022-04-21 11:28:05 +00:00
|
|
|
// GetByIndex must return the address of the
|
2021-02-21 06:53:18 +00:00
|
|
|
// alphabet contract by index of the glagolitic
|
|
|
|
// letter (e.g 0 for Az, 40 for Izhitsa).
|
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Must return false if the index does not
|
|
|
|
// match any alphabet contract.
|
2021-02-21 06:53:18 +00:00
|
|
|
GetByIndex(int) (util.Uint160, bool)
|
|
|
|
}
|
|
|
|
|
2023-04-26 07:51:30 +00:00
|
|
|
netmapClient interface {
|
|
|
|
NetMap() (*netmap.NetMap, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
morphClient interface {
|
2023-11-07 15:13:26 +00:00
|
|
|
Invoke(contract util.Uint160, fee fixedn.Fixed8, method string, args ...any) (uint32, error)
|
2023-04-26 07:51:30 +00:00
|
|
|
TransferGas(receiver util.Uint160, amount fixedn.Fixed8) error
|
|
|
|
BatchTransferGas(receivers []util.Uint160, amount fixedn.Fixed8) error
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Processor of events produced for alphabet contracts in the sidechain.
|
2020-10-12 10:17:40 +00:00
|
|
|
Processor struct {
|
2023-05-12 07:41:04 +00:00
|
|
|
parsedWallets []util.Uint160
|
|
|
|
// protects parsedWallets from concurrent change
|
2023-05-30 07:14:37 +00:00
|
|
|
pwLock sync.RWMutex
|
2022-09-28 07:41:01 +00:00
|
|
|
log *logger.Logger
|
2023-05-26 10:24:41 +00:00
|
|
|
metrics metrics.Register
|
2020-10-12 10:17:40 +00:00
|
|
|
pool *ants.Pool
|
2021-02-21 06:53:18 +00:00
|
|
|
alphabetContracts Contracts
|
2023-04-26 07:51:30 +00:00
|
|
|
netmapClient netmapClient
|
|
|
|
morphClient morphClient
|
2020-10-12 10:17:40 +00:00
|
|
|
irList Indexer
|
2020-11-03 08:03:15 +00:00
|
|
|
storageEmission uint64
|
2020-10-12 10:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Params of the processor constructor.
|
|
|
|
Params struct {
|
2023-03-09 13:19:39 +00:00
|
|
|
ParsedWallets []util.Uint160
|
2022-09-28 07:41:01 +00:00
|
|
|
Log *logger.Logger
|
2023-05-26 10:24:41 +00:00
|
|
|
Metrics metrics.Register
|
2020-10-12 10:17:40 +00:00
|
|
|
PoolSize int
|
2021-02-21 06:53:18 +00:00
|
|
|
AlphabetContracts Contracts
|
2023-04-26 07:51:30 +00:00
|
|
|
NetmapClient netmapClient
|
|
|
|
MorphClient morphClient
|
2020-10-12 10:17:40 +00:00
|
|
|
IRList Indexer
|
2020-11-03 08:03:15 +00:00
|
|
|
StorageEmission uint64
|
2020-10-12 10:17:40 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
// New creates a frostfs mainnet contract processor instance.
|
2020-10-12 10:17:40 +00:00
|
|
|
func New(p *Params) (*Processor, error) {
|
|
|
|
switch {
|
|
|
|
case p.Log == nil:
|
|
|
|
return nil, errors.New("ir/alphabet: logger is not set")
|
|
|
|
case p.MorphClient == nil:
|
|
|
|
return nil, errors.New("ir/alphabet: neo:morph client is not set")
|
|
|
|
case p.IRList == nil:
|
|
|
|
return nil, errors.New("ir/alphabet: global state is not set")
|
|
|
|
}
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
p.Log.Debug(context.Background(), logs.AlphabetAlphabetWorkerPool, zap.Int("size", p.PoolSize))
|
2020-10-12 10:17:40 +00:00
|
|
|
|
|
|
|
pool, err := ants.NewPool(p.PoolSize, ants.WithNonblocking(true))
|
|
|
|
if err != nil {
|
2022-12-23 17:35:35 +00:00
|
|
|
return nil, fmt.Errorf("ir/frostfs: can't create worker pool: %w", err)
|
2020-10-12 10:17:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 10:24:41 +00:00
|
|
|
metricsRegister := p.Metrics
|
|
|
|
if metricsRegister == nil {
|
|
|
|
metricsRegister = metrics.DefaultRegister{}
|
|
|
|
}
|
|
|
|
|
2020-10-12 10:17:40 +00:00
|
|
|
return &Processor{
|
2023-03-09 13:19:39 +00:00
|
|
|
parsedWallets: p.ParsedWallets,
|
2020-10-12 10:17:40 +00:00
|
|
|
log: p.Log,
|
2023-05-26 10:24:41 +00:00
|
|
|
metrics: metricsRegister,
|
2020-10-12 10:17:40 +00:00
|
|
|
pool: pool,
|
|
|
|
alphabetContracts: p.AlphabetContracts,
|
2021-05-31 11:50:11 +00:00
|
|
|
netmapClient: p.NetmapClient,
|
2020-10-12 10:17:40 +00:00
|
|
|
morphClient: p.MorphClient,
|
|
|
|
irList: p.IRList,
|
2020-11-03 08:03:15 +00:00
|
|
|
storageEmission: p.StorageEmission,
|
2020-10-12 10:17:40 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-05-12 07:41:04 +00:00
|
|
|
func (ap *Processor) SetParsedWallets(parsedWallets []util.Uint160) {
|
|
|
|
ap.pwLock.Lock()
|
|
|
|
ap.parsedWallets = parsedWallets
|
|
|
|
ap.pwLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
// ListenerNotificationParsers for the 'event.Listener' event producer.
|
2021-09-08 08:28:41 +00:00
|
|
|
func (ap *Processor) ListenerNotificationParsers() []event.NotificationParserInfo {
|
2020-10-12 10:17:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
// ListenerNotificationHandlers for the 'event.Listener' event producer.
|
2021-09-08 08:28:41 +00:00
|
|
|
func (ap *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerNotaryParsers for the 'event.Listener' event producer.
|
|
|
|
func (ap *Processor) ListenerNotaryParsers() []event.NotaryParserInfo {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerNotaryHandlers for the 'event.Listener' event producer.
|
|
|
|
func (ap *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo {
|
2020-10-12 10:17:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-04-26 07:51:30 +00:00
|
|
|
|
|
|
|
// WaitPoolRunning waits while pool has running tasks
|
|
|
|
// For use in test only.
|
|
|
|
func (ap *Processor) WaitPoolRunning() {
|
|
|
|
for ap.pool.Running() > 0 {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|