OSDN Git Service

a126447a4af6bd8be262461fa8d58a478cd07993
[bytom/vapor.git] / database / leveldb / store_test.go
1 package leveldb
2
3 import (
4         "os"
5         "testing"
6
7         "github.com/vapor/common"
8         "github.com/vapor/config"
9         "github.com/vapor/consensus"
10         "github.com/vapor/protocol/bc"
11         "github.com/vapor/protocol/bc/types"
12
13         dbm "github.com/tendermint/tmlibs/db"
14 )
15
16 func TestLoadBlockIndex(t *testing.T) {
17         defer os.RemoveAll("temp")
18         testDB := dbm.NewDB("testdb", "leveldb", "temp")
19         store := NewStore(testDB)
20         config.CommonConfig = config.DefaultConfig()
21         config.CommonConfig.Consensus.SelfVoteSigners = append(config.CommonConfig.Consensus.SelfVoteSigners, "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep")
22         config.CommonConfig.Consensus.XPrv = "a8e281b615809046698fb0b0f2804a36d824d48fa443350f10f1b80649d39e5f1e85cf9855548915e36137345910606cbc8e7dd8497c831dce899ee6ac112445"
23         for _, v := range config.CommonConfig.Consensus.SelfVoteSigners {
24                 address, err := common.DecodeAddress(v, &consensus.SoloNetParams)
25                 if err != nil {
26                         t.Fatal(err)
27                 }
28                 config.CommonConfig.Consensus.Signers = append(config.CommonConfig.Consensus.Signers, address)
29         }
30         block := config.GenesisBlock()
31         txStatus := bc.NewTransactionStatus()
32
33         if err := store.SaveBlock(block, txStatus); err != nil {
34                 t.Fatal(err)
35         }
36
37         for block.Height <= 128 {
38                 preHash := block.Hash()
39                 block.PreviousBlockHash = preHash
40                 block.Height += 1
41                 if err := store.SaveBlock(block, txStatus); err != nil {
42                         t.Fatal(err)
43                 }
44
45                 if block.Height%32 != 0 {
46                         continue
47                 }
48
49                 for i := uint64(0); i < block.Height/32; i++ {
50                         if err := store.SaveBlock(block, txStatus); err != nil {
51                                 t.Fatal(err)
52                         }
53                 }
54         }
55
56         if _, err := store.LoadBlockIndex(128); err != nil {
57                 t.Fatal(err)
58         }
59 }
60
61 func TestLoadBlockIndexBestHeight(t *testing.T) {
62         cases := []struct {
63                 blockBestHeight uint64
64                 stateBestHeight uint64
65         }{
66                 {
67                         blockBestHeight: 100,
68                         stateBestHeight: 90,
69                 },
70                 {
71                         blockBestHeight: 100,
72                         stateBestHeight: 0,
73                 },
74                 {
75                         blockBestHeight: 100,
76                         stateBestHeight: 100,
77                 },
78         }
79
80         defer os.RemoveAll("temp")
81         testDB := dbm.NewDB("testdb", "leveldb", "temp")
82         store := NewStore(testDB)
83         var savedBlocks []types.Block
84         config.CommonConfig = config.DefaultConfig()
85         config.CommonConfig.Consensus.SelfVoteSigners = append(config.CommonConfig.Consensus.SelfVoteSigners, "vsm1qkm743xmgnvh84pmjchq2s4tnfpgu9ae2f9slep")
86         config.CommonConfig.Consensus.XPrv = "a8e281b615809046698fb0b0f2804a36d824d48fa443350f10f1b80649d39e5f1e85cf9855548915e36137345910606cbc8e7dd8497c831dce899ee6ac112445"
87         for _, v := range config.CommonConfig.Consensus.SelfVoteSigners {
88                 address, err := common.DecodeAddress(v, &consensus.SoloNetParams)
89                 if err != nil {
90                         t.Fatal(err)
91                 }
92                 config.CommonConfig.Consensus.Signers = append(config.CommonConfig.Consensus.Signers, address)
93         }
94
95         for _, c := range cases {
96                 block := config.GenesisBlock()
97                 txStatus := bc.NewTransactionStatus()
98
99                 for i := uint64(0); i < c.blockBestHeight; i++ {
100                         if err := store.SaveBlock(block, txStatus); err != nil {
101                                 t.Fatal(err)
102                         }
103
104                         savedBlocks = append(savedBlocks, *block)
105                         block.PreviousBlockHash = block.Hash()
106                         block.Height++
107                 }
108
109                 index, err := store.LoadBlockIndex(c.stateBestHeight)
110                 if err != nil {
111                         t.Fatal(err)
112                 }
113
114                 for _, block := range savedBlocks {
115                         blockHash := block.Hash()
116                         if block.Height <= c.stateBestHeight != index.BlockExist(&blockHash) {
117                                 t.Errorf("Error in load block index")
118                         }
119                 }
120         }
121 }