Hyperledger fabric 如何更新事务处理器函数内的关系值?
我有一个模型文件Hyperledger fabric 如何更新事务处理器函数内的关系值?,hyperledger-fabric,hyperledger,hyperledger-composer,Hyperledger Fabric,Hyperledger,Hyperledger Composer,我有一个模型文件org.acme.interm.container.cto,看起来像这样: namespace org.acme.interm.container asset Container identified by containerId { o String containerId regex=/[A-Z][A-Z][0-9][0-9][0-9]-[0-9][0-9]-[0-3][0-9]-[0-9][0-9]/ o String containerNumber --&
org.acme.interm.container.cto
,看起来像这样:
namespace org.acme.interm.container
asset Container identified by containerId {
o String containerId regex=/[A-Z][A-Z][0-9][0-9][0-9]-[0-9][0-9]-[0-3][0-9]-[0-9][0-9]/
o String containerNumber
--> Truck truck optional
}
asset Truck identified by truckId {
o String truckId
o Ownership ownershipType default="LEASED"
o Integer normalWeight range = [,100]
o Integer fragileWeight range = [,50]
}
enum Ownership {
o LEASED
o OWNED
}
transaction AssignTruck {
o String containerId
o String truckId
}
event TruckAssigned {
o String containerId
o String truckId
}
transaction LoadContainer {
o String containerId
--> Truck truck
o Integer fragileWeight
o Integer normalWeight
}
我在以下事务中将卡车关系分配给集装箱资产:
function AssignTruck(containerTruckData) {
console.log("ASSIGN TRUCK CALLED!");
var containerRegistry={}
return getAssetRegistry('org.acme.interm.container.Container').then(function(registry){
containerRegistry = registry
return containerRegistry.get(containerTruckData.containerId);
}).then(function(container){
console.log("Got Container",container);
if(!container) throw new Error("Container : "+containerTruckData.containerId," Not Found!!!");
var factory = getFactory();
var relationship = factory.newRelationship('org.acme.interm.truck','Truck',containerTruckData.truckId);
container.truck = relationship;
return containerRegistry.update(container);
}).then(function(){
// Successful update
var event = getFactory().newEvent('org.acme.interm.container', 'TruckAssigned');
event.containerId = containerTruckData.containerId;
event.truckId = containerTruckData.truckId;
emit(event);
}).catch(function(error){
throw new Error(error);
});
}
现在,如何编写loadContainer事务,其中容器(对于给定的containerId)的normalWeight和fragileWeight属性的现有值应添加到新传递的fragileWeight和normalWeight中
值?首先,除非每个容器都有一个重量变量,否则无法跟踪单个重量。即使你不断更新卡车的重量,你也会知道集装箱的总重量是不够的。尤其是如果你想卸载集装箱。我建议采用这种方法
asset Container identified by containerId {
o String containerId regex=/[A-Z][A-Z][0-9][0-9][0-9]-[0-9][0-9]-[0-3][0-9]-[0-9][0-9]/
o String containerNumber
o Integer normalWeight range = [,100]
o Integer fragileWeight range = [,50]
--> Truck truck optional
}
asset Truck identified by truckId {
o String truckId
o Ownership ownershipType default="LEASED"
o Integer totalNormalWeight range default=0
o Integer totalFragileWeight range default=0
--> Container containersLoaded[] optional
}
因此,这将让您跟踪每个集装箱的重量,然后将它们相加,得到卡车的总重量。containersLoaded
变量可用于跟踪已装载的集装箱。这样做的一个优点是,如果卸载或拆分容器(我认为这将是用户流的一部分),您可以准确地减轻重量
这应该对你有用。现在,您可以通过检查totalNormalWeight
和totalFragileWeight
变量来查询卡车并获取所有集装箱的总重量。您还可以检查containersLoaded
数组以获取容器列表。要获取容器的单个重量,您可以查询容器并获取其重量,也可以直接从containersLoaded
变量获取其重量。
当集装箱需要从卡车上卸下/卸载时,您可以得到它们各自的重量并从总重量中减去,然后从
集装箱装载
中弹出关系。我有点困惑。根据您的资产,容器
不存储任何与重量相关的变量。我假设它们将从定义的Truck
关系中引用。是否要向卡车添加多个集装箱并跟踪卡车的总重量?或者你的意思是分配和跟踪单个集装箱的重量,然后将其合计为卡车重量?嗨,瓦伦,你说得对。集装箱重量将从指定为关系的卡车中引用。正如你们所说,我想知道我们如何分配和跟踪单个集装箱的重量,然后将其合计为卡车重量
function AssignTruck(containerTruckData) {
var myContainer; // store data from the promise
var myTruck;
console.log("ASSIGN TRUCK CALLED!");
var containerRegistry={}
var truckRegistry;
return getAssetRegistry('org.acme.interm.container.Container').then(function(registry){
containerRegistry = registry
return containerRegistry.get(containerTruckData.containerId);
}).then(function(container){
console.log("Got Container",container);
myContainer = container;
if(!container) throw new Error("Container : "+containerTruckData.containerId," Not Found!!!");
var factory = getFactory();
var relationship = factory.newRelationship('org.acme.interm.truck','Truck',containerTruckData.truckId);
container.truck = relationship;
return containerRegistry.update(container);
}).then(function() {
return getAssetRegistry('org.acme.interm.truck.Truck');
}).then(function (truckRegistry) {
truckRegistry = truckRegistry;
return truckRegistry.get(containerTruckData.truckId);
}).then(function (truck) {
truck.totalNormalWeight += myContainer.normalWeight;
truck.totalFragileWeight += myContainer.fragileWeight;
if (!truck.containersLoaded) {
truck.containersLoaded = [];
} else {
truck.containersLoaded.push(factory.newRelationship('org.acme.interm.container', 'Container', myContainer.containerId))
};
return truckRegistry.update(truck)
}).then(function(){
// Successful update
var event = getFactory().newEvent('org.acme.interm.container', 'TruckAssigned');
event.containerId = containerTruckData.containerId;
event.truckId = containerTruckData.truckId;
emit(event);
}).catch(function(error){
throw new Error(error);
});
}