2021-03-25 13:23:53 +00:00
|
|
|
package governance
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/morph/client"
|
2023-01-10 12:50:17 +00:00
|
|
|
frostfscontract "github.com/TrueCloudLab/frostfs-node/pkg/morph/client/frostfs"
|
2022-12-23 17:35:35 +00:00
|
|
|
nmClient "github.com/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/morph/event"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/morph/event/rolemanagement"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/util/logger"
|
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/panjf2000/ants/v2"
|
|
|
|
)
|
|
|
|
|
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 {
|
2022-12-23 17:35:35 +00:00
|
|
|
log *logger.Logger
|
|
|
|
pool *ants.Pool
|
|
|
|
frostfsClient *frostfscontract.Client
|
|
|
|
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 {
|
2022-09-28 07:41:01 +00:00
|
|
|
Log *logger.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-12-23 17:35:35 +00:00
|
|
|
NeoFSClient *frostfscontract.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
|
2022-11-03 12:44:59 +00:00
|
|
|
designate := p.MainnetClient.GetDesignateHash()
|
2021-08-26 07:59:02 +00:00
|
|
|
|
2021-03-25 13:23:53 +00:00
|
|
|
return &Processor{
|
2021-04-29 13:40:34 +00:00
|
|
|
log: p.Log,
|
|
|
|
pool: pool,
|
2022-12-23 17:35:35 +00:00
|
|
|
frostfsClient: 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
|
|
|
|
}
|