code.vegaprotocol.io/vega@v0.79.0/core/execution/liquidation/snapshot.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package liquidation 17 18 import ( 19 "context" 20 21 "code.vegaprotocol.io/vega/core/types" 22 "code.vegaprotocol.io/vega/libs/proto" 23 ) 24 25 func (e *Engine) Namespace() types.SnapshotNamespace { 26 return types.LiquidationSnapshot 27 } 28 29 func (e *Engine) Keys() []string { 30 return []string{e.mID} 31 } 32 33 // GetState must be thread-safe as it may be called from multiple goroutines concurrently! 34 func (e *Engine) GetState(key string) ([]byte, []types.StateProvider, error) { 35 if key != e.mID { 36 return nil, nil, types.ErrSnapshotKeyDoesNotExist 37 } 38 if e.stopped { 39 return nil, nil, nil 40 } 41 payload := e.buildPayload() 42 43 s, err := proto.Marshal(payload.IntoProto()) 44 return s, nil, err 45 } 46 47 func (e *Engine) LoadState(ctx context.Context, pl *types.Payload) ([]types.StateProvider, error) { 48 if e.Namespace() != pl.Namespace() { 49 return nil, types.ErrInvalidSnapshotNamespace 50 } 51 52 switch d := pl.Data.(type) { 53 case *types.LiquidationNode: 54 e.mID = d.MarketID 55 e.pos.open = d.NetworkPos 56 e.nextStep = d.NextStep 57 if d.Config != nil { 58 e.cfg = d.Config.DeepClone() 59 } else { 60 // this can probably be removed now 61 e.cfg = GetLegacyStrat() 62 } 63 // @NOTE this should have a protocol upgrade guard around it 64 if e.cfg.DisposalFraction.IsZero() { 65 e.cfg.DisposalFraction = defaultStrat.DisposalSlippage 66 } 67 default: 68 return nil, types.ErrUnknownSnapshotType 69 } 70 return nil, nil 71 } 72 73 func (e *Engine) Stopped() bool { 74 return e.stopped 75 } 76 77 func (e *Engine) StopSnapshots() { 78 e.stopped = true 79 } 80 81 func (e *Engine) buildPayload() *types.Payload { 82 // this should not be needed 83 var cfg *types.LiquidationStrategy 84 if e.cfg != nil { 85 cfg = e.cfg.DeepClone() 86 } 87 return &types.Payload{ 88 Data: &types.LiquidationNode{ 89 MarketID: e.mID, 90 NetworkPos: e.pos.open, 91 NextStep: e.nextStep, 92 Config: cfg, 93 }, 94 } 95 }