2021-03-25 13:23:53 +00:00
|
|
|
package governance
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2021-05-18 07:40:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
2021-03-25 13:23:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2021-06-01 11:35:53 +00:00
|
|
|
neofscontract "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs/wrapper"
|
2021-05-31 11:50:11 +00:00
|
|
|
nmWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper"
|
2021-03-25 13:23:53 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
2021-05-18 07:40:21 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event/rolemanagement"
|
2021-03-25 13:23:53 +00:00
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-06-23 13:29:46 +00:00
|
|
|
// ProcessorPoolSize limits pool size for governance Processor. Processor manages
|
|
|
|
// governance sync tasks. This process must not be interrupted by other sync
|
|
|
|
// operation, so we limit pool size for processor to one.
|
2021-03-25 13:23:53 +00:00
|
|
|
const ProcessorPoolSize = 1
|
|
|
|
|
|
|
|
type (
|
|
|
|
// AlphabetState is a callback interface for inner ring global state.
|
|
|
|
AlphabetState interface {
|
|
|
|
IsAlphabet() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Voter is a callback interface for alphabet contract voting.
|
|
|
|
Voter interface {
|
|
|
|
VoteForSidechainValidator(keys keys.PublicKeys) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// EpochState is a callback interface for inner ring global state.
|
|
|
|
EpochState interface {
|
|
|
|
EpochCounter() uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Processor of events related to governance in the network.
|
|
|
|
Processor struct {
|
2021-06-01 11:35:53 +00:00
|
|
|
log *zap.Logger
|
|
|
|
pool *ants.Pool
|
|
|
|
neofsClient *neofscontract.ClientWrapper
|
|
|
|
netmapClient *nmWrapper.Wrapper
|
2021-03-25 13:23:53 +00:00
|
|
|
|
|
|
|
alphabetState AlphabetState
|
|
|
|
epochState EpochState
|
|
|
|
voter Voter
|
|
|
|
|
|
|
|
mainnetClient *client.Client
|
|
|
|
morphClient *client.Client
|
2021-04-29 13:40:34 +00:00
|
|
|
|
|
|
|
notaryDisabled bool
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Params of the processor constructor.
|
|
|
|
Params struct {
|
2021-06-01 11:35:53 +00:00
|
|
|
Log *zap.Logger
|
2021-03-25 13:23:53 +00:00
|
|
|
|
|
|
|
AlphabetState AlphabetState
|
|
|
|
EpochState EpochState
|
|
|
|
Voter Voter
|
|
|
|
|
|
|
|
MorphClient *client.Client
|
|
|
|
MainnetClient *client.Client
|
2021-06-01 11:35:53 +00:00
|
|
|
NeoFSClient *neofscontract.ClientWrapper
|
2021-05-31 11:50:11 +00:00
|
|
|
NetmapClient *nmWrapper.Wrapper
|
2021-04-29 13:40:34 +00:00
|
|
|
|
|
|
|
NotaryDisabled bool
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// New creates balance contract processor instance.
|
|
|
|
func New(p *Params) (*Processor, error) {
|
|
|
|
switch {
|
|
|
|
case p.Log == nil:
|
|
|
|
return nil, errors.New("ir/governance: logger is not set")
|
|
|
|
case p.MainnetClient == nil:
|
|
|
|
return nil, errors.New("ir/governance: neo:mainnet client is not set")
|
|
|
|
case p.MorphClient == nil:
|
|
|
|
return nil, errors.New("ir/governance: neo:sidechain client is not set")
|
|
|
|
case p.AlphabetState == nil:
|
|
|
|
return nil, errors.New("ir/governance: global state is not set")
|
|
|
|
case p.EpochState == nil:
|
|
|
|
return nil, errors.New("ir/governance: global state is not set")
|
|
|
|
case p.Voter == nil:
|
|
|
|
return nil, errors.New("ir/governance: global state is not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := ants.NewPool(ProcessorPoolSize, ants.WithNonblocking(true))
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("ir/governance: can't create worker pool: %w", err)
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Processor{
|
2021-04-29 13:40:34 +00:00
|
|
|
log: p.Log,
|
|
|
|
pool: pool,
|
2021-06-01 11:35:53 +00:00
|
|
|
neofsClient: p.NeoFSClient,
|
2021-05-31 11:50:11 +00:00
|
|
|
netmapClient: p.NetmapClient,
|
2021-04-29 13:40:34 +00:00
|
|
|
alphabetState: p.AlphabetState,
|
|
|
|
epochState: p.EpochState,
|
|
|
|
voter: p.Voter,
|
|
|
|
mainnetClient: p.MainnetClient,
|
|
|
|
morphClient: p.MorphClient,
|
|
|
|
notaryDisabled: p.NotaryDisabled,
|
2021-03-25 13:23:53 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerParsers for the 'event.Listener' event producer.
|
|
|
|
func (gp *Processor) ListenerParsers() []event.ParserInfo {
|
2021-05-18 07:40:21 +00:00
|
|
|
var pi event.ParserInfo
|
|
|
|
pi.SetScriptHash(gp.mainnetClient.GetDesignateHash())
|
|
|
|
pi.SetType(event.TypeFromString(native.DesignationEventName))
|
|
|
|
pi.SetParser(rolemanagement.ParseDesignate)
|
|
|
|
return []event.ParserInfo{pi}
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerHandlers for the 'event.Listener' event producer.
|
|
|
|
func (gp *Processor) ListenerHandlers() []event.HandlerInfo {
|
2021-05-18 07:40:21 +00:00
|
|
|
var hi event.HandlerInfo
|
|
|
|
hi.SetScriptHash(gp.mainnetClient.GetDesignateHash())
|
|
|
|
hi.SetType(event.TypeFromString(native.DesignationEventName))
|
|
|
|
hi.SetHandler(gp.HandleAlphabetSync)
|
|
|
|
return []event.HandlerInfo{hi}
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TimersHandlers for the 'Timers' event producer.
|
|
|
|
func (gp *Processor) TimersHandlers() []event.HandlerInfo {
|
|
|
|
return nil
|
|
|
|
}
|