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))},
30 BigToCompact(big.NewInt(1000)),
34 Height: consensus.BlocksPerRetarget,
35 Timestamp: targetTimeSpan * 2,
36 Bits: BigToCompact(big.NewInt(1000))},
40 BigToCompact(big.NewInt(2000)),
44 Height: consensus.BlocksPerRetarget - 1,
45 Timestamp: targetTimeSpan*2 - consensus.TargetSecondsPerBlock,
46 Bits: BigToCompact(big.NewInt(1000))},
50 BigToCompact(big.NewInt(1000)),
54 Height: consensus.BlocksPerRetarget,
55 Timestamp: targetTimeSpan / 2,
56 Bits: BigToCompact(big.NewInt(1000))},
60 BigToCompact(big.NewInt(500)),
64 Height: consensus.BlocksPerRetarget * 2,
65 Timestamp: targetTimeSpan + targetTimeSpan*2,
66 Bits: BigToCompact(big.NewInt(1000))},
68 Height: consensus.BlocksPerRetarget,
69 Timestamp: targetTimeSpan},
70 BigToCompact(big.NewInt(2000)),
74 Height: consensus.BlocksPerRetarget * 2,
75 Timestamp: targetTimeSpan + targetTimeSpan/2,
76 Bits: BigToCompact(big.NewInt(1000))},
78 Height: consensus.BlocksPerRetarget,
79 Timestamp: targetTimeSpan},
80 BigToCompact(big.NewInt(500)),
84 Height: consensus.BlocksPerRetarget*2 - 1,
85 Timestamp: targetTimeSpan + targetTimeSpan*2 - consensus.TargetSecondsPerBlock,
86 Bits: BigToCompact(big.NewInt(1000))},
88 Height: consensus.BlocksPerRetarget,
89 Timestamp: targetTimeSpan},
90 BigToCompact(big.NewInt(1000)),
94 Height: consensus.BlocksPerRetarget*2 - 1,
95 Timestamp: targetTimeSpan + targetTimeSpan/2 - consensus.TargetSecondsPerBlock,
96 Bits: BigToCompact(big.NewInt(1000))},
98 Height: consensus.BlocksPerRetarget,
99 Timestamp: targetTimeSpan},
100 BigToCompact(big.NewInt(1000)),
104 for i, c := range cases {
105 if got := CalcNextRequiredDifficulty(c.lastBH, c.compareBH); got != c.want {
106 t.Errorf("Compile(%d) = %d want %d\n", i, got, c.want)
112 func TestHashToBig(t *testing.T) {
119 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
120 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
121 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
122 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
125 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
126 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
127 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
128 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
133 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
136 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
139 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
140 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
141 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
142 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
147 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
148 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
149 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
150 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
153 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8,
154 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
155 0xef, 0xee, 0xed, 0xec, 0xeb, 0xea, 0xe9, 0xe8,
156 0xe7, 0xe6, 0xe5, 0xe4, 0xe3, 0xe2, 0xe1, 0xe0,
161 for i, c := range cases {
162 bhash := bc.NewHash(c.in)
163 result := HashToBig(&bhash).Bytes()
166 copy(resArr[:], result)
168 if !reflect.DeepEqual(resArr, c.out) {
169 t.Errorf("TestHashToBig test #%d failed:\n\tgot\t%x\n\twant\t%x\n", i, resArr, c.out)
175 func TestCompactToBig(t *testing.T) {
181 in: `00000000` + //Exponent
183 `0000000000000000000000000000000000000000000000000000000`, //Mantissa
187 in: `00000000` + //Exponent
189 `0000000000000000000000000000000000000000000000000000000`, //Mantissa
193 in: `00000001` + //Exponent
195 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
199 in: `00000001` + //Exponent
201 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
205 in: `00000011` + //Exponent
207 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
208 out: big.NewInt(65536),
211 in: `00000011` + //Exponent
213 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
214 out: big.NewInt(-65536),
217 in: `00000100` + //Exponent
219 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
220 out: big.NewInt(16777216),
223 in: `00000100` + //Exponent
225 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
226 out: big.NewInt(-16777216),
230 // PowMinBits = 2161727821138738707, i.e 0x1e000000000dbe13, as defined
231 // in /consensus/general.go
232 in: `00011110` + //Exponent
234 `0000000000000000000000000000000000011011011111000010011`, //Mantissa
235 out: big.NewInt(0).Lsh(big.NewInt(0x0dbe13), 27*8), //2161727821138738707
239 for i, c := range cases {
240 compact, _ := strconv.ParseUint(c.in, 2, 64)
241 r := CompactToBig(compact)
242 if r.Cmp(c.out) != 0 {
243 t.Error("TestCompactToBig test #", i, "failed: got", r, "want", c.out)
249 func TestBigToCompact(t *testing.T) {
255 {0, 0x0000000000000000},
256 {-0, 0x0000000000000000},
257 {1, 0x0100000000010000},
258 {-1, 0x0180000000010000},
259 {65536, 0x0300000000010000},
260 {-65536, 0x0380000000010000},
261 {16777216, 0x0400000000010000},
262 {-16777216, 0x0480000000010000},
265 for x, test := range tests {
266 n := big.NewInt(test.in)
269 t.Errorf("TestBigToCompact test #%d failed: got 0x%016x want 0x%016x\n",
276 // PowMinBits = 2161727821138738707, i.e 0x1e000000000dbe13, as defined
277 // in /consensus/general.go
278 n := big.NewInt(0).Lsh(big.NewInt(0x0dbe13), 27*8)
279 out := uint64(0x1e000000000dbe13)
282 t.Errorf("TestBigToCompact test #%d failed: got 0x%016x want 0x%016x\n",