9 "github.com/bytom/consensus"
10 "github.com/bytom/protocol/bc"
11 "github.com/bytom/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",