OSDN Git Service

fix ci
[bytom/vapor.git] / protocol / block_test.go
1 package protocol
2
3 import (
4         "testing"
5
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"
11 )
12
13 func TestCalcReorganizeNodes(t *testing.T) {
14         c := &Chain{index: state.NewBlockIndex()}
15         config.CommonConfig = config.DefaultConfig()
16         config.CommonConfig.Consensus.Dpos.SelfVoteSigners = append(config.CommonConfig.Consensus.Dpos.SelfVoteSigners, "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep")
17         config.CommonConfig.Consensus.Dpos.XPrv = "a8e281b615809046698fb0b0f2804a36d824d48fa443350f10f1b80649d39e5f1e85cf9855548915e36137345910606cbc8e7dd8497c831dce899ee6ac112445"
18         for _, v := range config.CommonConfig.Consensus.Dpos.SelfVoteSigners {
19                 address, err := common.DecodeAddress(v, &consensus.SoloNetParams)
20                 if err != nil {
21                         t.Fatal(err)
22                 }
23                 config.CommonConfig.Consensus.Dpos.Signers = append(config.CommonConfig.Consensus.Dpos.Signers, address)
24         }
25         header := config.GenesisBlock().BlockHeader
26         initNode, err := state.NewBlockNode(&header, nil)
27         if err != nil {
28                 t.Fatal(err)
29         }
30
31         c.index.AddNode(initNode)
32         var wantAttachNodes []*state.BlockNode
33         var wantDetachNodes []*state.BlockNode
34
35         mainChainNode := initNode
36         for i := 1; i <= 7; i++ {
37                 header.Height = uint64(i)
38                 mainChainNode, err = state.NewBlockNode(&header, mainChainNode)
39                 if err != nil {
40                         t.Fatal(err)
41                 }
42                 wantDetachNodes = append([]*state.BlockNode{mainChainNode}, wantDetachNodes...)
43                 c.index.AddNode(mainChainNode)
44         }
45         c.bestNode = mainChainNode
46         c.index.SetMainChain(mainChainNode)
47
48         sideChainNode := initNode
49         for i := 1; i <= 13; i++ {
50                 header.Height = uint64(i)
51                 sideChainNode, err = state.NewBlockNode(&header, sideChainNode)
52                 if err != nil {
53                         t.Fatal(err)
54                 }
55                 wantAttachNodes = append(wantAttachNodes, sideChainNode)
56                 c.index.AddNode(sideChainNode)
57         }
58
59         getAttachNodes, getDetachNodes := c.calcReorganizeNodes(sideChainNode)
60         if !testutil.DeepEqual(wantAttachNodes, getAttachNodes) {
61                 t.Errorf("attach nodes want %v but get %v", wantAttachNodes, getAttachNodes)
62         }
63         if !testutil.DeepEqual(wantDetachNodes, getDetachNodes) {
64                 t.Errorf("detach nodes want %v but get %v", wantDetachNodes, getDetachNodes)
65         }
66 }