Fabirc入门

  1. 前提条件

    • 需要安装 Git、cURL、Docker 和 Docker Compose
    • 推荐使用 Linux 或 macOS 系统
    •  安装样例、二进制文件和 Docker 镜像
      • 使用 curl 命令下载并执行安装脚
      • 脚本会克隆 fabric-samples 仓库并下载特定版本的 Fabric 二进制文件和 Docker 镜像
  2. 安装 Git:

    sudo yum install git
    
  3. 安装 cURL:

    sudo yum install curl
    
  4. 安装 Docker:

    sudo yum install -y yum-utils
    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    sudo yum install docker-ce docker-ce-cli containerd.io
    sudo systemctl start docker
    sudo systemctl enable docker
    
  5. 安装 Docker Compose:

    sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    
  6. 验证安装:

    git --version
    curl --version
    docker --version
    docker-compose --version
    
  7. 确保当前用户可以运行 Docker 命令:

    sudo usermod -aG docker $USER
    

    (注意:执行此命令后需要注销并重新登录)

  8. 下载 Fabric 样例和二进制文件:

    curl -sSL https://bit.ly/2ysbOFE | bash -s
    
  9. 验证 Fabric 二进制文件:

    fabric-samples/bin/peer version
  10. 测试网络

    • 进入 fabric-samples/test-network 目录
    • 使用 ./network.sh 脚本启动测试网络
    • 可以创建通道、部署智能合约等
  11. 部署智能合约

    • 使用 ./network.sh deployCC 命令部署示例智能合约
    • 解释了智能合约的基本概念和部署过程
  12. 与网络交互

    • 设置环境变量以使用 peer CLI
    • 示范如何调用智能合约和查询账本
  13. 关闭网络

    • 使用 ./network.sh down 命令关闭测试网络
  1. 进入测试网络目录:

    cd fabric-samples/test-network
    
  2. 启动测试网络:

    plainText

    ./network.sh up
    
  3. 创建通道:

    plainText

    ./network.sh createChannel
    
  4. 部署智能合约:

    plainText

    ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
    
  5. 设置环境变量以与 Org1 交互:

    plainText

    export PATH=${PWD}/../bin:$PATH
    export FABRIC_CFG_PATH=$PWD/../config/
    export CORE_PEER_TLS_ENABLED=true
    export CORE_PEER_LOCALMSPID="Org1MSP"
    export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
    export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
    export CORE_PEER_ADDRESS=localhost:7051
    
  6. 调用智能合约(初始化账本):

    plainText

    peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"InitLedger","Args":[]}'
    
  7. 查询智能合约:

    plainText

    peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
    
  8. 关闭测试网络:

    plainText

    ./network.sh down
    

其他有用的命令:

  1. 启动网络并创建通道(一步完成):

    plainText

    ./network.sh up createChannel
    
  2. 启动网络、创建通道并部署智能合约(一步完成):

    plainText

    ./network.sh up createChannel -ca -c mychannel -s couchdb
    
  3. 查看网络日志:

    plainText

    docker logs -f <container_name>
    

