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++ {
40 if err := store.SaveBlock(block, txStatus); err != nil {
46 if _, err := store.LoadBlockIndex(128); err != nil {
51 func TestLoadBlockIndexBestHeight(t *testing.T) {
53 blockBestHeight uint64
54 stateBestHeight uint64
70 defer os.RemoveAll("temp")
71 testDB := dbm.NewDB("testdb", "leveldb", "temp")
72 store := NewStore(testDB)
73 var savedBlocks []types.Block
75 for _, c := range cases {
76 block := config.GenesisBlock()
77 txStatus := bc.NewTransactionStatus()
79 for i := uint64(0); i < c.blockBestHeight; i++ {
80 if err := store.SaveBlock(block, txStatus); err != nil {
84 savedBlocks = append(savedBlocks, *block)
85 block.PreviousBlockHash = block.Hash()
89 index, err := store.LoadBlockIndex(c.stateBestHeight)
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")