使用nodejs,仅在〜3/5情况下查询链码才成功 [英] Querying chaincode is only successful in ~3/5 cases with nodejs

查看:69
本文介绍了使用nodejs,仅在〜3/5情况下查询链码才成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I successfully installed and instantiated chaincode on my chain. I'm able to enroll the admin and register a user trough nodejs. If I query the chaincode it only returns a correct response around 3 out of 5 times. The rest throws errors that the chaincode can't be found.

已安装的链码是结构示例中的基本示例. 我的js文件用于查询链式代码(基于fabcar示例):

The chaincode installed is the basic example from the fabric samples. My js file to query the chaincode (based on the fabcar example):

/*
 * 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秒钟被调用.

The successful queries with the errors, nothing has been changed between those executions and they have been called with around 5 seconds beetween them.

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

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

I expect the code to successfuly return a correct result every time and not randomly show an error that the code doesn't exist.

对这种情况如何发生的任何见解都受到赞赏.

Any insight on how this can happen is appreciated.

推荐答案

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

Looking at the logs I can see why sometimes you get peer0 and occasionally you get peer1, and that is because the discovery results and processing result in it not being possible to get the list of peers in any particular order so with 2 peers in your org and the fact that this is not a long running app but a short running invocation sometimes peer0 will be first in the list of peers and sometimes peer1. Because you have only installed the chaincode on peer0, peer1 can't honour the evaluate request and return an error.

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

The node-sdk should detect this and then try peer0 but either you are using an older version of the node-sdk which definitely had an issue where it did not try a different peer or the node-sdk is thinking this is (incorrectly, but may not be able to distinguish) a chaincode response and passes that back to the caller. The solution to avoid the error is to install the chaincode on all the peers.

这篇关于使用nodejs,仅在〜3/5情况下查询链码才成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