[英]Error after fabcar chaincode modify-Error: No valid responses from any peers
我正在學習教程並嘗試更改fabcar的鏈碼。 但是我收到了這個錯誤。
$ cd fabcar/javascript
$ node enrollAdmin.js
Wallet path: /home/ricky/fabric/fabric-samples/fabcar/javascript/wallet
Successfully enrolled admin user "admin" and imported it into the wallet
$ node registerUser.js
Wallet path: /home/ricky/fabric/fabric-samples/fabcar/javascript/wallet
Successfully registered and enrolled admin user "appUser" and imported it into the wallet
$ node invoke.js
Wallet path: /home/ricky/fabric/fabric-samples/fabcar/javascript/wallet
2022-09-24T19:23:05.281Z - error: [Transaction]: Error: No valid responses from any peers. Errors:
peer=peer0.org2.example.com:9051, status=500, message=error in simulation: transaction returned with failure: ReferenceError: from is not defined
peer=peer0.org1.example.com:7051, status=500, message=error in simulation: transaction returned with failure: ReferenceError: from is not defined
at newEndorsementError (/home/ricky/fabric/fabric-samples/fabcar/javascript/node_modules/fabric-network/lib/transaction.js:74:12)
at getResponsePayload (/home/ricky/fabric/fabric-samples/fabcar/javascript/node_modules/fabric-network/lib/transaction.js:41:23)
at Transaction.submit (/home/ricky/fabric/fabric-samples/fabcar/javascript/node_modules/fabric-network/lib/transaction.js:255:28)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async main (/home/ricky/fabric/fabric-samples/fabcar/javascript/invoke.js:83:9)
Failed to submit transaction: Error: No valid responses from any peers. Errors:
peer=peer0.org2.example.com:9051, status=500, message=error in simulation: transaction returned with failure: ReferenceError: from is not defined
peer=peer0.org1.example.com:7051, status=500, message=error in simulation: transaction returned with failure: ReferenceError: from is not defined
這是我的文件: fabcar.js
/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const { Contract } = require('fabric-contract-api');
class FabCar extends Contract {
async initLedger(ctx){
await ctx.stub.putState("test","hello world")
return "success"
}
async writeJsonData(ctx,key,value){
let jsonvalue = JSON.parse(value)
console.log("**********",jsonvalue);
value=JSON.parse(value)
await ctx.stub.putState(key,Buffer,from(JSON.stringify(jsonvalue)));
return Buffer.from(JSON.stringify(jsonvalue));
}
async readData(ctx,key){
let response = await ctx.stub.getState(key);
response=response.toString('utf-8')
return JSON.stringify(response);
}
async queryPatientsByDiagnosis(ctx,diagnosis){
let queryString = {}
queryString.selector = {"diagnosis":diagnosis}
let iterator = await ctx.stub.getQueryresult(JSON.stringify(queryString))
let result = await this.getIteratorData(iterator)
return JSON.stringify(result)
}
async getIteratorData(iterator){
let resultArray =[]
while(true){
let res = await iterator.next()
let resJson = {}
if(res.value && res.value.value.toString()){
resJson.key = res.value.key;
resJson.value=JSON.parse(res.value.value.toString('utf-8'))
resultArray.push(resJson)
}
if(res.done){
await iterator.close();
return resultArray
}
}
}
}
module.exports = FabCar;
注冊管理員.js
/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const FabricCAServices = require('fabric-ca-client');
const { Wallets } = require('fabric-network');
const fs = require('fs');
const path = require('path');
async function main() {
try {
// load the network configuration
const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json');
const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));
// Create a new CA client for interacting with the CA.
const caInfo = ccp.certificateAuthorities['ca.org1.example.com'];
const caTLSCACerts = caInfo.tlsCACerts.pem;
const ca = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName);
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the admin user.
const identity = await wallet.get('admin');
if (identity) {
console.log('An identity for the admin user "admin" already exists in the wallet');
return;
}
// Enroll the admin user, and import the new identity into the wallet.
const enrollment = await ca.enroll({ enrollmentID: 'admin', enrollmentSecret: 'adminpw' });
const x509Identity = {
credentials: {
certificate: enrollment.certificate,
privateKey: enrollment.key.toBytes(),
},
mspId: 'Org1MSP',
type: 'X.509',
};
await wallet.put('admin', x509Identity);
console.log('Successfully enrolled admin user "admin" and imported it into the wallet');
} catch (error) {
console.error(`Failed to enroll admin user "admin": ${error}`);
process.exit(1);
}
}
main();
調用.js
/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const { Gateway, Wallets } = require('fabric-network');
const fs = require('fs');
const { relative } = require('path');
const path = require('path');
const { stringify } = require('querystring');
async function main() {
try {
// load the network configuration
const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json');
let ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the user.
const identity = await wallet.get('appUser');
if (!identity) {
console.log('An identity for the user "appUser" does not exist in the wallet');
console.log('Run the registerUser.js application before retrying');
return;
}
// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } });
// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork('mychannel');
// Get the contract from the network.
const contract = network.getContract('fabcar');
let key1 = "p1"
var patient1 = {
"name":"kumar",
"diagnosis" : "cancer",
"hospital": "apollo"
}
let key2 = "p2"
var patient2 = {
"name":"Rajesh",
"diagnosis" :"diabetes",
"hospital":"CMS"
}
let key3 = "p3"
var patient3= {
"name":"Rahul",
"diagnosis" :"diabetes",
"hospital":"CMS"
}
let key4 ="p4"
var patient4 = {
"name":"Madhu",
"diagnosis" :"dialysis",
"hospital":"CMS"
}
let key5 = "p5"
var patient5 = {
"name":"Ravi",
"diagnosis" :"angiogram",
"hospital":"CMS"
}
// Submit the specified transaction.
// createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom')
// changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR12', 'Dave')
await contract.submitTransaction('writeJsonData',key1,JSON.stringify(patient1));
await contract.submitTransaction('writeJsonData',key2,JSON.stringify(patient2));
await contract.submitTransaction('writeJsonData',key3,JSON.stringify(patient3));
await contract.submitTransaction('writeJsonData',key4,JSON.stringify(patient4));
await contract.submitTransaction('writeJsonData',key5,JSON.stringify(patient5));
console.log('Transaction has been submitted');
// Disconnect from the gateway.
await gateway.disconnect();
return;
} catch (error) {
console.error(`Failed to submit transaction: ${error}`);
process.exit(1);
}
}
main();
查詢.js
/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const { Gateway, Wallets } = require('fabric-network');
const path = require('path');
const fs = require('fs');
async function main() {
try {
// load the network configuration
const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json');
const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the user.
const identity = await wallet.get('appUser');
if (!identity) {
console.log('An identity for the user "appUser" does not exist in the wallet');
console.log('Run the registerUser.js application before retrying');
return;
}
// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } });
// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork('mychannel');
// Get the contract from the network.
const contract = network.getContract('fabcar');
// Evaluate the specified transaction.
// queryCar transaction - requires 1 argument, ex: ('queryCar', 'CAR4')
// queryAllCars transaction - requires no arguments, ex: ('queryAllCars')
let result = await contract.evaluateTransaction('queryPatientsByDiagnosis','diabetes');
//console.log(`Transaction has been evaluated, result is: ${result.toString()}`);
console.log(result)
result = result.toString('utf-8')
console.log(result)
// Disconnect from the gateway.
await gateway.disconnect();
} catch (error) {
console.error(`Failed to evaluate transaction: ${error}`);
process.exit(1);
}
}
main();
這是我的docker ps -a
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0907aa1fcae dev-peer0.org2.example.com-fabcar_1-5186256e9e2b0a552bc2083b79447653343b12fdd8a23edcced41b2c38a4d7f6-e3a5770e7e3aef0a6a8d7ea9e15ff6e514d81a1c08bab41f3ba6e9f6520332b4 "docker-entrypoint.s…" 10 hours ago Up 10 hours dev-peer0.org2.example.com-fabcar_1-5186256e9e2b0a552bc2083b79447653343b12fdd8a23edcced41b2c38a4d7f6
18b6da067756 dev-peer0.org1.example.com-fabcar_1-5186256e9e2b0a552bc2083b79447653343b12fdd8a23edcced41b2c38a4d7f6-4015ed4ba9ddd130ffb0eed9589fe89bc6f80d2e734a60a9a86bcd8c47341a1e "docker-entrypoint.s…" 10 hours ago Up 10 hours dev-peer0.org1.example.com-fabcar_1-5186256e9e2b0a552bc2083b79447653343b12fdd8a23edcced41b2c38a4d7f6
6affdc017949 hyperledger/fabric-tools:latest "/bin/bash" 10 hours ago Up 10 hours cli
a827cd7c8ac0 hyperledger/fabric-peer:latest "peer node start" 10 hours ago Up 10 hours 7051/tcp, 0.0.0.0:9051->9051/tcp, :::9051->9051/tcp peer0.org2.example.com
672f5857730b hyperledger/fabric-peer:latest "peer node start" 10 hours ago Up 10 hours 0.0.0.0:7051->7051/tcp, :::7051->7051/tcp peer0.org1.example.com
692da8ec1a9f hyperledger/fabric-orderer:latest "orderer" 10 hours ago Up 10 hours 0.0.0.0:7050->7050/tcp, :::7050->7050/tcp orderer.example.com
f035a0064c8c couchdb:3.1.1 "tini -- /docker-ent…" 10 hours ago Up 10 hours 4369/tcp, 9100/tcp, 0.0.0.0:7984->5984/tcp, :::7984->5984/tcp couchdb1
b869cb3e0ecd couchdb:3.1.1 "tini -- /docker-ent…" 10 hours ago Up 10 hours 4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp, :::5984->5984/tcp couchdb0
c55aa294278e hyperledger/fabric-ca:latest "sh -c 'fabric-ca-se…" 10 hours ago Up 10 hours 7054/tcp, 0.0.0.0:8054->8054/tcp, :::8054->8054/tcp ca_org2
18889ec68ece hyperledger/fabric-ca:latest "sh -c 'fabric-ca-se…" 10 hours ago Up 10 hours 7054/tcp, 0.0.0.0:9054->9054/tcp, :::9054->9054/tcp ca_orderer
12e416920956 hyperledger/fabric-ca:latest "sh -c 'fabric-ca-se…" 10 hours ago Up 10 hours 0.0.0.0:7054->7054/tcp, :::7054->7054/tcp ca_org1
8a055dbb0f25 hyperledger/fabric-nodeenv:2.2 "docker-entrypoint.s…" 14 hours ago Exited (0) 14 hours ago sharp_jepsen
1ccd7c827ee8 ece149884124 "peer node start" 45 hours ago Exited (1) 45 hours ago peer0.rubbershipperorg3.supplychain.com
75a96497a154 ece149884124 "peer node start" 45 hours ago Exited (1) 45 hours ago peer1.goodscustomorg4.supplychain.com
393023a965dd ece149884124 "peer node start" 45 hours ago Exited (1) 45 hours ago peer1.rubbershipperorg3.supplychain.com
04d2bdb53161 ece149884124 "peer node start" 45 hours ago Exited (1) 45 hours ago peer1.indonesianfarmorg1.supplychain.com
b850d79cd7f4 ece149884124 "peer node start" 45 hours ago Exited (1) 45 hours ago peer0.goodscustomorg4.supplychain.com
ae4b4f0947f9 ece149884124 "peer node start" 45 hours ago Exited (1) 45 hours ago peer1.usclientorg2.supplychain.com
a4674beea134 78a16ddd2cf4 "orderer" 45 hours ago Exited (2) 45 hours ago orderer2.supplychain.com
03af77cd3fd3 couchdb:3.1.1 "tini -- /docker-ent…" 45 hours ago Exited (255) 16 hours ago 4369/tcp, 9100/tcp, 0.0.0.0:6984->5984/tcp, :::6984->5984/tcp couchdb2
eb8b60ce5908 couchdb:3.1.1 "tini -- /docker-ent…" 45 hours ago Exited (255) 16 hours ago 4369/tcp, 9100/tcp, 0.0.0.0:11984->5984/tcp, :::11984->5984/tcp couchdb7
35c5c6a6e198 78a16ddd2cf4 "orderer" 45 hours ago Exited (2) 45 hours ago orderer3.supplychain.com
b02c0006e93c 78a16ddd2cf4 "orderer" 45 hours ago Created orderer.supplychain.com
860178e6cd6b couchdb:3.1.1 "tini -- /docker-ent…" 45 hours ago Exited (255) 16 hours ago 4369/tcp, 9100/tcp, 0.0.0.0:8984->5984/tcp, :::8984->5984/tcp couchdb4
4b4515185dde couchdb:3.1.1 "tini -- /docker-ent…" 45 hours ago Created couchdb3
e104998fc7a9 couchdb:3.1.1 "tini -- /docker-ent…" 45 hours ago Exited (255) 16 hours ago 4369/tcp, 9100/tcp, 0.0.0.0:10984->5984/tcp, :::10984->5984/tcp couchdb6
ee2b80732cc4 couchdb:3.1.1 "tini -- /docker-ent…" 45 hours ago Exited (255) 16 hours ago 4369/tcp, 9100/tcp, 0.0.0.0:12984->5984/tcp, :::12984->5984/tcp couchdb8
04955859f5a6 couchdb:3.1.1 "tini -- /docker-ent…" 45 hours ago Exited (255) 16 hours ago 4369/tcp, 9100/tcp, 0.0.0.0:9984->5984/tcp, :::9984->5984/tcp couchdb5
025548def0c4 gliderlabs/logspout "/bin/logspout" 2 weeks ago Exited (255) 16 hours ago 127.0.0.1:8000->80/tcp logspout
91bc51f80b88 hyperledger/explorer-db:latest "docker-entrypoint.s…" 3 weeks ago Exited (255) 3 weeks ago 5432/tcp explorerdb.mynetwork.com
docker 日志容器名稱
docker logs dev-peer0.org2.example.com-fabcar_1-5186256e9e2b0a552bc2083b79447653343b12fdd8a23edcced41b2c38a4d7f6
+ CHAINCODE_DIR=/usr/local/src
+ cd /usr/local/src
+ npm start -- --peer.address peer0.org2.example.com:9052
> fabcar@1.0.0 start /usr/local/src
> fabric-chaincode-node start "--peer.address" "peer0.org2.example.com:9052"
2022-09-24T19:21:01.940Z info [c-api:contracts-spi/bootstrap.js] No metadata file supplied in contract, introspection will generate all the data
2022-09-24T19:21:02.352Z info [c-api:lib/chaincode.js] Registering with peer peer0.org2.example.com:9052 as chaincode "fabcar_1:5186256e9e2b0a552bc2083b79447653343b12fdd8a23edcced41b2c38a4d7f6"
2022-09-24T19:21:02.502Z info [c-api:lib/handler.js] Successfully registered with peer node. State transferred to "established"
2022-09-24T19:21:02.504Z info [c-api:lib/handler.js] Successfully established communication with peer node. State transferred to "ready"
2022-09-24T19:21:02.564Z info [c-api:lib/handler.js] [mychannel-1f33601d] Calling chaincode Init() succeeded. Sending COMPLETED message back to peer
********** { name: 'kumar', diagnosis: 'cancer', hospital: 'apollo' }
2022-09-24T19:23:05.239Z error [c-api:contracts-spi/chaincodefromcontract.js] [mychannel-84269974] ReferenceError: from is not defined
2022-09-24T19:23:05.243Z error [c-api:lib/handler.js] [mychannel-84269974] Calling chaincode Invoke() returned error response [ReferenceError: from is not defined
at FabCar.writeJsonData (/usr/local/src/lib/fabcar.js:24:33)
at ChaincodeFromContract.invokeFunctionality (/usr/local/src/node_modules/fabric-shim/lib/contract-spi/chaincodefromcontract.js:378:58)
at processTicksAndRejections (internal/process/task_queues.js:97:5)]. Sending ERROR message back to peer
我嘗試了幾種解決方案,包括解決方案鏈接,但它仍然給我相同的結果。我不知道還有什么地方可以改變來解決以下問題。 謝謝
function writeJsonData() 的第 4 行中的鏈代碼有錯誤。
等待 ctx.stub.putState(key,Buffer,from(JSON.stringify(jsonvalue)));
它應該是Buffer.from(JSON.stringify(jsonvalue))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.