Skip to content

Commit

Permalink
Merge pull request #1054 from mysteriumnetwork/feature/proposal-compo…
Browse files Browse the repository at this point in the history
…site-id

Introduce ProposalID for unique proposal discovery with composite identifier
  • Loading branch information
Waldz authored May 13, 2019
2 parents 2e06e24 + e137141 commit 87e869f
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 38 deletions.
45 changes: 31 additions & 14 deletions market/mysterium/mysterium_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,26 +151,43 @@ func (mApi *MysteriumAPI) PingProposal(proposal market.ServiceProposal, signer i
return err
}

// FindProposals fetches currently active service proposals from discovery
func (mApi *MysteriumAPI) FindProposals(providerID, serviceType, accessPolicyID, accessPolicySource string) ([]market.ServiceProposal, error) {
values := url.Values{}
if providerID != "" {
values.Set("node_key", providerID)
// GetProposal fetches service proposal from discovery by exact ID
func (mApi *MysteriumAPI) GetProposal(id market.ProposalID) (*market.ServiceProposal, error) {
proposals, err := mApi.doProposalRequest(url.Values{
"node_key": {id.ProviderID},
"service_type": {id.ServiceType},
})
if err != nil {
return nil, err
}

if serviceType != "" {
values.Set("service_type", serviceType)
if len(proposals) == 0 {
return nil, nil
}

if accessPolicyID != "" {
values.Set("access_policy[id]", accessPolicyID)
}
return &proposals[0], nil
}

if accessPolicySource != "" {
values.Set("access_policy[source]", accessPolicySource)
// FindProposals fetches currently active service proposals from discovery
func (mApi *MysteriumAPI) FindProposals(filter market.ProposalFilter) ([]market.ServiceProposal, error) {
query := url.Values{}
if filter.ProviderID != "" {
query.Set("node_key", filter.ProviderID)
}
if filter.ServiceType != "" {
query.Set("service_type", filter.ServiceType)
}
if filter.AccessPolicyID != "" {
query.Set("access_policy[id]", filter.AccessPolicyID)
}
if filter.AccessPolicySource != "" {
query.Set("access_policy[source]", filter.AccessPolicySource)
}

return mApi.doProposalRequest(query)
}

req, err := requests.NewGetRequest(mApi.discoveryAPIAddress, "proposals", values)
func (mApi *MysteriumAPI) doProposalRequest(query url.Values) ([]market.ServiceProposal, error) {
req, err := requests.NewGetRequest(mApi.discoveryAPIAddress, "proposals", query)
if err != nil {
return nil, err
}
Expand Down
26 changes: 26 additions & 0 deletions market/service_proposal_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package market

// ProposalFilter defines all flags for proposal filtering in discovery of Mysterium Network
type ProposalFilter struct {
ProviderID string
ServiceType string
AccessPolicyID string
AccessPolicySource string
}
30 changes: 30 additions & 0 deletions market/service_proposal_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package market

// ProposalID defines composite ID to identify unique proposal discovery of Mysterium Network
type ProposalID struct {
// Type of service type offered
ServiceType string

// Unique identifier of a provider
ProviderID string

// Per provider unique serial number of service description provided
ID int
}
13 changes: 8 additions & 5 deletions tequilapi/endpoints/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/mysteriumnetwork/node/core/connection"
"github.com/mysteriumnetwork/node/core/ip"
"github.com/mysteriumnetwork/node/identity"
"github.com/mysteriumnetwork/node/market"
"github.com/mysteriumnetwork/node/tequilapi/utils"
"github.com/mysteriumnetwork/node/tequilapi/validation"
)
Expand Down Expand Up @@ -196,20 +197,22 @@ func (ce *ConnectionEndpoint) Create(resp http.ResponseWriter, req *http.Request
return
}

proposals, err := ce.proposalProvider.FindProposals(cr.ProviderID, cr.ServiceType, "", "")
// TODO Pass proposal ID directly in request
proposal, err := ce.proposalProvider.GetProposal(market.ProposalID{
ProviderID: cr.ProviderID,
ServiceType: cr.ServiceType,
})
if err != nil {
utils.SendError(resp, err, http.StatusInternalServerError)
return
}
if len(proposals) == 0 {
if proposal == nil {
utils.SendError(resp, errors.New("provider has no service proposals"), http.StatusBadRequest)
return
}

proposal := proposals[0]

connectOptions := getConnectOptions(cr)
err = ce.manager.Connect(identity.FromAddress(cr.ConsumerID), proposal, connectOptions)
err = ce.manager.Connect(identity.FromAddress(cr.ConsumerID), *proposal, connectOptions)

if err != nil {
switch err {
Expand Down
14 changes: 8 additions & 6 deletions tequilapi/endpoints/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ func mapProposalsToRes(

// ProposalProvider allows to fetch proposals by specified params
type ProposalProvider interface {
FindProposals(providerID, serviceType, accessPolicyID, accessPolicySource string) ([]market.ServiceProposal, error)
GetProposal(id market.ProposalID) (*market.ServiceProposal, error)
FindProposals(filter market.ProposalFilter) ([]market.ServiceProposal, error)
}

type proposalsEndpoint struct {
Expand Down Expand Up @@ -161,13 +162,14 @@ func NewProposalsEndpoint(proposalProvider ProposalProvider, morqaClient metrics
// schema:
// "$ref": "#/definitions/ErrorMessageDTO"
func (pe *proposalsEndpoint) List(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
providerID := req.URL.Query().Get("providerId")
serviceType := req.URL.Query().Get("serviceType")
fetchConnectCounts := req.URL.Query().Get("fetchConnectCounts")
accessPolicyID := req.URL.Query().Get("accessPolicyId")
accessPolicySource := req.URL.Query().Get("accessPolicySource")

proposals, err := pe.proposalProvider.FindProposals(providerID, serviceType, accessPolicyID, accessPolicySource)
proposals, err := pe.proposalProvider.FindProposals(market.ProposalFilter{
ProviderID: req.URL.Query().Get("providerId"),
ServiceType: req.URL.Query().Get("serviceType"),
AccessPolicyID: req.URL.Query().Get("accessPolicyId"),
AccessPolicySource: req.URL.Query().Get("accessPolicySource"),
})
if err != nil {
utils.SendError(resp, err, http.StatusInternalServerError)
return
Expand Down
32 changes: 19 additions & 13 deletions tequilapi/endpoints/proposals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestProposalsEndpointListByNodeId(t *testing.T) {
}`,
resp.Body.String(),
)
assert.Equal(t, "0xProviderId", mockProposalProvider.recordedProviderID)
assert.Equal(t, market.ProposalFilter{ProviderID: "0xProviderId"}, mockProposalProvider.recordedFilter)
}

func TestProposalsEndpointAcceptsAccessPolicyParams(t *testing.T) {
Expand Down Expand Up @@ -133,8 +133,13 @@ func TestProposalsEndpointAcceptsAccessPolicyParams(t *testing.T) {
}`,
resp.Body.String(),
)
assert.Equal(t, "accessPolicySource", mockProposalProvider.recordedAccessPolicySource)
assert.Equal(t, "accessPolicyId", mockProposalProvider.recordedAccessPolicyID)
assert.Equal(t,
market.ProposalFilter{
AccessPolicyID: "accessPolicyId",
AccessPolicySource: "accessPolicySource",
},
mockProposalProvider.recordedFilter,
)
}

func TestProposalsEndpointList(t *testing.T) {
Expand Down Expand Up @@ -265,18 +270,19 @@ func (m *mysteriumMorqaFake) ProposalsMetrics() []json.RawMessage {
}

type mockProposalProvider struct {
recordedProviderID string
recordedServiceType string
recordedAccessPolicyID string
recordedAccessPolicySource string
proposals []market.ServiceProposal
recordedFilter market.ProposalFilter
proposals []market.ServiceProposal
}

func (mpp *mockProposalProvider) GetProposal(id market.ProposalID) (*market.ServiceProposal, error) {
if len(mpp.proposals) == 0 {
return nil, nil
}
return &mpp.proposals[0], nil
}

func (mpp *mockProposalProvider) FindProposals(providerID, serviceType, accessPolicyID, accessPolicySource string) ([]market.ServiceProposal, error) {
mpp.recordedProviderID = providerID
mpp.recordedServiceType = serviceType
mpp.recordedAccessPolicyID = accessPolicyID
mpp.recordedAccessPolicySource = accessPolicySource
func (mpp *mockProposalProvider) FindProposals(filter market.ProposalFilter) ([]market.ServiceProposal, error) {
mpp.recordedFilter = filter
return mpp.proposals, nil
}

Expand Down

0 comments on commit 87e869f

Please sign in to comment.