1.开启网络
没啥好说的,之前要下samples,二进制和镜像
cd fabric-samples/test-network
把之前网络关闭了
./network.sh down
用下面的指令开启测试网络:
./network.sh up createChannel
createchannel指令创造了一个叫mychannel的channel(两个组织org1和2)。
成功的画面8说了,就是channel successfully joined。
这会可以用peer cli来部署asset-transfer (basic)链码到通道了。
第0步:开启Logspout
这就是一个监测链码的工具了,也可以不打开。
cd fabric-samples/test-network
cp ../commercial-paper/organization/digibank/configuration/cli/monitordocker.sh
官方第一行指令多了一个.,删掉。接着运行它:
./monitordocker.sh net_test
显示 Starting monitoring on all containers on the network net_basic,就ok了
第一步:打包智能合约
在安装到我们peer节点之前我们要打包。用go,js, Typescript步骤略有不同,我这边用的是go
在打包链码之前,要安装链码的依赖。导航到go版本的那个文件夹(包含the asset-transfer (basic) chaincode.)
cd fabric-samples/asset-transfer-basic/chaincode-go
案例用的是go 模组来安装链码依赖。依赖都在go.mod文件中。(代码不贴了)
go.mod文件把fabric 合约api输出到智能合约包了。
打开asset-transfer-basic/chaincode-go/chaincode/smartcontract.go,能够看到合约api是怎么用来定义smartcontract类型的:
// SmartContract provides functions for managing an Asset
type SmartContract struct {
contractapi.Contract
}
这个smartcontract type用于为智能合约中定义的函数创建事务上下文,这些函数用于向区块链分类账读写数据。
// CreateAsset issues a new asset to the world state with given details.
func (s *SmartContract) CreateAsset(ctx contractapi.TransactionContextInterface, id string, color string, size int, owner string, appraisedValue int) error {
exists, err := s.AssetExists(ctx, id)
if err != nil {
return err
}
if exists {
return fmt.Errorf("the asset %s already exists", id)
}
asset := Asset{
ID: id,
Color: color,
Size: size,
Owner: owner,
AppraisedValue: appraisedValue,
}
assetJSON, err := json.Marshal(asset)
if err != nil {