Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into listen
Browse files Browse the repository at this point in the history
  • Loading branch information
awrichar committed Apr 12, 2022
2 parents 2540e71 + 09a7cc6 commit 87e5a23
Show file tree
Hide file tree
Showing 37 changed files with 2,095 additions and 929 deletions.
8 changes: 4 additions & 4 deletions docs/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ GEM
coffee-script-source (1.11.1)
colorator (1.1.0)
commonmarker (0.23.4)
concurrent-ruby (1.1.9)
concurrent-ruby (1.1.10)
dnsruby (1.61.9)
simpleidn (~> 0.1)
em-websocket (0.5.3)
Expand Down Expand Up @@ -102,7 +102,7 @@ GEM
octokit (~> 4.0)
public_suffix (>= 3.0, < 5.0)
typhoeus (~> 1.3)
html-pipeline (2.14.0)
html-pipeline (2.14.1)
activesupport (>= 2)
nokogiri (>= 1.4)
http_parser.rb (0.8.0)
Expand Down Expand Up @@ -232,15 +232,15 @@ GEM
jekyll-seo-tag (~> 2.1)
minitest (5.15.0)
multipart-post (2.1.1)
nokogiri (1.13.3)
nokogiri (1.13.4)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
octokit (4.22.0)
faraday (>= 0.9)
sawyer (~> 0.8.0, >= 0.5.3)
pathutil (0.16.2)
forwardable-extended (~> 2.6)
public_suffix (4.0.6)
public_suffix (4.0.7)
racc (1.6.0)
rb-fsevent (0.11.1)
rb-inotify (0.10.1)
Expand Down
1,121 changes: 552 additions & 569 deletions docs/reference/config.md

Large diffs are not rendered by default.

328 changes: 328 additions & 0 deletions docs/swagger/swagger.yaml

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions internal/apiserver/route_get_contract_api_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright © 2022 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package apiserver

import (
"net/http"

"github.com/hyperledger/firefly/internal/coreconfig"
"github.com/hyperledger/firefly/internal/coremsgs"
"github.com/hyperledger/firefly/internal/oapispec"
"github.com/hyperledger/firefly/pkg/fftypes"
)

var getContractAPIInterface = &oapispec.Route{
Name: "getContractAPIInterface",
Path: "namespaces/{ns}/apis/{apiName}/interface",
Method: http.MethodGet,
PathParams: []*oapispec.PathParam{
{Name: "ns", ExampleFromConf: coreconfig.NamespacesDefault, Description: coremsgs.APIMessageTBD},
{Name: "apiName", Description: coremsgs.APIMessageTBD},
},
QueryParams: nil,
FilterFactory: nil,
Description: coremsgs.APIMessageTBD,
JSONInputValue: nil,
JSONOutputValue: func() interface{} { return &fftypes.FFI{} },
JSONOutputCodes: []int{http.StatusOK},
JSONHandler: func(r *oapispec.APIRequest) (output interface{}, err error) {
return getOr(r.Ctx).Contracts().GetContractAPIInterface(r.Ctx, r.PP["ns"], r.PP["apiName"])
},
}
47 changes: 47 additions & 0 deletions internal/apiserver/route_get_contract_api_interface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright © 2022 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package apiserver

