返回

Fabric HelloWorld 区块链入门:轻松搭建开发环境

后端

Fabric 区块链开发入门:搭建 HelloWorld 智能合约网络

准备工作

踏入区块链开发世界的第一步是从头开始搭建一个 Fabric 网络。为此,你需要准备以下工具:

  • Docker
  • Docker Compose
  • Go
  • 代码编辑器(如 Visual Studio Code)

搭建 Fabric 网络

1. 克隆 Fabric 源代码库

git clone https://github.com/hyperledger/fabric

2. 设置 GOPATH

export GOPATH=$HOME/go

3. 运行 Docker Compose

docker-compose up -d

编写 HelloWorld 智能合约

现在,我们来编写一个简单的智能合约,它将存储和检索一个名为 "message" 的值。

1. 创建 Chaincode 项目

mkdir chaincode
cd chaincode

2. 创建 HelloWorld 合约

package main

import (
    "fmt"
    "github.com/hyperledger/fabric/core/chaincode/shim"
)

// HelloWorld 结构体
type HelloWorld struct {
}

// Init 初始化函数
func (t *HelloWorld) Init(stub shim.ChaincodeStubInterface) error {
    return nil
}

// Invoke 调用函数
func (t *HelloWorld) Invoke(stub shim.ChaincodeStubInterface) error {
    fcn, args := stub.GetFunctionAndParameters()

    switch fcn {
    case "set":
        if len(args) != 2 {
            return fmt.Errorf("不正确的参数个数,需要 2 个")
        }
        return stub.PutState("message", []byte(args[1]))
    case "get":
        if len(args) != 1 {
            return fmt.Errorf("不正确的参数个数,需要 1 个")
        }
        value, err := stub.GetState("message")
        if err != nil {
            return fmt.Errorf("无法获取 state: %s", err)
        }
        stub.SetEvent("evSetMessage", []byte(value))
        return nil
    default:
        return fmt.Errorf("无效的函数名称:%s", fcn)
    }
}

// Query 查询函数
func (t *HelloWorld) Query(stub shim.ChaincodeStubInterface) error {
    value, err := stub.GetState("message")
    if err != nil {
        return fmt.Errorf("无法获取 state: %s", err)
    }
    return shim.Success(value)
}

// main 函数
func main() {
    err := shim.Start(new(HelloWorld))
    if err != nil {
        fmt.Printf("Error starting HelloWorld chaincode: %s", err)
    }
}

编译并部署智能合约

1. 编译

go build

2. 安装

peer chaincode install -n hello -v 1.0 -p github.com/chaincode/go/

3. 实例化

peer chaincode instantiate -n hello -v 1.0 -C mychannel -o orderer.example.com:7050 -c '{"Args":["a","100"]}'

调用智能合约

1. 调用

peer chaincode invoke -n hello -C mychannel -o orderer.example.com:7050 --tls --cafile /path/to/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -c '{"Args":["set","Hello World"]}'

2. 查询

peer chaincode query -n hello -C mychannel -o orderer.example.com:7050 --tls --cafile /path/to/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -c '{"Args":["get"]}'

管理 Fabric 网络

  • 停止网络: docker-compose down
  • 重新启动网络: docker-compose up -d
  • 重启容器: docker-compose restart <容器名>

常见问题解答

1. 网络启动失败?

  • 检查网络配置是否正确。
  • 确保 Docker 和 Docker Compose 已正确安装。
  • 查看容器日志是否存在错误信息。

2. 智能合约部署失败?

  • 检查智能合约代码是否正确。
  • 确保智能合约已成功安装。
  • 检查网络配置是否正确。

3. 智能合约调用失败?

  • 检查智能合约函数的定义是否正确。
  • 确保智能合约已成功部署。
  • 检查网络配置是否正确。

4. 如何获得智能合约的状态?

使用 peer chaincode getstatus 命令。

5. 如何管理通道成员资格?

使用 peer channel 命令,例如 peer channel joinpeer channel update

结语

恭喜你!你已经成功地在 Fabric 区块链上部署并调用了一个 HelloWorld 智能合约。这只是你区块链开发之旅的开始。现在,你可以深入探索 Fabric 的其他功能,如共识机制、权限管理和链码生命周期管理,并开始构建你的第一个区块链应用程序。