8 "github.com/bytom/protocol/bc"
9 "github.com/bytom/protocol/bc/bctest"
10 "github.com/bytom/protocol/bc/legacy"
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},
22 RefDataHash: bc.Hash{},
24 spentOutputID, err := legacy.ComputeOutputID(&sc)
30 snap.Tree.Insert(spentOutputID.Bytes())
32 tx := legacy.MapTx(&legacy.TxData{
34 Inputs: []*legacy.TxInput{
35 legacy.NewSpendInput(nil, sourceID, assetID, 100, 0, nil, bc.Hash{}, nil),
37 Outputs: []*legacy.TxOutput{},
40 // Apply the spend transaction.
41 err = snap.ApplyTx(tx)
45 if snap.Tree.Contains(spentOutputID.Bytes()) {
46 t.Error("snapshot contains spent prevout")
48 err = snap.ApplyTx(tx)
50 t.Error("expected error applying spend twice, got nil")
54 func TestApplyIssuanceTwice(t *testing.T) {
56 issuance := legacy.MapTx(&bctest.NewIssuanceTx(t, bc.EmptyStringHash).TxData)
57 err := snap.ApplyTx(issuance)
61 err = snap.ApplyTx(issuance)
63 t.Errorf("expected error for duplicate nonce, got %s", err)
67 func TestCopySnapshot(t *testing.T) {
69 err := snap.ApplyTx(legacy.MapTx(&bctest.NewIssuanceTx(t, bc.EmptyStringHash).TxData))
74 if !reflect.DeepEqual(dupe, snap) {
75 t.Errorf("got %#v, want %#v", dupe, snap)
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) {})
84 err := snap.ApplyTx(legacy.MapTx(&issuance.TxData))
88 if n := len(snap.Nonces); n != 1 {
89 t.Errorf("got %d nonces, want 1", n)
92 // Land a block later than the issuance's max time.
93 block := &legacy.Block{
94 BlockHeader: legacy.BlockHeader{
95 TimestampMS: maxTime + 1,
98 err = snap.ApplyBlock(legacy.MapBlock(block))
102 if n := len(snap.Nonces); n != 1 {
103 t.Errorf("got %d nonces, want 1", n)