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 all 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
9 changes: 6 additions & 3 deletions internal/blockchain/ethereum/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,11 +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 {

var output interface{}
if err = json.Unmarshal(res.Body(), &output); err != nil {
return nil, err
}
return output, nil
return output, nil // note UNLIKE fabric this is just `output`, not `output.Result` - but either way the top level of what we return to the end user, is whatever the Connector sent us
}

func (e *Ethereum) NormalizeContractLocation(ctx context.Context, ntype blockchain.NormalizeType, location *fftypes.JSONAny) (result *fftypes.JSONAny, err error) {
Expand Down Expand Up @@ -1015,6 +1016,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
Loading