Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multiple named tuple result from contract #1333

Merged
merged 5 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions internal/blockchain/ethereum/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,10 +836,12 @@ func (e *Ethereum) QueryContract(ctx context.Context, signingKey string, locatio
if err != nil || !res.IsSuccess() {
return nil, err
}
output := &queryOutput{}
if err = json.Unmarshal(res.Body(), output); err != nil {

output := make(map[string]interface{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EnriqueL8 - I believe this should just be interface{} (not a map)

arbitrary map of string to any (interface{}

I agree that's what EVMConnect is configured to return today. But in firefly-signer there are other options (possibly not exposed in config on EVMConnect yet).

For example, you can make it work just like Fabric where it returns an array (not an object) of the values.

I don't see any reason why we'd want to make a change now in Core that precludes EVMConnect being configured by someone to do that in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Amending

if err = json.Unmarshal(res.Body(), &output); err != nil {
return nil, err
}

return output, nil
}

Expand Down Expand Up @@ -1015,6 +1017,8 @@ func (e *Ethereum) queryNetworkVersion(ctx context.Context, address string) (ver
}
return 0, err
}

// Leave as queryOutput as it only has one value
output := &queryOutput{}
if err = json.Unmarshal(res.Body(), output); err != nil {
return 0, err
Expand Down
97 changes: 97 additions & 0 deletions internal/blockchain/ethereum/ethereum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2895,6 +2895,103 @@ func TestQueryContractOK(t *testing.T) {
assert.Equal(t, `{"output":"3"}`, string(j))
}

func TestQueryContractMultipleUnnamedOutputOK(t *testing.T) {
e, cancel := newTestEthereum()
defer cancel()
httpmock.ActivateNonDefault(e.client.GetClient())
defer httpmock.DeactivateAndReset()
location := &Location{
Address: "0x12345",
}
method := testFFIMethod()
errors := testFFIErrors()
params := map[string]interface{}{}
options := map[string]interface{}{
"customOption": "customValue",
}

outputStruct := struct {
Test string `json:"test"`
Value int `json:"value"`
}{
Test: "myvalue",
Value: 3,
}

output := map[string]interface{}{
"output": "foo",
"output1": outputStruct,
"anything": 3,
}

locationBytes, err := json.Marshal(location)
assert.NoError(t, err)
httpmock.RegisterResponder("POST", `http://localhost:12345/`,
func(req *http.Request) (*http.Response, error) {
var body map[string]interface{}
json.NewDecoder(req.Body).Decode(&body)
headers := body["headers"].(map[string]interface{})
assert.Equal(t, "Query", headers["type"])
assert.Equal(t, "customValue", body["customOption"].(string))
assert.Equal(t, "0x12345", body["to"].(string))
assert.Equal(t, "0x01020304", body["from"].(string))
return httpmock.NewJsonResponderOrPanic(200, output)(req)
})
result, err := e.QueryContract(context.Background(), "0x01020304", fftypes.JSONAnyPtrBytes(locationBytes), method, params, errors, options)
assert.NoError(t, err)
j, err := json.Marshal(result)
assert.NoError(t, err)
assert.Equal(t, `{"anything":3,"output":"foo","output1":{"test":"myvalue","value":3}}`, string(j))
}

func TestQueryContractNamedOutputOK(t *testing.T) {
e, cancel := newTestEthereum()
defer cancel()
httpmock.ActivateNonDefault(e.client.GetClient())
defer httpmock.DeactivateAndReset()
location := &Location{
Address: "0x12345",
}
method := testFFIMethod()
errors := testFFIErrors()
params := map[string]interface{}{}
options := map[string]interface{}{
"customOption": "customValue",
}

outputStruct := struct {
Test string `json:"test"`
Value int `json:"value"`
}{
Test: "myvalue",
Value: 3,
}

output := map[string]interface{}{
"mynamedparam": "foo",
"mynamedstruct": outputStruct,
}

locationBytes, err := json.Marshal(location)
assert.NoError(t, err)
httpmock.RegisterResponder("POST", `http://localhost:12345/`,
func(req *http.Request) (*http.Response, error) {
var body map[string]interface{}
json.NewDecoder(req.Body).Decode(&body)
headers := body["headers"].(map[string]interface{})
assert.Equal(t, "Query", headers["type"])
assert.Equal(t, "customValue", body["customOption"].(string))
assert.Equal(t, "0x12345", body["to"].(string))
assert.Equal(t, "0x01020304", body["from"].(string))
return httpmock.NewJsonResponderOrPanic(200, output)(req)
})
result, err := e.QueryContract(context.Background(), "0x01020304", fftypes.JSONAnyPtrBytes(locationBytes), method, params, errors, options)
assert.NoError(t, err)
j, err := json.Marshal(result)
assert.NoError(t, err)
assert.Equal(t, `{"mynamedparam":"foo","mynamedstruct":{"test":"myvalue","value":3}}`, string(j))
}

func TestQueryContractInvalidOption(t *testing.T) {
e, cancel := newTestEthereum()
defer cancel()
Expand Down