github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/database/store_test.go (about)

     1  package database
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/bytom/bytom/config"
     8  	dbm "github.com/bytom/bytom/database/leveldb"
     9  	"github.com/bytom/bytom/database/storage"
    10  	"github.com/bytom/bytom/protocol/bc"
    11  	"github.com/bytom/bytom/protocol/bc/types"
    12  	"github.com/bytom/bytom/protocol/state"
    13  	"github.com/bytom/bytom/testutil"
    14  )
    15  
    16  func TestSaveChainStatus(t *testing.T) {
    17  	defer os.RemoveAll("temp")
    18  	testDB := dbm.NewDB("testdb", "leveldb", "temp")
    19  	store := NewStore(testDB)
    20  
    21  	blockHeader := &types.BlockHeader{Height: 100}
    22  	blockHash := blockHeader.Hash() //Hash: bc.Hash{V0: 0, V1: 1, V2: 2, V3: 3}
    23  	view := &state.UtxoViewpoint{
    24  		Entries: map[bc.Hash]*storage.UtxoEntry{
    25  			bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.NormalUTXOType, BlockHeight: 100, Spent: false},
    26  			bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.CoinbaseUTXOType, BlockHeight: 100, Spent: true},
    27  			bc.Hash{V0: 1, V1: 1, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.NormalUTXOType, BlockHeight: 100, Spent: true},
    28  		},
    29  	}
    30  
    31  	contractView := state.NewContractViewpoint()
    32  	if err := store.SaveChainStatus(blockHeader, []*types.BlockHeader{blockHeader}, view, contractView, 0, &bc.Hash{}); err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	expectStatus := &state.BlockStoreState{Height: blockHeader.Height, Hash: &blockHash, FinalizedHeight: 0, FinalizedHash: &bc.Hash{}}
    37  	if !testutil.DeepEqual(store.GetStoreStatus(), expectStatus) {
    38  		t.Errorf("got block status:%v, expect block status:%v", store.GetStoreStatus(), expectStatus)
    39  	}
    40  
    41  	for hash, utxo := range view.Entries {
    42  		if utxo.Spent && utxo.Type != storage.CoinbaseUTXOType {
    43  			continue
    44  		}
    45  
    46  		gotUtxo, err := store.GetUtxo(&hash)
    47  		if err != nil {
    48  			t.Fatal(err)
    49  		}
    50  
    51  		if !testutil.DeepEqual(utxo, gotUtxo) {
    52  			t.Errorf("got utxo entry:%v, expect utxo entry:%v", gotUtxo, utxo)
    53  		}
    54  	}
    55  }
    56  
    57  func TestSaveBlock(t *testing.T) {
    58  	testDB := dbm.NewDB("testdb", "leveldb", "temp")
    59  	defer func() {
    60  		testDB.Close()
    61  		os.RemoveAll("temp")
    62  	}()
    63  
    64  	store := NewStore(testDB)
    65  	block := config.GenesisBlock()
    66  	if err := store.SaveBlock(block); err != nil {
    67  		t.Fatal(err)
    68  	}
    69  
    70  	blockHash := block.Hash()
    71  	gotBlock, err := store.GetBlock(&blockHash)
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  
    76  	gotBlock.Transactions[0].Tx.SerializedSize = 0
    77  	gotBlock.Transactions[0].SerializedSize = 0
    78  	gotBlock.Transactions[1].Tx.SerializedSize = 0
    79  	gotBlock.Transactions[1].SerializedSize = 0
    80  
    81  	if !testutil.DeepEqual(block, gotBlock) {
    82  		t.Errorf("got block:%v, expect block:%v", gotBlock, block)
    83  	}
    84  
    85  	data := store.db.Get(CalcBlockHeaderKey(&blockHash))
    86  	gotBlockHeader := types.BlockHeader{}
    87  	if err := gotBlockHeader.UnmarshalText(data); err != nil {
    88  		t.Fatal(err)
    89  	}
    90  
    91  	if !testutil.DeepEqual(block.BlockHeader, gotBlockHeader) {
    92  		t.Errorf("got block header:%v, expect block header:%v", gotBlockHeader, block.BlockHeader)
    93  	}
    94  }