OSDN Git Service

Dev (#148)
[bytom/bytom.git] / protocol / state / snapshot_test.go
1 package state
2
3 import (
4         "reflect"
5         "testing"
6         "time"
7
8         "github.com/bytom/protocol/bc"
9         "github.com/bytom/protocol/bc/bctest"
10         "github.com/bytom/protocol/bc/legacy"
11 )
12
13 func TestApplyTxSpend(t *testing.T) {
14         assetID := bc.AssetID{}
15         sourceID := bc.NewHash([32]byte{0x01, 0x02, 0x03})
16         sc := legacy.SpendCommitment{
17                 AssetAmount:    bc.AssetAmount{AssetId: &assetID, Amount: 100},
18                 SourceID:       sourceID,
19                 SourcePosition: 0,
20                 VMVersion:      1,
21                 ControlProgram: nil,
22                 RefDataHash:    bc.Hash{},
23         }
24         spentOutputID, err := legacy.ComputeOutputID(&sc)
25         if err != nil {
26                 t.Fatal(err)
27         }
28
29         snap := Empty()
30         snap.Tree.Insert(spentOutputID.Bytes())
31
32         tx := legacy.MapTx(&legacy.TxData{
33                 Version: 1,
34                 Inputs: []*legacy.TxInput{
35                         legacy.NewSpendInput(nil, sourceID, assetID, 100, 0, nil, bc.Hash{}, nil),
36                 },
37                 Outputs: []*legacy.TxOutput{},
38         })
39
40         // Apply the spend transaction.
41         err = snap.ApplyTx(tx)
42         if err != nil {
43                 t.Fatal(err)
44         }
45         if snap.Tree.Contains(spentOutputID.Bytes()) {
46                 t.Error("snapshot contains spent prevout")
47         }
48         err = snap.ApplyTx(tx)
49         if err == nil {
50                 t.Error("expected error applying spend twice, got nil")
51         }
52 }
53
54 func TestApplyIssuanceTwice(t *testing.T) {
55         snap := Empty()
56         issuance := legacy.MapTx(&bctest.NewIssuanceTx(t, bc.EmptyStringHash).TxData)
57         err := snap.ApplyTx(issuance)
58         if err != nil {
59                 t.Fatal(err)
60         }
61         err = snap.ApplyTx(issuance)
62         if err == nil {
63                 t.Errorf("expected error for duplicate nonce, got %s", err)
64         }
65 }
66
67 func TestCopySnapshot(t *testing.T) {
68         snap := Empty()
69         err := snap.ApplyTx(legacy.MapTx(&bctest.NewIssuanceTx(t, bc.EmptyStringHash).TxData))
70         if err != nil {
71                 t.Fatal(err)
72         }
73         dupe := Copy(snap)
74         if !reflect.DeepEqual(dupe, snap) {
75                 t.Errorf("got %#v, want %#v", dupe, snap)
76         }
77 }
78
79 func TestApplyBlock(t *testing.T) {
80         // Setup a snapshot with a nonce with a known expiry.
81         maxTime := bc.Millis(time.Now().Add(5 * time.Minute))
82         issuance := bctest.NewIssuanceTx(t, bc.EmptyStringHash, func(tx *legacy.Tx) {})
83         snap := Empty()
84         err := snap.ApplyTx(legacy.MapTx(&issuance.TxData))
85         if err != nil {
86                 t.Fatal(err)
87         }
88         if n := len(snap.Nonces); n != 1 {
89                 t.Errorf("got %d nonces, want 1", n)
90         }
91
92         // Land a block later than the issuance's max time.
93         block := &legacy.Block{
94                 BlockHeader: legacy.BlockHeader{
95                         TimestampMS: maxTime + 1,
96                 },
97         }
98         err = snap.ApplyBlock(legacy.MapBlock(block))
99         if err != nil {
100                 t.Fatal(err)
101         }
102         if n := len(snap.Nonces); n != 1 {
103                 t.Errorf("got %d nonces, want 1", n)
104         }
105 }