code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/network_parameters_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 sqlstore_test
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"testing"
    22  	"time"
    23  
    24  	"code.vegaprotocol.io/vega/datanode/entities"
    25  	"code.vegaprotocol.io/vega/datanode/sqlstore"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func addNetParam(t *testing.T, ctx context.Context, ns *sqlstore.NetworkParameters, key, value string, block entities.Block) entities.NetworkParameter {
    32  	t.Helper()
    33  	p := entities.NetworkParameter{
    34  		Key:      key,
    35  		Value:    value,
    36  		VegaTime: block.VegaTime,
    37  		TxHash:   generateTxHash(),
    38  	}
    39  	ns.Add(ctx, p)
    40  	return p
    41  }
    42  
    43  func TestNetParams(t *testing.T) {
    44  	ctx := tempTransaction(t)
    45  
    46  	netParamStore := sqlstore.NewNetworkParameters(connectionSource)
    47  	blockStore := sqlstore.NewBlocks(connectionSource)
    48  	block1 := addTestBlock(t, ctx, blockStore)
    49  	block2 := addTestBlock(t, ctx, blockStore)
    50  
    51  	param1a := addNetParam(t, ctx, netParamStore, "foo", "bar", block1)
    52  	param1b := addNetParam(t, ctx, netParamStore, "foo", "baz", block1)
    53  	param2a := addNetParam(t, ctx, netParamStore, "cake", "apples", block1)
    54  	param2b := addNetParam(t, ctx, netParamStore, "cake", "banana", block2)
    55  
    56  	_ = param1a
    57  	_ = param2a
    58  
    59  	t.Run("GetAll", func(t *testing.T) {
    60  		expected := []entities.NetworkParameter{param2b, param1b}
    61  		pagination := entities.CursorPagination{}
    62  		actual, _, err := netParamStore.GetAll(ctx, pagination)
    63  		require.NoError(t, err)
    64  		assert.Equal(t, expected, actual)
    65  	})
    66  
    67  	t.Run("GetByTxHash", func(t *testing.T) {
    68  		expected := []entities.NetworkParameter{param2b}
    69  		actual, err := netParamStore.GetByTxHash(ctx, param2b.TxHash)
    70  		require.NoError(t, err)
    71  		assert.Equal(t, expected, actual)
    72  	})
    73  
    74  	t.Run("GetByKey", func(t *testing.T) {
    75  		param, err := netParamStore.GetByKey(ctx, "foo")
    76  		require.NoError(t, err)
    77  		assert.Equal(t, "baz", param.Value)
    78  
    79  		_, err = netParamStore.GetByKey(ctx, "foo1")
    80  		assert.Equal(t, entities.ErrNotFound, err)
    81  	})
    82  }
    83  
    84  func TestNetworkParameterPagination(t *testing.T) {
    85  	t.Run("should return all network parameters if no pagination is specified", testNetworkParameterPaginationNoPagination)
    86  	t.Run("should return first page of network parameters if first is provided", testNetworkParameterPaginationFirst)
    87  	t.Run("should return last page of network parameters if last is provided", testNetworkParameterPaginationLast)
    88  	t.Run("should return specified page of network parameters if first and after is specified", testNetworkParameterPaginationFirstAndAfter)
    89  	t.Run("should return specified page of network parameters if last and before is specified", testNetworkParameterPaginationLastAndBefore)
    90  }
    91  
    92  func testNetworkParameterPaginationNoPagination(t *testing.T) {
    93  	ctx := tempTransaction(t)
    94  
    95  	ps, parameters := setupNetworkParameterPaginationTest(t, ctx)
    96  
    97  	pagination, err := entities.NewCursorPagination(nil, nil, nil, nil, false)
    98  	require.NoError(t, err)
    99  	got, pageInfo, err := ps.GetAll(ctx, pagination)
   100  	require.NoError(t, err)
   101  	want := parameters[10:]
   102  	assert.Equal(t, want, got)
   103  	assert.Equal(t, entities.PageInfo{
   104  		HasNextPage:     false,
   105  		HasPreviousPage: false,
   106  		StartCursor:     want[0].Cursor().Encode(),
   107  		EndCursor:       want[9].Cursor().Encode(),
   108  	}, pageInfo)
   109  }
   110  
   111  func testNetworkParameterPaginationFirst(t *testing.T) {
   112  	ctx := tempTransaction(t)
   113  
   114  	ps, parameters := setupNetworkParameterPaginationTest(t, ctx)
   115  
   116  	first := int32(3)
   117  	pagination, err := entities.NewCursorPagination(&first, nil, nil, nil, false)
   118  	require.NoError(t, err)
   119  	got, pageInfo, err := ps.GetAll(ctx, pagination)
   120  	require.NoError(t, err)
   121  	want := parameters[10:13]
   122  	assert.Equal(t, want, got)
   123  	assert.Equal(t, entities.PageInfo{
   124  		HasNextPage:     true,
   125  		HasPreviousPage: false,
   126  		StartCursor:     want[0].Cursor().Encode(),
   127  		EndCursor:       want[2].Cursor().Encode(),
   128  	}, pageInfo)
   129  }
   130  
   131  func testNetworkParameterPaginationLast(t *testing.T) {
   132  	ctx := tempTransaction(t)
   133  
   134  	ps, parameters := setupNetworkParameterPaginationTest(t, ctx)
   135  
   136  	last := int32(3)
   137  	pagination, err := entities.NewCursorPagination(nil, nil, &last, nil, false)
   138  	require.NoError(t, err)
   139  	got, pageInfo, err := ps.GetAll(ctx, pagination)
   140  	require.NoError(t, err)
   141  	want := parameters[17:]
   142  	assert.Equal(t, want, got)
   143  	assert.Equal(t, entities.PageInfo{
   144  		HasNextPage:     false,
   145  		HasPreviousPage: true,
   146  		StartCursor:     want[0].Cursor().Encode(),
   147  		EndCursor:       want[2].Cursor().Encode(),
   148  	}, pageInfo)
   149  }
   150  
   151  func testNetworkParameterPaginationFirstAndAfter(t *testing.T) {
   152  	ctx := tempTransaction(t)
   153  
   154  	ps, parameters := setupNetworkParameterPaginationTest(t, ctx)
   155  
   156  	first := int32(3)
   157  	after := parameters[2].Cursor().Encode()
   158  	pagination, err := entities.NewCursorPagination(&first, &after, nil, nil, false)
   159  	require.NoError(t, err)
   160  	got, pageInfo, err := ps.GetAll(ctx, pagination)
   161  	require.NoError(t, err)
   162  	want := parameters[13:16]
   163  	assert.Equal(t, want, got)
   164  	assert.Equal(t, entities.PageInfo{
   165  		HasNextPage:     true,
   166  		HasPreviousPage: true,
   167  		StartCursor:     want[0].Cursor().Encode(),
   168  		EndCursor:       want[2].Cursor().Encode(),
   169  	}, pageInfo)
   170  }
   171  
   172  func testNetworkParameterPaginationLastAndBefore(t *testing.T) {
   173  	ctx := tempTransaction(t)
   174  
   175  	ps, parameters := setupNetworkParameterPaginationTest(t, ctx)
   176  
   177  	last := int32(3)
   178  	before := parameters[17].Cursor().Encode()
   179  	pagination, err := entities.NewCursorPagination(nil, nil, &last, &before, false)
   180  	require.NoError(t, err)
   181  	got, pageInfo, err := ps.GetAll(ctx, pagination)
   182  	require.NoError(t, err)
   183  	want := parameters[14:17]
   184  	assert.Equal(t, want, got)
   185  	assert.Equal(t, entities.PageInfo{
   186  		HasNextPage:     true,
   187  		HasPreviousPage: true,
   188  		StartCursor:     want[0].Cursor().Encode(),
   189  		EndCursor:       want[2].Cursor().Encode(),
   190  	}, pageInfo)
   191  }
   192  
   193  func setupNetworkParameterPaginationTest(t *testing.T, ctx context.Context) (*sqlstore.NetworkParameters, []entities.NetworkParameter) {
   194  	t.Helper()
   195  	bs := sqlstore.NewBlocks(connectionSource)
   196  	ps := sqlstore.NewNetworkParameters(connectionSource)
   197  
   198  	blockTime := time.Date(2022, 7, 27, 8, 0, 0, 0, time.Local)
   199  	parameters := make([]entities.NetworkParameter, 20)
   200  
   201  	for i := 0; i < 10; i++ {
   202  		blockTime = blockTime.Add(time.Minute)
   203  		block := addTestBlockForTime(t, ctx, bs, blockTime)
   204  		id := int64(i + 1)
   205  		parameters[i] = addNetParam(t, ctx, ps, fmt.Sprintf("key%02d", id), fmt.Sprintf("value%02d", id), block)
   206  	}
   207  
   208  	for i := 0; i < 10; i++ {
   209  		blockTime = blockTime.Add(time.Minute)
   210  		block := addTestBlockForTime(t, ctx, bs, blockTime)
   211  		id := int64(i + 1)
   212  		parameters[10+i] = addNetParam(t, ctx, ps, fmt.Sprintf("key%02d", id), fmt.Sprintf("value%02d", id+10), block)
   213  	}
   214  	return ps, parameters
   215  }