use of com.jd.blockchain.tools.initializer.LedgerBindingConfig.BindingConfig in project jdchain-core by blockchain-jd-com.
the class LedgerBindingConfigTest method assertLedgerBindingConfig.
// @Test
// public void testLedgerBindingRegister() throws IOException {
// LedgerManager ledgerManager = new LedgerManager();
// ClassPathResource ledgerBindingConfigFile = new ClassPathResource("ledger-binding-1.conf");
// InputStream in = ledgerBindingConfigFile.getInputStream();
// Exception ex = null;
// try {
// LedgerBindingConfig conf = LedgerBindingConfig.resolve(in);
// // assertLedgerBindingConfig(conf);
//
// HashDigest[] existingLedgerHashs = ledgerManager.getLedgerHashs();
// for (HashDigest lh : existingLedgerHashs) {
// ledgerManager.unregister(lh);
// }
// HashDigest[] ledgerHashs = conf.getLedgerHashs();
// for (HashDigest ledgerHash : ledgerHashs) {
// // setConfig(conf,ledgerHash);
// LedgerBindingConfig.BindingConfig bindingConfig = conf.getLedger(ledgerHash);
// }
// } catch (Exception e) {
// ex =e;
// } finally {
// in.close();
// }
//
// assertNull(ex);
// }
/**
* 判断指定的对象跟测试模板是否一致;
*
* @param conf
*/
private void assertLedgerBindingConfig(LedgerBindingConfig conf) {
String[] expectedHashs = { "j5ptBmn67B2p3yki3ji1j2ZMjnJhrUvP4kFpGmcXgvrhmk", "j5kLUENMvcUooZjKfz2bEYU6zoK9DAqbdDDU8aZEZFR4qf" };
HashDigest[] hashs = conf.getLedgerHashs();
for (int i = 0; i < hashs.length; i++) {
assertEquals(expectedHashs[i], hashs[i].toBase58());
}
BindingConfig bindingConf_0 = conf.getLedger(hashs[0]);
assertEquals("1", bindingConf_0.getParticipant().getAddress());
assertEquals("keys/jd-com.priv", bindingConf_0.getParticipant().getPkPath());
assertEquals("AdSXsf5QJpy", bindingConf_0.getParticipant().getPk());
assertNull(bindingConf_0.getParticipant().getPassword());
assertEquals("redis://ip:port/1", bindingConf_0.getDbConnection().getUri());
assertEquals("kksfweffj", bindingConf_0.getDbConnection().getPassword());
BindingConfig bindingConf_1 = conf.getLedger(hashs[1]);
assertEquals("2", bindingConf_1.getParticipant().getAddress());
assertEquals("keys/jd-com-1.priv", bindingConf_1.getParticipant().getPkPath());
assertNull(bindingConf_1.getParticipant().getPk());
assertEquals("kksafe", bindingConf_1.getParticipant().getPassword());
assertEquals("redis://ip:port/2", bindingConf_1.getDbConnection().getUri());
assertNull(bindingConf_1.getDbConnection().getPassword());
}
use of com.jd.blockchain.tools.initializer.LedgerBindingConfig.BindingConfig in project jdchain-core by blockchain-jd-com.
the class LedgerInitCommand method startInit.
public HashDigest startInit(int currId, PrivKey privKey, String base58Pwd, LedgerInitProperties ledgerInitProperties, DBConnectionConfig dbConnConfig, Prompter prompter, LedgerBindingConfig conf, Object... extBeans) {
if (StringUtils.isEmpty(base58Pwd)) {
base58Pwd = Base58Utils.encode(UUID.randomUUID().toString().getBytes());
prompter.info("Your base58 encode private key password : [%s]!!!", base58Pwd);
}
if (currId < 0 || currId >= ledgerInitProperties.getConsensusParticipantCount()) {
prompter.info("Your participant id is illegal which is less than 1 or great than the total participants count[%s]!!!", ledgerInitProperties.getConsensusParticipantCount());
return null;
}
// generate binding config;
BindingConfig bindingConf = new BindingConfig();
// 设置账本名称
bindingConf.setLedgerName(ledgerInitProperties.getLedgerName());
bindingConf.setDataStructure(ledgerInitProperties.getLedgerDataStructure());
bindingConf.getParticipant().setAddress(ledgerInitProperties.getConsensusParticipant(currId).getAddress().toBase58());
// 设置参与方名称
bindingConf.getParticipant().setName(ledgerInitProperties.getConsensusParticipant(currId).getName());
String encodedPrivKey = KeyGenUtils.encodePrivKey(privKey, base58Pwd);
bindingConf.getParticipant().setPk(encodedPrivKey);
bindingConf.getParticipant().setPassword(base58Pwd);
bindingConf.getDbConnection().setConnectionUri(dbConnConfig.getUri());
bindingConf.getDbConnection().setPassword(dbConnConfig.getPassword());
// confirm continue;
prompter.info("\r\n\r\n This is participant [%s], the ledger initialization is ready to start!\r\n", currId);
// ConsoleUtils.confirm("Press any key to continue... ");
// prompter.confirm("Press any key to continue... ");
// start the web controller of Ledger Initializer;
NetworkAddress serverAddress = ledgerInitProperties.getConsensusParticipant(currId).getInitializerAddress();
// for dockers binding the 0.0.0.0;
// if ledger-init.sh set up the -DhostPort=xxx -DhostIp=xxx, then get it;
String preHostPort = System.getProperty("hostPort");
if (!StringUtils.isEmpty(preHostPort)) {
int port = NumberUtils.parseNumber(preHostPort, Integer.class);
serverAddress.setPort(port);
ConsoleUtils.info("###ledger-init.sh###,set up the -DhostPort=" + port);
}
String preHostIp = System.getProperty("hostIp");
if (!StringUtils.isEmpty(preHostIp)) {
serverAddress.setHost(preHostIp);
ConsoleUtils.info("###ledger-init.sh###,set up the -DhostIp=" + preHostIp);
}
String argServerAddress = String.format("--server.address=%s", serverAddress.getHost());
String argServerPort = String.format("--server.port=%s", serverAddress.getPort());
String[] innerArgs = { argServerAddress, argServerPort };
SpringApplication app = new SpringApplication(LedgerInitCommand.class);
if (extBeans != null && extBeans.length > 0) {
app.addInitializers((ApplicationContextInitializer<ConfigurableApplicationContext>) applicationContext -> {
ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
for (Object bean : extBeans) {
beanFactory.registerSingleton(bean.toString(), bean);
}
});
}
ConfigurableApplicationContext ctx = app.run(innerArgs);
this.ledgerManager = ctx.getBean(LedgerManager.class);
prompter.info("\r\n------ Web controller of Ledger Initializer[%s:%s] was started. ------\r\n", serverAddress.getHost(), serverAddress.getPort());
try {
LedgerInitProcess initProc = ctx.getBean(LedgerInitProcess.class);
HashDigest ledgerHash = initProc.initialize(currId, privKey, ledgerInitProperties, bindingConf.getDbConnection(), prompter);
if (ledgerHash == null) {
// ledger init fail;
prompter.error("\r\n------ Ledger initialize fail! ------\r\n");
return null;
} else {
prompter.info("\r\n------ Ledger initialize success! ------");
prompter.info("New Ledger Hash is :[%s]", ledgerHash.toBase58());
if (conf == null) {
conf = new LedgerBindingConfig();
}
conf.addLedgerBinding(ledgerHash, bindingConf);
return ledgerHash;
}
} finally {
ctx.close();
prompter.info("\r\n------ Web listener[%s:%s] was closed. ------\r\n", serverAddress.getHost(), serverAddress.getPort());
}
}
use of com.jd.blockchain.tools.initializer.LedgerBindingConfig.BindingConfig in project jdchain-core by blockchain-jd-com.
the class LedgerInitCommand method startInit.
public HashDigest startInit(int currId, PrivKey privKey, String base58Pwd, LedgerInitProperties ledgerInitProperties, LocalConfig localConfig, Prompter prompter, LedgerBindingConfig conf, Object... extBeans) {
if (currId < 0 || currId >= ledgerInitProperties.getConsensusParticipantCount()) {
prompter.info("Your participant id is illegal which is less than 1 or great than the total participants count[%s]!!!", ledgerInitProperties.getConsensusParticipantCount());
return null;
}
// generate binding config;
BindingConfig bindingConf = new BindingConfig();
// 设置账本名称
bindingConf.setLedgerName(ledgerInitProperties.getLedgerName());
// 设置账本存储数据库的锚定类型
bindingConf.setDataStructure(ledgerInitProperties.getLedgerDataStructure());
// 设置额外参数
bindingConf.setExtraProperties(localConfig.getExtraProperties());
bindingConf.getParticipant().setAddress(ledgerInitProperties.getConsensusParticipant(currId).getAddress().toBase58());
// 设置参与方名称
bindingConf.getParticipant().setName(ledgerInitProperties.getConsensusParticipant(currId).getName());
// 证书模式下私钥处理
if (ledgerInitProperties.getIdentityMode() == IdentityMode.CA) {
bindingConf.getParticipant().setPkPath(localConfig.getLocal().getPrivKeyPath());
} else {
String encodedPrivKey = KeyGenUtils.encodePrivKey(privKey, base58Pwd);
bindingConf.getParticipant().setPk(encodedPrivKey);
}
if (!StringUtils.isEmpty(base58Pwd)) {
bindingConf.getParticipant().setPassword(base58Pwd);
}
// 共识服务TLS相关参数
bindingConf.getParticipant().setSslKeyStore(localConfig.getLocal().getSslKeyStore());
bindingConf.getParticipant().setSslKeyStorePassword(localConfig.getLocal().getSslKeyStorePassword());
bindingConf.getParticipant().setSslKeyStoreType(localConfig.getLocal().getSslKeyStoreType());
bindingConf.getParticipant().setSslKeyAlias(localConfig.getLocal().getSslKeyAlias());
bindingConf.getParticipant().setSslTrustStore(localConfig.getLocal().getSslTrustStore());
bindingConf.getParticipant().setSslTrustStorePassword(localConfig.getLocal().getSslTrustStorePassword());
bindingConf.getParticipant().setSslTrustStoreType(localConfig.getLocal().getSslTrustStoreType());
bindingConf.getParticipant().setProtocol(localConfig.getLocal().getSslProtocol());
bindingConf.getParticipant().setEnabledProtocols(localConfig.getLocal().getSslEnabledProtocols());
bindingConf.getParticipant().setCiphers(localConfig.getLocal().getSslCiphers());
bindingConf.getDbConnection().setConnectionUri(localConfig.getStoragedDb().getUri());
bindingConf.getDbConnection().setPassword(localConfig.getStoragedDb().getPassword());
// confirm continue;
prompter.info("\r\n\r\n This is participant [%s], the ledger initialization is ready to start!\r\n", currId);
// start the web controller of Ledger Initializer;
NetworkAddress serverAddress = ledgerInitProperties.getConsensusParticipant(currId).getInitializerAddress();
// for dockers binding the 0.0.0.0;
// if ledger-init.sh set up the -DhostPort=xxx -DhostIp=xxx, then get it;
String preHostPort = System.getProperty("hostPort");
if (!StringUtils.isEmpty(preHostPort)) {
int port = NumberUtils.parseNumber(preHostPort, Integer.class);
serverAddress.setPort(port);
ConsoleUtils.info("###ledger-init.sh###,set up the -DhostPort=" + port);
}
String preHostIp = System.getProperty("hostIp");
if (!StringUtils.isEmpty(preHostIp)) {
serverAddress.setHost(preHostIp);
ConsoleUtils.info("###ledger-init.sh###,set up the -DhostIp=" + preHostIp);
}
String argServerAddress = String.format("--server.address=%s", serverAddress.getHost());
String argServerPort = String.format("--server.port=%s", serverAddress.getPort());
String[] innerArgs = { argServerAddress, argServerPort };
SpringApplication app = new SpringApplication(LedgerInitCommand.class);
if (extBeans != null && extBeans.length > 0) {
app.addInitializers((ApplicationContextInitializer<ConfigurableApplicationContext>) applicationContext -> {
ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
for (Object bean : extBeans) {
beanFactory.registerSingleton(bean.toString(), bean);
}
});
}
ConfigurableApplicationContext ctx = app.run(innerArgs);
this.ledgerManager = ctx.getBean(LedgerManager.class);
prompter.info("\r\n------ Web controller of Ledger Initializer[%s:%s] was started. ------\r\n", serverAddress.getHost(), serverAddress.getPort());
try {
LedgerInitProcess initProc = ctx.getBean(LedgerInitProcess.class);
HashDigest ledgerHash = initProc.initialize(currId, privKey, ledgerInitProperties, bindingConf.getDbConnection(), prompter);
if (ledgerHash == null) {
// ledger init fail;
prompter.error("\r\n------ Ledger initialize fail! ------\r\n");
return null;
} else {
prompter.info("\r\n------ Ledger initialize success! ------");
prompter.info("New Ledger Hash is :[%s]", ledgerHash.toBase58());
if (conf == null) {
conf = new LedgerBindingConfig();
}
conf.addLedgerBinding(ledgerHash, bindingConf);
return ledgerHash;
}
} finally {
ctx.close();
prompter.info("\r\n------ Web listener[%s:%s] was closed. ------\r\n", serverAddress.getHost(), serverAddress.getPort());
}
}
Aggregations