OSDN Git Service

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