为什么以下添加的资产不会在超级边界注册表中保留? [英] Why are the following added assets not persisted in the hyperledger registry?

查看:89
本文介绍了为什么以下添加的资产不会在超级边界注册表中保留?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Hyperledger编写器环境中有以下应用程序代码(我的问题仅适用于 RequestT 事务,因为我还没有编写 Respond 交易代码):



型号代码(.cto)

  / * 
*为chama事务定义数据模型
* /

namespace org.acme.account.chama

enum responseType {
o YES
o NO
}

enum requestType {
o DEPOSIT
o WITHDRAW
}

enum状态{
o PENDING_AUTHORIZATION
o AUTHORIZED
o DECLINED
}

参与者用id标识的用户{
o String id
o Double balance
o String firstName
o String lastName
}

asset由id标识的请求{
o String id
o requestType类型
o州州
o双倍金额
o回复[]回复
- >用户用户
}

资产由id标识的帐户{
o String id
o requestType transaction
o Double amount
- >请求请求
- >用户用户
}

资产由id标识的响应{
o String id
o responseType reply
- >请求请求
- >用户用户
}

交易回复{
o responseType回复
- >请求请求
- >用户用户
}

交易RequestT {
o请求请求
}

JavaScript代码(.js)

  var getAccountBalance = function( ){
var accountQuery = buildQuery(SELECT org.acme.account.chama.Account \
WHERE(transaction == _ $ type));
返回查询(accountQuery,{type:DEPOSIT})
//总计存款金额
.then(函数(存款){
var depositedAmount = 0;
deposits.forEach(函数(存款){
depositAmount + = deposit.amount;
});
return depositedAmount;
})
.then(function(depositedAmount){
返回查询(accountQuery,{type:WITHDRAW})
//总结提取的金额
。然后(函数(提款)) {
var withdrawnAmount = 0;
withdrawals.forEach(function(withdraw){
withdrawnAmount + = withdraw.amount;
});
return withdrawnAmount;
})
//找到账户余额
。然后(函数(withdrawnAmount){
return depositedAmount - withdrawnAmount;
});
});
};

/ **
* @param {org.acme.account.chama.RequestT} requestT
* @transaction
* /
功能请求( requestT){
var request = requestT.request;
var user = request.user;
//获取账户余额
getAccountBalance()
.then(函数(余额){
//如果用户资金不足则拒绝存款
if((用户) .balance< request.amount)&&(request.type ===DEPOSIT)){
抛出新错误(错误:用户资金不足!);
}
//如果账户资金不足则拒绝提款
if((余额< request.amount)&&(request.type ===WITHDRAW)){
抛出新的错误(错误:帐户资金不足!);
}
console.log(检查有效期);
返还余额;
})
//获取用户的注册表
.then(function(){
console.log(获取用户注册);
返回getParticipantRegistry(org.acme.account.chama.User) );
})
//获取所有用户
.then(function(userRegistry){
console.log(获取所有用户);
返回userRegis try.getAll();
})
.then(function(users){
console.log(USERS:,users);
//如果请求者是唯一用户,则授权请求
if(users.length === 1){
request.state =AUTHORIZED;
}
console.log(获取请求'注册);
//获取请求'注册表
返回getAssetRegistry(org.acme.account.chama.Request)
//保存请求
.then(function(requestRegistry){
return requestRegistry.add(request)
//获取所有请求
.then(function(){return requestRegistry.getAll();})
//获取总数发出请求的数量
.then(function(requests){console.log(REQUESTS,requests); return requests.length;});
});
})
.then(函数(长度){
console.log(LENGTH:,长度);
//如果请求已被授权
if(reque) st.state ===AUTHORIZED){
console.log(获取账户注册);
返回getAssetRegistry(org.acme.account.chama.Account)
.then(function(accountRegistry){
//创建新的存款交易
var factory = getFactory( );
var depositTransaction = factory.newResource(
org.acme.account.chama,Account,user.id + length);
depositTransaction.transaction = request.type;
depositTransaction.amount = request.amount;
depositTransaction.user = request.user;
depositTransaction.request = request;
console.log(DEPO T:,depositTransaction) ;
console.log(添加存款请求注册);
//保存存款交易
返回accountRegistry.add(depositTransaction);
})
/ /更新用户余额
.then(function(){
console.log(获取用户注册);
返回n getParticipantRegistry(org.acme.account.chama.User)
.then(function(userRegistry){
if(request.type ===WITHDRAW){
user。余额+ = request.amount;
} else if(request.type ===DEPOSIT){
user.balance - = request.amount;
}
console.log(在注册表中更新用户);
返回userRegistry.update(user);
});
});
}
返回true;
})
.catch(函数(错误){
alert(错误);
});
}



但是JavaScript代码中添加的资产不会持久存储到注册表中。任何想法为什么?

解决方案

