我正在 Hyperledger Fabric 2.4 中进行编码。我需要查询链码中当前的块高度,但我没有找到这样的函数。我怎样才能实现这个目标?
system chaincode
获取有关通道的信息。当区块链网络启动时,system chaincode
是“内置”的
通道信息(包括区块高度)可以通过
QSCC
系统链码获取。
查询系统链码(QSCC)在所有节点中运行,提供账本 API,包括区块查询、交易查询等
import (
"fmt"
"github.com/hyperledger/fabric-chaincode-go/shim"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/golang/protobuf/proto"
commonProto "github.com/hyperledger/fabric-protos-go/common"
)
func (t *MyChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
// ... your chaincode logic ...
// Define the system chaincode you want to call and the arguments.
// In this example, we'll query the block height using the "qscc" system chaincode.
sysCC := "qscc"
fcn := "GetChainInfo"
channelID := "mychannel"
// Call the system chaincode
response := stub.InvokeChaincode(sysCC, [][]byte{[]byte(fcn), []byte(channelID)}, channelID)
// Check the response for errors
if response.Status != shim.OK {
errMsg := fmt.Sprintf("Error invoking '%s' chaincode: %s", sysCC, response.Message)
return shim.Error(errMsg)
}
// Process the response payload
result := response.Payload
// Unmarshal the response payload
chainInfo := &commonProto.BlockchainInfo{}
err := proto.Unmarshal(result, chainInfo)
if err != nil {
errMsg := fmt.Sprintf("Error unmarshaling BlockchainInfo: %s", err)
return shim.Error(errMsg)
}
// Get the block height
blockHeight := chainInfo.GetHeight()
// ... do something with the block height
// ... continue with your chaincode logic and return the appropriate response
}
感谢您的回答!我总是得到0。
线路是否正确,channelID出现了两次。
response := stub.InvokeChaincode(sysCC, [][]byte{[]byte(fcn), []byte(channelID)}, channelID)
应该是吗
response := stub.InvokeChaincode(sysCC, [][]byte{[]byte(fcn)}, channelID)