-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-7972] Whitelist discovery filter
Change-Id: Id36a762e260fcd38ff6b7f7783a2c8cc67e92adc Signed-off-by: Firas Qutishat <firas.qutishat@securekey.com>
- Loading branch information
Showing
12 changed files
with
216 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package discovery | ||
|
||
import ( | ||
"github.com/hyperledger/fabric-sdk-go/api/apifabclient" | ||
) | ||
|
||
// filterService implements discovery service | ||
type filterService struct { | ||
discoveryService apifabclient.DiscoveryService | ||
targetFilter apifabclient.TargetFilter | ||
} | ||
|
||
// NewDiscoveryFilterService return discovery service with filter | ||
func NewDiscoveryFilterService(discoveryService apifabclient.DiscoveryService, targetFilter apifabclient.TargetFilter) apifabclient.DiscoveryService { | ||
return &filterService{discoveryService: discoveryService, targetFilter: targetFilter} | ||
} | ||
|
||
// GetPeers is used to get peers | ||
func (fs *filterService) GetPeers() ([]apifabclient.Peer, error) { | ||
peers, err := fs.discoveryService.GetPeers() | ||
if err != nil { | ||
return nil, err | ||
} | ||
targets := filterTargets(peers, fs.targetFilter) | ||
return targets, nil | ||
} | ||
|
||
// filterTargets is helper method to filter peers | ||
func filterTargets(peers []apifabclient.Peer, filter apifabclient.TargetFilter) []apifabclient.Peer { | ||
|
||
if filter == nil { | ||
return peers | ||
} | ||
|
||
filteredPeers := []apifabclient.Peer{} | ||
for _, peer := range peers { | ||
if filter.Accept(peer) { | ||
filteredPeers = append(filteredPeers, peer) | ||
} | ||
} | ||
|
||
return filteredPeers | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package discovery | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric-sdk-go/api/apifabclient" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/config" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-txn/discovery/staticdiscovery" | ||
) | ||
|
||
type mockFilter struct { | ||
called bool | ||
} | ||
|
||
// Accept returns true if this peer is to be included in the target list | ||
func (df *mockFilter) Accept(peer apifabclient.Peer) bool { | ||
df.called = true | ||
return true | ||
} | ||
|
||
func TestDiscoveryFilter(t *testing.T) { | ||
|
||
config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml")() | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
|
||
discoveryProvider, err := staticdiscovery.NewDiscoveryProvider(config) | ||
if err != nil { | ||
t.Fatalf("Failed to setup discovery provider: %s", err) | ||
} | ||
|
||
discoveryService, err := discoveryProvider.NewDiscoveryService("mychannel") | ||
if err != nil { | ||
t.Fatalf("Failed to setup discovery service: %s", err) | ||
} | ||
|
||
discoveryFilter := &mockFilter{called: false} | ||
|
||
discoveryService = NewDiscoveryFilterService(discoveryService, discoveryFilter) | ||
|
||
peers, err := discoveryService.GetPeers() | ||
if err != nil { | ||
t.Fatalf("Failed to get peers from discovery service: %s", err) | ||
} | ||
|
||
// One peer is configured for "mychannel" | ||
expectedNumOfPeers := 1 | ||
if len(peers) != expectedNumOfPeers { | ||
t.Fatalf("Expecting %d, got %d peers", expectedNumOfPeers, len(peers)) | ||
} | ||
|
||
if !discoveryFilter.called { | ||
t.Fatalf("Expecting true, got false") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.