挖个坑,以后填
源码下载于github
理解源码参考:https://blog.csdn.net/weixin_39291399/article/details/82191707
https://blog.csdn.net/yanhuibin315/article/details/84061079#SDK_97
https://www.liangzl.com/get-article-detail-125638.html
End2endIT源码及个人理解
不想直接修改End2endIT.java,就复制了一份命名End2endIT2.java并在这上面学习和修改
/*
* Copyright 2016, 2017 DTCC, Fujitsu Australia Software Technology, IBM - 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 org.hyperledger.fabric.sdkintegration;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.nio.file.Paths;
import java.security.PrivateKey;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.openssl.PEMWriter;
import org.hyperledger.fabric.protos.ledger.rwset.kvrwset.KvRwset;
import org.hyperledger.fabric.sdk.BlockEvent;
import org.hyperledger.fabric.sdk.BlockInfo;
import org.hyperledger.fabric.sdk.BlockchainInfo;
import org.hyperledger.fabric.sdk.ChaincodeEndorsementPolicy;
import org.hyperledger.fabric.sdk.ChaincodeEvent;
import org.hyperledger.fabric.sdk.ChaincodeID;
import org.hyperledger.fabric.sdk.Channel;
import org.hyperledger.fabric.sdk.ChannelConfiguration;
import org.hyperledger.fabric.sdk.Enrollment;
import org.hyperledger.fabric.sdk.EventHub;
import org.hyperledger.fabric.sdk.HFClient;
import org.hyperledger.fabric.sdk.InstallProposalRequest;
import org.hyperledger.fabric.sdk.InstantiateProposalRequest;
import org.hyperledger.fabric.sdk.Orderer;
import org.hyperledger.fabric.sdk.Peer;
import org.hyperledger.fabric.sdk.Peer.PeerRole;
import org.hyperledger.fabric.sdk.ProposalResponse;
import org.hyperledger.fabric.sdk.QueryByChaincodeRequest;
import org.hyperledger.fabric.sdk.SDKUtils;
import org.hyperledger.fabric.sdk.TestConfigHelper;
import org.hyperledger.fabric.sdk.TransactionInfo;
import org.hyperledger.fabric.sdk.TransactionProposalRequest;
import org.hyperledger.fabric.sdk.TransactionRequest.Type;
import org.hyperledger.fabric.sdk.TxReadWriteSetInfo;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.InvalidProtocolBufferRuntimeException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.hyperledger.fabric.sdk.exception.TransactionEventException;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import org.hyperledger.fabric.sdk.testutils.TestConfig;
import org.hyperledger.fabric_ca.sdk.EnrollmentRequest;
import org.hyperledger.fabric_ca.sdk.HFCAClient;
import org.hyperledger.fabric_ca.sdk.HFCAInfo;
import org.hyperledger.fabric_ca.sdk.RegistrationRequest;
import org.junit.Before;
import org.junit.Test;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hyperledger.fabric.sdk.BlockInfo.EnvelopeType.TRANSACTION_ENVELOPE;
import static org.hyperledger.fabric.sdk.Channel.NOfEvents.createNofEvents;
import static org.hyperledger.fabric.sdk.Channel.PeerOptions.createPeerOptions;
import static org.hyperledger.fabric.sdk.Channel.TransactionOptions.createTransactionOptions;
import static org.hyperledger.fabric.sdk.testutils.TestUtils.resetConfig;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Test end to end scenario
*/
public class End2endIT2 {
private static final TestConfig testConfig = TestConfig.getConfig();
private static final String TEST_ADMIN_NAME = "admin";
private static final String TESTUSER_1_NAME = "user0";
private static final String TEST_FIXTURES_PATH = "src/test/fixture";
private static final String FOO_CHANNEL_NAME = "mychannel";
private static final String BAR_CHANNEL_NAME = "bar";
private static final byte[] EXPECTED_EVENT_DATA = "!".getBytes(UTF_8);
private static final String EXPECTED_EVENT_NAME = "event";
private static final Map<String, String> TX_EXPECTED;
String testName = "End2endIT";
String CHAIN_CODE_FILEPATH = "sdkintegration/gocc/sample1";
String CHAIN_CODE_NAME = "example_cc_go"; // example_cc.go
String CHAIN_CODE_PATH = "github.com/example_cc0";
String CHAIN_CODE_VERSION = "1";
Type CHAIN_CODE_LANG = Type.GO_LANG;
static {
TX_EXPECTED = new HashMap<>();
TX_EXPECTED.put("readset1", "Missing readset for channel bar block 1");
TX_EXPECTED.put("writeset1", "Missing writeset for channel bar block 1");
}
private final TestConfigHelper configHelper = new TestConfigHelper();
String testTxID = null; // save the CC invoke TxID and use in queries
SampleStore sampleStore = null;
private Collection<SampleOrg> testSampleOrgs;
static void out(String format, Object... args) {
System.err.flush();
System.out.flush();
System.out.println(format(format, args));
System.err.flush();
System.out.flush();
}
// CHECKSTYLE.ON: Method length is 320 lines (max allowed is 150).
static String printableString(final String string) {
int maxLogStringLength = 64;
if (string == null || string.length() == 0) {
return string;
}
String ret = string.replaceAll("[^\\p{Print}]", "?");// 这干嘛的???
ret = ret.substring(0, Math.min(ret.length(), maxLogStringLength))
+ (ret.length() > maxLogStringLength ? "..." : "");
return ret;
}
@Before
public void checkConfig()
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException,
MalformedURLException, org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException {
out("\n\n\nRUNNING: %s.\n", testName);
// configHelper.clearConfig();
// assertEquals(256, Config.getConfig().getSecurityLevel());
resetConfig();// 先重置配置文件org.hyperledger.fabric.sdk.helper.Config,config主要是的fabric需要的配置项
configHelper.customizeConfig();// 调用命令行输入的指定变量,覆盖掉上面的Config配置
// 获取testConfig配置的组织,TestConfig在End2endIT已经被new出来,里面也有大量的测试配置项
testSampleOrgs = testConfig.getIntegrationTestsSampleOrgs();
// Set up hfca for each sample org
for (SampleOrg sampleOrg : testSampleOrgs) {
// 设置组织的CA客户端,参数包括caName,CALocation,CAProperties
String caName = sampleOrg.getCAName(); // Try one of each name and no name.
// caName的值此前在 TestConfig 类的构造函数里面有设置
if (caName != null && !caName.isEmpty()) {
sampleOrg.setCAClient(
// caName=ca,CALocation=http://xxxx:7054,CAProperties=null
HFCAClient.createNewInstance(caName, sampleOrg.getCALocation(), sampleOrg.getCAProperties()));
/
// 输出运行中的一些结果
/
System.out.println("*** caName有值: " + caName + " sampleOrg.getCALocation: " + sampleOrg.getCALocation()
+ " sampleOrg.getCAProperties: " + sampleOrg.getCAProperties());
} else {
sampleOrg.setCAClient(
// CALocation=http://xxxx:7054,CAProperties=null
HFCAClient.createNewInstance(sampleOrg.getCALocation(), sampleOrg.getCAProperties()));
/
// 输出运行中的一些结果
/
System.out.println("*** caName无值,caName: " + caName + " sampleOrg.getCALocation: " + sampleOrg.getCALocation()
+ " sampleOrg.getCAProperties: " + sampleOrg.getCAProperties());
}
}
}
Map<String, Properties> clientTLSProperties = new HashMap<>();
// 这个是系统的缓存文件,保存授权过的用户数据,下次要用就不需要再授权了直接拿来用
// 将路径输出是 C:\Users\Louzen\AppData\Local\Temp\/HFCSampletest.properties
// 这里肯定会出问题,需要将\换成/
File sampleStoreFile = new File(System.getProperty("java.io.tmpdir") + "/HFCSampletest.properties");
@Test
public void setup() throws Exception {
// Persistence is not part of SDK. Sample file store is for demonstration
// purposes only!
// MUST be replaced with more robust application implementation (Database, LDAP)
// 看缓存文件在不在,如果在就。。。删掉?是的,开始测试前要删掉缓存文件
if (sampleStoreFile.exists()) {
// For testing start fresh
sampleStoreFile.delete();// 模拟数据库存储使用了HFCSampletest.properties
}
sampleStore = new SampleStore(sampleStoreFile);// 初始化存储
// 利用ca做初始化
// 想法:这一块是不是更换crypto-config文件夹就可以了?
///
// enrollUsersSetup函数开始标志
///
System.out.println("### 注册用户 enrollUsersSetup 函数开始");
System.out.println("## 前 sampleStore: " + sampleStore.toString());
enrollUsersSetup(sampleStore); // This enrolls users with fabric ca and setups sample store to get users later.
System.out.println("### 注册用户 enrollUsersSetup 函数结束\n");
System.out.println("## 后 sampleStore: " + sampleStore.toString());
// 核心方法
///
// runFabricTest函数开始标志
///
System.out.println("### runFabricTest 函数开始");
runFabricTest(sampleStore); // Runs Fabric tests with constructing channels, joining peers, exercising
// chaincode
System.out.println("### runFabricTest 函数结束");
}
public void runFabricTest(final SampleStore sampleStore) throws Exception {
// Setup client
// Create instance of client.
System.out.println("## 初始化一个链接客户端,类似于cli,并设置加密算法 开始");
HFClient client = HFClient.createNewInstance();// 初始化一个链接客户端,类似cli
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());// 设置加密算法
System.out.println("## 初始化一个链接客户端,类似于cli,并设置加密算法 结束");
// Construct and run the channels
// 获取peerOrg1组织1
System.out.println("## testConfig.getIntegrationTestsSampleOrg(\"peerOrg1\") 开始");
SampleOrg sampleOrg = testConfig.getIntegrationTestsSampleOrg("peerOrg1");
/
//输出sampleOrg,我想知道sampleOrg里面的PeerAdmin是怎么来的
System.out.println("#### runFabricTest 里 peerOrg1组织的 sampleOrg = \n" + sampleOrg.toString());
/
System.out.println("## testConfig.getIntegrationTestsSampleOrg(\"peerOrg1\") 结束");
// 构建一个channel通道,Org1加入到该通道中
// 【核心方法1】
System.out.println("## 【核心方法1】创建foo通道 开始");
Channel fooChannel = constructChannel(FOO_CHANNEL_NAME, client, sampleOrg);
System.out.println("## 【核心方法1】创建foo通道 结束");
// 保存通道名称到数据库中(这里是存储到上面方法文件)(个人想法:这里是把上面的文件当做数据库,只是模拟了一下存储过程)
System.out.println("## sampleStore.saveChannel(fooChannel) 开始");
sampleStore.saveChannel(fooChannel);
System.out.println("## sampleStore.saveChannel(fooChannel) 结束");
// 安装链码、实例化链码、执行一个查询测试
// 【核心方法2】
System.out.println("## 【核心方法2】runChannel(client, fooChannel, true, sampleOrg, 0) 开始");
runChannel(client, fooChannel, true, sampleOrg, 0);
System.out.println("## 【核心方法2】runChannel(client, fooChannel, true, sampleOrg, 0) 结束");
// 强制关闭foo通道
System.out.println("## 强制关闭foo通道 开始");
assertFalse(fooChannel.isShutdown());
fooChannel.shutdown(true); // Force foo channel to shutdown clean up resources.
assertTrue(fooChannel.isShutdown());
assertNull(client.getChannel(FOO_CHANNEL_NAME));
System.out.println(