code.vegaprotocol.io/vega@v0.79.0/core/limits/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 limits_test
    17  
    18  import (
    19  	"bytes"
    20  	"context"
    21  	"testing"
    22  
    23  	"code.vegaprotocol.io/vega/core/limits"
    24  	"code.vegaprotocol.io/vega/core/types"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  var allKey = (&types.PayloadLimitState{}).Key()
    31  
    32  func TestLimitSnapshotEmpty(t *testing.T) {
    33  	l := getLimitsTest(t)
    34  
    35  	s, _, err := l.GetState(allKey)
    36  	require.Nil(t, err)
    37  	require.NotNil(t, s)
    38  }
    39  
    40  func TestLimitSnapshotWrongPayLoad(t *testing.T) {
    41  	l := getLimitsTest(t)
    42  	snap := &types.Payload{Data: &types.PayloadEpoch{}}
    43  	_, err := l.LoadState(context.Background(), snap)
    44  	assert.ErrorIs(t, types.ErrInvalidSnapshotNamespace, err)
    45  }
    46  
    47  func TestLimitSnapshotGenesisState(t *testing.T) {
    48  	gs := &limits.GenesisState{}
    49  	lmt := getLimitsTest(t)
    50  	s1, _, err := lmt.GetState(allKey)
    51  	require.Nil(t, err)
    52  
    53  	lmt.loadGenesisState(t, gs)
    54  
    55  	s2, _, err := lmt.GetState(allKey)
    56  	require.Nil(t, err)
    57  	require.False(t, bytes.Equal(s1, s2))
    58  }
    59  
    60  func TestSnapshotRoundTrip(t *testing.T) {
    61  	gs := &limits.GenesisState{}
    62  	lmt := getLimitsTest(t)
    63  	lmt.loadGenesisState(t, gs)
    64  	lmt.OnLimitsProposeSpotMarketEnabledFromUpdate(context.Background(), 1)
    65  
    66  	s1, _, err := lmt.GetState(allKey)
    67  	require.Nil(t, err)
    68  
    69  	s2, _, err := lmt.GetState(allKey)
    70  	require.Nil(t, err)
    71  	require.True(t, bytes.Equal(s1, s2))
    72  }