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"
|
2021-08-26 07:59:02 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-03-25 13:23:53 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2022-01-31 10:15:36 +00:00
|
|
|
neofscontract "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs"
|
2022-01-31 11:58:55 +00:00
|
|
|
nmClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
|
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"
|
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ProcessorPoolSize limits the pool size for governance Processor. Processor manages
|
2021-06-23 13:29:46 +00:00
|
|
|
// governance sync tasks. This process must not be interrupted by other sync
|
2022-04-21 11:28:05 +00:00
|
|
|
// operation, so we limit the pool size for the processor to one.
|
2021-03-25 13:23:53 +00:00
|
|
|
const ProcessorPoolSize = 1
|
|
|
|
|
|
|
|
type (
|
2021-07-22 10:04:37 +00:00
|
|
|
// AlphabetState is a callback interface for innerring global state.
|
2021-03-25 13:23:53 +00:00
|
|
|
AlphabetState interface {
|
|
|
|
IsAlphabet() bool
|
|
|
|
}
|
2021-11-10 11:05:51 +00:00
|
|
|
)
|
2021-03-25 13:23:53 +00:00
|
|
|
|
2021-11-10 11:05:51 +00:00
|
|
|
// VoteValidatorPrm groups parameters of the VoteForSidechainValidator
|
|
|
|
// operation.
|
|
|
|
type VoteValidatorPrm struct {
|
|
|
|
Validators keys.PublicKeys
|
|
|
|
Hash *util.Uint256 // hash of the transaction that triggered voting
|
|
|
|
}
|
2021-03-25 13:23:53 +00:00
|
|
|
|
2021-11-10 11:05:51 +00:00
|
|
|
// Voter is a callback interface for alphabet contract voting.
|
|
|
|
type Voter interface {
|
|
|
|
VoteForSidechainValidator(VoteValidatorPrm) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
2021-07-22 10:04:37 +00:00
|
|
|
// EpochState is a callback interface for innerring global state.
|
2021-03-25 13:23:53 +00:00
|
|
|
EpochState interface {
|
|
|
|
EpochCounter() uint64
|
|
|
|
}
|
|
|
|
|
2021-07-22 10:04:37 +00:00
|
|
|
// IRFetcher is a callback interface for innerring keys.
|
|
|
|
// Implementation must take into account availability of
|
|
|
|
// the notary contract.
|
|
|
|
IRFetcher interface {
|
|
|
|
InnerRingKeys() (keys.PublicKeys, error)
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:23:53 +00:00
|
|
|
// 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
|
2022-01-31 10:15:36 +00:00
|
|
|
neofsClient *neofscontract.Client
|
2022-01-31 11:58:55 +00:00
|
|
|
netmapClient *nmClient.Client
|
2021-03-25 13:23:53 +00:00
|
|
|
|
|
|
|
alphabetState AlphabetState
|
|
|
|
epochState EpochState
|
|
|
|
voter Voter
|
2021-07-22 10:04:37 +00:00
|
|
|
irFetcher IRFetcher
|
2021-03-25 13:23:53 +00:00
|
|
|
|
|
|
|
mainnetClient *client.Client
|
|
|
|
morphClient *client.Client
|
2021-04-29 13:40:34 +00:00
|
|
|
|
|
|
|
notaryDisabled bool
|
2021-08-26 07:59:02 +00:00
|
|
|
|
|
|
|
designate util.Uint160
|
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
|
2021-07-22 10:04:37 +00:00
|
|
|
IRFetcher IRFetcher
|
2021-03-25 13:23:53 +00:00
|
|
|
|
|
|
|
MorphClient *client.Client
|
|
|
|
MainnetClient *client.Client
|
2022-01-31 10:15:36 +00:00
|
|
|
NeoFSClient *neofscontract.Client
|
2022-01-31 11:58:55 +00:00
|
|
|
NetmapClient *nmClient.Client
|
2021-04-29 13:40:34 +00:00
|
|
|
|
|
|
|
NotaryDisabled bool
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// New creates a balance contract processor instance.
|
2021-03-25 13:23:53 +00:00
|
|
|
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")
|
2021-07-22 10:04:37 +00:00
|
|
|
case p.IRFetcher == nil:
|
|
|
|
return nil, errors.New("ir/governance: innerring keys fetcher is not set")
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-08-26 07:59:02 +00:00
|
|
|
// result is cached by neo-go, so we can pre-calc it
|
|
|
|
designate, err := p.MainnetClient.GetDesignateHash()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get designate hash: %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,
|
2021-07-22 10:04:37 +00:00
|
|
|
irFetcher: p.IRFetcher,
|
2021-04-29 13:40:34 +00:00
|
|
|
mainnetClient: p.MainnetClient,
|
|
|
|
morphClient: p.MorphClient,
|
|
|
|
notaryDisabled: p.NotaryDisabled,
|
2021-08-26 07:59:02 +00:00
|
|
|
designate: designate,
|
2021-03-25 13:23:53 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
// ListenerNotificationParsers for the 'event.Listener' event producer.
|
|
|
|
func (gp *Processor) ListenerNotificationParsers() []event.NotificationParserInfo {
|
|
|
|
var pi event.NotificationParserInfo
|
2021-08-26 07:59:02 +00:00
|
|
|
pi.SetScriptHash(gp.designate)
|
2021-05-18 07:40:21 +00:00
|
|
|
pi.SetType(event.TypeFromString(native.DesignationEventName))
|
|
|
|
pi.SetParser(rolemanagement.ParseDesignate)
|
2021-08-12 15:24:17 +00:00
|
|
|
return []event.NotificationParserInfo{pi}
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
// ListenerNotificationHandlers for the 'event.Listener' event producer.
|
|
|
|
func (gp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
|
|
|
|
var hi event.NotificationHandlerInfo
|
2021-08-26 07:59:02 +00:00
|
|
|
hi.SetScriptHash(gp.designate)
|
2021-05-18 07:40:21 +00:00
|
|
|
hi.SetType(event.TypeFromString(native.DesignationEventName))
|
|
|
|
hi.SetHandler(gp.HandleAlphabetSync)
|
2021-08-12 15:24:17 +00:00
|
|
|
return []event.NotificationHandlerInfo{hi}
|
2021-03-25 13:23:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 08:28:41 +00:00
|
|
|
// ListenerNotaryParsers for the 'event.Listener' event producer.
|
|
|
|
func (gp *Processor) ListenerNotaryParsers() []event.NotaryParserInfo {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenerNotaryHandlers for the 'event.Listener' event producer.
|
|
|
|
func (gp *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:23:53 +00:00
|
|
|
// TimersHandlers for the 'Timers' event producer.
|
2021-08-12 15:24:17 +00:00
|
|
|
func (gp *Processor) TimersHandlers() []event.NotificationHandlerInfo {
|
2021-03-25 13:23:53 +00:00
|
|
|
return nil
|
|
|
|
}
|