OSDN Git Service

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