Hyperledger Fabric 1.4: Getting an error while trying to connect ChannelEventHub.connect(true). Error: Error connect the ChannelEventhub to peer

Hyperledger Fabric 1.4: Getting an error while trying to connect ChannelEventHub.connect(true). Error: Error connect the ChannelEventhub to peer

这是我之前提出的问题 () 的后续问题,我正在尝试实施@david_k.

建议的解决方案

我正在使用 Fabcar 示例作为基础并以此为基础进行构建。

我 运行 startFabric.sh,我收到的消息是 "Successfully submitted proposal to join channel"

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer0.org1.example.com peer channel create -o orderer.example.com:7050 -c mychannel -f /etc/hyperledger/configtx/channel.tx

2019-03-11 08:54:51.857 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized

2019-03-11 08:54:52.076 UTC [cli.common] readBlock -> INFO 002 Received block: 0

Join peer0.org1.example.com to the channel.

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer0.org1.example.com peer channel join -b mychannel.block

2019-03-11 08:54:52.387 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized

2019-03-11 08:54:52.481 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel

然后我尝试使用 Fabric Node SDK(fabric-client)从 'Channel' Class 初始化 channelEventHub,以订阅从我的链代码发出的事件。

const channel = new Channel('mychannel',ccp);
const channelEventHub = channel.getChannelEventHub('peer0.org1.example.com')

我收到以下错误:

Failed to submit transaction: Error: Failed to discover::Error: Peer with name "peer1.org1.example.com" not assigned to this channel

之后,我尝试使用 Channel 对象中的 getPeers() 函数,但得到一个空数组,否则它应该返回一个在通道上注册的对等点数组。

const channel = new Channel('mychannel',ccp);

let peers = channel.getChannelPeers();

我控制台记录了对等点,它返回了 []。

我的连接配置文件JSON如下(基本网络里的,我没改过)

{"name":"basic-network","version":"1.0.0", "client":{"organization":"Org1", "connection":{"timeout":{"peer":{"endorser":"300"},"orderer":"300"}}}, "channels": {"mychannel": { "orderers":["orderer.example.com"], "peers": { "peer0.org1.example.com":{} } } },"organizations":{"Org1":{"mspid":"Org1MSP","peers":["peer0.org1.example.com"],"certificateAuthorities":["ca.example.com"]},"Org2":{"mspid":"Org2MSP","peers":["peer0.org2.example.com"],"certificateAuthorities":["ca.example.com"]}},"orderers":{"orderer.example.com":{"url":"grpc://localhost:7050"}},"peers":{"peer0.org1.example.com":{"url":"grpc://localhost:7051"},"peer0.org2.example.com":{"url":"grpc://localhost:7052"}},"certificateAuthorities":{"ca.example.com":{"url":"http://localhost:7054","caName":"ca.example.com"}}}

这里可以清楚的看到通道中定义了peer

我不明白为什么会出现此错误。任何有关如何解决此问题的指示都将不胜感激。

Update:

我创建了一个 StateStore class 它实现了 fabric_client.IKeyValueStore 接口

实例化了。然后使用

设置状态存储
await fabric_client.setStateStore(stateStore);

之后我创建了一个用户并使用以下命令设置用户上下文

    let user = await fabric_client.createUser(userOpts);
    // Set the userContext by passing the User Context
    await fabric_client.setUserContext(user);

然后我设置了管理员签名权限(不确定是否需要)。然后将用户保存到 State Store。

等待fabric_client.setAdminSigningIdentity(admin_pk,admin_cert,admin_mspid);

等待fabric_client.saveUserToStateStore();

然后我console Logged了EventHub,很明显有clientContext存在。

