0

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

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

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

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

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

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

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":{"背书人":"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":"com":{"url":"grpc://localhost:7051"},"peer0.org2.example.com":{"url":"grpc://localhost:7052"}},"certificateAuthorities": {"ca.example.com":{"url":"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"}}}

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

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

更新:

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

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

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

然后我控制台记录了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 连接到对等点时出错,clientContext 尚未正确初始化,缺少 userContext 或管理员身份或缺少 signedEvent

4

2 回答 2

0

我使用了@david_k 的方法并实现了 ChaincodeEventEmitter 类,并且只使用了 fabric-network 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 谢谢!!!

于 2019-03-14T08:58:31.443 回答
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

于 2019-03-11T11:50:10.660 回答