你的承诺链比它需要的更复杂,但没有任何东西可行的原因是你忘记了getAccountBalance之前的return语句,所以你只需要

 函数请求(requestT){
var request = requestT.request;
var user = request.user;
//获取账户余额
返回getAccountBalance()


I have the following application code in my Hyperledger composer environment (My question only pertains to the RequestT transaction as I am yet to write the Respond transaction code):

Model Code (.cto)

/*
* Defines a data model for chama transaction
*/

namespace org.acme.account.chama

enum responseType {
  o YES
  o NO
}

enum requestType{
  o DEPOSIT
  o WITHDRAW
}

enum State {
  o PENDING_AUTHORIZATION
  o AUTHORIZED
  o DECLINED
}

participant User identified by id {
  o String id
  o Double balance
  o String firstName
  o String lastName
}

asset Request identified by id {
  o String id
  o requestType type
  o State state
  o Double amount
  o Response[] responses
  --> User user
}

asset Account identified by id {
  o String id
  o requestType transaction
  o Double amount
  --> Request request
  --> User user
}

asset Response identified by id {
  o String id
  o responseType reply
  --> Request request
  --> User user
}

transaction Respond {
  o responseType reply
  --> Request request
  --> User user
}

transaction RequestT{
  o Request request
}

JavaScript code (.js)

var getAccountBalance = function(){
  var accountQuery = buildQuery("SELECT org.acme.account.chama.Account \
    WHERE (transaction == _$type)");
  return query(accountQuery, { type: "DEPOSIT" })
    // sum up the amounts of the deposits made
    .then(function(deposits){
      var depositedAmount = 0;
      deposits.forEach(function(deposit){
        depositedAmount += deposit.amount;
      });
      return depositedAmount;
    })
    .then(function(depositedAmount){
      return query(accountQuery, { type: "WITHDRAW" })
        // sum up the amounts of the withdrawals made
        .then(function(withdrawals){
          var withdrawnAmount = 0;
          withdrawals.forEach(function(withdrawal){
            withdrawnAmount += withdrawal.amount;
          });
          return withdrawnAmount;
        })
        // find the account's balance
        .then(function(withdrawnAmount){
          return depositedAmount - withdrawnAmount;
        });
    });
};

/**
* @param  {org.acme.account.chama.RequestT} requestT
* @transaction
*/
function request(requestT){
  var request = requestT.request;
  var user = request.user;
  // get the account's balance
  getAccountBalance()
    .then(function(balance){
      // reject deposit if user has insufficient funds
      if ((user.balance < request.amount) && (request.type === "DEPOSIT")){
        throw new Error("ERROR: User has insufficient funds!");
      }
      // reject withdrawal if account has insufficient funds
      if ((balance < request.amount) && (request.type === "WITHDRAW")){
        throw new Error("ERROR: Account has insufficient funds!");
      }
      console.log("CHECKING VALIDITY");
      return balance;
    })
    // get the users' registry
    .then(function(){
      console.log("GETTING USERS' REGISTRY");
      return getParticipantRegistry("org.acme.account.chama.User");
    })
    // get all the users
    .then(function(userRegistry){
      console.log("GETTING ALL USERS");
      return userRegistry.getAll();
    })
    .then(function(users){
      console.log("USERS:", users);
      // authorize the request if the requester is the sole user
      if (users.length === 1){
        request.state = "AUTHORIZED";
      }
      console.log("GETTING REQUESTS' REGISTRY");
      // get the requests' registry
      return getAssetRegistry("org.acme.account.chama.Request")
        // save the request
        .then(function(requestRegistry){
          return requestRegistry.add(request)
            // get all of the requests
            .then(function(){ return requestRegistry.getAll(); })
            // obtain the total number of requests made
            .then(function(requests){ console.log("REQUESTS", requests);return requests.length; });
        });
    })
    .then(function(length){
      console.log("LENGTH:", length);
      // if the request has been authorized
      if (request.state === "AUTHORIZED"){
        console.log("GETTING ACCOUNT REGISTRY");
        return getAssetRegistry("org.acme.account.chama.Account")
          .then(function(accountRegistry){
            // create new deposit transaction
            var factory = getFactory();
            var depositTransaction = factory.newResource(
              "org.acme.account.chama", "Account", user.id + length);
            depositTransaction.transaction = request.type;
            depositTransaction.amount = request.amount;
            depositTransaction.user =  request.user;
            depositTransaction.request = request;
            console.log("DEPO T:", depositTransaction);
            console.log("ADDING DEPOSIT REQUEST TO REGISTRY");
            // save deposit transaction
            return accountRegistry.add(depositTransaction);
          })
          // update user's balance
          .then(function(){
            console.log("GETTING USER REGISTRY");
            return getParticipantRegistry("org.acme.account.chama.User")
              .then(function(userRegistry){
                if (request.type === "WITHDRAW"){
                  user.balance += request.amount;
                } else if (request.type === "DEPOSIT"){
                  user.balance -= request.amount;
                }
                console.log("UPDATING USER IN REGISTRY");
                return userRegistry.update(user);
              });
          });
      }
      return true;
    })
    .catch(function(error){
      alert(error);
    });
}

On the Hyperledger Composer online playground, I create a new user asset with the following data:

{
  "$class": "org.acme.account.chama.User",
  "id": "id:1001",
  "balance": 1000,
  "firstName": "ONE",
  "lastName": "ONE"
}

Now when I submit a RequestT transaction with the following data:

{
  "$class": "org.acme.account.chama.RequestT",
  "request": {
    "$class": "org.acme.account.chama.Request",
    "id": "id:8729",
    "type": "DEPOSIT",
    "state": "PENDING_AUTHORIZATION",
    "amount": 500,
    "responses": [],
    "user": "resource:org.acme.account.chama.User#id:1001"
  }
}

All promises seem to be running and resolve without an error as depicted in the following image:

But the assets added in the JavaScript code do not get persisted to the registry. Any ideas why?

解决方案

Your promise chain is more complex than it needs to be, but the reason that nothing appears to work is you have forgotten a return statement before the getAccountBalance, so you just need

function request(requestT){
var request = requestT.request;
var user = request.user;
// get the account's balance
return getAccountBalance()

 

这篇关于为什么以下添加的资产不会在超级边界注册表中保留?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