Search code examples
javahyperledger-fabrichyperledgerchaincode

Failed to invoke chaincode in hyperledger fabric


I am learning how to use Hyperledger fabric and Chaincode. Basically, I followed this link to create a Java chain code in Hyperledger fabric, but I can't invoke my chain code in the cli/channel.

I used BYFN.sh from Hyperledger fabric official guide to building up the fabric network and followed the blog to make Gradle & Java Chaincode.

After I tried to invoke it with

peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["createWallet","tom","100"]}'

I got this error:

Error: error getting endorser client for invoke: endorser client failed to connect to peer0.org2.example.com:7051: failed to create new connection: connection error: desc = "transport: error while dialing: dial tcp 172.21.0.4:7051: connect: connection refused"

It would be very helpful if someone can point a direction and tell me what might possibly be wrong. I tried to google it but I could not find any useful information.


update:

Turns out it is a port number issue. I went into one of the peer's container and turns out that the port number is 9051 instead of 7051. So I change the port number in the chaincode invoke command and everything works fine.


Solution

  • You are specifying same port number for peer0.0rg1.example.com and peer0.0rg2.example.com in your command. if you have created you network using byfn topology peer0.org2 listens on port 9051. Also, when asking these type of question, it's always better to provide your configuration i.e docker-compose.yaml files. That is very useful when understanding the questions.

    It maybe possible that in your compose.yaml file you have not mapped the port number of Org2 to a different port number.

    You can try any of the following-

    1. Look at the port number defined in docker yaml file and use that in command for invoking chain-code. It should be different for every peer.

    2. Modify the yaml file so that 7051 redirects to a new port number, say 9051. like below

      environment:

      • CORE_PEER_ID=peer0.org2.example.com
      • CORE_PEER_ADDRESS=peer0.org2.example.com:7051
      • CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:7051
      • CORE_PEER_GOSSIP_BOOTSTRAP=peer1.org2.example.com:7051
      • CORE_PEER_LOCALMSPID=Org2MSP volumes:
        • /var/run/:/host/var/run/
        • ../crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp:/etc/hyperledger/fabric/msp
        • ../crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls:/etc/hyperledger/fabric/tls
        • peer0.org2.example.com:/var/hyperledger/production ports:
      • 9051:7051
      • 9053:7053`.

    Look at the end where port number 7051 is mapped to 9051.