Hyperledger fabric 在多个节点上的Hyperledger结构区块链上执行链码时,gRPC失败
我想在多个节点上运行我自己的hyperledger区块链 我有以下设置: 我的Ubuntu PC上有一个订购者、对等者、沙发数据库和CA服务器 我的Raspberry Pi(运行UbuntuMate)正在托管:一个对等机和一个沙发数据库 这两个节点都在我的docker swarm组中。(我用于设置和在RPi上运行结构。) 我成功地创建了一个频道。订购方和两个对等方都能够加入。然后,我使用命令行界面中的Hyperledger fabric 在多个节点上的Hyperledger结构区块链上执行链码时,gRPC失败,hyperledger-fabric,hyperledger,grpc,hyperledger-fabric-ca,grpc-node,Hyperledger Fabric,Hyperledger,Grpc,Hyperledger Fabric Ca,Grpc Node,我想在多个节点上运行我自己的hyperledger区块链 我有以下设置: 我的Ubuntu PC上有一个订购者、对等者、沙发数据库和CA服务器 我的Raspberry Pi(运行UbuntuMate)正在托管:一个对等机和一个沙发数据库 这两个节点都在我的docker swarm组中。(我用于设置和在RPi上运行结构。) 我成功地创建了一个频道。订购方和两个对等方都能够加入。然后,我使用命令行界面中的peer chaincode工具安装并实例化了chaincode。我还用一些值初始化了我的分类账
peer chaincode
工具安装并实例化了chaincode。我还用一些值初始化了我的分类账
如果在命令行界面docker映像中执行以下命令:
peer chaincode query -o orderer.example.com:7050 -C mychannel -n v2x_sc -c '{"function":"queryAllVehicles","Args":[""]}'
它成功地从分类账返回所有数据,无论我是在RPi还是PC节点上执行cmd
我面临的问题如下:
只要我想通过Hyperledger Fabric SDK运行相同的查询,它仍然会返回一些grpc错误
Javascript SDK:
下面是我执行的query.js(它源于
以下行:返回频道。queryByChaincode(请求);
返回错误:查询错误=错误:在截止日期之前连接失败
我在谷歌上搜索了这个错误的根源,发现这可能是grpc的一个短暂故障,因此他们关闭了它,直到有人再次体验到它
执行:
query.js:
因为我真的很绝望,所以我尝试在JavaSDK中使用同样的方法
我在运行它时遇到了类似的错误。
此行:channel.initialize();
导致以下错误:
错误通道-向peer0.org1.example.com发送建议失败,原因是:gRPC失败=状态{code=UNKNOWN,description=access denied:Channel[mychannel]creator org[Org1MSP],cause=null}
异常跟踪如下所示:
ERROR Channel - Sending proposal to peer0.org1.example.com failed because of: gRPC failure=Status{code=UNKNOWN, description=access denied: channel [mychannel] creator org [Org1MSP], cause=null}
java.lang.Exception: io.grpc.StatusRuntimeException: UNKNOWN: access denied: channel [mychannel] creator org [Org1MSP]
at org.hyperledger.fabric.sdk.Channel.sendProposalToPeers(Channel.java:3619)
at org.hyperledger.fabric.sdk.Channel.getConfigBlock(Channel.java:834)
at org.hyperledger.fabric.sdk.Channel.parseConfigBlock(Channel.java:1759)
at org.hyperledger.fabric.sdk.Channel.loadCACertificates(Channel.java:1596)
at org.hyperledger.fabric.sdk.Channel.initialize(Channel.java:1083)
at Main.getChannel(Main.java:119)
at Main.main(Main.java:65)
Main.java:
import org.apache.log4j.Logger;
import org.hyperledger.fabric.sdk.*;
import org.hyperledger.fabric.sdk.exception.CryptoException;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.hyperledger.fabric.sdk.exception.TransactionException;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import org.hyperledger.fabric_ca.sdk.HFCAClient;
import org.hyperledger.fabric_ca.sdk.RegistrationRequest;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.PrivateKey;
import java.util.Collection;
import java.util.Properties;
/**
* <h1>HFJavaSDKBasicExample</h1>
* <p>
* Simple example showcasing basic fabric-ca and fabric actions.
* The demo required fabcar fabric up and running.
* <p>
* The demo shows
* <ul>
* <li>connecting to fabric-ca</li>
* <li>enrolling admin to get new key-pair, certificate</li>
* <li>registering and enrolling a new user using admin</li>
* <li>creating HF client and initializing channel</li>
* <li>invoking chaincode query</li>
* </ul>
*
*/
public class Main {
private static final Logger log = Logger.getLogger(Main.class);
public static final String CERT_FOLDER_PATH = "/home/filip/masterthesis/prototype/features_analyser/hyperledger-fabric/Build-Multi-Host-Network-Hyperledger/v2x_java/hfc-key-store";
public static void main(String[] args) throws Exception {
// create fabric-ca client
HFCAClient caClient = getHfCaClient("http://localhost:7054", null);
// enroll or load admin
AppUser admin = getAdmin(caClient);
log.info(admin);
// register and enroll new user
AppUser appUser = getUser(caClient, admin, "hfuser");
log.info(appUser);
// get HFC client instance
HFClient client = getHfClient();
// set user context
client.setUserContext(admin);
// get HFC channel using the client
Channel channel = getChannel(client);
log.info("Channel: " + channel.getName());
// call query blockchain example
queryBlockChain(client);
}
/**
* Invoke blockchain query
*
* @param client The HF Client
* @throws ProposalException
* @throws InvalidArgumentException
*/
static void queryBlockChain(HFClient client) throws ProposalException, InvalidArgumentException {
// get channel instance from client
Channel channel = client.getChannel("mychannel");
// create chaincode request
QueryByChaincodeRequest qpr = client.newQueryProposalRequest();
// build cc id providing the chaincode name. Version is omitted here.
ChaincodeID fabcarCCId = ChaincodeID.newBuilder().setName("v2x_sc").build();
qpr.setChaincodeID(fabcarCCId);
// CC function to be called
qpr.setFcn("queryAllVehicles");
Collection<ProposalResponse> res = channel.queryByChaincode(qpr);
// display response
for (ProposalResponse pres : res) {
String stringResponse = new String(pres.getChaincodeActionResponsePayload());
log.info(stringResponse);
}
}
/**
* Initialize and get HF channel
*
* @param client The HFC client
* @return Initialized channel
* @throws InvalidArgumentException
* @throws TransactionException
*/
static Channel getChannel(HFClient client) throws InvalidArgumentException, TransactionException {
// initialize channel
// peer name and endpoint in fabcar network
Peer peer = client.newPeer("peer0.org1.example.com", "grpc://192.168.1.24:8051");
// eventhub name and endpoint in fabcar network
EventHub eventHub = client.newEventHub("eventhub01", "grpc://localhost:7053");
// orderer name and endpoint in fabcar network
Orderer orderer = client.newOrderer("orderer.example.com", "grpc://localhost:7050");
// channel name in fabcar network
Channel channel = client.newChannel("mychannel");
channel.addPeer(peer);
channel.addEventHub(eventHub);
channel.addOrderer(orderer);
channel.initialize();
return channel;
}
/**
* Create new HLF client
*
* @return new HLF client instance. Never null.
* @throws CryptoException
* @throws InvalidArgumentException
*/
static HFClient getHfClient() throws Exception {
// initialize default cryptosuite
CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();
// setup the client
HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(cryptoSuite);
return client;
}
/**
* Register and enroll user with userId.
* If AppUser object with the name already exist on fs it will be loaded and
* registration and enrollment will be skipped.
*
* @param caClient The fabric-ca client.
* @param registrar The registrar to be used.
* @param userId The user id.
* @return AppUser instance with userId, affiliation,mspId and enrollment set.
* @throws Exception
*/
static AppUser getUser(HFCAClient caClient, AppUser registrar, String userId) throws Exception {
AppUser appUser = tryDeserialize(userId);
if (appUser == null) {
RegistrationRequest rr = new RegistrationRequest(userId, "org1");
String enrollmentSecret = caClient.register(rr, registrar);
Enrollment enrollment = caClient.enroll(userId, enrollmentSecret);
appUser = new AppUser(userId, "org1", "Org1MSP", enrollment);
serialize(appUser);
}
return appUser;
}
/**
* Enroll admin into fabric-ca using {@code admin/adminpw} credentials.
* If AppUser object already exist serialized on fs it will be loaded and
* new enrollment will not be executed.
*
* @param caClient The fabric-ca client
* @return AppUser instance with userid, affiliation, mspId and enrollment set
* @throws Exception
*/
static AppUser getAdmin(HFCAClient caClient) throws Exception {
AppUser admin = tryDeserialize("admin");
if (admin == null) {
Enrollment adminEnrollment = caClient.enroll("admin", "adminpw");
admin = new AppUser("admin", "org1", "Org1MSP", adminEnrollment);
serialize(admin);
}
return admin;
}
/**
* Get new fabic-ca client
*
* @param caUrl The fabric-ca-server endpoint url
* @param caClientProperties The fabri-ca client properties. Can be null.
* @return new client instance. never null.
* @throws Exception
*/
static HFCAClient getHfCaClient(String caUrl, Properties caClientProperties) throws Exception {
CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();
HFCAClient caClient = HFCAClient.createNewInstance(caUrl, caClientProperties);
caClient.setCryptoSuite(cryptoSuite);
return caClient;
}
// user serialization and deserialization utility functions
// files are stored in the base directory
/**
* Serialize AppUser object to file
*
* @param appUser The object to be serialized
* @throws IOException
*/
static void serialize(AppUser appUser) throws IOException {
try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(
Paths.get(appUser.getName() + ".jso")))) {
oos.writeObject(appUser);
}
}
/**
* Deserialize AppUser object from file
*
* @param name The name of the user. Used to build file name ${name}.jso
* @return
* @throws Exception
*/
static AppUser tryDeserialize(String name) throws Exception {
if (Files.exists(Paths.get(name + ".jso"))) {
return deserialize(name);
}
return null;
}
static AppUser deserialize(String name) throws Exception {
try (ObjectInputStream decoder = new ObjectInputStream(
Files.newInputStream(Paths.get(name + ".jso")))) {
return (AppUser) decoder.readObject();
}
}
}
import org.apache.log4j.Logger;
导入org.hyperledger.fabric.sdk.*;
导入org.hyperledger.fabric.sdk.exception.CryptoException;
导入org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
导入org.hyperledger.fabric.sdk.exception.ProposalException;
导入org.hyperledger.fabric.sdk.exception.TransactionException;
导入org.hyperledger.fabric.sdk.security.CryptoSuite;
导入org.hyperledger.fabric_ca.sdk.HFCAClient;
导入org.hyperledger.fabric_ca.sdk.RegistrationRequest;
导入java.io.IOException;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutputStream;
导入java.nio.file.Files;
导入java.nio.file.path;
导入java.security.PrivateKey;
导入java.util.Collection;
导入java.util.Properties;
/**
*HFJavaSDKBasiceExample
*
*展示基本结构ca和结构动作的简单示例。
*演示需要fabcar织物启动并运行。
*
*演示显示
*
*- 连接到结构ca
*- 注册管理员以获取新密钥对、证书
*- 使用admin注册和注册新用户
*- 创建HF客户端并初始化通道
*- 调用链码查询
*
*
*/
公共班机{
私有静态最终记录器log=Logger.getLogger(Main.class);
公共静态最终字符串CERT_FOLDER_PATH=“/home/filip/masterthesis/prototype/features_analyzer/hyperledger fabric/Build多主机网络hyperledger/v2x_java/hfc密钥存储”;
公共静态void main(字符串[]args)引发异常{
//创建结构ca客户端
HFCAClient caClient=getHfCaClient(“http://localhost:7054“,空);
//注册或加载管理员
AppUser admin=getAdmin(caClient);
日志信息(管理员);
//注册并注册新用户
AppUser AppUser=getUser(caClient,admin,“hfuser”);
log.info(appUser);
//获取HFC客户端实例
HFClient=getHfClient();
//设置用户上下文
setUserContext(admin);
//使用客户端获取HFC通道
Channel=getChannel(客户端);
log.info(“通道:+Channel.getName());
//调用查询区块链示例
查询锁链(客户端);
}
/**
*调用区块链查询
*
*@param客户机HF客户机
*@1.2.2.2.1.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2
*@InvalidArgumentException
*/
静态无效queryBlockChain(HFClient客户端)抛出ProposalException、InvalidArgumentException{
//从客户端获取通道实例
Channel=client.getChannel(“mychannel”);
//创建链码请求
QueryByChaincodeRequest qpr=client.newQueryProposalRequest();
//生成cc id,提供链码名称。此处省略版本。
ChaincodeID fabcarCCId=ChaincodeID.newBuilder().setName(“v2x_sc”).build();
qpr.setChaincodeID(fabcarCCId);
//要调用的CC函数
qpr.setFcn(“查询车辆”);
Collection res=channel.queryByChaincode(qpr);
//显示响应
用于(建议响应压力:res){
String stringResponse=新字符串(pres.getChaincodeActionResponsePayload());
日志信息(stringResponse);
}
}
/**
*初始化并获取高频通道
*
*@param客户端HFC客户端
*@return初始化通道
*@InvalidArgumentException
*@Trows TransactionException
*/
静态通道getChannel(HFClient客户端)抛出InvalidArgumentException、TransactionException{
//初始化通道
//fabcar网络中的节点名称和端点
Peer=client.newPeer(“peer0.org1.example.com”)grpc://192.168.1.24:8051");
//fabcar网络中的eventhub名称和端点
EventHub EventHub=client.newEventHub(“eventhub01”,”grpc://localhost:7053");
//fabcar网络中的订购方名称和端点
命令
ERROR Channel - Sending proposal to peer0.org1.example.com failed because of: gRPC failure=Status{code=UNKNOWN, description=access denied: channel [mychannel] creator org [Org1MSP], cause=null}
java.lang.Exception: io.grpc.StatusRuntimeException: UNKNOWN: access denied: channel [mychannel] creator org [Org1MSP]
at org.hyperledger.fabric.sdk.Channel.sendProposalToPeers(Channel.java:3619)
at org.hyperledger.fabric.sdk.Channel.getConfigBlock(Channel.java:834)
at org.hyperledger.fabric.sdk.Channel.parseConfigBlock(Channel.java:1759)
at org.hyperledger.fabric.sdk.Channel.loadCACertificates(Channel.java:1596)
at org.hyperledger.fabric.sdk.Channel.initialize(Channel.java:1083)
at Main.getChannel(Main.java:119)
at Main.main(Main.java:65)
import org.apache.log4j.Logger;
import org.hyperledger.fabric.sdk.*;
import org.hyperledger.fabric.sdk.exception.CryptoException;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.hyperledger.fabric.sdk.exception.TransactionException;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import org.hyperledger.fabric_ca.sdk.HFCAClient;
import org.hyperledger.fabric_ca.sdk.RegistrationRequest;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.PrivateKey;
import java.util.Collection;
import java.util.Properties;
/**
* <h1>HFJavaSDKBasicExample</h1>
* <p>
* Simple example showcasing basic fabric-ca and fabric actions.
* The demo required fabcar fabric up and running.
* <p>
* The demo shows
* <ul>
* <li>connecting to fabric-ca</li>
* <li>enrolling admin to get new key-pair, certificate</li>
* <li>registering and enrolling a new user using admin</li>
* <li>creating HF client and initializing channel</li>
* <li>invoking chaincode query</li>
* </ul>
*
*/
public class Main {
private static final Logger log = Logger.getLogger(Main.class);
public static final String CERT_FOLDER_PATH = "/home/filip/masterthesis/prototype/features_analyser/hyperledger-fabric/Build-Multi-Host-Network-Hyperledger/v2x_java/hfc-key-store";
public static void main(String[] args) throws Exception {
// create fabric-ca client
HFCAClient caClient = getHfCaClient("http://localhost:7054", null);
// enroll or load admin
AppUser admin = getAdmin(caClient);
log.info(admin);
// register and enroll new user
AppUser appUser = getUser(caClient, admin, "hfuser");
log.info(appUser);
// get HFC client instance
HFClient client = getHfClient();
// set user context
client.setUserContext(admin);
// get HFC channel using the client
Channel channel = getChannel(client);
log.info("Channel: " + channel.getName());
// call query blockchain example
queryBlockChain(client);
}
/**
* Invoke blockchain query
*
* @param client The HF Client
* @throws ProposalException
* @throws InvalidArgumentException
*/
static void queryBlockChain(HFClient client) throws ProposalException, InvalidArgumentException {
// get channel instance from client
Channel channel = client.getChannel("mychannel");
// create chaincode request
QueryByChaincodeRequest qpr = client.newQueryProposalRequest();
// build cc id providing the chaincode name. Version is omitted here.
ChaincodeID fabcarCCId = ChaincodeID.newBuilder().setName("v2x_sc").build();
qpr.setChaincodeID(fabcarCCId);
// CC function to be called
qpr.setFcn("queryAllVehicles");
Collection<ProposalResponse> res = channel.queryByChaincode(qpr);
// display response
for (ProposalResponse pres : res) {
String stringResponse = new String(pres.getChaincodeActionResponsePayload());
log.info(stringResponse);
}
}
/**
* Initialize and get HF channel
*
* @param client The HFC client
* @return Initialized channel
* @throws InvalidArgumentException
* @throws TransactionException
*/
static Channel getChannel(HFClient client) throws InvalidArgumentException, TransactionException {
// initialize channel
// peer name and endpoint in fabcar network
Peer peer = client.newPeer("peer0.org1.example.com", "grpc://192.168.1.24:8051");
// eventhub name and endpoint in fabcar network
EventHub eventHub = client.newEventHub("eventhub01", "grpc://localhost:7053");
// orderer name and endpoint in fabcar network
Orderer orderer = client.newOrderer("orderer.example.com", "grpc://localhost:7050");
// channel name in fabcar network
Channel channel = client.newChannel("mychannel");
channel.addPeer(peer);
channel.addEventHub(eventHub);
channel.addOrderer(orderer);
channel.initialize();
return channel;
}
/**
* Create new HLF client
*
* @return new HLF client instance. Never null.
* @throws CryptoException
* @throws InvalidArgumentException
*/
static HFClient getHfClient() throws Exception {
// initialize default cryptosuite
CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();
// setup the client
HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(cryptoSuite);
return client;
}
/**
* Register and enroll user with userId.
* If AppUser object with the name already exist on fs it will be loaded and
* registration and enrollment will be skipped.
*
* @param caClient The fabric-ca client.
* @param registrar The registrar to be used.
* @param userId The user id.
* @return AppUser instance with userId, affiliation,mspId and enrollment set.
* @throws Exception
*/
static AppUser getUser(HFCAClient caClient, AppUser registrar, String userId) throws Exception {
AppUser appUser = tryDeserialize(userId);
if (appUser == null) {
RegistrationRequest rr = new RegistrationRequest(userId, "org1");
String enrollmentSecret = caClient.register(rr, registrar);
Enrollment enrollment = caClient.enroll(userId, enrollmentSecret);
appUser = new AppUser(userId, "org1", "Org1MSP", enrollment);
serialize(appUser);
}
return appUser;
}
/**
* Enroll admin into fabric-ca using {@code admin/adminpw} credentials.
* If AppUser object already exist serialized on fs it will be loaded and
* new enrollment will not be executed.
*
* @param caClient The fabric-ca client
* @return AppUser instance with userid, affiliation, mspId and enrollment set
* @throws Exception
*/
static AppUser getAdmin(HFCAClient caClient) throws Exception {
AppUser admin = tryDeserialize("admin");
if (admin == null) {
Enrollment adminEnrollment = caClient.enroll("admin", "adminpw");
admin = new AppUser("admin", "org1", "Org1MSP", adminEnrollment);
serialize(admin);
}
return admin;
}
/**
* Get new fabic-ca client
*
* @param caUrl The fabric-ca-server endpoint url
* @param caClientProperties The fabri-ca client properties. Can be null.
* @return new client instance. never null.
* @throws Exception
*/
static HFCAClient getHfCaClient(String caUrl, Properties caClientProperties) throws Exception {
CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();
HFCAClient caClient = HFCAClient.createNewInstance(caUrl, caClientProperties);
caClient.setCryptoSuite(cryptoSuite);
return caClient;
}
// user serialization and deserialization utility functions
// files are stored in the base directory
/**
* Serialize AppUser object to file
*
* @param appUser The object to be serialized
* @throws IOException
*/
static void serialize(AppUser appUser) throws IOException {
try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(
Paths.get(appUser.getName() + ".jso")))) {
oos.writeObject(appUser);
}
}
/**
* Deserialize AppUser object from file
*
* @param name The name of the user. Used to build file name ${name}.jso
* @return
* @throws Exception
*/
static AppUser tryDeserialize(String name) throws Exception {
if (Files.exists(Paths.get(name + ".jso"))) {
return deserialize(name);
}
return null;
}
static AppUser deserialize(String name) throws Exception {
try (ObjectInputStream decoder = new ObjectInputStream(
Files.newInputStream(Paths.get(name + ".jso")))) {
return (AppUser) decoder.readObject();
}
}
}