code.vegaprotocol.io/vega@v0.79.0/core/limits/engine_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  	"context"
    20  	"encoding/json"
    21  	"testing"
    22  	"time"
    23  
    24  	bmocks "code.vegaprotocol.io/vega/core/broker/mocks"
    25  	"code.vegaprotocol.io/vega/core/limits"
    26  	"code.vegaprotocol.io/vega/core/limits/mocks"
    27  	"code.vegaprotocol.io/vega/logging"
    28  
    29  	"github.com/golang/mock/gomock"
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  type limitsTest struct {
    34  	*limits.Engine
    35  	log *logging.Logger
    36  }
    37  
    38  func getLimitsTest(t *testing.T) *limitsTest {
    39  	t.Helper()
    40  	log := logging.NewTestLogger()
    41  	ctrl := gomock.NewController(t)
    42  	broker := bmocks.NewMockBroker(ctrl)
    43  	broker.EXPECT().Send(gomock.Any()).AnyTimes()
    44  
    45  	timeSvc := mocks.NewMockTimeService(ctrl)
    46  	timeSvc.EXPECT().GetTimeNow().AnyTimes()
    47  
    48  	return &limitsTest{
    49  		Engine: limits.New(log, limits.NewDefaultConfig(), timeSvc, broker),
    50  		log:    log,
    51  	}
    52  }
    53  
    54  func (l *limitsTest) loadGenesisState(t *testing.T, lstate *limits.GenesisState) {
    55  	t.Helper()
    56  	state := struct {
    57  		Limits *limits.GenesisState `json:"network_limits"`
    58  	}{
    59  		Limits: lstate,
    60  	}
    61  
    62  	buf, err := json.Marshal(state)
    63  	assert.NoError(t, err)
    64  	assert.NotNil(t, buf)
    65  
    66  	assert.NoError(t, l.UponGenesis(context.Background(), buf))
    67  }
    68  
    69  func TestLimits(t *testing.T) {
    70  	t.Run("test empty genesis", testEmptyGenesis)
    71  	t.Run("test nil genesis", testNilGenesis)
    72  	t.Run("test all disabled", testAllDisabled)
    73  	t.Run("test all enabled", testAllEnabled)
    74  	t.Run("test market enabled asset disabled", testMarketEnabledAssetDisabled)
    75  	t.Run("test market disabled asset enbled", testMarketdisabledAssetenabled)
    76  	t.Run("proposal enabled with time reach becomes enabled", testDisabledUntilTimeIsReach)
    77  	t.Run("proposals disabled with time reach stay disabled", testStayDisabledIfTimeIsReachedButEnabledIsFalse)
    78  }
    79  
    80  func testEmptyGenesis(t *testing.T) {
    81  	lmts := getLimitsTest(t)
    82  
    83  	assert.False(t, lmts.CanProposeAsset())
    84  	assert.False(t, lmts.CanProposeMarket())
    85  	assert.False(t, lmts.CanTrade())
    86  }
    87  
    88  func testNilGenesis(t *testing.T) {
    89  	lmts := getLimitsTest(t)
    90  	lmts.loadGenesisState(t, nil)
    91  
    92  	assert.True(t, lmts.CanProposeAsset())
    93  	assert.True(t, lmts.CanProposeMarket())
    94  	assert.True(t, lmts.CanTrade())
    95  }
    96  
    97  func testAllDisabled(t *testing.T) {
    98  	lmts := getLimitsTest(t)
    99  	lmts.loadGenesisState(t, &limits.GenesisState{})
   100  
   101  	assert.False(t, lmts.CanProposeAsset())
   102  	assert.False(t, lmts.CanProposeMarket())
   103  	assert.False(t, lmts.CanTrade())
   104  }
   105  
   106  func testAllEnabled(t *testing.T) {
   107  	lmts := getLimitsTest(t)
   108  	lmts.loadGenesisState(t, &limits.GenesisState{
   109  		ProposeAssetEnabled:  true,
   110  		ProposeMarketEnabled: true,
   111  	})
   112  
   113  	assert.True(t, lmts.CanProposeAsset())
   114  	assert.True(t, lmts.CanProposeMarket())
   115  	assert.True(t, lmts.CanTrade())
   116  }
   117  
   118  func testMarketEnabledAssetDisabled(t *testing.T) {
   119  	lmts := getLimitsTest(t)
   120  	lmts.loadGenesisState(t, &limits.GenesisState{
   121  		ProposeAssetEnabled:  false,
   122  		ProposeMarketEnabled: true,
   123  	})
   124  
   125  	assert.True(t, lmts.CanProposeMarket())
   126  	assert.False(t, lmts.CanProposeAsset())
   127  	assert.False(t, lmts.CanTrade())
   128  }
   129  
   130  func testMarketdisabledAssetenabled(t *testing.T) {
   131  	lmts := getLimitsTest(t)
   132  	lmts.loadGenesisState(t, &limits.GenesisState{
   133  		ProposeAssetEnabled:  true,
   134  		ProposeMarketEnabled: false,
   135  	})
   136  
   137  	assert.False(t, lmts.CanProposeMarket())
   138  	assert.True(t, lmts.CanProposeAsset())
   139  	assert.False(t, lmts.CanTrade())
   140  }
   141  
   142  func testDisabledUntilTimeIsReach(t *testing.T) {
   143  	lmts := getLimitsTest(t)
   144  	lmts.loadGenesisState(t, &limits.GenesisState{
   145  		ProposeAssetEnabled:  true,
   146  		ProposeMarketEnabled: true,
   147  	})
   148  
   149  	lmts.OnLimitsProposeAssetEnabledFromUpdate(context.Background(), time.Unix(2000, 0).Format(time.RFC3339))
   150  	lmts.OnLimitsProposeMarketEnabledFromUpdate(context.Background(), time.Unix(2000, 0).Format(time.RFC3339))
   151  
   152  	// need to call onTick
   153  	lmts.OnTick(context.Background(), time.Unix(1000, 0))
   154  
   155  	assert.False(t, lmts.CanProposeMarket())
   156  	assert.False(t, lmts.CanProposeAsset())
   157  	assert.False(t, lmts.CanTrade())
   158  
   159  	// need to call onTick again, now it should be fine
   160  	lmts.OnTick(context.Background(), time.Unix(3000, 0))
   161  
   162  	assert.True(t, lmts.CanProposeMarket())
   163  	assert.True(t, lmts.CanProposeAsset())
   164  	assert.True(t, lmts.CanTrade())
   165  }
   166  
   167  func testStayDisabledIfTimeIsReachedButEnabledIsFalse(t *testing.T) {
   168  	lmts := getLimitsTest(t)
   169  	lmts.loadGenesisState(t, &limits.GenesisState{
   170  		ProposeAssetEnabled:  false,
   171  		ProposeMarketEnabled: false,
   172  	})
   173  
   174  	lmts.OnLimitsProposeAssetEnabledFromUpdate(context.Background(), time.Unix(2000, 0).Format(time.RFC3339))
   175  	lmts.OnLimitsProposeMarketEnabledFromUpdate(context.Background(), time.Unix(2000, 0).Format(time.RFC3339))
   176  
   177  	// need to call onTick
   178  	lmts.OnTick(context.Background(), time.Unix(1000, 0))
   179  
   180  	assert.False(t, lmts.CanProposeMarket())
   181  	assert.False(t, lmts.CanProposeAsset())
   182  	assert.False(t, lmts.CanTrade())
   183  
   184  	// need to call onTick again, now it should be fine
   185  	lmts.OnTick(context.Background(), time.Unix(3000, 0))
   186  
   187  	assert.False(t, lmts.CanProposeMarket())
   188  	assert.False(t, lmts.CanProposeAsset())
   189  	assert.False(t, lmts.CanTrade())
   190  }