code.vegaprotocol.io/vega@v0.79.0/core/liquidity/target/snapshot_test.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 target_test
    17  
    18  import (
    19  	"bytes"
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  	"time"
    24  
    25  	"code.vegaprotocol.io/vega/core/liquidity/target"
    26  	"code.vegaprotocol.io/vega/core/types"
    27  	"code.vegaprotocol.io/vega/libs/num"
    28  	"code.vegaprotocol.io/vega/libs/proto"
    29  	snapshot "code.vegaprotocol.io/vega/protos/vega/snapshot/v1"
    30  
    31  	"github.com/stretchr/testify/assert"
    32  )
    33  
    34  func newSnapshotEngine(marketID string) *target.SnapshotEngine {
    35  	params := types.TargetStakeParameters{
    36  		TimeWindow:    5,
    37  		ScalingFactor: num.NewDecimalFromFloat(2),
    38  	}
    39  	var oiCalc target.OpenInterestCalculator
    40  
    41  	return target.NewSnapshotEngine(params, oiCalc, marketID, num.DecimalFromFloat(1))
    42  }
    43  
    44  func TestSaveAndLoadSnapshot(t *testing.T) {
    45  	a := assert.New(t)
    46  	marketID := "market-1"
    47  	key := fmt.Sprintf("target:%s", marketID)
    48  	se := newSnapshotEngine(marketID)
    49  
    50  	s, _, err := se.GetState("")
    51  	a.Empty(s)
    52  	a.EqualError(err, types.ErrSnapshotKeyDoesNotExist.Error())
    53  
    54  	d := time.Date(2015, time.December, 24, 19, 0, 0, 0, time.UTC)
    55  	se.RecordOpenInterest(40, d)
    56  	se.RecordOpenInterest(40, d.Add(time.Hour*3))
    57  
    58  	s, _, err = se.GetState(key)
    59  	a.NotEmpty(s)
    60  	a.NoError(err)
    61  
    62  	se2 := newSnapshotEngine(marketID)
    63  
    64  	pl := snapshot.Payload{}
    65  	assert.NoError(t, proto.Unmarshal(s, &pl))
    66  
    67  	_, err = se2.LoadState(context.TODO(), types.PayloadFromProto(&pl))
    68  	a.NoError(err)
    69  
    70  	s2, _, err := se2.GetState(key)
    71  	a.NoError(err)
    72  	a.True(bytes.Equal(s, s2))
    73  }
    74  
    75  func TestStopSnapshotTaking(t *testing.T) {
    76  	marketID := "market-1"
    77  	key := fmt.Sprintf("target:%s", marketID)
    78  	se := newSnapshotEngine(marketID)
    79  
    80  	// signal to kill the engine's snapshots
    81  	se.StopSnapshots()
    82  
    83  	s, _, err := se.GetState(key)
    84  	assert.NoError(t, err)
    85  	assert.Nil(t, s)
    86  	assert.True(t, se.Stopped())
    87  }