OSDN Git Service

uxto view test (#396)
[bytom/vapor.git] / database / utxo_view_test.go
index 525624a..b776b54 100644 (file)
@@ -14,7 +14,10 @@ import (
 func TestSaveUtxoView(t *testing.T) {
        testDB := dbm.NewDB("testdb", "leveldb", "temp")
        batch := testDB.NewBatch()
-       defer os.RemoveAll("temp")
+       defer func() {
+               testDB.Close()
+               os.RemoveAll("temp")
+       }()
 
        cases := []struct {
                hash      bc.Hash
@@ -23,24 +26,44 @@ func TestSaveUtxoView(t *testing.T) {
        }{
                {
                        hash:      bc.Hash{V0: 0},
-                       utxoEntry: storage.NewUtxoEntry(true, 0, true),
+                       utxoEntry: storage.NewUtxoEntry(storage.CoinbaseUTXOType, 0, true),
                        exist:     true,
                },
                {
                        hash:      bc.Hash{V0: 1},
-                       utxoEntry: storage.NewUtxoEntry(true, 0, false),
+                       utxoEntry: storage.NewUtxoEntry(storage.CoinbaseUTXOType, 0, false),
                        exist:     true,
                },
                {
                        hash:      bc.Hash{V0: 2},
-                       utxoEntry: storage.NewUtxoEntry(false, 0, false),
+                       utxoEntry: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
                        exist:     true,
                },
                {
                        hash:      bc.Hash{V0: 3},
-                       utxoEntry: storage.NewUtxoEntry(false, 0, true),
+                       utxoEntry: storage.NewUtxoEntry(storage.NormalUTXOType, 0, true),
                        exist:     false,
                },
+               {
+                       hash:      bc.Hash{V0: 4},
+                       utxoEntry: storage.NewUtxoEntry(storage.CrosschainUTXOType, 0, true),
+                       exist:     true,
+               },
+               {
+                       hash:      bc.Hash{V0: 5},
+                       utxoEntry: storage.NewUtxoEntry(storage.CrosschainUTXOType, 0, false),
+                       exist:     false,
+               },
+               {
+                       hash:      bc.Hash{V0: 6},
+                       utxoEntry: storage.NewUtxoEntry(storage.VoteUTXOType, 0, true),
+                       exist:     true,
+               },
+               {
+                       hash:      bc.Hash{V0: 7},
+                       utxoEntry: storage.NewUtxoEntry(storage.VoteUTXOType, 0, false),
+                       exist:     true,
+               },
        }
 
        view := state.NewUtxoViewpoint()
@@ -69,13 +92,20 @@ func TestSaveUtxoView(t *testing.T) {
 
 func TestGetTransactionsUtxo(t *testing.T) {
        testDB := dbm.NewDB("testdb", "leveldb", "temp")
-       defer os.RemoveAll("temp")
+       defer func() {
+               testDB.Close()
+               os.RemoveAll("temp")
+       }()
 
        batch := testDB.NewBatch()
        inputView := state.NewUtxoViewpoint()
        for i := 0; i <= 2; i++ {
-               inputView.Entries[bc.Hash{V0: uint64(i)}] = storage.NewUtxoEntry(false, uint64(i), false)
+               inputView.Entries[bc.Hash{V0: uint64(i)}] = storage.NewUtxoEntry(storage.NormalUTXOType, uint64(i), false)
        }
+       inputView.Entries[bc.Hash{V0: uint64(3)}] = storage.NewUtxoEntry(storage.CrosschainUTXOType, uint64(3), true)
+       inputView.Entries[bc.Hash{V0: uint64(4)}] = storage.NewUtxoEntry(storage.CoinbaseUTXOType, uint64(4), false)
+       inputView.Entries[bc.Hash{V0: uint64(5)}] = storage.NewUtxoEntry(storage.VoteUTXOType, uint64(5), false)
+
        saveUtxoView(batch, inputView)
        batch.Write()
 
@@ -99,13 +129,50 @@ func TestGetTransactionsUtxo(t *testing.T) {
                {
                        txs: []*bc.Tx{
                                &bc.Tx{
+                                       MainchainOutputIDs: []bc.Hash{
+                                               bc.Hash{V0: 10},
+                                               bc.Hash{V0: 3},
+                                       },
+                               },
+                       },
+                       inputView: state.NewUtxoViewpoint(),
+                       fetchView: &state.UtxoViewpoint{
+                               Entries: map[bc.Hash]*storage.UtxoEntry{
+                                       bc.Hash{V0: 10}: storage.NewUtxoEntry(storage.CrosschainUTXOType, 0, false),
+                                       bc.Hash{V0: 3}:  storage.NewUtxoEntry(storage.CrosschainUTXOType, 3, true),
+                               },
+                       },
+                       err: false,
+               },
+               {
+                       txs: []*bc.Tx{
+                               &bc.Tx{
+                                       SpentOutputIDs: []bc.Hash{
+                                               bc.Hash{V0: 4},
+                                               bc.Hash{V0: 5},
+                                               bc.Hash{V0: 6},//no spentOutputID store
+                                       },
+                               },
+                       },
+                       inputView: state.NewUtxoViewpoint(),
+                       fetchView: &state.UtxoViewpoint{
+                               Entries: map[bc.Hash]*storage.UtxoEntry{
+                                       bc.Hash{V0: 4}: storage.NewUtxoEntry(storage.CoinbaseUTXOType, 4, false),
+                                       bc.Hash{V0: 5}: storage.NewUtxoEntry(storage.VoteUTXOType, 5, false),
+                               },
+                       },
+                       err: false,
+               },
+               {
+                       txs: []*bc.Tx{
+                               &bc.Tx{
                                        SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
                                },
                        },
                        inputView: state.NewUtxoViewpoint(),
                        fetchView: &state.UtxoViewpoint{
                                Entries: map[bc.Hash]*storage.UtxoEntry{
-                                       bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
+                                       bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
                                },
                        },
                        err: false,
@@ -122,8 +189,8 @@ func TestGetTransactionsUtxo(t *testing.T) {
                        inputView: state.NewUtxoViewpoint(),
                        fetchView: &state.UtxoViewpoint{
                                Entries: map[bc.Hash]*storage.UtxoEntry{
-                                       bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
-                                       bc.Hash{V0: 1}: storage.NewUtxoEntry(false, 1, false),
+                                       bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
+                                       bc.Hash{V0: 1}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
                                },
                        },
                        err: false,
@@ -145,9 +212,9 @@ func TestGetTransactionsUtxo(t *testing.T) {
                        inputView: state.NewUtxoViewpoint(),
                        fetchView: &state.UtxoViewpoint{
                                Entries: map[bc.Hash]*storage.UtxoEntry{
-                                       bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
-                                       bc.Hash{V0: 1}: storage.NewUtxoEntry(false, 1, false),
-                                       bc.Hash{V0: 2}: storage.NewUtxoEntry(false, 2, false),
+                                       bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
+                                       bc.Hash{V0: 1}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
+                                       bc.Hash{V0: 2}: storage.NewUtxoEntry(storage.NormalUTXOType, 2, false),
                                },
                        },
                        err: false,
@@ -160,12 +227,12 @@ func TestGetTransactionsUtxo(t *testing.T) {
                        },
                        inputView: &state.UtxoViewpoint{
                                Entries: map[bc.Hash]*storage.UtxoEntry{
-                                       bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 1, false),
+                                       bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
                                },
                        },
                        fetchView: &state.UtxoViewpoint{
                                Entries: map[bc.Hash]*storage.UtxoEntry{
-                                       bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 1, false),
+                                       bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
                                },
                        },
                        err: false,