/
subsidy.go
63 lines (55 loc) · 2.12 KB
/
subsidy.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
// Copyright (c) 2015-2018 The Decred developers
// Copyright (c) 2013-2015 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package txhelpers
import (
"github.com/decred/dcrd/blockchain"
"github.com/decred/dcrd/chaincfg"
)
// UltimateSubsidy computes the total subsidy over the entire subsidy
// distribution period of the network.
func UltimateSubsidy(params *chaincfg.Params) int64 {
subsidyCache := blockchain.NewSubsidyCache(0, params)
totalSubsidy := params.BlockOneSubsidy()
for i := int64(0); ; i++ {
// Genesis block or first block.
if i <= 1 {
continue
}
if i%params.SubsidyReductionInterval == 0 {
numBlocks := params.SubsidyReductionInterval
// First reduction internal, which is reduction interval - 2 to skip
// the genesis block and block one.
if i == params.SubsidyReductionInterval {
numBlocks -= 2
}
height := i - numBlocks
work := blockchain.CalcBlockWorkSubsidy(subsidyCache, height,
params.TicketsPerBlock, params)
stake := blockchain.CalcStakeVoteSubsidy(subsidyCache, height,
params) * int64(params.TicketsPerBlock)
tax := blockchain.CalcBlockTaxSubsidy(subsidyCache, height,
params.TicketsPerBlock, params)
if (work + stake + tax) == 0 {
break // all done
}
totalSubsidy += ((work + stake + tax) * numBlocks)
// First reduction internal -- subtract the stake subsidy for blocks
// before the staking system is enabled.
if i == params.SubsidyReductionInterval {
totalSubsidy -= stake * (params.StakeValidationHeight - 2)
}
}
}
return totalSubsidy
}
// RewardsAtBlock computes the PoW, PoS (per vote), and project fund subsidies
// at for the specified block index, assuming a certain number of votes.
func RewardsAtBlock(blockIdx int64, votes uint16, p *chaincfg.Params) (work, stake, tax int64) {
subsidyCache := blockchain.NewSubsidyCache(0, p)
work = blockchain.CalcBlockWorkSubsidy(subsidyCache, blockIdx, votes, p)
stake = blockchain.CalcStakeVoteSubsidy(subsidyCache, blockIdx, p)
tax = blockchain.CalcBlockTaxSubsidy(subsidyCache, blockIdx, votes, p)
return
}