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")
100 batch := testDB.NewBatch()
101 inputView := state.NewUtxoViewpoint()
102 for i := 0; i <= 2; i++ {
103 inputView.Entries[bc.Hash{V0: uint64(i)}] = storage.NewUtxoEntry(storage.NormalUTXOType, uint64(i), false)
105 inputView.Entries[bc.Hash{V0: uint64(3)}] = storage.NewUtxoEntry(storage.CrosschainUTXOType, uint64(3), true)
106 inputView.Entries[bc.Hash{V0: uint64(4)}] = storage.NewUtxoEntry(storage.CoinbaseUTXOType, uint64(4), false)
107 inputView.Entries[bc.Hash{V0: uint64(5)}] = storage.NewUtxoEntry(storage.VoteUTXOType, uint64(5), false)
109 saveUtxoView(batch, inputView)
114 inputView *state.UtxoViewpoint
115 fetchView *state.UtxoViewpoint
122 SpentOutputIDs: []bc.Hash{bc.Hash{V0: 10}},
125 inputView: state.NewUtxoViewpoint(),
126 fetchView: state.NewUtxoViewpoint(),
132 MainchainOutputIDs: []bc.Hash{
138 inputView: state.NewUtxoViewpoint(),
139 fetchView: &state.UtxoViewpoint{
140 Entries: map[bc.Hash]*storage.UtxoEntry{
141 bc.Hash{V0: 10}: storage.NewUtxoEntry(storage.CrosschainUTXOType, 0, false),
142 bc.Hash{V0: 3}: storage.NewUtxoEntry(storage.CrosschainUTXOType, 3, true),
150 SpentOutputIDs: []bc.Hash{
153 bc.Hash{V0: 6},//no spentOutputID store
157 inputView: state.NewUtxoViewpoint(),
158 fetchView: &state.UtxoViewpoint{
159 Entries: map[bc.Hash]*storage.UtxoEntry{
160 bc.Hash{V0: 4}: storage.NewUtxoEntry(storage.CoinbaseUTXOType, 4, false),
161 bc.Hash{V0: 5}: storage.NewUtxoEntry(storage.VoteUTXOType, 5, false),
169 SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
172 inputView: state.NewUtxoViewpoint(),
173 fetchView: &state.UtxoViewpoint{
174 Entries: map[bc.Hash]*storage.UtxoEntry{
175 bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
183 SpentOutputIDs: []bc.Hash{
189 inputView: state.NewUtxoViewpoint(),
190 fetchView: &state.UtxoViewpoint{
191 Entries: map[bc.Hash]*storage.UtxoEntry{
192 bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
193 bc.Hash{V0: 1}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
201 SpentOutputIDs: []bc.Hash{
207 SpentOutputIDs: []bc.Hash{
212 inputView: state.NewUtxoViewpoint(),
213 fetchView: &state.UtxoViewpoint{
214 Entries: map[bc.Hash]*storage.UtxoEntry{
215 bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 0, false),
216 bc.Hash{V0: 1}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
217 bc.Hash{V0: 2}: storage.NewUtxoEntry(storage.NormalUTXOType, 2, false),
225 SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
228 inputView: &state.UtxoViewpoint{
229 Entries: map[bc.Hash]*storage.UtxoEntry{
230 bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
233 fetchView: &state.UtxoViewpoint{
234 Entries: map[bc.Hash]*storage.UtxoEntry{
235 bc.Hash{V0: 0}: storage.NewUtxoEntry(storage.NormalUTXOType, 1, false),
242 for i, c := range cases {
243 if err := getTransactionsUtxo(testDB, c.inputView, c.txs); c.err != (err != nil) {
244 t.Errorf("test case %d, want err = %v, get err = %v", i, c.err, err)
246 if !testutil.DeepEqual(c.inputView, c.fetchView) {
247 t.Errorf("test case %d, want %v, get %v", i, c.fetchView, c.inputView)