错误:集合中ID为'$ sysregistries'的ID为'Asset:org.supplychain.food.model.LiveAsset'的对象不存在”

时间:2019-04-26 02:25:37

标签: hyperledger-fabric hyperledger hyperledger-composer

我正在尝试运行此GitHub项目https://github.com/Daniel-Szego/FoodSupplyChainDemo/blob/master/README.md 一切都进行得很好,除了此事务“消耗”抛出上述错误 模型文件是

我已经创建了每个参与者并进行了资产评估,我要消耗的资产为atstate = resturant 在历史学家中,我已经看到牛排资产在那里,并且它的当前状态是我正在通过消费交易但还算不上运气的餐馆。

namespace org.supplychain.food.model

/**
 * ASSETS
 */

enum AssetStatus {
  o LIVE 
  o PROCESSED
  o CONSUMED
}

abstract asset LiveAsset identified by assetId {
  o String assetId
  o AssetStatus assetStatus
  o Double aggregatedGHG
  --> SupplyChainState atState
}

abstract asset Vegetables extends LiveAsset {
  o Double amount
}

abstract asset Animals extends LiveAsset {
  o Integer amount
}

asset Cow extends Animals{
}

asset Steak extends Animals{
}


/**
 * PARTICIPANTS
 */

concept Address {
  o String country
  o String city optional
  o String street optional
  o Integer houseNr optional
}

abstract participant SupplyChainState identified by stateId {
  o String stateId
  o String stateName
  o Address stateAddress
  o Double GHG 
  --> SupplyChainState [] stateFrom optional
}

participant Production extends SupplyChainState {

}

participant Processing extends SupplyChainState {

}

participant Distribution extends SupplyChainState {

}

participant Retail extends SupplyChainState {

}

participant Restaurant extends SupplyChainState {

}

/**
 * TRANSACTION
 */

transaction InitTestData {
}

transaction ClearData {
}

transaction Process {
  --> LiveAsset liveAsset
  --> SupplyChainState fromState
  --> SupplyChainState toState
}

transaction Produce {
 --> Production atProduction
}

transaction Consume {
  --> LiveAsset liveAsset
  --> Restaurant atRestaurant
}

/**
 * EVENTS
 */

event AssetProduced {
  --> LiveAsset liveAsset
  o Double creationGHG
}

event AssetProcessed {
  --> LiveAsset liveAsset
  o Double transferGHG
}

event AssetConsumed {
  --> LiveAsset liveAsset
  o Double endGHG
}

脚本文件是 我猜最后消费功能有问题 但我对此了解不多。

/**
 * Transaction file for food supply chain
 */

const namespace = "org.supplychain.food.model";

/**
 *
 * @param {org.supplychain.food.model.InitTestData} param - model instance
 * @transaction
 */
async function InitTestDataFunction(param) {  

      const ghgBsaeLineInfo = await request.get( { uri:'https://iaspub.epa.gov/enviro/efservice/tri_facility/state_abbr/VA/rows/102:102/JSON', json: true});
    const baseline = parseInt(ghgBsaeLineInfo[0].PREF_QA_CODE) / 10;

    console.log('init test data');

    console.log('Creating a Production state');  
    const factory = getFactory(); 

    // adding Production state 
    const productionReg = await getParticipantRegistry(namespace + '.Production');   
    const production = await factory.newResource(namespace, 'Production', "1");
    production.stateName = "Production";
    production.GHG = baseline;
    const newAddress = await factory.newConcept(namespace, 'Address');
    newAddress.country = "Bejing";
    newAddress.city = "China";
    newAddress.street = "Xia Mo Street";
    newAddress.houseNr = 16;
    production.stateAddress = newAddress;

    await productionReg.add(production);       

    // adding Processing State
    console.log('Creating a Processing state');  

    const processingReg = await getParticipantRegistry(namespace + '.Processing');   
    const processing = await factory.newResource(namespace, 'Processing', "2");
    processing.stateName = "Processing";
    processing.GHG = baseline / 2;
    const newAddress2 = await factory.newConcept(namespace, 'Address');
    newAddress2.country = "Hong Kong";
    newAddress2.city = "China";
    newAddress2.street = "Mua Mo Street";
    newAddress2.houseNr = 22;
    processing.stateAddress = newAddress2;
    processing.stateFrom = new Array();
    processing.stateFrom.push(production);

    await processingReg.add(processing);       

    // adding Distribution State 
    console.log('Creating a Distribution State');  

    const distributionReg = await getParticipantRegistry(namespace + '.Distribution');   
    const distribution = await factory.newResource(namespace, 'Distribution', "3");
    distribution.stateName = "Distribution";
    distribution.GHG = baseline / 1.75;
    const newAddress5 = await factory.newConcept(namespace, 'Address');
    newAddress5.country = "China";
    newAddress5.city = "Hong Kong";
    newAddress5.street = "Seeside";
    newAddress5.houseNr = 4;
    distribution.stateAddress = newAddress5;
    distribution.stateFrom = new Array();
    distribution.stateFrom.push(processing);

    await distributionReg.add(distribution);       

    // adding Retail State
    console.log('Creating a Retail State');  

    const retailReg = await getParticipantRegistry(namespace + '.Retail'); 
    const retail = await factory.newResource(namespace, 'Retail', "4");
    retail.stateName = "Retail";
    retail.GHG = baseline * 1.5;
    const newAddress3 = await factory.newConcept(namespace, 'Address');
    newAddress3.country = "Sydney";
    newAddress3.city = "Australia";
    newAddress3.street = "Beecon Str";
    newAddress3.houseNr = 122;
    retail.stateAddress = newAddress3;
    retail.stateFrom = new Array();
    retail.stateFrom.push(distribution);  

    await retailReg.add(retail);       

    // adding Restaurant 
    console.log('Creating a Restaurant State');  

    const restaurantReg = await getParticipantRegistry(namespace + '.Restaurant'); 
    const restaurant = await factory.newResource(namespace, 'Restaurant', "5");
    restaurant.stateName = "Restaurant";
    restaurant.GHG = baseline * 1.25;
    const newAddress6 = await factory.newConcept(namespace, 'Address');
    newAddress6.country = "Italy";
    newAddress6.city = "Triest";
    newAddress6.street = "Via Rue";
    newAddress6.houseNr = 44;
    restaurant.stateAddress = newAddress6;
    restaurant.stateFrom = new Array();
    restaurant.stateFrom.push(retail);  

    await restaurantReg.add(restaurant);       
}

