attacker melon
attacker melon

Reputation: 13

【Hyperledger Fabric】Can't send invoke request to peer

My fabric sdk server is working as a docker container. It is running on mac.

Fabric docker containers(peers, orderers and CAs...) are also running on mac.

Now I'm trying to send invoke request from sdk container(ubuntu:18.04) to peer.

And I'm using first-network of fabric-samples.

This is peer config file(docker-compose-base.yaml).

version: '2'

services:

  orderer.example.com:
    container_name: orderer.example.com
    extends:
      file: peer-base.yaml
      service: orderer-base
    volumes:
        - ../channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
        - ../crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp:/var/hyperledger/orderer/msp
        - ../crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/:/var/hyperledger/orderer/tls
        - orderer.example.com:/var/hyperledger/production/orderer
    ports:
      - 7050:7050

  peer0.org1.example.com:
    container_name: peer0.org1.example.com
    extends:
      file: peer-base.yaml
      service: peer-base
    environment:
      - CORE_PEER_ID=peer0.org1.example.com
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      - CORE_PEER_LISTENADDRESS=0.0.0.0:7051
      - CORE_PEER_CHAINCODEADDRESS=peer0.org1.example.com:7052
      - CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052
      - CORE_PEER_GOSSIP_BOOTSTRAP=peer1.org1.example.com:8051
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
    volumes:
        - /var/run/:/host/var/run/
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/fabric/msp
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tls
        - peer0.org1.example.com:/var/hyperledger/production
    ports:
      - 7051:7051

  peer1.org1.example.com:
    container_name: peer1.org1.example.com
    extends:
      file: peer-base.yaml
      service: peer-base
    environment:
      - CORE_PEER_ID=peer1.org1.example.com
      - CORE_PEER_ADDRESS=peer1.org1.example.com:8051
      - CORE_PEER_LISTENADDRESS=0.0.0.0:8051
      - CORE_PEER_CHAINCODEADDRESS=peer1.org1.example.com:8052
      - CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:8052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org1.example.com:8051
      - CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
    volumes:
        - /var/run/:/host/var/run/
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/msp:/etc/hyperledger/fabric/msp
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls:/etc/hyperledger/fabric/tls
        - peer1.org1.example.com:/var/hyperledger/production

    ports:
      - 8051:8051

  peer0.org2.example.com:
    container_name: peer0.org2.example.com
    extends:
      file: peer-base.yaml
      service: peer-base
    environment:
      - CORE_PEER_ID=peer0.org2.example.com
      - CORE_PEER_ADDRESS=peer0.org2.example.com:9051
      - CORE_PEER_LISTENADDRESS=0.0.0.0:9051
      - CORE_PEER_CHAINCODEADDRESS=peer0.org2.example.com:9052
      - CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:9052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:9051
      - CORE_PEER_GOSSIP_BOOTSTRAP=peer1.org2.example.com:10051
      - 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:9051

  peer1.org2.example.com:
    container_name: peer1.org2.example.com
    extends:
      file: peer-base.yaml
      service: peer-base
    environment:
      - CORE_PEER_ID=peer1.org2.example.com
      - CORE_PEER_ADDRESS=peer1.org2.example.com:10051
      - CORE_PEER_LISTENADDRESS=0.0.0.0:10051
      - CORE_PEER_CHAINCODEADDRESS=peer1.org2.example.com:10052
      - CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:10052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org2.example.com:10051
      - CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org2.example.com:9051
      - CORE_PEER_LOCALMSPID=Org2MSP
    volumes:
        - /var/run/:/host/var/run/
        - ../crypto-config/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/msp:/etc/hyperledger/fabric/msp
        - ../crypto-config/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/tls:/etc/hyperledger/fabric/tls
        - peer1.org2.example.com:/var/hyperledger/production
    ports:
      - 10051:10051

This is connection file (connection-org1.json).

