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()
24 utxoEntry *storage.UtxoEntry
29 utxoEntry: storage.NewUtxoEntry(storage.CoinbaseUTXOType, 0, true),
34 utxoEntry: storage.NewUtxoEntry(storage.CoinbaseUTXOType, 0, false),
39 utxoEntry: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
44 utxoEntry: storage.NewUtxoEntry(storage.NormalUTXOType, 0, true),
49 utxoEntry: storage.NewUtxoEntry(storage.CrosschainUTXOType, 0, true),
54 utxoEntry: storage.NewUtxoEntry(storage.CrosschainUTXOType, 0, false),
59 utxoEntry: storage.NewUtxoEntry(storage.VoteUTXOType, 0, true),
64 utxoEntry: storage.NewUtxoEntry(storage.VoteUTXOType, 0, false),
69 view := state.NewUtxoViewpoint()
70 for _, c := range cases {
71 view.Entries[c.hash] = c.utxoEntry
74 saveUtxoView(batch, view)
77 for _, c := range cases {
78 entry, err := getUtxo(testDB, &c.hash)
82 t.Errorf("%v should be unexisted, but it's in the db", c)
87 if !testutil.DeepEqual(entry, c.utxoEntry) {
88 t.Errorf("%v utxo in the db isn't match", c)
93 func TestGetTransactionsUtxo(t *testing.T) {
94 testDB := dbm.NewDB("testdb", "leveldb", "temp")
95 defer os.RemoveAll("temp")
97 batch := testDB.NewBatch()
98 inputView := state.NewUtxoViewpoint()
99 for i := 0; i <= 2; i++ {
100 inputView.Entries[bc.Hash{V0: uint64(i)}] = storage.NewUtxoEntry(storage.NormalUTXOType, uint64(i), false)
102 saveUtxoView(batch, inputView)
107 inputView *state.UtxoViewpoint
108 fetchView *state.UtxoViewpoint
115 SpentOutputIDs: []bc.Hash{bc.Hash{V0: 10}},
118 inputView: state.NewUtxoViewpoint(),
119 fetchView: state.NewUtxoViewpoint(),
125 SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
128 inputView: state.NewUtxoViewpoint(),
129 fetchView: &state.UtxoViewpoint{
130 Entries: map[bc.Hash]*storage.UtxoEntry{
131 bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
139 SpentOutputIDs: []bc.Hash{
145 inputView: state.NewUtxoViewpoint(),
146 fetchView: &state.UtxoViewpoint{
147 Entries: map[bc.Hash]*storage.UtxoEntry{
148 bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
149 bc.Hash{V0: 1}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
157 SpentOutputIDs: []bc.Hash{
163 SpentOutputIDs: []bc.Hash{
168 inputView: state.NewUtxoViewpoint(),
169 fetchView: &state.UtxoViewpoint{
170 Entries: map[bc.Hash]*storage.UtxoEntry{
171 bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
172 bc.Hash{V0: 1}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
173 bc.Hash{V0: 2}: storage.NewUtxoEntry(storage.NormalUTXOType, 2, false),
181 SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
184 inputView: &state.UtxoViewpoint{
185 Entries: map[bc.Hash]*storage.UtxoEntry{
186 bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
189 fetchView: &state.UtxoViewpoint{
190 Entries: map[bc.Hash]*storage.UtxoEntry{
191 bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
198 for i, c := range cases {
199 if err := getTransactionsUtxo(testDB, c.inputView, c.txs); c.err != (err != nil) {
200 t.Errorf("test case %d, want err = %v, get err = %v", i, c.err, err)
202 if !testutil.DeepEqual(c.inputView, c.fetchView) {
203 t.Errorf("test case %d, want %v, get %v", i, c.fetchView, c.inputView)