forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdk_provider_test.go
168 lines (136 loc) · 4.91 KB
/
sdk_provider_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package sdk
import (
"strconv"
"testing"
"github.com/hyperledger/fabric-sdk-go/test/integration"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/factory/defsvc"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/provider/chpvdr"
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/dynamicselection"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/stretchr/testify/require"
)
func TestDynamicSelection(t *testing.T) {
// Using shared SDK instance to increase test speed.
testSetup := mainTestSetup
// Create SDK setup for channel client with dynamic selection
sdk, err := fabsdk.New(integration.ConfigBackend,
fabsdk.WithServicePkg(&DynamicSelectionProviderFactory{}))
if err != nil {
t.Fatalf("Failed to create new SDK: %s", err)
}
defer sdk.Close()
if err = testSetup.Initialize(sdk); err != nil {
t.Fatal(err)
}
chainCodeID := integration.GenerateRandomID()
resp, err := integration.InstallAndInstantiateExampleCC(sdk, fabsdk.WithUser("Admin"), testSetup.OrgID, chainCodeID)
require.Nil(t, err, "InstallAndInstantiateExampleCC return error")
require.NotEmpty(t, resp, "instantiate response should be populated")
//prepare contexts
org1ChannelClientContext := sdk.ChannelContext(testSetup.ChannelID, fabsdk.WithUser(org1User), fabsdk.WithOrg(org1Name))
chClient, err := channel.New(org1ChannelClientContext)
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}
response, err := chClient.Query(channel.Request{ChaincodeID: chainCodeID, Fcn: "invoke", Args: integration.ExampleCCQueryArgs()},
channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
t.Fatalf("Failed to query funds: %s", err)
}
value := response.Payload
// Move funds
response, err = chClient.Execute(channel.Request{ChaincodeID: chainCodeID, Fcn: "invoke", Args: integration.ExampleCCTxArgs()},
channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
t.Fatalf("Failed to move funds: %s", err)
}
// Verify move funds transaction result
response, err = chClient.Query(channel.Request{ChaincodeID: chainCodeID, Fcn: "invoke", Args: integration.ExampleCCQueryArgs()})
if err != nil {
t.Fatalf("Failed to query funds after transaction: %s", err)
}
valueInt, _ := strconv.Atoi(string(value))
valueAfterInvokeInt, _ := strconv.Atoi(string(response.Payload))
if valueInt+1 != valueAfterInvokeInt {
t.Fatalf("Execute failed. Before: %s, after: %s", value, response.Payload)
}
}
// DynamicSelectionProviderFactory is configured with dynamic (endorser) selection provider
type DynamicSelectionProviderFactory struct {
defsvc.ProviderFactory
}
// CreateChannelProvider returns a new default implementation of channel provider
func (f *DynamicSelectionProviderFactory) CreateChannelProvider(config fab.EndpointConfig) (fab.ChannelProvider, error) {
chProvider, err := chpvdr.New(config)
if err != nil {
return nil, err
}
return &dynamicSelectionChannelProvider{
ChannelProvider: chProvider,
services: make(map[string]*dynamicselection.SelectionService),
}, nil
}
type dynamicSelectionChannelProvider struct {
fab.ChannelProvider
services map[string]*dynamicselection.SelectionService
}
type initializer interface {
Initialize(providers context.Providers) error
}
// Initialize sets the provider context
func (cp *dynamicSelectionChannelProvider) Initialize(providers context.Providers) error {
if init, ok := cp.ChannelProvider.(initializer); ok {
init.Initialize(providers)
}
return nil
}
type closable interface {
Close()
}
// Close frees resources and caches.
func (cp *dynamicSelectionChannelProvider) Close() {
if c, ok := cp.ChannelProvider.(closable); ok {
c.Close()
}
for _, service := range cp.services {
service.Close()
}
}
// ChannelService creates a ChannelService
func (cp *dynamicSelectionChannelProvider) ChannelService(ctx fab.ClientContext, channelID string) (fab.ChannelService, error) {
chService, err := cp.ChannelProvider.ChannelService(ctx, channelID)
if err != nil {
return nil, err
}
selection, ok := cp.services[channelID]
if !ok {
discovery, err := chService.Discovery()
if err != nil {
return nil, err
}
selection, err := dynamicselection.NewService(ctx, channelID, discovery)
if err != nil {
return nil, err
}
cp.services[channelID] = selection
}
return &dynamicSelectionChannelService{
ChannelService: chService,
selection: selection,
}, nil
}
type dynamicSelectionChannelService struct {
fab.ChannelService
selection fab.SelectionService
}
func (cs *dynamicSelectionChannelService) Selection() (fab.SelectionService, error) {
return cs.selection, nil
}