OSDN Git Service

delete miner
[bytom/vapor.git] / protocol / state / blockindex_test.go
1 package state
2
3 import (
4         "math/big"
5         "testing"
6
7         "github.com/vapor/consensus"
8         "github.com/vapor/consensus/difficulty"
9         "github.com/vapor/protocol/bc"
10         "github.com/vapor/protocol/bc/types"
11 )
12
13 func TestCalcPastMedianTime(t *testing.T) {
14         cases := []struct {
15                 Timestamps []uint64
16                 MedianTime uint64
17         }{
18                 {
19                         Timestamps: []uint64{1},
20                         MedianTime: 1,
21                 },
22                 {
23                         Timestamps: []uint64{1, 2},
24                         MedianTime: 2,
25                 },
26                 {
27                         Timestamps: []uint64{1, 3, 2},
28                         MedianTime: 2,
29                 },
30                 {
31                         Timestamps: []uint64{1, 3, 2, 3},
32                         MedianTime: 3,
33                 },
34                 {
35                         Timestamps: []uint64{1, 2, 3, 4, 5, 6, 7, 8, 11, 10, 9},
36                         MedianTime: 6,
37                 },
38                 {
39                         Timestamps: []uint64{1, 2, 3, 4, 5, 6, 7, 8, 11, 10, 9, 11, 11, 11, 14},
40                         MedianTime: 10,
41                 },
42         }
43
44         for idx, c := range cases {
45                 var parentNode *BlockNode
46                 for i, _ := range c.Timestamps {
47                         blockHeader := &types.BlockHeader{
48                                 Height:    uint64(i),
49                                 Timestamp: c.Timestamps[i],
50                         }
51
52                         blockNode, err := NewBlockNode(blockHeader, parentNode)
53                         if err != nil {
54                                 t.Fatal(err)
55                         }
56                         parentNode = blockNode
57                 }
58
59                 medianTime := parentNode.CalcPastMedianTime()
60                 if medianTime != c.MedianTime {
61                         t.Fatalf("calc median timestamp failed, index: %d, expected: %d, have: %d", idx, c.MedianTime, medianTime)
62                 }
63         }
64 }
65
66 func TestCalcNextBits(t *testing.T) {
67         targetTimeSpan := uint64(consensus.BlocksPerRetarget * consensus.TargetSecondsPerBlock)
68         cases := []struct {
69                 parentNode  *BlockNode
70                 currentNode *BlockNode
71                 bits        uint64
72         }{
73                 {
74                         currentNode: &BlockNode{
75                                 Height: 0,
76                                 Bits:   1000,
77                         },
78                         bits: 1000,
79                 },
80                 {
81                         currentNode: &BlockNode{
82                                 Height: consensus.BlocksPerRetarget - 1,
83                                 Bits:   1000,
84                         },
85                         bits: 1000,
86                 },
87                 {
88                         parentNode: &BlockNode{
89                                 Height:    0,
90                                 Timestamp: 0,
91                         },
92                         currentNode: &BlockNode{
93                                 Height:    consensus.BlocksPerRetarget,
94                                 Bits:      difficulty.BigToCompact(big.NewInt(1000)),
95                                 Timestamp: targetTimeSpan,
96                         },
97                         bits: difficulty.BigToCompact(big.NewInt(1000)),
98                 },
99                 {
100                         parentNode: &BlockNode{
101                                 Height:    0,
102                                 Timestamp: 0,
103                         },
104                         currentNode: &BlockNode{
105                                 Height:    consensus.BlocksPerRetarget,
106                                 Bits:      difficulty.BigToCompact(big.NewInt(1000)),
107                                 Timestamp: targetTimeSpan * 2,
108                         },
109                         bits: difficulty.BigToCompact(big.NewInt(2000)),
110                 },
111         }
112
113         for i, c := range cases {
114                 c.currentNode.Parent = c.parentNode
115                 bits := c.currentNode.CalcNextBits()
116                 if bits != c.bits {
117                         t.Fatalf("calc next bit failed, index: %d, expected: %d, have: %d", i, c.bits, bits)
118                 }
119         }
120 }
121
122 func TestCalcNextSeed(t *testing.T) {
123         cases := []struct {
124                 node *BlockNode
125                 seed *bc.Hash
126         }{
127                 {
128                         node: &BlockNode{
129                                 Height: 0,
130                         },
131                         seed: consensus.InitialSeed,
132                 },
133                 {
134                         node: &BlockNode{
135                                 Height: consensus.SeedPerRetarget - 1,
136                                 Seed:   &bc.Hash{V1: 100},
137                         },
138                         seed: &bc.Hash{V1: 100},
139                 },
140                 {
141                         node: &BlockNode{
142                                 Height: consensus.SeedPerRetarget,
143                                 Seed:   &bc.Hash{V2: 200},
144                                 Hash:   bc.Hash{V3: 300},
145                         },
146                         seed: &bc.Hash{V3: 300},
147                 },
148         }
149
150         for i, c := range cases {
151                 seed := c.node.CalcNextSeed()
152                 if *seed != *c.seed {
153                         t.Fatalf("calc next seed failed, index: %d, expected: %v, have: %v", i, c.seed, seed)
154                 }
155         }
156 }
157
158 func TestSetMainChain(t *testing.T) {
159         blockIndex := NewBlockIndex()
160         var lastNode *BlockNode
161         for i := uint64(0); i < 4; i++ {
162                 node := &BlockNode{
163                         Height: i,
164                         Hash:   bc.Hash{V0: i},
165                         Parent: lastNode,
166                 }
167                 blockIndex.AddNode(node)
168                 lastNode = node
169         }
170
171         tailNode := lastNode
172         blockIndex.SetMainChain(lastNode)
173         for lastNode.Parent != nil {
174                 if !blockIndex.InMainchain(lastNode.Hash) {
175                         t.Fatalf("block %d, hash %v is not in main chain", lastNode.Height, lastNode.Hash)
176                 }
177                 lastNode = lastNode.Parent
178         }
179
180         // fork and set main chain
181         forkHeight := uint64(1)
182         lastNode = blockIndex.nodeByHeight(forkHeight)
183         for i := uint64(1); i <= 3; i++ {
184                 node := &BlockNode{
185                         Height: lastNode.Height + 1,
186                         Hash:   bc.Hash{V1: uint64(i)},
187                         Parent: lastNode,
188                 }
189                 blockIndex.AddNode(node)
190                 lastNode = node
191         }
192
193         bestNode := lastNode
194         blockIndex.SetMainChain(lastNode)
195         for lastNode.Parent != nil {
196                 if !blockIndex.InMainchain(lastNode.Hash) {
197                         t.Fatalf("after fork, block %d, hash %v is not in main chain", lastNode.Height, lastNode.Hash)
198                 }
199                 lastNode = lastNode.Parent
200         }
201
202         if bestNode != blockIndex.BestNode() {
203                 t.Fatalf("check best node failed")
204         }
205
206         for tailNode.Parent != nil && tailNode.Height > forkHeight {
207                 if blockIndex.InMainchain(tailNode.Hash) {
208                         t.Fatalf("old chain block %d, hash %v still in main chain", tailNode.Height, tailNode.Hash)
209                 }
210                 tailNode = tailNode.Parent
211         }
212 }