/**
 *
 * @param {org.supplychain.food.model.ClearData} param - model instance
 * @transaction
 */
async function ClearDataFunction(param) {  
    console.log('clearing test data');

    // deleting assets
    const CowReg = await getAssetRegistry(namespace + '.Cow'); 
    let Cows = await CowReg.getAll();
    await CowReg.removeAll(Cows);

    const steakReg = await getAssetRegistry(namespace + '.Steak'); 
    let steaks = await steakReg.getAll();
    await steakReg.removeAll(steaks);

    // deleting participants
    const productionReg = await getParticipantRegistry(namespace + '.Production');
    let production = await productionReg.getAll();
    await productionReg.removeAll(production);

    const processingReg = await getParticipantRegistry(namespace + '.Processing'); 
    let processing = await processingReg.getAll();
    await processingReg.removeAll(processing);

    const distributionReg = await getParticipantRegistry(namespace + '.Distribution'); 
    let istribution = await distributionReg.getAll();
    await distributionReg.removeAll(istribution);

    const retailReg  = await getParticipantRegistry(namespace + '.Retail'); 
    let retail = await retailReg.getAll();
    await retailReg.removeAll(retail);

    const restaurantReg  = await getParticipantRegistry(namespace + '.Restaurant'); 
    let restaurant = await restaurantReg.getAll();
    await restaurantReg.removeAll(restaurant);

    console.log('clearing all data finished');  
}

/**
 *
 * @param {org.supplychain.food.model.Process} param - model instance
 * @transaction
 */
