-
Notifications
You must be signed in to change notification settings - Fork 19
/
topology.go
394 lines (329 loc) · 10.2 KB
/
topology.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package erc20multisig
import (
"context"
"errors"
"sort"
"sync"
"time"
"code.vegaprotocol.io/vega/core/broker"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/core/validators"
"code.vegaprotocol.io/vega/logging"
)
//go:generate go run github.com/golang/mock/mockgen -destination mocks/mocks.go -package mocks code.vegaprotocol.io/vega/core/validators/erc20multisig Witness,MultiSigOnChainVerifier,EthConfirmations,EthereumEventSource
const (
// 3 weeks, duration of the whole network at first?
timeTilCancel = 24 * 21 * time.Hour
)
var (
ErrDuplicatedSignerEvent = errors.New("duplicated signer event")
ErrDuplicatedThresholdEvent = errors.New("duplicated threshold event")
)
// Witness provide foreign chain resources validations.
type Witness interface {
StartCheck(validators.Resource, func(interface{}, bool), time.Time) error
RestoreResource(validators.Resource, func(interface{}, bool)) error
}
type MultiSigOnChainVerifier interface {
CheckSignerEvent(*types.SignerEvent) error
CheckThresholdSetEvent(*types.SignerThresholdSetEvent) error
}
type EthereumEventSource interface {
UpdateMultisigControlStartingBlock(uint64)
}
// Topology keeps track of all the validators
// registered in the erc20 bridge.
type Topology struct {
config Config
log *logging.Logger
chainID string
currentTime time.Time
witness Witness
broker broker.Interface
ocv MultiSigOnChainVerifier
// use to access both the pendingEvents and pendingThresholds maps
mu sync.Mutex
// the current map of all the signer on the bridge
signers map[string]struct{}
// signer address to list of all events related to it
// order by block time.
eventsPerAddress map[string][]*types.SignerEvent
// a map of all pending events waiting to be processed
pendingSigners map[string]*pendingSigner
// the signer required treshold
// last one is always kept
threshold *types.SignerThresholdSetEvent
pendingThresholds map[string]*pendingThresholdSet
// a map of all seen events
seen map[string]struct{}
witnessedThresholds map[string]struct{}
witnessedSigners map[string]struct{}
ethEventSource EthereumEventSource
}
type pendingSigner struct {
*types.SignerEvent
check func() error
}
func (p pendingSigner) GetID() string { return p.ID }
func (p pendingSigner) GetChainID() string { return p.ChainID }
func (p pendingSigner) GetType() types.NodeVoteType {
var ty types.NodeVoteType
switch p.Kind {
case types.SignerEventKindAdded:
ty = types.NodeVoteTypeSignerAdded
case types.SignerEventKindRemoved:
ty = types.NodeVoteTypeSignerRemoved
}
return ty
}
func (p *pendingSigner) Check(ctx context.Context) error { return p.check() }
type pendingThresholdSet struct {
*types.SignerThresholdSetEvent
check func() error
}
func (p pendingThresholdSet) GetID() string { return p.ID }
func (p pendingThresholdSet) GetChainID() string { return p.ChainID }
func (p pendingThresholdSet) GetType() types.NodeVoteType {
return types.NodeVoteTypeSignerThresholdSet
}
func (p *pendingThresholdSet) Check(ctx context.Context) error { return p.check() }
func NewTopology(
config Config,
log *logging.Logger,
witness Witness,
ocv MultiSigOnChainVerifier,
broker broker.Interface,
scope string,
) *Topology {
log = log.Named(namedLogger + ".topology")
log.SetLevel(config.Level.Get())
t := &Topology{
config: config,
log: log,
witness: witness,
ocv: ocv,
broker: broker,
signers: map[string]struct{}{},
eventsPerAddress: map[string][]*types.SignerEvent{},
pendingSigners: map[string]*pendingSigner{},
pendingThresholds: map[string]*pendingThresholdSet{},
seen: map[string]struct{}{},
witnessedThresholds: map[string]struct{}{},
witnessedSigners: map[string]struct{}{},
}
return t
}
// SetChainID sets the chainID of the EVM chain this multisig tracker belongs to.
func (t *Topology) SetChainID(chainID string) {
t.chainID = chainID
}
func (t *Topology) ChainID() string {
return t.chainID
}
func (t *Topology) SetWitness(w Witness) {
t.witness = w
}
func (t *Topology) SetEthereumEventSource(e EthereumEventSource) {
t.ethEventSource = e
}
func (t *Topology) ExcessSigners(addresses []string) bool {
addressesMap := map[string]struct{}{}
for _, v := range addresses {
addressesMap[v] = struct{}{}
}
for k := range t.signers {
if _, ok := addressesMap[k]; !ok {
return true
}
}
return false
}
func (t *Topology) GetSigners() []string {
t.mu.Lock()
defer t.mu.Unlock()
out := make([]string, 0, len(t.signers))
for k := range t.signers {
out = append(out, k)
}
sort.Strings(out)
return out
}
func (t *Topology) IsSigner(address string) bool {
t.mu.Lock()
defer t.mu.Unlock()
_, ok := t.signers[address]
return ok
}
func (t *Topology) GetThreshold() uint32 {
t.mu.Lock()
defer t.mu.Unlock()
if t.threshold != nil {
return t.threshold.Threshold
}
return 0
}
func (t *Topology) ProcessSignerEvent(event *types.SignerEvent) error {
if ok := t.ensureNotDuplicate(event.Hash()); !ok {
t.log.Error("signer event already exists",
logging.String("chain-id", t.chainID),
logging.String("event", event.String()))
return ErrDuplicatedSignerEvent
}
pending := &pendingSigner{
SignerEvent: event,
check: func() error { return t.ocv.CheckSignerEvent(event) },
}
t.pendingSigners[event.ID] = pending
t.log.Info("signer event received, starting validation",
logging.String("chain-id", t.chainID),
logging.String("event", event.String()))
return t.witness.StartCheck(
pending, t.onEventVerified, t.currentTime.Add(timeTilCancel))
}
func (t *Topology) ProcessThresholdEvent(event *types.SignerThresholdSetEvent) error {
if ok := t.ensureNotDuplicate(event.Hash()); !ok {
t.log.Error("threshold event already exists",
logging.String("chain-id", t.chainID),
logging.String("event", event.String()))
return ErrDuplicatedThresholdEvent
}
pending := &pendingThresholdSet{
SignerThresholdSetEvent: event,
check: func() error { return t.ocv.CheckThresholdSetEvent(event) },
}
t.pendingThresholds[event.ID] = pending
t.log.Info("signer threshold set event received, starting validation",
logging.String("chain-id", t.chainID),
logging.String("event", event.String()))
return t.witness.StartCheck(
pending, t.onEventVerified, t.currentTime.Add(timeTilCancel))
}
func (t *Topology) ensureNotDuplicate(h string) bool {
t.mu.Lock()
defer t.mu.Unlock()
if _, ok := t.seen[h]; ok {
return false
}
t.seen[h] = struct{}{}
return true
}
func (t *Topology) onEventVerified(event interface{}, ok bool) {
switch e := event.(type) {
case *pendingSigner:
if !ok {
// invalid, just delete from the map
delete(t.pendingSigners, e.ID)
return
}
t.witnessedSigners[e.ID] = struct{}{}
case *pendingThresholdSet:
if !ok {
// invalid, just delete from the map
delete(t.pendingThresholds, e.ID)
return
}
t.witnessedThresholds[e.ID] = struct{}{}
default:
t.log.Error("multisig verifier received invalid event",
logging.String("chain-id", t.chainID),
)
return
}
}
func (t *Topology) OnTick(ctx context.Context, ct time.Time) {
t.currentTime = ct
t.updateThreshold(ctx)
t.updateSigners(ctx)
}
func (t *Topology) updateThreshold(ctx context.Context) {
t.mu.Lock()
defer t.mu.Unlock()
if len(t.witnessedThresholds) <= 0 {
return
}
// sort all IDs to access pendings events in order
ids := []string{}
for k := range t.witnessedThresholds {
ids = append(ids, k)
delete(t.witnessedThresholds, k)
}
sort.Strings(ids)
// now iterate over all events and update the
// threshold if we get an event with a more recent
// block time.
for _, v := range ids {
event := t.pendingThresholds[v]
t.setThresholdSetEvent(ctx, event.SignerThresholdSetEvent)
delete(t.pendingThresholds, v)
}
}
func (t *Topology) setThresholdSetEvent(
ctx context.Context, event *types.SignerThresholdSetEvent,
) {
// if it's out first time here
if t.threshold == nil || event.BlockTime > t.threshold.BlockTime {
t.threshold = event
}
// send the event anyway so APIs can be aware of past thresholds
t.broker.Send(events.NewERC20MultiSigThresholdSet(ctx, *event))
}
func (t *Topology) updateSigners(ctx context.Context) {
t.mu.Lock()
defer t.mu.Unlock()
if len(t.witnessedSigners) <= 0 {
return
}
// sort all IDs to access pendings events in order
ids := []string{}
for k := range t.witnessedSigners {
ids = append(ids, k)
delete(t.witnessedSigners, k)
}
sort.Strings(ids)
// first add all events to the map of events per addresses
for _, id := range ids {
// get the event
event := t.pendingSigners[id]
t.addSignerEvent(ctx, event.SignerEvent)
// delete from pending then
delete(t.pendingSigners, id)
}
}
func (t *Topology) addSignerEvent(ctx context.Context, event *types.SignerEvent) {
epa, ok := t.eventsPerAddress[event.Address]
if !ok {
epa = []*types.SignerEvent{}
}
// now add the event to the list for this address
epa = append(epa, event)
// sort them in arrival order
sort.Slice(epa, func(i, j int) bool {
return epa[i].BlockTime < epa[j].BlockTime
})
t.eventsPerAddress[event.Address] = epa
// now depending of the last event received,
// we add or remove from the list of signers
switch epa[len(epa)-1].Kind {
case types.SignerEventKindRemoved:
delete(t.signers, event.Address)
case types.SignerEventKindAdded:
t.signers[event.Address] = struct{}{}
}
// send the event anyway so APIs can be aware of past thresholds
t.broker.Send(events.NewERC20MultiSigSigner(ctx, *event))
}