问题描述
这是我先前提出的问题的后续问题(),我正在尝试实施@david_k建议的解决方案.
This is a follow-up Question to the one which I asked previously(here) and I am trying to implement the solution suggested by @david_k.
我以Fabcar示例为基础,并以此为基础.
I am using the Fabcar example as a base and building up on that.
我运行了startFabric.sh,并收到消息已成功提交提案以加入频道"
I ran the startFabric.sh, I got the message that "Successfully submitted proposal to join channel"
2019-03-11 08:54:51.857 UTC [channelCmd] InitCmdFactory->信息001 代言人和订购者的连接已初始化
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->信息002 收到的块:0
2019-03-11 08:54:52.076 UTC [cli.common] readBlock -> INFO 002 Received block: 0
将peer0.org1.example.com加入该频道.
Join peer0.org1.example.com to the channel.
docker exec -e"CORE_PEER_LOCALMSPID = Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/[email protected]/msp" peer0.org1.example.com对等频道加入-b mychannel.block
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/[email protected]/msp" peer0.org1.example.com peer channel join -b mychannel.block
2019-03-11 08:54:52.387 UTC [channelCmd] InitCmdFactory->信息001 代言人和订购者的连接已初始化
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->信息002 成功提交了加入频道的提案
2019-03-11 08:54:52.481 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
然后,我尝试使用Fabric Node SDK(fabric-client)从"Channel"类初始化channelEventHub,以订阅从链码中发出的事件.
Then I try to initialize the channelEventHub from the 'Channel' Class using the Fabric Node SDK (fabric-client), to subscribe to events emitted from my chaincode.
const channel = new Channel('mychannel',ccp);
const channelEventHub = channel.getChannelEventHub('peer0.org1.example.com')
我收到以下错误:
此后,我尝试使用Channel Object的getPeers()函数,但是得到了一个空数组,否则该数组应该返回在通道上注册的对等项的数组.
After which I tried using getPeers() function from Channel Object but got an empty array, which otherwise should have returned an array of peers registered on the channel.
const channel = new Channel('mychannel',ccp);
let peers = channel.getChannelPeers();
我通过控制台登录了对等节点,并且返回了[].
I Console logged peers and it returned [].
我的连接配置文件JSON如下(它是基本网络中的一个,我没有更改.)
My Connection Profile JSON is as follows (Its the one in Basic Network, I haven't changed it.)
在这里您可以清楚地看到在通道中定义了对等方.
Here you can clearly see that the peer is defined in the channel.
我不明白为什么会出现此错误.任何有关如何解决此问题的指针将不胜感激.
I am not understanding why I am getting this error. Any pointers on how to fix this will be hugely appreciated.
我创建了一个StateStore类,该类实现了fabric_client.IKeyValueStore
接口
I created a StateStore class Which Implements the fabric_client.IKeyValueStore
Interface
实例化.然后使用
await fabric_client.setStateStore(stateStore);
此后,我创建了一个用户并使用以下命令设置User上下文
After which I created a user and set the User context using the following commands
let user = await fabric_client.createUser(userOpts);
// Set the userContext by passing the User Context
await fabric_client.setUserContext(user);
然后,我设置了管理员签名授权机构(不确定是否需要).然后将用户保存到状态存储中.
Then I set the Admin Signing Authority (Not sure if it's needed). Then saved the user to the State Store.
等待fabric_client.setAdminSigningIdentity(admin_pk,admin_cert,admin_mspid);
等待fabric_client.saveUserToStateStore();
await fabric_client.saveUserToStateStore();
然后,我控制台登录了EventHub,它清楚地表明存在一个clientContext.
Then I console Logged the EventHub, which clearly shows that there is a 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)
连接时,出现以下错误.
But still, when I try to connect using EventHub.connect(true)
, I get the following error.
推荐答案
我使用@david_k的方法并实现了ChaincodeEventEmitter类,仅使用结构网络API来创建通道.
I used @david_k 's approach and implemented the ChaincodeEventEmitter Class and just used the fabric-network API's to create the channel.
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类使用了与此处相同的代码 https://github.com/davidkel/bnaport/blob/master/trade-network/native/client-new-js/chaincodeeventemitter.js .
And I used the same code for the ChaincodeEventEmitter Class as here https://github.com/davidkel/bnaport/blob/master/trade-network/native/client-new-js/chaincodeeventemitter.js.
再一次@david_k谢谢!!!
Once again @david_k Thank you!!!
这篇关于Hyperledger Fabric 1.4:尝试连接ChannelEventHub.connect(true)时出错.错误:将ChannelEventhub连接到同级时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!