-
Notifications
You must be signed in to change notification settings - Fork 38
/
processor.go
163 lines (139 loc) · 4.86 KB
/
processor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package governance
import (
"errors"
"fmt"
"github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
neofscontract "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs"
nmClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
"github.com/nspcc-dev/neofs-node/pkg/morph/event/rolemanagement"
"github.com/panjf2000/ants/v2"
"go.uber.org/zap"
)
// ProcessorPoolSize limits the pool size for governance Processor. Processor manages
// governance sync tasks. This process must not be interrupted by other sync
// operation, so we limit the pool size for the processor to one.
const ProcessorPoolSize = 1
type (
// AlphabetState is a callback interface for innerring global state.
AlphabetState interface {
IsAlphabet() bool
}
)
// VoteValidatorPrm groups parameters of the VoteForSidechainValidator
// operation.
type VoteValidatorPrm struct {
Validators keys.PublicKeys
Hash *util.Uint256 // hash of the transaction that triggered voting
}
// Voter is a callback interface for alphabet contract voting.
type Voter interface {
VoteForSidechainValidator(VoteValidatorPrm) error
}
type (
// EpochState is a callback interface for innerring global state.
EpochState interface {
EpochCounter() uint64
}
// IRFetcher is a callback interface for innerring keys.
// Implementation must take into account availability of
// the notary contract.
IRFetcher interface {
InnerRingKeys() (keys.PublicKeys, error)
}
// Processor of events related to governance in the network.
Processor struct {
log *zap.Logger
pool *ants.Pool
neofsClient *neofscontract.Client
netmapClient *nmClient.Client
alphabetState AlphabetState
epochState EpochState
voter Voter
irFetcher IRFetcher
mainnetClient *client.Client
morphClient *client.Client
designate util.Uint160
}
// Params of the processor constructor.
Params struct {
Log *zap.Logger
AlphabetState AlphabetState
EpochState EpochState
Voter Voter
IRFetcher IRFetcher
MorphClient *client.Client
MainnetClient *client.Client
NeoFSClient *neofscontract.Client
NetmapClient *nmClient.Client
}
)
// New creates a 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")
case p.IRFetcher == nil:
return nil, errors.New("ir/governance: innerring keys fetcher is not set")
}
pool, err := ants.NewPool(ProcessorPoolSize, ants.WithNonblocking(true))
if err != nil {
return nil, fmt.Errorf("ir/governance: can't create worker pool: %w", err)
}
// result is cached by neo-go, so we can pre-calc it
designate := p.MainnetClient.GetDesignateHash()
return &Processor{
log: p.Log,
pool: pool,
neofsClient: p.NeoFSClient,
netmapClient: p.NetmapClient,
alphabetState: p.AlphabetState,
epochState: p.EpochState,
voter: p.Voter,
irFetcher: p.IRFetcher,
mainnetClient: p.MainnetClient,
morphClient: p.MorphClient,
designate: designate,
}, nil
}
// ListenerNotificationParsers for the 'event.Listener' event producer.
func (gp *Processor) ListenerNotificationParsers() []event.NotificationParserInfo {
var pi event.NotificationParserInfo
pi.SetScriptHash(gp.designate)
pi.SetType(event.TypeFromString(native.DesignationEventName))
pi.SetParser(rolemanagement.ParseDesignate)
return []event.NotificationParserInfo{pi}
}
// ListenerNotificationHandlers for the 'event.Listener' event producer.
func (gp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
var hi event.NotificationHandlerInfo
hi.SetScriptHash(gp.designate)
hi.SetType(event.TypeFromString(native.DesignationEventName))
hi.SetHandler(gp.HandleAlphabetSync)
return []event.NotificationHandlerInfo{hi}
}
// 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
}
// TimersHandlers for the 'Timers' event producer.
func (gp *Processor) TimersHandlers() []event.NotificationHandlerInfo {
return nil
}