kaspad/domain/consensus/processes/coinbasemanager/coinbasemanager_test.go
stasatdaglabs 0bdd19136f
Implement the new monetary policy (#1892)
* Remove unused functions, constants, and variables.

* Rename maxSubsidy to baseSubsidy.

* Remove unused parameters from CalcBlockSubsidy.

* Remove link to old monetary policy.

* If a block's DAA score is smaller than half a year, it should have a base subsidy.

* Fix merge errors.

* Fix more merge errors.

* Add DeflationaryPhaseBaseSubsidy to the params.

* Implement TestCalcDeflationaryPeriodBlockSubsidy.

* Implement calcDeflationaryPeriodBlockSubsidy naively.

* Implement calcDeflationaryPeriodBlockSubsidy not naively.

* Adjust the subsidy based on target block rate.

* Fix deflationaryPhaseDaaScore in TestCalcDeflationaryPeriodBlockSubsidy.

* Explain how secondsPerMonth is calculated.

* Don't adjust the subsidy based on the target block rate.

* Update defaultDeflationaryPhaseDaaScore and add an explanation.

* Use a pre-calculated table for subsidy per month

* Make the generation function fail if base subsidy is changed

* go fmt

* Use test logger for printing + simplify print loop

Co-authored-by: msutton <mikisiton2@gmail.com>
Co-authored-by: Ori Newman <orinewman1@gmail.com>
2021-12-29 20:49:20 +02:00

127 lines
3.6 KiB
Go

package coinbasemanager
import (
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
"github.com/kaspanet/kaspad/domain/dagconfig"
"strconv"
"testing"
)
func TestCalcDeflationaryPeriodBlockSubsidy(t *testing.T) {
const secondsPerMonth = 2629800
const secondsPerHalving = secondsPerMonth * 12
const deflationaryPhaseDaaScore = secondsPerMonth * 6
const deflationaryPhaseBaseSubsidy = 440 * constants.SompiPerKaspa
coinbaseManagerInterface := New(
nil,
0,
0,
0,
&externalapi.DomainHash{},
deflationaryPhaseDaaScore,
deflationaryPhaseBaseSubsidy,
nil,
nil,
nil,
nil,
nil,
nil,
nil)
coinbaseManagerInstance := coinbaseManagerInterface.(*coinbaseManager)
tests := []struct {
name string
blockDaaScore uint64
expectedBlockSubsidy uint64
}{
{
name: "start of deflationary phase",
blockDaaScore: deflationaryPhaseDaaScore,
expectedBlockSubsidy: deflationaryPhaseBaseSubsidy,
},
{
name: "after one halving",
blockDaaScore: deflationaryPhaseDaaScore + secondsPerHalving,
expectedBlockSubsidy: deflationaryPhaseBaseSubsidy / 2,
},
{
name: "after two halvings",
blockDaaScore: deflationaryPhaseDaaScore + secondsPerHalving*2,
expectedBlockSubsidy: deflationaryPhaseBaseSubsidy / 4,
},
{
name: "after five halvings",
blockDaaScore: deflationaryPhaseDaaScore + secondsPerHalving*5,
expectedBlockSubsidy: deflationaryPhaseBaseSubsidy / 32,
},
{
name: "after 32 halvings",
blockDaaScore: deflationaryPhaseDaaScore + secondsPerHalving*32,
expectedBlockSubsidy: deflationaryPhaseBaseSubsidy / 4294967296,
},
{
name: "just before subsidy depleted",
blockDaaScore: deflationaryPhaseDaaScore + secondsPerHalving*35,
expectedBlockSubsidy: 1,
},
{
name: "after subsidy depleted",
blockDaaScore: deflationaryPhaseDaaScore + secondsPerHalving*36,
expectedBlockSubsidy: 0,
},
}
for _, test := range tests {
blockSubsidy := coinbaseManagerInstance.calcDeflationaryPeriodBlockSubsidy(test.blockDaaScore)
if blockSubsidy != test.expectedBlockSubsidy {
t.Errorf("TestCalcDeflationaryPeriodBlockSubsidy: test '%s' failed. Want: %d, got: %d",
test.name, test.expectedBlockSubsidy, blockSubsidy)
}
}
}
func TestBuildSubsidyTable(t *testing.T) {
deflationaryPhaseBaseSubsidy := dagconfig.MainnetParams.DeflationaryPhaseBaseSubsidy
if deflationaryPhaseBaseSubsidy != 440*constants.SompiPerKaspa {
t.Errorf("TestBuildSubsidyTable: table generation function was not updated to reflect "+
"the new base subsidy %d. Please fix the constant above and replace subsidyByDeflationaryMonthTable "+
"in coinbasemanager.go with the printed table", deflationaryPhaseBaseSubsidy)
}
coinbaseManagerInterface := New(
nil,
0,
0,
0,
&externalapi.DomainHash{},
0,
deflationaryPhaseBaseSubsidy,
nil,
nil,
nil,
nil,
nil,
nil,
nil)
coinbaseManagerInstance := coinbaseManagerInterface.(*coinbaseManager)
var subsidyTable []uint64
for M := uint64(0); ; M++ {
subsidy := coinbaseManagerInstance.calcDeflationaryPeriodBlockSubsidyFloatCalc(M)
subsidyTable = append(subsidyTable, subsidy)
if subsidy == 0 {
break
}
}
tableStr := "\n{\t"
for i := 0; i < len(subsidyTable); i++ {
tableStr += strconv.FormatUint(subsidyTable[i], 10) + ", "
if (i+1)%25 == 0 {
tableStr += "\n\t"
}
}
tableStr += "\n}"
t.Logf(tableStr)
}