OSDN Git Service

update master (#487)
[bytom/bytom-spv.git] / consensus / difficulty / difficulty_test.go
1 package difficulty
2
3 import (
4         "math/big"
5         "testing"
6
7         "github.com/bytom/consensus"
8         "github.com/bytom/protocol/bc/types"
9 )
10
11 func TestCalcNextRequiredDifficulty(t *testing.T) {
12         targetTimeSpan := uint64(consensus.BlocksPerRetarget * consensus.TargetSecondsPerBlock)
13         cases := []struct {
14                 lastBH    *types.BlockHeader
15                 compareBH *types.BlockHeader
16                 want      uint64
17         }{
18                 //{nil, nil, powMinBits},
19                 //{&types.BlockHeader{Height: BlocksPerRetarget, Bits: 87654321}, nil, 87654321},
20                 {
21                         &types.BlockHeader{Height: consensus.BlocksPerRetarget, Timestamp: targetTimeSpan, Bits: BigToCompact(big.NewInt(1000))},
22                         &types.BlockHeader{Height: 0, Timestamp: 0},
23                         BigToCompact(big.NewInt(1000)),
24                 },
25                 {
26                         &types.BlockHeader{Height: consensus.BlocksPerRetarget, Timestamp: targetTimeSpan * 2, Bits: BigToCompact(big.NewInt(1000))},
27                         &types.BlockHeader{Height: 0, Timestamp: 0},
28                         BigToCompact(big.NewInt(2000)),
29                 },
30                 {
31                         &types.BlockHeader{Height: consensus.
32                                 BlocksPerRetarget, Timestamp: targetTimeSpan / 2, Bits: BigToCompact(big.NewInt(1000))},
33                         &types.BlockHeader{Height: 0, Timestamp: 0},
34                         BigToCompact(big.NewInt(500)),
35                 },
36         }
37
38         for i, c := range cases {
39                 if got := CalcNextRequiredDifficulty(c.lastBH, c.compareBH); got != c.want {
40                         t.Errorf("Compile(%d) = %d want %d", i, got, c.want)
41                 }
42         }
43 }