-
Notifications
You must be signed in to change notification settings - Fork 22
/
engine_checkpoint.go
66 lines (60 loc) · 2.07 KB
/
engine_checkpoint.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
// 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 execution
import (
"context"
"sort"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/proto"
checkpoint "code.vegaprotocol.io/vega/protos/vega/checkpoint/v1"
)
func (e *Engine) Name() types.CheckpointName {
return types.ExecutionCheckpoint
}
func (e *Engine) Checkpoint() ([]byte, error) {
for id, mkt := range e.futureMarkets {
state := mkt.GetCPState()
e.marketCPStates[id] = state
}
data := make([]*types.CPMarketState, 0, len(e.marketCPStates))
for _, s := range e.marketCPStates {
data = append(data, s)
}
sort.SliceStable(data, func(i, j int) bool {
return data[i].ID < data[j].ID
})
wrapped := types.ExecutionState{
Data: data,
}
cpData, err := proto.Marshal(wrapped.IntoProto())
return cpData, err
}
func (e *Engine) Load(ctx context.Context, data []byte) error {
if len(data) == 0 {
// because the checkpoint data may be missing from older checkpoint data
e.marketCPStates = map[string]*types.CPMarketState{}
return nil
}
wrapper := checkpoint.ExecutionState{}
if err := proto.Unmarshal(data, &wrapper); err != nil {
return err
}
e.marketCPStates = make(map[string]*types.CPMarketState, len(wrapper.Data))
// for now, restore all pending markets as though their state is valid for the full TTL window
for _, mcp := range wrapper.Data {
e.marketCPStates[mcp.Id] = types.NewMarketStateFromProto(mcp)
}
return nil
}