{
  "_last_block_seen": null,
  "_starting_block_number": null,
  "_ending_block_number": null,
  "_ending_block_seen": false,
  "_ending_block_newest": false,
  "_allowRegistration": true,
  "_start_stop_registration": null,
  "_chaincodeRegistrants": {},
  "_block_registrant_count": 0,
  "_blockRegistrations": {},
  "connectCallback": null,
  "_transactionRegistrations": {},
  "_event_client": null,
  "_stream": null,
  "_connected": false,
  "_connect_running": false,
  "_disconnect_running": false,
  "_filtered_stream": true,
  "_current_stream": 0,
  "_clientContext": {
    "name": "basic-network",
    "version": "1.0.0",
    "client": {
      "organization": "Org1",
      "connection": {
        "timeout": {
          "peer": {
            "endorser": "300"
          },
          "orderer": "300"
        }
      }
    },
    "channels": {
      "mychannel": {
        "orderers": [
          "orderer.example.com"
        ],
        "peers": {
          "peer0.org1.example.com": {}
        }
      }
    },
    "organizations": {
      "Org1": {
        "mspid": "Org1MSP",
        "peers": [
          "peer0.org1.example.com"
        ],
        "certificateAuthorities": [
          "ca.example.com"
        ]
      },
      "Org2": {
        "mspid": "Org2MSP",
        "peers": [
          "peer0.org2.example.com"
        ],
        "certificateAuthorities": [
          "ca.example.com"
        ]
      }
    },
    "orderers": {
      "orderer.example.com": {
        "url": "grpc://localhost:7050"
      }
    },
    "peers": {
      "peer0.org1.example.com": {
        "url": "grpc://localhost:7051"
      },
      "peer0.org2.example.com": {
        "url": "grpc://localhost:7052"
      }
    },
    "certificateAuthorities": {
      "ca.example.com": {
        "url": "http://localhost:7054",
        "caName": "ca.example.com"
      }
    }
  },
  "_channel": {
    "_name": "mychannel",
    "_channel_peers": {},
    "_anchor_peers": [],
    "_orderers": {},
    "_kafka_brokers": [],
    "_clientContext": {
      "name": "basic-network",
      "version": "1.0.0",
      "client": {
        "organization": "Org1",
        "connection": {
          "timeout": {
            "peer": {
              "endorser": "300"
            },
            "orderer": "300"
          }
        }
      },
      "channels": {
        "mychannel": {
          "orderers": [
            "orderer.example.com"
          ],
          "peers": {
            "peer0.org1.example.com": {}
          }
        }
      },
      "organizations": {
        "Org1": {
          "mspid": "Org1MSP",
          "peers": [
            "peer0.org1.example.com"
          ],
          "certificateAuthorities": [
            "ca.example.com"
          ]
        },
        "Org2": {
          "mspid": "Org2MSP",
          "peers": [
            "peer0.org2.example.com"
          ],
          "certificateAuthorities": [
            "ca.example.com"
          ]
        }
      },
      "orderers": {
        "orderer.example.com": {
          "url": "grpc://localhost:7050"
        }
      },
      "peers": {
        "peer0.org1.example.com": {
          "url": "grpc://localhost:7051"
        },
        "peer0.org2.example.com": {
          "url": "grpc://localhost:7052"
        }
      },
      "certificateAuthorities": {
        "ca.example.com": {
          "url": "http://localhost:7054",
          "caName": "ca.example.com"
        }
      }
    },
    "_msp_manager": {
      "_msps": {}
    },
    "_discovery_interests": {},
    "_discovery_results": null,
    "_last_discover_timestamp": null,
    "_discovery_peer": null,
    "_use_discovery": false,
    "_as_localhost": true,
    "_endorsement_handler": null,
    "_commit_handler": null
  },
  "_peer": {
    "_options": {
      "grpc.max_receive_message_length": -1,
      "grpc.max_send_message_length": -1
    },
    "_url": "grpc://localhost:7051",
    "_endpoint": {
      "addr": "localhost:7051",
      "creds": {}
    },
    "_name": "localhost:7051",
    "_request_timeout": 45000,
    "_grpc_wait_for_ready_timeout": 3000,
    "_endorserClient": {
      "$interceptors": [],
      "$interceptor_providers": [],
      "$channel": {}
    },
    "_discoveryClient": {
      "$interceptors": [],
      "$interceptor_providers": [],
      "$channel": {}
    }
  }
}

但是,当我尝试使用 EventHub.connect(true) 进行连接时,我仍然收到以下错误。

Failed to submit transaction: Error: Error connect the ChannelEventhub to peer, either the clientContext has not been properly initialized, missing userContext or admin identity or missing signedEvent

我不确定这是否是解决方案,但在我这边,它有效并且有细微差别。

我没有将对等点的名称传递给事件中心,而是传递了对等点对象:

var peer = fabric_client.newPeer(creds.peers['org1-peer1'].url, { pem: creds.peers['org1-peer1'].tlsCACerts.pem, 'ssl-target-name-override': null });
var channel = fabric_client.newChannel('defaultchannel');
let event_hub = channel.newChannelEventHub(peer);

注意:creds是包含连接配置文件aas的对象json。

此处提供完整示例:https://github.com/IBM/car-auction-network-fabric-node-sdk

我使用了@david_k 的方法并实现了 ChaincodeEventEmitter Class 并且仅使用结构网络 API 创建了通道。

    const walletPath = path.join(process.cwd(), 'wallet');
    const wallet = new FileSystemWallet(walletPath);
    const useDiscovery = false;
    const convertDiscoveredToLocalHost = null;
    const userExists = await wallet.exists('user1');
        if (!userExists) {
            console.log('An identity for the user "user1" does not exist in the wallet');
            console.log('Run the registerUser.js application before retrying');
            return;
        }
    const gateway = new Gateway();
    const discoveryOptions = {
            enabled: useDiscovery
        };
    if (useDiscovery && convertDiscoveredToLocalHost !== null) {
            discoveryOptions.asLocalhost = convertDiscoveredToLocalHost;
     }

    await gateway.connect(ccp, {
            wallet,
            identity: 'user1',
            discovery: discoveryOptions
    });

    const network = await gateway.getNetwork('mychannel');
    const chaincodeEventEmitter = new ChaincodeEventEmitter(network, 'Org1MSP', 'Fabcae');
    await chaincodeEventEmitter.initialize();

    chaincodeEventEmitter.on('eventName', async (event) => {
            console.log('Event Received');
            console.log('Received Event --> ', event);

    });

    await gateway.disconnect();

并且我对 ChaincodeEventEmitter Class 使用了与此处相同的代码 https://github.com/davidkel/bnaport/blob/master/trade-network/native/client-new-js/chaincodeeventemitter.js

再次@david_k谢谢!!!