OSDN Git Service

fix coinbase fee bug (#1642)
[bytom/bytom.git] / consensus / difficulty / difficulty_test.go
1 package difficulty
2
3 import (
4         "math/big"
5         "reflect"
6         "strconv"
7         "testing"
8
9         "github.com/bytom/consensus"
10         "github.com/bytom/protocol/bc"
11         "github.com/bytom/protocol/bc/types"
12 )
13
14 // A lower difficulty Int actually reflects a more difficult mining progress.
15 func TestCalcNextRequiredDifficulty(t *testing.T) {
16         targetTimeSpan := uint64(consensus.BlocksPerRetarget * consensus.TargetSecondsPerBlock)
17         cases := []struct {
18                 lastBH    *types.BlockHeader
19                 compareBH *types.BlockHeader
20                 want      uint64
21         }{
22                 {
23                         &types.BlockHeader{
24                                 Height:    consensus.BlocksPerRetarget,
25                                 Timestamp: targetTimeSpan,
26                                 Bits:      BigToCompact(big.NewInt(1000)),
27                         },
28                         &types.BlockHeader{
29                                 Height:    0,
30                                 Timestamp: 0,
31                         },
32                         BigToCompact(big.NewInt(1000)),
33                 },
34                 {
35                         &types.BlockHeader{
36                                 Height:    consensus.BlocksPerRetarget,
37                                 Timestamp: targetTimeSpan * 2,
38                                 Bits:      BigToCompact(big.NewInt(1000)),
39                         },
40                         &types.BlockHeader{
41                                 Height:    0,
42                                 Timestamp: 0,
43                         },
44                         BigToCompact(big.NewInt(2000)),
45                 },
46                 {
47                         &types.BlockHeader{
48                                 Height:    consensus.BlocksPerRetarget - 1,
49                                 Timestamp: targetTimeSpan*2 - consensus.TargetSecondsPerBlock,
50                                 Bits:      BigToCompact(big.NewInt(1000)),
51                         },
52                         &types.BlockHeader{
53                                 Height:    0,
54                                 Timestamp: 0,
55                         },
56                         BigToCompact(big.NewInt(1000)),
57                 },
58                 {
59                         &types.BlockHeader{
60                                 Height:    consensus.BlocksPerRetarget,
61                                 Timestamp: targetTimeSpan / 2,
62                                 Bits:      BigToCompact(big.NewInt(1000)),
63                         },
64                         &types.BlockHeader{
65                                 Height:    0,
66                                 Timestamp: 0,
67                         },
68                         BigToCompact(big.NewInt(500)),
69                 },
70                 {
71                         &types.BlockHeader{
72                                 Height:    consensus.BlocksPerRetarget * 2,
73                                 Timestamp: targetTimeSpan + targetTimeSpan*2,
74                                 Bits:      BigToCompact(big.NewInt(1000)),
75                         },
76                         &types.BlockHeader{
77                                 Height:    consensus.BlocksPerRetarget,
78                                 Timestamp: targetTimeSpan,
79                         },
80                         BigToCompact(big.NewInt(2000)),
81                 },
82                 {
83                         &types.BlockHeader{
84                                 Height:    consensus.BlocksPerRetarget * 2,
85                                 Timestamp: targetTimeSpan + targetTimeSpan/2,
86                                 Bits:      BigToCompact(big.NewInt(1000)),
87                         },
88                         &types.BlockHeader{
89                                 Height:    consensus.BlocksPerRetarget,
90                                 Timestamp: targetTimeSpan,
91                         },
92                         BigToCompact(big.NewInt(500)),
93                 },
94                 {
95                         &types.BlockHeader{
96                                 Height:    consensus.BlocksPerRetarget*2 - 1,
97                                 Timestamp: targetTimeSpan + targetTimeSpan*2 - consensus.TargetSecondsPerBlock,
98                                 Bits:      BigToCompact(big.NewInt(1000)),
99                         },
100                         &types.BlockHeader{
101                                 Height:    consensus.BlocksPerRetarget,
102                                 Timestamp: targetTimeSpan,
103                         },
104                         BigToCompact(big.NewInt(1000)),
105                 },
106                 {
107                         &types.BlockHeader{
108                                 Height:    consensus.BlocksPerRetarget*2 - 1,
109                                 Timestamp: targetTimeSpan + targetTimeSpan/2 - consensus.TargetSecondsPerBlock,
110                                 Bits:      BigToCompact(big.NewInt(1000)),
111                         },
112                         &types.BlockHeader{
113                                 Height:    consensus.BlocksPerRetarget,
114                                 Timestamp: targetTimeSpan,
115                         },
116                         BigToCompact(big.NewInt(1000)),
117                 },
118                 // lastBH.Height: 0, lastBH.Timestamp - compareBH.Timestamp: 0, lastBH.Bits: 0
119                 {
120                         &types.BlockHeader{
121                                 Height:    0,
122                                 Timestamp: 0,
123                                 Bits:      0,
124                         },
125                         &types.BlockHeader{
126                                 Height:    0,
127                                 Timestamp: 0,
128                         },
129                         0,
130                 },
131                 // lastBH.Height: 0, lastBH.Timestamp - compareBH.Timestamp: 0, lastBH.Bits: 18446744073709551615
132                 {
133                         &types.BlockHeader{
134                                 Height:    0,
135                                 Timestamp: 0,
136                                 Bits:      18446744073709551615,
137                         },
138                         &types.BlockHeader{
139                                 Height:    0,
140                                 Timestamp: 0,
141                         },
142                         18446744073709551615,
143                 },
144                 // lastBH.Height: 0, lastBH.Timestamp - compareBH.Timestamp: 0, lastBH.Bits: bigInt(1000)
145                 {
146                         &types.BlockHeader{
147                                 Height:    0,
148                                 Timestamp: 0,
149                                 Bits:      BigToCompact(big.NewInt(1000)),
150                         },
151                         &types.BlockHeader{
152                                 Height:    0,
153                                 Timestamp: 0,
154                         },
155                         BigToCompact(big.NewInt(1000)),
156                 },
157                 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 0, lastBH.Bits: bigInt(1000)
158                 {
159                         &types.BlockHeader{
160                                 Height:    consensus.BlocksPerRetarget,
161                                 Timestamp: targetTimeSpan,
162                                 Bits:      BigToCompact(big.NewInt(1000)),
163                         },
164                         &types.BlockHeader{
165                                 Height:    consensus.BlocksPerRetarget - 1,
166                                 Timestamp: targetTimeSpan,
167                         },
168                         0,
169                 },
170                 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: -9223372036854775808, lastBH.Bits: bigInt(1000)
171                 {
172                         &types.BlockHeader{
173                                 Height:    consensus.BlocksPerRetarget,
174                                 Timestamp: targetTimeSpan,
175                                 Bits:      BigToCompact(big.NewInt(1000)),
176                         },
177                         &types.BlockHeader{
178                                 Height:    consensus.BlocksPerRetarget - 1,
179                                 Timestamp: targetTimeSpan + 9223372036854775808,
180                         },
181                         540431955291560988,
182                 },
183                 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 9223372036854775807, lastBH.Bits: bigInt(1000)
184                 {
185                         &types.BlockHeader{
186                                 Height:    consensus.BlocksPerRetarget,
187                                 Timestamp: targetTimeSpan + 9223372036854775807,
188                                 Bits:      BigToCompact(big.NewInt(1000)),
189                         },
190                         &types.BlockHeader{
191                                 Height:    consensus.BlocksPerRetarget - 1,
192                                 Timestamp: targetTimeSpan,
193                         },
194                         504403158272597019,
195                 },
196                 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 18446744073709551615, lastBH.Bits: bigInt(1000)
197                 {
198                         &types.BlockHeader{
199                                 Height:    consensus.BlocksPerRetarget,
200                                 Timestamp: 18446744073709551615,
201                                 Bits:      BigToCompact(big.NewInt(1000)),
202                         },
203                         &types.BlockHeader{
204                                 Height:    consensus.BlocksPerRetarget - 1,
205                                 Timestamp: 0,
206                         },
207                         108086391056957440,
208                 },
209                 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: bigInt(1000)
210                 {
211                         &types.BlockHeader{
212                                 Height:    consensus.BlocksPerRetarget,
213                                 Timestamp: targetTimeSpan * 2,
214                                 Bits:      BigToCompact(big.NewInt(1000)),
215                         },
216                         &types.BlockHeader{
217                                 Height:    consensus.BlocksPerRetarget - 1,
218                                 Timestamp: targetTimeSpan,
219                         },
220                         BigToCompact(big.NewInt(1000)),
221                 },
222                 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 604800, lastBH.Bits: bigInt(1000)
223                 {
224                         &types.BlockHeader{
225                                 Height:    consensus.BlocksPerRetarget,
226                                 Timestamp: targetTimeSpan * 3,
227                                 Bits:      BigToCompact(big.NewInt(1000)),
228                         },
229                         &types.BlockHeader{
230                                 Height:    consensus.BlocksPerRetarget - 1,
231                                 Timestamp: targetTimeSpan,
232                         },
233                         144115188076367872,
234                 },
235                 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 151200, lastBH.Bits: bigInt(1000)
236                 {
237                         &types.BlockHeader{
238                                 Height:    consensus.BlocksPerRetarget,
239                                 Timestamp: targetTimeSpan + 9223372036854775807,
240                                 Bits:      BigToCompact(big.NewInt(1000)),
241                         },
242                         &types.BlockHeader{
243                                 Height:    consensus.BlocksPerRetarget - 1,
244                                 Timestamp: targetTimeSpan,
245                         },
246                         504403158272597019,
247                 },
248                 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: 0
249                 {
250                         &types.BlockHeader{
251                                 Height:    consensus.BlocksPerRetarget,
252                                 Timestamp: targetTimeSpan * 2,
253                                 Bits:      0,
254                         },
255                         &types.BlockHeader{
256                                 Height:    consensus.BlocksPerRetarget - 1,
257                                 Timestamp: targetTimeSpan,
258                         },
259                         0,
260                 },
261                 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: 18446744073709551615
262                 {
263                         &types.BlockHeader{
264                                 Height:    consensus.BlocksPerRetarget,
265                                 Timestamp: targetTimeSpan * 2,
266                                 Bits:      18446744073709551615,
267                         },
268                         &types.BlockHeader{
269                                 Height:    consensus.BlocksPerRetarget - 1,
270                                 Timestamp: targetTimeSpan,
271                         },
272                         252201579141136384,
273                 },
274                 // lastBH.Height: consensus.BlocksPerRetarget + 1, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: bigInt(1000)
275                 {
276                         &types.BlockHeader{
277                                 Height:    consensus.BlocksPerRetarget + 1,
278                                 Timestamp: targetTimeSpan * 2,
279                                 Bits:      BigToCompact(big.NewInt(1000)),
280                         },
281                         &types.BlockHeader{
282                                 Height:    consensus.BlocksPerRetarget,
283                                 Timestamp: targetTimeSpan,
284                         },
285                         BigToCompact(big.NewInt(1000)),
286                 },
287                 // lastBH.Height: consensus.BlocksPerRetarget - 1, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: bigInt(1000)
288                 {
289                         &types.BlockHeader{
290                                 Height:    consensus.BlocksPerRetarget - 1,
291                                 Timestamp: targetTimeSpan * 2,
292                                 Bits:      BigToCompact(big.NewInt(1000)),
293                         },
294                         &types.BlockHeader{
295                                 Height:    consensus.BlocksPerRetarget - 2,
296                                 Timestamp: targetTimeSpan,
297                         },
298                         BigToCompact(big.NewInt(1000)),
299                 },
300                 // lastBH.Height: consensus.BlocksPerRetarget * 2, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: bigInt(1000)
301                 {
302                         &types.BlockHeader{
303                                 Height:    consensus.BlocksPerRetarget * 2,
304                                 Timestamp: targetTimeSpan * 2,
305                                 Bits:      BigToCompact(big.NewInt(1000)),
306                         },
307                         &types.BlockHeader{
308                                 Height:    consensus.BlocksPerRetarget*2 - 1,
309                                 Timestamp: targetTimeSpan,
310                         },
311                         BigToCompact(big.NewInt(1000)),
312                 },
313                 // lastBH.Height: consensus.BlocksPerRetarget / 2, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: bigInt(1000)
314                 {
315                         &types.BlockHeader{
316                                 Height:    consensus.BlocksPerRetarget / 2,
317                                 Timestamp: targetTimeSpan * 2,
318                                 Bits:      BigToCompact(big.NewInt(1000)),
319                         },
320                         &types.BlockHeader{
321                                 Height:    consensus.BlocksPerRetarget/2 - 1,
322                                 Timestamp: targetTimeSpan,
323                         },
324                         BigToCompact(big.NewInt(1000)),
325                 },
326         }
327
328         for i, c := range cases {
329                 if got := CalcNextRequiredDifficulty(c.lastBH, c.compareBH); got != c.want {
330                         t.Errorf("Compile(%d) = %d want %d\n", i, got, c.want)
331                         return
332                 }
333         }
334 }
335
336 func TestHashToBig(t *testing.T) {
337         cases := []struct {
338                 in  [32]byte
339                 out [32]byte
340         }{
341                 {
342                         in: [32]byte{
343                                 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
344                                 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
345                                 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
346                                 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
347                         },
348                         out: [32]byte{
349                                 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
350                                 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
351                                 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
352                                 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
353                         },
354                 },
355                 {
356                         in: [32]byte{
357                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
358                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
359                                 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
360                                 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
361                         },
362                         out: [32]byte{
363                                 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
364                                 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
365                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
366                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
367                         },
368                 },
369                 {
370                         in: [32]byte{
371                                 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
372                                 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
373                                 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
374                                 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
375                         },
376                         out: [32]byte{
377                                 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8,
378                                 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
379                                 0xef, 0xee, 0xed, 0xec, 0xeb, 0xea, 0xe9, 0xe8,
380                                 0xe7, 0xe6, 0xe5, 0xe4, 0xe3, 0xe2, 0xe1, 0xe0,
381                         },
382                 },
383         }
384
385         for i, c := range cases {
386                 bhash := bc.NewHash(c.in)
387                 result := HashToBig(&bhash).Bytes()
388
389                 var resArr [32]byte
390                 copy(resArr[:], result)
391
392                 if !reflect.DeepEqual(resArr, c.out) {
393                         t.Errorf("TestHashToBig test #%d failed:\n\tgot\t%x\n\twant\t%x\n", i, resArr, c.out)
394                         return
395                 }
396         }
397 }
398
399 func TestCompactToBig(t *testing.T) {
400         cases := []struct {
401                 in  string
402                 out *big.Int
403         }{
404                 {
405                         in: `00000000` + //Exponent
406                                 `0` + //Sign
407                                 `0000000000000000000000000000000000000000000000000000000`, //Mantissa
408                         out: big.NewInt(0),
409                 },
410                 {
411                         in: `00000000` + //Exponent
412                                 `1` + //Sign
413                                 `0000000000000000000000000000000000000000000000000000000`, //Mantissa
414                         out: big.NewInt(0),
415                 },
416                 {
417                         in: `00000001` + //Exponent
418                                 `0` + //Sign
419                                 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
420                         out: big.NewInt(1),
421                 },
422                 {
423                         in: `00000001` + //Exponent
424                                 `1` + //Sign
425                                 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
426                         out: big.NewInt(-1),
427                 },
428                 {
429                         in: `00000011` + //Exponent
430                                 `0` + //Sign
431                                 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
432                         out: big.NewInt(65536),
433                 },
434                 {
435                         in: `00000011` + //Exponent
436                                 `1` + //Sign
437                                 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
438                         out: big.NewInt(-65536),
439                 },
440                 {
441                         in: `00000100` + //Exponent
442                                 `0` + //Sign
443                                 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
444                         out: big.NewInt(16777216),
445                 },
446                 {
447                         in: `00000100` + //Exponent
448                                 `1` + //Sign
449                                 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
450                         out: big.NewInt(-16777216),
451                 },
452                 {
453                         //btm PowMin test
454                         // PowMinBits = 2161727821138738707, i.e 0x1e000000000dbe13, as defined
455                         // in /consensus/general.go
456                         in: `00011110` + //Exponent
457                                 `0` + //Sign
458                                 `0000000000000000000000000000000000011011011111000010011`, //Mantissa
459                         out: big.NewInt(0).Lsh(big.NewInt(0x0dbe13), 27*8), //2161727821138738707
460                 },
461         }
462
463         for i, c := range cases {
464                 compact, _ := strconv.ParseUint(c.in, 2, 64)
465                 r := CompactToBig(compact)
466                 if r.Cmp(c.out) != 0 {
467                         t.Error("TestCompactToBig test #", i, "failed: got", r, "want", c.out)
468                         return
469                 }
470         }
471 }
472
473 func TestBigToCompact(t *testing.T) {
474         // basic tests
475         tests := []struct {
476                 in  int64
477                 out uint64
478         }{
479                 {0, 0x0000000000000000},
480                 {-0, 0x0000000000000000},
481                 {1, 0x0100000000010000},
482                 {-1, 0x0180000000010000},
483                 {65536, 0x0300000000010000},
484                 {-65536, 0x0380000000010000},
485                 {16777216, 0x0400000000010000},
486                 {-16777216, 0x0480000000010000},
487         }
488
489         for x, test := range tests {
490                 n := big.NewInt(test.in)
491                 r := BigToCompact(n)
492                 if r != test.out {
493                         t.Errorf("TestBigToCompact test #%d failed: got 0x%016x want 0x%016x\n",
494                                 x, r, test.out)
495                         return
496                 }
497         }
498
499         // btm PowMin test
500         // PowMinBits = 2161727821138738707, i.e 0x1e000000000dbe13, as defined
501         // in /consensus/general.go
502         n := big.NewInt(0).Lsh(big.NewInt(0x0dbe13), 27*8)
503         out := uint64(0x1e000000000dbe13)
504         r := BigToCompact(n)
505         if r != out {
506                 t.Errorf("TestBigToCompact test #%d failed: got 0x%016x want 0x%016x\n",
507                         len(tests), r, out)
508                 return
509         }
510 }