github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/protocol/block_test.go (about) 1 package protocol 2 3 import ( 4 "testing" 5 6 "github.com/bytom/bytom/config" 7 "github.com/bytom/bytom/protocol/bc" 8 "github.com/bytom/bytom/protocol/state" 9 "github.com/bytom/bytom/testutil" 10 ) 11 12 func TestCalcReorganizeNodes(t *testing.T) { 13 c := &Chain{index: state.NewBlockIndex()} 14 header := config.GenesisBlock().BlockHeader 15 initNode, err := state.NewBlockNode(&header, nil) 16 if err != nil { 17 t.Fatal(err) 18 } 19 20 c.index.AddNode(initNode) 21 var wantAttachNodes []*state.BlockNode 22 var wantDetachNodes []*state.BlockNode 23 24 mainChainNode := initNode 25 for i := 1; i <= 7; i++ { 26 header.Height = uint64(i) 27 header.Nonce = 0 28 mainChainNode, err = state.NewBlockNode(&header, mainChainNode) 29 if err != nil { 30 t.Fatal(err) 31 } 32 wantDetachNodes = append([]*state.BlockNode{mainChainNode}, wantDetachNodes...) 33 c.index.AddNode(mainChainNode) 34 } 35 c.bestNode = mainChainNode 36 c.index.SetMainChain(mainChainNode) 37 38 sideChainNode := initNode 39 for i := 1; i <= 13; i++ { 40 header.Height = uint64(i) 41 header.Nonce = 1 42 sideChainNode, err = state.NewBlockNode(&header, sideChainNode) 43 if err != nil { 44 t.Fatal(err) 45 } 46 wantAttachNodes = append(wantAttachNodes, sideChainNode) 47 c.index.AddNode(sideChainNode) 48 } 49 50 getAttachNodes, getDetachNodes := c.calcReorganizeNodes(sideChainNode) 51 if !testutil.DeepEqual(wantAttachNodes, getAttachNodes) { 52 t.Errorf("attach nodes want %v but get %v", wantAttachNodes, getAttachNodes) 53 } 54 if !testutil.DeepEqual(wantDetachNodes, getDetachNodes) { 55 t.Errorf("detach nodes want %v but get %v", wantDetachNodes, getDetachNodes) 56 } 57 } 58 59 func TestEdgeCalcReorganizeNodes(t *testing.T) { 60 header := config.GenesisBlock().BlockHeader 61 initNode, err := state.NewBlockNode(&header, nil) 62 if err != nil { 63 t.Fatal(err) 64 } 65 66 testNodes := []*state.BlockNode{initNode} 67 testNewNodes := []*state.BlockNode{initNode} 68 for i := uint64(1); i <= 5; i++ { 69 node := &state.BlockNode{ 70 Height: i, 71 Nonce: 0, 72 Hash: bc.Hash{V0: uint64(i)}, 73 Parent: testNodes[i-1], 74 } 75 testNodes = append(testNodes, node) 76 77 newNode := &state.BlockNode{ 78 Height: i, 79 Nonce: 1, 80 Hash: bc.Hash{V1: uint64(i)}, 81 Parent: testNewNodes[i-1], 82 } 83 testNewNodes = append(testNewNodes, newNode) 84 } 85 86 cases := []struct { 87 mainChainNode *state.BlockNode 88 newNode *state.BlockNode 89 wantAttachNodes []*state.BlockNode 90 wantDetachNodes []*state.BlockNode 91 }{ 92 { 93 mainChainNode: testNodes[1], 94 newNode: testNodes[5], 95 wantAttachNodes: testNodes[2:], 96 wantDetachNodes: []*state.BlockNode{}, 97 }, 98 { 99 mainChainNode: testNodes[5], 100 newNode: testNodes[2], 101 wantAttachNodes: []*state.BlockNode{}, 102 wantDetachNodes: []*state.BlockNode{testNodes[5], testNodes[4], testNodes[3]}, 103 }, 104 { 105 mainChainNode: testNodes[2], 106 newNode: testNodes[2], 107 wantAttachNodes: []*state.BlockNode{}, 108 wantDetachNodes: []*state.BlockNode{}, 109 }, 110 { 111 mainChainNode: testNewNodes[3], 112 newNode: testNodes[2], 113 wantAttachNodes: testNodes[1:3], 114 wantDetachNodes: []*state.BlockNode{testNewNodes[3], testNewNodes[2], testNewNodes[1]}, 115 }, 116 { 117 mainChainNode: testNewNodes[2], 118 newNode: testNodes[3], 119 wantAttachNodes: testNodes[1:4], 120 wantDetachNodes: []*state.BlockNode{testNewNodes[2], testNewNodes[1]}, 121 }, 122 { 123 mainChainNode: testNodes[5], 124 newNode: testNewNodes[3], 125 wantAttachNodes: testNewNodes[1:4], 126 wantDetachNodes: []*state.BlockNode{testNodes[5], testNodes[4], testNodes[3], testNodes[2], testNodes[1]}, 127 }, 128 } 129 130 for i, c := range cases { 131 chain := &Chain{index: state.NewBlockIndex()} 132 chain.index.AddNode(initNode) 133 for i := uint64(1); i <= c.mainChainNode.Height; i++ { 134 chain.index.AddNode(testNodes[i]) 135 } 136 chain.bestNode = c.mainChainNode 137 chain.index.SetMainChain(c.mainChainNode) 138 getAttachNodes, getDetachNodes := chain.calcReorganizeNodes(c.newNode) 139 140 if !testutil.DeepEqual(c.wantAttachNodes, getAttachNodes) { 141 t.Errorf("test case %d, attach nodes want %v but get %v", i, c.wantAttachNodes, getAttachNodes) 142 } 143 144 if !testutil.DeepEqual(c.wantDetachNodes, getDetachNodes) { 145 t.Errorf("test case %d, detach nodes want %v but get %v", i, c.wantDetachNodes, getDetachNodes) 146 } 147 } 148 }