OSDN Git Service

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