Skip to content

Commit

Permalink
Include empty arrays for components/inputs/outputs on JSON Serialize
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Broadhurst <peter.broadhurst@kaleido.io>
  • Loading branch information
peterbroadhurst committed Sep 8, 2024
1 parent 27d2240 commit 1db1664
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ type Parameter struct {
Name string `ffstruct:"EthABIParameter" json:"name"` // The name of the argument - does not affect the signature
Type string `ffstruct:"EthABIParameter" json:"type"` // The canonical type of the parameter
InternalType string `ffstruct:"EthABIParameter" json:"internalType,omitempty"` // Additional internal type information that might be generated by the compiler
Components ParameterArray `ffstruct:"EthABIParameter" json:"components,omitempty"` // An ordered list (tuple) of nested elements for array/object types
Components ParameterArray `ffstruct:"EthABIParameter" json:"components"` // An ordered list (tuple) of nested elements for array/object types
Indexed bool `ffstruct:"EthABIParameter" json:"indexed,omitempty"` // Events only: Whether the parameter is indexed into one of the topics of the log, or in the log's data segment

parsed *typeComponent // cached components
Expand Down
19 changes: 19 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 @@ -1027,3 +1028,21 @@ func TestComplexStructSolidityDef(t *testing.T) {
}, childStructs)

}

func TestRoundTripEmptyComponents(t *testing.T) {

abiString := `[
{"type":"function", "name":"fn1", "inputs": [
{ "type": "tuple", "name":"param1","components": [] }
], "outputs": []}
]`
var abi ABI
err := json.Unmarshal([]byte(abiString), &abi)
require.NoError(t, err)

roundTripped, err := json.Marshal(&abi)
require.NoError(t, err)

assert.JSONEq(t, abiString, string(roundTripped))

}
6 changes: 3 additions & 3 deletions pkg/abi/signedi256.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ var posMax = map[uint16]*big.Int{}
var negMax = map[uint16]*big.Int{}

func init() {
for i := 8; i <= 256; i += 8 {
posMax[uint16(i)] = maxPositiveSignedInt(uint(i))
negMax[uint16(i)] = maxNegativeSignedInt(uint(i))
for i := uint16(8); i <= uint16(256); i += 8 {
posMax[i] = maxPositiveSignedInt(uint(i))
negMax[i] = maxNegativeSignedInt(uint(i))
}
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/abi/typecomponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ func parseMSuffix(ctx context.Context, abiTypeString string, ec *typeComponent,
if err != nil {
return i18n.WrapError(ctx, err, signermsgs.MsgInvalidABISuffix, abiTypeString, ec.elementaryType)
}
//nolint:gosec // we used bitSize on ParseUint above
ec.m = uint16(val)
if ec.m < ec.elementaryType.mMin || ec.m > ec.elementaryType.mMax {
return i18n.NewError(ctx, signermsgs.MsgInvalidABISuffix, abiTypeString, ec.elementaryType)
Expand All @@ -646,6 +647,7 @@ func parseNSuffix(ctx context.Context, abiTypeString string, ec *typeComponent,
if err != nil {
return i18n.WrapError(ctx, err, signermsgs.MsgInvalidABISuffix, abiTypeString, ec.elementaryType)
}
//nolint:gosec // we used bitSize on ParseUint above
ec.n = uint16(val)
if ec.n < ec.elementaryType.nMin || ec.n > ec.elementaryType.nMax {
return i18n.NewError(ctx, signermsgs.MsgInvalidABISuffix, abiTypeString, ec.elementaryType)
Expand All @@ -672,10 +674,11 @@ func parseMxNSuffix(ctx context.Context, abiTypeString string, ec *typeComponent

// parseArrayM parses the "8" in "uint256[8]" for a fixed length array of <type>[M]
func parseArrayM(ctx context.Context, abiTypeString string, ac *typeComponent, mStr string) error {
val, err := strconv.ParseUint(mStr, 10, 64)
val, err := strconv.ParseUint(mStr, 10, 32)
if err != nil {
return i18n.WrapError(ctx, err, signermsgs.MsgInvalidABIArraySpec, abiTypeString)
}
//nolint:gosec // we used bitSize on ParseUint above
ac.arrayLength = int(val)
return nil
}
Expand Down

0 comments on commit 1db1664

Please sign in to comment.