code.vegaprotocol.io/vega@v0.79.0/core/execution/common/market_activity_tracker_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 common
    17  
    18  import (
    19  	"context"
    20  	"sort"
    21  
    22  	"code.vegaprotocol.io/vega/core/types"
    23  	"code.vegaprotocol.io/vega/libs/num"
    24  	"code.vegaprotocol.io/vega/libs/proto"
    25  	checkpoint "code.vegaprotocol.io/vega/protos/vega/checkpoint/v1"
    26  )
    27  
    28  func (mat *MarketActivityTracker) Name() types.CheckpointName {
    29  	return types.MarketActivityTrackerCheckpoint
    30  }
    31  
    32  func (mat *MarketActivityTracker) Checkpoint() ([]byte, error) {
    33  	assets := make([]string, 0, len(mat.assetToMarketTrackers))
    34  	for k := range mat.assetToMarketTrackers {
    35  		assets = append(assets, k)
    36  	}
    37  	sort.Strings(assets)
    38  
    39  	marketTracker := []*checkpoint.MarketActivityTracker{}
    40  	for _, asset := range assets {
    41  		assetTrackers := mat.assetToMarketTrackers[asset]
    42  		markets := make([]string, 0, len(assetTrackers))
    43  		for k := range assetTrackers {
    44  			markets = append(markets, k)
    45  		}
    46  		sort.Strings(markets)
    47  		for _, market := range markets {
    48  			mt := assetTrackers[market]
    49  			marketTracker = append(marketTracker, mt.IntoProto(market))
    50  		}
    51  	}
    52  
    53  	msg := &checkpoint.MarketTracker{
    54  		MarketActivity:                   marketTracker,
    55  		TakerNotionalVolume:              takerNotionalToProto(mat.partyTakerNotionalVolume),
    56  		MarketToPartyTakerNotionalVolume: marketToPartyTakerNotionalToProto(mat.marketToPartyTakerNotionalVolume),
    57  		EpochTakerFees:                   epochTakerFeesToProto(mat.takerFeesPaidInEpoch),
    58  		GameEligibilityTracker:           epochEligitbilityToProto(mat.eligibilityInEpoch),
    59  	}
    60  	ret, err := proto.Marshal(msg)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return ret, nil
    65  }
    66  
    67  func (mat *MarketActivityTracker) Load(_ context.Context, data []byte) error {
    68  	b := checkpoint.MarketTracker{}
    69  	if err := proto.Unmarshal(data, &b); err != nil {
    70  		return err
    71  	}
    72  
    73  	for _, data := range b.MarketActivity {
    74  		if _, ok := mat.assetToMarketTrackers[data.Asset]; !ok {
    75  			mat.assetToMarketTrackers[data.Asset] = map[string]*marketTracker{}
    76  		}
    77  		mat.assetToMarketTrackers[data.Asset][data.Market] = marketTrackerFromProto(data)
    78  	}
    79  	for _, tnv := range b.TakerNotionalVolume {
    80  		if len(tnv.Volume) > 0 {
    81  			mat.partyTakerNotionalVolume[tnv.Party] = num.UintFromBytes(tnv.Volume)
    82  		}
    83  	}
    84  	for _, marketToPartyStats := range b.MarketToPartyTakerNotionalVolume {
    85  		mat.marketToPartyTakerNotionalVolume[marketToPartyStats.Market] = map[string]*num.Uint{}
    86  		for _, partyStats := range marketToPartyStats.TakerNotionalVolume {
    87  			if len(partyStats.Volume) > 0 {
    88  				mat.marketToPartyTakerNotionalVolume[marketToPartyStats.Market][partyStats.Party] = num.UintFromBytes(partyStats.Volume)
    89  			}
    90  		}
    91  	}
    92  	if b.EpochTakerFees != nil {
    93  		for _, epochData := range b.EpochTakerFees {
    94  			epochMap := map[string]map[string]map[string]*num.Uint{}
    95  			for _, assetMarketParty := range epochData.EpochPartyTakerFeesPaid {
    96  				if _, ok := epochMap[assetMarketParty.Asset]; !ok {
    97  					epochMap[assetMarketParty.Asset] = map[string]map[string]*num.Uint{}
    98  				}
    99  				if _, ok := epochMap[assetMarketParty.Asset][assetMarketParty.Market]; !ok {
   100  					epochMap[assetMarketParty.Asset][assetMarketParty.Market] = map[string]*num.Uint{}
   101  				}
   102  				for _, tf := range assetMarketParty.TakerFees {
   103  					epochMap[assetMarketParty.Asset][assetMarketParty.Market][tf.Party] = num.UintFromBytes(tf.TakerFees)
   104  				}
   105  			}
   106  			mat.takerFeesPaidInEpoch = append(mat.takerFeesPaidInEpoch, epochMap)
   107  		}
   108  	}
   109  	if b.GameEligibilityTracker != nil {
   110  		for _, get := range b.GameEligibilityTracker {
   111  			mat.eligibilityInEpoch[get.GameId] = make([]map[string]struct{}, len(get.EpochEligibility))
   112  			for i, epoch := range get.EpochEligibility {
   113  				mat.eligibilityInEpoch[get.GameId][i] = make(map[string]struct{}, len(epoch.EligibleParties))
   114  				for _, party := range epoch.EligibleParties {
   115  					mat.eligibilityInEpoch[get.GameId][i][party] = struct{}{}
   116  				}
   117  			}
   118  		}
   119  	}
   120  
   121  	return nil
   122  }