OSDN Git Service

feat: add build crosschain input (#91)
[bytom/vapor.git] / database / store_test.go
index f04907a..e9d7527 100644 (file)
@@ -15,9 +15,13 @@ import (
 )
 
 func TestLoadBlockIndex(t *testing.T) {
-       defer os.RemoveAll("temp")
+       config.CommonConfig = config.DefaultConfig()
        testDB := dbm.NewDB("testdb", "leveldb", "temp")
        store := NewStore(testDB)
+       defer func() {
+               testDB.Close()
+               os.RemoveAll("temp")
+       }()
 
        block := config.GenesisBlock()
        txStatus := bc.NewTransactionStatus()
@@ -39,7 +43,7 @@ func TestLoadBlockIndex(t *testing.T) {
                }
 
                for i := uint64(0); i < block.Height/32; i++ {
-                       block.Nonce++
+                       block.Version++
                        if err := store.SaveBlock(block, txStatus); err != nil {
                                t.Fatal(err)
                        }
@@ -70,8 +74,11 @@ func TestLoadBlockIndexBestHeight(t *testing.T) {
                },
        }
 
-       defer os.RemoveAll("temp")
        testDB := dbm.NewDB("testdb", "leveldb", "temp")
+       defer func() {
+               testDB.Close()
+               os.RemoveAll("temp")
+       }()
        store := NewStore(testDB)
        var savedBlocks []types.Block
 
@@ -104,9 +111,12 @@ func TestLoadBlockIndexBestHeight(t *testing.T) {
 }
 
 func TestLoadBlockIndexEquals(t *testing.T) {
-       defer os.RemoveAll("temp")
        testDB := dbm.NewDB("testdb", "leveldb", "temp")
        store := NewStore(testDB)
+       defer func() {
+               testDB.Close()
+               os.RemoveAll("temp")
+       }()
 
        block := config.GenesisBlock()
        txStatus := bc.NewTransactionStatus()
@@ -141,31 +151,41 @@ func TestLoadBlockIndexEquals(t *testing.T) {
                t.Errorf("got block index:%v, expect block index:%v", index, expectBlockIndex)
        }
 }
+
 func TestSaveChainStatus(t *testing.T) {
-       defer os.RemoveAll("temp")
        testDB := dbm.NewDB("testdb", "leveldb", "temp")
+       defer func() {
+               testDB.Close()
+               os.RemoveAll("temp")
+       }()
+
        store := NewStore(testDB)
 
        node := &state.BlockNode{Height: 100, Hash: bc.Hash{V0: 0, V1: 1, V2: 2, V3: 3}}
        view := &state.UtxoViewpoint{
                Entries: map[bc.Hash]*storage.UtxoEntry{
-                       bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{IsCoinBase: false, BlockHeight: 100, Spent: false},
-                       bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{IsCoinBase: true, BlockHeight: 100, Spent: true},
-                       bc.Hash{V0: 1, V1: 1, V2: 3, V3: 4}: &storage.UtxoEntry{IsCoinBase: false, BlockHeight: 100, Spent: true},
+                       bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.NormalUTXOType, BlockHeight: 100, Spent: false},
+                       bc.Hash{V0: 1, V1: 2, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.CoinbaseUTXOType, BlockHeight: 100, Spent: true},
+                       bc.Hash{V0: 1, V1: 1, V2: 3, V3: 4}: &storage.UtxoEntry{Type: storage.NormalUTXOType, BlockHeight: 100, Spent: true},
+                       bc.Hash{V0: 1, V1: 1, V2: 3, V3: 5}: &storage.UtxoEntry{Type: storage.CrosschainUTXOType, BlockHeight: 100, Spent: false},
+                       bc.Hash{V0: 1, V1: 1, V2: 3, V3: 6}: &storage.UtxoEntry{Type: storage.CrosschainUTXOType, BlockHeight: 100, Spent: true},
                },
        }
 
-       if err := store.SaveChainStatus(node, view); err != nil {
+       if err := store.SaveChainStatus(node, node, view, map[uint64]*state.VoteResult{}); err != nil {
                t.Fatal(err)
        }
 
-       expectStatus := &protocol.BlockStoreState{Height: node.Height, Hash: &node.Hash}
+       expectStatus := &protocol.BlockStoreState{Height: node.Height, Hash: &node.Hash, IrreversibleHeight: node.Height, IrreversibleHash: &node.Hash}
        if !testutil.DeepEqual(store.GetStoreStatus(), expectStatus) {
                t.Errorf("got block status:%v, expect block status:%v", store.GetStoreStatus(), expectStatus)
        }
 
        for hash, utxo := range view.Entries {
-               if utxo.Spent && !utxo.IsCoinBase {
+               if (utxo.Type == storage.NormalUTXOType) && utxo.Spent {
+                       continue
+               }
+               if (utxo.Type == storage.CrosschainUTXOType) && (!utxo.Spent) {
                        continue
                }
 
@@ -181,8 +201,12 @@ func TestSaveChainStatus(t *testing.T) {
 }
 
 func TestSaveBlock(t *testing.T) {
-       defer os.RemoveAll("temp")
        testDB := dbm.NewDB("testdb", "leveldb", "temp")
+       defer func() {
+               testDB.Close()
+               os.RemoveAll("temp")
+       }()
+
        store := NewStore(testDB)
 
        block := config.GenesisBlock()