9 "github.com/vapor/consensus"
10 "github.com/vapor/protocol/bc"
11 "github.com/vapor/protocol/bc/types"
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)
18 lastBH *types.BlockHeader
19 compareBH *types.BlockHeader
24 Height: consensus.BlocksPerRetarget,
25 Timestamp: targetTimeSpan,
26 Bits: BigToCompact(big.NewInt(1000)),
32 BigToCompact(big.NewInt(1000)),
36 Height: consensus.BlocksPerRetarget,
37 Timestamp: targetTimeSpan * 2,
38 Bits: BigToCompact(big.NewInt(1000)),
44 BigToCompact(big.NewInt(2000)),
48 Height: consensus.BlocksPerRetarget - 1,
49 Timestamp: targetTimeSpan*2 - consensus.TargetSecondsPerBlock,
50 Bits: BigToCompact(big.NewInt(1000)),
56 BigToCompact(big.NewInt(1000)),
60 Height: consensus.BlocksPerRetarget,
61 Timestamp: targetTimeSpan / 2,
62 Bits: BigToCompact(big.NewInt(1000)),
68 BigToCompact(big.NewInt(500)),
72 Height: consensus.BlocksPerRetarget * 2,
73 Timestamp: targetTimeSpan + targetTimeSpan*2,
74 Bits: BigToCompact(big.NewInt(1000)),
77 Height: consensus.BlocksPerRetarget,
78 Timestamp: targetTimeSpan,
80 BigToCompact(big.NewInt(2000)),
84 Height: consensus.BlocksPerRetarget * 2,
85 Timestamp: targetTimeSpan + targetTimeSpan/2,
86 Bits: BigToCompact(big.NewInt(1000)),
89 Height: consensus.BlocksPerRetarget,
90 Timestamp: targetTimeSpan,
92 BigToCompact(big.NewInt(500)),
96 Height: consensus.BlocksPerRetarget*2 - 1,
97 Timestamp: targetTimeSpan + targetTimeSpan*2 - consensus.TargetSecondsPerBlock,
98 Bits: BigToCompact(big.NewInt(1000)),
101 Height: consensus.BlocksPerRetarget,
102 Timestamp: targetTimeSpan,
104 BigToCompact(big.NewInt(1000)),
108 Height: consensus.BlocksPerRetarget*2 - 1,
109 Timestamp: targetTimeSpan + targetTimeSpan/2 - consensus.TargetSecondsPerBlock,
110 Bits: BigToCompact(big.NewInt(1000)),
113 Height: consensus.BlocksPerRetarget,
114 Timestamp: targetTimeSpan,
116 BigToCompact(big.NewInt(1000)),
118 // lastBH.Height: 0, lastBH.Timestamp - compareBH.Timestamp: 0, lastBH.Bits: 0
131 // lastBH.Height: 0, lastBH.Timestamp - compareBH.Timestamp: 0, lastBH.Bits: 18446744073709551615
136 Bits: 18446744073709551615,
142 18446744073709551615,
144 // lastBH.Height: 0, lastBH.Timestamp - compareBH.Timestamp: 0, lastBH.Bits: bigInt(1000)
149 Bits: BigToCompact(big.NewInt(1000)),
155 BigToCompact(big.NewInt(1000)),
157 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 0, lastBH.Bits: bigInt(1000)
160 Height: consensus.BlocksPerRetarget,
161 Timestamp: targetTimeSpan,
162 Bits: BigToCompact(big.NewInt(1000)),
165 Height: consensus.BlocksPerRetarget - 1,
166 Timestamp: targetTimeSpan,
170 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: -9223372036854775808, lastBH.Bits: bigInt(1000)
173 Height: consensus.BlocksPerRetarget,
174 Timestamp: targetTimeSpan,
175 Bits: BigToCompact(big.NewInt(1000)),
178 Height: consensus.BlocksPerRetarget - 1,
179 Timestamp: targetTimeSpan + 9223372036854775808,
183 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 9223372036854775807, lastBH.Bits: bigInt(1000)
186 Height: consensus.BlocksPerRetarget,
187 Timestamp: targetTimeSpan + 9223372036854775807,
188 Bits: BigToCompact(big.NewInt(1000)),
191 Height: consensus.BlocksPerRetarget - 1,
192 Timestamp: targetTimeSpan,
196 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 18446744073709551615, lastBH.Bits: bigInt(1000)
199 Height: consensus.BlocksPerRetarget,
200 Timestamp: 18446744073709551615,
201 Bits: BigToCompact(big.NewInt(1000)),
204 Height: consensus.BlocksPerRetarget - 1,
209 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: bigInt(1000)
212 Height: consensus.BlocksPerRetarget,
213 Timestamp: targetTimeSpan * 2,
214 Bits: BigToCompact(big.NewInt(1000)),
217 Height: consensus.BlocksPerRetarget - 1,
218 Timestamp: targetTimeSpan,
220 BigToCompact(big.NewInt(1000)),
222 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 604800, lastBH.Bits: bigInt(1000)
225 Height: consensus.BlocksPerRetarget,
226 Timestamp: targetTimeSpan * 3,
227 Bits: BigToCompact(big.NewInt(1000)),
230 Height: consensus.BlocksPerRetarget - 1,
231 Timestamp: targetTimeSpan,
235 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 151200, lastBH.Bits: bigInt(1000)
238 Height: consensus.BlocksPerRetarget,
239 Timestamp: targetTimeSpan + 9223372036854775807,
240 Bits: BigToCompact(big.NewInt(1000)),
243 Height: consensus.BlocksPerRetarget - 1,
244 Timestamp: targetTimeSpan,
248 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: 0
251 Height: consensus.BlocksPerRetarget,
252 Timestamp: targetTimeSpan * 2,
256 Height: consensus.BlocksPerRetarget - 1,
257 Timestamp: targetTimeSpan,
261 // lastBH.Height: consensus.BlocksPerRetarget, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: 18446744073709551615
264 Height: consensus.BlocksPerRetarget,
265 Timestamp: targetTimeSpan * 2,
266 Bits: 18446744073709551615,
269 Height: consensus.BlocksPerRetarget - 1,
270 Timestamp: targetTimeSpan,
274 // lastBH.Height: consensus.BlocksPerRetarget + 1, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: bigInt(1000)
277 Height: consensus.BlocksPerRetarget + 1,
278 Timestamp: targetTimeSpan * 2,
279 Bits: BigToCompact(big.NewInt(1000)),
282 Height: consensus.BlocksPerRetarget,
283 Timestamp: targetTimeSpan,
285 BigToCompact(big.NewInt(1000)),
287 // lastBH.Height: consensus.BlocksPerRetarget - 1, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: bigInt(1000)
290 Height: consensus.BlocksPerRetarget - 1,
291 Timestamp: targetTimeSpan * 2,
292 Bits: BigToCompact(big.NewInt(1000)),
295 Height: consensus.BlocksPerRetarget - 2,
296 Timestamp: targetTimeSpan,
298 BigToCompact(big.NewInt(1000)),
300 // lastBH.Height: consensus.BlocksPerRetarget * 2, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: bigInt(1000)
303 Height: consensus.BlocksPerRetarget * 2,
304 Timestamp: targetTimeSpan * 2,
305 Bits: BigToCompact(big.NewInt(1000)),
308 Height: consensus.BlocksPerRetarget*2 - 1,
309 Timestamp: targetTimeSpan,
311 BigToCompact(big.NewInt(1000)),
313 // lastBH.Height: consensus.BlocksPerRetarget / 2, lastBH.Timestamp - compareBH.Timestamp: 302400, lastBH.Bits: bigInt(1000)
316 Height: consensus.BlocksPerRetarget / 2,
317 Timestamp: targetTimeSpan * 2,
318 Bits: BigToCompact(big.NewInt(1000)),
321 Height: consensus.BlocksPerRetarget/2 - 1,
322 Timestamp: targetTimeSpan,
324 BigToCompact(big.NewInt(1000)),
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)
336 func TestHashToBig(t *testing.T) {
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,
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,
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,
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,
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,
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,
385 for i, c := range cases {
386 bhash := bc.NewHash(c.in)
387 result := HashToBig(&bhash).Bytes()
390 copy(resArr[:], result)
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)
399 func TestCompactToBig(t *testing.T) {
405 in: `00000000` + //Exponent
407 `0000000000000000000000000000000000000000000000000000000`, //Mantissa
411 in: `00000000` + //Exponent
413 `0000000000000000000000000000000000000000000000000000000`, //Mantissa
417 in: `00000001` + //Exponent
419 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
423 in: `00000001` + //Exponent
425 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
429 in: `00000011` + //Exponent
431 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
432 out: big.NewInt(65536),
435 in: `00000011` + //Exponent
437 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
438 out: big.NewInt(-65536),
441 in: `00000100` + //Exponent
443 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
444 out: big.NewInt(16777216),
447 in: `00000100` + //Exponent
449 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
450 out: big.NewInt(-16777216),
454 // PowMinBits = 2161727821138738707, i.e 0x1e000000000dbe13, as defined
455 // in /consensus/general.go
456 in: `00011110` + //Exponent
458 `0000000000000000000000000000000000011011011111000010011`, //Mantissa
459 out: big.NewInt(0).Lsh(big.NewInt(0x0dbe13), 27*8), //2161727821138738707
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)
473 func TestBigToCompact(t *testing.T) {
479 {0, 0x0000000000000000},
480 {-0, 0x0000000000000000},
481 {1, 0x0100000000010000},
482 {-1, 0x0180000000010000},
483 {65536, 0x0300000000010000},
484 {-65536, 0x0380000000010000},
485 {16777216, 0x0400000000010000},
486 {-16777216, 0x0480000000010000},
489 for x, test := range tests {
490 n := big.NewInt(test.in)
493 t.Errorf("TestBigToCompact test #%d failed: got 0x%016x want 0x%016x\n",
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)
506 t.Errorf("TestBigToCompact test #%d failed: got 0x%016x want 0x%016x\n",
512 func TestCalcWorkWithIntStr(t *testing.T) {
517 // Exponent: 0, Sign: 0, Mantissa: 0
519 `00000000` + //Exponent
521 `0000000000000000000000000000000000000000000000000000000`, //Mantissa
524 // Exponent: 0, Sign: 0, Mantissa: 1 (difficultyNum = 0 and difficultyNum.Sign() = 0)
528 `0000000000000000000000000000000000000000000000000000001`,
531 // Exponent: 0, Sign: 0, Mantissa: 65536 (difficultyNum = 0 and difficultyNum.Sign() = 0)
535 `0000000000000000000000000000000000000010000000000000000`,
538 // Exponent: 0, Sign: 0, Mantissa: 16777216 (difficultyNum = 1 and difficultyNum.Sign() = 1)
542 `0000000000000000000000000000001000000000000000000000000`,
543 new(big.Int).Div(oneLsh256, big.NewInt(2)),
545 // Exponent: 0, Sign: 0, Mantissa: 0x007fffffffffffff
549 `1111111111111111111111111111111111111111111111111111111`,
550 big.NewInt(0).Lsh(big.NewInt(0x020000), 208),
552 // Exponent: 0, Sign: 1, Mantissa: 0
556 `0000000000000000000000000000000000000000000000000000000`,
559 // Exponent: 0, Sign: 1, Mantissa: 1 (difficultyNum = 0 and difficultyNum.Sign() = 0)
563 `0000000000000000000000000000000000000000000000000000001`,
566 // Exponent: 0, Sign: 1, Mantissa: 65536 (difficultyNum = 0 and difficultyNum.Sign() = 0)
570 `0000000000000000000000000000000000000010000000000000000`,
573 // Exponent: 0, Sign: 0, Mantissa: 16777216 (difficultyNum = -1 and difficultyNum.Sign() = -1)
577 `0000000000000000000000000000001000000000000000000000000`,
580 // Exponent: 0, Sign: 1, Mantissa: 0x007fffffffffffff
584 `1111111111111111111111111111111111111111111111111111111`,
587 // Exponent: 3, Sign: 0, Mantissa: 0
591 `0000000000000000000000000000000000000000000000000000000`,
594 // Exponent: 3, Sign: 0, Mantissa: 1 (difficultyNum = 1 and difficultyNum.Sign() = 1)
598 `0000000000000000000000000000000000000000000000000000001`,
599 new(big.Int).Div(oneLsh256, big.NewInt(2)),
601 // Exponent: 3, Sign: 0, Mantissa: 65536 (difficultyNum = 65536 and difficultyNum.Sign() = 1)
605 `0000000000000000000000000000000000000010000000000000000`,
606 new(big.Int).Div(oneLsh256, big.NewInt(65537)),
608 // Exponent: 0, Sign: 0, Mantissa: 16777216 (difficultyNum = 16777216 and difficultyNum.Sign() = 1)
612 `0000000000000000000000000000001000000000000000000000000`,
613 new(big.Int).Div(oneLsh256, big.NewInt(16777217)),
615 // Exponent: 3, Sign: 0, Mantissa: 0x007fffffffffffff
619 `1111111111111111111111111111111111111111111111111111111`,
620 new(big.Int).Div(oneLsh256, big.NewInt(36028797018963968)),
622 // Exponent: 3, Sign: 1, Mantissa: 0
626 `0000000000000000000000000000000000000000000000000000000`,
629 //Exponent: 3, Sign: 1, Mantissa: 1 (difficultyNum = -1 and difficultyNum.Sign() = -1)
633 `0000000000000000000000000000000000000000000000000000001`,
636 // Exponent: 3, Sign: 1, Mantissa: 65536 (difficultyNum = -65536 and difficultyNum.Sign() = -1)
640 `0000000000000000000000000000000000000010000000000000000`,
643 // Exponent: 3, Sign: 1, Mantissa: 16777216 (difficultyNum = -16777216 and difficultyNum.Sign() = -1)
647 `0000000000000000000000000000001000000000000000000000000`,
650 // Exponent: 3, Sign: 1, Mantissa: 0x007fffffffffffff
654 `1111111111111111111111111111111111111111111111111111111`,
657 // Exponent: 7, Sign: 0, Mantissa: 0
661 `0000000000000000000000000000000000000000000000000000000`,
664 //Exponent: 7, Sign: 1, Mantissa: 1 (difficultyNum = 4294967296 and difficultyNum.Sign() = 1)
668 `0000000000000000000000000000000000000000000000000000001`,
669 new(big.Int).Div(oneLsh256, big.NewInt(4294967297)),
671 // Exponent: 7, Sign: 0, Mantissa: 65536 (difficultyNum = 4294967296 and difficultyNum.Sign() = 1)
675 `0000000000000000000000000000000000000010000000000000000`,
676 new(big.Int).Div(oneLsh256, big.NewInt(281474976710657)),
678 // Exponent: 7, Sign: 0, Mantissa: 16777216 (difficultyNum = 72057594037927936 and difficultyNum.Sign() = 1)
682 `0000000000000000000000000000001000000000000000000000000`,
683 new(big.Int).Div(oneLsh256, big.NewInt(72057594037927937)),
685 // Exponent: 7, Sign: 0, Mantissa: 0x007fffffffffffff
689 `1111111111111111111111111111111111111111111111111111111`,
690 new(big.Int).Div(oneLsh256, new(big.Int).Add(big.NewInt(0).Lsh(big.NewInt(36028797018963967), 32), bigOne)),
692 // Exponent: 7, Sign: 1, Mantissa: 0
696 `0000000000000000000000000000000000000000000000000000000`,
699 // Exponent: 7, Sign: 1, Mantissa: 1 (difficultyNum = -4294967296 and difficultyNum.Sign() = -1)
703 `0000000000000000000000000000000000000000000000000000001`,
706 // Exponent: 7, Sign: 1, Mantissa: 65536 (difficultyNum = -72057594037927936 and difficultyNum.Sign() = -1)
710 `0000000000000000000000000000000000000010000000000000000`,
713 // Exponent: 7, Sign: 1, Mantissa: 16777216 (difficultyNum = -154742504910672530067423232 and difficultyNum.Sign() = -1)
717 `0000000000000000000000000000001000000000000000000000000`,
720 // Exponent: 7, Sign: 1, Mantissa: 0x007fffffffffffff
724 `1111111111111111111111111111111111111111111111111111111`,
727 // Exponent: 255, Sign: 0, Mantissa: 1 (difficultyNum.Sign() = 1)
731 `0000000000000000000000000000000000000000000000000000001`,
734 // Exponent: 255, Sign: 0, Mantissa: 65536 (difficultyNum.Sign() = 1)
738 `0000000000000000000000000000000000000010000000000000000`,
741 // Exponent: 255, Sign: 0, Mantissa: 16777216 (difficultyNum.Sign() = 1)
745 `0000000000000000000000000000001000000000000000000000000`,
748 // Exponent: 255, Sign: 0, Mantissa: 0x007fffffffffffff
752 `1111111111111111111111111111111111111111111111111111111`,
755 // Exponent: 255, Sign: 1, Mantissa: 1
759 `0000000000000000000000000000000000000000000000000000001`,
762 // Exponent: 255, Sign: 1, Mantissa: 65536
766 `0000000000000000000000000000000000000010000000000000000`,
769 // Exponent: 255, Sign: 1, Mantissa: 16777216
773 `0000000000000000000000000000001000000000000000000000000`,
776 // Exponent: 255, Sign: 1, Mantissa: 0x007fffffffffffff
780 `1111111111111111111111111111111111111111111111111111111`,
785 for i, c := range cases {
786 bits, err := strconv.ParseUint(c.strBits, 2, 64)
788 t.Errorf("convert string into uint error: %s\n", err)
792 if got := CalcWork(bits); got.Cmp(c.want) != 0 {
793 t.Errorf("CalcWork(%d) = %s, want %s\n", i, got, c.want)
799 func TestCalcWork(t *testing.T) {
800 testCases := []struct {
822 new(big.Int).Div(oneLsh256, big.NewInt(2)),
826 new(big.Int).Div(oneLsh256, big.NewInt(256)),
830 new(big.Int).Div(oneLsh256, big.NewInt(2147483648)),
842 new(big.Int).Div(oneLsh256, big.NewInt(2)),
846 new(big.Int).Div(oneLsh256, big.NewInt(16777217)),
850 new(big.Int).Div(oneLsh256, big.NewInt(36028797018963968)),
862 new(big.Int).Div(oneLsh256, big.NewInt(257)),
866 new(big.Int).Div(oneLsh256, new(big.Int).Add(big.NewInt(0).Lsh(big.NewInt(36028797018963967), 32), bigOne)),
877 18446744073709551615,
882 for i, c := range testCases {
883 if got := CalcWork(c.bits); got.Cmp(c.want) != 0 {
884 t.Errorf("test case with uint64 for CalcWork(%d) = %s, want %s\n", i, got, c.want)