Skip to content

Commit

Permalink
Merge pull request #83 from hyperledger/unamed-obj-input
Browse files Browse the repository at this point in the history
Handle unnamed tuple entries consistently on input and output
  • Loading branch information
EnriqueL8 authored Oct 8, 2024
2 parents 0b9dbf8 + 40e3592 commit 41530b2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 39 deletions.
1 change: 0 additions & 1 deletion internal/signermsgs/en_error_messges.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ var (
MsgFixedLengthABIArrayMismatch = ffe("FF22036", "Input array is length %d, and required fixed array length is %d for component %s")
MsgTupleABIArrayMismatch = ffe("FF22037", "Input array is length %d, and required tuple component count is %d for component %s")
MsgTupleABINotArrayOrMap = ffe("FF22038", "Input type %T is not array or map for component %s")
MsgTupleInABINoName = ffe("FF22039", "Tuple child %d does not have a name for component %s")
MsgMissingInputKeyABITuple = ffe("FF22040", "Input map missing key '%s' required for tuple component %s")
MsgBadABITypeComponent = ffe("FF22041", "Bad ABI type component: %d")
MsgWrongTypeComponentABIEncode = ffe("FF22042", "Incorrect type expected=%s found=%T for ABI encoding of component %s")
Expand Down
24 changes: 24 additions & 0 deletions pkg/abi/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/hyperledger/firefly-common/pkg/ffapi"
"github.com/hyperledger/firefly-signer/pkg/ethtypes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const sampleABI1 = `[
Expand Down Expand Up @@ -1072,3 +1073,26 @@ func TestErrorString(t *testing.T) {
assert.False(t, ok)

}

func TestUnnamedInputOutput(t *testing.T) {

sampleABI := ABI{
{Type: Function, Name: "set", Inputs: ParameterArray{
{Type: "uint256"},
{Type: "string"},
}},
}

cv, err := sampleABI[0].Inputs.ParseJSON([]byte(`{"0":12345,"1":"test"}`))
require.NoError(t, err)
res, err := NewSerializer().SetFormattingMode(FormatAsFlatArrays).SerializeJSON(cv)
require.NoError(t, err)
require.JSONEq(t, `["12345","test"]`, string(res))

cv, err = sampleABI[0].Inputs.ParseJSON([]byte(`[12345,"test"]`))
require.NoError(t, err)
res, err = NewSerializer().SetFormattingMode(FormatAsObjects).SerializeJSON(cv)
require.NoError(t, err)
require.JSONEq(t, `{"0":"12345","1":"test"}`, string(res))

}
12 changes: 7 additions & 5 deletions pkg/abi/inputparsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"math/big"
"reflect"
"strconv"
"strings"

"github.com/hyperledger/firefly-common/pkg/i18n"
Expand Down Expand Up @@ -456,13 +457,14 @@ func walkTupleInput(ctx context.Context, breadcrumbs string, input interface{},
Children: make([]*ComponentValue, len(component.tupleChildren)),
}
for i, tupleChild := range component.tupleChildren {
if tupleChild.keyName == "" {
return nil, i18n.NewError(ctx, signermsgs.MsgTupleInABINoName, i, breadcrumbs)
keyName := tupleChild.keyName
if keyName == "" {
keyName = strconv.Itoa(i)
}
childBreadcrumbs := fmt.Sprintf("%s.%s", breadcrumbs, tupleChild.keyName)
v, ok := iMap[tupleChild.keyName]
childBreadcrumbs := fmt.Sprintf("%s.%s", breadcrumbs, keyName)
v, ok := iMap[keyName]
if !ok {
return nil, i18n.NewError(ctx, signermsgs.MsgMissingInputKeyABITuple, tupleChild.keyName, childBreadcrumbs)
return nil, i18n.NewError(ctx, signermsgs.MsgMissingInputKeyABITuple, keyName, childBreadcrumbs)
}
cv.Children[i], err = walkInput(ctx, childBreadcrumbs, v, component.tupleChildren[i])
if err != nil {
Expand Down
37 changes: 4 additions & 33 deletions pkg/abi/inputparsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ func TestGetIntegerFromInterface(t *testing.T) {
assert.Regexp(t, "FF22030", err)
assert.Nil(t, i)

i, err = getIntegerFromInterface(ctx, "ut", json.Number("wrong"))
assert.Regexp(t, "FF22030", err)
assert.Nil(t, i)

}

func TestGetFloatFromInterface(t *testing.T) {
Expand Down Expand Up @@ -668,39 +672,6 @@ func TestTuplesWrongType(t *testing.T) {

}

func TestTuplesMissingName(t *testing.T) {
const sample = `[
{
"name": "foo",
"type": "function",
"inputs": [
{
"name": "a",
"type": "tuple",
"components": [
{
"type": "uint256"
}
]
}
],
"outputs": []
}
]`

inputs := testABI(t, sample)[0].Inputs

// Fine if you use the array syntax
values := `{ "a": [12345] }`
_, err := inputs.ParseJSON([]byte(values))
assert.NoError(t, err)

// But the missing name is a problem for the object syntax
values = `{ "a": {"b":12345} }`
_, err = inputs.ParseJSON([]byte(values))
assert.Regexp(t, "FF22039", err)
}

func TestTupleEncodeIndividualFixedParam(t *testing.T) {
const sample = `[
{
Expand Down

0 comments on commit 41530b2

Please sign in to comment.