如何在 Hyperledger Fabric 中更新资产之前检查条件

how to check condition before updating asset in Hyperledger Fabric

我正在尝试在更新资产之前使用 if 条件检查条件。但我不能这样做。我需要检查 属性 是否存在,并且用户的账户余额是否大于 属性.

的市场价格

我已经尝试在 knowledge.But 中使用所有类型的选项,我是新手,所以无法解决问题。如果我只尝试检查 属性 是否存在,那么它就可以正常工作。但是当我尝试检查余额情况时出现问题。

Model.cto

/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* This namespace helps in idetifying the entities for the network. */
namespace org.example.property

/* Asset Property identified by a striing PID
This is used to maintain the properties which are registered in the system.
*/

asset Property identified by PID {
o String PID
o String owner
o Integer mktprice
o String RegistrationDate
o String PropertyType
o String Location
o String Status default = "Registered"
o Boolean Public
o Boolean Private
o Boolean IntentOfSale

}

/* Asset PropertyListing identified by a striing PLID
This is used to maintain the properties which are listed for sale in the system.
*/
asset PropertyListing identified by PLID {

o String PLID
o String owner
o Integer mktprice
o String RegistrationDate
o String PropertyType
o String Location
o String Status default = "Intent Of Sale"
}

/* Participant Buyer identified by a striing Bname
This is used to maintain the buyers who are part of the system.
*/ 
participant Buyer identified by Bname {
o String Bname
o String Bemail
o Integer IdentityNo //Passport, SSN, Aadhar etc.
o String Bnkname
o String Bnkaddress
o Integer AccNo
o String IFSC
o Integer Balance
}

/* Participant Seller identified by a striing Sname
This is used to maintain the sellers who are part of the system.
*/ 
participant Seller identified by Sname {
o String Sname
o String Semail
o Integer IdentityNo
o String Bnkname
o String Bnkaddress
o Integer AccNo
o String IFSC
o Integer Balance
o String SaleDeedDocs
}

/* Participant Registrar identified by a striing Rname
This is used to maintain the registrar who are part of the system.
*/ 
participant Registrar identified by Rname {
o String Rname 
o String Remail
}

/* Transaction Created
This is used to add new properties in the system.
*/ 
transaction Created {
o String PID
--> Property cproperty
}

transaction Registered {
o String PID
--> PropertyListing rpropertylisting
--> Buyer rbuyer
}

transaction IntentForSale { 
--> Property iproperty
--> PropertyListing ipropertylisting
--> Seller iseller  
}

Script.js

/**
* Transaction Created to add the new property in the system
* @param {org.example.property.Registered} tx3
* @transaction
*/

async function Registered(tx3) {
   const propertynamespace = 'org.example.property';
   const factory = getFactory();
   var updateOwner;
   var buyerBalance = tx3.rbuyer.Balance;
   var marketPrice = tx3.rpropertylisting.mktprice;

  return getAssetRegistry(propertynamespace + '.Property')
      .then(function(assetRegistry){
       return assetRegistry.exists(tx3.PID);

     })
     return getParticipantRegistry(propertynamespace + '.Buyer')
        .then(function (participantRegistry) {
         // Get the specific driver from the driver participant registry.
        return participantRegistry.get(tx3.rbuyer.Bname);
     })

      .then(function(exists){


       if(exists && buyerBalance > marketPrice){

          return getAssetRegistry(propertynamespace + '.Property')                       
                    .then(function(assetRegistry2){                               
                    return assetRegistry2.get(tx3.PID);
          })
          .then(function(updateProperty){
                        updateOwner = updateProperty 
                        updateOwner.owner = tx3.rbuyer.Bname;
                        updateOwner.Status = "Registered";


                        return getAssetRegistry(propertynamespace + '.Property')


                })
                .then(function(assetRegistry3){
                        return assetRegistry3.update(updateOwner);
                })
       }

     else{
    console.log('Property doesnot exists')
  }

  })
}

它应该更新 属性 的所有者和状态。

看起来问题出在 exists 函数上。由于传递了错误的参数。

Script.js

async function Registered(tx3) {

  //Getting the namespace and factory 
  const factory = getFactory();
  const propertynamespace = 'org.example.property';

 //Checking if property exists
 return getAssetRegistry(propertynamespace + '.Property')
      .then(function(assetRegistry){
       return assetRegistry.exists(tx3.PID);
 })

  .then(function(exists){
  //Creating the condition if property exists
  if(exists && tx3.rbuyer.Balance > tx3.rpropertylisting.mktprice){

      //updating property
     const property = factory.newResource(propertynamespace, 'Property', tx3.PID);
     property.owner = tx3.rbuyer.Bname;
     property.mktprice = tx3.rpropertylisting.mktprice;
     property.RegistrationDate = tx3.rpropertylisting.RegistrationDate;
     property.PropertyType = tx3.rpropertylisting.PropertyType;
     property.Location = tx3.rpropertylisting.Location;
     property.Status = "Registered";
     //Setting the flags
     property.Public = true;
     property.Private = false;
     property.IntentOfSale = false;

     //Getting asset registry
     return getAssetRegistry(propertynamespace + '.Property')
     //Saving property
    .then(function(assetRegistry3){
        return assetRegistry3.update(property);
    })
     }
   //Creating condition if property dosent exist  
  else{
    console.log('Property does not exist');
  }
 })
}