OSDN Git Service

new repo
[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                 header.Nonce = 0
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                 header.Nonce = 1
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 }