forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_test_setup.go
246 lines (201 loc) · 7.2 KB
/
base_test_setup.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package integration
import (
"fmt"
"os"
"path"
"time"
"github.com/hyperledger/fabric-sdk-go/config"
"github.com/hyperledger/fabric-sdk-go/fabric-client/events"
fcUtil "github.com/hyperledger/fabric-sdk-go/fabric-client/helpers"
fabricClient "github.com/hyperledger/fabric-sdk-go/fabric-client"
bccspFactory "github.com/hyperledger/fabric/bccsp/factory"
)
// BaseSetupImpl implementation of BaseTestSetup
type BaseSetupImpl struct {
Client fabricClient.Client
Chain fabricClient.Chain
EventHub events.EventHub
ConnectEventHub bool
ConfigFile string
ChainID string
ChainCodeID string
Initialized bool
ChannelConfig string
}
// Initialize reads configuration from file and sets up client, chain and event hub
func (setup *BaseSetupImpl) Initialize() error {
if err := setup.InitConfig(); err != nil {
return fmt.Errorf("Init from config failed: %v", err)
}
// Initialize bccsp factories before calling get client
err := bccspFactory.InitFactories(&bccspFactory.FactoryOpts{
ProviderName: "SW",
SwOpts: &bccspFactory.SwOpts{
HashFamily: config.GetSecurityAlgorithm(),
SecLevel: config.GetSecurityLevel(),
FileKeystore: &bccspFactory.FileKeystoreOpts{
KeyStorePath: config.GetKeyStorePath(),
},
Ephemeral: false,
},
})
if err != nil {
return fmt.Errorf("Failed getting ephemeral software-based BCCSP [%s]", err)
}
client, err := fcUtil.GetClient("admin", "adminpw", "/tmp/enroll_user")
if err != nil {
return fmt.Errorf("Create client failed: %v", err)
}
setup.Client = client
chain, err := fcUtil.GetChain(setup.Client, setup.ChainID)
if err != nil {
return fmt.Errorf("Create chain (%s) failed: %v", setup.ChainID, err)
}
setup.Chain = chain
// Create and join channel
if err := fcUtil.CreateAndJoinChannel(client, chain, setup.ChannelConfig); err != nil {
return fmt.Errorf("CreateAndJoinChannel return error: %v", err)
}
if err := setup.setupEventHub(); err != nil {
return err
}
setup.Initialized = true
return nil
}
func (setup *BaseSetupImpl) setupEventHub() error {
eventHub, err := getEventHub()
if err != nil {
return err
}
if setup.ConnectEventHub {
if err := eventHub.Connect(); err != nil {
return fmt.Errorf("Failed eventHub.Connect() [%s]", err)
}
}
setup.EventHub = eventHub
return nil
}
// InitConfig ...
func (setup *BaseSetupImpl) InitConfig() error {
if err := config.InitConfig(setup.ConfigFile); err != nil {
return err
}
return nil
}
// InstantiateCC ...
func (setup *BaseSetupImpl) InstantiateCC(chainCodeID string, chainID string, chainCodePath string, chainCodeVersion string, args []string) error {
if err := fcUtil.SendInstantiateCC(setup.Chain, chainCodeID, chainID, args, chainCodePath, chainCodeVersion, []fabricClient.Peer{setup.Chain.GetPrimaryPeer()}, setup.EventHub); err != nil {
return err
}
return nil
}
// InstallCC ...
func (setup *BaseSetupImpl) InstallCC(chainCodeID string, chainCodePath string, chainCodeVersion string, chaincodePackage []byte) error {
if err := fcUtil.SendInstallCC(setup.Client, setup.Chain, chainCodeID, chainCodePath, chainCodeVersion, chaincodePackage, setup.Chain.GetPeers(), setup.GetDeployPath()); err != nil {
return fmt.Errorf("SendInstallProposal return error: %v", err)
}
return nil
}
// GetDeployPath ..
func (setup *BaseSetupImpl) GetDeployPath() string {
pwd, _ := os.Getwd()
return path.Join(pwd, "../fixtures")
}
// InstallAndInstantiateExampleCC ..
func (setup *BaseSetupImpl) InstallAndInstantiateExampleCC() error {
chainCodePath := "github.com/example_cc"
chainCodeVersion := "v0"
if setup.ChainCodeID == "" {
setup.ChainCodeID = fcUtil.GenerateRandomID()
}
if err := setup.InstallCC(setup.ChainCodeID, chainCodePath, chainCodeVersion, nil); err != nil {
return err
}
var args []string
args = append(args, "init")
args = append(args, "a")
args = append(args, "100")
args = append(args, "b")
args = append(args, "200")
return setup.InstantiateCC(setup.ChainCodeID, setup.ChainID, chainCodePath, chainCodeVersion, args)
}
// Query ...
func (setup *BaseSetupImpl) Query(chainID string, chainCodeID string, args []string) (string, error) {
transactionProposalResponses, _, err := fcUtil.CreateAndSendTransactionProposal(setup.Chain, chainCodeID, chainID, args, []fabricClient.Peer{setup.Chain.GetPrimaryPeer()}, nil)
if err != nil {
return "", fmt.Errorf("CreateAndSendTransactionProposal return error: %v", err)
}
return string(transactionProposalResponses[0].GetResponsePayload()), nil
}
// QueryAsset ...
func (setup *BaseSetupImpl) QueryAsset() (string, error) {
var args []string
args = append(args, "invoke")
args = append(args, "query")
args = append(args, "b")
return setup.Query(setup.ChainID, setup.ChainCodeID, args)
}
// MoveFunds ...
func (setup *BaseSetupImpl) MoveFunds() (string, error) {
var args []string
args = append(args, "invoke")
args = append(args, "move")
args = append(args, "a")
args = append(args, "b")
args = append(args, "1")
transientDataMap := make(map[string][]byte)
transientDataMap["result"] = []byte("Transient data in move funds...")
transactionProposalResponse, txID, err := fcUtil.CreateAndSendTransactionProposal(setup.Chain, setup.ChainCodeID, setup.ChainID, args, []fabricClient.Peer{setup.Chain.GetPrimaryPeer()}, transientDataMap)
if err != nil {
return "", fmt.Errorf("CreateAndSendTransactionProposal return error: %v", err)
}
// Register for commit event
done, fail := fcUtil.RegisterTxEvent(txID, setup.EventHub)
txResponse, err := fcUtil.CreateAndSendTransaction(setup.Chain, transactionProposalResponse)
if err != nil {
return "", fmt.Errorf("CreateAndSendTransaction return error: %v", err)
}
fmt.Println(txResponse)
select {
case <-done:
case <-fail:
return "", fmt.Errorf("invoke Error received from eventhub for txid(%s) error(%v)", txID, fail)
case <-time.After(time.Second * 30):
return "", fmt.Errorf("invoke Didn't receive block event for txid(%s)", txID)
}
return txID, nil
}
// getEventHub initilizes the event hub
func getEventHub() (events.EventHub, error) {
eventHub := events.NewEventHub()
foundEventHub := false
peerConfig, err := config.GetPeersConfig()
if err != nil {
return nil, fmt.Errorf("Error reading peer config: %v", err)
}
for _, p := range peerConfig {
if p.EventHost != "" && p.EventPort != 0 {
fmt.Printf("******* EventHub connect to peer (%s:%d) *******\n", p.EventHost, p.EventPort)
eventHub.SetPeerAddr(fmt.Sprintf("%s:%d", p.EventHost, p.EventPort),
p.TLS.Certificate, p.TLS.ServerHostOverride)
foundEventHub = true
break
}
}
if !foundEventHub {
return nil, fmt.Errorf("No EventHub configuration found")
}
return eventHub, nil
}