code.vegaprotocol.io/vega@v0.79.0/core/netparams/checks/checks_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 checks_test
    17  
    18  import (
    19  	"testing"
    20  
    21  	"code.vegaprotocol.io/vega/core/netparams/checks"
    22  	types "code.vegaprotocol.io/vega/protos/vega"
    23  
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestLongBlockChecks(t *testing.T) {
    28  	// wrong type - error
    29  	type Junk struct{}
    30  	require.Equal(t, "invalid long block auction duration table", checks.LongBlockAuctionDurationTable()(&Junk{}, &Junk{}).Error())
    31  
    32  	// empty - is fine
    33  	table := &types.LongBlockAuctionDurationTable{}
    34  	require.NoError(t, checks.LongBlockAuctionDurationTable()(table, nil))
    35  
    36  	// invalid threshold at index 0 - error
    37  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "banana", Duration: "1s"}}
    38  	require.Equal(t, "invalid long block auction duration table - threshold at index 0 is not a valid duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    39  
    40  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "-1", Duration: "1s"}}
    41  	require.Equal(t, "invalid long block auction duration table - threshold at index 0 is not a valid duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    42  
    43  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "-1s", Duration: "1s"}}
    44  	require.Equal(t, "invalid long block auction duration table - threshold at index 0 is not a positive duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    45  
    46  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "0s", Duration: "1s"}}
    47  	require.Equal(t, "invalid long block auction duration table - threshold at index 0 is not a positive duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    48  
    49  	// invalid duration a index 0 - error
    50  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "1s", Duration: "banana"}}
    51  	require.Equal(t, "invalid long block auction duration table - duration at index 0 is not a valid duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    52  
    53  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "1s", Duration: "-1"}}
    54  	require.Equal(t, "invalid long block auction duration table - duration at index 0 is not a valid duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    55  
    56  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "1s", Duration: "-1s"}}
    57  	require.Equal(t, "invalid long block auction duration table - duration at index 0 is not a positive duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    58  
    59  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "1s", Duration: "0s"}}
    60  	require.Equal(t, "invalid long block auction duration table - duration at index 0 is not a positive duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    61  
    62  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "1s", Duration: "0.5s"}}
    63  	require.Equal(t, "invalid long block auction duration table - duration at index 0 is less than one second", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    64  
    65  	// invalid threshold at index 1 - error
    66  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "1s", Duration: "1s"}, {Threshold: "banana", Duration: "1s"}}
    67  	require.Equal(t, "invalid long block auction duration table - threshold at index 1 is not a valid duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    68  
    69  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "1s", Duration: "1s"}, {Threshold: "-1", Duration: "1s"}}
    70  	require.Equal(t, "invalid long block auction duration table - threshold at index 1 is not a valid duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    71  
    72  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "1s", Duration: "1s"}, {Threshold: "-1s", Duration: "1s"}}
    73  	require.Equal(t, "invalid long block auction duration table - threshold at index 1 is not a positive duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    74  
    75  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "1s", Duration: "1s"}, {Threshold: "0s", Duration: "1s"}}
    76  	require.Equal(t, "invalid long block auction duration table - threshold at index 1 is not a positive duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    77  
    78  	// invalid duration a index 1 - error
    79  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "1s", Duration: "1s"}, {Threshold: "1s", Duration: "banana"}}
    80  	require.Equal(t, "invalid long block auction duration table - duration at index 1 is not a valid duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    81  
    82  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "1s", Duration: "1s"}, {Threshold: "1s", Duration: "-1"}}
    83  	require.Equal(t, "invalid long block auction duration table - duration at index 1 is not a valid duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    84  
    85  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "1s", Duration: "1s"}, {Threshold: "1s", Duration: "-1s"}}
    86  	require.Equal(t, "invalid long block auction duration table - duration at index 1 is not a positive duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    87  
    88  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "1s", Duration: "1s"}, {Threshold: "1s", Duration: "0s"}}
    89  	require.Equal(t, "invalid long block auction duration table - duration at index 1 is not a positive duration", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    90  
    91  	// duplicate threshold - error
    92  	table.ThresholdAndDuration = []*types.LongBlockAuction{{Threshold: "1s", Duration: "1s"}, {Threshold: "1s", Duration: "100s"}}
    93  	require.Equal(t, "invalid long block auction duration table - duplicate threshold", checks.LongBlockAuctionDurationTable()(table, nil).Error())
    94  }