6 "github.com/vapor/config"
7 "github.com/vapor/protocol/state"
8 "github.com/vapor/testutil"
11 func TestCalcReorganizeNodes(t *testing.T) {
12 c := &Chain{index: state.NewBlockIndex()}
13 header := config.GenesisBlock().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)
26 mainChainNode, err = state.NewBlockNode(&header, mainChainNode)
30 wantDetachNodes = append([]*state.BlockNode{mainChainNode}, wantDetachNodes...)
31 c.index.AddNode(mainChainNode)
33 c.bestNode = mainChainNode
34 c.index.SetMainChain(mainChainNode)
36 sideChainNode := initNode
37 for i := 1; i <= 13; i++ {
38 header.Height = uint64(i)
39 sideChainNode, err = state.NewBlockNode(&header, sideChainNode)
43 wantAttachNodes = append(wantAttachNodes, sideChainNode)
44 c.index.AddNode(sideChainNode)
47 getAttachNodes, getDetachNodes := c.calcReorganizeNodes(sideChainNode)
48 if !testutil.DeepEqual(wantAttachNodes, getAttachNodes) {
49 t.Errorf("attach nodes want %v but get %v", wantAttachNodes, getAttachNodes)
51 if !testutil.DeepEqual(wantDetachNodes, getDetachNodes) {
52 t.Errorf("detach nodes want %v but get %v", wantDetachNodes, getDetachNodes)