我在链上成功安装并实例化了链码。我可以注册管理员并通过nodejs注册用户。如果我查询chaincode,它只会返回正确的响应,大约是5次中的3次。其余的抛出无法找到链码的错误。

已安装的链码是结构样本中的基本示例。
我的js文件来查询链码(基于fabcar示例):

/*
 * SPDX-License-Identifier: Apache-2.0
 */

'use strict';

const { FileSystemWallet, Gateway } = require('fabric-network');
const path = require('path');

const ccpPath = path.resolve(__dirname, 'connection-org1.json');

async function main() {
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(ccpPath, { wallet, identity: 'user1', 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('mycc');

    const result = await contract.evaluateTransaction('query', 'a');
    console.log(`Transaction has been evaluated, result is: ${result}`);

} catch (error) {
    console.error(`Failed to evaluate transaction: ${error}`);
    process.exit(1);
}
}

main();

成功的查询带有错误,在这些执行之间没有任何更改,它们之间的间隔大约5秒钟被调用。
root@devserver ~/fabric-samples/bla/first-network # node index.js
Transaction has been evaluated, resultb is: 210

root@devserver ~/fabric-samples/bla/first-network # node index.js
Transaction has been evaluated, resultb is: 210

root@devserver ~/fabric-samples/bla/first-network # node index.js
Transaction has been evaluated, resultb is: 210

root@devserver ~/fabric-samples/bla/first-network # node index.js
Transaction has been evaluated, resultb is: 210

root@devserver ~/fabric-samples/bla/first-network # node index.js
2019-09-09T18:53:24.646Z - warn: [Query]: evaluate: Query ID "[object Object]" of peer "peer1.PharmaProducer.bigpharma.com:8051" failed: message=cannot retrieve package for chaincode mycc/1.0, error open /var/hyperledger/production/chaincodes/mycc.1.0: no such file or directory, stack=Error: cannot retrieve package for chaincode mycc/1.0, error open /var/hyperledger/production/chaincodes/mycc.1.0: no such file or directory
  at self._endorserClient.processProposal (/root/fabric-samples/bla/first-network/node_modules/fabric-network/node_modules/fabric-client/lib/Peer.js:140:36)
  at Object.onReceiveStatus (/root/fabric-samples/bla/first-network/node_modules/grpc/src/client_interceptors.js:1207:9)
  at InterceptingListener._callNext (/root/fabric-samples/bla/first-network/node_modules/grpc/src/client_interceptors.js:568:42)
  at InterceptingListener.onReceiveStatus (/root/fabric-samples/bla/first-network/node_modules/grpc/src/client_interceptors.js:618:8)
  at callback (/root/fabric-samples/bla/first-network/node_modules/grpc/src/client_interceptors.js:845:24), status=500, , url=grpcs://localhost:8051, name=peer1.PharmaProducer.bigpharma.com:8051, grpc.max_receive_message_length=-1, grpc.max_send_message_length=-1, grpc.keepalive_time_ms=120000, grpc.http2.min_time_between_pings_ms=120000, grpc.keepalive_timeout_ms=20000, grpc.http2.max_pings_without_data=0, grpc.keepalive_permit_without_calls=1, name=peer1.PharmaProducer.bigpharma.com:8051, grpc.ssl_target_name_override=peer1.PharmaProducer.bigpharma.com, grpc.default_authority=peer1.PharmaProducer.bigpharma.com, isProposalResponse=true
Failed to evaluate transaction: Error: cannot retrieve package for chaincode mycc/1.0, error open /var/hyperledger/production/chaincodes/mycc.1.0: no such file or directory

我希望代码每次都能成功返回正确的结果,并且不会随机显示该代码不存在的错误。

对此有任何见解,我们将不胜感激。

最佳答案

查看日志,我可以看到为什么有时有时会得到peer0,而有时却会得到peer1,这是因为发现结果和处理结果导致无法以任何特定顺序获取对等体列表,因此在组织中只有2个对等体而且这不是一个运行时间较长的应用程序,而是一个运行时间较短的应用程序,有时peer0会在对等项(有时是peer1)的列表中排在第一位。因为您仅在peer0上安装了链码,所以peer1无法满足评估请求并返回错误。

node-sdk应该检测到此问题,然后尝试peer0,但是您使用的是Node-sdk的旧版本,肯定有一个问题,即它没有尝试其他对等项,或者node-sdk认为这是(不正确的,但是可能无法区分)链码响应,并将其传递回调用方。
避免该错误的解决方案是在所有对等方上安装链码。

10-08 01:00