0

我在Hyperledger作曲家环境下的应用程序代码(因为我还没有写Respond交易代码我的问题只适用于RequestT交易):为什么以下添加的资产没有保留在超级注册表中?

型号代码(.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代码(的.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); 
    }); 
} 

Hyperledger Composer online playground,我创建具有以下数据的新用户资产:

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

现在,当我提出一个RequestT交易数据如下:

{ 
    "$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" 
    } 
} 

所有承诺似乎运行,解决没有一个错误如图所示如下图:

code output on the console

但在JavaScript中加入资产颂歌不会持续到注册表。任何想法为什么?

回答

1

你的诺言链相当复杂,它需要的,但没有什么似乎工作是你已经忘记了之前getAccountBalance return语句的原因,所以你只需要

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

相关问题