【发布时间】:2020-02-09 14:01:09
【问题描述】:
我正在使用 fabcar 项目 (https://github.com/IBM/blockchain-application-using-fabric-java-sdk)
我添加了“价格”作为汽车结构的新参数。
我想获得我的区块链网络中汽车的平均价格。
是否有任何 go 函数可以这样做?
【问题讨论】:
标签: go hyperledger-fabric blockchain hyperledger-chaincode
我正在使用 fabcar 项目 (https://github.com/IBM/blockchain-application-using-fabric-java-sdk)
我添加了“价格”作为汽车结构的新参数。
我想获得我的区块链网络中汽车的平均价格。
是否有任何 go 函数可以这样做?
【问题讨论】:
标签: go hyperledger-fabric blockchain hyperledger-chaincode
您可以使用 GetHistoryForKey() 检索所有汽车,然后计算它们的平均价格。
假设你想知道你能做到的最高价格:
resultsIterator, err := stub.GetHistoryForKey(id)
if err != nil {
return shim.Error('Error')
}
var max = nil;
for resultsIterator.HasNext() {
response, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
if (response.Value.Price > max)
max = response.Value.Price
}
return shim.Success(max)
Go 中有类似的东西。 平均价格容易改变。
【讨论】: