7 dbm "github.com/vapor/database/leveldb"
8 "github.com/vapor/database/storage"
9 "github.com/vapor/protocol/bc"
10 "github.com/vapor/protocol/state"
11 "github.com/vapor/testutil"
14 func TestSaveUtxoView(t *testing.T) {
15 testDB := dbm.NewDB("testdb", "leveldb", "temp")
16 batch := testDB.NewBatch()
17 defer os.RemoveAll("temp")
21 utxoEntry *storage.UtxoEntry
26 utxoEntry: storage.NewUtxoEntry(storage.CoinbaseUTXOType, 0, true),
31 utxoEntry: storage.NewUtxoEntry(storage.CoinbaseUTXOType, 0, false),
36 utxoEntry: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
41 utxoEntry: storage.NewUtxoEntry(storage.NormalUTXOType, 0, true),
46 utxoEntry: storage.NewUtxoEntry(storage.CrosschainUTXOType, 0, true),
51 utxoEntry: storage.NewUtxoEntry(storage.CrosschainUTXOType, 0, false),
56 view := state.NewUtxoViewpoint()
57 for _, c := range cases {
58 view.Entries[c.hash] = c.utxoEntry
61 saveUtxoView(batch, view)
64 for _, c := range cases {
65 entry, err := getUtxo(testDB, &c.hash)
69 t.Errorf("%v should be unexisted, but it's in the db", c)
74 if !testutil.DeepEqual(entry, c.utxoEntry) {
75 t.Errorf("%v utxo in the db isn't match", c)
80 func TestGetTransactionsUtxo(t *testing.T) {
81 testDB := dbm.NewDB("testdb", "leveldb", "temp")
82 defer os.RemoveAll("temp")
84 batch := testDB.NewBatch()
85 inputView := state.NewUtxoViewpoint()
86 for i := 0; i <= 2; i++ {
87 inputView.Entries[bc.Hash{V0: uint64(i)}] = storage.NewUtxoEntry(storage.NormalUTXOType, uint64(i), false)
89 saveUtxoView(batch, inputView)
94 inputView *state.UtxoViewpoint
95 fetchView *state.UtxoViewpoint
102 SpentOutputIDs: []bc.Hash{bc.Hash{V0: 10}},
105 inputView: state.NewUtxoViewpoint(),
106 fetchView: state.NewUtxoViewpoint(),
112 SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
115 inputView: state.NewUtxoViewpoint(),
116 fetchView: &state.UtxoViewpoint{
117 Entries: map[bc.Hash]*storage.UtxoEntry{
118 bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
126 SpentOutputIDs: []bc.Hash{
132 inputView: state.NewUtxoViewpoint(),
133 fetchView: &state.UtxoViewpoint{
134 Entries: map[bc.Hash]*storage.UtxoEntry{
135 bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
136 bc.Hash{V0: 1}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
144 SpentOutputIDs: []bc.Hash{
150 SpentOutputIDs: []bc.Hash{
155 inputView: state.NewUtxoViewpoint(),
156 fetchView: &state.UtxoViewpoint{
157 Entries: map[bc.Hash]*storage.UtxoEntry{
158 bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
159 bc.Hash{V0: 1}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
160 bc.Hash{V0: 2}: storage.NewUtxoEntry(storage.NormalUTXOType, 2, false),
168 SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
171 inputView: &state.UtxoViewpoint{
172 Entries: map[bc.Hash]*storage.UtxoEntry{
173 bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
176 fetchView: &state.UtxoViewpoint{
177 Entries: map[bc.Hash]*storage.UtxoEntry{
178 bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
185 for i, c := range cases {
186 if err := getTransactionsUtxo(testDB, c.inputView, c.txs); c.err != (err != nil) {
187 t.Errorf("test case %d, want err = %v, get err = %v", i, c.err, err)
189 if !testutil.DeepEqual(c.inputView, c.fetchView) {
190 t.Errorf("test case %d, want %v, get %v", i, c.fetchView, c.inputView)