/
subsidy.go
140 lines (120 loc) · 4.59 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
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2015-2019 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-Next/dcrnd/blockchain/stake/version2/v8"
"github.com/Decred-Next/dcrnd/blockchain/standalone/v8"
"github.com/Decred-Next/dcrnd/chaincfg/v8"
dcrutil "github.com/Decred-Next/dcrnd/dcrutil/version2/v8"
"github.com/Decred-Next/dcrnd/wire/v8"
)
// 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 {
// Nothing to do when there is no ledger specified.
if len(params.BlockOneLedger) == 0 {
return nil
}
if tx.MsgTx().LockTime != 0 {
str := fmt.Sprintf("block 1 coinbase has invalid locktime")
return ruleError(ErrBlockOneTx, str)
}
if tx.MsgTx().Expiry != wire.NoExpiryValue {
str := fmt.Sprintf("block 1 coinbase has invalid expiry")
return ruleError(ErrBlockOneTx, str)
}
if tx.MsgTx().TxIn[0].Sequence != wire.MaxTxInSequenceNum {
str := fmt.Sprintf("block 1 coinbase not finalized")
return ruleError(ErrBlockOneInputs, str)
}
if len(tx.MsgTx().TxOut) == 0 {
str := fmt.Sprintf("coinbase outputs empty in block 1")
return ruleError(ErrBlockOneOutputs, str)
}
//ledger := params.BlockOneLedger
//if len(ledger) != len(tx.MsgTx().TxOut) {
// str := fmt.Sprintf("wrong number of outputs in block 1 coinbase; "+
// "got %v, expected %v", len(tx.MsgTx().TxOut), len(ledger))
// return ruleError(ErrBlockOneOutputs, str)
//}
// Check the addresses and output amounts against those in the ledger.
//const consensusScriptVersion = 0
//for i, txOut := range tx.MsgTx().TxOut {
// ledgerEntry := &ledger[i]
// if txOut.Version != ledgerEntry.ScriptVersion {
// str := fmt.Sprintf("block one output %d script version %d is not %d",
// i, txOut.Version, consensusScriptVersion)
// return ruleError(ErrBlockOneOutputs, str)
// }
//
// if !bytes.Equal(txOut.PkScript, ledgerEntry.Script) {
// str := fmt.Sprintf("block one output %d script %x is not %x", i,
// txOut.PkScript, ledgerEntry.Script)
// return ruleError(ErrBlockOneOutputs, str)
// }
//
// if txOut.Value != ledgerEntry.Amount {
// str := fmt.Sprintf("block one output %d generates %v instead of "+
// "required %v", i, dcrutil.Amount(txOut.Value),
// dcrutil.Amount(ledgerEntry.Amount))
// return ruleError(ErrBlockOneOutputs, str)
// }
//}
return nil
}
// coinbasePaysTreasury checks to see if a given block's coinbase correctly pays
// the treasury.
func coinbasePaysTreasury(subsidyCache *standalone.SubsidyCache, tx *dcrutil.Tx, height int64, voters uint16, params *chaincfg.Params) error {
// Treasury subsidy only applies from block 2 onwards.
if height <= 1 {
return nil
}
// Treasury subsidy is disabled.
if params.BlockTaxProportion == 0 {
return nil
}
if len(tx.MsgTx().TxOut) == 0 {
str := fmt.Sprintf("invalid coinbase (no outputs)")
return ruleError(ErrNoTxOutputs, str)
}
treasuryOutput := tx.MsgTx().TxOut[0]
if treasuryOutput.Version != params.OrganizationPkScriptVersion {
str := fmt.Sprintf("treasury output version %d is instead of %d",
treasuryOutput.Version, params.OrganizationPkScriptVersion)
return ruleError(ErrNoTax, str)
}
if !bytes.Equal(treasuryOutput.PkScript, params.OrganizationPkScript) {
str := fmt.Sprintf("treasury output script is %x instead of %x",
treasuryOutput.PkScript, params.OrganizationPkScript)
return ruleError(ErrNoTax, str)
}
// Calculate the amount of subsidy that should have been paid out to the
// Treasury and ensure the subsidy generated is correct.
orgSubsidy := subsidyCache.CalcTreasurySubsidy(height, voters)
if orgSubsidy != treasuryOutput.Value {
str := fmt.Sprintf("treasury output amount is %s instead of %s",
dcrutil.Amount(treasuryOutput.Value), dcrutil.Amount(orgSubsidy))
return ruleError(ErrNoTax, str)
}
return nil
}
// calculateAddedSubsidy calculates the amount of subsidy added by a block
// and its parent. The blocks passed to this function MUST be valid blocks
// that have already been confirmed to abide by the consensus rules of the
// network, or the function might panic.
func calculateAddedSubsidy(block, parent *dcrutil.Block) int64 {
var subsidy int64
if headerApprovesParent(&block.MsgBlock().Header) {
subsidy += parent.MsgBlock().Transactions[0].TxIn[0].ValueIn
}
for _, stx := range block.MsgBlock().STransactions {
if stake.IsSSGen(stx) {
subsidy += stx.TxIn[0].ValueIn
}
}
return subsidy
}