7 "github.com/vapor/config"
8 "github.com/vapor/protocol/bc"
9 "github.com/vapor/protocol/bc/types"
11 dbm "github.com/tendermint/tmlibs/db"
14 func TestLoadBlockIndex(t *testing.T) {
15 defer os.RemoveAll("temp")
16 testDB := dbm.NewDB("testdb", "leveldb", "temp")
17 store := NewStore(testDB)
19 block := config.GenesisBlock()
20 txStatus := bc.NewTransactionStatus()
22 if err := store.SaveBlock(block, txStatus); err != nil {
26 for block.Height <= 128 {
27 preHash := block.Hash()
28 block.PreviousBlockHash = preHash
30 if err := store.SaveBlock(block, txStatus); err != nil {
34 if block.Height%32 != 0 {
38 for i := uint64(0); i < block.Height/32; i++ {
39 if err := store.SaveBlock(block, txStatus); err != nil {
45 if _, err := store.LoadBlockIndex(128); err != nil {
50 func TestLoadBlockIndexBestHeight(t *testing.T) {
52 blockBestHeight uint64
53 stateBestHeight uint64
69 defer os.RemoveAll("temp")
70 testDB := dbm.NewDB("testdb", "leveldb", "temp")
71 store := NewStore(testDB)
72 var savedBlocks []types.Block
74 for _, c := range cases {
75 block := config.GenesisBlock()
76 txStatus := bc.NewTransactionStatus()
78 for i := uint64(0); i < c.blockBestHeight; i++ {
79 if err := store.SaveBlock(block, txStatus); err != nil {
83 savedBlocks = append(savedBlocks, *block)
84 block.PreviousBlockHash = block.Hash()
88 index, err := store.LoadBlockIndex(c.stateBestHeight)
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")