我正在构建一个简单的业务网络,用户可以在其中创建一个组合并出售/购买该组合。我在当地安装了操场,正在研究它。我成功地能够创建用户和资产(货物),但销售交易产量预期资源或概念错误。
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.)
答案 0 :(得分:1)
您的交易代码存在问题:
/**
* 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);
});
}
sell.goods
资源作为关系。