OSDN Git Service

Merge pull request #15 from Bytom/dev_ci
[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/state"
8         "github.com/vapor/testutil"
9 )
10
11 func TestCalcReorganizeNodes(t *testing.T) {
12         c := &Chain{index: state.NewBlockIndex()}
13         header := config.GenesisBlock().BlockHeader
14         initNode, err := state.NewBlockNode(&header, nil)
15         if err != nil {
16                 t.Fatal(err)
17         }
18
19         c.index.AddNode(initNode)
20         var wantAttachNodes []*state.BlockNode
21         var wantDetachNodes []*state.BlockNode
22
23         mainChainNode := initNode
24         for i := 1; i <= 7; i++ {
25                 header.Height = uint64(i)
26                 mainChainNode, err = state.NewBlockNode(&header, mainChainNode)
27                 if err != nil {
28                         t.Fatal(err)
29                 }
30                 wantDetachNodes = append([]*state.BlockNode{mainChainNode}, wantDetachNodes...)
31                 c.index.AddNode(mainChainNode)
32         }
33         c.bestNode = mainChainNode
34         c.index.SetMainChain(mainChainNode)
35
36         sideChainNode := initNode
37         for i := 1; i <= 13; i++ {
38                 header.Height = uint64(i)
39                 sideChainNode, err = state.NewBlockNode(&header, sideChainNode)
40                 if err != nil {
41                         t.Fatal(err)
42                 }
43                 wantAttachNodes = append(wantAttachNodes, sideChainNode)
44                 c.index.AddNode(sideChainNode)
45         }
46
47         getAttachNodes, getDetachNodes := c.calcReorganizeNodes(sideChainNode)
48         if !testutil.DeepEqual(wantAttachNodes, getAttachNodes) {
49                 t.Errorf("attach nodes want %v but get %v", wantAttachNodes, getAttachNodes)
50         }
51         if !testutil.DeepEqual(wantDetachNodes, getDetachNodes) {
52                 t.Errorf("detach nodes want %v but get %v", wantDetachNodes, getDetachNodes)
53         }
54 }