HyperLeDger-Composer应用程序中的简单交易产生了预期的资源或概念误差



我正在建立一个简单的商业网络,用户可以创建驴子并销售/购买驴子。我在本地安装了操场,并正在努力。我成功地创建了用户和资产(商品(,但出售交易产量预期资源或概念误差。

model.cto文件

/**
 * Simple buy and sell goods business network definition.
 */
namespace org.example.biznet
enum GoodsState {
  o FOR_SALE
  o SOLD
}
asset Goods identified by goodsId {
  o String goodsId
  o GoodsState state
  o String description
  --> User owner
}
participant User identified by email {
  o String email
  o String firstName
  o String lastName
  o Double balance
}
transaction Buy {
  --> User user
  --> Goods goods
  o Double productPrice
}
transaction Sell { 
  --> User user 
  --> Goods goods
  o Double sellingPrice 
}

logic.js文件

/**
 * Selling the product owned by the user
 * @param {org.example.biznet.Sell} selling - the selling transaction
 * @transaction
 */
function sell(sell) {
    if(sell.user.email !== sell.goods.owner.email){
        throw new Error('The user does not own the assest');
   }
   state = sell.goods.state;
   state = 'SOLD';
   console.log('#### he can sell the goods');
   return getAssetRegistry('org.example.biznet.Goods')
     .then(function(goodsRegistry) {
     // save the updated goods status
     return goodsRegistry.update(state);
   });
 }

permissions.acl文件

/**
 * Access control list for simple buy and sell network.
 */
rule Member {
    description: "Allow all participants read access to all resources"
    participant: "org.example.biznet.User"
    operation: READ
    resource: "org.example.biznet.*"
    action: ALLOW
}
rule OwnerHasFullAccessToTheirAssets {
    description: "Allow all participants full access to their assets"
    participant(p): "org.example.biznet.User"
    operation: ALL
    resource(r): "org.example.biznet.Goods"
    condition: (r.owner.getIdentifier() === p.getIdentifier())
    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
}

因此,正如我之前提到的那样,我尝试以这种方式创建几个用户和商品。

{
  "$class": "org.example.biznet.User",
  "email": "memberA@gmail.com",
  "firstName": "Jenny",
  "lastName": "Jones",
  "balance": 3000
}
{
  "$class": "org.example.biznet.User",
  "email": "memberB@gmail.com",
  "firstName": "Amy",
  "lastName": "Williams",
  "balance": 5000
}
{
  "$class": "org.example.biznet.Goods",
  "goodsId": "goodsId:1",
  "state": "FOR_SALE",
  "description": "car",
  "owner": "resource:org.example.biznet.User#memberA@gmail.com"
}

它起作用。

但是,当我尝试出售资产时,以下方式,我会得到这个错误,我无法弄清楚为什么

{
  "$class": "org.example.biznet.Sell",
  "sellingPrice": 5000,
  "user": "resource:org.example.biznet.User#memberA@gmail.com",
  "goods": "resource:org.example.biznet.Goods#goodsId:1"
}

Error: Error trying invoke business network. Error: No valid responses from any peers. Response from attempted peer comms was an error: Error: chaincode error (status: 500, message: Error: Expected a Resource or Concept.)

您在交易代码中有问题:

 /**
 * Selling the product owned by the user
 * @param {org.example.biznet.Sell} sell - the selling transaction
 * @transaction
 */
function sell(sell) {
    if(sell.user.email !== sell.goods.owner.email){
        throw new Error('The user does not own the assest');
   }
   state = 'SOLD';
   sell.goods.state = state;
   console.log('#### he can sell the goods');
   return getAssetRegistry('org.example.biznet.Goods')
     .then(function(goodsRegistry) {
     // save the updated goods status to the goods asset instance
      return goodsRegistry.update(sell.goods);
   });
 }
  • 您的@param定义不正确(应该是'sell'(
  • 您需要将状态保存到传递给TXN的sell.goods资源作为关系。

最新更新