OSDN Git Service

new repo
[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                         block.Nonce += 1
40                         if err := store.SaveBlock(block, txStatus); err != nil {
41                                 t.Fatal(err)
42                         }
43                 }
44         }
45
46         if _, err := store.LoadBlockIndex(128); err != nil {
47                 t.Fatal(err)
48         }
49 }
50
51 func TestLoadBlockIndexBestHeight(t *testing.T) {
52         cases := []struct {
53                 blockBestHeight uint64
54                 stateBestHeight uint64
55         }{
56                 {
57                         blockBestHeight: 100,
58                         stateBestHeight: 90,
59                 },
60                 {
61                         blockBestHeight: 100,
62                         stateBestHeight: 0,
63                 },
64                 {
65                         blockBestHeight: 100,
66                         stateBestHeight: 100,
67                 },
68         }
69
70         defer os.RemoveAll("temp")
71         testDB := dbm.NewDB("testdb", "leveldb", "temp")
72         store := NewStore(testDB)
73         var savedBlocks []types.Block
74
75         for _, c := range cases {
76                 block := config.GenesisBlock()
77                 txStatus := bc.NewTransactionStatus()
78
79                 for i := uint64(0); i < c.blockBestHeight; i++ {
80                         if err := store.SaveBlock(block, txStatus); err != nil {
81                                 t.Fatal(err)
82                         }
83
84                         savedBlocks = append(savedBlocks, *block)
85                         block.PreviousBlockHash = block.Hash()
86                         block.Height++
87                 }
88
89                 index, err := store.LoadBlockIndex(c.stateBestHeight)
90                 if err != nil {
91                         t.Fatal(err)
92                 }
93
94                 for _, block := range savedBlocks {
95                         blockHash := block.Hash()
96                         if block.Height <= c.stateBestHeight != index.BlockExist(&blockHash) {
97                                 t.Errorf("Error in load block index")
98                         }
99                 }
100         }
101 }