Hyperledger Fabric 1.4:尝试连接ChannelEventHub.connect时出错(true)。错误:将ChannelEventhub连接到peer时出错

问题描述 投票:0回答:2

这是我之前询问过的问题的后续问题(here),我正在尝试实施@david_k建议的解决方案。

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

我运行了startFabric.sh,收到了“已成功提交加入渠道提案”的消息

docker exec -e“CORE_PEER_LOCALMSPID = Org1MSP”-e“CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/[email protected]/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和orderer连接已初始化

2019-03-11 08:54:52.076 UTC [cli.common] readBlock - > INFO 002收到栏:0

将peer0.org1.example.com加入频道。

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 - > INFO 001 Endorser和orderer连接已初始化

2019-03-11 08:54:52.481 UTC [channelCmd] executeJoin - > INFO 002成功提交加入渠道的提案

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

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

我收到以下错误:

无法提交事务:错误:无法发现::错误:名称为“peer1.org1.example.com”的对等方未分配给此通道

之后我尝试使用Channel Object中的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 “ ”对等体“:[ ”peer0.org2.example.com“], ”certificateAuthorities“:[” CA。 example.com “]}},” orderers “:{” orderer.example.com “:{” URL “:” GRPC://本地主机:7050 “}},” 对等体 “:{” peer0.org1.example。 COM “:{” URL “:” GRPC://本地主机:7051 “},” peer0.org2.example.com “:{” URL “:” GRPC://本地主机:7052 “},” certificateAuthorities“: { “ca.example.com”:{ “URL”: “http://localhost:7054”, “caName”: “ca.example.com”}}}

在这里,您可以清楚地看到对等体是在通道中定义的。

我不明白为什么我会收到这个错误。关于如何解决这个问题的任何指示都将非常感激。

更新:

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

实例化它。然后使用设置状态存储

await fabric_client.setStateStore(stateStore);

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

    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);

await fabric_client.saveUserToStateStore();

然后我控制Logged the 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)连接时,我收到以下错误。

提交事务失败:错误:将ChannelEventhub连接到peer时出错,clientContext未正确初始化,缺少userContext或admin身份或缺少signedEvent

node.js hyperledger-fabric blockchain hyperledger-fabric-sdk-js
2个回答
0
投票

我不确定这是否是解决方案,但在我看来,它有效并且有一点不同。

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

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


0
投票

我使用了@david_k的方法并实现了ChaincodeEventEmitter类,并且只使用了结构网络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类相同的代码https://github.com/davidkel/bnaport/blob/master/trade-network/native/client-new-js/chaincodeeventemitter.js

再次@david_k谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.