1

编辑:似乎network.sh脚本需要设置COMPOSE_PROJECT_NAME环境变量,test否则它不会正确初始化网络?完成此操作后,我尝试编写一个新config.yaml文件,但仍然不正确,但我得到的错误已经改变。

我最近开始使用Hyperledger织物。使用“fabric-samples”测试网络,我开发并安装了连接到网关后成功调用的链代码。

要启动区块链网络,我运行以下test-network/network.sh命令:

./network.sh up createChannel -ca

现在,我正在尝试以编程方式访问一个块(为了查看背书者),但clientChannelContext我得到导致此错误:

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

不知道 fabsdk 参考需要什么配置,我尝试了在文件夹下找到的每个 文件,但没有成功。*.yamltest-network

虽然fabsdk.New()返回“有效” *FabricSDK,但我怀疑它没有正确初始化。

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)

}

我当前的 config.yaml,基于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
4

0 回答 0