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(true, 0, true),
31 utxoEntry: storage.NewUtxoEntry(true, 0, false),
36 utxoEntry: storage.NewUtxoEntry(false, 0, false),
41 utxoEntry: storage.NewUtxoEntry(false, 0, true),
46 view := state.NewUtxoViewpoint()
47 for _, c := range cases {
48 view.Entries[c.hash] = c.utxoEntry
51 saveUtxoView(batch, view)
54 for _, c := range cases {
55 entry, err := getUtxo(testDB, &c.hash)
59 t.Errorf("%v should be unexisted, but it's in the db", c)
64 if !testutil.DeepEqual(entry, c.utxoEntry) {
65 t.Errorf("%v utxo in the db isn't match", c)
70 func TestGetTransactionsUtxo(t *testing.T) {
71 testDB := dbm.NewDB("testdb", "leveldb", "temp")
72 defer os.RemoveAll("temp")
74 batch := testDB.NewBatch()
75 inputView := state.NewUtxoViewpoint()
76 for i := 0; i <= 2; i++ {
77 inputView.Entries[bc.Hash{V0: uint64(i)}] = storage.NewUtxoEntry(false, uint64(i), false)
79 saveUtxoView(batch, inputView)
84 inputView *state.UtxoViewpoint
85 fetchView *state.UtxoViewpoint
92 SpentOutputIDs: []bc.Hash{bc.Hash{V0: 10}},
95 inputView: state.NewUtxoViewpoint(),
96 fetchView: state.NewUtxoViewpoint(),
102 SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
105 inputView: state.NewUtxoViewpoint(),
106 fetchView: &state.UtxoViewpoint{
107 Entries: map[bc.Hash]*storage.UtxoEntry{
108 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
116 SpentOutputIDs: []bc.Hash{
122 inputView: state.NewUtxoViewpoint(),
123 fetchView: &state.UtxoViewpoint{
124 Entries: map[bc.Hash]*storage.UtxoEntry{
125 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
126 bc.Hash{V0: 1}: storage.NewUtxoEntry(false, 1, false),
134 SpentOutputIDs: []bc.Hash{
140 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(false, 0, false),
149 bc.Hash{V0: 1}: storage.NewUtxoEntry(false, 1, false),
150 bc.Hash{V0: 2}: storage.NewUtxoEntry(false, 2, false),
158 SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
161 inputView: &state.UtxoViewpoint{
162 Entries: map[bc.Hash]*storage.UtxoEntry{
163 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 1, false),
166 fetchView: &state.UtxoViewpoint{
167 Entries: map[bc.Hash]*storage.UtxoEntry{
168 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 1, false),
175 for i, c := range cases {
176 if err := getTransactionsUtxo(testDB, c.inputView, c.txs); c.err != (err != nil) {
177 t.Errorf("test case %d, want err = %v, get err = %v", i, c.err, err)
179 if !testutil.DeepEqual(c.inputView, c.fetchView) {
180 t.Errorf("test case %d, want %v, get %v", i, c.fetchView, c.inputView)