hyperledger fabric sdk trying to create channel when there is one already

37 Views Asked by At

my problem is that my java application for hyperledger tries to create channel when there is one already created and i don't have any idea why. I have no idea what to do further with this but would love for someone to help

    private static final String CHANNEL_NAME = System.getenv().getOrDefault("CHANNEL_NAME", "assecodschannel");
    private static final String CHAINCODE_NAME = System.getenv().getOrDefault("CHAINCODE_NAME", "ccv1");

    static {
        System.setProperty("org.hyperledger.fabric.sdk.service_discovery.as_localhost", "true");
    }

    // helper function for getting connected to the gateway
    public static Gateway connect() throws Exception {

        // Load a file system based wallet for managing identities.
        Path walletPath = Paths.get("wallet");
        Wallet wallet = Wallets.newFileSystemWallet(walletPath);
        System.out.println("TLS Cert Content: " + new String(Files.readAllBytes(Paths.get("tls-cert.pem"))));
        System.out.println("CA Cert Content: " + new String(Files.readAllBytes(Paths.get("ca.crt"))));

        Path networkConfigPath = Paths.get("template","connections.yml");

        Gateway.Builder builder = Gateway.createBuilder();
        builder.identity(wallet, "User").networkConfig(networkConfigPath).discovery(true);
        return builder.connect();
    }

public byte[] getAllAssets() throws ContractException {
    System.out.println("Before connecting to the network");
    try (Gateway gateway = connect()) {
        System.out.println("After connecting to the network");
//        System.out.println(gateway.getNetwork("assecodschannel").getChannel().joinPeer());
        gateway.getNetwork("assecodschannel").getChannel();
        System.out.println("jj");
        Network network = gateway.getNetwork("assecodschannel");
                System.out.println("huj");
        Contract contract = network.getContract("chaincode");
        return contract.evaluateTransaction("GetAllAssets");
    }
    catch(Exception e){
        System.err.println(e);
        System.exit(1);
        return null;
    }

}

also my network configs, connections.yml:

name: "AssecoDS-network"
version: "1.0"
x-type: "hlfv1"

client:
  organization: AssecoDS
  connection:
    timeout:
      peer:
        endorser: '300'
      orderer: '300'
channels:
  assecodschannel:
    orderers:
      - orderer1.assecods.pl
    peers:
      peer1.assecods.pl:
        endorsingPeer: true
        chaincodeQuery: true
        ledgerQuery: true
        eventSource: true
        discover: true

organizations:
  AssecoDS:
    mspid: AssecoDSMSP
    peers:
      - peer1.assecods.pl
    certificateAuthorities:
      - ca.assecods.pl

orderers:
  orderer1.assecods.pl:
    url: 100.64.192.62:7050
    grpcOptions:
      ssl-target-name-override: orderer1.assecods.pl
      grpc-max-send-message-length: -1
    tlsCACerts:
      path: ca.crt

peers:
  peer1.assecods.pl:
    url: 100.64.192.62:7051
    eventUrl: 100.64.192.62:7053
    grpcOptions:
      ssl-target-name-override: peer1.assecods.pl
      grpc.keepalive_time_ms: 120001
    tlsCACerts:
      path: /home/hubert/Desktop/hyperledger-fabric-generic-network-master/application-java/tls-cert.pem

certificateAuthorities:
  ca.assecods.pl:
    url: https://localhost:7054
    httpOptions:
      verify: false
    registrar:
      - enrollId: admin
        enrollSecret: adminpw
    caName: ca.assecods.pl

and connection.yaml:

---
name: AssecoDS-network
version: 1.0.0
client:
  organization: AssecoDS
  connection:
    timeout:
      peer:
        endorser: '300'
      orderer: '300'
channels:
  assecodschannel:
    orderers:
    - orderer1.assecods.pl
    peers:
     peer1.assecods.pl: {}
organizations:
  AssecoDS:
    mspid: AssecoDSMSP
    peers:
    - peer1.assecods.pl
    certificateAuthorities:
    - ca.assecods.pl
orderers:
  orderer1.assecods.pl:
    url: 127.0.0.1:7050
peers:
  peer1.assecods.pl:
    url: 127.0.0.1:7051
    tlsCACerts:
      pem: /home/hubert/Desktop/hyperledger-fabric-generic-network-master/application-java/tls-cert.pem
certificateAuthorities:
  ca.assecods.pl:
    url: 127.0.0.1:7054
    caName: ca.assecods.pl
    httpOptions:
      verify: false
    tlsCACerts:
      pem: /home/hubert/Desktop/hyperledger-fabric-generic-network-master/application-java/tls-cert.pem
    registrar:
    - enrollId: admin
      enrollSecret: adminpw


Screenshot of an error

i've tried changing network config, removing channel section from network config, adding peers manually to channels without network config, as well as many smaller changes. Only result i've got was channel has no config or channel has no peers

1

There are 1 best solutions below

0
bestbeforetoday On

I think what is happening here is that, when your Gateway client obtains service discovery information about the network topology, it is finding a definition of assecodschannel. When it tries to add that to the network information provided by your connection profile, it is finding a duplicate since your connection profile already contains a definition of assecodschannel.

When using service discovery, your connection profile only needs to contain enough information for the client to be able to connect to a network peer. A channel definition is not required. As an example, see this connection profile used by service discovery tests for the fabric-gateway-java API:

https://github.com/hyperledger/fabric-gateway-java/blob/b4b5bb6335b5369be329d0b92e4699bd60c6c148/src/test/java/org/hyperledger/fabric/gateway/connection-discovery.json

Also the template used to generate connection profiles for the test-network used by the Fabric samples:

https://github.com/hyperledger/fabric-samples/blob/02d9f8c58b8416019682120dc179fa8146ae7dad/test-network/organizations/ccp-template.json

Note that neither contain a channel definition.

Please also note that the legacy Java SDKs are deprecated as of Fabric v2.5. If you are using (or can use) Fabric v2.4 or later, you should use the newer Fabric Gateway client API. This simplifies the client connection experience and does not require a connection profile at all.