forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdk_provider_test.go
196 lines (164 loc) · 6.36 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package provider
import (
"fmt"
"strconv"
"strings"
"testing"
"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/common/errors/retry"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/status"
"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
"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"
"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/test/integration"
"github.com/stretchr/testify/require"
)
func TestDynamicSelection(t *testing.T) {
// Using shared SDK instance to increase test speed.
testSetup := mainTestSetup
aKey := integration.GetKeyName(t)
bKey := integration.GetKeyName(t)
moveTxArg := integration.ExampleCCTxArgs(aKey, bKey, "1")
queryArg := integration.ExampleCCQueryArgs(bKey)
// 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.GenerateExampleID(false)
err = integration.PrepareExampleCC(sdk, fabsdk.WithUser("Admin"), testSetup.OrgID, chaincodeID)
require.NoError(t, err, "InstallAndInstantiateExampleCC returned error")
//prepare contexts
org1ChannelClientContext := sdk.ChannelContext(testSetup.ChannelID, fabsdk.WithUser(org1User), fabsdk.WithOrg(org1Name))
//Reset example cc keys
integration.ResetKeys(t, org1ChannelClientContext, chaincodeID, "200", aKey, bKey)
chClient, err := channel.New(org1ChannelClientContext)
require.NoError(t, err, "Failed to create new channel client")
//response, err := chClient.Query(channel.Request{ChaincodeID: chaincodeID, Fcn: "invoke", Args: queryArg},
// channel.WithRetry(retry.TestRetryOpts))
response, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
b, e := chClient.Query(channel.Request{ChaincodeID: chaincodeID, Fcn: "invoke", Args: queryArg},
channel.WithRetry(retry.TestRetryOpts))
if e != nil {
// return a retryable code if key/value query is nil (ie not propagated to all peers yet)
if strings.Contains(e.Error(), "Nil amount for") {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("QueryBlock returned error: %v", e), nil)
}
}
return b, e
},
)
require.NoError(t, err, "Failed to query funds, ccID: %s, queryArgs: [%+v]", chaincodeID, queryArg)
value := response.(channel.Response).Payload
// Move funds
_, err = chClient.Execute(channel.Request{ChaincodeID: chaincodeID, Fcn: "invoke", Args: moveTxArg},
channel.WithRetry(retry.DefaultChannelOpts))
require.NoError(t, err, "Failed to move funds, ccID: %s, queryArgs:[%+v]", chaincodeID, moveTxArg)
valueInt, _ := strconv.Atoi(string(value))
verifyValue(t, chClient, queryArg, valueInt+1, chaincodeID)
}
func verifyValue(t *testing.T, chClient *channel.Client, queryArg [][]byte, expectedValue int, ccID string) {
req := channel.Request{
ChaincodeID: ccID,
Fcn: "invoke",
Args: queryArg,
}
_, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
resp, err := chClient.Query(req, channel.WithRetry(retry.DefaultChannelOpts))
require.NoError(t, err, "query funds failed")
// Verify that transaction changed block state
actualValue, _ := strconv.Atoi(string(resp.Payload))
if expectedValue != actualValue {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("ledger value didn't match expectation [%d, %d]", expectedValue, actualValue), nil)
}
return &actualValue, nil
},
)
require.NoError(t, err, "Execute failed. Value was not updated")
}
// 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, opts ...options.Opt) (fab.ChannelProvider, error) {
chProvider, err := chpvdr.New(config, opts...)
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
}
// 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
}
type closable interface {
Close()
}