6 "github.com/bytom/vapor/consensus"
7 "github.com/bytom/vapor/protocol/bc"
8 "github.com/bytom/vapor/protocol/bc/types"
9 "github.com/bytom/vapor/protocol/state"
10 "github.com/bytom/vapor/testutil"
13 func TestCalCoinbaseTxReward(t *testing.T) {
14 consensus.ActiveNetParams.ProducerSubsidys = []consensus.ProducerSubsidy{
15 {BeginBlock: 0, EndBlock: 0, Subsidy: 24},
16 {BeginBlock: 1, EndBlock: 840000, Subsidy: 24},
17 {BeginBlock: 840001, EndBlock: 1680000, Subsidy: 12},
18 {BeginBlock: 1680001, EndBlock: 3360000, Subsidy: 6},
20 reductionInterval := uint64(840000)
25 wantReward state.CoinbaseReward
28 desc: "the block height is reductionInterval - 1",
30 BlockHeader: types.BlockHeader{
31 Height: reductionInterval - 1,
33 Transactions: []*types.Tx{
36 Inputs: []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
37 Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 0, []byte{0x51})},
42 wantReward: state.CoinbaseReward{
44 ControlProgram: []byte{0x51},
48 desc: "the block height is reductionInterval",
50 BlockHeader: types.BlockHeader{
51 Height: reductionInterval,
53 Transactions: []*types.Tx{
56 Inputs: []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
57 Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 0, []byte{0x51})},
62 wantReward: state.CoinbaseReward{
64 ControlProgram: []byte{0x51},
68 desc: "the block height is reductionInterval + 1",
70 BlockHeader: types.BlockHeader{
71 Height: reductionInterval + 1,
73 Transactions: []*types.Tx{
76 Inputs: []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
77 Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 0, []byte{0x51})},
82 wantReward: state.CoinbaseReward{
84 ControlProgram: []byte{0x51},
88 desc: "the block height is reductionInterval * 2",
90 BlockHeader: types.BlockHeader{
91 Height: reductionInterval * 2,
93 Transactions: []*types.Tx{
96 Inputs: []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
97 Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 0, []byte{0x51})},
102 wantReward: state.CoinbaseReward{
104 ControlProgram: []byte{0x51},
108 desc: "the block height is 2*reductionInterval + 1",
110 BlockHeader: types.BlockHeader{
111 Height: 2*reductionInterval + 1,
113 Transactions: []*types.Tx{
115 TxData: types.TxData{
116 Inputs: []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
117 Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 0, []byte{0x51})},
122 wantReward: state.CoinbaseReward{
124 ControlProgram: []byte{0x51},
129 for i, c := range cases {
130 gotReward, err := state.CalCoinbaseReward(c.block)
135 if !testutil.DeepEqual(*gotReward, c.wantReward) {
136 t.Fatalf("test case %d: %s, the coinbase reward got: %v, want: %v", i, c.desc, *gotReward, c.wantReward)
141 func TestCountCoinbaseTxRewards(t *testing.T) {
142 consensus.ActiveNetParams.ProducerSubsidys = []consensus.ProducerSubsidy{
143 {BeginBlock: 0, EndBlock: 0, Subsidy: 24},
144 {BeginBlock: 1, EndBlock: 840000, Subsidy: 24},
145 {BeginBlock: 840001, EndBlock: 1680000, Subsidy: 12},
146 {BeginBlock: 1680001, EndBlock: 3360000, Subsidy: 6},
152 consensusResult *state.ConsensusResult
153 wantRewards []state.CoinbaseReward
154 wantConsensusResult *state.ConsensusResult
157 desc: "the block height is RoundVoteBlockNums - 1",
159 BlockHeader: types.BlockHeader{
160 Height: consensus.ActiveNetParams.RoundVoteBlockNums - 1,
162 Transactions: []*types.Tx{nil},
164 consensusResult: &state.ConsensusResult{
165 CoinbaseReward: map[string]uint64{
169 wantRewards: []state.CoinbaseReward{},
170 wantConsensusResult: &state.ConsensusResult{
171 CoinbaseReward: map[string]uint64{
177 desc: "the block height is RoundVoteBlockNums",
179 BlockHeader: types.BlockHeader{
180 Height: consensus.ActiveNetParams.RoundVoteBlockNums,
182 Transactions: []*types.Tx{nil},
184 consensusResult: &state.ConsensusResult{
185 CoinbaseReward: map[string]uint64{
190 wantRewards: []state.CoinbaseReward{},
191 wantConsensusResult: &state.ConsensusResult{
192 CoinbaseReward: map[string]uint64{
199 desc: "the block height is RoundVoteBlockNums + 1",
201 BlockHeader: types.BlockHeader{
202 Height: consensus.ActiveNetParams.RoundVoteBlockNums + 1,
204 Transactions: []*types.Tx{nil},
206 consensusResult: &state.ConsensusResult{
207 CoinbaseReward: map[string]uint64{
211 wantRewards: []state.CoinbaseReward{
212 state.CoinbaseReward{
214 ControlProgram: []byte{0x51},
217 wantConsensusResult: &state.ConsensusResult{
218 CoinbaseReward: map[string]uint64{
224 desc: "the block height is RoundVoteBlockNums * 2",
226 BlockHeader: types.BlockHeader{
227 Height: consensus.ActiveNetParams.RoundVoteBlockNums * 2,
229 Transactions: []*types.Tx{nil},
231 consensusResult: &state.ConsensusResult{
232 CoinbaseReward: map[string]uint64{
236 wantRewards: []state.CoinbaseReward{},
237 wantConsensusResult: &state.ConsensusResult{
238 CoinbaseReward: map[string]uint64{
244 desc: "the block height is 2*RoundVoteBlockNums + 1",
246 BlockHeader: types.BlockHeader{
247 Height: 2*consensus.ActiveNetParams.RoundVoteBlockNums + 1,
249 Transactions: []*types.Tx{nil},
251 consensusResult: &state.ConsensusResult{
252 CoinbaseReward: map[string]uint64{
257 wantRewards: []state.CoinbaseReward{
258 state.CoinbaseReward{
260 ControlProgram: []byte{0x52},
262 state.CoinbaseReward{
264 ControlProgram: []byte{0x51},
267 wantConsensusResult: &state.ConsensusResult{
268 CoinbaseReward: map[string]uint64{
275 for i, c := range cases {
276 rewards, err := c.consensusResult.GetCoinbaseRewards(c.block.Height - 1)
281 if !testutil.DeepEqual(rewards, c.wantRewards) {
282 t.Fatalf("test case %d: %s, the coinbase reward got: %v, want: %v", i, c.desc, rewards, c.wantRewards)
285 // create coinbase transaction
286 c.block.Transactions[0], err = createCoinbaseTxByReward(nil, c.block.Height, rewards)
291 if err := c.consensusResult.AttachCoinbaseReward(c.block); err != nil {
295 if !testutil.DeepEqual(c.consensusResult, c.wantConsensusResult) {
296 t.Fatalf("test case %d: %s, the consensusResult got: %v, want: %v", i, c.desc, c.consensusResult, c.wantConsensusResult)