How to initialize FabricSDK correctly?

707 views Asked by At

EDIT: It appears the network.sh script requires the COMPOSE_PROJECT_NAME environment variable to be set to test or it won't initialize the network correctly? Having done this, I've tried writing a new config.yaml file, which is still not correct, but the error I get has changed.

I have recently started working with Hyperledger fabric. Using the "fabric-samples" test-network, I've developed and installed chaincode which I've successfully invoked after connecting to the gateway.

To start the block-chain network, I run the test-network/network.sh command:

./network.sh up createChannel -ca

Now, I'm trying to programmatically access a block (for the purpose of viewing the endorsers) but the clientChannelContext I get causes this error:

Can not get ledger client failed to get client context to create channel client: user not found

Not knowing what configuration was needed for the fabsdk reference, I tried every *.yaml file found under the test-network folder without success.

Although fabsdk.New() returns a "valid" *FabricSDK I suspect it isn't initialized properly.

package main

import (
    "fmt"
    "os"

    "github.com/hyperledger/fabric-sdk-go/pkg/client/ledger"
    "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
    "github.com/hyperledger/fabric-sdk-go/pkg/core/config"
    "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
)

func main() {
    if len(os.Args) != 2 {
        return
    }

    channelID := "mychannel"
    orgAdmin := "Admin"
    orgName := "Org1"
    fmt.Printf("Using:\n\tChannel:\t %s,\n\tOrg:\t\t %s,\n\tUser:\t\t %s\n", channelID, orgName, orgAdmin)

    cfgProvider := config.FromFile("./config.yaml")

    sdk, err := fabsdk.New(cfgProvider)
    if err != nil {
        fmt.Printf("Failed to create new SDK: %s", err)
    }
    defer sdk.Close()
    clientChannelContext := sdk.ChannelContext(channelID, fabsdk.WithUser(orgAdmin), fabsdk.WithOrg(orgName))
    client, err := ledger.New(clientChannelContext)
    if err != nil {
        fmt.Printf("Can not get ledger client %v", err)
        return
    } else {
        fmt.Printf("The client is %v", client)
    }
    tx, err := client.QueryBlockByTxID(fab.TransactionID(os.Args[1]))
    if err != nil {
        fmt.Printf("Epic fail, but at least we've got a client now: %v", err)
        return
    }
    fmt.Printf("The tx is %v", tx)

}

My current config.yaml, based on config_e2e.yaml:

#
# Copyright SecureKey Technologies Inc. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
#
# The network connection profile provides client applications the information about the target
# blockchain network that are necessary for the applications to interact with it. These are all
# knowledge that must be acquired from out-of-band sources. This file provides such a source.
#
version: 1.0.0
client:
  organization: Org1

  logging:
    level: info

  # Root of the MSP directories with keys and certs.
  cryptoconfig:
    path: ${FABRIC_SDK_GO_PROJECT_PATH}/test-network/organizations

  credentialStore:
    # [Optional]. Used by user store. Not needed if all credentials are embedded in configuration
    # and enrollments are performed elswhere.
    path: "/tmp/state-store"

    # [Optional]. Specific to the CryptoSuite implementation used by GO SDK. Software-based implementations
    # requiring a key store. PKCS#11 based implementations does not.
    cryptoStore:
      # Specific to the underlying KeyValueStore that backs the crypto key store.
      path: /tmp/msp

  # [Optional] BCCSP config for the client. Used by GO SDK.
  BCCSP:
    security:
     enabled: true
     default:
      provider: "SW"
     hashAlgorithm: "SHA2"
     softVerify: true
     level: 256

  tlsCerts:
    # [Optional]. Use system certificate pool when connecting to peers, orderers (for negotiating TLS) Default: false
    systemCertPool: true

