stub.GetHistoryKeys() reporting GetHistoryKeys() function undefined. when tried go build on my Chaincode

huangapple go评论104阅读模式
英文:

stub.GetHistoryKeys() reporting GetHistoryKeys() function undefined. when tried go build on my Chaincode

问题

我对Hyperledger还不太熟悉。我正在使用Docker来运行Hyperledger。我从Docker Hub上拉取了hyperledger/fabric-peer:latest镜像,并且能够在我的Chaincode中运行stub.CreateTable()stub.GetRows()stub.InsertRows()和其他一些函数。但是当我尝试在我的Chaincode中运行stub.GetHistoryKeys()stub.GetCompositeKeys()等函数时,报错了:

stub.GetHistoryForKey undefined (type shim.ChaincodeStubInterface has no field or method GetHistoryForKey)

我发现在我的interface.go文件中没有这些函数。我在Google上搜索了很多,但没有找到相关信息。有人能告诉我正确的hyperledger/fabric-peer镜像,以便在Chaincode中运行上述函数吗?

英文:

I am new to Hyperledger .I am using docker to run Hyperledger. Pulled hyperledger/fabric-peer:latest from Docker hub and
able to run stub.CreateTable() ,stub.GetRows() , stub.InsertRows() and some other functions in my Chaincode. But when i tried to run

stub.GetHistoryKeys() or stub.GetCompositeKeys() ...etc in my chaincode
It's reporting an error

 stub.GetHistoryForKey undefined (type shim.ChaincodeStubInterface has no field 
  or method GetHistoryForKey)

I found that in my interface.go file there are no such functions . Googled a lot but found nothing .Can anyone tell the correct hyperledger/fabric-peer image to pull so that the above functions can run in Chaincode.

答案1

得分: 0

请下载最新版本的fabric镜像(hyperledger/fabric-peer x86_64-1.1.0)。可以从hyperledger官方网站上提到的脚本(安装二进制文件)下载(https://hyperledger-fabric.readthedocs.io/en/latest/install.html)。由于stackoverflow的政策问题,无法粘贴URL。下载完成后,创建一个Go代码。只需在一个键上添加一个JSON记录,然后尝试添加一些更改JSON字段的内容,再次添加到同一个键上。完成后,使用以下代码进行gethistory:

func (s *SmartContract) getAllTransactionForid(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
fmt.Println("getAllTransactionForNumber called")
id := args[0]

resultsIterator, err := APIstub.GetHistoryForKey(id)
if err != nil {
	return shim.Error(err.Error())
}
defer resultsIterator.Close()

// buffer is a JSON array containing historic values for the number
var buffer bytes.Buffer
buffer.WriteString("[")

bArrayMemberAlreadyWritten := false
for resultsIterator.HasNext() {
	response, err := resultsIterator.Next()
	if err != nil {
		return shim.Error(err.Error())
	}
	// Add a comma before array members, suppress it for the first array member
	if bArrayMemberAlreadyWritten == true {
		buffer.WriteString(",")
	}
	buffer.WriteString("{\"TxId\":\"")
	buffer.WriteString("\"")
	buffer.WriteString(response.TxId)
	buffer.WriteString("\"")

	buffer.WriteString(", \"Value\":")
	// if it was a delete operation on given key, then we need to set the
	//corresponding value null. Else, we will write the response.Value
	//as-is (as the Value itself a JSON marble)
	if response.IsDelete {
		buffer.WriteString("null")
	} else {
		buffer.WriteString(string(response.Value))
	}

	buffer.WriteString(", \"Timestamp\":\"")
	buffer.WriteString("\"")
	buffer.WriteString(time.Unix(response.Timestamp.Seconds, int64(response.Timestamp.Nanos)).String())
	buffer.WriteString("\"")

	buffer.WriteString(", \"IsDelete\":\"")
	buffer.WriteString("\"")
	buffer.WriteString(strconv.FormatBool(response.IsDelete))
	buffer.WriteString("\"")

	buffer.WriteString("}")
	bArrayMemberAlreadyWritten = true
}
if !bArrayMemberAlreadyWritten {
	buffer.WriteString("No record found")
}
buffer.WriteString("]")

fmt.Printf("- getAllTransactionForNumber returning:\n%s\n", buffer.String())

return shim.Success(buffer.Bytes())

}

如果还有疑问,请回复。我会给你整个源代码,以使其正常工作。但我希望这能解决你的问题 stub.GetHistoryKeys() reporting GetHistoryKeys() function undefined. when tried go build on my Chaincode

英文:

Please download latest version of fabric image, (
hyperledger/fabric-peer x86_64-1.1.0 ). It can be downloaded from script mentioned on hyperledger official website (Install binary)=> (https://hyperledger-fabric.readthedocs.io/en/latest/install.html. Can't paste url due to stackover flow policy issue). Once you have it. Create a go code. Simply add one json record on one key add then try to add some change some field of json and again add to same key. Once you have done that, fire the below code for gethistory:=>

func (s *SmartContract) getAllTransactionForid(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
fmt.Println("getAllTransactionForNumber called")
id:= args[0]

resultsIterator, err := APIstub.GetHistoryForKey(id)
if err != nil {
	return shim.Error(err.Error())
}
defer resultsIterator.Close()

// buffer is a JSON array containing historic values for the number
var buffer bytes.Buffer
buffer.WriteString("[")

bArrayMemberAlreadyWritten := false
for resultsIterator.HasNext() {
	response, err := resultsIterator.Next()
	if err != nil {
		return shim.Error(err.Error())
	}
	// Add a comma before array members, suppress it for the first array member
	if bArrayMemberAlreadyWritten == true {
		buffer.WriteString(",")
	}
	buffer.WriteString("{\"TxId\":")
	buffer.WriteString("\"")
	buffer.WriteString(response.TxId)
	buffer.WriteString("\"")

	buffer.WriteString(", \"Value\":")
	// if it was a delete operation on given key, then we need to set the
	//corresponding value null. Else, we will write the response.Value
	//as-is (as the Value itself a JSON marble)
	if response.IsDelete {
		buffer.WriteString("null")
	} else {
		buffer.WriteString(string(response.Value))
	}

	buffer.WriteString(", \"Timestamp\":")
	buffer.WriteString("\"")
	buffer.WriteString(time.Unix(response.Timestamp.Seconds, int64(response.Timestamp.Nanos)).String())
	buffer.WriteString("\"")

	buffer.WriteString(", \"IsDelete\":")
	buffer.WriteString("\"")
	buffer.WriteString(strconv.FormatBool(response.IsDelete))
	buffer.WriteString("\"")

	buffer.WriteString("}")
	bArrayMemberAlreadyWritten = true
}
if !bArrayMemberAlreadyWritten {
	buffer.WriteString("No record found")
}
buffer.WriteString("]")

fmt.Printf("- getAllTransactionForNumber returning:\n%s\n", buffer.String())

return shim.Success(buffer.Bytes())

}

If still in doubt, please revert. I will give you my whole source code to make it work. But I hope this will make your problem go away stub.GetHistoryKeys() reporting GetHistoryKeys() function undefined. when tried go build on my Chaincode

答案2

得分: -2

最后,我终于找到了方法来获取支持我的链码的Hyperledger镜像。

英文:

At last I am able to figure it out to get the hyperledger images to support my chaincode.

huangapple
  • 本文由 发表于 2017年4月24日 14:34:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/43581283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定