code.vegaprotocol.io/vega@v0.79.0/core/spam/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 spam 17 18 import ( 19 "context" 20 21 "code.vegaprotocol.io/vega/core/types" 22 "code.vegaprotocol.io/vega/logging" 23 ) 24 25 func (e *Engine) Namespace() types.SnapshotNamespace { 26 return types.SpamSnapshot 27 } 28 29 func (e *Engine) Keys() []string { 30 return e.hashKeys 31 } 32 33 func (e *Engine) Stopped() bool { 34 return false 35 } 36 37 // get the serialised form and hash of the given key. 38 func (e *Engine) serialise(k string) ([]byte, error) { 39 if _, ok := e.policyNameToPolicy[k]; !ok { 40 return nil, types.ErrSnapshotKeyDoesNotExist 41 } 42 43 data, err := e.policyNameToPolicy[k].Serialise() 44 if err != nil { 45 return nil, err 46 } 47 return data, nil 48 } 49 50 func (e *Engine) GetState(k string) ([]byte, []types.StateProvider, error) { 51 state, err := e.serialise(k) 52 return state, nil, err 53 } 54 55 func (e *Engine) LoadState(_ context.Context, p *types.Payload) ([]types.StateProvider, error) { 56 if e.Namespace() != p.Data.Namespace() { 57 return nil, types.ErrInvalidSnapshotNamespace 58 } 59 60 if _, ok := e.policyNameToPolicy[p.Key()]; !ok { 61 return nil, types.ErrUnknownSnapshotType 62 } 63 64 return nil, e.policyNameToPolicy[p.Key()].Deserialise(p) 65 } 66 67 // OnEpochEvent is a callback for epoch events. 68 func (e *Engine) OnEpochRestore(ctx context.Context, epoch types.Epoch) { 69 e.log.Debug("epoch restoration notification received", logging.String("epoch", epoch.String())) 70 e.currentEpoch = &epoch 71 }