+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --output json
+ res=0
{
        "approvals": {
                "Org1MSP": true,
                "Org2MSP": false
        }
}
Checking the commit readiness of the chaincode definition successful on peer0.org1 on channel 'mychannel'
Using organization 2
Checking the commit readiness of the chaincode definition on peer0.org2 on channel 'mychannel'...
Attempting to check the commit readiness of the chaincode definition on peer0.org2, Retry after 3 seconds.
+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --output json
+ res=0
{
        "approvals": {
                "Org1MSP": true,
                "Org2MSP": false
        }
}
Checking the commit readiness of the chaincode definition successful on peer0.org2 on channel 'mychannel'
Using organization 2
+ peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile /usr/local/chain-demo/fabric-samples/test-network/organizations/ordererOrganizations/example.com/tlsca/tlsca.example.com-cert.pem --channelID mychannel --name basic --version 1.0 --package-id basic_1.0:29cafe2e5576ddf8ca872875fb58b46b1f501a954627b27eab72d87ed756a8be --sequence 1
+ res=0
2025-01-14 16:06:59.748 CST 0001 INFO [chaincodeCmd] ClientWait -> txid [d34c5484235df88e34d43ed589fde93614b8511294ec177efb62e98748d02eb5] committed with status (VALID) at localhost:9051
Chaincode definition approved on peer0.org2 on channel 'mychannel'
Using organization 1
Checking the commit readiness of the chaincode definition on peer0.org1 on channel 'mychannel'...
Attempting to check the commit readiness of the chaincode definition on peer0.org1, Retry after 3 seconds.
+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --output json
+ res=0
{
        "approvals": {
                "Org1MSP": true,
                "Org2MSP": true
        }
}
Checking the commit readiness of the chaincode definition successful on peer0.org1 on channel 'mychannel'
Using organization 2
Checking the commit readiness of the chaincode definition on peer0.org2 on channel 'mychannel'...
Attempting to check the commit readiness of the chaincode definition on peer0.org2, Retry after 3 seconds.
+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --output json
+ res=0
{
        "approvals": {
                "Org1MSP": true,
                "Org2MSP": true
        }
}
Checking the commit readiness of the chaincode definition successful on peer0.org2 on channel 'mychannel'
Using organization 1
Using organization 2
+ peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile /usr/local/chain-demo/fabric-samples/test-network/organizations/ordererOrganizations/example.com/tlsca/tlsca.example.com-cert.pem --channelID mychannel --name basic --peerAddresses localhost:7051 --tlsRootCertFiles /usr/local/chain-demo/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pem --peerAddresses localhost:9051 --tlsRootCertFiles /usr/local/chain-demo/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/tlsca/tlsca.org2.example.com-cert.pem --version 1.0 --sequence 1
+ res=0
2025-01-14 16:07:08.200 CST 0001 INFO [chaincodeCmd] ClientWait -> txid [dbb1c83b3b0839ee6a30bd2a4b1b6aada0813240849ae6da4c2381b98b04d419] committed with status (VALID) at localhost:7051
2025-01-14 16:07:08.257 CST 0002 INFO [chaincodeCmd] ClientWait -> txid [dbb1c83b3b0839ee6a30bd2a4b1b6aada0813240849ae6da4c2381b98b04d419] committed with status (VALID) at localhost:9051
Chaincode definition committed on channel 'mychannel'
Using organization 1
Querying chaincode definition on peer0.org1 on channel 'mychannel'...
Attempting to Query committed status on peer0.org1, Retry after 3 seconds.
+ peer lifecycle chaincode querycommitted --channelID mychannel --name basic
+ res=0
Committed chaincode definition for chaincode 'basic' on channel 'mychannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
Query chaincode definition successful on peer0.org1 on channel 'mychannel'
Using organization 2
Querying chaincode definition on peer0.org2 on channel 'mychannel'...
Attempting to Query committed status on peer0.org2, Retry after 3 seconds.
+ peer lifecycle chaincode querycommitted --channelID mychannel --name basic
+ res=0
Committed chaincode definition for chaincode 'basic' on channel 'mychannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
Query chaincode definition successful on peer0.org2 on channel 'mychannel'
Chaincode initialization is not required
[root@cdn-156-230-62-101 test-network]# export PATH=${PWD}/../bin:$PATH
PATH=$PWD/../config/
export CORE_PEER_TLS_ENABLED=[root@cdn-156-230-62-101 test-network]# export FABRIC_CFG_PATH=$PWD/../config/
true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/p[root@cdn-156-230-62-101 test-network]# export CORE_PEER_TLS_ENABLED=true
eerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.e[root@cdn-156-230-62-101 test-network]# export CORE_PEER_LOCALMSPID="Org1MSP"
xample.com/users/Admin@org1.example.com/msp
export[root@cdn-156-230-62-101 test-network]# export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
[root@cdn-156-230-62-101 test-network]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
[root@cdn-156-230-62-101 test-network]# export CORE_PEER_ADDRESS=localhost:7051
[root@cdn-156-230-62-101 test-network]# peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"InitLedger","Args":[]}'
2025-01-14 16:07:31.955 CST 0001 INFO [chaincodeCmd] chaincodeInvokeOrQuery -> Chaincode invoke successful. result: status:200 
[root@cdn-156-230-62-101 test-network]# peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
[{"AppraisedValue":300,"Color":"blue","ID":"asset1","Owner":"Tomoko","Size":5},{"AppraisedValue":400,"Color":"red","ID":"asset2","Owner":"Brad","Size":5},{"AppraisedValue":500,"Color":"green","ID":"asset3","Owner":"Jin Soo","Size":10},{"AppraisedValue":600,"Color":"yellow","ID":"asset4","Owner":"Max","Size":10},{"AppraisedValue":700,"Color":"black","ID":"asset5","Owner":"Adriana","Size":15},{"AppraisedValue":800,"Color":"white","ID":"asset6","Owner":"Michel","Size":15}]
[root@cdn-156-230-62-101 test-network]# ./network.sh down

