-
Notifications
You must be signed in to change notification settings - Fork 19
/
engine.go
243 lines (201 loc) · 6.66 KB
/
engine.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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.VEGA file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package limits
import (
"context"
"fmt"
"time"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/logging"
"code.vegaprotocol.io/vega/protos/vega"
)
type Engine struct {
log *logging.Logger
cfg Config
broker Broker
timeService TimeService
// are these action possible?
canProposeMarket, canProposeAsset bool
// Settings from the genesis state
proposeMarketEnabled, proposeAssetEnabled, proposeSpotMarketEnabled, proposePerpsMarketEnabled bool
proposeMarketEnabledFrom, proposeAssetEnabledFrom time.Time
genesisLoaded bool
// snapshot state
lss *limitsSnapshotState
}
type Broker interface {
Send(event events.Event)
}
// TimeService provide the time of the vega node using the tm time.
//
//go:generate go run github.com/golang/mock/mockgen -destination mocks/time_service_mock.go -package mocks code.vegaprotocol.io/vega/core/limits TimeService
type TimeService interface {
GetTimeNow() time.Time
}
func New(log *logging.Logger, cfg Config, tm TimeService, broker Broker) *Engine {
log = log.Named(namedLogger)
log.SetLevel(cfg.Level.Get())
return &Engine{
log: log,
cfg: cfg,
lss: &limitsSnapshotState{},
broker: broker,
timeService: tm,
}
}
// UponGenesis load the limits from the genesis state.
func (e *Engine) UponGenesis(ctx context.Context, rawState []byte) (err error) {
e.log.Debug("Entering limits.Engine.UponGenesis")
defer func() {
if err != nil {
e.log.Debug("Failure in limits.Engine.UponGenesis", logging.Error(err))
} else {
e.log.Debug("Leaving limits.Engine.UponGenesis without error")
}
e.genesisLoaded = true
}()
state, err := LoadGenesisState(rawState)
if err != nil && err != ErrNoLimitsGenesisState {
e.log.Error("unable to load genesis state",
logging.Error(err))
return err
}
defer func() {
e.sendEvent(ctx)
}()
if err == ErrNoLimitsGenesisState {
defaultState := DefaultGenesisState()
state = &defaultState
}
// set enabled by default if not genesis state
if state == nil {
e.proposeAssetEnabled = true
e.proposeMarketEnabled = true
} else {
e.proposeAssetEnabled = state.ProposeAssetEnabled
e.proposeMarketEnabled = state.ProposeMarketEnabled
}
// at this point we only know about the genesis state
// of the limits, so we should set the can* fields to
// this state
e.canProposeAsset = e.proposeAssetEnabled
e.canProposeMarket = e.proposeMarketEnabled
e.log.Info("loaded limits genesis state",
logging.String("state", fmt.Sprintf("%#v", *state)))
return nil
}
func (e *Engine) OnLimitsProposeMarketEnabledFromUpdate(ctx context.Context, date string) error {
// already validated by the netparams
// no need to check it again, this is a valid date
if len(date) <= 0 {
e.proposeMarketEnabledFrom = time.Time{}
} else {
t, _ := time.Parse(time.RFC3339, date)
e.proposeMarketEnabledFrom = t
}
e.onUpdate(e.timeService.GetTimeNow())
e.sendEvent(ctx)
return nil
}
func (e *Engine) OnLimitsProposeSpotMarketEnabledFromUpdate(ctx context.Context, enabled int64) error {
e.proposeSpotMarketEnabled = enabled == 1
e.sendEvent(ctx)
return nil
}
func (e *Engine) OnLimitsProposePerpsMarketEnabledFromUpdate(ctx context.Context, enabled int64) error {
e.proposePerpsMarketEnabled = enabled == 1
e.sendEvent(ctx)
return nil
}
func (e *Engine) OnLimitsProposeAssetEnabledFromUpdate(ctx context.Context, date string) error {
// already validated by the netparams
// no need to check it again, this is a valid date
if len(date) <= 0 {
e.proposeAssetEnabledFrom = time.Time{}
} else {
t, _ := time.Parse(time.RFC3339, date)
e.proposeAssetEnabledFrom = t
}
e.onUpdate(e.timeService.GetTimeNow())
e.sendEvent(ctx)
return nil
}
func (e *Engine) OnTick(ctx context.Context, t time.Time) {
canProposeAsset, canProposeMarket := e.canProposeAsset, e.canProposeMarket
defer func() {
if canProposeAsset != e.canProposeAsset || canProposeMarket != e.canProposeMarket {
e.sendEvent(ctx)
}
}()
e.onUpdate(t)
}
func (e *Engine) onUpdate(t time.Time) {
// if propose market enabled in genesis
if e.proposeMarketEnabled {
// we can propose a market and a new date have been set in the future
if e.canProposeMarket && t.Before(e.proposeMarketEnabledFrom) {
e.log.Info("proposing market is now disabled")
e.canProposeMarket = false
}
// we can't propose a market for now, is the date in the past?
if !e.canProposeMarket && t.After(e.proposeMarketEnabledFrom) {
e.log.Info("all required conditions are met, proposing markets is now allowed")
e.canProposeMarket = true
}
}
// if propose market enabled in genesis
if e.proposeAssetEnabled {
// we can propose a market and a new date have been set in the future
if e.canProposeAsset && t.Before(e.proposeAssetEnabledFrom) {
e.log.Info("proposing asset have been disabled")
e.canProposeAsset = false
}
if !e.canProposeAsset && t.After(e.proposeAssetEnabledFrom) {
e.log.Info("all required conditions are met, proposing assets is now allowed")
e.canProposeAsset = true
}
}
}
func (e *Engine) CanProposeMarket() bool {
return e.canProposeMarket
}
func (e *Engine) CanProposeAsset() bool {
return e.canProposeAsset
}
func (e *Engine) CanTrade() bool {
return e.canProposeAsset && e.canProposeMarket
}
func (e *Engine) CanProposeSpotMarket() bool {
return e.proposeSpotMarketEnabled
}
func (e *Engine) CanProposePerpsMarket() bool {
return e.proposePerpsMarketEnabled
}
func (e *Engine) sendEvent(ctx context.Context) {
limits := vega.NetworkLimits{
CanProposeMarket: e.canProposeMarket,
CanProposeAsset: e.canProposeAsset,
ProposeMarketEnabled: e.proposeMarketEnabled,
ProposeAssetEnabled: e.proposeAssetEnabled,
GenesisLoaded: e.genesisLoaded,
CanProposeSpotMarket: e.proposeSpotMarketEnabled,
CanProposePerpetualMarket: e.proposePerpsMarketEnabled,
}
if !e.proposeMarketEnabledFrom.IsZero() {
limits.ProposeMarketEnabledFrom = e.proposeAssetEnabledFrom.UnixNano()
}
if !e.proposeAssetEnabledFrom.IsZero() {
limits.ProposeAssetEnabledFrom = e.proposeAssetEnabledFrom.UnixNano()
}
event := events.NewNetworkLimitsEvent(ctx, &limits)
e.broker.Send(event)
}