Skip to content

Commit

Permalink
[FAB-8866] Cleanup top level interfaces
Browse files Browse the repository at this point in the history
Change-Id: I5683197d7aa0632879cfcc811ec7fc724329ae52
Signed-off-by: Aleksandar Likic <aleksandar.likic@securekey.com>
Signed-off-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
Aleksandar Likic authored and troyronda committed Mar 14, 2018
1 parent 5030a01 commit a854819
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 60 deletions.
10 changes: 5 additions & 5 deletions pkg/client/ledger/ledger.go
Expand Up @@ -43,13 +43,13 @@ type Client struct {
ledger *channel.Ledger
}

// MSPFilter is default filter
type MSPFilter struct {
// mspFilter is default filter
type mspFilter struct {
mspID string
}

// Accept returns true if this peer is to be included in the target list
func (f *MSPFilter) Accept(peer fab.Peer) bool {
func (f *mspFilter) Accept(peer fab.Peer) bool {
return peer.MSPID() == f.mspID
}

Expand Down Expand Up @@ -84,7 +84,7 @@ func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client
if channelContext.MspID() == "" {
return nil, errors.New("mspID not available in user context")
}
filter := &MSPFilter{mspID: channelContext.MspID()}
filter := &mspFilter{mspID: channelContext.MspID()}
ledgerClient.filter = filter
}

Expand Down Expand Up @@ -183,7 +183,7 @@ func (c *Client) QueryBlockByHash(blockHash []byte, options ...RequestOption) (*
// This query will be made to specified targets.
// blockNumber: The number which is the ID of the Block.
// It returns the block.
func (c *Client) QueryBlock(blockNumber int, options ...RequestOption) (*common.Block, error) {
func (c *Client) QueryBlock(blockNumber uint64, options ...RequestOption) (*common.Block, error) {

opts, err := c.prepareRequestOpts(options...)
if err != nil {
Expand Down
34 changes: 17 additions & 17 deletions pkg/client/msp/msp.go
Expand Up @@ -19,46 +19,46 @@ import (

var logger = logging.NewLogger("fabsdk/client")

// MSP enables access to MSP services
type MSP struct {
// Client enables access to Client services
type Client struct {
orgName string
ctx context.Client
}

// ClientOption describes a functional parameter for the New constructor
type ClientOption func(*MSP) error
type ClientOption func(*Client) error

// WithOrg option
func WithOrg(orgName string) ClientOption {
return func(msp *MSP) error {
return func(msp *Client) error {
msp.orgName = orgName
return nil
}
}

// New creates a new MSP instance
func New(clientProvider context.ClientProvider, opts ...ClientOption) (*MSP, error) {
// New creates a new Client instance
func New(clientProvider context.ClientProvider, opts ...ClientOption) (*Client, error) {

ctx, err := clientProvider()
if err != nil {
return nil, errors.WithMessage(err, "failed to create MSP")
return nil, errors.WithMessage(err, "failed to create Client")
}

msp := MSP{
msp := Client{
ctx: ctx,
}

for _, param := range opts {
err := param(&msp)
if err != nil {
return nil, errors.WithMessage(err, "failed to create MSP")
return nil, errors.WithMessage(err, "failed to create Client")
}
}

if msp.orgName == "" {
clientConfig, err := ctx.Config().Client()
if err != nil {
return nil, errors.WithMessage(err, "failed to create MSP")
return nil, errors.WithMessage(err, "failed to create Client")
}
msp.orgName = clientConfig.Organization
}
Expand All @@ -74,7 +74,7 @@ func newCAClient(ctx context.Client, orgName string) (mspapi.CAClient, error) {
}
caClient, err := msp.NewCAClient(orgName, identityManager, ctx.UserStore(), ctx.CryptoSuite(), ctx.Config())
if err != nil {
return nil, errors.WithMessage(err, "failed to create CA MSP")
return nil, errors.WithMessage(err, "failed to create CA Client")
}

return caClient, nil
Expand Down Expand Up @@ -103,7 +103,7 @@ func WithSecret(secret string) EnrollmentOption {
//
// enrollmentID enrollment ID of a registered user
// opts represent enrollment options
func (c *MSP) Enroll(enrollmentID string, opts ...EnrollmentOption) error {
func (c *Client) Enroll(enrollmentID string, opts ...EnrollmentOption) error {

eo := enrollmentOptions{}
for _, param := range opts {
Expand All @@ -121,7 +121,7 @@ func (c *MSP) Enroll(enrollmentID string, opts ...EnrollmentOption) error {
}

// Reenroll reenrolls an enrolled user in order to obtain a new signed X509 certificate
func (c *MSP) Reenroll(enrollmentID string) error {
func (c *Client) Reenroll(enrollmentID string) error {
ca, err := newCAClient(c.ctx, c.orgName)
if err != nil {
return err
Expand All @@ -132,7 +132,7 @@ func (c *MSP) Reenroll(enrollmentID string) error {
// Register registers a User with the Fabric CA
// request: Registration Request
// Returns Enrolment Secret
func (c *MSP) Register(request *RegistrationRequest) (string, error) {
func (c *Client) Register(request *RegistrationRequest) (string, error) {
ca, err := newCAClient(c.ctx, c.orgName)
if err != nil {
return "", err
Expand All @@ -154,7 +154,7 @@ func (c *MSP) Register(request *RegistrationRequest) (string, error) {

// Revoke revokes a User with the Fabric CA
// request: Revocation Request
func (c *MSP) Revoke(request *RevocationRequest) (*RevocationResponse, error) {
func (c *Client) Revoke(request *RevocationRequest) (*RevocationResponse, error) {
ca, err := newCAClient(c.ctx, c.orgName)
if err != nil {
return nil, err
Expand All @@ -181,7 +181,7 @@ func (c *MSP) Revoke(request *RevocationRequest) (*RevocationResponse, error) {
}

// GetSigningIdentity returns a signing identity for the given user name
func (c *MSP) GetSigningIdentity(userName string) (*SigningIdentity, error) {
func (c *Client) GetSigningIdentity(userName string) (*SigningIdentity, error) {
user, err := c.GetUser(userName)
if err != nil {
if err == mspctx.ErrUserNotFound {
Expand All @@ -194,7 +194,7 @@ func (c *MSP) GetSigningIdentity(userName string) (*SigningIdentity, error) {
}

// GetUser returns a user for the given user name
func (c *MSP) GetUser(userName string) (User, error) {
func (c *Client) GetUser(userName string) (User, error) {
im, _ := c.ctx.IdentityManager(c.orgName)
user, err := im.GetUser(userName)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/msp/msp_test.go
Expand Up @@ -26,7 +26,7 @@ const (
configPath = "testdata/config_test.yaml"
)

// TestMSP is a unit test for MSP enrollment and re-enrollment scenarios
// TestMSP is a unit test for Client enrollment and re-enrollment scenarios
func TestMSP(t *testing.T) {

f := textFixture{}
Expand All @@ -35,7 +35,7 @@ func TestMSP(t *testing.T) {

ctxProvider := sdk.Context()

// Get the MSP.
// Get the Client.
// Without WithOrg option, it uses default client organization.
msp, err := New(ctxProvider)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/client/resmgmt/resmgmt.go
Expand Up @@ -105,13 +105,13 @@ type Client struct {
filter TargetFilter
}

// MSPFilter is default filter
type MSPFilter struct {
// mspFilter is default filter
type mspFilter struct {
mspID string
}

// Accept returns true if this peer is to be included in the target list
func (f *MSPFilter) Accept(peer fab.Peer) bool {
func (f *mspFilter) Accept(peer fab.Peer) bool {
return peer.MSPID() == f.mspID
}

Expand Down Expand Up @@ -157,7 +157,7 @@ func New(clientProvider context.ClientProvider, opts ...ClientOption) (*Client,
if ctx.MspID() == "" {
return nil, errors.New("mspID not available in user context")
}
rcFilter := &MSPFilter{mspID: ctx.MspID()}
rcFilter := &mspFilter{mspID: ctx.MspID()}
resourceClient.filter = rcFilter
}
return resourceClient, nil
Expand Down
20 changes: 10 additions & 10 deletions pkg/client/resmgmt/resmgmt_test.go
Expand Up @@ -212,7 +212,7 @@ func TestJoinChannelWithOptsRequiredParameters(t *testing.T) {
peers = append(peers, peer1)

// Test both targets and filter provided (error condition)
err = rc.JoinChannel("mychannel", WithTargets(peers...), WithTargetFilter(&MSPFilter{mspID: "MspID"}))
err = rc.JoinChannel("mychannel", WithTargets(peers...), WithTargetFilter(&mspFilter{mspID: "MspID"}))
if err == nil || !strings.Contains(err.Error(), "If targets are provided, filter cannot be provided") {
t.Fatalf("Should have failed if both target and filter provided")
}
Expand All @@ -224,7 +224,7 @@ func TestJoinChannelWithOptsRequiredParameters(t *testing.T) {
}

// Test filter only (filter has no match)
err = rc.JoinChannel("mychannel", WithTargetFilter(&MSPFilter{mspID: "MspID"}))
err = rc.JoinChannel("mychannel", WithTargetFilter(&mspFilter{mspID: "MspID"}))
if err == nil || !strings.Contains(err.Error(), "No targets available") {
t.Fatalf("InstallCC should have failed with no targets error")
}
Expand All @@ -241,7 +241,7 @@ func TestJoinChannelWithOptsRequiredParameters(t *testing.T) {
rc.discovery, _ = disProvider.CreateDiscoveryService("mychannel")

// Test filter only (filter has a match)
err = rc.JoinChannel("mychannel", WithTargetFilter(&MSPFilter{mspID: "Org1MSP"}))
err = rc.JoinChannel("mychannel", WithTargetFilter(&mspFilter{mspID: "Org1MSP"}))
if err != nil {
t.Fatalf(err.Error())
}
Expand Down Expand Up @@ -672,7 +672,7 @@ func TestInstallCCWithOptsRequiredParameters(t *testing.T) {
peers = append(peers, &peer)

// Test both targets and filter provided (error condition)
_, err = rc.InstallCC(req, WithTargets(peers...), WithTargetFilter(&MSPFilter{mspID: "Org1MSP"}))
_, err = rc.InstallCC(req, WithTargets(peers...), WithTargetFilter(&mspFilter{mspID: "Org1MSP"}))
if err == nil {
t.Fatalf("Should have failed if both target and filter provided")
}
Expand All @@ -692,7 +692,7 @@ func TestInstallCCWithOptsRequiredParameters(t *testing.T) {
}

// Test filter only provided (filter rejects discovery service peer msp)
_, err = rc.InstallCC(req, WithTargetFilter(&MSPFilter{mspID: "Org2MSP"}))
_, err = rc.InstallCC(req, WithTargetFilter(&mspFilter{mspID: "Org2MSP"}))
if err == nil {
t.Fatalf("Should have failed with no targets since filter rejected all discovery targets")
}
Expand Down Expand Up @@ -848,7 +848,7 @@ func TestInstantiateCCWithOptsRequiredParameters(t *testing.T) {
peers = append(peers, &peer)

// Test both targets and filter provided (error condition)
err = rc.InstantiateCC("mychannel", req, WithTargets(peers...), WithTargetFilter(&MSPFilter{mspID: "Org1MSP"}))
err = rc.InstantiateCC("mychannel", req, WithTargets(peers...), WithTargetFilter(&mspFilter{mspID: "Org1MSP"}))
if err == nil {
t.Fatalf("Should have failed if both target and filter provided")
}
Expand All @@ -868,7 +868,7 @@ func TestInstantiateCCWithOptsRequiredParameters(t *testing.T) {
}

// Test filter only provided (filter rejects discovery service peer msp)
err = rc.InstantiateCC("mychannel", req, WithTargetFilter(&MSPFilter{mspID: "Org2MSP"}))
err = rc.InstantiateCC("mychannel", req, WithTargetFilter(&mspFilter{mspID: "Org2MSP"}))
if err == nil {
t.Fatalf("Should have failed with no targets since filter rejected all discovery targets")
}
Expand Down Expand Up @@ -1054,7 +1054,7 @@ func TestUpgradeCCWithOptsRequiredParameters(t *testing.T) {
peers = append(peers, &peer)

// Test both targets and filter provided (error condition)
err = rc.UpgradeCC("mychannel", req, WithTargets(peers...), WithTargetFilter(&MSPFilter{mspID: "Org1MSP"}))
err = rc.UpgradeCC("mychannel", req, WithTargets(peers...), WithTargetFilter(&mspFilter{mspID: "Org1MSP"}))
if err == nil {
t.Fatalf("Should have failed if both target and filter provided")
}
Expand All @@ -1074,7 +1074,7 @@ func TestUpgradeCCWithOptsRequiredParameters(t *testing.T) {
}

// Test filter only provided (filter rejects discovery service peer msp)
err = rc.UpgradeCC("mychannel", req, WithTargetFilter(&MSPFilter{mspID: "Org2MSP"}))
err = rc.UpgradeCC("mychannel", req, WithTargetFilter(&mspFilter{mspID: "Org2MSP"}))
if err == nil {
t.Fatalf("Should have failed with no targets since filter rejected all discovery targets")
}
Expand Down Expand Up @@ -1232,7 +1232,7 @@ func TestCCProposal(t *testing.T) {
}

func getDefaultTargetFilterOption() ClientOption {
targetFilter := &MSPFilter{mspID: "Org1MSP"}
targetFilter := &mspFilter{mspID: "Org1MSP"}
return WithDefaultTargetFilter(targetFilter)
}

Expand Down
6 changes: 1 addition & 5 deletions pkg/fab/channel/ledger.go
Expand Up @@ -99,11 +99,7 @@ func (c *Ledger) QueryBlockByHash(reqCtx reqContext.Context, blockHash []byte, t
// This query will be made to specified targets.
// blockNumber: The number which is the ID of the Block.
// It returns the block.
func (c *Ledger) QueryBlock(reqCtx reqContext.Context, blockNumber int, targets []fab.ProposalProcessor) ([]*common.Block, error) {

if blockNumber < 0 {
return nil, errors.New("blockNumber must be a positive integer")
}
func (c *Ledger) QueryBlock(reqCtx reqContext.Context, blockNumber uint64, targets []fab.ProposalProcessor) ([]*common.Block, error) {

cir := createBlockByNumberInvokeRequest(c.chName, blockNumber)
tprs, errs := queryChaincode(reqCtx, c.chName, cir, targets)
Expand Down
7 changes: 1 addition & 6 deletions pkg/fab/channel/ledger_test.go
Expand Up @@ -45,12 +45,7 @@ func TestQueryMethods(t *testing.T) {
reqCtx, cancel := context.NewRequest(setupContext(), context.WithTimeout(10*time.Second))
defer cancel()

_, err := channel.QueryBlock(reqCtx, -1, []fab.ProposalProcessor{&peer})
if err == nil {
t.Fatalf("Query block cannot be negative number")
}

_, err = channel.QueryBlockByHash(reqCtx, nil, []fab.ProposalProcessor{&peer})
_, err := channel.QueryBlockByHash(reqCtx, nil, []fab.ProposalProcessor{&peer})
if err == nil {
t.Fatalf("Query hash cannot be nil")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/fab/channel/qscc.go
Expand Up @@ -59,11 +59,11 @@ func createBlockByHashInvokeRequest(channelID string, blockHash []byte) fab.Chai
return cir
}

func createBlockByNumberInvokeRequest(channelID string, blockNumber int) fab.ChaincodeInvokeRequest {
func createBlockByNumberInvokeRequest(channelID string, blockNumber uint64) fab.ChaincodeInvokeRequest {

var args [][]byte
args = append(args, []byte(channelID))
args = append(args, []byte(strconv.Itoa(blockNumber)))
args = append(args, []byte(strconv.FormatUint(blockNumber, 10)))

cir := fab.ChaincodeInvokeRequest{
ChaincodeID: qscc,
Expand Down
2 changes: 1 addition & 1 deletion pkg/fabsdk/fabsdk.go
Expand Up @@ -61,7 +61,7 @@ func WithConfig(config core.Config) core.ConfigProvider {

// fromPkgSuite creates an SDK based on the implementations in the provided pkg suite.
// TODO: For now leaving this method as private until we have more usage.
func fromPkgSuite(config core.Config, pkgSuite PkgSuite, opts ...Option) (*FabricSDK, error) {
func fromPkgSuite(config core.Config, pkgSuite pkgSuite, opts ...Option) (*FabricSDK, error) {
core, err := pkgSuite.Core()
if err != nil {
return nil, errors.WithMessage(err, "Unable to initialize core pkg")
Expand Down
4 changes: 2 additions & 2 deletions pkg/fabsdk/factory.go
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/logging/api"
)

// PkgSuite provides the package factories that create clients and providers
type PkgSuite interface {
// pkgSuite provides the package factories that create clients and providers
type pkgSuite interface {
Core() (sdkApi.CoreProviderFactory, error)
MSP() (sdkApi.MSPProviderFactory, error)
Service() (sdkApi.ServiceProviderFactory, error)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/msp/enrollment_test.go
Expand Up @@ -41,7 +41,7 @@ func TestRegisterEnroll(t *testing.T) {

ctxProvider := sdk.Context()

// Get the MSP.
// Get the Client.
// Without WithOrg option, uses default client organization.
mspClient, err := msp.New(ctxProvider)
if err != nil {
Expand Down Expand Up @@ -104,7 +104,7 @@ func getRegistrarEnrollmentCredentials(t *testing.T, config core.Config) (string

clientConfig, err := config.Client()
if err != nil {
t.Fatalf("config.MSP() failed: %v", err)
t.Fatalf("config.Client() failed: %v", err)
}

myOrg := clientConfig.Organization
Expand Down
6 changes: 3 additions & 3 deletions test/integration/msp/user_data_mgmt_test.go
Expand Up @@ -79,11 +79,11 @@ func TestWithCustomStores(t *testing.T) {

ctxProvider := sdk.Context()

// Get the MSP.
// Get the Client.
// Without WithOrg option, uses default client organization.
mspClient, err := msp.New(ctxProvider)
if err != nil {
t.Fatalf("failed to create MSP: %v", err)
t.Fatalf("failed to create Client: %v", err)
}

// As this integration test spawns a fresh CA instance,
Expand Down Expand Up @@ -155,7 +155,7 @@ func getMyMSPID(t *testing.T, config core.Config) string {

clientConfig, err := config.Client()
if err != nil {
t.Fatalf("config.MSP() failed: %v", err)
t.Fatalf("config.Client() failed: %v", err)
}

netConfig, err := config.NetworkConfig()
Expand Down
2 changes: 1 addition & 1 deletion test/integration/orgs/multiple_orgs_test.go
Expand Up @@ -248,7 +248,7 @@ func testWithOrg1(t *testing.T, sdk *fabsdk.FabricSDK) int {
}

// Test Query Block by Hash - retrieve current block by number
block, err = ledgerClient.QueryBlock(int(ledgerInfoAfter.BCI.Height)-1, ledger.WithTargets(orgTestPeer0.(fab.Peer), orgTestPeer1.(fab.Peer)), ledger.WithMinTargets(2))
block, err = ledgerClient.QueryBlock(ledgerInfoAfter.BCI.Height-1, ledger.WithTargets(orgTestPeer0.(fab.Peer), orgTestPeer1.(fab.Peer)), ledger.WithMinTargets(2))
if err != nil {
t.Fatalf("QueryBlock return error: %v", err)
}
Expand Down

0 comments on commit a854819

Please sign in to comment.