code.vegaprotocol.io/vega@v0.79.0/core/epochtime/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 epochtime 17 18 import ( 19 "context" 20 21 "code.vegaprotocol.io/vega/core/types" 22 "code.vegaprotocol.io/vega/libs/proto" 23 "code.vegaprotocol.io/vega/protos/vega" 24 ) 25 26 func (s *Svc) serialise() error { 27 s.state.Seq = s.epoch.Seq 28 s.state.StartTime = s.epoch.StartTime 29 s.state.ExpireTime = s.epoch.ExpireTime 30 s.state.ReadyToStartNewEpoch = s.readyToStartNewEpoch 31 s.state.ReadyToEndEpoch = s.readyToEndEpoch 32 33 data, err := proto.Marshal(s.pl.IntoProto()) 34 if err != nil { 35 return err 36 } 37 38 s.data = data 39 return nil 40 } 41 42 func (s *Svc) Namespace() types.SnapshotNamespace { 43 return types.EpochSnapshot 44 } 45 46 func (s *Svc) Keys() []string { 47 return []string{s.pl.Key()} 48 } 49 50 func (s *Svc) Stopped() bool { 51 return false 52 } 53 54 func (s *Svc) GetState(k string) ([]byte, []types.StateProvider, error) { 55 if k != s.pl.Key() { 56 return nil, nil, types.ErrSnapshotKeyDoesNotExist 57 } 58 s.serialise() 59 return s.data, nil, nil 60 } 61 62 func (s *Svc) LoadState(ctx context.Context, payload *types.Payload) ([]types.StateProvider, error) { 63 if s.Namespace() != payload.Data.Namespace() { 64 return nil, types.ErrInvalidSnapshotNamespace 65 } 66 switch pl := payload.Data.(type) { 67 case *types.PayloadEpoch: 68 snap := pl.EpochState 69 s.epoch = types.Epoch{ 70 Seq: snap.Seq, 71 StartTime: snap.StartTime, 72 ExpireTime: snap.ExpireTime, 73 Action: vega.EpochAction_EPOCH_ACTION_START, 74 } 75 76 s.readyToStartNewEpoch = snap.ReadyToStartNewEpoch 77 s.readyToEndEpoch = snap.ReadyToEndEpoch 78 79 // notify all the engines that store epoch data about the current restored epoch 80 s.notifyRestore(ctx, s.epoch) 81 return nil, s.serialise() 82 default: 83 return nil, types.ErrUnknownSnapshotType 84 } 85 } 86 87 func (s *Svc) notifyRestore(ctx context.Context, e types.Epoch) { 88 for _, f := range s.restoreListeners { 89 f(ctx, e) 90 } 91 }