-
Notifications
You must be signed in to change notification settings - Fork 0
/
subsidy_test.go
142 lines (124 loc) · 4.08 KB
/
subsidy_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package picfightcoin
import (
"fmt"
"github.com/jfixby/coin"
"github.com/jfixby/pin"
"testing"
)
func TestPremine(t *testing.T) {
block1_subsidy := PicFightCoinSubsidy().CalcBlockSubsidy(1)
// Block height 1 subsidy is 'special' and used to
// distribute initial tokens, if any.
block1_spremined := calcPremineTotal().AtomsValue
if block1_subsidy != block1_spremined {
t.Errorf("Premine mismatch: got %v expected %v ",
block1_subsidy,
block1_spremined,
)
t.Fail()
}
}
var expectedPFCActual = coin.Amount{7777699369282}
func TestPicfightCoinSubsidy(t *testing.T) {
calc := PicFightCoinSubsidy()
//calc.SetEngine(bignum.BigDecimalEngine{})
expected := calc.ExpectedTotalNetworkSubsidy().AtomsValue
expected = expectedPFCActual.AtomsValue
fullSubsidyCheck(t, calc, expected)
}
func TestDecredSubsidy(t *testing.T) {
calc := DecredMainNetSubsidy()
expected := calc.ExpectedTotalNetworkSubsidy().AtomsValue
fullSubsidyCheck(t, calc.(SubsidyCalculator), expected)
}
func fullSubsidyCheck(t *testing.T, calc SubsidyCalculator, expected int64) {
cache := map[int64]int64{}
for i := int64(0); ; i++ {
blockIndex := i
//blockSubsidy := calc.CalcBlockSubsidy(blockIndex)
work := calc.CalcBlockWorkSubsidy(blockIndex,
calc.TicketsPerBlock())
stake := calc.CalcStakeVoteSubsidy(blockIndex) * int64(calc.TicketsPerBlock())
tax := calc.CalcBlockTaxSubsidy(blockIndex, calc.TicketsPerBlock())
if i%1000000 == 0 {
pin.D(fmt.Sprintf("block: %v/%v: %v", i, calc.NumberOfGeneratingBlocks(), work+stake+tax))
}
//if blockSubsidy != work+stake+tax && blockIndex > 1 {
// t.Errorf("Bad block[%v] subsidy; want %v, got %v\n"+
// "work: %v\n"+
// "stake: %v\n"+
// "tax: %v\n",
// blockIndex,
// blockSubsidy,
// work+stake+tax,
// work,
// stake,
// tax,
// )
// t.FailNow()
//}
if (work+stake+tax) == 0 && i > 1 {
break
}
cache[i] = (work + stake + tax)
}
totalSubsidy := coin.Amount{0}
for i := int64(0); i <= int64(len(cache)); i++ {
k := int64(len(cache)) - 1 - i
totalSubsidy.AtomsValue = totalSubsidy.AtomsValue + cache[k]
}
fmt.Println(fmt.Sprintf("total: %v", totalSubsidy.AtomsValue))
expectedTotal := coin.Amount{expected}
if totalSubsidy.AtomsValue != expectedTotal.AtomsValue {
t.Errorf("Bad total subsidy; want %v, got %v",
expectedTotal.AtomsValue,
totalSubsidy.AtomsValue,
)
t.Errorf("Bad total subsidy; want %v, got %v",
expectedTotal,
totalSubsidy,
)
}
}
// originalTestExpected is value from the original decred/dcrd repo
// most likely is invalid due to incorrect testing
const originalTestExpected int64 = 2099999999800912
func TestDecredSubsidyOriginal(t *testing.T) {
calc := DecredMainNetSubsidy().(*decredMainNetSubsidyCalculator)
expected := calc.ExpectedTotalNetworkSubsidy().AtomsValue
expected = originalTestExpected
originalDecredSubsidyCheck(t, calc, expected)
}
func originalDecredSubsidyCheck(t *testing.T, calc *decredMainNetSubsidyCalculator, expected int64) {
totalSubsidy := calc.BlockOneSubsidy()
for i := int64(0); ; i++ {
// Genesis block or first block.
if i == 0 || i == 1 {
continue
}
if i%calc.subsidyParams.SubsidyReductionInterval == 0 {
numBlocks := calc.subsidyParams.SubsidyReductionInterval
// First reduction internal, which is reduction interval - 2
// to skip the genesis block and block one.
if i == calc.subsidyParams.SubsidyReductionInterval {
numBlocks -= 2
}
height := i - numBlocks
work := calc.CalcBlockWorkSubsidy(height, calc.TicketsPerBlock())
stake := calc.CalcStakeVoteSubsidy(height) * int64(calc.TicketsPerBlock())
tax := calc.CalcBlockTaxSubsidy(height, calc.TicketsPerBlock())
if (work + stake + tax) == 0 {
break
}
totalSubsidy += ((work + stake + tax) * numBlocks)
// First reduction internal, subtract the stake subsidy for
// blocks before the staking system is enabled.
if i == calc.subsidyParams.SubsidyReductionInterval {
totalSubsidy -= stake * (calc.StakeValidationHeight() - 2)
}
}
}
if totalSubsidy != expected {
t.Errorf("Bad total subsidy; want %v, got %v", expected, totalSubsidy)
}
}