java随记

坚持就是胜利!

 

开源区块链Hyperleger Fabric通过SDK-JAVA动态添加通道


在阅读本篇之前需要理解
configtxgen工具使用原理.基本上能跑通
开源区块链Hyperleger Fabric的SDK-JAVA新手上路指引

中的内容。可以参考http://www.blogjava.net/fool/archive/2018/02/01/433032.html.
如果知道通过命令行工具进行智能合约的安装测试就更好了。fabric中的channel可以理解为泳道。在这个泳道中可以布署
多个智能合约。当然也可以安装多个channel. 顺便多一嘴,在智能合约可以调用另一个channel中的智能合约中的查询方法.
通过sdk动态安装通道的步骤:
1,通过configtxgen命令生成交易通道初始文件.
configtxgen --profile TestSoft --outputCreateChannelTx testsoft.tx --channelID testsoft
2,下载testsoft.tx到本机.
3,sdk安装通道代码及解释
@Override
public boolean installChannel(String channelName) throws IOException, InvalidArgumentException, TransactionException, ProposalException {
// TODO Auto-generated method stub
 String configPath = this.config.getConfigPath();
                         //初始化 ChannelConfiguration 对象.参数是通道初始化文件路径
 ChannelConfiguration channelConfiguration = new ChannelConfiguration(new File(configPath + "/"+channelName + ".tx"));
  //构造orderder节点信息
   String ordererName = orderers.get().get(0).getOrdererName();
Properties ordererProperties = loadOrderProperty(ordererName);
Orderer anOrderer = client.newOrderer(
ordererName,
orderers.get().get(0).getOrdererLocation(), ordererProperties);
       //Create channel that has only one signer that is this orgs peer admin. If channel creation policy needed more signature they would need to be added too.
       Channel channel = client.newChannel(channelName, anOrderer, channelConfiguration, client.getChannelConfigurationSignature(channelConfiguration, fabricOrg.getPeerAdmin()));
                       //构造peer节点信息,并且将peer节点加入到通道中。peer节点加入通道后,才能通过该peer节点中访问通道中的智能合约
       for (com.ygsoft.fabric.bean.Peers.Peer p : config.getInstallPeers().get()) {
Properties peerProperties = this.loadPeerProperties(p.getPeerName());
Peer peer = client.newPeer(p.getPeerName(),p.getPeerLocation(),peerProperties);
channel.joinPeer(peer);
}
for (int i = 0; i < orderers.get().size(); i++) {
Properties ordererProperties2 = loadOrderProperty(ordererName);
channel.addOrderer(client.newOrderer(
orderers.get().get(i).getOrdererName(),
orderers.get().get(i)
.getOrdererLocation(), ordererProperties2));
}
log.debug("channel.isInitialized() = " + channel.isInitialized());
  channel.initialize();
 return true;
}

知道了手工命令行安装合约的过程,上面的理解还是比较简单的。安装通道后就可以调用sdk安装合约了。











posted on 2018-03-12 11:55 傻 瓜 阅读(2748) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(7)

我参与的团队

随笔分类

随笔档案

文章分类

友情链接

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