正常的调用命令如下
peer chaincode invoke --tls --cafile $ORDERER_CA -C mychannel -n mycc -c '{"Args":["get","a"]}'
但是我写了一个这样一个函数
//接收对象并调用 PutFile 函数实现保存状态的功能
func (t *DataChaincode) addFile(stub shim.ChaincodeStubInterface, args []string) peer.Response {
//两个参数前一个是file,后一个是事件ID
if len(args) != 2 {
return shim.Error("给定的参数个数不符合要求")
}
var file DataFile
err := json.Unmarshal([]byte(args[0]), &file)
if err != nil {
return shim.Error("反序列化信息时发生错误")
}
// 查重: 文件名必须唯一
_, exist := GetFileInfo(stub, file.FileName)
if exist {
return shim.Error("要添加的文件名已存在")
}
_, bl := PutFile(stub, file)
if !bl {
return shim.Error("保存信息时发生错误")
}
err = stub.SetEvent(args[1], []byte{})
if err != nil {
return shim.Error(err.Error())
}
return shim.Success([]byte("信息添加成功"))
}
其中两个方法是这样写的
//PutFile 写入要上传的文件信息,实现将指定的对象序列化后保存至分类账本中
func PutFile(stub shim.ChaincodeStubInterface, file DataFile) ([]byte, bool) {
f, err := json.Marshal(file)
if err != nil {
return nil, false
}
err = stub.PutState(file.FileName, f)
if err != nil {
return nil, false
}
return f, true
}
// GetFileInfo 根据文件名查询信息状态
// args: Filename
func GetFileInfo(stub shim.ChaincodeStubInterface, filename string) (DataFile, bool) {
var file DataFile
// 根据记录序号查询信息状态
f, err := stub.GetState(filename)
if err != nil {
return file, false
}
if f == nil {
return file, false
}
// 对查询到的状态进行反序列化
err = json.Unmarshal(f, &file)
if err != nil {
return file, false
}
DataFile结构体定义如下
type DataFile struct {
FileName string `json:"FileName"` //数据资源文件名
ID string `json:"ID"` //提供者ID
IPAdress string `json:"IPAdress"` //提供者IP地址
OrgName string `json:"OrgName"` //提供者所属组织名
ModifyTime string `json:"ModifyTime"` //修改时间
}
我要在命令行中调用addfile这个函数,给它一个如下参数,参数应该怎么写
file1 := DataFile{
FileName: "1.txt",
ID: "121",
IPAdress: "1.1.1.2",
OrgName: "org2",
ModifyTime: "8:27 2019/9/12",
}