OSDN Git Service

add log (#373)
[bytom/vapor.git] / proposal / proposal_test.go
1 package proposal
2
3 import (
4         "testing"
5
6         "github.com/vapor/consensus"
7         "github.com/vapor/protocol/bc"
8         "github.com/vapor/protocol/bc/types"
9         "github.com/vapor/protocol/state"
10         "github.com/vapor/testutil"
11 )
12
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},
19         }
20         reductionInterval := uint64(840000)
21
22         cases := []struct {
23                 desc       string
24                 block      *types.Block
25                 wantReward state.CoinbaseReward
26         }{
27                 {
28                         desc: "the block height is reductionInterval - 1",
29                         block: &types.Block{
30                                 BlockHeader: types.BlockHeader{
31                                         Height: reductionInterval - 1,
32                                 },
33                                 Transactions: []*types.Tx{
34                                         &types.Tx{
35                                                 TxData: types.TxData{
36                                                         Inputs:  []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
37                                                         Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 0, []byte{0x51})},
38                                                 },
39                                         },
40                                 },
41                         },
42                         wantReward: state.CoinbaseReward{
43                                 Amount:         24,
44                                 ControlProgram: []byte{0x51},
45                         },
46                 },
47                 {
48                         desc: "the block height is reductionInterval",
49                         block: &types.Block{
50                                 BlockHeader: types.BlockHeader{
51                                         Height: reductionInterval,
52                                 },
53                                 Transactions: []*types.Tx{
54                                         &types.Tx{
55                                                 TxData: types.TxData{
56                                                         Inputs:  []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
57                                                         Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 0, []byte{0x51})},
58                                                 },
59                                         },
60                                 },
61                         },
62                         wantReward: state.CoinbaseReward{
63                                 Amount:         24,
64                                 ControlProgram: []byte{0x51},
65                         },
66                 },
67                 {
68                         desc: "the block height is reductionInterval + 1",
69                         block: &types.Block{
70                                 BlockHeader: types.BlockHeader{
71                                         Height: reductionInterval + 1,
72                                 },
73                                 Transactions: []*types.Tx{
74                                         &types.Tx{
75                                                 TxData: types.TxData{
76                                                         Inputs:  []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
77                                                         Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 0, []byte{0x51})},
78                                                 },
79                                         },
80                                 },
81                         },
82                         wantReward: state.CoinbaseReward{
83                                 Amount:         12,
84                                 ControlProgram: []byte{0x51},
85                         },
86                 },
87                 {
88                         desc: "the block height is reductionInterval * 2",
89                         block: &types.Block{
90                                 BlockHeader: types.BlockHeader{
91                                         Height: reductionInterval * 2,
92                                 },
93                                 Transactions: []*types.Tx{
94                                         &types.Tx{
95                                                 TxData: types.TxData{
96                                                         Inputs:  []*types.TxInput{types.NewCoinbaseInput([]byte{0x01})},
97                                                         Outputs: []*types.TxOutput{types.NewIntraChainOutput(bc.AssetID{}, 0, []byte{0x51})},
98                                                 },
99                                         },
100                                 },
101                         },
102                         wantReward: state.CoinbaseReward{
103                                 Amount:         12,
104                                 ControlProgram: []byte{0x51},
105                         },
106                 },
107                 {
108                         desc: "the block height is 2*reductionInterval + 1",
109                         block: &types.Block{
110                                 BlockHeader: types.BlockHeader{
111                                         Height: 2*reductionInterval + 1,
112                                 },
113                                 Transactions: []*types.Tx{
114                                         &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})},
118                                                 },
119                                         },
120                                 },
121                         },
122                         wantReward: state.CoinbaseReward{
123                                 Amount:         6,
124                                 ControlProgram: []byte{0x51},
125                         },
126                 },
127         }
128
129         for i, c := range cases {
130                 gotReward, err := state.CalCoinbaseReward(c.block)
131                 if err != nil {
132                         t.Fatal(err)
133                 }
134
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)
137                 }
138         }
139 }
140
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},
147         }
148
149         cases := []struct {
150                 desc                string
151                 block               *types.Block
152                 consensusResult     *state.ConsensusResult
153                 wantRewards         []state.CoinbaseReward
154                 wantConsensusResult *state.ConsensusResult
155         }{
156                 {
157                         desc: "the block height is RoundVoteBlockNums - 1",
158                         block: &types.Block{
159                                 BlockHeader: types.BlockHeader{
160                                         Height: consensus.ActiveNetParams.RoundVoteBlockNums - 1,
161                                 },
162                                 Transactions: []*types.Tx{nil},
163                         },
164                         consensusResult: &state.ConsensusResult{
165                                 CoinbaseReward: map[string]uint64{
166                                         "51": 10,
167                                 },
168                         },
169                         wantRewards: []state.CoinbaseReward{},
170                         wantConsensusResult: &state.ConsensusResult{
171                                 CoinbaseReward: map[string]uint64{
172                                         "51": 34,
173                                 },
174                         },
175                 },
176                 {
177                         desc: "the block height is RoundVoteBlockNums",
178                         block: &types.Block{
179                                 BlockHeader: types.BlockHeader{
180                                         Height: consensus.ActiveNetParams.RoundVoteBlockNums,
181                                 },
182                                 Transactions: []*types.Tx{nil},
183                         },
184                         consensusResult: &state.ConsensusResult{
185                                 CoinbaseReward: map[string]uint64{
186                                         "51": 10,
187                                         "52": 20,
188                                 },
189                         },
190                         wantRewards: []state.CoinbaseReward{},
191                         wantConsensusResult: &state.ConsensusResult{
192                                 CoinbaseReward: map[string]uint64{
193                                         "51": 34,
194                                         "52": 20,
195                                 },
196                         },
197                 },
198                 {
199                         desc: "the block height is RoundVoteBlockNums + 1",
200                         block: &types.Block{
201                                 BlockHeader: types.BlockHeader{
202                                         Height: consensus.ActiveNetParams.RoundVoteBlockNums + 1,
203                                 },
204                                 Transactions: []*types.Tx{nil},
205                         },
206                         consensusResult: &state.ConsensusResult{
207                                 CoinbaseReward: map[string]uint64{
208                                         "51": 10,
209                                 },
210                         },
211                         wantRewards: []state.CoinbaseReward{
212                                 state.CoinbaseReward{
213                                         Amount:         10,
214                                         ControlProgram: []byte{0x51},
215                                 },
216                         },
217                         wantConsensusResult: &state.ConsensusResult{
218                                 CoinbaseReward: map[string]uint64{
219                                         "51": 24,
220                                 },
221                         },
222                 },
223                 {
224                         desc: "the block height is RoundVoteBlockNums * 2",
225                         block: &types.Block{
226                                 BlockHeader: types.BlockHeader{
227                                         Height: consensus.ActiveNetParams.RoundVoteBlockNums * 2,
228                                 },
229                                 Transactions: []*types.Tx{nil},
230                         },
231                         consensusResult: &state.ConsensusResult{
232                                 CoinbaseReward: map[string]uint64{
233                                         "51": 10,
234                                 },
235                         },
236                         wantRewards: []state.CoinbaseReward{},
237                         wantConsensusResult: &state.ConsensusResult{
238                                 CoinbaseReward: map[string]uint64{
239                                         "51": 34,
240                                 },
241                         },
242                 },
243                 {
244                         desc: "the block height is 2*RoundVoteBlockNums + 1",
245                         block: &types.Block{
246                                 BlockHeader: types.BlockHeader{
247                                         Height: 2*consensus.ActiveNetParams.RoundVoteBlockNums + 1,
248                                 },
249                                 Transactions: []*types.Tx{nil},
250                         },
251                         consensusResult: &state.ConsensusResult{
252                                 CoinbaseReward: map[string]uint64{
253                                         "51": 10,
254                                         "52": 20,
255                                 },
256                         },
257                         wantRewards: []state.CoinbaseReward{
258                                 state.CoinbaseReward{
259                                         Amount:         20,
260                                         ControlProgram: []byte{0x52},
261                                 },
262                                 state.CoinbaseReward{
263                                         Amount:         10,
264                                         ControlProgram: []byte{0x51},
265                                 },
266                         },
267                         wantConsensusResult: &state.ConsensusResult{
268                                 CoinbaseReward: map[string]uint64{
269                                         "51": 24,
270                                 },
271                         },
272                 },
273         }
274
275         for i, c := range cases {
276                 rewards, err := c.consensusResult.GetCoinbaseRewards(c.block.Height - 1)
277                 if err != nil {
278                         t.Fatal(err)
279                 }
280
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)
283                 }
284
285                 // create coinbase transaction
286                 c.block.Transactions[0], err = createCoinbaseTx(nil, c.block.Height, rewards)
287                 if err != nil {
288                         t.Fatal(err)
289                 }
290
291                 if err := c.consensusResult.AttachCoinbaseReward(c.block); err != nil {
292                         t.Fatal(err)
293                 }
294
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)
297                 }
298         }
299 }