6 "github.com/vapor/common"
7 "github.com/vapor/config"
8 "github.com/vapor/consensus"
9 "github.com/vapor/protocol/state"
10 "github.com/vapor/testutil"
13 func TestCalcReorganizeNodes(t *testing.T) {
14 c := &Chain{index: state.NewBlockIndex()}
15 config.CommonConfig = config.DefaultConfig()
16 config.CommonConfig.Consensus.SelfVoteSigners = append(config.CommonConfig.Consensus.SelfVoteSigners, "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep")
17 config.CommonConfig.Consensus.XPrv = "a8e281b615809046698fb0b0f2804a36d824d48fa443350f10f1b80649d39e5f1e85cf9855548915e36137345910606cbc8e7dd8497c831dce899ee6ac112445"
18 for _, v := range config.CommonConfig.Consensus.SelfVoteSigners {
19 address, err := common.DecodeAddress(v, &consensus.SoloNetParams)
23 config.CommonConfig.Consensus.Signers = append(config.CommonConfig.Consensus.Signers, address)
25 header := config.GenesisBlock().BlockHeader
26 initNode, err := state.NewBlockNode(&header, nil)
31 c.index.AddNode(initNode)
32 var wantAttachNodes []*state.BlockNode
33 var wantDetachNodes []*state.BlockNode
35 mainChainNode := initNode
36 for i := 1; i <= 7; i++ {
37 header.Height = uint64(i)
38 mainChainNode, err = state.NewBlockNode(&header, mainChainNode)
42 wantDetachNodes = append([]*state.BlockNode{mainChainNode}, wantDetachNodes...)
43 c.index.AddNode(mainChainNode)
45 c.bestNode = mainChainNode
46 c.index.SetMainChain(mainChainNode)
48 sideChainNode := initNode
49 for i := 1; i <= 13; i++ {
50 header.Height = uint64(i)
51 sideChainNode, err = state.NewBlockNode(&header, sideChainNode)
55 wantAttachNodes = append(wantAttachNodes, sideChainNode)
56 c.index.AddNode(sideChainNode)
59 getAttachNodes, getDetachNodes := c.calcReorganizeNodes(sideChainNode)
60 if !testutil.DeepEqual(wantAttachNodes, getAttachNodes) {
61 t.Errorf("attach nodes want %v but get %v", wantAttachNodes, getAttachNodes)
63 if !testutil.DeepEqual(wantDetachNodes, getDetachNodes) {
64 t.Errorf("detach nodes want %v but get %v", wantDetachNodes, getDetachNodes)