OSDN Git Service

delete miner
[bytom/vapor.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/vapor/consensus"
10         "github.com/vapor/protocol/bc"
11         "github.com/vapor/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                         &types.BlockHeader{
28                                 Height:    0,
29                                 Timestamp: 0},
30                         BigToCompact(big.NewInt(1000)),
31                 },
32                 {
33                         &types.BlockHeader{
34                                 Height:    consensus.BlocksPerRetarget,
35                                 Timestamp: targetTimeSpan * 2,
36                                 Bits:      BigToCompact(big.NewInt(1000))},
37                         &types.BlockHeader{
38                                 Height:    0,
39                                 Timestamp: 0},
40                         BigToCompact(big.NewInt(2000)),
41                 },
42                 {
43                         &types.BlockHeader{
44                                 Height:    consensus.BlocksPerRetarget - 1,
45                                 Timestamp: targetTimeSpan*2 - consensus.TargetSecondsPerBlock,
46                                 Bits:      BigToCompact(big.NewInt(1000))},
47                         &types.BlockHeader{
48                                 Height:    0,
49                                 Timestamp: 0},
50                         BigToCompact(big.NewInt(1000)),
51                 },
52                 {
53                         &types.BlockHeader{
54                                 Height:    consensus.BlocksPerRetarget,
55                                 Timestamp: targetTimeSpan / 2,
56                                 Bits:      BigToCompact(big.NewInt(1000))},
57                         &types.BlockHeader{
58                                 Height:    0,
59                                 Timestamp: 0},
60                         BigToCompact(big.NewInt(500)),
61                 },
62                 {
63                         &types.BlockHeader{
64                                 Height:    consensus.BlocksPerRetarget * 2,
65                                 Timestamp: targetTimeSpan + targetTimeSpan*2,
66                                 Bits:      BigToCompact(big.NewInt(1000))},
67                         &types.BlockHeader{
68                                 Height:    consensus.BlocksPerRetarget,
69                                 Timestamp: targetTimeSpan},
70                         BigToCompact(big.NewInt(2000)),
71                 },
72                 {
73                         &types.BlockHeader{
74                                 Height:    consensus.BlocksPerRetarget * 2,
75                                 Timestamp: targetTimeSpan + targetTimeSpan/2,
76                                 Bits:      BigToCompact(big.NewInt(1000))},
77                         &types.BlockHeader{
78                                 Height:    consensus.BlocksPerRetarget,
79                                 Timestamp: targetTimeSpan},
80                         BigToCompact(big.NewInt(500)),
81                 },
82                 {
83                         &types.BlockHeader{
84                                 Height:    consensus.BlocksPerRetarget*2 - 1,
85                                 Timestamp: targetTimeSpan + targetTimeSpan*2 - consensus.TargetSecondsPerBlock,
86                                 Bits:      BigToCompact(big.NewInt(1000))},
87                         &types.BlockHeader{
88                                 Height:    consensus.BlocksPerRetarget,
89                                 Timestamp: targetTimeSpan},
90                         BigToCompact(big.NewInt(1000)),
91                 },
92                 {
93                         &types.BlockHeader{
94                                 Height:    consensus.BlocksPerRetarget*2 - 1,
95                                 Timestamp: targetTimeSpan + targetTimeSpan/2 - consensus.TargetSecondsPerBlock,
96                                 Bits:      BigToCompact(big.NewInt(1000))},
97                         &types.BlockHeader{
98                                 Height:    consensus.BlocksPerRetarget,
99                                 Timestamp: targetTimeSpan},
100                         BigToCompact(big.NewInt(1000)),
101                 },
102         }
103
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)
107                         return
108                 }
109         }
110 }
111
112 func TestHashToBig(t *testing.T) {
113         cases := []struct {
114                 in  [32]byte
115                 out [32]byte
116         }{
117                 {
118                         in: [32]byte{
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,
123                         },
124                         out: [32]byte{
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,
129                         },
130                 },
131                 {
132                         in: [32]byte{
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,
137                         },
138                         out: [32]byte{
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,
143                         },
144                 },
145                 {
146                         in: [32]byte{
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,
151                         },
152                         out: [32]byte{
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,
157                         },
158                 },
159         }
160
161         for i, c := range cases {
162                 bhash := bc.NewHash(c.in)
163                 result := HashToBig(&bhash).Bytes()
164
165                 var resArr [32]byte
166                 copy(resArr[:], result)
167
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)
170                         return
171                 }
172         }
173 }
174
175 func TestCompactToBig(t *testing.T) {
176         cases := []struct {
177                 in  string
178                 out *big.Int
179         }{
180                 {
181                         in: `00000000` + //Exponent
182                                 `0` + //Sign
183                                 `0000000000000000000000000000000000000000000000000000000`, //Mantissa
184                         out: big.NewInt(0),
185                 },
186                 {
187                         in: `00000000` + //Exponent
188                                 `1` + //Sign
189                                 `0000000000000000000000000000000000000000000000000000000`, //Mantissa
190                         out: big.NewInt(0),
191                 },
192                 {
193                         in: `00000001` + //Exponent
194                                 `0` + //Sign
195                                 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
196                         out: big.NewInt(1),
197                 },
198                 {
199                         in: `00000001` + //Exponent
200                                 `1` + //Sign
201                                 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
202                         out: big.NewInt(-1),
203                 },
204                 {
205                         in: `00000011` + //Exponent
206                                 `0` + //Sign
207                                 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
208                         out: big.NewInt(65536),
209                 },
210                 {
211                         in: `00000011` + //Exponent
212                                 `1` + //Sign
213                                 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
214                         out: big.NewInt(-65536),
215                 },
216                 {
217                         in: `00000100` + //Exponent
218                                 `0` + //Sign
219                                 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
220                         out: big.NewInt(16777216),
221                 },
222                 {
223                         in: `00000100` + //Exponent
224                                 `1` + //Sign
225                                 `0000000000000000000000000000000000000010000000000000000`, //Mantissa
226                         out: big.NewInt(-16777216),
227                 },
228                 {
229                         //btm PowMin test
230                         // PowMinBits = 2161727821138738707, i.e 0x1e000000000dbe13, as defined
231                         // in /consensus/general.go
232                         in: `00011110` + //Exponent
233                                 `0` + //Sign
234                                 `0000000000000000000000000000000000011011011111000010011`, //Mantissa
235                         out: big.NewInt(0).Lsh(big.NewInt(0x0dbe13), 27*8), //2161727821138738707
236                 },
237         }
238
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)
244                         return
245                 }
246         }
247 }
248
249 func TestBigToCompact(t *testing.T) {
250         // basic tests
251         tests := []struct {
252                 in  int64
253                 out uint64
254         }{
255                 {0, 0x0000000000000000},
256                 {-0, 0x0000000000000000},
257                 {1, 0x0100000000010000},
258                 {-1, 0x0180000000010000},
259                 {65536, 0x0300000000010000},
260                 {-65536, 0x0380000000010000},
261                 {16777216, 0x0400000000010000},
262                 {-16777216, 0x0480000000010000},
263         }
264
265         for x, test := range tests {
266                 n := big.NewInt(test.in)
267                 r := BigToCompact(n)
268                 if r != test.out {
269                         t.Errorf("TestBigToCompact test #%d failed: got 0x%016x want 0x%016x\n",
270                                 x, r, test.out)
271                         return
272                 }
273         }
274
275         // btm PowMin test
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)
280         r := BigToCompact(n)
281         if r != out {
282                 t.Errorf("TestBigToCompact test #%d failed: got 0x%016x want 0x%016x\n",
283                         len(tests), r, out)
284                 return
285         }
286 }