7 dbm "github.com/tendermint/tmlibs/db"
9 "github.com/vapor/database/storage"
10 "github.com/vapor/protocol/bc"
11 "github.com/vapor/protocol/state"
12 "github.com/vapor/testutil"
15 func TestSaveUtxoView(t *testing.T) {
16 testDB := dbm.NewDB("testdb", "leveldb", "temp")
17 batch := testDB.NewBatch()
18 defer os.RemoveAll("temp")
22 utxoEntry *storage.UtxoEntry
27 utxoEntry: storage.NewUtxoEntry(true, 0, true),
32 utxoEntry: storage.NewUtxoEntry(true, 0, false),
37 utxoEntry: storage.NewUtxoEntry(false, 0, false),
42 utxoEntry: storage.NewUtxoEntry(false, 0, true),
47 view := state.NewUtxoViewpoint()
48 for _, c := range cases {
49 view.Entries[c.hash] = c.utxoEntry
52 saveUtxoView(batch, view)
55 for _, c := range cases {
56 entry, err := getUtxo(testDB, &c.hash)
60 t.Errorf("%v should be unexisted, but it's in the db", c)
65 if !testutil.DeepEqual(entry, c.utxoEntry) {
66 t.Errorf("%v utxo in the db isn't match", c)
71 func TestGetTransactionsUtxo(t *testing.T) {
72 testDB := dbm.NewDB("testdb", "leveldb", "temp")
73 defer os.RemoveAll("temp")
75 batch := testDB.NewBatch()
76 inputView := state.NewUtxoViewpoint()
77 for i := 0; i <= 2; i++ {
78 inputView.Entries[bc.Hash{V0: uint64(i)}] = storage.NewUtxoEntry(false, uint64(i), false)
80 saveUtxoView(batch, inputView)
85 inputView *state.UtxoViewpoint
86 fetchView *state.UtxoViewpoint
93 SpentOutputIDs: []bc.Hash{bc.Hash{V0: 10}},
96 inputView: state.NewUtxoViewpoint(),
97 fetchView: state.NewUtxoViewpoint(),
103 SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
106 inputView: state.NewUtxoViewpoint(),
107 fetchView: &state.UtxoViewpoint{
108 Entries: map[bc.Hash]*storage.UtxoEntry{
109 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
117 SpentOutputIDs: []bc.Hash{
123 inputView: state.NewUtxoViewpoint(),
124 fetchView: &state.UtxoViewpoint{
125 Entries: map[bc.Hash]*storage.UtxoEntry{
126 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
127 bc.Hash{V0: 1}: storage.NewUtxoEntry(false, 1, false),
135 SpentOutputIDs: []bc.Hash{
141 SpentOutputIDs: []bc.Hash{
146 inputView: state.NewUtxoViewpoint(),
147 fetchView: &state.UtxoViewpoint{
148 Entries: map[bc.Hash]*storage.UtxoEntry{
149 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
150 bc.Hash{V0: 1}: storage.NewUtxoEntry(false, 1, false),
151 bc.Hash{V0: 2}: storage.NewUtxoEntry(false, 2, false),
159 SpentOutputIDs: []bc.Hash{bc.Hash{V0: 0}},
162 inputView: &state.UtxoViewpoint{
163 Entries: map[bc.Hash]*storage.UtxoEntry{
164 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 1, false),
167 fetchView: &state.UtxoViewpoint{
168 Entries: map[bc.Hash]*storage.UtxoEntry{
169 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 1, false),
176 for i, c := range cases {
177 if err := getTransactionsUtxo(testDB, c.inputView, c.txs); c.err != (err != nil) {
178 t.Errorf("test case %d, want err = %v, get err = %v", i, c.err, err)
180 if !testutil.DeepEqual(c.inputView, c.fetchView) {
181 t.Errorf("test case %d, want %v, get %v", i, c.fetchView, c.inputView)