forked from decred/dcrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subsidy.go
277 lines (236 loc) · 8.73 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2015 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package blockchain
import (
"bytes"
"fmt"
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/txscript"
"github.com/decred/dcrd/wire"
"github.com/decred/dcrutil"
)
// CalcBlockSubsidy returns the subsidy amount a block at the provided height
// should have. This is mainly used for determining how much the coinbase for
// newly generated blocks awards as well as validating the coinbase for blocks
// has the expected value.
//
// Subsidy calculation for exponential reductions:
// 0 for i in range (0, height / ReductionInterval):
// 1 subsidy *= MulSubsidy
// 2 subsidy /= DivSubsidy
//
// Safe for concurrent access.
func calcBlockSubsidy(height int64, params *chaincfg.Params) int64 {
// Block height 1 subsidy is 'special' and used to
// distribute initial tokens, if any.
if height == 1 {
return params.BlockOneSubsidy()
}
iterations := height / params.ReductionInterval
subsidy := params.BaseSubsidy
// You could stick all these values in a LUT for faster access if you
// wanted to, but this calculation is already really fast until you
// get very very far into the blockchain. The other method you could
// use is storing the total subsidy in a block node and do the
// multiplication and division when needed when adding a block.
if iterations > 0 {
for i := int64(0); i < iterations; i++ {
subsidy *= params.MulSubsidy
subsidy /= params.DivSubsidy
}
}
return subsidy
}
// CalcBlockWorkSubsidy calculates the proof of work subsidy for a block as a
// proportion of the total subsidy.
func CalcBlockWorkSubsidy(height int64, voters uint16,
params *chaincfg.Params) int64 {
subsidy := calcBlockSubsidy(height, params)
proportionWork := int64(params.WorkRewardProportion)
proportions := int64(params.TotalSubsidyProportions())
subsidy *= proportionWork
subsidy /= proportions
// Ignore the voters field of the header before we're at a point
// where there are any voters.
if height < params.StakeValidationHeight {
return subsidy
}
// If there are no voters, subsidy is 0. The block will fail later anyway.
if voters == 0 {
return 0
}
// Adjust for the number of voters. This shouldn't ever overflow if you start
// with 50 * 10^8 Atoms and voters and potentialVoters are uint16.
potentialVoters := params.TicketsPerBlock
actual := (int64(voters) * subsidy) / int64(potentialVoters)
return actual
}
// CalcStakeVoteSubsidy calculates the subsidy for a stake vote based on the height
// of its input SStx.
//
// Safe for concurrent access.
func CalcStakeVoteSubsidy(height int64, params *chaincfg.Params) int64 {
// Calculate the actual reward for this block, then further reduce reward
// proportional to StakeRewardProportion.
// Note that voters/potential voters is 1, so that vote reward is calculated
// irrespective of block reward.
subsidy := calcBlockSubsidy(height, params)
proportionStake := int64(params.StakeRewardProportion)
proportions := int64(params.TotalSubsidyProportions())
subsidy *= proportionStake
subsidy /= (proportions * int64(params.TicketsPerBlock))
return subsidy
}
// CalcBlockTaxSubsidy calculates the subsidy for the organization address in the
// coinbase.
//
// Safe for concurrent access.
func CalcBlockTaxSubsidy(height int64, voters uint16,
params *chaincfg.Params) int64 {
if params.BlockTaxProportion == 0 {
return 0
}
subsidy := calcBlockSubsidy(int64(height), params)
proportionTax := int64(params.BlockTaxProportion)
proportions := int64(params.TotalSubsidyProportions())
subsidy *= proportionTax
subsidy /= proportions
// Assume all voters 'present' before stake voting is turned on.
if height < params.StakeValidationHeight {
voters = 5
}
// If there are no voters, subsidy is 0. The block will fail later anyway.
if voters == 0 && height >= params.StakeValidationHeight {
return 0
}
// Adjust for the number of voters. This shouldn't ever overflow if you start
// with 50 * 10^8 Atoms and voters and potentialVoters are uint16.
potentialVoters := params.TicketsPerBlock
adjusted := (int64(voters) * subsidy) / int64(potentialVoters)
return adjusted
}
// BlockOneCoinbasePaysTokens checks to see if the first block coinbase pays
// out to the network initial token ledger.
func BlockOneCoinbasePaysTokens(tx *dcrutil.Tx, params *chaincfg.Params) error {
// If no ledger is specified, just return true.
if len(params.BlockOneLedger) == 0 {
return nil
}
if tx.MsgTx().LockTime != 0 {
errStr := fmt.Sprintf("block 1 coinbase has invalid locktime")
return ruleError(ErrBlockOneTx, errStr)
}
if tx.MsgTx().Expiry != wire.NoExpiryValue {
errStr := fmt.Sprintf("block 1 coinbase has invalid expiry")
return ruleError(ErrBlockOneTx, errStr)
}
if tx.MsgTx().TxIn[0].Sequence != wire.MaxTxInSequenceNum {
errStr := fmt.Sprintf("block 1 coinbase not finalized")
return ruleError(ErrBlockOneInputs, errStr)
}
if len(tx.MsgTx().TxOut) == 0 {
errStr := fmt.Sprintf("coinbase outputs empty in block 1")
return ruleError(ErrBlockOneOutputs, errStr)
}
ledger := params.BlockOneLedger
if len(ledger) != len(tx.MsgTx().TxOut) {
errStr := fmt.Sprintf("wrong number of outputs in block 1 coinbase; "+
"got %v, expected %v", len(tx.MsgTx().TxOut), len(ledger))
return ruleError(ErrBlockOneOutputs, errStr)
}
// Check the addresses and output amounts against those in the ledger.
for i, txout := range tx.MsgTx().TxOut {
if txout.Version != txscript.DefaultScriptVersion {
errStr := fmt.Sprintf("bad block one output version; want %v, got %v",
txscript.DefaultScriptVersion, txout.Version)
return ruleError(ErrBlockOneOutputs, errStr)
}
// There should only be one address.
_, addrs, _, err :=
txscript.ExtractPkScriptAddrs(txout.Version, txout.PkScript, params)
if len(addrs) != 1 {
errStr := fmt.Sprintf("too many addresses in output")
return ruleError(ErrBlockOneOutputs, errStr)
}
addrLedger, err := dcrutil.DecodeAddress(ledger[i].Address, params)
if err != nil {
return err
}
if !bytes.Equal(addrs[0].ScriptAddress(), addrLedger.ScriptAddress()) {
errStr := fmt.Sprintf("address in output %v has non matching "+
"address; got %v (hash160 %x), want %v (hash160 %x)",
i,
addrs[0].EncodeAddress(),
addrs[0].ScriptAddress(),
addrLedger.EncodeAddress(),
addrLedger.ScriptAddress())
return ruleError(ErrBlockOneOutputs, errStr)
}
if txout.Value != ledger[i].Amount {
errStr := fmt.Sprintf("address in output %v has non matching "+
"amount; got %v, want %v", i, txout.Value, ledger[i].Amount)
return ruleError(ErrBlockOneOutputs, errStr)
}
}
return nil
}
// CoinbasePaysTax checks to see if a given block's coinbase correctly pays
// tax to the developer organization.
func CoinbasePaysTax(tx *dcrutil.Tx, height uint32, voters uint16,
params *chaincfg.Params) error {
// Taxes only apply from block 2 onwards.
if height <= 1 {
return nil
}
// Tax is disabled.
if params.BlockTaxProportion == 0 {
return nil
}
if len(tx.MsgTx().TxOut) == 0 {
errStr := fmt.Sprintf("invalid coinbase (no outputs)")
return ruleError(ErrNoTxOutputs, errStr)
}
// Coinbase output 0 must be the subsidy to the dev organization.
taxPkVersion := tx.MsgTx().TxOut[0].Version
taxPkScript := tx.MsgTx().TxOut[0].PkScript
class, addrs, _, err :=
txscript.ExtractPkScriptAddrs(taxPkVersion, taxPkScript, params)
// The script can't be a weird class.
if !(class == txscript.ScriptHashTy ||
class == txscript.PubKeyHashTy ||
class == txscript.PubKeyTy) {
errStr := fmt.Sprintf("wrong script class for tax output")
return ruleError(ErrNoTax, errStr)
}
// There should only be one address.
if len(addrs) != 1 {
errStr := fmt.Sprintf("no or too many addresses in output")
return ruleError(ErrNoTax, errStr)
}
// Decode the organization address.
addrOrg, err := dcrutil.DecodeAddress(params.OrganizationAddress, params)
if err != nil {
return err
}
if !bytes.Equal(addrs[0].ScriptAddress(), addrOrg.ScriptAddress()) {
errStr := fmt.Sprintf("address in output 0 has non matching org "+
"address; got %v (hash160 %x), want %v (hash160 %x)",
addrs[0].EncodeAddress(),
addrs[0].ScriptAddress(),
addrOrg.EncodeAddress(),
addrOrg.ScriptAddress())
return ruleError(ErrNoTax, errStr)
}
// Get the amount of subsidy that should have been paid out to
// the organization, then check it.
orgSubsidy := CalcBlockTaxSubsidy(int64(height), voters, params)
amountFound := tx.MsgTx().TxOut[0].Value
if orgSubsidy != amountFound {
errStr := fmt.Sprintf("amount in output 0 has non matching org "+
"calculated amount; got %v, want %v", amountFound, orgSubsidy)
return ruleError(ErrNoTax, errStr)
}
return nil
}