Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 0d62377

Browse files
committed
[FABG-693] Add Block Height to Dynamic Discovery Peers
Add PeerState to the set of interfaces implemented by the peer endpoint returned from the Dynamic Discovery service. Change-Id: Ifdcde9e2a2d4e18b614731b0d9568257a7fa0ea9 Signed-off-by: Bob Stasyszyn <Bob.Stasyszyn@securekey.com>
1 parent 72bc6c6 commit 0d62377

File tree

6 files changed

+73
-24
lines changed

6 files changed

+73
-24
lines changed

pkg/client/common/discovery/dynamicdiscovery/chservice.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,31 @@ func (s *ChannelService) evaluate(ctx contextAPI.Client, responses []fabdiscover
103103
logger.Warn(lastErr.Error())
104104
continue
105105
}
106-
return asPeers(ctx, endpoints), nil
106+
return s.asPeers(ctx, endpoints), nil
107107
}
108108
return nil, lastErr
109109
}
110+
111+
func (s *ChannelService) asPeers(ctx contextAPI.Client, endpoints []*discclient.Peer) []fab.Peer {
112+
var peers []fab.Peer
113+
for _, endpoint := range endpoints {
114+
peer, ok := asPeer(ctx, endpoint)
115+
if !ok {
116+
continue
117+
}
118+
peers = append(peers, &peerEndpoint{
119+
Peer: peer,
120+
blockHeight: endpoint.StateInfoMessage.GetStateInfo().GetProperties().LedgerHeight,
121+
})
122+
}
123+
return peers
124+
}
125+
126+
type peerEndpoint struct {
127+
fab.Peer
128+
blockHeight uint64
129+
}
130+
131+
func (p *peerEndpoint) BlockHeight() uint64 {
132+
return p.blockHeight
133+
}

pkg/client/common/discovery/dynamicdiscovery/chservice_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111
"time"
1212

13+
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/discovery"
1314
clientmocks "github.com/hyperledger/fabric-sdk-go/pkg/client/common/mocks"
1415
contextAPI "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
1516
pfab "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
@@ -101,7 +102,7 @@ func TestDiscoveryService(t *testing.T) {
101102
{
102103
MSPID: mspID2,
103104
Endpoint: peer1MSP2,
104-
LedgerHeight: 5,
105+
LedgerHeight: 15,
105106
},
106107
},
107108
},
@@ -112,4 +113,20 @@ func TestDiscoveryService(t *testing.T) {
112113
peers, err = service.GetPeers()
113114
assert.NoError(t, err)
114115
assert.Equalf(t, 2, len(peers), "Expected 2 peers")
116+
117+
filteredService := discovery.NewDiscoveryFilterService(service, &blockHeightFilter{minBlockHeight: 10})
118+
peers, err = filteredService.GetPeers()
119+
require.NoError(t, err)
120+
require.Equalf(t, 1, len(peers), "expecting discovery filter to return only one peer")
121+
}
122+
123+
type blockHeightFilter struct {
124+
minBlockHeight uint64
125+
}
126+
127+
func (f *blockHeightFilter) Accept(peer pfab.Peer) bool {
128+
if p, ok := peer.(pfab.PeerState); ok {
129+
return p.BlockHeight() >= f.minBlockHeight
130+
}
131+
panic("expecting peer to have state")
115132
}

