-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++
- 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++ {
- block.Nonce++
- 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")
- }
- }
- }
-}
-
-func TestLoadBlockIndexEquals(t *testing.T) {
- defer os.RemoveAll("temp")
- testDB := dbm.NewDB("testdb", "leveldb", "temp")
- store := NewStore(testDB)
-
- block := config.GenesisBlock()
- txStatus := bc.NewTransactionStatus()
- expectBlockIndex := state.NewBlockIndex()
- var parent *state.BlockNode
-
- for block.Height <= 100 {
- if err := store.SaveBlock(block, txStatus); err != nil {
- t.Fatal(err)
- }
-
- if block.Height != 0 {
- parent = expectBlockIndex.GetNode(&block.PreviousBlockHash)
- }
-
- node, err := state.NewBlockNode(&block.BlockHeader, parent)
- if err != nil {
- t.Fatal(err)
- }
-
- expectBlockIndex.AddNode(node)
- block.PreviousBlockHash = block.Hash()
- block.Height++
- }
-
- index, err := store.LoadBlockIndex(100)
- if err != nil {
- t.Fatal(err)
- }
-
- if !testutil.DeepEqual(expectBlockIndex, index) {
- t.Errorf("got block index:%v, expect block index:%v", index, expectBlockIndex)
- }
-}