github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/vm/vmutil/script_test.go (about) 1 package vmutil 2 3 import ( 4 "crypto/ed25519" 5 "encoding/hex" 6 "reflect" 7 "testing" 8 9 "github.com/bytom/bytom/errors" 10 ) 11 12 // TestIsUnspendable ensures the IsUnspendable function returns the expected 13 // results. 14 func TestIsUnspendable(t *testing.T) { 15 tests := []struct { 16 pkScript []byte 17 expected bool 18 }{ 19 { 20 // Unspendable 21 pkScript: []byte{0x6a, 0x04, 0x74, 0x65, 0x73, 0x74}, 22 expected: true, 23 }, 24 { 25 // Spendable 26 pkScript: []byte{0x76, 0xa9, 0x14, 0x29, 0x95, 0xa0, 27 0xfe, 0x68, 0x43, 0xfa, 0x9b, 0x95, 0x45, 28 0x97, 0xf0, 0xdc, 0xa7, 0xa4, 0x4d, 0xf6, 29 0xfa, 0x0b, 0x5c, 0x88, 0xac}, 30 expected: false, 31 }, 32 } 33 34 for i, test := range tests { 35 res := IsUnspendable(test.pkScript) 36 if res != test.expected { 37 t.Errorf("TestIsUnspendable #%d failed: got %v want %v", 38 i, res, test.expected) 39 continue 40 } 41 } 42 } 43 44 func TestP2SPMultiSigProgram(t *testing.T) { 45 pub1, _ := hex.DecodeString("988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6") 46 pub2, _ := hex.DecodeString("7192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c62") 47 pub3, _ := hex.DecodeString("8bcd251d9f4e03877130b6e6f1d577eda562375f07c3cdfad8f1d541002fd1a3") 48 49 tests := []struct { 50 pubkeys []ed25519.PublicKey 51 nrequired int 52 wantProgram string 53 wantErr error 54 }{ 55 { 56 pubkeys: []ed25519.PublicKey{pub1}, 57 nrequired: 1, 58 wantProgram: "ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb65151ad", 59 }, 60 { 61 pubkeys: []ed25519.PublicKey{pub1, pub2}, 62 nrequired: 2, 63 wantProgram: "ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6207192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c625252ad", 64 }, 65 { 66 pubkeys: []ed25519.PublicKey{pub1, pub2, pub3}, 67 nrequired: 2, 68 wantProgram: "ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6207192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c62208bcd251d9f4e03877130b6e6f1d577eda562375f07c3cdfad8f1d541002fd1a35253ad", 69 }, 70 { 71 pubkeys: []ed25519.PublicKey{pub1}, 72 nrequired: -1, 73 wantErr: errors.WithDetail(ErrBadValue, "negative quorum"), 74 }, 75 { 76 pubkeys: []ed25519.PublicKey{pub1}, 77 nrequired: 0, 78 wantErr: errors.WithDetail(ErrBadValue, "quorum empty with non-empty pubkey list"), 79 }, 80 { 81 pubkeys: []ed25519.PublicKey{pub1, pub2}, 82 nrequired: 3, 83 wantErr: errors.WithDetail(ErrBadValue, "quorum too big"), 84 }, 85 } 86 87 for i, test := range tests { 88 got, err := P2SPMultiSigProgram(test.pubkeys, test.nrequired) 89 if err != nil { 90 if test.wantErr != nil && err.Error() != test.wantErr.Error() { 91 t.Errorf("TestP2SPMultiSigProgram #%d failed: got %v want %v", i, err.Error(), test.wantErr.Error()) 92 } else if test.wantErr == nil { 93 t.Fatal(err) 94 } 95 } 96 97 if hex.EncodeToString(got) != test.wantProgram { 98 t.Errorf("TestP2SPMultiSigProgram #%d failed: got %v want %v", i, hex.EncodeToString(got), test.wantProgram) 99 } 100 } 101 } 102 103 func TestP2SPMultiSigProgramWithHeight(t *testing.T) { 104 pub1, _ := hex.DecodeString("988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6") 105 pub2, _ := hex.DecodeString("7192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c62") 106 pub3, _ := hex.DecodeString("8bcd251d9f4e03877130b6e6f1d577eda562375f07c3cdfad8f1d541002fd1a3") 107 108 tests := []struct { 109 pubkeys []ed25519.PublicKey 110 nrequired int 111 height uint64 112 wantProgram string 113 wantErr error 114 }{ 115 { 116 pubkeys: []ed25519.PublicKey{pub1}, 117 nrequired: 1, 118 wantProgram: "ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb65151ad", 119 }, 120 { 121 pubkeys: []ed25519.PublicKey{pub1, pub2}, 122 nrequired: 2, 123 wantProgram: "ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6207192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c625252ad", 124 }, 125 { 126 pubkeys: []ed25519.PublicKey{pub1, pub2, pub3}, 127 nrequired: 2, 128 wantProgram: "ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6207192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c62208bcd251d9f4e03877130b6e6f1d577eda562375f07c3cdfad8f1d541002fd1a35253ad", 129 }, 130 { 131 pubkeys: []ed25519.PublicKey{pub1}, 132 nrequired: 1, 133 height: 0, 134 wantProgram: "ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb65151ad", 135 }, 136 { 137 pubkeys: []ed25519.PublicKey{pub1}, 138 nrequired: 1, 139 height: 200, 140 wantProgram: "01c8cda069ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb65151ad", 141 }, 142 { 143 pubkeys: []ed25519.PublicKey{pub1, pub2}, 144 nrequired: 2, 145 height: 200, 146 wantProgram: "01c8cda069ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6207192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c625252ad", 147 }, 148 { 149 pubkeys: []ed25519.PublicKey{pub1, pub2, pub3}, 150 nrequired: 2, 151 height: 200, 152 wantProgram: "01c8cda069ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6207192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c62208bcd251d9f4e03877130b6e6f1d577eda562375f07c3cdfad8f1d541002fd1a35253ad", 153 }, 154 { 155 pubkeys: []ed25519.PublicKey{pub1}, 156 nrequired: -1, 157 wantErr: errors.WithDetail(ErrBadValue, "negative quorum"), 158 }, 159 { 160 pubkeys: []ed25519.PublicKey{pub1}, 161 nrequired: 0, 162 wantErr: errors.WithDetail(ErrBadValue, "quorum empty with non-empty pubkey list"), 163 }, 164 { 165 pubkeys: []ed25519.PublicKey{pub1, pub2}, 166 nrequired: 3, 167 wantErr: errors.WithDetail(ErrBadValue, "quorum too big"), 168 }, 169 } 170 171 for i, test := range tests { 172 got, err := P2SPMultiSigProgramWithHeight(test.pubkeys, test.nrequired, test.height) 173 if err != nil { 174 if test.wantErr != nil && err.Error() != test.wantErr.Error() { 175 t.Errorf("TestP2SPMultiSigProgram #%d failed: got %v want %v", i, err.Error(), test.wantErr.Error()) 176 } else if test.wantErr == nil { 177 t.Fatal(err) 178 } 179 } 180 181 if hex.EncodeToString(got) != test.wantProgram { 182 t.Errorf("TestP2SPMultiSigProgram #%d failed: got %v want %v", i, hex.EncodeToString(got), test.wantProgram) 183 } 184 } 185 } 186 187 func TestGetIssuanceProgramRestrictHeight(t *testing.T) { 188 tests := []struct { 189 issuanceProgram string 190 wantHeight uint64 191 }{ 192 { 193 issuanceProgram: "", 194 wantHeight: 0, 195 }, 196 { 197 issuanceProgram: "ae20ac20f5cdb9ada2ae9836bcfff32126d6b885aa3f73ee111a95d1bf37f3904aca5151ad", 198 wantHeight: 0, 199 }, 200 { 201 issuanceProgram: "01c8cda069ae20f44dd85be89de08b0f894476ccc7b3eebcf0a288c79504fa7e4c8033f5b7338020c86dc682ce3ecac64e165d9b5f8cca9ee05bd0d4df07adbfd11251ad7e88f1685152ad", 202 wantHeight: 200, 203 }, 204 { 205 issuanceProgram: "08c8c8c8c8c8c8c8c8cda069ae20f44dd85be89de08b0f894476ccc7b3eebcf0a288c79504fa7e4c8033f5b7338020c86dc682ce3ecac64e165d9b5f8cca9ee05bd0d4df07adbfd11251ad7e88f1685152ad", 206 wantHeight: 14468034567615334600, 207 }, 208 } 209 210 for i, test := range tests { 211 program, err := hex.DecodeString(test.issuanceProgram) 212 if err != nil { 213 t.Fatal(err) 214 } 215 originProgram := make([]byte, len(program)) 216 copy(originProgram, program) 217 gotHeight := GetIssuanceProgramRestrictHeight(program) 218 if gotHeight != test.wantHeight { 219 t.Errorf("TestGetIssuanceProgramRestrictHeight #%d failed: got %d want %d", i, gotHeight, test.wantHeight) 220 return 221 } 222 223 if !reflect.DeepEqual(originProgram, program) { 224 t.Errorf("TestGetIssuanceProgramRestrictHeight #%d failed: after got %v before %v", i, program, originProgram) 225 } 226 } 227 } 228 229 func TestRegisterProgram(t *testing.T) { 230 tests := []struct { 231 contract string 232 expected string 233 }{ 234 { 235 // len(contract) == 40 236 contract: "20e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c0", 237 expected: "6a046263727001012820e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c0", 238 }, 239 { 240 // len(contract) == 150 241 contract: "20e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403", 242 expected: "6a046263727001014c9620e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403", 243 }, 244 { 245 // len(contract) == 300 246 contract: "20e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403", 247 expected: "6a046263727001014d2c0120e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403", 248 }, 249 } 250 251 for _, test := range tests { 252 contract, err := hex.DecodeString(test.contract) 253 if err != nil { 254 t.Fatal(err) 255 } 256 257 program, err := RegisterProgram(contract) 258 if err != nil { 259 t.Fatal(err) 260 } 261 262 if hex.EncodeToString(program) != test.expected { 263 t.Errorf("got program data: %s, expect program data: %s", hex.EncodeToString(program), test.expected) 264 } 265 } 266 } 267 268 func TestCallContractProgram(t *testing.T) { 269 tests := []struct { 270 contractID string 271 expected string 272 }{ 273 { 274 contractID: "4e4f02d43bf50171f7f25d046b7f016002da410fc00d2e8902e7b170c98cf946", 275 expected: "0462637270204e4f02d43bf50171f7f25d046b7f016002da410fc00d2e8902e7b170c98cf946", 276 }, 277 } 278 279 for _, test := range tests { 280 contractID, err := hex.DecodeString(test.contractID) 281 if err != nil { 282 t.Fatal(err) 283 } 284 285 program, err := CallContractProgram(contractID) 286 if err != nil { 287 t.Fatal(err) 288 } 289 290 if hex.EncodeToString(program) != test.expected { 291 t.Errorf("got program data: %s, expect program data: %s", hex.EncodeToString(program), test.expected) 292 } 293 } 294 }