一. pkg/client/resmgmt 包resmgmt支持在Fabric网络上创建和更新资源。 它允许管理员创建和/或更新通道,并允许管理员加入通道。 管理员还可以在对等方上执行与链代码相关的操作,例如安装,实例化和升级链代码.

  1. 基本流程:

    1)准备客户端上下文
    2)创建资源管理客户端
    3)创建新频道
    4)Peer(s)加入频道
    5)将链代码安装到peer(s)文件系统
    6)在通道上实例化链码
    7)查询peer通道,已安装/实例化的链代码等。

     1 // Create new resource management client
     2 c, err := New(mockClientProvider())
     3 if err != nil {
     4     fmt.Println("failed to create client")
     5 }
     6 
     7 // Read channel configuration
     8 r, err := os.Open(channelConfig)
     9 if err != nil {
    10     fmt.Printf("failed to open channel config: %s\n", err)
    11 }
    12 defer r.Close()
    13 
    14 // Create new channel 'mychannel'
    15 _, err = c.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r})
    16 if err != nil {
    17     fmt.Printf("failed to save channel: %s\n", err)
    18 }
    19 
    20 peer := mockPeer()
    21 
    22 // Peer joins channel 'mychannel'
    23 err = c.JoinChannel("mychannel", WithTargets(peer))
    24 if err != nil {
    25     fmt.Printf("failed to join channel: %s\n", err)
    26 }
    27 
    28 // Install example chaincode to peer
    29 installReq := InstallCCRequest{Name: "ExampleCC", Version: "v0", Path: "path", Package: &resource.CCPackage{Type: 1, Code: []byte("bytes")}}
    30 _, err = c.InstallCC(installReq, WithTargets(peer))
    31 if err != nil {
    32     fmt.Printf("failed to install chaincode: %s\n", err)
    33 }
    34 
    35 // Instantiate example chaincode on channel 'mychannel'
    36 ccPolicy := cauthdsl.SignedByMspMember("Org1MSP")
    37 instantiateReq := InstantiateCCRequest{Name: "ExampleCC", Version: "v0", Path: "path", Policy: ccPolicy}
    38 _, err = c.InstantiateCC("mychannel", instantiateReq, WithTargets(peer))
    39 if err != nil {
    40     fmt.Printf("failed to install chaincode: %s\n", err)
    41 }
    42 
    43 fmt.Println("Network setup completed")
    View Code

相关文章: