code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/epoch_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  	"testing"
    21  	"time"
    22  
    23  	"code.vegaprotocol.io/vega/datanode/entities"
    24  	"code.vegaprotocol.io/vega/datanode/sqlstore"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func addTestEpoch(t *testing.T, ctx context.Context, es *sqlstore.Epochs,
    31  	epochID int64,
    32  	startTime time.Time,
    33  	expireTime time.Time,
    34  	endTime *time.Time,
    35  	block entities.Block,
    36  ) entities.Epoch {
    37  	t.Helper()
    38  	r := entities.Epoch{
    39  		ID:         epochID,
    40  		StartTime:  startTime,
    41  		ExpireTime: expireTime,
    42  		EndTime:    endTime,
    43  		VegaTime:   block.VegaTime,
    44  	}
    45  	if endTime == nil {
    46  		r.FirstBlock = &block.Height
    47  	} else {
    48  		r.LastBlock = &block.Height
    49  	}
    50  	err := es.Add(ctx, r)
    51  	require.NoError(t, err)
    52  	return r
    53  }
    54  
    55  func TestEpochs(t *testing.T) {
    56  	ctx := tempTransaction(t)
    57  
    58  	es := sqlstore.NewEpochs(connectionSource)
    59  	bs := sqlstore.NewBlocks(connectionSource)
    60  
    61  	epoch1Start := time.Date(2022, 1, 1, 0, 0, 0, 0, time.Local)
    62  	epoch1Expire := epoch1Start.Add(time.Minute)
    63  	epoch1End := epoch1Start.Add(time.Second)
    64  	epoch2Start := epoch1End
    65  	epoch2Expire := epoch2Start.Add(time.Minute)
    66  	epoch2End := epoch2Start.Add(time.Second)
    67  	epoch3Start := epoch2End
    68  	epoch3Expire := epoch3Start.Add(time.Minute)
    69  
    70  	block1 := addTestBlockForHeightAndTime(t, ctx, bs, 1, epoch1Start)
    71  	block2 := addTestBlockForHeightAndTime(t, ctx, bs, 2, epoch2Start)
    72  	block3 := addTestBlockForHeightAndTime(t, ctx, bs, 3, epoch3Start)
    73  
    74  	// Insert one epoch that gets updated in the same block
    75  	epoch1 := addTestEpoch(t, ctx, es, 1, epoch1Start, epoch1Expire, nil, block1)
    76  	epoch1b := addTestEpoch(t, ctx, es, 1, epoch1Start, epoch1Expire, &epoch1End, block2)
    77  	epoch1b.FirstBlock = epoch1.FirstBlock
    78  
    79  	// And another which is updated in a subsequent block
    80  	epoch2 := addTestEpoch(t, ctx, es, 2, epoch2Start, epoch2Expire, nil, block2)
    81  	epoch2b := addTestEpoch(t, ctx, es, 2, epoch2Start, epoch2Expire, &epoch2End, block3)
    82  	epoch2b.FirstBlock = epoch2.FirstBlock
    83  
    84  	// And finally one which isn't updated (e.g. hasn't ended yet)
    85  	epoch3 := addTestEpoch(t, ctx, es, 3, epoch3Start, epoch3Expire, nil, block3)
    86  
    87  	t.Run("GetAll", func(t *testing.T) {
    88  		expected := []entities.Epoch{epoch1b, epoch2b, epoch3}
    89  		actual, err := es.GetAll(ctx)
    90  		require.NoError(t, err)
    91  		assert.Equal(t, expected, actual)
    92  	})
    93  
    94  	t.Run("GetCurrent", func(t *testing.T) {
    95  		actual, err := es.GetCurrent(ctx)
    96  		require.NoError(t, err)
    97  		assert.Equal(t, epoch3, actual)
    98  	})
    99  
   100  	t.Run("Get", func(t *testing.T) {
   101  		actual, err := es.Get(ctx, 2)
   102  		require.NoError(t, err)
   103  		assert.Equal(t, epoch2b, actual)
   104  	})
   105  
   106  	t.Run("GetByBlock", func(t *testing.T) {
   107  		actual, err := es.GetByBlock(ctx, uint64(block2.Height))
   108  		require.NoError(t, err)
   109  		assert.Equal(t, epoch2b, actual)
   110  
   111  		actual, err = es.GetByBlock(ctx, uint64(block3.Height))
   112  		require.NoError(t, err)
   113  		assert.Equal(t, epoch3, actual)
   114  	})
   115  }