Skip to content

Commit

Permalink
Merge pull request #233 from kaleido-io/empty-bytes
Browse files Browse the repository at this point in the history
Differentiate bytes vs. fixed bytes in encoding parameters
  • Loading branch information
peterbroadhurst authored May 4, 2023
2 parents 77e3c90 + d36fc58 commit 111eb89
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/eth/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,11 @@ func (tx *Txn) generateTypedArg(requiredType *ethbinding.ABIType, param interfac
return nil, errors.Errorf(errors.TransactionSendInputTypeBadJSONTypeForBytes, methodName, path, requiredType, suppliedType)
}
if len(bSlice) == 0 {
return [0]byte{}, nil
if requiredType.T == ethbinding.BytesTy {
return []byte{}, nil
} else {
return [0]byte{}, nil
}
} else if requiredType.GetType().Kind() == reflect.Array {
// Create ourselves an array of the right size (ethereum won't accept a slice)
bArrayType := reflect.ArrayOf(len(bSlice), reflect.TypeOf(bSlice[0]))
Expand Down
48 changes: 48 additions & 0 deletions internal/eth/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,54 @@ func TestSendTxnPackError(t *testing.T) {
assert.Regexp("cannot use \\[0\\]uint8 as type \\[1\\]uint8 as argument", err.Error())
}

func TestSendTxnPackEmptyBytes(t *testing.T) {
assert := assert.New(t)

var msg messages.SendTransaction
msg.Parameters = []interface{}{"0x"}
msg.Method = &ethbinding.ABIElementMarshaling{
Name: "testFunc",
Inputs: []ethbinding.ABIArgumentMarshaling{
{
Name: "param1",
Type: "bytes",
},
},
Outputs: []ethbinding.ABIArgumentMarshaling{
{
Name: "ret1",
Type: "uint256",
},
},
}
_, err := NewSendTxn(&msg, nil)
assert.NoError(err)
}

func TestSendTxnPackBytes(t *testing.T) {
assert := assert.New(t)

var msg messages.SendTransaction
msg.Parameters = []interface{}{"0x1234"}
msg.Method = &ethbinding.ABIElementMarshaling{
Name: "testFunc",
Inputs: []ethbinding.ABIArgumentMarshaling{
{
Name: "param1",
Type: "bytes2",
},
},
Outputs: []ethbinding.ABIArgumentMarshaling{
{
Name: "ret1",
Type: "uint256",
},
},
}
_, err := NewSendTxn(&msg, nil)
assert.NoError(err)
}

func TestProcessRLPBytesValidTypes(t *testing.T) {
assert := assert.New(t)

Expand Down

0 comments on commit 111eb89

Please sign in to comment.