OSDN Git Service

feat: add processIssuing (#152)
[bytom/vapor.git] / vendor / github.com / bytom / protocol / bc / types / txinput_test.go
1 package types
2
3 import (
4         "bytes"
5         "encoding/hex"
6         "strings"
7         "testing"
8
9         "github.com/davecgh/go-spew/spew"
10
11         "github.com/bytom/encoding/blockchain"
12         "github.com/bytom/testutil"
13 )
14
15 func TestSerializationIssuance(t *testing.T) {
16         arguments := [][]byte{
17                 []byte("arguments1"),
18                 []byte("arguments2"),
19         }
20         issuance := NewIssuanceInput([]byte("nonce"), 254354, []byte("issuanceProgram"), arguments, []byte("assetDefinition"))
21
22         wantHex := strings.Join([]string{
23                 "01",         // asset version
24                 "2a",         // serialization length
25                 "00",         // issuance type flag
26                 "05",         // nonce length
27                 "6e6f6e6365", // nonce
28                 "a69849e11add96ac7053aad22ba2349a4abf5feb0475a0afcadff4e128be76cf", // assetID
29                 "92c30f", // amount
30                 "38",     // input witness length
31                 "0f",     // asset definition length
32                 "6173736574446566696e6974696f6e", // asset definition
33                 "01", // vm version
34                 "0f", // issuanceProgram length
35                 "69737375616e636550726f6772616d", // issuance program
36                 "02", // argument array length
37                 "0a", // first argument length
38                 "617267756d656e747331", // first argument data
39                 "0a", // second argument length
40                 "617267756d656e747332", // second argument data
41         }, "")
42
43         // Test convert struct to hex
44         var buffer bytes.Buffer
45         if err := issuance.writeTo(&buffer); err != nil {
46                 t.Fatal(err)
47         }
48
49         gotHex := hex.EncodeToString(buffer.Bytes())
50         if gotHex != wantHex {
51                 t.Errorf("serialization bytes = %s want %s", gotHex, wantHex)
52         }
53
54         // Test convert hex to struct
55         var gotIssuance TxInput
56         decodeHex, err := hex.DecodeString(wantHex)
57         if err != nil {
58                 t.Fatal(err)
59         }
60
61         if err := gotIssuance.readFrom(blockchain.NewReader(decodeHex)); err != nil {
62                 t.Fatal(err)
63         }
64
65         if !testutil.DeepEqual(*issuance, gotIssuance) {
66                 t.Errorf("expected marshaled/unmarshaled txinput to be:\n%sgot:\n%s", spew.Sdump(*issuance), spew.Sdump(gotIssuance))
67         }
68 }
69
70 func TestSerializationSpend(t *testing.T) {
71         arguments := [][]byte{
72                 []byte("arguments1"),
73                 []byte("arguments2"),
74         }
75         spend := NewSpendInput(arguments, testutil.MustDecodeHash("fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409"), testutil.MustDecodeAsset("fe9791d71b67ee62515e08723c061b5ccb952a80d804417c8aeedf7f633c524a"), 254354, 3, []byte("spendProgram"))
76
77         wantHex := strings.Join([]string{
78                 "01", // asset version
79                 "54", // input commitment length
80                 "01", // spend type flag
81                 "52", // spend commitment length
82                 "fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409", // source id
83                 "fe9791d71b67ee62515e08723c061b5ccb952a80d804417c8aeedf7f633c524a", // assetID
84                 "92c30f", // amount
85                 "03",     // source position
86                 "01",     // vm version
87                 "0c",     // spend program length
88                 "7370656e6450726f6772616d", // spend program
89                 "17", // witness length
90                 "02", // argument array length
91                 "0a", // first argument length
92                 "617267756d656e747331", // first argument data
93                 "0a", // second argument length
94                 "617267756d656e747332", // second argument data
95         }, "")
96
97         // Test convert struct to hex
98         var buffer bytes.Buffer
99         if err := spend.writeTo(&buffer); err != nil {
100                 t.Fatal(err)
101         }
102
103         gotHex := hex.EncodeToString(buffer.Bytes())
104         if gotHex != wantHex {
105                 t.Errorf("serialization bytes = %s want %s", gotHex, wantHex)
106         }
107
108         // Test convert hex to struct
109         var gotSpend TxInput
110         decodeHex, err := hex.DecodeString(wantHex)
111         if err != nil {
112                 t.Fatal(err)
113         }
114
115         if err := gotSpend.readFrom(blockchain.NewReader(decodeHex)); err != nil {
116                 t.Fatal(err)
117         }
118
119         if !testutil.DeepEqual(*spend, gotSpend) {
120                 t.Errorf("expected marshaled/unmarshaled txinput to be:\n%sgot:\n%s", spew.Sdump(*spend), spew.Sdump(gotSpend))
121         }
122 }
123
124 func TestSerializationCoinbase(t *testing.T) {
125         coinbase := NewCoinbaseInput([]byte("arbitrary"))
126         wantHex := strings.Join([]string{
127                 "01",                 // asset version
128                 "0b",                 // input commitment length
129                 "02",                 // coinbase type flag
130                 "09",                 // arbitrary length
131                 "617262697472617279", // arbitrary data
132                 "00",                 // witness length
133         }, "")
134
135         // Test convert struct to hex
136         var buffer bytes.Buffer
137         if err := coinbase.writeTo(&buffer); err != nil {
138                 t.Fatal(err)
139         }
140
141         gotHex := hex.EncodeToString(buffer.Bytes())
142         if gotHex != wantHex {
143                 t.Errorf("serialization bytes = %s want %s", gotHex, wantHex)
144         }
145
146         // Test convert hex to struct
147         var gotCoinbase TxInput
148         decodeHex, err := hex.DecodeString(wantHex)
149         if err != nil {
150                 t.Fatal(err)
151         }
152
153         if err := gotCoinbase.readFrom(blockchain.NewReader(decodeHex)); err != nil {
154                 t.Fatal(err)
155         }
156
157         if !testutil.DeepEqual(*coinbase, gotCoinbase) {
158                 t.Errorf("expected marshaled/unmarshaled txinput to be:\n%sgot:\n%s", spew.Sdump(*coinbase), spew.Sdump(gotCoinbase))
159         }
160 }