OSDN Git Service

d8f7abf4f02d12c14c2fc99bb983f5eca667674f
[bytom/vapor.git] / proposal / proposal_test.go
1 package proposal
2
3 import (
4         "encoding/hex"
5         "testing"
6
7         "github.com/vapor/consensus"
8         "github.com/vapor/protocol/bc/types"
9         "github.com/vapor/protocol/state"
10         "github.com/vapor/protocol/vm/vmutil"
11         "github.com/vapor/testutil"
12 )
13
14 func TestCalCoinbaseTxReward(t *testing.T) {
15         consensus.ActiveNetParams.ProducerSubsidys = []consensus.ProducerSubsidy{
16                 {BeginBlock: 0, EndBlock: 0, Subsidy: 24},
17                 {BeginBlock: 1, EndBlock: 840000, Subsidy: 24},
18                 {BeginBlock: 840001, EndBlock: 1680000, Subsidy: 12},
19                 {BeginBlock: 1680001, EndBlock: 3360000, Subsidy: 6},
20         }
21         reductionInterval := uint64(840000)
22
23         cases := []struct {
24                 desc            string
25                 block           *types.Block
26                 consensusResult *state.ConsensusResult
27                 wantReward      state.CoinbaseReward
28         }{
29                 {
30                         desc: "the block height is reductionInterval - 1",
31                         block: &types.Block{
32                                 BlockHeader: types.BlockHeader{
33                                         Height: reductionInterval - 1,
34                                 },
35                                 Transactions: []*types.Tx{nil},
36                         },
37                         consensusResult: &state.ConsensusResult{
38                                 CoinbaseReward: map[string]uint64{},
39                         },
40                         wantReward: state.CoinbaseReward{
41                                 Amount:         24,
42                                 ControlProgram: []byte{0x51},
43                         },
44                 },
45                 {
46                         desc: "the block height is reductionInterval",
47                         block: &types.Block{
48                                 BlockHeader: types.BlockHeader{
49                                         Height: reductionInterval,
50                                 },
51                                 Transactions: []*types.Tx{nil},
52                         },
53                         consensusResult: &state.ConsensusResult{
54                                 CoinbaseReward: map[string]uint64{},
55                         },
56                         wantReward: state.CoinbaseReward{
57                                 Amount:         24,
58                                 ControlProgram: []byte{0x51},
59                         },
60                 },
61                 {
62                         desc: "the block height is reductionInterval + 1",
63                         block: &types.Block{
64                                 BlockHeader: types.BlockHeader{
65                                         Height: reductionInterval + 1,
66                                 },
67                                 Transactions: []*types.Tx{nil},
68                         },
69                         consensusResult: &state.ConsensusResult{
70                                 CoinbaseReward: map[string]uint64{},
71                         },
72                         wantReward: state.CoinbaseReward{
73                                 Amount:         12,
74                                 ControlProgram: []byte{0x51},
75                         },
76                 },
77                 {
78                         desc: "the block height is reductionInterval * 2",
79                         block: &types.Block{
80                                 BlockHeader: types.BlockHeader{
81                                         Height: reductionInterval * 2,
82                                 },
83                                 Transactions: []*types.Tx{nil},
84                         },
85                         consensusResult: &state.ConsensusResult{
86                                 CoinbaseReward: map[string]uint64{},
87                         },
88                         wantReward: state.CoinbaseReward{
89                                 Amount:         12,
90                                 ControlProgram: []byte{0x51},
91                         },
92                 },
93                 {
94                         desc: "the block height is 2*reductionInterval + 1",
95                         block: &types.Block{
96                                 BlockHeader: types.BlockHeader{
97                                         Height: 2*reductionInterval + 1,
98                                 },
99                                 Transactions: []*types.Tx{nil},
100                         },
101                         consensusResult: &state.ConsensusResult{
102                                 CoinbaseReward: map[string]uint64{},
103                         },
104                         wantReward: state.CoinbaseReward{
105                                 Amount:         6,
106                                 ControlProgram: []byte{0x51},
107                         },
108                 },
109         }
110
111         var err error
112         for i, c := range cases {
113                 c.block.Transactions[0], err = createCoinbaseTx(nil, c.block.Height)
114                 if err != nil {
115                         t.Fatal(err)
116                 }
117
118                 if err := c.consensusResult.AttachCoinbaseReward(c.block); err != nil {
119                         t.Fatal(err)
120                 }
121
122                 defaultProgram, _ := vmutil.DefaultCoinbaseProgram()
123                 gotReward := state.CoinbaseReward{
124                         Amount:         c.consensusResult.CoinbaseReward[hex.EncodeToString(defaultProgram)],
125                         ControlProgram: defaultProgram,
126                 }
127
128                 if !testutil.DeepEqual(gotReward, c.wantReward) {
129                         t.Fatalf("test case %d: %s, the coinbase reward got: %v, want: %v", i, c.desc, gotReward, c.wantReward)
130                 }
131         }
132 }
133
134 func TestCountCoinbaseTxRewards(t *testing.T) {
135         consensus.ActiveNetParams.ProducerSubsidys = []consensus.ProducerSubsidy{
136                 {BeginBlock: 0, EndBlock: 0, Subsidy: 24},
137                 {BeginBlock: 1, EndBlock: 840000, Subsidy: 24},
138                 {BeginBlock: 840001, EndBlock: 1680000, Subsidy: 12},
139                 {BeginBlock: 1680001, EndBlock: 3360000, Subsidy: 6},
140         }
141
142         cases := []struct {
143                 desc            string
144                 block           *types.Block
145                 consensusResult *state.ConsensusResult
146                 wantRewards     []state.CoinbaseReward
147         }{
148                 {
149                         desc: "the block height is RoundVoteBlockNums - 1",
150                         block: &types.Block{
151                                 BlockHeader: types.BlockHeader{
152                                         Height: consensus.ActiveNetParams.RoundVoteBlockNums - 1,
153                                 },
154                                 Transactions: []*types.Tx{nil},
155                         },
156                         consensusResult: &state.ConsensusResult{
157                                 CoinbaseReward: map[string]uint64{
158                                         "51": 10,
159                                 },
160                         },
161                         wantRewards: []state.CoinbaseReward{},
162                 },
163                 {
164                         desc: "the block height is RoundVoteBlockNums",
165                         block: &types.Block{
166                                 BlockHeader: types.BlockHeader{
167                                         Height: consensus.ActiveNetParams.RoundVoteBlockNums,
168                                 },
169                                 Transactions: []*types.Tx{nil},
170                         },
171                         consensusResult: &state.ConsensusResult{
172                                 CoinbaseReward: map[string]uint64{
173                                         "51": 10,
174                                         "52": 20,
175                                 },
176                         },
177                         wantRewards: []state.CoinbaseReward{
178                                 state.CoinbaseReward{
179                                         Amount:         34,
180                                         ControlProgram: []byte{0x51},
181                                 },
182                                 state.CoinbaseReward{
183                                         Amount:         20,
184                                         ControlProgram: []byte{0x52},
185                                 },
186                         },
187                 },
188                 {
189                         desc: "the block height is RoundVoteBlockNums + 1",
190                         block: &types.Block{
191                                 BlockHeader: types.BlockHeader{
192                                         Height: consensus.ActiveNetParams.RoundVoteBlockNums + 1,
193                                 },
194                                 Transactions: []*types.Tx{nil},
195                         },
196                         consensusResult: &state.ConsensusResult{
197                                 CoinbaseReward: map[string]uint64{
198                                         "51": 10,
199                                 },
200                         },
201                         wantRewards: []state.CoinbaseReward{},
202                 },
203                 {
204                         desc: "the block height is RoundVoteBlockNums * 2",
205                         block: &types.Block{
206                                 BlockHeader: types.BlockHeader{
207                                         Height: consensus.ActiveNetParams.RoundVoteBlockNums * 2,
208                                 },
209                                 Transactions: []*types.Tx{nil},
210                         },
211                         consensusResult: &state.ConsensusResult{
212                                 CoinbaseReward: map[string]uint64{
213                                         "50": 20,
214                                         "51": 10,
215                                         "52": 20,
216                                         "53": 30,
217                                 },
218                         },
219                         wantRewards: []state.CoinbaseReward{
220                                 state.CoinbaseReward{
221                                         Amount:         34,
222                                         ControlProgram: []byte{0x51},
223                                 },
224                                 state.CoinbaseReward{
225                                         Amount:         30,
226                                         ControlProgram: []byte{0x53},
227                                 },
228                                 state.CoinbaseReward{
229                                         Amount:         20,
230                                         ControlProgram: []byte{0x52},
231                                 },
232                                 state.CoinbaseReward{
233                                         Amount:         20,
234                                         ControlProgram: []byte{0x50},
235                                 },
236                         },
237                 },
238                 {
239                         desc: "the block height is 2*RoundVoteBlockNums + 1",
240                         block: &types.Block{
241                                 BlockHeader: types.BlockHeader{
242                                         Height: 2*consensus.ActiveNetParams.RoundVoteBlockNums + 1,
243                                 },
244                                 Transactions: []*types.Tx{nil},
245                         },
246                         consensusResult: &state.ConsensusResult{
247                                 CoinbaseReward: map[string]uint64{},
248                         },
249                         wantRewards: []state.CoinbaseReward{},
250                 },
251         }
252
253         var err error
254         for i, c := range cases {
255                 c.block.Transactions[0], err = createCoinbaseTx(nil, c.block.Height)
256                 if err != nil {
257                         t.Fatal(err)
258                 }
259
260                 if err := c.consensusResult.AttachCoinbaseReward(c.block); err != nil {
261                         t.Fatal(err)
262                 }
263
264                 rewards, err := c.consensusResult.GetCoinbaseRewards(c.block.Height)
265                 if err != nil {
266                         t.Fatal(err)
267                 }
268                 if !testutil.DeepEqual(rewards, c.wantRewards) {
269                         t.Fatalf("test case %d: %s, the coinbase reward got: %v, want: %v", i, c.desc, rewards, c.wantRewards)
270                 }
271         }
272 }