github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/bc/types/txinput_test.go (about) 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/bytom/encoding/blockchain" 12 "github.com/bytom/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"), [][]byte{[]byte("stateData")}) 76 77 wantHex := strings.Join([]string{ 78 "01", // asset version 79 "5f", // input commitment length 80 "01", // spend type flag 81 "5d", // 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 "0109", // state length 90 "737461746544617461", // state 91 "17", // witness length 92 "02", // argument array length 93 "0a", // first argument length 94 "617267756d656e747331", // first argument data 95 "0a", // second argument length 96 "617267756d656e747332", // second argument data 97 }, "") 98 99 // Test convert struct to hex 100 var buffer bytes.Buffer 101 if err := spend.writeTo(&buffer); err != nil { 102 t.Fatal(err) 103 } 104 105 gotHex := hex.EncodeToString(buffer.Bytes()) 106 if gotHex != wantHex { 107 t.Errorf("serialization bytes = %s want %s", gotHex, wantHex) 108 } 109 110 // Test convert hex to struct 111 var gotSpend TxInput 112 decodeHex, err := hex.DecodeString(wantHex) 113 if err != nil { 114 t.Fatal(err) 115 } 116 117 if err := gotSpend.readFrom(blockchain.NewReader(decodeHex)); err != nil { 118 t.Fatal(err) 119 } 120 121 if !testutil.DeepEqual(*spend, gotSpend) { 122 t.Errorf("expected marshaled/unmarshaled txinput to be:\n%sgot:\n%s", spew.Sdump(*spend), spew.Sdump(gotSpend)) 123 } 124 } 125 126 func TestSerializationCoinbase(t *testing.T) { 127 coinbase := NewCoinbaseInput([]byte("arbitrary")) 128 wantHex := strings.Join([]string{ 129 "01", // asset version 130 "0b", // input commitment length 131 "02", // coinbase type flag 132 "09", // arbitrary length 133 "617262697472617279", // arbitrary data 134 "00", // witness length 135 }, "") 136 137 // Test convert struct to hex 138 var buffer bytes.Buffer 139 if err := coinbase.writeTo(&buffer); err != nil { 140 t.Fatal(err) 141 } 142 143 gotHex := hex.EncodeToString(buffer.Bytes()) 144 if gotHex != wantHex { 145 t.Errorf("serialization bytes = %s want %s", gotHex, wantHex) 146 } 147 148 // Test convert hex to struct 149 var gotCoinbase TxInput 150 decodeHex, err := hex.DecodeString(wantHex) 151 if err != nil { 152 t.Fatal(err) 153 } 154 155 if err := gotCoinbase.readFrom(blockchain.NewReader(decodeHex)); err != nil { 156 t.Fatal(err) 157 } 158 159 if !testutil.DeepEqual(*coinbase, gotCoinbase) { 160 t.Errorf("expected marshaled/unmarshaled txinput to be:\n%sgot:\n%s", spew.Sdump(*coinbase), spew.Sdump(gotCoinbase)) 161 } 162 } 163 164 func TestSerializationVeto(t *testing.T) { 165 arguments := [][]byte{ 166 []byte("arguments1"), 167 []byte("arguments2"), 168 } 169 170 vetoInput := NewVetoInput(arguments, testutil.MustDecodeHash("fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409"), testutil.MustDecodeAsset("fe9791d71b67ee62515e08723c061b5ccb952a80d804417c8aeedf7f633c524a"), 254354, 3, []byte("spendProgram"), []byte("af594006a40837d9f028daabb6d589df0b9138daefad5683e5233c2646279217294a8d532e60863bcf196625a35fb8ceeffa3c09610eb92dcfb655a947f13269"), [][]byte{}) 171 172 wantHex := strings.Join([]string{ 173 "01", // asset version 174 "d701", // input commitment length 175 "03", // veto type flag 176 "53", // veto commitment length 177 "fad5195a0c8e3b590b86a3c0a95e7529565888508aecca96e9aeda633002f409", // source id 178 "fe9791d71b67ee62515e08723c061b5ccb952a80d804417c8aeedf7f633c524a", // assetID 179 "92c30f", // amount 180 "03", // source position 181 "01", // vm version 182 "0c", // veto program length 183 "7370656e6450726f6772616d", // veto program 184 "00", // state length 185 "8001", //xpub length 186 "6166353934303036613430383337643966303238646161626236643538396466306239313338646165666164353638336535323333633236343632373932313732393461386435333265363038363362636631393636323561333566623863656566666133633039363130656239326463666236353561393437663133323639", //voter xpub 187 "17", // witness length 188 "02", // argument array length 189 "0a", // first argument length 190 "617267756d656e747331", // first argument data 191 "0a", // second argument length 192 "617267756d656e747332", // second argument data 193 }, "") 194 195 // Test convert struct to hex 196 var buffer bytes.Buffer 197 if err := vetoInput.writeTo(&buffer); err != nil { 198 t.Fatal(err) 199 } 200 201 gotHex := hex.EncodeToString(buffer.Bytes()) 202 if gotHex != wantHex { 203 t.Errorf("serialization bytes = %s want %s", gotHex, wantHex) 204 } 205 206 // Test convert hex to struct 207 var gotVeto TxInput 208 decodeHex, err := hex.DecodeString(wantHex) 209 if err != nil { 210 t.Fatal(err) 211 } 212 213 if err := gotVeto.readFrom(blockchain.NewReader(decodeHex)); err != nil { 214 t.Fatal(err) 215 } 216 217 if !testutil.DeepEqual(*vetoInput, gotVeto) { 218 t.Errorf("expected marshaled/unmarshaled txinput to be:\n%sgot:\n%s", spew.Sdump(*vetoInput), spew.Sdump(gotVeto)) 219 } 220 }