安装jd

  1. 安装 EPEL 仓库(如果尚未安装):

    plainText

    sudo yum install epel-release
    
  2. 安装 jq:

    plainText

    sudo yum install jq
    
  3. 验证安装:

    plainText

    jq --version

Hyperledger Fabric 网络中的链码(智能合约)确实提供了一种类似于数据库的操作方式,允许程序员通过定义和调用链码中的函数来实现数据的增删改查操作。不过,它与传统数据库还是有一些区别的,以下是详细的对比和解释:

类似数据库的方面

  1. 数据存储

    • 链码状态(World State):Fabric 中的链码状态可以看作是一个键值存储,类似于数据库中的表。每个键对应一个值,这些键值对存储在账本的状态数据库中(默认是 LevelDB,也可以配置为 CouchDB)。
    • 增删改查操作
      • PutState:类似于数据库中的 INSERT 和 UPDATE 操作,用于添加或更新键值对。
      • GetState:类似于数据库中的 SELECT 操作,用于查询键对应的值。
      • DelState:类似于数据库中的 DELETE 操作,用于删除键值对。
  2. 事务处理

    • 事务提交:链码中的操作(如 PutState 和 GetState)会被封装在一个事务中,事务提交后,所有操作才会生效。这类似于数据库中的事务处理机制。
    • 原子性:事务要么全部成功,要么全部失败,确保数据的一致性。

与传统数据库的不同

  1. 分布式账本

    • 去中心化:Fabric 是一个分布式账本技术,数据存储在多个节点上,每个节点都维护一份账本的副本。这与传统数据库的集中式存储不同。
    • 共识机制:在 Fabric 中,事务需要通过共识机制(如 PBFT、SBFT、Kafka 等)来确保所有节点上的账本状态一致。传统数据库通常不需要这样的共识机制。
  2. 不可篡改

    • 区块链特性:Fabric 的账本是不可篡改的,一旦数据被写入账本,就无法修改。这与传统数据库中的数据可以被修改不同。
    • 历史记录:Fabric 保留了所有历史交易的记录,可以通过区块链查询历史数据。传统数据库通常只保留当前状态,历史数据需要额外的机制来管理。
  3. 智能合约

    • 链码逻辑:链码(智能合约)定义了数据的操作逻辑,这些逻辑是自动执行的,确保交易的业务规则被正确执行。传统数据库通常需要应用程序来实现业务逻辑。
    • 自动执行:链码中的函数在事务提交时自动执行,确保数据的一致性和业务规则的遵守。

示例

假设你有一个简单的投票应用,链码中定义了以下函数:

  • Vote:为候选人投票,更新候选人的票数。
  • GetVotes:查询候选人的票数。

在链码中,这些函数可以这样实现:


go

// Vote 为候选人投票
func (s *VoteContract) Vote(ctx contractapi.TransactionContextInterface, candidate string) error {
    votes, err := ctx.GetStub().GetState(candidate)
    if err != nil {
        return fmt.Errorf("failed to read votes: %v", err)
    }
    if votes == nil {
        votes = []byte("1")
    } else {
        voteCount := int(votes[0])
        votes = []byte{byte(voteCount + 1)}
    }
    err = ctx.GetStub().PutState(candidate, votes)
    if err != nil {
        return fmt.Errorf("failed to update votes: %v", err)
    }
    return nil
}

