7 "github.com/bytom/bytom/consensus"
8 "github.com/bytom/bytom/database/storage"
9 "github.com/bytom/bytom/protocol/bc"
10 "github.com/bytom/bytom/testutil"
13 var defaultEntry = map[bc.Hash]bc.Entry{
14 bc.Hash{V0: 0}: &bc.OriginalOutput{
15 Source: &bc.ValueSource{
16 Value: &bc.AssetAmount{
17 AssetId: &bc.AssetID{V0: 0},
23 var gasOnlyTxEntry = map[bc.Hash]bc.Entry{
24 bc.Hash{V1: 0}: &bc.OriginalOutput{
25 Source: &bc.ValueSource{
26 Value: &bc.AssetAmount{
27 AssetId: consensus.BTMAssetID,
31 bc.Hash{V1: 1}: &bc.OriginalOutput{
32 Source: &bc.ValueSource{
33 Value: &bc.AssetAmount{
34 AssetId: &bc.AssetID{V0: 999},
40 func TestApplyBlock(t *testing.T) {
43 inputView *UtxoViewpoint
44 fetchView *UtxoViewpoint
49 // can't find prevout in tx entries
51 BlockHeader: &bc.BlockHeader{},
52 Transactions: []*bc.Tx{
54 SpentOutputIDs: []bc.Hash{
57 Entries: defaultEntry,
61 inputView: &UtxoViewpoint{
62 Entries: map[bc.Hash]*storage.UtxoEntry{
63 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
66 fetchView: NewUtxoViewpoint(),
71 BlockHeader: &bc.BlockHeader{},
72 Transactions: []*bc.Tx{
74 SpentOutputIDs: []bc.Hash{
77 Entries: defaultEntry,
81 inputView: NewUtxoViewpoint(),
82 fetchView: NewUtxoViewpoint(),
87 BlockHeader: &bc.BlockHeader{},
88 Transactions: []*bc.Tx{
90 SpentOutputIDs: []bc.Hash{
93 Entries: defaultEntry,
97 inputView: &UtxoViewpoint{
98 Entries: map[bc.Hash]*storage.UtxoEntry{
99 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, true),
106 BlockHeader: &bc.BlockHeader{},
107 Transactions: []*bc.Tx{
109 TxHeader: &bc.TxHeader{
110 ResultIds: []*bc.Hash{},
112 SpentOutputIDs: []bc.Hash{
115 Entries: defaultEntry,
119 inputView: &UtxoViewpoint{
120 Entries: map[bc.Hash]*storage.UtxoEntry{
121 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
124 fetchView: &UtxoViewpoint{
125 Entries: map[bc.Hash]*storage.UtxoEntry{
126 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, true),
133 BlockHeader: &bc.BlockHeader{
136 Transactions: []*bc.Tx{
138 TxHeader: &bc.TxHeader{
139 ResultIds: []*bc.Hash{},
141 SpentOutputIDs: []bc.Hash{
144 Entries: defaultEntry,
148 inputView: &UtxoViewpoint{
149 Entries: map[bc.Hash]*storage.UtxoEntry{
150 bc.Hash{V0: 0}: storage.NewUtxoEntry(true, 0, false),
153 fetchView: &UtxoViewpoint{
154 Entries: map[bc.Hash]*storage.UtxoEntry{
155 bc.Hash{V0: 0}: storage.NewUtxoEntry(true, 0, true),
162 BlockHeader: &bc.BlockHeader{
165 Transactions: []*bc.Tx{
167 TxHeader: &bc.TxHeader{
168 ResultIds: []*bc.Hash{},
170 SpentOutputIDs: []bc.Hash{
173 Entries: defaultEntry,
177 inputView: &UtxoViewpoint{
178 Entries: map[bc.Hash]*storage.UtxoEntry{
179 bc.Hash{V0: 0}: storage.NewUtxoEntry(true, 0, false),
182 fetchView: &UtxoViewpoint{
183 Entries: map[bc.Hash]*storage.UtxoEntry{
184 bc.Hash{V0: 0}: storage.NewUtxoEntry(true, 0, true),
190 // output will be store
192 BlockHeader: &bc.BlockHeader{},
193 Transactions: []*bc.Tx{
195 TxHeader: &bc.TxHeader{
196 ResultIds: []*bc.Hash{
200 SpentOutputIDs: []bc.Hash{},
201 Entries: defaultEntry,
205 inputView: NewUtxoViewpoint(),
206 fetchView: &UtxoViewpoint{
207 Entries: map[bc.Hash]*storage.UtxoEntry{
208 bc.Hash{V0: 0}: storage.NewUtxoEntry(true, 0, false),
214 // non-btm asset spent input will be spent
216 BlockHeader: &bc.BlockHeader{},
217 Transactions: []*bc.Tx{
219 TxHeader: &bc.TxHeader{
220 ResultIds: []*bc.Hash{},
222 SpentOutputIDs: []bc.Hash{
226 Entries: gasOnlyTxEntry,
230 inputView: &UtxoViewpoint{
231 Entries: map[bc.Hash]*storage.UtxoEntry{
232 bc.Hash{V1: 0}: storage.NewUtxoEntry(false, 0, false),
233 bc.Hash{V1: 1}: storage.NewUtxoEntry(false, 0, false),
236 fetchView: &UtxoViewpoint{
237 Entries: map[bc.Hash]*storage.UtxoEntry{
238 bc.Hash{V1: 0}: storage.NewUtxoEntry(false, 0, true),
239 bc.Hash{V1: 1}: storage.NewUtxoEntry(false, 0, true),
245 // apply gas only tx, non-btm asset spent output will not be store
247 BlockHeader: &bc.BlockHeader{},
248 Transactions: []*bc.Tx{
250 TxHeader: &bc.TxHeader{
251 ResultIds: []*bc.Hash{
256 SpentOutputIDs: []bc.Hash{},
257 Entries: gasOnlyTxEntry,
261 inputView: NewUtxoViewpoint(),
262 fetchView: &UtxoViewpoint{
263 Entries: map[bc.Hash]*storage.UtxoEntry{
264 bc.Hash{V1: 0}: storage.NewUtxoEntry(true, 0, false),
265 bc.Hash{V1: 1}: storage.NewUtxoEntry(true, 0, false),
272 for i, c := range cases {
273 if err := c.inputView.ApplyBlock(c.block); c.err != (err != nil) {
274 t.Errorf("case #%d want err = %v, get err = %v", i, c.err, err)
279 if !testutil.DeepEqual(c.inputView, c.fetchView) {
280 t.Errorf("test case %d, want %v, get %v", i, c.fetchView, c.inputView)
285 func TestDetachBlock(t *testing.T) {
288 inputView *UtxoViewpoint
289 fetchView *UtxoViewpoint
295 BlockHeader: &bc.BlockHeader{},
296 Transactions: []*bc.Tx{
298 TxHeader: &bc.TxHeader{
299 ResultIds: []*bc.Hash{},
301 SpentOutputIDs: []bc.Hash{
304 Entries: defaultEntry,
308 inputView: NewUtxoViewpoint(),
309 fetchView: &UtxoViewpoint{
310 Entries: map[bc.Hash]*storage.UtxoEntry{
311 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
318 BlockHeader: &bc.BlockHeader{},
319 Transactions: []*bc.Tx{
321 TxHeader: &bc.TxHeader{
322 ResultIds: []*bc.Hash{
326 SpentOutputIDs: []bc.Hash{},
327 Entries: defaultEntry,
331 inputView: NewUtxoViewpoint(),
332 fetchView: &UtxoViewpoint{
333 Entries: map[bc.Hash]*storage.UtxoEntry{
334 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, true),
341 BlockHeader: &bc.BlockHeader{},
342 Transactions: []*bc.Tx{
344 TxHeader: &bc.TxHeader{
345 ResultIds: []*bc.Hash{},
347 SpentOutputIDs: []bc.Hash{
350 Entries: defaultEntry,
354 inputView: &UtxoViewpoint{
355 Entries: map[bc.Hash]*storage.UtxoEntry{
356 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
363 BlockHeader: &bc.BlockHeader{},
364 Transactions: []*bc.Tx{
366 TxHeader: &bc.TxHeader{
367 ResultIds: []*bc.Hash{},
369 SpentOutputIDs: []bc.Hash{
372 Entries: defaultEntry,
376 inputView: &UtxoViewpoint{
377 Entries: map[bc.Hash]*storage.UtxoEntry{
378 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, true),
381 fetchView: &UtxoViewpoint{
382 Entries: map[bc.Hash]*storage.UtxoEntry{
383 bc.Hash{V0: 0}: storage.NewUtxoEntry(false, 0, false),
390 for i, c := range cases {
391 if err := c.inputView.DetachBlock(c.block); c.err != (err != nil) {
392 t.Errorf("case %d want err = %v, get err = %v", i, c.err, err)
397 if !testutil.DeepEqual(c.inputView, c.fetchView) {
398 for hash, entry := range c.inputView.Entries {
399 fmt.Println(hash.String(), ":", entry.String())
402 for hash, entry := range c.fetchView.Entries {
403 fmt.Println(hash.String(), ":", entry.String())
406 t.Errorf("test case %d, want %v, get %v", i, c.fetchView, c.inputView)