fabric 2.4.2-simple(1)链码开发(go)

链码开发(go)基于fabric-contract-api-go项目进行开发。这里以fabric-samples/asset-transfer-basic/chaincode-go为例进行说明。

开发步骤
1)本地保证工程编译通过;
2)部署到测试网络环境,然后进行打包、安装、实例化、提交等。

1 fabric-contract-api-go

github:
https://github.com/hyperledger/fabric-contract-api-go

hyperledger fabric平台已经实现TransactionContextInterface接口,开发链码可直接调用。

	//package contractapi
	type TransactionContextInterface interface {
	    GetStub() shim.ChaincodeStubInterface
		...
	
	//package shim
	type ChaincodeStubInterface interface {
		InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response
		GetState(key string) ([]byte, error)
		PutState(key string, value []byte) error
		DelState(key string) error
		GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error)

2 本地开发

开发环境:go 1.16
编辑器:Goland

第1步:资产model的struct:

type Asset struct {
	AppraisedValue int    `json:"AppraisedValue"`
	Color          string `json:"Color"`
	ID             string `json:"ID"`
	Owner          string `json:"Owner"`
	Size           int    `json:"Size"`
}

第2步:定义链码:管理资产的struct:

type SmartContract struct {
	contractapi.Contract
}
```go

接着实现链码的功能函数:

// 初始化账本
func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) error{...
} 
// 创建资产
func (s *SmartContract) CreateAsset(ctx contractapi.TransactionContextInterface, id string, color string, size int, owner string, appraisedValue int) error {...
} 
// 通过ID来读取资产
func (s *SmartContract) ReadAsset(ctx contractapi.TransactionContextInterface, id string) (*Asset, error) {...
} 
// 更新资产
func (s *SmartContract) UpdateAsset(ctx contractapi.TransactionContextInterface, id string, color string, size int, owner string, appraisedValue int) error {...
} 
// 删除资产
func (s *SmartContract) DeleteAsset(ctx contractapi.TransactionContextInterface, id string) error {...
} 
// 通过id判断资产是否存在
func (s *SmartContract) AssetExists(ctx contractapi.TransactionContextInterface, id string) (bool, error) {...
} 
// 转移资产
func (s *SmartContract) TransferAsset(ctx contractapi.TransactionContextInterface, id string, newOwner string) (string, error) {...
} 
// 获取所有的资产
func (s *SmartContract) GetAllAssets(ctx contractapi.TransactionContextInterface) ([]*Asset, error) {...
} 

可见主要通过TransactionContextInterface接口来实现各种功能。

第3步:调用链码:

func main() {
	assetChaincode, err := contractapi.NewChaincode(&chaincode.SmartContract{})
	if err != nil {
		log.Panicf("Error creating asset-transfer-basic chaincode: %v", err)
	} else{
		log.Println("链码创建成功!")
	}

	if err := assetChaincode.Start(); err != nil {
		//log.Panicf("Error starting asset-transfer-basic chaincode: %v", err)
	}
}

最后保证编译通过:
在这里插入图片描述

3 部署测试网络

参考:
Hyperledger Fabric入门(4)-fabric CLI调用智能合约

代码详见:
https://gitee.com/linghufeixia/fabric-simple
chaincode-go工程。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值