Search in sources :

Example 1 with BindingConfig

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());
}
Also used : HashDigest(com.jd.blockchain.crypto.HashDigest) BindingConfig(com.jd.blockchain.tools.initializer.LedgerBindingConfig.BindingConfig) LedgerBindingConfig(com.jd.blockchain.tools.initializer.LedgerBindingConfig)

Example 2 with BindingConfig

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());
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) CertificateUtils(com.jd.blockchain.ca.CertificateUtils) X509Certificate(java.security.cert.X509Certificate) BindingConfig(com.jd.blockchain.tools.initializer.LedgerBindingConfig.BindingConfig) NumberUtils(org.springframework.util.NumberUtils) LedgerInitProperties(com.jd.blockchain.ledger.LedgerInitProperties) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) IdentityMode(com.jd.blockchain.ledger.IdentityMode) SpringApplication(org.springframework.boot.SpringApplication) LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) FileUtils(utils.io.FileUtils) ArgEntry(utils.ArgumentSet.ArgEntry) ParticipantProperties(com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) SpringBootApplication(org.springframework.boot.autoconfigure.SpringBootApplication) ArgumentSet(utils.ArgumentSet) com.jd.blockchain.crypto(com.jd.blockchain.crypto) Base58Utils(utils.codec.Base58Utils) StringUtils(utils.StringUtils) UUID(java.util.UUID) File(java.io.File) Setting(utils.ArgumentSet.Setting) NetworkAddress(utils.net.NetworkAddress) ConsoleUtils(utils.ConsoleUtils) ApplicationContextInitializer(org.springframework.context.ApplicationContextInitializer) LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) SpringApplication(org.springframework.boot.SpringApplication) NetworkAddress(utils.net.NetworkAddress) BindingConfig(com.jd.blockchain.tools.initializer.LedgerBindingConfig.BindingConfig) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 3 with BindingConfig

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());
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) CertificateUtils(com.jd.blockchain.ca.CertificateUtils) X509Certificate(java.security.cert.X509Certificate) BindingConfig(com.jd.blockchain.tools.initializer.LedgerBindingConfig.BindingConfig) NumberUtils(org.springframework.util.NumberUtils) LedgerInitProperties(com.jd.blockchain.ledger.LedgerInitProperties) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) IdentityMode(com.jd.blockchain.ledger.IdentityMode) SpringApplication(org.springframework.boot.SpringApplication) LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) FileUtils(utils.io.FileUtils) ArgEntry(utils.ArgumentSet.ArgEntry) ParticipantProperties(com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) SpringBootApplication(org.springframework.boot.autoconfigure.SpringBootApplication) ArgumentSet(utils.ArgumentSet) com.jd.blockchain.crypto(com.jd.blockchain.crypto) Base58Utils(utils.codec.Base58Utils) StringUtils(utils.StringUtils) UUID(java.util.UUID) File(java.io.File) Setting(utils.ArgumentSet.Setting) NetworkAddress(utils.net.NetworkAddress) ConsoleUtils(utils.ConsoleUtils) ApplicationContextInitializer(org.springframework.context.ApplicationContextInitializer) LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) SpringApplication(org.springframework.boot.SpringApplication) NetworkAddress(utils.net.NetworkAddress) BindingConfig(com.jd.blockchain.tools.initializer.LedgerBindingConfig.BindingConfig) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Aggregations

BindingConfig (com.jd.blockchain.tools.initializer.LedgerBindingConfig.BindingConfig)3 CertificateUtils (com.jd.blockchain.ca.CertificateUtils)2 com.jd.blockchain.crypto (com.jd.blockchain.crypto)2 IdentityMode (com.jd.blockchain.ledger.IdentityMode)2 LedgerInitProperties (com.jd.blockchain.ledger.LedgerInitProperties)2 ParticipantProperties (com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties)2 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)2 File (java.io.File)2 X509Certificate (java.security.cert.X509Certificate)2 UUID (java.util.UUID)2 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)2 SpringApplication (org.springframework.boot.SpringApplication)2 SpringBootApplication (org.springframework.boot.autoconfigure.SpringBootApplication)2 EnableConfigurationProperties (org.springframework.boot.context.properties.EnableConfigurationProperties)2 ApplicationContextInitializer (org.springframework.context.ApplicationContextInitializer)2 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)2 NumberUtils (org.springframework.util.NumberUtils)2 ArgumentSet (utils.ArgumentSet)2 ArgEntry (utils.ArgumentSet.ArgEntry)2 Setting (utils.ArgumentSet.Setting)2