pkg/client/common/discovery/dynamicdiscovery/service.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,23 +124,31 @@ func (s *service) discoveryClient() discoveryClient {
124124
func asPeers(ctx contextAPI.Client, endpoints []*discclient.Peer) []fab.Peer {
125125
var peers []fab.Peer
126126
for _, endpoint := range endpoints {
127-
url := endpoint.AliveMessage.GetAliveMsg().Membership.Endpoint
128-
129-
logger.Debugf("Adding endpoint [%s]", url)
130-
131-
peerConfig, found := ctx.EndpointConfig().PeerConfig(url)
132-
if !found {
133-
logger.Debugf("Peer config not found for url [%s]", url)
134-
continue
135-
}
136-
137-
peer, err := ctx.InfraProvider().CreatePeerFromConfig(&fab.NetworkPeer{PeerConfig: *peerConfig, MSPID: endpoint.MSPID})
138-
if err != nil {
139-
logger.Warnf("Unable to create peer config for [%s]: %s", url, err)
127+
peer, ok := asPeer(ctx, endpoint)
128+
if !ok {
140129
continue
141130
}
142131
peers = append(peers, peer)
143132
}
144-
145133
return peers
146134
}
135+
136+
func asPeer(ctx contextAPI.Client, endpoint *discclient.Peer) (fab.Peer, bool) {
137+
url := endpoint.AliveMessage.GetAliveMsg().Membership.Endpoint
138+
139+
logger.Debugf("Adding endpoint [%s]", url)
140+
141+
peerConfig, found := ctx.EndpointConfig().PeerConfig(url)
142+
if !found {
143+
logger.Debugf("Peer config not found for url [%s]", url)
144+
return nil, false
145+
}
146+
147+
peer, err := ctx.InfraProvider().CreatePeerFromConfig(&fab.NetworkPeer{PeerConfig: *peerConfig, MSPID: endpoint.MSPID})
148+
if err != nil {
149+
logger.Warnf("Unable to create peer config for [%s]: %s", url, err)
150+
return nil, false
151+
}
152+
153+
return peer, true
154+
}

pkg/client/common/selection/fabricselection/fabricselection.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ var defaultRetryOpts = retry.Opts{
5252
RetryableCodes: retryableCodes,
5353
}
5454

55-
// PeerState provides state information about the Peer
56-
type PeerState interface {
57-
BlockHeight() uint64
58-
}
59-
6055
type discoveryClient interface {
6156
Send(ctx context.Context, req *discclient.Request, targets ...fab.PeerConfig) ([]fabdiscovery.Response, error)
6257
}

pkg/client/common/selection/fabricselection/selection_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func TestSelection(t *testing.T) {
160160
t.Run("Peer Filter", func(t *testing.T) {
161161
endorsers, err := service.GetEndorsersForChaincode([]*fab.ChaincodeCall{{ID: cc1}},
162162
options.WithPeerFilter(func(peer fab.Peer) bool {
163-
return peer.(PeerState).BlockHeight() > 1001
163+
return peer.(fab.PeerState).BlockHeight() > 1001
164164
}),
165165
)
166166

@@ -170,7 +170,7 @@ func TestSelection(t *testing.T) {
170170
// Ensure the endorsers all have a block height > 1001 and they are returned in descending order of block height
171171
lastBlockHeight := uint64(9999999)
172172
for _, endorser := range endorsers {
173-
blockHeight := endorser.(PeerState).BlockHeight()
173+
blockHeight := endorser.(fab.PeerState).BlockHeight()
174174
assert.Truef(t, blockHeight > 1001, "Expecting block height to be > 1001")
175175
assert.Truef(t, blockHeight <= lastBlockHeight, "Expecting endorsers to be returned in order of descending block height. Block Height: %d, Last Block Height: %d", blockHeight, lastBlockHeight)
176176
lastBlockHeight = blockHeight
@@ -268,7 +268,7 @@ func TestWithDiscoveryFilter(t *testing.T) {
268268

269269
endorsers, err := service.GetEndorsersForChaincode([]*fab.ChaincodeCall{cc1ChaincodeCall},
270270
options.WithPeerFilter(func(peer fab.Peer) bool {
271-
return peer.(PeerState).BlockHeight() > 1001
271+
return peer.(fab.PeerState).BlockHeight() > 1001
272272
}))
273273
assert.NoError(t, err)
274274
assert.Equalf(t, 3, len(endorsers), "Expecting 3 endorser")

pkg/common/providers/fab/peer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ type Peer interface {
1818

1919
// TODO: Roles, Name, EnrollmentCertificate (if needed)
2020
}
21+
22+
// PeerState provides state information about the Peer
23+
type PeerState interface {
24+
BlockHeight() uint64
25+
}

0 commit comments

Comments
 (0)