github.com/ava-labs/avalanchego@v1.11.11/vms/components/gas/gas_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package gas
     5  
     6  import (
     7  	"fmt"
     8  	"math"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  var calculatePriceTests = []struct {
    15  	minPrice                 Price
    16  	excess                   Gas
    17  	excessConversionConstant Gas
    18  	expected                 Price
    19  }{
    20  	{
    21  		minPrice:                 1,
    22  		excess:                   0,
    23  		excessConversionConstant: 1,
    24  		expected:                 1,
    25  	},
    26  	{
    27  		minPrice:                 1,
    28  		excess:                   1,
    29  		excessConversionConstant: 1,
    30  		expected:                 2,
    31  	},
    32  	{
    33  		minPrice:                 1,
    34  		excess:                   2,
    35  		excessConversionConstant: 1,
    36  		expected:                 6,
    37  	},
    38  	{
    39  		minPrice:                 1,
    40  		excess:                   10_000,
    41  		excessConversionConstant: 10_000,
    42  		expected:                 2,
    43  	},
    44  	{
    45  		minPrice:                 1,
    46  		excess:                   1_000_000,
    47  		excessConversionConstant: 10_000,
    48  		expected:                 math.MaxUint64,
    49  	},
    50  	{
    51  		minPrice:                 10,
    52  		excess:                   10_000_000,
    53  		excessConversionConstant: 1_000_000,
    54  		expected:                 220_264,
    55  	},
    56  	{
    57  		minPrice:                 math.MaxUint64,
    58  		excess:                   math.MaxUint64,
    59  		excessConversionConstant: 1,
    60  		expected:                 math.MaxUint64,
    61  	},
    62  	{
    63  		minPrice:                 math.MaxUint32,
    64  		excess:                   1,
    65  		excessConversionConstant: 1,
    66  		expected:                 11_674_931_546,
    67  	},
    68  	{
    69  		minPrice:                 6_786_177_901_268_885_274, // ~ MaxUint64 / e
    70  		excess:                   1,
    71  		excessConversionConstant: 1,
    72  		expected:                 math.MaxUint64 - 11,
    73  	},
    74  	{
    75  		minPrice:                 6_786_177_901_268_885_274, // ~ MaxUint64 / e
    76  		excess:                   math.MaxUint64,
    77  		excessConversionConstant: math.MaxUint64,
    78  		expected:                 math.MaxUint64 - 1,
    79  	},
    80  }
    81  
    82  func Test_Gas_Cost(t *testing.T) {
    83  	require := require.New(t)
    84  
    85  	const (
    86  		gas      Gas    = 40
    87  		price    Price  = 100
    88  		expected uint64 = 4000
    89  	)
    90  	actual, err := gas.Cost(price)
    91  	require.NoError(err)
    92  	require.Equal(expected, actual)
    93  }
    94  
    95  func Test_Gas_AddPerSecond(t *testing.T) {
    96  	tests := []struct {
    97  		initial      Gas
    98  		gasPerSecond Gas
    99  		seconds      uint64
   100  		expected     Gas
   101  	}{
   102  		{
   103  			initial:      5,
   104  			gasPerSecond: 1,
   105  			seconds:      2,
   106  			expected:     7,
   107  		},
   108  		{
   109  			initial:      5,
   110  			gasPerSecond: math.MaxUint64,
   111  			seconds:      2,
   112  			expected:     math.MaxUint64,
   113  		},
   114  		{
   115  			initial:      math.MaxUint64,
   116  			gasPerSecond: 1,
   117  			seconds:      2,
   118  			expected:     math.MaxUint64,
   119  		},
   120  		{
   121  			initial:      math.MaxUint64,
   122  			gasPerSecond: math.MaxUint64,
   123  			seconds:      math.MaxUint64,
   124  			expected:     math.MaxUint64,
   125  		},
   126  	}
   127  	for _, test := range tests {
   128  		t.Run(fmt.Sprintf("%d+%d*%d=%d", test.initial, test.gasPerSecond, test.seconds, test.expected), func(t *testing.T) {
   129  			actual := test.initial.AddPerSecond(test.gasPerSecond, test.seconds)
   130  			require.Equal(t, test.expected, actual)
   131  		})
   132  	}
   133  }
   134  
   135  func Test_Gas_SubPerSecond(t *testing.T) {
   136  	tests := []struct {
   137  		initial      Gas
   138  		gasPerSecond Gas
   139  		seconds      uint64
   140  		expected     Gas
   141  	}{
   142  		{
   143  			initial:      5,
   144  			gasPerSecond: 1,
   145  			seconds:      2,
   146  			expected:     3,
   147  		},
   148  		{
   149  			initial:      5,
   150  			gasPerSecond: math.MaxUint64,
   151  			seconds:      2,
   152  			expected:     0,
   153  		},
   154  		{
   155  			initial:      1,
   156  			gasPerSecond: 1,
   157  			seconds:      2,
   158  			expected:     0,
   159  		},
   160  		{
   161  			initial:      math.MaxUint64,
   162  			gasPerSecond: math.MaxUint64,
   163  			seconds:      math.MaxUint64,
   164  			expected:     0,
   165  		},
   166  	}
   167  	for _, test := range tests {
   168  		t.Run(fmt.Sprintf("%d-%d*%d=%d", test.initial, test.gasPerSecond, test.seconds, test.expected), func(t *testing.T) {
   169  			actual := test.initial.SubPerSecond(test.gasPerSecond, test.seconds)
   170  			require.Equal(t, test.expected, actual)
   171  		})
   172  	}
   173  }
   174  
   175  func Test_CalculatePrice(t *testing.T) {
   176  	for _, test := range calculatePriceTests {
   177  		t.Run(fmt.Sprintf("%d*e^(%d/%d)=%d", test.minPrice, test.excess, test.excessConversionConstant, test.expected), func(t *testing.T) {
   178  			actual := CalculatePrice(test.minPrice, test.excess, test.excessConversionConstant)
   179  			require.Equal(t, test.expected, actual)
   180  		})
   181  	}
   182  }
   183  
   184  func Benchmark_CalculatePrice(b *testing.B) {
   185  	for _, test := range calculatePriceTests {
   186  		b.Run(fmt.Sprintf("%d*e^(%d/%d)=%d", test.minPrice, test.excess, test.excessConversionConstant, test.expected), func(b *testing.B) {
   187  			for i := 0; i < b.N; i++ {
   188  				CalculatePrice(test.minPrice, test.excess, test.excessConversionConstant)
   189  			}
   190  		})
   191  	}
   192  }