package leveldb import ( "os" "testing" "github.com/vapor/config" "github.com/vapor/protocol/bc" "github.com/vapor/protocol/bc/types" dbm "github.com/tendermint/tmlibs/db" ) func TestLoadBlockIndex(t *testing.T) { defer os.RemoveAll("temp") testDB := dbm.NewDB("testdb", "leveldb", "temp") store := NewStore(testDB) block := config.GenesisBlock() txStatus := bc.NewTransactionStatus() if err := store.SaveBlock(block, txStatus); err != nil { t.Fatal(err) } for block.Height <= 128 { preHash := block.Hash() block.PreviousBlockHash = preHash block.Height += 1 if err := store.SaveBlock(block, txStatus); err != nil { t.Fatal(err) } if block.Height%32 != 0 { continue } for i := uint64(0); i < block.Height/32; i++ { if err := store.SaveBlock(block, txStatus); err != nil { t.Fatal(err) } } } if _, err := store.LoadBlockIndex(128); err != nil { t.Fatal(err) } } func TestLoadBlockIndexBestHeight(t *testing.T) { cases := []struct { blockBestHeight uint64 stateBestHeight uint64 }{ { blockBestHeight: 100, stateBestHeight: 90, }, { blockBestHeight: 100, stateBestHeight: 0, }, { blockBestHeight: 100, stateBestHeight: 100, }, } defer os.RemoveAll("temp") testDB := dbm.NewDB("testdb", "leveldb", "temp") store := NewStore(testDB) var savedBlocks []types.Block for _, c := range cases { block := config.GenesisBlock() txStatus := bc.NewTransactionStatus() for i := uint64(0); i < c.blockBestHeight; i++ { if err := store.SaveBlock(block, txStatus); err != nil { t.Fatal(err) } savedBlocks = append(savedBlocks, *block) block.PreviousBlockHash = block.Hash() block.Height++ } index, err := store.LoadBlockIndex(c.stateBestHeight) if err != nil { t.Fatal(err) } for _, block := range savedBlocks { blockHash := block.Hash() if block.Height <= c.stateBestHeight != index.BlockExist(&blockHash) { t.Errorf("Error in load block index") } } } }