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

v1.2: fix: multiple named tuple result from contract #1365

Merged
merged 11 commits into from
Jul 11, 2023
2 changes: 1 addition & 1 deletion docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ nav_order: 2
|address|The IP address on which the metrics HTTP API should listen|`int`|`127.0.0.1`
|enabled|Enables the metrics API|`boolean`|`true`
|path|The path from which to serve the Prometheus metrics|`string`|`/metrics`
|port|The port on which the metrics HTTP API should listen|`int`|`6000`
|port|The port on which the metrics HTTP API should listen|`int`|`7000`
|publicURL|The fully qualified public URL for the metrics API. This is used for building URLs in HTTP responses and in OpenAPI Spec generation|URL `string`|`<nil>`
|readTimeout|The maximum time to wait when reading from an HTTP connection|[`time.Duration`](https://pkg.go.dev/time#Duration)|`15s`
|shutdownTimeout|The maximum amount of time to wait for any open HTTP requests to finish before shutting down the HTTP server|[`time.Duration`](https://pkg.go.dev/time#Duration)|`10s`
Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type apiServer struct {
func InitConfig() {
httpserver.InitHTTPConfig(apiConfig, 5000)
httpserver.InitHTTPConfig(spiConfig, 5001)
httpserver.InitHTTPConfig(metricsConfig, 6000)
httpserver.InitHTTPConfig(metricsConfig, 7000)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure that we want to change the default metrics port in a patch release. Was there a reason for this change?

httpserver.InitCORSConfig(corsConfig)
initMetricsConfig(metricsConfig)
}
Expand Down
9 changes: 6 additions & 3 deletions internal/blockchain/ethereum/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,11 +742,12 @@ func (e *Ethereum) QueryContract(ctx context.Context, location *fftypes.JSONAny,
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, location *fftypes.JSONAny) (result *fftypes.JSONAny, err error) {
Expand Down Expand Up @@ -924,6 +925,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
95 changes: 95 additions & 0 deletions internal/blockchain/ethereum/ethereum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2582,6 +2582,101 @@ 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))
return httpmock.NewJsonResponderOrPanic(200, output)(req)
})
result, err := e.QueryContract(context.Background(), 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))
return httpmock.NewJsonResponderOrPanic(200, output)(req)
})
result, err := e.QueryContract(context.Background(), 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