You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.2 KiB
38 lines
1.2 KiB
2 years ago
|
package sdkInit
|
||
|
|
||
|
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
|
||
|
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/status"
|
||
|
contextAPI "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
|
||
|
fabAPI "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
|
||
|
contextImpl "github.com/hyperledger/fabric-sdk-go/pkg/context"
|
||
|
)
|
||
|
|
||
|
func DiscoverLocalPeers(ctxProvider contextAPI.ClientProvider, expectedPeers int) ([]fabAPI.Peer, error) {
|
||
|
ctx, err := contextImpl.NewLocal(ctxProvider)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("error creating local context: %v", err)
|
||
|
}
|
||
|
|
||
|
discoveredPeers, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
|
||
|
func() (interface{}, error) {
|
||
|
peers, serviceErr := ctx.LocalDiscoveryService().GetPeers()
|
||
|
if serviceErr != nil {
|
||
|
return nil, fmt.Errorf("getting peers for MSP [%s] error: %v", ctx.Identifier().MSPID, serviceErr)
|
||
|
}
|
||
|
if len(peers) < expectedPeers {
|
||
|
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("Expecting %d peers but got %d", expectedPeers, len(peers)), nil)
|
||
|
}
|
||
|
return peers, nil
|
||
|
},
|
||
|
)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return discoveredPeers.([]fabAPI.Peer), nil
|
||
|
}
|