|
|
@ -22,6 +22,7 @@ import ( |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"math/big" |
|
|
|
"math/big" |
|
|
|
"reflect" |
|
|
|
"reflect" |
|
|
|
|
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
"testing" |
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
|
@ -261,25 +262,27 @@ var unpackTests = []unpackTest{ |
|
|
|
|
|
|
|
|
|
|
|
func TestUnpack(t *testing.T) { |
|
|
|
func TestUnpack(t *testing.T) { |
|
|
|
for i, test := range unpackTests { |
|
|
|
for i, test := range unpackTests { |
|
|
|
def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def) |
|
|
|
t.Run(strconv.Itoa(i), func(t *testing.T) { |
|
|
|
abi, err := JSON(strings.NewReader(def)) |
|
|
|
def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def) |
|
|
|
if err != nil { |
|
|
|
abi, err := JSON(strings.NewReader(def)) |
|
|
|
t.Fatalf("invalid ABI definition %s: %v", def, err) |
|
|
|
if err != nil { |
|
|
|
} |
|
|
|
t.Fatalf("invalid ABI definition %s: %v", def, err) |
|
|
|
encb, err := hex.DecodeString(test.enc) |
|
|
|
} |
|
|
|
if err != nil { |
|
|
|
encb, err := hex.DecodeString(test.enc) |
|
|
|
t.Fatalf("invalid hex: %s" + test.enc) |
|
|
|
if err != nil { |
|
|
|
} |
|
|
|
t.Fatalf("invalid hex: %s" + test.enc) |
|
|
|
outptr := reflect.New(reflect.TypeOf(test.want)) |
|
|
|
} |
|
|
|
err = abi.Unpack(outptr.Interface(), "method", encb) |
|
|
|
outptr := reflect.New(reflect.TypeOf(test.want)) |
|
|
|
if err := test.checkError(err); err != nil { |
|
|
|
err = abi.Unpack(outptr.Interface(), "method", encb) |
|
|
|
t.Errorf("test %d (%v) failed: %v", i, test.def, err) |
|
|
|
if err := test.checkError(err); err != nil { |
|
|
|
continue |
|
|
|
t.Errorf("test %d (%v) failed: %v", i, test.def, err) |
|
|
|
} |
|
|
|
return |
|
|
|
out := outptr.Elem().Interface() |
|
|
|
} |
|
|
|
if !reflect.DeepEqual(test.want, out) { |
|
|
|
out := outptr.Elem().Interface() |
|
|
|
t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out) |
|
|
|
if !reflect.DeepEqual(test.want, out) { |
|
|
|
} |
|
|
|
t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -336,6 +339,29 @@ func TestMultiReturnWithStruct(t *testing.T) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestMultiReturnWithArray(t *testing.T) { |
|
|
|
|
|
|
|
const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]` |
|
|
|
|
|
|
|
abi, err := JSON(strings.NewReader(definition)) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
buff := new(bytes.Buffer) |
|
|
|
|
|
|
|
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009")) |
|
|
|
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9} |
|
|
|
|
|
|
|
ret2, ret2Exp := new(uint64), uint64(8) |
|
|
|
|
|
|
|
if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if !reflect.DeepEqual(*ret1, ret1Exp) { |
|
|
|
|
|
|
|
t.Error("array result", *ret1, "!= Expected", ret1Exp) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if *ret2 != ret2Exp { |
|
|
|
|
|
|
|
t.Error("int result", *ret2, "!= Expected", ret2Exp) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestUnmarshal(t *testing.T) { |
|
|
|
func TestUnmarshal(t *testing.T) { |
|
|
|
const definition = `[ |
|
|
|
const definition = `[ |
|
|
|
{ "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] }, |
|
|
|
{ "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] }, |
|
|
|