import (
"bytes"
"encoding/json"
"net/http/httptest"
"testing"

"github.com/hyperledger/firefly/mocks/contractmocks"
"github.com/hyperledger/firefly/pkg/fftypes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestGetContractAPIInterface(t *testing.T) {
o, r := newTestAPIServer()
mcm := &contractmocks.Manager{}
o.On("Contracts").Return(mcm)
input := fftypes.Datatype{}
var buf bytes.Buffer
json.NewEncoder(&buf).Encode(&input)
req := httptest.NewRequest("GET", "/api/v1/namespaces/ns1/apis/banana/interface", &buf)
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := httptest.NewRecorder()

mcm.On("GetContractAPIInterface", mock.Anything, "ns1", "banana").
Return(&fftypes.FFI{}, nil)
r.ServeHTTP(res, req)

assert.Equal(t, 200, res.Result().StatusCode)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package apiserver

import (
"net/http"
"strings"

"github.com/hyperledger/firefly/internal/coreconfig"
"github.com/hyperledger/firefly/internal/coremsgs"
Expand All @@ -34,13 +35,18 @@ var getContractInterfaceNameVersion = &oapispec.Route{
{Name: "name", Description: coremsgs.APIMessageTBD},
{Name: "version", Description: coremsgs.APIMessageTBD},
},
QueryParams: nil,
QueryParams: []*oapispec.QueryParam{
{Name: "fetchchildren", Example: "true", Description: coremsgs.APIMessageTBD, IsBool: true},
},
FilterFactory: nil,
Description: coremsgs.APIMessageTBD,
JSONInputValue: nil,
JSONOutputValue: func() interface{} { return &fftypes.FFI{} },
JSONOutputCodes: []int{http.StatusOK},
JSONHandler: func(r *oapispec.APIRequest) (output interface{}, err error) {
if strings.EqualFold(r.QP["fetchchildren"], "true") {
return getOr(r.Ctx).Contracts().GetFFIWithChildren(r.Ctx, r.PP["ns"], r.PP["name"], r.PP["version"])
}
return getOr(r.Ctx).Contracts().GetFFI(r.Ctx, r.PP["ns"], r.PP["name"], r.PP["version"])
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,21 @@ func TestGetContractInterfaceNameVersion(t *testing.T) {

assert.Equal(t, 200, res.Result().StatusCode)
}

func TestGetContractInterfaceNameVersionWithChildren(t *testing.T) {
o, r := newTestAPIServer()
mcm := &contractmocks.Manager{}
o.On("Contracts").Return(mcm)
input := fftypes.Datatype{}
var buf bytes.Buffer
json.NewEncoder(&buf).Encode(&input)
req := httptest.NewRequest("GET", "/api/v1/namespaces/ns1/contracts/interfaces/banana/v1.0.0?fetchchildren", &buf)
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := httptest.NewRecorder()

mcm.On("GetFFIWithChildren", mock.Anything, "ns1", "banana", "v1.0.0").
Return(&fftypes.FFI{}, nil)
r.ServeHTTP(res, req)

assert.Equal(t, 200, res.Result().StatusCode)
}
41 changes: 41 additions & 0 deletions internal/apiserver/route_get_status_websockets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright © 2022 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package apiserver

import (
"net/http"

"github.com/hyperledger/firefly/internal/coremsgs"
"github.com/hyperledger/firefly/internal/oapispec"
"github.com/hyperledger/firefly/pkg/fftypes"
)

var getStatusWebSockets = &oapispec.Route{
Name: "getStatusWebSockets",
Path: "status/websockets",
Method: http.MethodGet,
PathParams: nil,
QueryParams: nil,
FilterFactory: nil,
Description: coremsgs.APIMessageTBD,
JSONInputValue: nil,
JSONOutputValue: func() interface{} { return &fftypes.WebSocketStatus{} },
JSONOutputCodes: []int{http.StatusOK},
JSONHandler: func(r *oapispec.APIRequest) (output interface{}, err error) {
return getOr(r.Ctx).Events().GetWebSocketStatus(), nil
},
}
40 changes: 40 additions & 0 deletions internal/apiserver/route_get_status_websockets_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright © 2021 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package apiserver

import (
"net/http/httptest"
"testing"

"github.com/hyperledger/firefly/mocks/eventmocks"
"github.com/hyperledger/firefly/pkg/fftypes"
"github.com/stretchr/testify/assert"
)

func TestGetStatusWebSockets(t *testing.T) {
o, r := newTestAPIServer()
mem := &eventmocks.EventManager{}
o.On("Events").Return(mem)
req := httptest.NewRequest("GET", "/api/v1/status/websockets", nil)
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := httptest.NewRecorder()

mem.On("GetWebSocketStatus").Return(&fftypes.WebSocketStatus{})
r.ServeHTTP(res, req)

assert.Equal(t, 200, res.Result().StatusCode)
}
2 changes: 2 additions & 0 deletions internal/apiserver/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var routes = []*oapispec.Route{
getBlockchainEvents,
getChartHistogram,
getContractAPIByName,
getContractAPIInterface,
getContractAPIs,
getContractAPIListeners,
getContractInterface,
Expand Down Expand Up @@ -67,6 +68,7 @@ var routes = []*oapispec.Route{
getStatus,
getStatusBatchManager,
getStatusPins,
getStatusWebSockets,
getSubscriptionByID,
getSubscriptions,
getTokenAccountPools,
Expand Down
65 changes: 52 additions & 13 deletions internal/contracts/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ type Manager interface {

BroadcastFFI(ctx context.Context, ns string, ffi *fftypes.FFI, waitConfirm bool) (output *fftypes.FFI, err error)
GetFFI(ctx context.Context, ns, name, version string) (*fftypes.FFI, error)
GetFFIWithChildren(ctx context.Context, ns, name, version string) (*fftypes.FFI, error)
GetFFIByID(ctx context.Context, id *fftypes.UUID) (*fftypes.FFI, error)
GetFFIByIDWithChildren(ctx context.Context, id *fftypes.UUID) (*fftypes.FFI, error)
GetFFIs(ctx context.Context, ns string, filter database.AndFilter) ([]*fftypes.FFI, *database.FilterResult, error)

InvokeContract(ctx context.Context, ns string, req *fftypes.ContractCallRequest) (interface{}, error)
InvokeContractAPI(ctx context.Context, ns, apiName, methodPath string, req *fftypes.ContractCallRequest) (interface{}, error)
GetContractAPI(ctx context.Context, httpServerURL, ns, apiName string) (*fftypes.ContractAPI, error)
GetContractAPIInterface(ctx context.Context, ns, apiName string) (*fftypes.FFI, error)
GetContractAPIMethod(ctx context.Context, ns, apiName, methodPath string) (*fftypes.FFIMethod, error)
GetContractAPIEvent(ctx context.Context, ns, apiName, eventPath string) (*fftypes.FFIEvent, error)
GetContractAPIs(ctx context.Context, httpServerURL, ns string, filter database.AndFilter) ([]*fftypes.ContractAPI, *database.FilterResult, error)
BroadcastContractAPI(ctx context.Context, httpServerURL, ns string, api *fftypes.ContractAPI, waitConfirm bool) (output *fftypes.ContractAPI, err error)

Expand Down Expand Up @@ -147,29 +151,40 @@ func (cm *contractManager) GetFFI(ctx context.Context, ns, name, version string)
return cm.database.GetFFI(ctx, ns, name, version)
}

func (cm *contractManager) GetFFIWithChildren(ctx context.Context, ns, name, version string) (*fftypes.FFI, error) {
ffi, err := cm.GetFFI(ctx, ns, name, version)
if err == nil {
err = cm.getFFIChildren(ctx, ffi)
}
return ffi, err
}

func (cm *contractManager) GetFFIByID(ctx context.Context, id *fftypes.UUID) (*fftypes.FFI, error) {
return cm.database.GetFFIByID(ctx, id)
}

func (cm *contractManager) getFFIChildren(ctx context.Context, ffi *fftypes.FFI) (err error) {
mfb := database.FFIMethodQueryFactory.NewFilter(ctx)
ffi.Methods, _, err = cm.database.GetFFIMethods(ctx, mfb.Eq("interface", ffi.ID))
if err != nil {
return err
}

efb := database.FFIEventQueryFactory.NewFilter(ctx)
ffi.Events, _, err = cm.database.GetFFIEvents(ctx, efb.Eq("interface", ffi.ID))
if err != nil {
return err
}
return nil
}

func (cm *contractManager) GetFFIByIDWithChildren(ctx context.Context, id *fftypes.UUID) (ffi *fftypes.FFI, err error) {
err = cm.database.RunAsGroup(ctx, func(ctx context.Context) (err error) {
ffi, err = cm.database.GetFFIByID(ctx, id)
if err != nil || ffi == nil {
return err
}

mfb := database.FFIMethodQueryFactory.NewFilter(ctx)
ffi.Methods, _, err = cm.database.GetFFIMethods(ctx, mfb.Eq("interface", id))
if err != nil {
return err
}

efb := database.FFIEventQueryFactory.NewFilter(ctx)
ffi.Events, _, err = cm.database.GetFFIEvents(ctx, efb.Eq("interface", id))
if err != nil {
return err
}
return nil
return cm.getFFIChildren(ctx, ffi)
})
return ffi, err
}
Expand Down Expand Up @@ -277,6 +292,30 @@ func (cm *contractManager) GetContractAPI(ctx context.Context, httpServerURL, ns
return api, err
}

func (cm *contractManager) GetContractAPIInterface(ctx context.Context, ns, apiName string) (*fftypes.FFI, error) {
api, err := cm.GetContractAPI(ctx, "", ns, apiName)
if err != nil || api == nil {
return nil, err
}
return cm.GetFFIByIDWithChildren(ctx, api.Interface.ID)
}

func (cm *contractManager) GetContractAPIMethod(ctx context.Context, ns, apiName, methodPath string) (*fftypes.FFIMethod, error) {
api, err := cm.GetContractAPI(ctx, "", ns, apiName)
if err != nil || api == nil {
return nil, err
}
return cm.database.GetFFIMethod(ctx, ns, api.Interface.ID, methodPath)
}

func (cm *contractManager) GetContractAPIEvent(ctx context.Context, ns, apiName, eventPath string) (*fftypes.FFIEvent, error) {
api, err := cm.GetContractAPI(ctx, "", ns, apiName)
if err != nil || api == nil {
return nil, err
}
return cm.database.GetFFIEvent(ctx, ns, api.Interface.ID, eventPath)
}

func (cm *contractManager) GetContractAPIs(ctx context.Context, httpServerURL, ns string, filter database.AndFilter) ([]*fftypes.ContractAPI, *database.FilterResult, error) {
filter = cm.scopeNS(ns, filter)
apis, fr, err := cm.database.GetContractAPIs(ctx, ns, filter)
Expand Down
Loading

0 comments on commit 87e5a23

Please sign in to comment.