OSDN Git Service

store federation (#83)
[bytom/vapor.git] / protocol / block_test.go
1 package protocol
2
3 import (
4         "testing"
5
6         "github.com/vapor/config"
7         "github.com/vapor/protocol/bc"
8         "github.com/vapor/protocol/state"
9         "github.com/vapor/testutil"
10 )
11
12 func TestCalcReorganizeNodes(t *testing.T) {
13         config.CommonConfig = config.DefaultConfig()
14         c := &Chain{index: state.NewBlockIndex()}
15         header := config.GenesisBlock().BlockHeader
16         initNode, err := state.NewBlockNode(&header, nil)
17         if err != nil {
18                 t.Fatal(err)
19         }
20
21         c.index.AddNode(initNode)
22         var wantAttachNodes []*state.BlockNode
23         var wantDetachNodes []*state.BlockNode
24
25         mainChainNode := initNode
26         for i := 1; i <= 7; i++ {
27                 header.Height = uint64(i)
28                 mainChainNode, err = state.NewBlockNode(&header, mainChainNode)
29                 if err != nil {
30                         t.Fatal(err)
31                 }
32                 wantDetachNodes = append([]*state.BlockNode{mainChainNode}, wantDetachNodes...)
33                 c.index.AddNode(mainChainNode)
34         }
35         c.bestNode = mainChainNode
36         c.index.SetMainChain(mainChainNode)
37
38         sideChainNode := initNode
39         for i := 1; i <= 13; i++ {
40                 header.Height = uint64(i)
41                 sideChainNode, err = state.NewBlockNode(&header, sideChainNode)
42                 if err != nil {
43                         t.Fatal(err)
44                 }
45                 wantAttachNodes = append(wantAttachNodes, sideChainNode)
46                 c.index.AddNode(sideChainNode)
47         }
48
49         getAttachNodes, getDetachNodes := c.calcReorganizeNodes(sideChainNode)
50         if !testutil.DeepEqual(wantAttachNodes, getAttachNodes) {
51                 t.Errorf("attach nodes want %v but get %v", wantAttachNodes, getAttachNodes)
52         }
53         if !testutil.DeepEqual(wantDetachNodes, getDetachNodes) {
54                 t.Errorf("detach nodes want %v but get %v", wantDetachNodes, getDetachNodes)
55         }
56 }
57
58 func TestEdgeCalcReorganizeNodes(t *testing.T) {
59         config.CommonConfig = config.DefaultConfig()
60         header := config.GenesisBlock().BlockHeader
61         initNode, err := state.NewBlockNode(&header, nil)
62         if err != nil {
63                 t.Fatal(err)
64         }
65
66         testNodes := []*state.BlockNode{initNode}
67         testNewNodes := []*state.BlockNode{initNode}
68         for i := uint64(1); i <= 5; i++ {
69                 node := &state.BlockNode{
70                         Height: i,
71                         Hash:   bc.Hash{V0: uint64(i)},
72                         Parent: testNodes[i-1],
73                 }
74                 testNodes = append(testNodes, node)
75
76                 newNode := &state.BlockNode{
77                         Height: i,
78                         Hash:   bc.Hash{V1: uint64(i)},
79                         Parent: testNewNodes[i-1],
80                 }
81                 testNewNodes = append(testNewNodes, newNode)
82         }
83
84         cases := []struct {
85                 mainChainNode   *state.BlockNode
86                 newNode         *state.BlockNode
87                 wantAttachNodes []*state.BlockNode
88                 wantDetachNodes []*state.BlockNode
89         }{
90                 {
91                         mainChainNode:   testNodes[1],
92                         newNode:         testNodes[5],
93                         wantAttachNodes: testNodes[2:],
94                         wantDetachNodes: []*state.BlockNode{},
95                 },
96                 {
97                         mainChainNode:   testNodes[5],
98                         newNode:         testNodes[2],
99                         wantAttachNodes: []*state.BlockNode{},
100                         wantDetachNodes: []*state.BlockNode{testNodes[5], testNodes[4], testNodes[3]},
101                 },
102                 {
103                         mainChainNode:   testNodes[2],
104                         newNode:         testNodes[2],
105                         wantAttachNodes: []*state.BlockNode{},
106                         wantDetachNodes: []*state.BlockNode{},
107                 },
108                 {
109                         mainChainNode:   testNewNodes[3],
110                         newNode:         testNodes[2],
111                         wantAttachNodes: testNodes[1:3],
112                         wantDetachNodes: []*state.BlockNode{testNewNodes[3], testNewNodes[2], testNewNodes[1]},
113                 },
114                 {
115                         mainChainNode:   testNewNodes[2],
116                         newNode:         testNodes[3],
117                         wantAttachNodes: testNodes[1:4],
118                         wantDetachNodes: []*state.BlockNode{testNewNodes[2], testNewNodes[1]},
119                 },
120                 {
121                         mainChainNode:   testNodes[5],
122                         newNode:         testNewNodes[3],
123                         wantAttachNodes: testNewNodes[1:4],
124                         wantDetachNodes: []*state.BlockNode{testNodes[5], testNodes[4], testNodes[3], testNodes[2], testNodes[1]},
125                 },
126         }
127
128         for i, c := range cases {
129                 chain := &Chain{index: state.NewBlockIndex()}
130                 chain.index.AddNode(initNode)
131                 for i := uint64(1); i <= c.mainChainNode.Height; i++ {
132                         chain.index.AddNode(testNodes[i])
133                 }
134                 chain.bestNode = c.mainChainNode
135                 chain.index.SetMainChain(c.mainChainNode)
136                 getAttachNodes, getDetachNodes := chain.calcReorganizeNodes(c.newNode)
137
138                 if !testutil.DeepEqual(c.wantAttachNodes, getAttachNodes) {
139                         t.Errorf("test case %d, attach nodes want %v but get %v", i, c.wantAttachNodes, getAttachNodes)
140                 }
141
142                 if !testutil.DeepEqual(c.wantDetachNodes, getDetachNodes) {
143                         t.Errorf("test case %d, detach nodes want %v but get %v", i, c.wantDetachNodes, getDetachNodes)
144                 }
145         }
146 }