{
    "name": "first-network-org1",
    "version": "1.0.0",
    "client": {
        "organization": "Org1",
        "connection": {
            "timeout": {
                "peer": {
                    "endorser": "300"
                }
            }
        }
    },
    "organizations": {
        "Org1": {
            "mspid": "Org1MSP",
            "peers": [
                "peer0.org1.example.com",
                "peer1.org1.example.com"
            ],
            "certificateAuthorities": [
                "ca.org1.example.com"
            ]
        }
    },
    "peers": {
        "peer0.org1.example.com": {
            "url": "grpcs://host.docker.internal:7051",
            "tlsCACerts": {
                "pem": "-----BEGIN CERTIFICATE-----\nMIICVzCCAf2gAwIBAgIQIrzVUkH/VhPQNk1YHCtj3jAKBggqhkjOPQQDAjB2MQsw\nCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZy\nYW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEfMB0GA1UEAxMWdGxz\nY2Eub3JnMS5leGFtcGxlLmNvbTAeFw0xOTEyMTIwODIwMDBaFw0yOTEyMDkwODIw\nMDBaMHYxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQH\nEw1TYW4gRnJhbmNpc2NvMRkwFwYDVQQKExBvcmcxLmV4YW1wbGUuY29tMR8wHQYD\nVQQDExZ0bHNjYS5vcmcxLmV4YW1wbGUuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0D\nAQcDQgAE8TqkZLAW+F2rYwnicTo2NTo1+2kYUvI28UKvgAdm1iavbwunNlBB+Gph\nLT4z/XVDjp2XP3VYdv4jmCRSmBkREKNtMGswDgYDVR0PAQH/BAQDAgGmMB0GA1Ud\nJQQWMBQGCCsGAQUFBwMCBggrBgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MCkGA1Ud\nDgQiBCDW8LHjcUuWGmLSfNXZym6gDPb9twcHkByS3Yoa/rM4YTAKBggqhkjOPQQD\nAgNIADBFAiEAieGWQzmyWS2pUORmXczUM/XCaB4t33HNtizkr62YgWUCIB7HVwss\ny7l1k9ifxb0VN7q4pzIeHTFMeH6+e6Nl3p2C\n-----END CERTIFICATE-----\n"
            },
            "grpcOptions": {
                "ssl-target-name-override": "peer0.org1.example.com"
            }
        },
        "peer1.org1.example.com": {
            "url": "grpcs://host.docker.internal:8051",
            "tlsCACerts": {
                "pem": "-----BEGIN CERTIFICATE-----\nMIICVzCCAf2gAwIBAgIQIrzVUkH/VhPQNk1YHCtj3jAKBggqhkjOPQQDAjB2MQsw\nCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZy\nYW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEfMB0GA1UEAxMWdGxz\nY2Eub3JnMS5leGFtcGxlLmNvbTAeFw0xOTEyMTIwODIwMDBaFw0yOTEyMDkwODIw\nMDBaMHYxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQH\nEw1TYW4gRnJhbmNpc2NvMRkwFwYDVQQKExBvcmcxLmV4YW1wbGUuY29tMR8wHQYD\nVQQDExZ0bHNjYS5vcmcxLmV4YW1wbGUuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0D\nAQcDQgAE8TqkZLAW+F2rYwnicTo2NTo1+2kYUvI28UKvgAdm1iavbwunNlBB+Gph\nLT4z/XVDjp2XP3VYdv4jmCRSmBkREKNtMGswDgYDVR0PAQH/BAQDAgGmMB0GA1Ud\nJQQWMBQGCCsGAQUFBwMCBggrBgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MCkGA1Ud\nDgQiBCDW8LHjcUuWGmLSfNXZym6gDPb9twcHkByS3Yoa/rM4YTAKBggqhkjOPQQD\nAgNIADBFAiEAieGWQzmyWS2pUORmXczUM/XCaB4t33HNtizkr62YgWUCIB7HVwss\ny7l1k9ifxb0VN7q4pzIeHTFMeH6+e6Nl3p2C\n-----END CERTIFICATE-----\n"
            }
        }
    },
    "certificateAuthorities": {
        "ca.org1.example.com": {
            "url": "https://host.docker.internal:7054",
            "caName": "ca-org1",
            "tlsCACerts": {
                "pem": "-----BEGIN CERTIFICATE-----\nMIICUTCCAfegAwIBAgIQeWwAs49jzhe2XsEmY4M0jDAKBggqhkjOPQQDAjBzMQsw\nCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZy\nYW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEcMBoGA1UEAxMTY2Eu\nb3JnMS5leGFtcGxlLmNvbTAeFw0xOTEyMTIwODIwMDBaFw0yOTEyMDkwODIwMDBa\nMHMxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1T\nYW4gRnJhbmNpc2NvMRkwFwYDVQQKExBvcmcxLmV4YW1wbGUuY29tMRwwGgYDVQQD\nExNjYS5vcmcxLmV4YW1wbGUuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE\nhvJll8VoZC+0seO0fKrpbxWWAABOt2UoCbyq540wY3YSM2GCKuD2XMTtCsiC8XEB\nbKaokdxo5WyWXOsamK1hEKNtMGswDgYDVR0PAQH/BAQDAgGmMB0GA1UdJQQWMBQG\nCCsGAQUFBwMCBggrBgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MCkGA1UdDgQiBCC2\nwW1+TNe+qJeskHsq1AoNdYrmgKJ2Pf12KqootThXNDAKBggqhkjOPQQDAgNIADBF\nAiEA+2pAiOxL64KxOFWoqavs5NWeO+GLN0ArS14zCsBxS4MCIAQ7nBMVPYyOHYLS\nBy/zxDAzC+NsFq1iKxyWQxq3Yu9I\n-----END CERTIFICATE-----\n"
            },
            "httpOptions": {
                "verify": false
            }
        }
    }
}

