1 Star 0 Fork 0

BUPT-ZKJC/fabric-sdk-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mockselection.go 1.78 KB
一键复制 编辑 原始数据 按行查看 历史
MJL 提交于 2021-08-06 18:40 . first commit
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mocks
import (
selectopts "gitee.com/bupt-zkjc/fabric-sdk-go/pkg/client/common/selection/options"
"gitee.com/bupt-zkjc/fabric-sdk-go/pkg/common/options"
"gitee.com/bupt-zkjc/fabric-sdk-go/pkg/common/providers/context"
"gitee.com/bupt-zkjc/fabric-sdk-go/pkg/common/providers/fab"
"gitee.com/bupt-zkjc/fabric-sdk-go/pkg/fab/mocks"
)
// MockSelectionService implements mock selection service
type MockSelectionService struct {
Error error
Peers []fab.Peer
ChannelContext context.Channel
}
// NewMockSelectionService returns mock selection service
func NewMockSelectionService(err error, peers ...fab.Peer) *MockSelectionService {
return &MockSelectionService{Error: err, Peers: peers}
}
// GetEndorsersForChaincode mockcore retrieving endorsing peers
func (ds *MockSelectionService) GetEndorsersForChaincode(chaincodes []*fab.ChaincodeCall, opts ...options.Opt) ([]fab.Peer, error) {
if ds.Error != nil {
return nil, ds.Error
}
params := selectopts.NewParams(opts)
var peers []fab.Peer
if ds.ChannelContext != nil {
var err error
discovery, err := ds.ChannelContext.ChannelService().Discovery()
if err != nil {
return nil, err
}
peers, err = discovery.GetPeers()
if err != nil {
return nil, err
}
} else if ds.Peers == nil {
mockPeer := mocks.NewMockPeer("Peer1", "http://peer1.com")
peers = append(peers, mockPeer)
}
if params.PeerFilter != nil {
for _, p := range ds.Peers {
if params.PeerFilter(p) {
peers = append(peers, p)
}
}
} else {
peers = ds.Peers
}
if params.PeerSorter != nil {
sortedPeers := make([]fab.Peer, len(peers))
copy(sortedPeers, peers)
peers = params.PeerSorter(sortedPeers)
}
return peers, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/bupt-zkjc/fabric-sdk-go.git
git@gitee.com:bupt-zkjc/fabric-sdk-go.git
bupt-zkjc
fabric-sdk-go
fabric-sdk-go
84f269695ead

搜索帮助

0d507c66 1850385 C8b1a773 1850385