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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
| package sdkInit
import ( "fmt" mb "github.com/hyperledger/fabric-protos-go/msp" pb "github.com/hyperledger/fabric-protos-go/peer" "github.com/hyperledger/fabric-sdk-go/pkg/client/channel" mspclient "github.com/hyperledger/fabric-sdk-go/pkg/client/msp" "github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt" "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/providers/fab" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp" "github.com/hyperledger/fabric-sdk-go/pkg/core/config" lcpackager "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/lifecycle" "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk" "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/policydsl" "strings" )
func Setup(configFile string, info *SdkEnvInfo) (*fabsdk.FabricSDK, error) { var err error sdk, err := fabsdk.New(config.FromFile(configFile)) if err != nil { return nil, err }
for _, org := range info.Orgs { org.orgMspClient, err = mspclient.New(sdk.Context(), mspclient.WithOrg(org.OrgName)) if err != nil { return nil, err } orgContext := sdk.Context(fabsdk.WithUser(org.OrgAdminUser), fabsdk.WithOrg(org.OrgName)) org.OrgAdminClientContext = &orgContext
resMgmtClient, err := resmgmt.New(orgContext) if err != nil { return nil, fmt.Errorf("根据指定的资源管理客户端Context创建通道管理客户端失败: %v", err) } org.OrgResMgmt = resMgmtClient }
ordererClientContext := sdk.Context(fabsdk.WithUser(info.OrdererAdminUser), fabsdk.WithOrg(info.OrdererOrgName)) info.OrdererClientContext = &ordererClientContext return sdk, nil }
func CreateAndJoinChannel(info *SdkEnvInfo) error { fmt.Println(">> 开始创建通道......") if len(info.Orgs) == 0 { return fmt.Errorf("通道组织不能为空,请提供组织信息") }
signIds := []msp.SigningIdentity{} for _, org := range info.Orgs { orgSignId, err := org.orgMspClient.GetSigningIdentity(org.OrgAdminUser) if err != nil { return fmt.Errorf("GetSigningIdentity error: %v", err) } signIds = append(signIds, orgSignId) }
if err := createChannel(signIds, info); err != nil { return fmt.Errorf("Create channel error: %v", err) }
fmt.Println(">> 创建通道成功")
fmt.Println(">> 加入通道......") for _, org := range info.Orgs { if err := org.OrgResMgmt.JoinChannel(info.ChannelID, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint("orderer.example.com")); err != nil { return fmt.Errorf("%s peers failed to JoinChannel: %v", org.OrgName, err) } } fmt.Println(">> 加入通道成功") return nil }
func createChannel(signIDs []msp.SigningIdentity, info *SdkEnvInfo) error { chMgmtClient, err := resmgmt.New(*info.OrdererClientContext) if err != nil { return fmt.Errorf("Channel management client create error: %v", err) }
req := resmgmt.SaveChannelRequest{ChannelID: info.ChannelID, ChannelConfigPath: info.ChannelConfig, SigningIdentities: signIDs}
if _, err := chMgmtClient.SaveChannel(req, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint("orderer.example.com")); err != nil { return fmt.Errorf("error should be nil for SaveChannel of orgchannel: %v", err) }
fmt.Println(">>>> 使用每个org的管理员身份更新锚节点配置...") for i, org := range info.Orgs { req = resmgmt.SaveChannelRequest{ChannelID: info.ChannelID, ChannelConfigPath: org.OrgAnchorFile, SigningIdentities: []msp.SigningIdentity{signIDs[i]}}
if _, err = org.OrgResMgmt.SaveChannel(req, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint("orderer.example.com")); err != nil { return fmt.Errorf("SaveChannel for anchor org %s error: %v", org.OrgName, err) } } fmt.Println(">>>> 使用每个org的管理员身份更新锚节点配置完成") return nil }
func CreateCCLifecycle(info *SdkEnvInfo, sequence int64, upgrade bool, sdk *fabsdk.FabricSDK) error { if len(info.Orgs) == 0 { return fmt.Errorf("the number of organization should not be zero.") } fmt.Println(">> 开始打包链码......") label, ccPkg, err := packageCC(info.ChaincodeID, info.ChaincodeVersion, info.ChaincodePath) if err != nil { return fmt.Errorf("pakcagecc error: %v", err) } packageID := lcpackager.ComputePackageID(label, ccPkg) fmt.Println(">> 打包链码成功")
fmt.Println(">> 开始安装链码......") if err := installCC(label, ccPkg, info.Orgs); err != nil { return fmt.Errorf("installCC error: %v", err) }
if err := getInstalledCCPackage(packageID, info.Orgs[0]); err != nil { return fmt.Errorf("getInstalledCCPackage error: %v", err) }
if err := queryInstalled(packageID, info.Orgs[0]); err != nil { return fmt.Errorf("queryInstalled error: %v", err) } fmt.Println(">> 安装链码成功")
fmt.Println(">> 组织认可智能合约定义......") if err := approveCC(packageID, info.ChaincodeID, info.ChaincodeVersion, sequence, info.ChannelID, info.Orgs, info.OrdererEndpoint); err != nil { return fmt.Errorf("approveCC error: %v", err) }
if err:=queryApprovedCC(info.ChaincodeID, sequence, info.ChannelID, info.Orgs);err!=nil{ return fmt.Errorf("queryApprovedCC error: %v", err) } fmt.Println(">> 组织认可智能合约定义完成")
fmt.Println(">> 检查智能合约是否就绪......") if err:=checkCCCommitReadiness(packageID, info.ChaincodeID, info.ChaincodeVersion, sequence, info.ChannelID, info.Orgs); err!=nil{ return fmt.Errorf("checkCCCommitReadiness error: %v", err) } fmt.Println(">> 智能合约已经就绪")
fmt.Println(">> 提交智能合约定义......") if err:=commitCC(info.ChaincodeID, info.ChaincodeVersion, sequence, info.ChannelID, info.Orgs, info.OrdererEndpoint);err!=nil{ return fmt.Errorf("commitCC error: %v", err) } if err:=queryCommittedCC(info.ChaincodeID, info.ChannelID, sequence, info.Orgs); err!=nil{ return fmt.Errorf("queryCommittedCC error: %v", err) } fmt.Println(">> 智能合约定义提交完成")
fmt.Println(">> 调用智能合约初始化方法......") if err:=initCC(info.ChaincodeID, upgrade, info.ChannelID, info.Orgs[0], sdk); err!=nil{ return fmt.Errorf("initCC error: %v", err) } fmt.Println(">> 完成智能合约初始化") return nil }
func packageCC(ccName, ccVersion, ccpath string) (string, []byte, error) { label := ccName + "_" + ccVersion desc := &lcpackager.Descriptor{ Path: ccpath, Type: pb.ChaincodeSpec_GOLANG, Label: label, } ccPkg, err := lcpackager.NewCCPackage(desc) if err != nil { return "", nil, fmt.Errorf("Package chaincode source error: %v", err) } return desc.Label, ccPkg, nil }
func installCC(label string, ccPkg []byte, orgs []*OrgInfo) error { installCCReq := resmgmt.LifecycleInstallCCRequest{ Label: label, Package: ccPkg, }
packageID := lcpackager.ComputePackageID(installCCReq.Label, installCCReq.Package) for _, org := range orgs { orgPeers, err := DiscoverLocalPeers(*org.OrgAdminClientContext, org.OrgPeerNum) if err != nil { fmt.Errorf("DiscoverLocalPeers error: %v", err) } if flag, _ := checkInstalled(packageID, orgPeers[0], org.OrgResMgmt); flag == false { if _, err := org.OrgResMgmt.LifecycleInstallCC(installCCReq, resmgmt.WithTargets(orgPeers...), resmgmt.WithRetry(retry.DefaultResMgmtOpts)); err != nil { return fmt.Errorf("LifecycleInstallCC error: %v", err) } } } return nil }
func getInstalledCCPackage(packageID string, org *OrgInfo) error { orgPeers, err := DiscoverLocalPeers(*org.OrgAdminClientContext, 1) if err != nil { return fmt.Errorf("DiscoverLocalPeers error: %v", err) }
if _, err := org.OrgResMgmt.LifecycleGetInstalledCCPackage(packageID, resmgmt.WithTargets([]fab.Peer{orgPeers[0]}...)); err != nil { return fmt.Errorf("LifecycleGetInstalledCCPackage error: %v", err) } return nil }
func queryInstalled(packageID string, org *OrgInfo) error { orgPeers, err := DiscoverLocalPeers(*org.OrgAdminClientContext, 1) if err != nil { return fmt.Errorf("DiscoverLocalPeers error: %v", err) } resp1, err := org.OrgResMgmt.LifecycleQueryInstalledCC(resmgmt.WithTargets([]fab.Peer{orgPeers[0]}...)) if err != nil { return fmt.Errorf("LifecycleQueryInstalledCC error: %v", err) } packageID1 := "" for _, t := range resp1 { if t.PackageID == packageID { packageID1 = t.PackageID } } if !strings.EqualFold(packageID, packageID1) { return fmt.Errorf("check package id error") } return nil }
func checkInstalled(packageID string, peer fab.Peer, client *resmgmt.Client) (bool, error) { flag := false resp1, err := client.LifecycleQueryInstalledCC(resmgmt.WithTargets(peer)) if err != nil { return flag, fmt.Errorf("LifecycleQueryInstalledCC error: %v", err) } for _, t := range resp1 { if t.PackageID == packageID { flag = true } } return flag, nil }
func approveCC(packageID string, ccName, ccVersion string, sequence int64, channelID string, orgs []*OrgInfo, ordererEndpoint string) error { mspIDs := []string{} for _, org := range orgs { mspIDs = append(mspIDs, org.OrgMspId) } ccPolicy := policydsl.SignedByNOutOfGivenRole(int32(len(mspIDs)), mb.MSPRole_MEMBER, mspIDs) approveCCReq := resmgmt.LifecycleApproveCCRequest{ Name: ccName, Version: ccVersion, PackageID: packageID, Sequence: sequence, EndorsementPlugin: "escc", ValidationPlugin: "vscc", SignaturePolicy: ccPolicy, InitRequired: true, }
for _, org := range orgs{ orgPeers, err := DiscoverLocalPeers(*org.OrgAdminClientContext, org.OrgPeerNum) fmt.Printf(">>> chaincode approved by %s peers:\n", org.OrgName) for _, p := range orgPeers { fmt.Printf(" %s\n", p.URL()) }
if err!=nil{ return fmt.Errorf("DiscoverLocalPeers error: %v", err) } if _, err := org.OrgResMgmt.LifecycleApproveCC(channelID, approveCCReq, resmgmt.WithTargets(orgPeers...), resmgmt.WithOrdererEndpoint(ordererEndpoint), resmgmt.WithRetry(retry.DefaultResMgmtOpts));err != nil { fmt.Errorf("LifecycleApproveCC error: %v", err) } } return nil }
func queryApprovedCC(ccName string, sequence int64, channelID string, orgs []*OrgInfo) error { queryApprovedCCReq := resmgmt.LifecycleQueryApprovedCCRequest{ Name: ccName, Sequence: sequence, }
for _, org := range orgs{ orgPeers, err := DiscoverLocalPeers(*org.OrgAdminClientContext, org.OrgPeerNum) if err!=nil{ return fmt.Errorf("DiscoverLocalPeers error: %v", err) } for _, p := range orgPeers { resp, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke( func() (interface{}, error) { resp1, err := org.OrgResMgmt.LifecycleQueryApprovedCC(channelID, queryApprovedCCReq, resmgmt.WithTargets(p)) if err != nil { return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("LifecycleQueryApprovedCC returned error: %v", err), nil) } return resp1, err }, ) if err != nil { return fmt.Errorf("Org %s Peer %s NewInvoker error: %v", org.OrgName, p.URL(), err) } if resp==nil{ return fmt.Errorf("Org %s Peer %s Got nil invoker", org.OrgName, p.URL()) } } } return nil }
func checkCCCommitReadiness(packageID string, ccName, ccVersion string, sequence int64, channelID string, orgs []*OrgInfo) error { mspIds := []string{} for _, org := range orgs { mspIds = append(mspIds, org.OrgMspId) } ccPolicy := policydsl.SignedByNOutOfGivenRole(int32(len(mspIds)), mb.MSPRole_MEMBER, mspIds) req := resmgmt.LifecycleCheckCCCommitReadinessRequest{ Name: ccName, Version: ccVersion, EndorsementPlugin: "escc", ValidationPlugin: "vscc", SignaturePolicy: ccPolicy, Sequence: sequence, InitRequired: true, } for _, org := range orgs{ orgPeers, err := DiscoverLocalPeers(*org.OrgAdminClientContext, org.OrgPeerNum) if err!=nil{ fmt.Errorf("DiscoverLocalPeers error: %v", err) } for _, p := range orgPeers { resp, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke( func() (interface{}, error) { resp1, err := org.OrgResMgmt.LifecycleCheckCCCommitReadiness(channelID, req, resmgmt.WithTargets(p)) fmt.Printf("LifecycleCheckCCCommitReadiness cc = %v, = %v\n", ccName, resp1) if err != nil { return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("LifecycleCheckCCCommitReadiness returned error: %v", err), nil) } flag := true for _, r := range resp1.Approvals { flag = flag && r } if !flag { return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("LifecycleCheckCCCommitReadiness returned : %v", resp1), nil) } return resp1, err }, ) if err != nil { return fmt.Errorf("NewInvoker error: %v", err) } if resp==nil{ return fmt.Errorf("Got nill invoker response") } } }
return nil }
func commitCC(ccName, ccVersion string, sequence int64, channelID string, orgs []*OrgInfo, ordererEndpoint string) error{ mspIDs := []string{} for _, org := range orgs { mspIDs = append(mspIDs, org.OrgMspId) } ccPolicy := policydsl.SignedByNOutOfGivenRole(int32(len(mspIDs)), mb.MSPRole_MEMBER, mspIDs)
req := resmgmt.LifecycleCommitCCRequest{ Name: ccName, Version: ccVersion, Sequence: sequence, EndorsementPlugin: "escc", ValidationPlugin: "vscc", SignaturePolicy: ccPolicy, InitRequired: true, } _, err := orgs[0].OrgResMgmt.LifecycleCommitCC(channelID, req, resmgmt.WithOrdererEndpoint(ordererEndpoint), resmgmt.WithRetry(retry.DefaultResMgmtOpts)) if err != nil { return fmt.Errorf("LifecycleCommitCC error: %v", err) } return nil }
func queryCommittedCC( ccName string, channelID string, sequence int64, orgs []*OrgInfo) error { req := resmgmt.LifecycleQueryCommittedCCRequest{ Name: ccName, }
for _, org := range orgs { orgPeers, err := DiscoverLocalPeers(*org.OrgAdminClientContext, org.OrgPeerNum) if err!=nil{ return fmt.Errorf("DiscoverLocalPeers error: %v", err) } for _, p := range orgPeers { resp, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke( func() (interface{}, error) { resp1, err := org.OrgResMgmt.LifecycleQueryCommittedCC(channelID, req, resmgmt.WithTargets(p)) if err != nil { return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("LifecycleQueryCommittedCC returned error: %v", err), nil) } flag := false for _, r := range resp1 { if r.Name == ccName && r.Sequence == sequence { flag = true break } } if !flag { return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("LifecycleQueryCommittedCC returned : %v", resp1), nil) } return resp1, err }, ) if err != nil { return fmt.Errorf("NewInvoker error: %v", err) } if resp==nil{ return fmt.Errorf("Got nil invoker response") } } } return nil }
func initCC(ccName string, upgrade bool, channelID string, org *OrgInfo, sdk *fabsdk.FabricSDK) error { clientChannelContext := sdk.ChannelContext(channelID, fabsdk.WithUser(org.OrgUser), fabsdk.WithOrg(org.OrgName)) client, err := channel.New(clientChannelContext) if err != nil { return fmt.Errorf("Failed to create new channel client: %s", err) }
_, err = client.Execute(channel.Request{ChaincodeID: ccName, Fcn: "init", Args: nil, IsInit: true}, channel.WithRetry(retry.DefaultChannelOpts)) if err != nil { return fmt.Errorf("Failed to init: %s", err) } return nil }
|