github.com/kaspanet/kaspad@v0.12.15/domain/consensus/processes/coinbasemanager/coinbasemanager_test.go (about)

     1  package coinbasemanager
     2  
     3  import (
     4  	"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
     5  	"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
     6  	"github.com/kaspanet/kaspad/domain/dagconfig"
     7  	"strconv"
     8  	"testing"
     9  )
    10  
    11  func TestCalcDeflationaryPeriodBlockSubsidy(t *testing.T) {
    12  	const secondsPerMonth = 2629800
    13  	const secondsPerHalving = secondsPerMonth * 12
    14  	const deflationaryPhaseDaaScore = secondsPerMonth * 6
    15  	const deflationaryPhaseBaseSubsidy = 440 * constants.SompiPerKaspa
    16  	coinbaseManagerInterface := New(
    17  		nil,
    18  		0,
    19  		0,
    20  		0,
    21  		&externalapi.DomainHash{},
    22  		deflationaryPhaseDaaScore,
    23  		deflationaryPhaseBaseSubsidy,
    24  		nil,
    25  		nil,
    26  		nil,
    27  		nil,
    28  		nil,
    29  		nil,
    30  		nil)
    31  	coinbaseManagerInstance := coinbaseManagerInterface.(*coinbaseManager)
    32  
    33  	tests := []struct {
    34  		name                 string
    35  		blockDaaScore        uint64
    36  		expectedBlockSubsidy uint64
    37  	}{
    38  		{
    39  			name:                 "start of deflationary phase",
    40  			blockDaaScore:        deflationaryPhaseDaaScore,
    41  			expectedBlockSubsidy: deflationaryPhaseBaseSubsidy,
    42  		},
    43  		{
    44  			name:                 "after one halving",
    45  			blockDaaScore:        deflationaryPhaseDaaScore + secondsPerHalving,
    46  			expectedBlockSubsidy: deflationaryPhaseBaseSubsidy / 2,
    47  		},
    48  		{
    49  			name:                 "after two halvings",
    50  			blockDaaScore:        deflationaryPhaseDaaScore + secondsPerHalving*2,
    51  			expectedBlockSubsidy: deflationaryPhaseBaseSubsidy / 4,
    52  		},
    53  		{
    54  			name:                 "after five halvings",
    55  			blockDaaScore:        deflationaryPhaseDaaScore + secondsPerHalving*5,
    56  			expectedBlockSubsidy: deflationaryPhaseBaseSubsidy / 32,
    57  		},
    58  		{
    59  			name:                 "after 32 halvings",
    60  			blockDaaScore:        deflationaryPhaseDaaScore + secondsPerHalving*32,
    61  			expectedBlockSubsidy: deflationaryPhaseBaseSubsidy / 4294967296,
    62  		},
    63  		{
    64  			name:                 "just before subsidy depleted",
    65  			blockDaaScore:        deflationaryPhaseDaaScore + secondsPerHalving*35,
    66  			expectedBlockSubsidy: 1,
    67  		},
    68  		{
    69  			name:                 "after subsidy depleted",
    70  			blockDaaScore:        deflationaryPhaseDaaScore + secondsPerHalving*36,
    71  			expectedBlockSubsidy: 0,
    72  		},
    73  	}
    74  
    75  	for _, test := range tests {
    76  		blockSubsidy := coinbaseManagerInstance.calcDeflationaryPeriodBlockSubsidy(test.blockDaaScore)
    77  		if blockSubsidy != test.expectedBlockSubsidy {
    78  			t.Errorf("TestCalcDeflationaryPeriodBlockSubsidy: test '%s' failed. Want: %d, got: %d",
    79  				test.name, test.expectedBlockSubsidy, blockSubsidy)
    80  		}
    81  	}
    82  }
    83  
    84  func TestBuildSubsidyTable(t *testing.T) {
    85  	deflationaryPhaseBaseSubsidy := dagconfig.MainnetParams.DeflationaryPhaseBaseSubsidy
    86  	if deflationaryPhaseBaseSubsidy != 440*constants.SompiPerKaspa {
    87  		t.Errorf("TestBuildSubsidyTable: table generation function was not updated to reflect "+
    88  			"the new base subsidy %d. Please fix the constant above and replace subsidyByDeflationaryMonthTable "+
    89  			"in coinbasemanager.go with the printed table", deflationaryPhaseBaseSubsidy)
    90  	}
    91  	coinbaseManagerInterface := New(
    92  		nil,
    93  		0,
    94  		0,
    95  		0,
    96  		&externalapi.DomainHash{},
    97  		0,
    98  		deflationaryPhaseBaseSubsidy,
    99  		nil,
   100  		nil,
   101  		nil,
   102  		nil,
   103  		nil,
   104  		nil,
   105  		nil)
   106  	coinbaseManagerInstance := coinbaseManagerInterface.(*coinbaseManager)
   107  
   108  	var subsidyTable []uint64
   109  	for M := uint64(0); ; M++ {
   110  		subsidy := coinbaseManagerInstance.calcDeflationaryPeriodBlockSubsidyFloatCalc(M)
   111  		subsidyTable = append(subsidyTable, subsidy)
   112  		if subsidy == 0 {
   113  			break
   114  		}
   115  	}
   116  
   117  	tableStr := "\n{\t"
   118  	for i := 0; i < len(subsidyTable); i++ {
   119  		tableStr += strconv.FormatUint(subsidyTable[i], 10) + ", "
   120  		if (i+1)%25 == 0 {
   121  			tableStr += "\n\t"
   122  		}
   123  	}
   124  	tableStr += "\n}"
   125  	t.Logf(tableStr)
   126  }