Hyperledger fabric介绍之fabric-sdk-go入门
package main
import (
"fmt"
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
)
func main() {
// 初始化SDK,使用默认配置文件加载
sdk, err := fabsdk.New(config.FromFile("./config.yaml"))
if err != nil {
fmt.Printf("Failed to create SDK: %s\n", err)
return
}
defer sdk.Close()
// 初始化客户端
clientChannelContext := sdk.ChannelContext("mychannel", fabsdk.WithUser("User1"))
client, err := channel.New(clientChannelContext)
if err != nil {
fmt.Printf("Failed to create channel client: %s\n", err)
return
}
// 调用通道上的链码
response, err := client.Execute(channel.Request{ChaincodeID: "mycc", Fcn: "invoke", Args: [][]byte{[]byte("arg1"), []byte("arg2")}})
if err != nil {
fmt.Printf("Failed to execute chaincode: %s\n", err)
return
}
// 打印结果
fmt.Println("Chaincode invoke result:", string(response.Payload))
}
这段代码展示了如何使用fabric-sdk-go包初始化Hyperledger Fabric的SDK,创建客户端并在指定通道上调用链码。这是一个简化的示例,实际应用中需要配置具体的网络和身份信息。
评论已关闭