Search code examples
hyperledger-fabricgrpc-node

fabric-gateway on node.js takes 4 minutes to complete fail or success


I'm trying to migrate from fabric-network to fabric-gateway, in largely due to experiencing an issue with fabric-network where hundreds of connections are left open and eventually clog the server. I've read this is not that uncommon and that a solution would be to use fabric-gateway. However, the fabric-gateway implementation is somewhat different. My current implementation is taking 4 minutes (I measure the time) to complete, whether it's a success or failure. By that time, the API call (via Postman) triggered the SDK times out, but I eventually I get a response in the console.

This is the main code, pretty straightforward:

  // IDENTITY
  const credentials = fs.readFileSync(CERT_PATH);
  console.log({ credentials });
  const identity = { mspId: "Org1MSP", credentials };

  // SIGNER
  const privateKeyPem = fs.readFileSync(PK_PATH);
  const privateKey = crypto.createPrivateKey(privateKeyPem);
  console.log({ privateKey });
  const signer = signers.newPrivateKeySigner(privateKey);

  // CLIENT
  const rootCERT = await fs.readFileSync(ROOT_PATH);
  const tlsCredentials = await grpc.credentials.createSsl(rootCERT);
  console.log({ tlsCredentials });
  const client = new grpc.Client("localhost:7051", tlsCredentials, {
    "grpc.ssl_target_name_override": "peer0.org1.example.com",
    "grpc.default_authority": "localhost:7054",
  });
  console.log({ client });
  const gateway = connect({ identity, signer, client });
  try {
    const network = gateway.getNetwork("mychannel");
    const contract = network.getContract("transaction");
    const stringQuery = `{"selector": { "transactionId": {"$ne":"0"} } }`;
    const res = await contract.evaluateTransaction(
      "QueryTransaction",
      stringQuery
    );
    console.log("RESULT:", decoder.decode(res));
  } finally {
    gateway.close();
    client.close();
  }

I've tested the code above in my local environment, and it works just fine. But when I have it run on the TEST environment, it takes 4 minutes to resolve.

A couple of differences among environments:

  • My local implementation uses Root CA, and the TEST network implements Intermediate CA. So for the latter, I use a chain certificate.
  • My local implementation is running both peer and ordered nodes at 2.4.0, while the Test network has peer at 2.4.0 and ordered at 2.4.5.

I've also found this issue https://github.com/hyperledger/fabric/issues/3224 which talks a bit about a similar problem, but with the GO SDK, and If I understood correctly, It should be fixed.

It's worth mentioning that it takes (almost) exactly 4 minutes each time. Almost as if it were configured to last 360 seconds...


Solution

  • If you've been experiencing this problem, look for the following items:

    • Use fabric-gateway instead of fabric-network if you have fabric 2.4v and up.
    • Review your chaincode logic, avoid nested chaincode invocations and CouchDB queries using logical operators and/or without indexes. Always use indexes.