Fabric v2.2 单机部署 & 使用tape测试

文章目录

本篇博客基于 Fabric v2.2 部署了一个联盟链,包含一个Org和两个peer,使用了单个节点的raft作为Ordering service。部署过程主要参考了Fabric的官方文档,大部分脚本和配置文件都来自于官方提供的样例。
测试使用了 tape 这个轻量级的工具,个人觉得比 caliper 好用。

部署

curl -sSL https://bit.ly/2ysbOFE | bash -s -- 2.2.2 1.4.9
cd fabric-samples/test-network
./network.sh up
./network.sh createChannel   

# 使用smallbank进行测试
mkdir -p smallbank/go
cd smallbank/go
touch go.mod smallbank.go # 这两个文件内容在下面,复制进去之后,再运行下一条命令
./network.sh deployCC -ccn smallbank -ccp ./smallbank/go -ccl go -cci InitLedger

go.mod 文件如下:

module github.com/hyperledger/fabric-samples/chaincode/smallbank/go

go 1.13

require github.com/hyperledger/fabric-contract-api-go v1.1.0

smallbank.go 文件如下:

package main

import (
        "encoding/json"
        "fmt"
        "crypto/sha512"
        "encoding/hex"
        "strings"

        "github.com/hyperledger/fabric-contract-api-go/contractapi"
)

var namespace = hexdigest("smallbank")[:6]

// SmartContract provides functions for managing a Account
type SmartContract struct {
   
        contractapi.Contract
}

type Account struct {
   
        CustomId   string
        CustomName string
        SavingsBalance int
        CheckingBalance int
}

func hexdigest(str string) string {
   
        hash := sha512.New()
        hash.Write([]byte(str))
        hashBytes := hash.Sum(nil)
        return strings.ToLower(hex.EncodeToString(hashBytes))
}


func accountKey(id string) string {
   
        return namespace + hexdigest(id)[:64]
}


func saveAccount(ctx contractapi.TransactionContextInterface, account *Account) error {
   
        accountBytes, err := json.Marshal(account)
        if err != nil {
   
                return err
        }
        key := accountKey(account.CustomId)
        return ctx.GetStub().PutState(key, accountBytes)
}
func loadAccount(ctx contractapi.TransactionContextInterface, id string) (*Account, error) {
   
        key := accountKey(id)
        accountBytes,err := ctx.GetStub().GetState(key
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值