github.com/klaytn/klaytn@v1.12.1/params/governance_params_test.go (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package params
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestSetProposerUpdateInterval(t *testing.T) {
    24  	testData := []uint64{3600, 100, 500, 7200, 10}
    25  
    26  	for i := 0; i < len(testData); i++ {
    27  		SetProposerUpdateInterval(testData[i])
    28  
    29  		if ProposerUpdateInterval() != testData[i] {
    30  			t.Errorf("ProposerUadateInterval is different from the given testData. Result : %v, Expected : %v", ProposerUpdateInterval(), testData[i])
    31  		}
    32  	}
    33  }
    34  
    35  func TestSetStakingUpdateInterval(t *testing.T) {
    36  	testData := []uint64{1000, 3600, 86400, 100, 500, 7200, 10}
    37  
    38  	for i := 0; i < len(testData); i++ {
    39  		SetStakingUpdateInterval(testData[i])
    40  
    41  		if StakingUpdateInterval() != testData[i] {
    42  			t.Errorf("StakingUpdateInterval is different from the given testData. Result : %v, Expected : %v", StakingUpdateInterval(), testData[i])
    43  		}
    44  	}
    45  }
    46  
    47  func TestIsProposerUpdateInterval(t *testing.T) {
    48  	testCase := []struct {
    49  		interval uint64
    50  		blockNu  uint64
    51  		result   bool
    52  	}{
    53  		{10, 3, false},
    54  		{10, 10, true},
    55  		{10, 11, false},
    56  		{10, 20, true},
    57  		{10, 21, false},
    58  		{100, 99, false},
    59  		{100, 100, true},
    60  		{100, 101, false},
    61  		{100, 200, true},
    62  		{100, 201, false},
    63  		{3600, 3599, false},
    64  		{3600, 3600, true},
    65  		{3600, 3601, false},
    66  		{3600, 7200, true},
    67  		{3600, 7201, false},
    68  		{3600, 36000, true},
    69  		{3600, 36001, false},
    70  	}
    71  
    72  	for i := 0; i < len(testCase); i++ {
    73  		SetProposerUpdateInterval(testCase[i].interval)
    74  
    75  		result, _ := IsProposerUpdateInterval(testCase[i].blockNu)
    76  
    77  		if result != testCase[i].result {
    78  			t.Errorf("The result is different from the expected result. Result : %v, Expected : %v, block number : %v, update interval : %v",
    79  				result, testCase[i].result, testCase[i].blockNu, testCase[i].interval)
    80  		}
    81  	}
    82  }
    83  
    84  func TestIsStakingUpdatePossible(t *testing.T) {
    85  	testCase := []struct {
    86  		interval uint64
    87  		blockNu  uint64
    88  		result   bool
    89  	}{
    90  		{10, 3, false},
    91  		{10, 10, true},
    92  		{10, 11, false},
    93  		{10, 20, true},
    94  		{10, 21, false},
    95  		{3600, 3599, false},
    96  		{3600, 3600, true},
    97  		{3600, 3601, false},
    98  		{3600, 7200, true},
    99  		{3600, 7201, false},
   100  		{3600, 36000, true},
   101  		{3600, 36001, false},
   102  		{86400, 86399, false},
   103  		{86400, 86400, true},
   104  		{86400, 86401, false},
   105  		{86400, 864000, true},
   106  		{86400, 864001, false},
   107  	}
   108  
   109  	for i := 0; i < len(testCase); i++ {
   110  		SetStakingUpdateInterval(testCase[i].interval)
   111  
   112  		result := IsStakingUpdateInterval(testCase[i].blockNu)
   113  
   114  		if result != testCase[i].result {
   115  			t.Errorf("The result is different from the expected result. Result : %v, Expected : %v, block number : %v, update interval : %v",
   116  				result, testCase[i].result, testCase[i].blockNu, testCase[i].interval)
   117  		}
   118  	}
   119  }
   120  
   121  func TestCalcProposerBlockNumber(t *testing.T) {
   122  	testCase := []struct {
   123  		interval uint64
   124  		blockNu  uint64
   125  		result   uint64
   126  	}{
   127  		{10, 3, 0},
   128  		{10, 10, 0},
   129  		{10, 11, 10},
   130  		{10, 20, 10},
   131  		{10, 21, 20},
   132  		{3600, 3599, 0},
   133  		{3600, 3600, 0},
   134  		{3600, 3601, 3600},
   135  		{3600, 7199, 3600},
   136  		{3600, 7200, 3600},
   137  		{3600, 7201, 7200},
   138  	}
   139  
   140  	for i := 0; i < len(testCase); i++ {
   141  		SetProposerUpdateInterval(testCase[i].interval)
   142  
   143  		result := CalcProposerBlockNumber(testCase[i].blockNu)
   144  
   145  		if result != testCase[i].result {
   146  			t.Errorf("The result is different from the expected result. Result : %v, Expected : %v, block number : %v, update interval : %v",
   147  				result, testCase[i].result, testCase[i].blockNu, testCase[i].interval)
   148  		}
   149  	}
   150  }
   151  
   152  func TestCalcStakingBlockNumber(t *testing.T) {
   153  	testCase := []struct {
   154  		interval uint64
   155  		blockNu  uint64
   156  		result   uint64
   157  	}{
   158  		{10, 3, 0},
   159  		{10, 10, 0},
   160  		{10, 11, 0},
   161  		{10, 20, 0},
   162  		{10, 21, 10},
   163  		{10, 30, 10},
   164  		{10, 31, 20},
   165  		{10, 40, 20},
   166  		{3600, 3600, 0},
   167  		{3600, 5000, 0},
   168  		{3600, 7200, 0},
   169  		{3600, 7201, 3600},
   170  		{3600, 10800, 3600},
   171  		{3600, 10801, 7200},
   172  		{86400, 3600, 0},
   173  		{86400, 10000, 0},
   174  		{86400, 86400, 0},
   175  		{86400, 172800, 0},
   176  		{86400, 172801, 86400},
   177  		{86400, 259200, 86400},
   178  		{86400, 259201, 172800},
   179  	}
   180  
   181  	for i := 0; i < len(testCase); i++ {
   182  		SetStakingUpdateInterval(testCase[i].interval)
   183  
   184  		result := CalcStakingBlockNumber(testCase[i].blockNu)
   185  
   186  		if result != testCase[i].result {
   187  			t.Errorf("The result is different from the expected result. Result : %v, Expected : %v, block number : %v, update interval : %v",
   188  				result, testCase[i].result, testCase[i].blockNu, testCase[i].interval)
   189  		}
   190  	}
   191  }