.update和.update都无法正常运行

时间:2018-04-21 23:58:53

标签: hyperledger-fabric hyperledger hyperledger-composer

我在Composer上整合了一个应用程序,它与Playground上的示例Bond网络非常相似。我的部分申请要求在到期日删除过期合同。因此,代码必须获取所有合同,找到日期小于当前合同的合同,并将其5字段更改为valid。下面是我运行的一些代码:

false

现在,生成function SOD(sod) { // eslint-disable-line no-unused-vars return getAssetRegistry('org.acme.otc.LiveContract') .then(function (contractRegistry){ return contractRegistry.getAll(); }) .then(function (l){ allContracts=l; //console.log('Curr List: ',allContracts); var q = new Date(); var m = q.getMonth(); var d = q.getDay(); var y = q.getFullYear(); var date = new Date(y,m,d); var arr=[]; allContracts.forEach(function(contract) { //console.log('Contract Date: ',contract.contract.expiry); if (contract.contract.expiry<date) { //console.log('Checks Out'); //console.log('Destroying: ',contract); arr.push(contract); //console.log('Return: ',x); } }) return arr }).then( function(arr){ var factory = getFactory(); new_arr=[]; fLen = arr.length; for (i = 0; i < fLen; i++) { var old_contract=arr[i]; console.log('ISIN: ',old_contract.ISINcode); var new_contract = factory.newResource('org.acme.otc', 'LiveContract', old_contract.ISINcode); console.log('Entry: ',old_contract); new_contract.contract=old_contract.contract; new_contract.valid=false; new_arr.push(new_contract); } x=invalidate(new_arr); }); 的代码效果很好。我可以在Playground中查看,我得到new_arr的以下日志:

New Array

但是,如果我在new_arr上调用invalidate或者在new_arr内复制该代码,则不会对注册表产生所需的影响。我在SOD字段下没有false。当我尝试在调试器中读取x时,我得到了跛足的Promise响应:

Ugh

以下是我的.cto和.acl文件:

valid

//otc.cto

namespace org.acme.otc participant Trader identified by traderId { o String traderId o String name o Boolean marginThresh // margin meeting maintenance minimum } participant Administrator identified by adminId { o String adminId o Boolean tradingOpen } enum Product { o OPTION o FUTURE o FORWARD } concept Order { o String traderID o Product productType o String asset // commodity? underlying? - may later need typing o Boolean buy o Double notionalAmount o DateTime expiry // only needs to specify date } concept Contract { o String buyerID // buyer/seller vs maker/taker? o String sellerID o Product productType o String asset // commodity? underlying? - may later need typing o Double notionalAmount o Double value o DateTime expiry } asset OpenOrder identified by ISINcode { o String ISINcode o Order order o Boolean valid } asset LiveContract identified by ISINcode { o String ISINcode o Contract contract o Boolean valid } transaction PlaceOrder { o String ISINCode o Order order } transaction SOD { o String adminId } transaction Test {}

//permissions.acl

我对JavaScript缺乏经验,所以错误可能很少。谢谢你的帮助。

更新: 我在这里通过保罗的回应。我的新代码在

下面
  rule Issuer {
      description: "Allow full access to the issuer of a bond"
      participant(i): "org.acme.otc.Trader"
      operation: ALL
      resource(a): "org.acme.otc.OpenOrder"
      condition: (a.bond.issuer.memberId === i.memberId)
      action: ALLOW
  }

  rule Default {
      description: "Allow read access"
      participant: "org.acme.otc.*"
      operation: ALL
      resource: "org.acme.otc.*"
      action: ALLOW
  }

  rule SystemACL {
      description:  "System ACL to permit all access"
      participant: "org.hyperledger.composer.system.Participant"
      operation: ALL
      resource: "org.hyperledger.composer.system.**"
      action: ALLOW
  }

  rule NetworkAdminUser {
      description: "Grant business network administrators full access to user resources"
      participant: "org.hyperledger.composer.system.NetworkAdmin"
      operation: ALL
      resource: "**"
      action: ALLOW
  }

  rule NetworkAdminSystem {
      description: "Grant business network administrators full access to system resources"
      participant: "org.hyperledger.composer.system.NetworkAdmin"
      operation: ALL
      resource: "org.hyperledger.composer.system.**"
      action: ALLOW
  }

然而它不起作用。代码似乎永远不会到达最终的/** * Start of Day Update * @param {org.acme.otc.SOD} sod - the sod transaction * @transaction */ function SOD(sod) { // eslint-disable-line no-unused-vars //arr=pare(); var aR=null; return getAssetRegistry('org.acme.otc.LiveContract') .then(function (contractRegistry){ ar=contractRegistry; return ar.getAll(); }) .then(function (l){ allContracts=[]; allContracts=l; console.log('Curr List: ',allContracts); var q = new Date(); var m = q.getMonth(); var d = q.getDay(); var y = q.getFullYear(); var date = new Date(y,m,d); console.log('Current Date: ',date); arr=[]; allContracts.forEach(function(contract) { //console.log('Contract Date: ',contract.contract.expiry); if (contract.contract.expiry<date) { //console.log('Checks Out'); //console.log('Destroying: ',contract); arr.push(contract); //console.log('Return: ',x); } }) return arr }).then( function(arr){ var factory = getFactory(); new_arr=[]; fLen = arr.length; for (i = 0; i < fLen; i++) { var old_contract=arr[i]; console.log('ISIN: ',old_contract.ISINcode); var new_contract = factory.newResource('org.acme.otc', 'LiveContract', old_contract.ISINcode); console.log('Entry: ',old_contract); new_contract.contract=old_contract.contract; new_contract.valid=false; new_arr.push(new_contract); } console.log(new_arr); }); console.log("getting to update the records"); return aR.updateAll(new_arr); } 。以前的日志工作正常,其中的新合同是正确的。现在有什么想法吗?

1 个答案:

答案 0 :(得分:1)

没有看到updateAll代码位或知道函数invalidate(new_arr)的作用 - 所以我会发布类似的东西,但你得到了要点 - 它在Playground中有效。 updateupdateAll都适用于我。

我已粘贴下面的工作代码 - 只是从你的命名空间中给它一个事务/ TP装饰器等。

更新: (我原来的帖子我会离开这里 - 因为它有效) - 我还更新了你的第二个新代码块的工作版本 - 但是有更正,例如。你没有正确引用对象(比如ar而不是应该是aR所以它永远不会更新注册表等) - 你需要按照你的方式遵循JS保证链完成它 - 所以我在下面的'ANSWER 2'中为你添加了代码。)。另外,最好将new_arr声明为具有全局函数范围,并且我已经在我的代码中完成了。

/**
 * New script file
 */

/**
* A transaction processor function description
* @param { org.acme.otc.SOD } sod A human description of the parameter
* @transaction
*/

function SOD(sod) {  // eslint-disable-line no-unused-vars

  var aR = null;

  return getAssetRegistry('org.acme.otc.LiveContract')
  .then(function (contractRegistry){
     // return contractRegistry.getAll();
      aR = contractRegistry;
      return aR.getAll();
  })
  .then(function (l) {

      allContracts=[];
      allContracts=l;

      console.log('allContracts List is: ' + allContracts );

      var q = new Date();
      var m = q.getMonth();
      var d = q.getDay();
      var y = q.getFullYear();
      var datevar = new Date(y,m,d);
      arr=[];

      allContracts.forEach(function(contract) {
      console.log('Contract Date: ' + contract.contract.expiry);

      if (contract.contract.expiry < datevar ) {
      console.log('Checks Out - this contract needs fixing');
      console.log('Destroying: ' + contract);

      arr.push(contract);
      //console.log('Return: ',x);
      } 
    }) 
  return arr
  })
  .then( function(arr) {
      var factory = getFactory();
      new_arr=[];

      fLen = arr.length;
      for (i = 0; i < fLen; i++) {
          var old_contract=arr[i];
          console.log('ISIN: ' + old_contract.ISINcode);
          var new_contract = factory.newResource('org.acme.otc', 'LiveContract', old_contract.ISINcode);
          console.log('Entry: ' + old_contract);
          new_contract.contract=old_contract.contract;
          new_contract.valid=false;
          new_arr.push(new_contract);


       }  // x=invalidate(new_arr);


   });

   console.log("getting to update the records");
   return  aR.updateAll(new_arr);
    //.then(function () {

       // return getAssetRegistry('org.acme.otc.LiveContract')
       //.then(function (contractRegistry){

       //  return contractRegistry.updateAll(new_arr);

    // });
   //});
}

答案2:使用第二个代码块更新代码

/**
 * Start of Day Update
 * @param {org.acme.otc.SOD} sod - the sod transaction
 * @transaction
 */
function SOD(sod) {  // eslint-disable-line no-unused-vars
  //arr=pare();

  var aR=null;
  new_arr=[]; 

  return getAssetRegistry('org.acme.otc.LiveContract')
    .then(function (contractRegistry){
        aR=contractRegistry;
        return aR.getAll();

  })
    .then(function (l){
        allContracts=[];
        allContracts=l;
        console.log('Curr List: ',allContracts);
        var q = new Date();
        var m = q.getMonth();
        var d = q.getDay();
        var y = q.getFullYear();
        var date = new Date(y,m,d);
        console.log('Current Date: ',date);
        arr=[];
        allContracts.forEach(function(contract) {
          //console.log('Contract Date: ',contract.contract.expiry);
          if (contract.contract.expiry < date) {
            //console.log('Checks Out');
            //console.log('Destroying: ',contract);

            arr.push(contract);
          //console.log('Return: ',x);
          } 
        }) 
    return arr
    }).then( function(arr){
        var factory = getFactory();

        //new_arr=[];

        fLen = arr.length;
        for (i = 0; i < fLen; i++) {
            var old_contract=arr[i];
            console.log('ISIN: ',old_contract.ISINcode);
            var new_contract = factory.newResource('org.acme.otc', 'LiveContract', old_contract.ISINcode);
            console.log('Entry: ',old_contract);
            new_contract.contract=old_contract.contract;
            new_contract.valid=false;
            new_arr.push(new_contract);


        } 
       console.log("New Array is now " + new_arr);
  })
   .then(function () {

      console.log("getting to update the records");
      return  aR.updateAll(new_arr);
   });
}