6 "github.com/bytom/config"
7 "github.com/bytom/protocol/state"
8 "github.com/bytom/testutil"
11 func TestCalcReorganizeNodes(t *testing.T) {
12 c := &Chain{index: state.NewBlockIndex()}
13 header := config.GenerateGenesisBlock().BlockHeader
14 initNode, err := state.NewBlockNode(&header, nil)
19 c.index.AddNode(initNode)
20 var wantAttachNodes []*state.BlockNode
21 var wantDetachNodes []*state.BlockNode
23 mainChainNode := initNode
24 for i := 1; i <= 7; i++ {
25 header.Height = uint64(i)
27 mainChainNode, err = state.NewBlockNode(&header, mainChainNode)
31 wantDetachNodes = append([]*state.BlockNode{mainChainNode}, wantDetachNodes...)
32 c.index.AddNode(mainChainNode)
34 c.bestNode = mainChainNode
35 c.index.SetMainChain(mainChainNode)
37 sideChainNode := initNode
38 for i := 1; i <= 13; i++ {
39 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)