// GetVotes 查询候选人的票数
func (s *VoteContract) GetVotes(ctx contractapi.TransactionContextInterface, candidate string) (int, error) {
    votes, err := ctx.GetStub().GetState(candidate)
    if err != nil {
        return 0, fmt.Errorf("failed to read votes: %v", err)
    }
    if votes == nil {
        return 0, nil
    }
    return int(votes[0]), nil
}

通过 peer chaincode invokepeer chaincode query 命令,你可以在链码上调用这些函数,实现数据的增删改查操作。

总结

Hyperledger Fabric 网络中的链码确实提供了一种类似于数据库的操作方式,但它是基于分布式账本和智能合约的,具有去中心化、不可篡改和自动执行业务逻辑的特点。这些特性使得 Fabric 适用于需要高透明度和数据不可篡改的场景,如供应链管理、金融交易等

 

"Mstar Bin Tool"是一款专门针对Mstar系列芯片开发的固件处理软件,主要用于智能电视及相关电子设备的系统维护与深度定制。该工具包特别标注了"LETV USB SCRIPT"模块,表明其对乐视品牌设备具有兼容性,能够通过USB通信协议执行固件读写操作。作为一款专业的固件编辑器,它允许技术人员对Mstar芯片的底层二进制文件进行解析、修改与重构,从而实现系统功能的调整、性能优化或故障修复。 工具包中的核心组件包括固件编译环境、设备通信脚本、操作界面及技术文档等。其中"letv_usb_script"是一套针对乐视设备的自动化操作程序,可指导用户完成固件烧录全过程。而"mstar_bin"模块则专门处理芯片的二进制数据文件,支持固件版本的升级、降级或个性化定制。工具采用7-Zip压缩格式封装,用户需先使用解压软件提取文件内容。 操作前需确认目标设备采用Mstar芯片架构并具备完好的USB接口。建议预先备份设备原始固件作为恢复保障。通过编辑器修改固件参数时,可调整系统配置、增删功能模块或修复已知缺陷。执行刷机操作时需严格遵循脚本指示的步骤顺序,保持设备供电稳定,避免中断导致硬件损坏。该工具适用于具备嵌入式系统知识的开发人员或高级用户,在进行设备定制化开发、系统调试或维护修复时使用。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
Fabric环境搭建可按以下步骤进行: 1. **基础环境搭建**:采用Centos 7 64位操作系统,依次进行Docker安装、Docker - Compose安装、GO语言环境安装、Git安装和Fabric安装,最终完成可使用的Fabric环境 [^1]。 2. **下载执行脚本**:下载用来自动化下载fabric镜像、程序和测试网运行脚本等工具的脚本,使用命令 `wget https://gitee.com/real__cool/fabric_install/raw/main/bootstrap.sh` [^3]。 3. **配置二进制文件**:进入到 `~/hyperledger-fabric/fabric-samples/bin` 目录,将该目录下的二进制文件(如 `configtxgen`、`configtxlator` 等)拷贝到 `/usr/local/bin` 目录下,使fabric的这些可执行程序能在全局范围内使用。操作命令如下: ```bash cd ~/hyperledger-fabric/fabric-samples/bin sudo cp * /usr/local/bin ``` [^2] 4. **项目放置与网络启动**:将fabric项目放在 `github.com/hyperledger/` 下,进入到指定目录 `gopath` 下的 `fabric/examples/e2e_cli`,启动Fabric网络使用命令 `./network_setup.sh up`。启动脚本会进行以下操作: - 编译生成Fabric公私钥、证书的程序,程序在目录:`fabric/release/linux - amd64/bin`。 - 基于 `configtx.yaml` 生成创世区块和通道相关信息,并保存在 `channel - artifacts` 文件夹。 - 基于 `crypto - config.yaml` 生成公私钥和证书信息,并保存在 `crypto - config` 文件夹中。 - 基于 `docker - compose - cli.yaml` 启动1Orderer + 4Peer + 1CLI的Fabric容器。 - 在CLI启动的时候,会运行 `scripts/script.sh` 文件,该脚本文件包含了创建Channel,加入Channel等操作。若出现 `end` 说明启动没问题 [^5]。 5. **关闭Fabric网络**:若要关闭Fabric网络,首先运行 `exit` 命令退出 `cli` 容器,然后使用命令 `cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli` 和 `./network_setup.sh down` 关闭网络 [^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值