code.vegaprotocol.io/vega@v0.79.0/datanode/networkhistory/segment/contiguous_history_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 segment_test
    17  
    18  import (
    19  	"testing"
    20  
    21  	"code.vegaprotocol.io/vega/datanode/networkhistory/segment"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestContiguousHistory(t *testing.T) {
    27  	var segments segment.Segments[segment.Base]
    28  
    29  	_, err := segments.ContiguousHistoryInRange(0, 0)
    30  	assert.NotNil(t, err)
    31  
    32  	segments = []segment.Base{
    33  		{HeightFrom: 0, HeightTo: 1000},
    34  		{HeightFrom: 1001, HeightTo: 2000},
    35  		{HeightFrom: 2001, HeightTo: 3000},
    36  	}
    37  
    38  	ch, err := segments.ContiguousHistoryInRange(0, 3000)
    39  	assert.NoError(t, err)
    40  
    41  	assert.Equal(t, 3, len(ch.Segments))
    42  	assert.Equal(t, int64(0), ch.HeightFrom)
    43  	assert.Equal(t, int64(3000), ch.HeightTo)
    44  
    45  	assert.Equal(t, int64(3000), ch.Segments[2].GetToHeight())
    46  	assert.Equal(t, int64(2001), ch.Segments[2].GetFromHeight())
    47  	assert.Equal(t, int64(2000), ch.Segments[1].GetToHeight())
    48  	assert.Equal(t, int64(1001), ch.Segments[1].GetFromHeight())
    49  	assert.Equal(t, int64(1000), ch.Segments[0].GetToHeight())
    50  	assert.Equal(t, int64(0), ch.Segments[0].GetFromHeight())
    51  
    52  	segments = []segment.Base{
    53  		{HeightFrom: 2001, HeightTo: 3000},
    54  		{HeightFrom: 0, HeightTo: 1000},
    55  		{HeightFrom: 3001, HeightTo: 4000},
    56  		{HeightFrom: 1001, HeightTo: 2000},
    57  	}
    58  
    59  	ch, err = segments.ContiguousHistoryInRange(0, 4000)
    60  	assert.NoError(t, err)
    61  
    62  	assert.Equal(t, 4, len(ch.Segments))
    63  	assert.Equal(t, int64(0), ch.HeightFrom)
    64  	assert.Equal(t, int64(4000), ch.HeightTo)
    65  
    66  	assert.Equal(t, int64(4000), ch.Segments[3].GetToHeight())
    67  	assert.Equal(t, int64(3001), ch.Segments[3].GetFromHeight())
    68  	assert.Equal(t, int64(3000), ch.Segments[2].GetToHeight())
    69  	assert.Equal(t, int64(2001), ch.Segments[2].GetFromHeight())
    70  	assert.Equal(t, int64(2000), ch.Segments[1].GetToHeight())
    71  	assert.Equal(t, int64(1001), ch.Segments[1].GetFromHeight())
    72  	assert.Equal(t, int64(1000), ch.Segments[0].GetToHeight())
    73  	assert.Equal(t, int64(0), ch.Segments[0].GetFromHeight())
    74  }
    75  
    76  func TestRequestingContiguousHistoryAcrossNonContiguousRangeFails(t *testing.T) {
    77  	segments := segment.Segments[segment.Base]{
    78  		{HeightFrom: 2001, HeightTo: 3000},
    79  		{HeightFrom: 0, HeightTo: 1000},
    80  		{HeightFrom: 3001, HeightTo: 4000},
    81  	}
    82  
    83  	_, err := segments.ContiguousHistoryInRange(0, 4000)
    84  	assert.NotNil(t, err)
    85  }
    86  
    87  func TestContiguousHistoryInRange(t *testing.T) {
    88  	segments := segment.Segments[segment.Base]{
    89  		{HeightFrom: 0, HeightTo: 1000},
    90  		{HeightFrom: 1001, HeightTo: 2000},
    91  		{HeightFrom: 3001, HeightTo: 4000},
    92  		{HeightFrom: 4001, HeightTo: 5000},
    93  		{HeightFrom: 2001, HeightTo: 3000},
    94  		{HeightFrom: 5001, HeightTo: 6000},
    95  	}
    96  
    97  	ch, err := segments.ContiguousHistoryInRange(2001, 5000)
    98  	assert.NoError(t, err)
    99  
   100  	assert.Equal(t, 3, len(ch.Segments))
   101  	assert.Equal(t, int64(2001), ch.HeightFrom)
   102  	assert.Equal(t, int64(5000), ch.HeightTo)
   103  
   104  	assert.Equal(t, int64(5000), ch.Segments[2].GetToHeight())
   105  	assert.Equal(t, int64(4001), ch.Segments[2].GetFromHeight())
   106  	assert.Equal(t, int64(4000), ch.Segments[1].GetToHeight())
   107  	assert.Equal(t, int64(3001), ch.Segments[1].GetFromHeight())
   108  	assert.Equal(t, int64(3000), ch.Segments[0].GetToHeight())
   109  	assert.Equal(t, int64(2001), ch.Segments[0].GetFromHeight())
   110  }
   111  
   112  func TestContiguousHistoryInRangeWithIncorrectFromAndToHeights(t *testing.T) {
   113  	segments := segment.Segments[segment.Base]{
   114  		{HeightFrom: 0, HeightTo: 1000},
   115  		{HeightFrom: 1001, HeightTo: 2000},
   116  		{HeightFrom: 2001, HeightTo: 3000},
   117  	}
   118  
   119  	_, err := segments.ContiguousHistoryInRange(1000, 3000)
   120  	assert.NotNil(t, err)
   121  
   122  	_, err = segments.ContiguousHistoryInRange(0, 2001)
   123  	assert.NotNil(t, err)
   124  }
   125  
   126  func TestAllContigousHistories(t *testing.T) {
   127  	segments := segment.Segments[segment.Base]{
   128  		{HeightFrom: 6001, HeightTo: 7000},
   129  		{HeightFrom: 2001, HeightTo: 3000},
   130  		{HeightFrom: 11001, HeightTo: 12000},
   131  
   132  		{HeightFrom: 0, HeightTo: 1000},
   133  		{HeightFrom: 1001, HeightTo: 2000},
   134  
   135  		{HeightFrom: 16001, HeightTo: 17000},
   136  
   137  		{HeightFrom: 5001, HeightTo: 6000},
   138  
   139  		{HeightFrom: 10001, HeightTo: 11000},
   140  
   141  		{HeightFrom: 12001, HeightTo: 13000},
   142  
   143  		{HeightFrom: 7001, HeightTo: 8000},
   144  	}
   145  
   146  	contiguousHistories := segments.AllContigousHistories()
   147  	assert.Equal(t, 4, len(contiguousHistories))
   148  
   149  	assert.Equal(t, 3, len(contiguousHistories[0].Segments))
   150  	assert.Equal(t, int64(0), contiguousHistories[0].HeightFrom)
   151  	assert.Equal(t, int64(3000), contiguousHistories[0].HeightTo)
   152  	assert.Equal(t, int64(0), contiguousHistories[0].Segments[0].GetFromHeight())
   153  	assert.Equal(t, int64(1000), contiguousHistories[0].Segments[0].GetToHeight())
   154  	assert.Equal(t, int64(1001), contiguousHistories[0].Segments[1].GetFromHeight())
   155  	assert.Equal(t, int64(2000), contiguousHistories[0].Segments[1].GetToHeight())
   156  	assert.Equal(t, int64(2001), contiguousHistories[0].Segments[2].GetFromHeight())
   157  	assert.Equal(t, int64(3000), contiguousHistories[0].Segments[2].GetToHeight())
   158  
   159  	assert.Equal(t, 3, len(contiguousHistories[1].Segments))
   160  	assert.Equal(t, int64(5001), contiguousHistories[1].HeightFrom)
   161  	assert.Equal(t, int64(8000), contiguousHistories[1].HeightTo)
   162  	assert.Equal(t, int64(5001), contiguousHistories[1].Segments[0].GetFromHeight())
   163  	assert.Equal(t, int64(6000), contiguousHistories[1].Segments[0].GetToHeight())
   164  	assert.Equal(t, int64(6001), contiguousHistories[1].Segments[1].GetFromHeight())
   165  	assert.Equal(t, int64(7000), contiguousHistories[1].Segments[1].GetToHeight())
   166  	assert.Equal(t, int64(7001), contiguousHistories[1].Segments[2].GetFromHeight())
   167  	assert.Equal(t, int64(8000), contiguousHistories[1].Segments[2].GetToHeight())
   168  
   169  	assert.Equal(t, 3, len(contiguousHistories[2].Segments))
   170  	assert.Equal(t, int64(10001), contiguousHistories[2].HeightFrom)
   171  	assert.Equal(t, int64(13000), contiguousHistories[2].HeightTo)
   172  	assert.Equal(t, int64(10001), contiguousHistories[2].Segments[0].GetFromHeight())
   173  	assert.Equal(t, int64(11000), contiguousHistories[2].Segments[0].GetToHeight())
   174  	assert.Equal(t, int64(11001), contiguousHistories[2].Segments[1].GetFromHeight())
   175  	assert.Equal(t, int64(12000), contiguousHistories[2].Segments[1].GetToHeight())
   176  	assert.Equal(t, int64(12001), contiguousHistories[2].Segments[2].GetFromHeight())
   177  	assert.Equal(t, int64(13000), contiguousHistories[2].Segments[2].GetToHeight())
   178  
   179  	assert.Equal(t, 1, len(contiguousHistories[3].Segments))
   180  	assert.Equal(t, int64(16001), contiguousHistories[3].HeightFrom)
   181  	assert.Equal(t, int64(17000), contiguousHistories[3].HeightTo)
   182  	assert.Equal(t, int64(16001), contiguousHistories[3].Segments[0].GetFromHeight())
   183  	assert.Equal(t, int64(17000), contiguousHistories[3].Segments[0].GetToHeight())
   184  }