无法使用Hyperledger Fabric node-sdk初始化通道

时间:2019-09-14 08:09:44

标签: node.js hyperledger-fabric hyperledger

我正在尝试将UI连接到初始Fabcar教程,但是在连接到fabcar示例时遇到问题

调用getAllCars查询时,node.js API服务器会打印出以下错误

The console logged error

{"error":{"message":"Unable to initialize channel. Attempted to contact 2 Peers. Last error was Error: 2 UNKNOWN: Stream removed","stack":"Error: Unable to initialize channel. Attempted to contact 2 Peers. Last error was Error: 2 UNKNOWN: Stream removed\n at Network._initializeInternalChannel (/Users/rasheenruwisha/blockchain-dev/fabric-samples/fabcar/apiserver1/node_modules/fabric-network/lib/network.js:112:12)"}}

index.js代码如下


var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
// Setting for Hyperledger Fabric
const { FileSystemWallet, Gateway } = require('fabric-network');
const fs = require('fs');
const path = require('path');
const ccpPath = path.resolve(__dirname, '..', '..', 'basic-network', 'connection.json');
const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
const ccp = JSON.parse(ccpJSON);



app.get('/api/queryallcars', async function (req, res) {
  try {
// Create a new file system based wallet for managing identities.
      const walletPath = path.join(process.cwd(), 'wallet');
      const wallet = new FileSystemWallet(walletPath);
      console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the user.
      const userExists = await wallet.exists('user1');
      if (!userExists) {
          console.log('An identity for the user "user1" 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: 'user1', discovery: { enabled: false } });
// 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')
      const result = await contract.evaluateTransaction('queryAllCars');
      console.log(`Transaction has been evaluated, result is: ${result.toString()}`);
      res.status(200).json({response: result.toString()});
} catch (error) {
      console.error(`Failed to evaluate transaction: ${error}`);
      res.status(500).json({error: error});
      process.exit(1);
  }
});

app.listen(8082);

对于connection.json文件,我使用的是基本网络目录中提供的文件。connection.json文件如下

{
    "name": "basic-network",
    "version": "1.0.0",
    "client": {
        "organization": "Org1",
        "connection": {
            "timeout": {
                "peer": {
                    "endorser": "300"
                },
                "orderer": "300"
            }
        }
    },
    "channels": {
        "mychannel": {
            "orderers": [
                "orderer.example.com"
            ],
            "peers": {
                "peer0.org1.example.com": {}
            }
        }
    },
    "organizations": {
        "Org1": {
            "mspid": "Org1MSP",
            "peers": [
                "peer0.org1.example.com"
            ],
            "certificateAuthorities": [
                "ca.example.com"
            ]
        }
    },
    "orderers": {
        "orderer.example.com": {
            "url": "grpc://localhost:7050"
        }
    },
    "peers": {
        "peer0.org1.example.com": {
            "url": "grpc://localhost:7051"
        }
    },
    "certificateAuthorities": {
        "ca.example.com": {
            "url": "http://localhost:7054",
            "caName": "ca.example.com"
        }
    }
}

These are the currently running docker containers

我是Hyperledger的新手,将不胜感激

0 个答案:

没有答案