// Initialize reads the configuration file and sets up the client, chain and event hub
Initialize reads the configuration file and sets up the client, chain and event hub
// Initialize reads the configuration file and sets up the client chain and event hub
func (setup *FabricSetup) Initialize() error {
	if setup.initialized {
		return errors.New("sdk already initialized")
	}
	sdk, err := fabsdk.New(config.FromFile(setup.ConfigFile))
	if err != nil {
		return errors.WithMessage(err, "failed to create SDK")
	}
	setup.sdk = sdk
	fmt.Println("SDK created")
	org_nowMspClient, err := mspclient.New(setup.sdk.Context(), mspclient.WithOrg(setup.Org_now))
	if err != nil {
		return errors.WithMessage(err, "failed to create org_nowMspClient")
	}else{
		fmt.Println("org_nowMspClient successful")
	}
	org_nowAdminIdentity, err := org_nowMspClient.GetSigningIdentity(setup.Org_nowAdmin)
	if err != nil {
		return errors.WithMessage(err, "failed to get org_nowAdminIdentity")
	}else{
		fmt.Println("org_nowAdminIdentity successly.")
	}
	ctx, err := sdk.Context(fabsdk.WithUser(setup.Org_nowAdmin), fabsdk.WithOrg(setup.Org_now))()
	if err != nil {
		return errors.WithMessage(err, "context creation failed")
	}
	org_nowPeers, ok := ctx.EndpointConfig().PeersConfig(setup.Org_now)
	if ok {
		fmt.Println("org_now ok:ture")
	}else{
		fmt.Println("org_now ok:false")
	}
	orgTestPeernow, err := ctx.InfraProvider().CreatePeerFromConfig(&fab.NetworkPeer{PeerConfig: org_nowPeers[0]})
	if err != nil {
		return errors.WithMessage(err, "CreatePeerFromConfig org_nowPeers failed")
	}else{
		fmt.Println("orgTestPeernow:",orgTestPeernow)
	}
	setup.orgTestPeernow = orgTestPeernow
	CleanupUserData(setup.sdk)
	defer CleanupUserData(setup.sdk)
	setup.org_nowAdminClientContext = setup.sdk.Context(fabsdk.WithUser(setup.Org_nowAdmin), fabsdk.WithOrg(setup.Org_now))
	if err != nil {
		return errors.WithMessage(err, "failed to load org_nowRes Admin identity")
	}
	org_nowResMgmtClient, err := resmgmt.New(setup.org_nowAdminClientContext)
	if err != nil {
		return errors.WithMessage(err, "failed to org_nowResMgmtClient client from Admin identity")
	}
	setup.org_nowResMgmt = org_nowResMgmtClient
	fmt.Println("Ressource org_nowResMgmt client created")
	configQueryClient, err := resmgmt.New(setup.org_nowAdminClientContext)
	if err != nil {
		return errors.WithMessage(err, "failed to configQueryClient client from Admin identity")
	}else{
		fmt.Println("configQueryClient successful")
	}
	channelHasInstall := false
	channelRes, err := setup.org_nowResMgmt.QueryChannels(resmgmt.WithTargets(setup.orgTestPeernow))
	if err != nil {
		return errors.WithMessage(err, "failed to Query channel")
	}
	if channelRes != nil {
		for _, channel := range channelRes.Channels {
			if strings.EqualFold(setup.ChannelID, channel.ChannelId) {
				channelHasInstall = true
			}
		}
	}
	fmt.Println("channelHasInstall:", channelHasInstall)
	if !channelHasInstall {
		if err = setup.org_nowResMgmt.JoinChannel(setup.ChannelID, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint(setup.OrdererID)); err != nil {
			return errors.WithMessage(err, "org_nowResMgmt failed to make admin join channel")
		}else{
			fmt.Println("org_nowResMgmt Channel joined successful")
		}
	} else {
		var lastConfigBlock uint64
		lastConfigBlock = WaitForOrdererConfigUpdate(configQueryClient,setup.ChannelID, false, lastConfigBlock)
		req := resmgmt.SaveChannelRequest{ChannelID: setup.ChannelID, ChannelConfigPath: os.Getenv("GOPATH") + "/src/github.com/srezd/app-1g4p1/fixtures/artifacts/org4.srezd.anchors.tx", 
		SigningIdentities: []msp.SigningIdentity{org_nowAdminIdentity}}
		txID, err := setup.org_nowResMgmt.SaveChannel(req, resmgmt.WithOrdererEndpoint(setup.OrdererID))
		if err != nil || txID.TransactionID == "" {
			return errors.WithMessage(err, "org_nowResMgmt failed to save channel")
		}
		fmt.Println("org_nowResMgmt Channel created")
		if err = setup.org_nowResMgmt.JoinChannel(setup.ChannelID, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint(setup.OrdererID)); err != nil {
			return errors.WithMessage(err, "org_nowResMgmt failed to make admin join channel")
		}else{
			fmt.Println("org_nowResMgmt Channel joined successful")
		}
		fmt.Println("Channel already exist")
	}
	fmt.Println("Initialization Successful")
	setup.initialized = true
	return nil
}
func ( setup * FabricSetup ) Initialize ( ) error { if setup . initialized { return errors . New ( " " ) } sdk , err := fabsdk . New ( config . FromFile ( setup . ConfigFile ) ) if err != nil { return errors . WithMessage ( err , " " ) } setup . sdk = sdk fmt . Println ( " " ) org_nowMspClient , err := mspclient . New ( setup . sdk . Context ( ) , mspclient . WithOrg ( setup . Org_now ) ) if err != nil { return errors . WithMessage ( err , " " ) } else { fmt . Println ( " " ) } org_nowAdminIdentity , err := org_nowMspClient . GetSigningIdentity ( setup . Org_nowAdmin ) if err != nil { return errors . WithMessage ( err , " " ) } else { fmt . Println ( " " ) } ctx , err := sdk . Context ( fabsdk . WithUser ( setup . Org_nowAdmin ) , fabsdk . WithOrg ( setup . Org_now ) ) ( ) if err != nil { return errors . WithMessage ( err , " " ) } org_nowPeers , ok := ctx . EndpointConfig ( ) . PeersConfig ( setup . Org_now ) if ok { fmt . Println ( " " ) } else { fmt . Println ( " " ) } orgTestPeernow , err := ctx . InfraProvider ( ) . CreatePeerFromConfig ( & fab . NetworkPeer { PeerConfig : org_nowPeers [ 0 ] } ) if err != nil { return errors . WithMessage ( err , " " ) } else { fmt . Println ( " " , orgTestPeernow ) } setup . orgTestPeernow = orgTestPeernow CleanupUserData ( setup . sdk ) defer CleanupUserData ( setup . sdk ) setup . org_nowAdminClientContext = setup . sdk . Context ( fabsdk . WithUser ( setup . Org_nowAdmin ) , fabsdk . WithOrg ( setup . Org_now ) ) if err != nil { return errors . WithMessage ( err , " " ) } org_nowResMgmtClient , err := resmgmt . New ( setup . org_nowAdminClientContext ) if err != nil { return errors . WithMessage ( err , " " ) } setup . org_nowResMgmt = org_nowResMgmtClient fmt . Println ( " " ) configQueryClient , err := resmgmt . New ( setup . org_nowAdminClientContext ) if err != nil { return errors . WithMessage ( err , " " ) } else { fmt . Println ( " " ) } channelHasInstall := false channelRes , err := setup . org_nowResMgmt . QueryChannels ( resmgmt . WithTargets ( setup . orgTestPeernow ) ) if err != nil { return errors . WithMessage ( err , " " ) } if channelRes != nil { for _ , channel := range channelRes . Channels { if strings . EqualFold ( setup . ChannelID , channel . ChannelId ) { channelHasInstall = true } } } fmt . Println ( " " , channelHasInstall ) if ! channelHasInstall { if err = setup . org_nowResMgmt . JoinChannel ( setup . ChannelID , resmgmt . WithRetry ( retry . DefaultResMgmtOpts ) , resmgmt . WithOrdererEndpoint ( setup . OrdererID ) ) ; err != nil { return errors . WithMessage ( err , " " ) } else { fmt . Println ( " " ) } } else { var lastConfigBlock uint64 lastConfigBlock = WaitForOrdererConfigUpdate ( configQueryClient , setup . ChannelID , false , lastConfigBlock ) req := resmgmt . SaveChannelRequest { ChannelID : setup . ChannelID , ChannelConfigPath : os . Getenv ( " " ) + " " , SigningIdentities : [ ] msp . SigningIdentity { org_nowAdminIdentity } } txID , err := setup . org_nowResMgmt . SaveChannel ( req , resmgmt . WithOrdererEndpoint ( setup . OrdererID ) ) if err != nil || txID . TransactionID == " " { return errors . WithMessage ( err , " " ) } fmt . Println ( " " ) if err = setup . org_nowResMgmt . JoinChannel ( setup . ChannelID , resmgmt . WithRetry ( retry . DefaultResMgmtOpts ) , resmgmt . WithOrdererEndpoint ( setup . OrdererID ) ) ; err != nil { return errors . WithMessage ( err , " " ) } else { fmt . Println ( " " ) } fmt . Println ( " " ) } fmt . Println ( " " ) setup . initialized = true return nil }
Initialize reads the configuration file and sets up the client, chain and event hub
Initialize reads the configuration file and sets up the client chain and event hub
// Add parameters for the initialization // Initialize the SDK with the configuration file //资源管理客户负责管理通道 //ctx, err := ctxProvider() //删除加密套件store的所有私钥以及终端用户（utils.go）清除环境和用户数据 // 查询已经存在的channel //channelRes, err := setup.org_nowResMgmt.QueryChannels(resmgmt.WithTargetEndpoints("peer1.org4.hf.srezd.io")) // 如果没有同名的channel 再安装 /*
		var lastConfigBlock uint64
		lastConfigBlock = WaitForOrdererConfigUpdate(configQueryClient,setup.ChannelID, false, lastConfigBlock)
		req := resmgmt.SaveChannelRequest{ChannelID: setup.ChannelID, ChannelConfigPath: os.Getenv("GOPATH") + "/src/github.com/srezd/app-1g4p1/fixtures/artifacts/org4.srezd.anchors.tx", 
		SigningIdentities: []msp.SigningIdentity{org_nowAdminIdentity}}
		txID, err := setup.org_nowResMgmt.SaveChannel(req, resmgmt.WithOrdererEndpoint(setup.OrdererID))
		if err != nil || txID.TransactionID == "" {
			return errors.WithMessage(err, "org_nowResMgmt failed to save channel")
		}
		fmt.Println("org_nowResMgmt Channel created")
		*/