/*
* Copyright (c) 2018. Aberic - aberic@qq.com - All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.aberic.fabric.sdk;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.protobuf.InvalidProtocolBufferException;
import org.apache.commons.codec.binary.Hex;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hyperledger.fabric.protos.ledger.rwset.kvrwset.KvRwset;
import org.hyperledger.fabric.sdk.*;
import org.hyperledger.fabric.sdk.BlockInfo.EnvelopeInfo;
import org.hyperledger.fabric.sdk.BlockInfo.EnvelopeType;
import org.hyperledger.fabric.sdk.BlockInfo.TransactionEnvelopeInfo;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.hyperledger.fabric.sdk.exception.TransactionException;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
/**
* 描述:中继频道对象
*
* @author : Aberic 【2018/5/17 18:25】
*/
class IntermediateChannel {
private Logger log = LogManager.getLogger(IntermediateChannel.class);
/** 当前将要访问的智能合约所属频道名称 */
private String channelName; // myChannel
// /** 事务等待时间以秒为单位 */
// private int transactionWaitTime = 100000;
// /** 部署等待时间以秒为单位 */
// private int deployWatiTime = 120000;
/** 中继组织节点 */
private IntermediateOrg org;
private Channel channel;
void init(IntermediateOrg org) throws TransactionException, InvalidArgumentException {
this.org = org;
setChannel(org.getClient());
}
private void setChannel(HFClient client) throws InvalidArgumentException, TransactionException {
client.setUserContext(org.getUser(org.getUsername()));
// client.setUserContext(org.getUser());
channel = client.newChannel(channelName);
log.info("Get Chain " + channelName);
int sizeOrderers = org.getOrderers().size();
for (int i = 0; i < sizeOrderers; i++) {
Properties ordererProperties = new Properties();
if (org.openTLS()) {
File ordererCert = new File(org.getOrderers().get(i).getServerCrtPath());
File ordererUserClientCert = new File(org.getOrderers().get(i).getClientCertPath());
File ordererUserClientKey = new File(org.getOrderers().get(i).getClientKeyPath());
if (!ordererCert.exists()) {
throw new RuntimeException(
String.format("Missing cert file for: %s. Could not find at location: %s", org.getOrderers().get(i).getOrdererName(), ordererCert.getAbsolutePath()));
}
ordererProperties.setProperty("pemFile", ordererCert.getAbsolutePath());
ordererProperties.setProperty("clientCertFile", ordererUserClientCert.getAbsolutePath());
ordererProperties.setProperty("clientKeyFile", ordererUserClientKey.getAbsolutePath());
}
ordererProperties.setProperty("hostnameOverride", org.getOrderers().get(i).getOrdererName());
ordererProperties.setProperty("sslProvider", "openSSL");
ordererProperties.setProperty("negotiationType", "TLS");
// 设置keepAlive以避免在不活跃的http2连接上超时的例子。在5分钟内,需要对服务器端进行更改,以接受更快的ping速率。
ordererProperties.put("grpc.NettyChannelBuilderOption.keepAliveTime", new Object[]{5L, TimeUnit.MINUTES});
ordererProperties.put("grpc.NettyChannelBuilderOption.keepAliveTimeout", new Object[]{8L, TimeUnit.SECONDS});
ordererProperties.put("grpc.NettyChannelBuilderOption.keepAliveWithoutCalls", new Object[] {true});
channel.addOrderer(
client.newOrderer(org.getOrderers().get(i).getOrdererName(), org.getOrderers().get(i).getOrdererLocation(), ordererProperties));
}
int sizePeer = org.getPeers().size();
for (int i = 0; i < sizePeer; i++) {
Properties peerProperties = new Properties();
if (org.openTLS()) {
File peerCert = new File(org.getPeers().get(i).getServerCrtPath());
File peerUserClientCert = new File(org.getPeers().get(i).getClientCertPath());
File peerUserClientKey = new File(org.getPeers().get(i).getClientKeyPath());
if (!peerCert.exists()) {
throw new RuntimeException(
String.format("Missing cert file for: %s. Could not find at location: %s", org.getPeers().get(i).getPeerName(), peerCert.getAbsolutePath()));
}
peerProperties.setProperty("pemFile", peerCert.getAbsolutePath());
peerProperties.setProperty("clientCertFile", peerUserClientCert.getAbsolutePath());
peerProperties.setProperty("clientKeyFile", peerUserClientKey.getAbsolutePath());
}
// ret.setProperty("trustServerCertificate", "true"); //testing
// environment only NOT FOR PRODUCTION!
peerProperties.setProperty("hostnameOverride", org.getPeers().get(i).getPeerName());
peerProperties.setProperty("sslProvider", "openSSL");
peerProperties.setProperty("negotiationType", "TLS");
// 在grpc的NettyChannelBuilder上设置特定选项
peerProperties.put("grpc.NettyChannelBuilderOption.maxInboundMessageSize", 9000000);
// 如果未加入频道,该方法执行加入。如果已加入频道,则执行下一行方面新增Peer
// channel.joinPeer(client.newPeer(peers.get().get(i).getPeerName(), fabricOrg.getPeerLocation(peers.get().get(i).getPeerName()), peerProperties));
channel.addPeer(client.newPeer(org.getPeers().get(i).getPeerName(), org.getPeers().get(i).getPeerLocation(), peerProperties));
if (null != org.getPeers().get(i).getPeerEventHubLocation() && !org.getPeers().get(i).getPeerEventHubLocation().isEmpty()) {
channel.addEventHub(client.newEventHub(org.getPeers().get(i).getPeerName(), org.getPeers().get(i).getPeerEventHubLocation(), peerProperties));
}
}
log.info("channel.isInitialized() = " + channel.isInitialized());
if (!channel.isInitialized()) {
channel.initialize();
}
log.info(String.format("channel.isInitialized() = %s", channel.isInitialized()));
if (null != org.getBlockListener()) {
// channel.registerBlockListener(org.getBlockListener());
channel.registerBlockListener(blockEvent -> {
try {
org.getBlockListener().received(execBlockInfo(blockEvent));
} catch (Exception e) {
e.printStackTrace();
org.getBlockListener().received(getFailFromString(e.getMessage()));
}
});
}
if (null != org.getChaincodeEventListener()) {
String[] eventNames = org.getEventNames().split(",");
for (String eventName : eventNames) {
channel.registerChaincodeEven
没有合适的资源?快使用搜索试试~ 我知道了~
fabric-net-server-master.zip

共888个文件
pem:303个
java:125个
crt:114个

10 浏览量
2023-03-25
15:53:35
上传
评论
收藏 27.51MB ZIP 举报
温馨提示
FNS与一个已存在的HyperLedger Fabric网络进行交互,包括但不限于安装链码、实例化链码、升级链码以及对链码发起交易和查询等功能。
资源推荐
资源详情
资源评论




















收起资源包目录





































































































共 888 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
资源评论


m0_72731342
- 粉丝: 1
- 资源: 1833
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


会员权益专享
最新资源
- ChatGPT开发食品安全客诉分析系统,附加代码实现.docx
- TACRED数据集下载
- JSP+SQL网上选课系统的设计与实现(源代码+毕设+答辩PPT).zip
- 基于springboot的沁园健身房预约管理系统的设计与实现
- Scratch作品:Codecraft基岩版
- 基于springboot的时间管理系统的设计与实现PPT
- jsp+sql网络书店销售管理系统的设计与实现(毕设+源代码+任务书+开题报告+中期检查表+摘要+英文文献).zip
- 【openEuler 20.03 TLS编译openGauss2.0.0源码】
- 小型电子感应加速器电磁场的设计与实现_杨志强.caj
- 中期答辩-周长玉.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