channels:
  _default:
    peers:
      peer0.org1.example.com:
        endorsingPeer: true
        chaincodeQuery: true
        ledgerQuery: true
        eventSource: true

    policies:
      queryChannelConfig:
        minResponses: 1
        maxTargets: 1
        retryOpts:
          attempts: 5
          initialBackoff: 500ms
          maxBackoff: 5s
          backoffFactor: 2.0
      discovery:
        maxTargets: 2
        retryOpts:
          attempts: 4
          initialBackoff: 500ms
          maxBackoff: 5s
          backoffFactor: 2.0

      eventService:
        resolverStrategy: PreferOrg
        balancer: Random
        blockHeightLagThreshold: 5
        reconnectBlockHeightLagThreshold: 8
        peerMonitorPeriod: 6s

  mychannel:
    peers:
      peer0.org1.example.com:
        endorsingPeer: true
        chaincodeQuery: true
        ledgerQuery: true
        eventSource: true

#
# list of participating organizations in this network
#
organizations:
  Org1:
    mspid: Org1MSP
    cryptoPath:  peerOrganizations/org1.example.com/users/{username}@org1.example.com/msp
    peers:
      - peer0.org1.example.com

    certificateAuthorities:
      - ca_org1
  Org2:
    mspid: Org2MSP
    cryptoPath:  peerOrganizations/org2.example.com/users/{username}@org2.example.com/msp
    peers:
      - peer0.org2.example.com

    certificateAuthorities:
      - ca_org2

  # Orderer Org name
  orderer.example.com:
      mspID: OrdererMSP
      cryptoPath: ordererOrganizations/example.com/users/{username}@example.com/msp


#
# List of orderers to send transaction and channel create/update requests to. For the time
# being only one orderer is needed. If more than one is defined, which one get used by the
# SDK is implementation specific. Consult each SDK's documentation for its handling of orderers.
#
orderers:
  orderer.example.com:
    # [Optional] Default: Infer from hostname
    url: orderer.example.com:7050
    grpcOptions:
      ssl-target-name-override: orderer.example.com
      keep-alive-time: 0s
      keep-alive-timeout: 20s
      keep-alive-permit: false
      fail-fast: false
      allow-insecure: false

    tlsCACerts:
      path: ${FABRIC_SDK_GO_PROJECT_PATH}/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
#
# List of peers to send various requests to, including endorsement, query
# and event listener registration.
#
peers:
  peer0.org1.example.com:
    url: peer0.org1.example.com:7051

    grpcOptions:
      ssl-target-name-override: peer0.org1.example.com
      keep-alive-time: 0s
      keep-alive-timeout: 20s
      keep-alive-permit: false
      fail-fast: false
      allow-insecure: false

    tlsCACerts:
      path: ${FABRIC_SDK_GO_PROJECT_PATH}/test-network/organizations/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pem

  peer0.org2.example.com:
    url: peer0.org2.example.com:9051
    grpcOptions:
      ssl-target-name-override: peer0.org2.example.com
      keep-alive-time: 0s
      keep-alive-timeout: 20s
      keep-alive-permit: false
      fail-fast: false
      allow-insecure: false

    tlsCACerts:
      path: ${FABRIC_SDK_GO_PROJECT_PATH}/test-network/organizations/peerOrganizations/org2.example.com/tlsca/tlsca.org2.example.com-cert.pem

#
# Fabric-CA is a special kind of Certificate Authority provided by Hyperledger Fabric which allows
# certificate management to be done via REST APIs. Application may choose to use a standard
# Certificate Authority instead of Fabric-CA, in which case this section would not be specified.
#
certificateAuthorities:
  ca_org1:
    tlsCACerts:
      path: ${FABRIC_SDK_GO_PROJECT_PATH}/test-network/organizations/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pem
    registrar:
      enrollId: admin
      enrollSecret: adminpw
    caName: ca.org1.example.com
  ca_org2:
    tlsCACerts:
      path: ${FABRIC_SDK_GO_PROJECT_PATH}/test-network/organizations/peerOrganizations/org2.example.com/tlsca/tlsca.org2.example.com-cert.pem
    registrar:
      enrollId: admin
      enrollSecret: adminpw
    caName: ca_org2
0

There are 0 answers