And this is invoke file (invoke.js).

'use strict';

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

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

module.exports = async function (key, data) {
    try {

        // Create a new file system based wallet for managing identities.
        const walletPath = path.join(process.cwd(), 'wallet');
        const wallet = await Wallets.newFileSystemWallet(walletPath);
        console.log(`Wallet path: ${walletPath}`);

        // Check to see if we've already enrolled the user.
        const identity = await wallet.get('user1');
        if (!identity) {
            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('save-file-hash');
        // Submit the specified transaction.
        await contract.submitTransaction('registerHash', key, data.length.toString());


        for(var i = 0; i < data.length; i++) {
            await contract.submitTransaction('registerHash', key + i.toString(), data[i]);
        }

        console.log('Transactions has been submitted');

        // Disconnect from the gateway.
        await gateway.disconnect();

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

And this is error log.

# node main.js
Wallet path: /usr/src/app/wallet
2019-12-13T13:03:10.930Z - error: [Remote.js]: Error: Failed to connect before the deadline URL:grpcs://localhost:7051 timeout:3000
2019-12-13T13:03:10.935Z - warn: [DiscoveryEndorsementHandler]: _build_endorse_group_member >> G0:1 - endorsement failed - Error: Failed to connect before the deadline URL:grpcs://localhost:7051 timeout:3000
2019-12-13T13:03:10.937Z - error: [Remote.js]: Error: Failed to connect before the deadline URL:grpcs://localhost:9051 timeout:3000
2019-12-13T13:03:10.938Z - warn: [DiscoveryEndorsementHandler]: _build_endorse_group_member >> G1:0 - endorsement failed - Error: Failed to connect before the deadline URL:grpcs://localhost:9051 timeout:3000
2019-12-13T13:03:13.938Z - error: [Remote.js]: Error: Failed to connect before the deadline URL:grpcs://localhost:8051 timeout:3000
2019-12-13T13:03:13.941Z - warn: [DiscoveryEndorsementHandler]: _build_endorse_group_member >> G0:1 - endorsement failed - Error: Failed to connect before the deadline URL:grpcs://localhost:8051 timeout:3000
2019-12-13T13:03:13.944Z - error: [Remote.js]: Error: Failed to connect before the deadline URL:grpcs://localhost:10051 timeout:3000
2019-12-13T13:03:13.944Z - warn: [DiscoveryEndorsementHandler]: _build_endorse_group_member >> G1:0 - endorsement failed - Error: Failed to connect before the deadline URL:grpcs://localhost:10051 timeout:3000
2019-12-13T13:03:13.947Z - error: [DiscoveryEndorsementHandler]: _endorse - endorsement failed::Error: Endorsement has failed
    at DiscoveryEndorsementHandler._endorse (/usr/src/app/node_modules/fabric-client/lib/impl/DiscoveryEndorsementHandler.js:185:19)
    at process._tickCallback (internal/process/next_tick.js:68:7)
Failed to submit transaction: Error: Endorsement has failed

When sdk server runs on mac and not a docker container, invoke request success. And when it is a docker container, invoke request fail. Also, user1 is already enrolled in CA-org1. I think it is a problem with connection. Please tell me how to send invoke request from sdk server container.

Upvotes: 1

Views: 907

Answers (2)

Carlos
Carlos

Reputation: 16

First:
you can read the document:
https://hyperledger-fabric.readthedocs.io/en/latest/developapps/connectionprofile.html
There has a sample and your config miss channels:{} and orderers:{}

Second:
If you don't enable tls, use the grpc:XXX.XXX, or you should use grpcs:XXX.XXX. For example, your orderer not enable tls and you should use "url": "grpc://host.docker.internal:7050", your peers enable tls and you should use "url": "grpcs://host.docker.internal:8051".

Third:
After last two steps, the query transaction should be ok. But invoke(some write transactions) may still fail. I still block here...

Upvotes: 0

R Thatcher
R Thatcher

Reputation: 5570

I think this is the line that is causing you a problem in the "SDK Container":

await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } });

asLocalhost: true will be substituting all the addresses with localhost and they just try to connect bsack into the SDK container which is causing the errors. When running directly on the mac the port forwarding into the docker containers works with localhost.

After you use asLocalhost: false you may have to change the various urls in the connection profile to be peer1.org1.example.com etc.

Upvotes: 1

Related Questions