6 "github.com/vapor/config"
7 "github.com/vapor/protocol/bc"
8 "github.com/vapor/protocol/state"
9 "github.com/vapor/testutil"
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)
21 c.index.AddNode(initNode)
22 var wantAttachNodes []*state.BlockNode
23 var wantDetachNodes []*state.BlockNode
25 mainChainNode := initNode
26 for i := 1; i <= 7; i++ {
27 header.Height = uint64(i)
28 mainChainNode, err = state.NewBlockNode(&header, mainChainNode)
32 wantDetachNodes = append([]*state.BlockNode{mainChainNode}, wantDetachNodes...)
33 c.index.AddNode(mainChainNode)
35 c.bestNode = mainChainNode
36 c.index.SetMainChain(mainChainNode)
38 sideChainNode := initNode
39 for i := 1; i <= 13; i++ {
40 header.Height = uint64(i)
41 sideChainNode, err = state.NewBlockNode(&header, sideChainNode)
45 wantAttachNodes = append(wantAttachNodes, sideChainNode)
46 c.index.AddNode(sideChainNode)
49 getAttachNodes, getDetachNodes := c.calcReorganizeNodes(sideChainNode)
50 if !testutil.DeepEqual(wantAttachNodes, getAttachNodes) {
51 t.Errorf("attach nodes want %v but get %v", wantAttachNodes, getAttachNodes)
53 if !testutil.DeepEqual(wantDetachNodes, getDetachNodes) {
54 t.Errorf("detach nodes want %v but get %v", wantDetachNodes, getDetachNodes)
58 func TestEdgeCalcReorganizeNodes(t *testing.T) {
59 config.CommonConfig = config.DefaultConfig()
60 header := config.GenesisBlock().BlockHeader
61 initNode, err := state.NewBlockNode(&header, nil)
66 testNodes := []*state.BlockNode{initNode}
67 testNewNodes := []*state.BlockNode{initNode}
68 for i := uint64(1); i <= 5; i++ {
69 node := &state.BlockNode{
71 Hash: bc.Hash{V0: uint64(i)},
72 Parent: testNodes[i-1],
74 testNodes = append(testNodes, node)
76 newNode := &state.BlockNode{
78 Hash: bc.Hash{V1: uint64(i)},
79 Parent: testNewNodes[i-1],
81 testNewNodes = append(testNewNodes, newNode)
85 mainChainNode *state.BlockNode
86 newNode *state.BlockNode
87 wantAttachNodes []*state.BlockNode
88 wantDetachNodes []*state.BlockNode
91 mainChainNode: testNodes[1],
92 newNode: testNodes[5],
93 wantAttachNodes: testNodes[2:],
94 wantDetachNodes: []*state.BlockNode{},
97 mainChainNode: testNodes[5],
98 newNode: testNodes[2],
99 wantAttachNodes: []*state.BlockNode{},
100 wantDetachNodes: []*state.BlockNode{testNodes[5], testNodes[4], testNodes[3]},
103 mainChainNode: testNodes[2],
104 newNode: testNodes[2],
105 wantAttachNodes: []*state.BlockNode{},
106 wantDetachNodes: []*state.BlockNode{},
109 mainChainNode: testNewNodes[3],
110 newNode: testNodes[2],
111 wantAttachNodes: testNodes[1:3],
112 wantDetachNodes: []*state.BlockNode{testNewNodes[3], testNewNodes[2], testNewNodes[1]},
115 mainChainNode: testNewNodes[2],
116 newNode: testNodes[3],
117 wantAttachNodes: testNodes[1:4],
118 wantDetachNodes: []*state.BlockNode{testNewNodes[2], testNewNodes[1]},
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]},
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])
134 chain.bestNode = c.mainChainNode
135 chain.index.SetMainChain(c.mainChainNode)
136 getAttachNodes, getDetachNodes := chain.calcReorganizeNodes(c.newNode)
138 if !testutil.DeepEqual(c.wantAttachNodes, getAttachNodes) {
139 t.Errorf("test case %d, attach nodes want %v but get %v", i, c.wantAttachNodes, getAttachNodes)
142 if !testutil.DeepEqual(c.wantDetachNodes, getDetachNodes) {
143 t.Errorf("test case %d, detach nodes want %v but get %v", i, c.wantDetachNodes, getDetachNodes)