async function ProcessFunction(param) {  
    let liveAsset = param.liveAsset;
    let fromState = param.fromState;
    let toState = param.toState;

    liveAsset.assetStatus = "PROCESSED";

    // checking if transfer is valid

    if(fromState.stateFrom) {
        let isValidTransfer = false;
        await toState.stateFrom.forEach(function (state) {
            if(state == fromState)
            {
                isValidTransfer = true;
            }
        });
        if(isValidTransfer == false) {
            throw new Error('Invalid transfer');
        }  
    }

    liveAsset.atState = toState;
    liveAsset.aggregatedGHG = liveAsset.aggregatedGHG + toState.GHG;          

    if (fromState.$type == "Processing") {
        // producing meat from cow       
        // delete cow
        let liveAssetReg = await getAssetRegistry(namespace + '.Cow');        
        liveAssetReg.remove(liveAsset);

        // create 2 Steaks
        await ProduceMeat(toState); 

    } else {

        liveAsset.assetStatus = "PROCESSED";    
        var liveAssetReg;
        if (liveAsset.$type == "Cow") {
             liveAssetReg = await getAssetRegistry(namespace + '.Cow');        
        }
        else if (liveAsset.$type == "Steak") {
             liveAssetReg = await getAssetRegistry(namespace + '.Steak');            
        }
        else {
             liveAssetReg = await getAssetRegistry(namespace + '.LiveAsset');         
        }

        await liveAssetReg.update(liveAsset);    

        // emitting Transfer event
        let factory = await getFactory();

        let transferEvent = factory.newEvent(namespace, 'AssetProcessed');
        transferEvent.liveAsset = liveAsset;
        transferEvent.transferGHG = liveAsset.aggregatedGHG;
        await emit(transferEvent);            
    }    

}


 async function ProduceMeat(processing) {  
    let factory = await getFactory();

    // creating cell phone
    const meatReg = await getAssetRegistry(namespace + '.Steak');   

    // getting next id
    let existingMeats = await meatReg.getAll();
    let numberOfMeats = 0;

    await existingMeats.forEach(function (meat) {
      numberOfMeats ++;
    });
    numberOfMeats ++;   

    const meat = await factory.newResource(namespace, 'Steak', numberOfMeats.toString());

    meat.assetStatus = "PROCESSED";
    meat.aggregatedGHG = processing.GHG;
    meat.atState = processing;
    meat.amount = 1;
    await meatReg.add(meat);       

    // emitting create event

    let createEvent = factory.newEvent(namespace, 'AssetProduced');
    createEvent.liveAsset = meat;
    createEvent.creationGHG = meat.aggregatedGHG;
    await emit(createEvent);    
}


/**
 *
 * @param {org.supplychain.food.model.Produce} param - model instance
 * @transaction
 */
async function ProduceFunctionCow(param) {  
    let production = param.atProduction;
    console.log("prod 1",production)
    let factory = await getFactory();

    // creating cell phone
    const cowReg = await getAssetRegistry(namespace + '.Cow');   

    // getting next id
    let existingCows = await cowReg.getAll();
    let numberOfCows = 0;

    await existingCows.forEach(function (cow) {
      numberOfCows ++;
    });
    numberOfCows ++;    

    const cow = await factory.newResource(namespace, 'Cow', numberOfCows.toString());
    cow.assetStatus = "LIVE";
    cow.aggregatedGHG = production.GHG;
    cow.atState = production;
    console.log("prod 2",cow.atState)
    cow.amount = 1;
    await cowReg.add(cow);       

    // emitting create event

    let createEvent = factory.newEvent(namespace, 'AssetProduced');
    createEvent.liveAsset = cow;
    createEvent.creationGHG = cow.aggregatedGHG;
    await emit(createEvent);    
}

/**
 *
 * @param {org.supplychain.food.model.Consume} param - model instance
 * @transaction
 */
async function ConsumeFunction(param) {  
    let assetToConsume = param.liveAsset;
    let restaurant = param.atRestaurant;
    let factory = await getFactory();

    assetToConsume.assetStatus = "CONSUMED";

    const restaurantReg = await getAssetRegistry(namespace + '.LiveAsset'); 
    await restaurantReg.update(restaurant);    

    // emitting Sold event

    let consumedEvent = factory.newEvent('org.supplychain.green.model', 'AssetConsumed');
    consumedEvent.liveAsset = assetToConsume;
    consumedEvent.endGHG = assetToConsume.aggregatedGHG;
    await emit(consumedEvent);      
}

预期行为是,消费事务运行将牛排的状态更改为已消费,但是会抛出此500错误代码,并带有问题标题中给出的错误。 感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

据我了解,Consume函数会更改牛排的状态。因此,我认为您的意思是更新资产。函数中的最后一行代码将更新参与者餐厅,而不是资产。

/**
 *
 * @param {org.supplychain.food.model.Consume} param - model instance
 * @transaction
 */
async function ConsumeFunction(param) {  
    let assetToConsume = param.liveAsset;
    let restaurant = param.atRestaurant;
    let factory = await getFactory();

    assetToConsume.assetStatus = "CONSUMED";

    const assetReg = await getAssetRegistry(namespace + '.LiveAsset'); 
    await assetReg .update(assetToConsume);    

    // emitting Sold event

    let consumedEvent = factory.newEvent('org.supplychain.green.model', 'AssetConsumed');
    consumedEvent.liveAsset = assetToConsume;
    consumedEvent.endGHG = assetToConsume.aggregatedGHG;
    await emit(consumedEvent);      
}

答案 1 :(得分:0)

=我们必须在下面一行获得的名称空间不是LiveAsset的牛排,并且发出事件也是如此,因此在代码中的消费功能中 change

const assetReg = await getAssetRegistry(namespace + '.steak'); 
    await assetReg .update(assetToConsume);    

    // emitting Sold event

    let consumedEvent = factory.newEvent(namespace + 'AssetConsumed');