code.vegaprotocol.io/vega@v0.79.0/core/execution/engine_checkpoint.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 execution
    17  
    18  import (
    19  	"context"
    20  	"sort"
    21  
    22  	"code.vegaprotocol.io/vega/core/types"
    23  	"code.vegaprotocol.io/vega/libs/proto"
    24  	checkpoint "code.vegaprotocol.io/vega/protos/vega/checkpoint/v1"
    25  )
    26  
    27  func (e *Engine) Name() types.CheckpointName {
    28  	return types.ExecutionCheckpoint
    29  }
    30  
    31  func (e *Engine) Checkpoint() ([]byte, error) {
    32  	for id, mkt := range e.futureMarkets {
    33  		state := mkt.GetCPState()
    34  		e.marketCPStates[id] = state
    35  	}
    36  	data := make([]*types.CPMarketState, 0, len(e.marketCPStates))
    37  	for _, s := range e.marketCPStates {
    38  		data = append(data, s)
    39  	}
    40  	sort.SliceStable(data, func(i, j int) bool {
    41  		return data[i].ID < data[j].ID
    42  	})
    43  	wrapped := types.ExecutionState{
    44  		Data: data,
    45  	}
    46  	cpData, err := proto.Marshal(wrapped.IntoProto())
    47  	return cpData, err
    48  }
    49  
    50  func (e *Engine) Load(ctx context.Context, data []byte) error {
    51  	if len(data) == 0 {
    52  		// because the checkpoint data may be missing from older checkpoint data
    53  		e.marketCPStates = map[string]*types.CPMarketState{}
    54  		return nil
    55  	}
    56  	wrapper := checkpoint.ExecutionState{}
    57  	if err := proto.Unmarshal(data, &wrapper); err != nil {
    58  		return err
    59  	}
    60  	e.marketCPStates = make(map[string]*types.CPMarketState, len(wrapper.Data))
    61  	// for now, restore all pending markets as though their state is valid for the full TTL window
    62  	for _, mcp := range wrapper.Data {
    63  		e.marketCPStates[mcp.Id] = types.NewMarketStateFromProto(mcp)
    64  	}
    65  	return nil
    66  }