Skip to content

Commit

Permalink
smartcontract: extend slice types accepted by NewParameterFromValue
Browse files Browse the repository at this point in the history
It's _very_ annoying to not be able to use []string types properly:
    test invocation failed: unsupported parameter []string

But the same thing can happen to any other slice, so accept slices of every
basic type we accept above. Reorder tests to match implementation switch as
well.

Signed-off-by: Roman Khimov <[email protected]>
  • Loading branch information
roman-khimov committed Sep 11, 2024
1 parent f80f453 commit f134387
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 13 deletions.
42 changes: 40 additions & 2 deletions pkg/smartcontract/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,49 @@ func NewParameterFromValue(value any) (Parameter, error) {
case *keys.PublicKey:
result.Type = PublicKeyType
result.Value = v.Bytes()
case [][]byte:
return newArrayParameter(v)
case []Parameter:
result.Type = ArrayType
result.Value = slices.Clone(v)
case [][]byte:
return newArrayParameter(v)
case []string:
return newArrayParameter(v)
case []bool:
return newArrayParameter(v)
case []*big.Int:
return newArrayParameter(v)
case []int8:
return newArrayParameter(v)
case []int16:
return newArrayParameter(v)
case []uint16:
return newArrayParameter(v)
case []int32:
return newArrayParameter(v)
case []uint32:
return newArrayParameter(v)
case []int:
return newArrayParameter(v)
case []uint:
return newArrayParameter(v)
case []int64:
return newArrayParameter(v)
case []uint64:
return newArrayParameter(v)
case []*Parameter:
return newArrayParameter(v)
case []Convertible:
return newArrayParameter(v)
case []util.Uint160:
return newArrayParameter(v)
case []util.Uint256:
return newArrayParameter(v)
case []*util.Uint160:
return newArrayParameter(v)
case []*util.Uint256:
return newArrayParameter(v)
case []keys.PublicKey:
return newArrayParameter(v)
case []*keys.PublicKey:
return newArrayParameter(v)
case keys.PublicKeys:
Expand Down
118 changes: 107 additions & 11 deletions pkg/smartcontract/parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,11 @@ func TestParameterFromValue(t *testing.T) {
expType: ByteArrayType,
expVal: []byte{1, 2, 3},
},
{
value: testConvertible{i: 123},
expType: IntegerType,
expVal: 123,
},
{
value: util.Uint160{1, 2, 3},
expType: Hash160Type,
Expand Down Expand Up @@ -712,22 +717,102 @@ func TestParameterFromValue(t *testing.T) {
expVal: pk2.PublicKey().Bytes(),
},
{
value: nil,
expType: AnyType,
expVal: nil,
value: []Parameter{{ByteArrayType, []byte{1, 2, 3}}, {ByteArrayType, []byte{3, 2, 1}}},
expType: ArrayType,
expVal: []Parameter{{ByteArrayType, []byte{1, 2, 3}}, {ByteArrayType, []byte{3, 2, 1}}},
},
{
value: [][]byte{{1, 2, 3}, {3, 2, 1}},
expType: ArrayType,
expVal: []Parameter{{ByteArrayType, []byte{1, 2, 3}}, {ByteArrayType, []byte{3, 2, 1}}},
},
{
value: []Parameter{{ByteArrayType, []byte{1, 2, 3}}, {ByteArrayType, []byte{3, 2, 1}}},
value: []string{"qwe", "asd"},
expType: ArrayType,
expVal: []Parameter{{ByteArrayType, []byte{1, 2, 3}}, {ByteArrayType, []byte{3, 2, 1}}},
expVal: []Parameter{{StringType, "qwe"}, {StringType, "asd"}},
},
{
value: []*keys.PublicKey{pk1.PublicKey(), pk2.PublicKey()},
value: []bool{false, true},
expType: ArrayType,
expVal: []Parameter{{BoolType, false}, {BoolType, true}},
},
{
value: []*big.Int{big.NewInt(100), big.NewInt(42)},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []int8{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []int16{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []uint16{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []int32{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []uint32{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []int{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []uint{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []int64{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []uint64{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []Convertible{testConvertible{i: 123}, testConvertible{i: 321}},
expType: ArrayType,
expVal: []Parameter{{IntegerType, 123}, {IntegerType, 321}},
},
{
value: []util.Uint160{{1, 2, 3}, {3, 2, 1}},
expType: ArrayType,
expVal: []Parameter{{Hash160Type, util.Uint160{1, 2, 3}}, {Hash160Type, util.Uint160{3, 2, 1}}},
},
{
value: []util.Uint256{{1, 2, 3}, {3, 2, 1}},
expType: ArrayType,
expVal: []Parameter{{Hash256Type, util.Uint256{1, 2, 3}}, {Hash256Type, util.Uint256{3, 2, 1}}},
},
{
value: []*util.Uint160{&util.Uint160{1, 2, 3}, nil, &util.Uint160{3, 2, 1}},

Check failure on line 805 in pkg/smartcontract/parameter_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gofmt`-ed with `-s` (gofmt)
expType: ArrayType,
expVal: []Parameter{{Hash160Type, util.Uint160{1, 2, 3}}, {AnyType, nil}, {Hash160Type, util.Uint160{3, 2, 1}}},
},
{
value: []*util.Uint256{&util.Uint256{1, 2, 3}, nil, &util.Uint256{3, 2, 1}},
expType: ArrayType,
expVal: []Parameter{{Hash256Type, util.Uint256{1, 2, 3}}, {AnyType, nil}, {Hash256Type, util.Uint256{3, 2, 1}}},
},
{
value: []keys.PublicKey{*pk1.PublicKey(), *pk2.PublicKey()},
expType: ArrayType,
expVal: []Parameter{{
Type: PublicKeyType,
Expand All @@ -748,6 +833,17 @@ func TestParameterFromValue(t *testing.T) {
Value: pk2.PublicKey().Bytes(),
}},
},
{
value: []*keys.PublicKey{pk1.PublicKey(), pk2.PublicKey()},
expType: ArrayType,
expVal: []Parameter{{
Type: PublicKeyType,
Value: pk1.PublicKey().Bytes(),
}, {
Type: PublicKeyType,
Value: pk2.PublicKey().Bytes(),
}},
},
{
value: []any{-42, "random", []byte{1, 2, 3}},
expType: ArrayType,
Expand All @@ -762,11 +858,6 @@ func TestParameterFromValue(t *testing.T) {
Value: []byte{1, 2, 3},
}},
},
{
value: testConvertible{i: 123},
expType: IntegerType,
expVal: 123,
},
{
value: []any{1, testConvertible{i: 123}},
expType: ArrayType,
Expand All @@ -781,6 +872,11 @@ func TestParameterFromValue(t *testing.T) {
},
},
},
{
value: nil,
expType: AnyType,
expVal: nil,
},
{
value: testConvertible{err: "invalid i value"},
err: "invalid i value",
Expand Down

0 comments on commit f134387

Please sign in to comment.