Search in sources :

Example 1 with LedgerInitProperties

use of com.jd.blockchain.ledger.LedgerInitProperties in project jdchain-core by blockchain-jd-com.

the class LedgerInitConfiguration method createConsensusConfig.

private static ConsensusConfig createConsensusConfig(LedgerInitProperties initProps) {
    ConsensusProvider consensusProvider = ConsensusProviders.getProvider(initProps.getConsensusProvider());
    Properties csProps = initProps.getConsensusConfig();
    ConsensusViewSettings protocolSettings = consensusProvider.getSettingsFactory().getConsensusSettingsBuilder().createSettings(csProps, ParticipantReplica.wrap(initProps.getConsensusParticipantNodes()));
    ConsensusConfig config = new ConsensusConfig();
    config.setProvider(consensusProvider);
    config.setProtocolSettings(protocolSettings);
    return config;
}
Also used : ConsensusViewSettings(com.jd.blockchain.consensus.ConsensusViewSettings) ConsensusProvider(com.jd.blockchain.consensus.ConsensusProvider) LedgerInitProperties(com.jd.blockchain.ledger.LedgerInitProperties) ParticipantProperties(com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties) Properties(java.util.Properties) CryptoProperties(com.jd.blockchain.ledger.LedgerInitProperties.CryptoProperties)

Example 2 with LedgerInitProperties

use of com.jd.blockchain.ledger.LedgerInitProperties 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 LedgerInitProperties

use of com.jd.blockchain.ledger.LedgerInitProperties in project jdchain-core by blockchain-jd-com.

the class LedgerInitCommand method main.

/**
 * 入口;
 *
 * @param args
 */
public static void main(String[] args) {
    Prompter prompter = DEFAULT_PROMPTER;
    Setting argSetting = ArgumentSet.setting().prefix(LOCAL_ARG, INI_ARG).option(MONITOR_OPT);
    ArgumentSet argSet = ArgumentSet.resolve(args, argSetting);
    try {
        if (argSet.hasOption(MONITOR_OPT)) {
            prompter = LOG_PROMPTER;
        }
        ArgEntry localArg = argSet.getArg(LOCAL_ARG);
        if (localArg == null) {
            prompter.info("Miss local config file which can be specified with arg [%s]!!!", LOCAL_ARG);
        }
        LocalConfig localConf = LocalConfig.resolve(localArg.getValue());
        ArgEntry iniArg = argSet.getArg(INI_ARG);
        if (iniArg == null) {
            prompter.info("Miss ledger initializing config file which can be specified with arg [%s]!!!", INI_ARG);
            return;
        }
        // load ledger init setting;
        LedgerInitProperties ledgerInitProperties = LedgerInitProperties.resolve(iniArg.getValue());
        // 加载当前节点的私钥;
        // 根据 identity-mode 验证 local.conf 参数的正确性
        String base58Pwd = localConf.getLocal().getPassword();
        PubKey localNodePubKey;
        PrivKey privKey;
        if (ledgerInitProperties.getIdentityMode() == IdentityMode.CA) {
            X509Certificate certificate = CertificateUtils.parseCertificate(FileUtils.readText(localConf.getLocal().getCaPath()));
            localNodePubKey = CertificateUtils.resolvePubKey(certificate);
            if (StringUtils.isEmpty(base58Pwd)) {
                privKey = CertificateUtils.parsePrivKey(localNodePubKey.getAlgorithm(), FileUtils.readText(localConf.getLocal().getPrivKeyPath()));
            } else {
                privKey = CertificateUtils.parsePrivKey(localNodePubKey.getAlgorithm(), FileUtils.readText(localConf.getLocal().getPrivKeyPath()), base58Pwd);
            }
            if (!StringUtils.isEmpty(base58Pwd)) {
                base58Pwd = Base58Utils.encode(base58Pwd.getBytes());
            }
        } else {
            if (StringUtils.isEmpty(base58Pwd)) {
                base58Pwd = KeyGenUtils.readPasswordString();
            }
            localNodePubKey = KeyGenUtils.decodePubKey(localConf.getLocal().getPubKeyString());
            privKey = KeyGenUtils.decodePrivKey(localConf.getLocal().getPrivKeyString(), base58Pwd);
        }
        // 地址根据公钥生成
        String localNodeAddress = AddressEncoding.generateAddress(localNodePubKey).toBase58();
        // 加载全部公钥;
        int currId = -1;
        for (int i = 0; i < ledgerInitProperties.getConsensusParticipantCount(); i++) {
            ParticipantProperties partiConf = ledgerInitProperties.getConsensusParticipant(i);
            if (localNodeAddress.equals(partiConf.getAddress().toBase58())) {
                currId = i;
            }
        }
        if (currId == -1) {
            throw new IllegalStateException("The current node specified in local.conf is not found in ledger.init!");
        }
        // Output ledger binding config of peer;
        if (!FileUtils.existDirectory(localConf.getBindingOutDir())) {
            FileUtils.makeDirectory(localConf.getBindingOutDir());
        }
        File ledgerBindingFile = new File(localConf.getBindingOutDir(), LEDGER_BINDING_FILE_NAME);
        LedgerBindingConfig conf;
        if (ledgerBindingFile.exists()) {
            conf = LedgerBindingConfig.resolve(ledgerBindingFile);
        } else {
            conf = new LedgerBindingConfig();
        }
        // 启动初始化;
        LedgerInitCommand initCommand = new LedgerInitCommand();
        HashDigest newLedgerHash = initCommand.startInit(currId, privKey, base58Pwd, ledgerInitProperties, localConf, prompter, conf);
        if (newLedgerHash != null) {
            // success;
            // so save ledger binding config to file system;
            conf.store(ledgerBindingFile);
            prompter.info("\r\n------ Update Ledger binding configuration success! ------[%s]", ledgerBindingFile.getAbsolutePath());
        }
    } catch (Exception e) {
        e.printStackTrace();
        prompter.error("\r\n Ledger init process has been broken by error!");
    }
    prompter.confirm(InitializingStep.LEDGER_INIT_COMPLETED.toString(), "\r\n\r\n Press any key to quit. :>");
    if (argSet.hasOption(MONITOR_OPT)) {
        // 管理工具启动的方式下,需自动退出
        System.exit(0);
    }
}
Also used : Setting(utils.ArgumentSet.Setting) ParticipantProperties(com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties) ArgEntry(utils.ArgumentSet.ArgEntry) X509Certificate(java.security.cert.X509Certificate) LedgerInitProperties(com.jd.blockchain.ledger.LedgerInitProperties) ArgumentSet(utils.ArgumentSet) File(java.io.File)

Example 4 with LedgerInitProperties

use of com.jd.blockchain.ledger.LedgerInitProperties 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

LedgerInitProperties (com.jd.blockchain.ledger.LedgerInitProperties)4 ParticipantProperties (com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties)4 File (java.io.File)3 X509Certificate (java.security.cert.X509Certificate)3 ArgumentSet (utils.ArgumentSet)3 ArgEntry (utils.ArgumentSet.ArgEntry)3 Setting (utils.ArgumentSet.Setting)3 CertificateUtils (com.jd.blockchain.ca.CertificateUtils)2 com.jd.blockchain.crypto (com.jd.blockchain.crypto)2 IdentityMode (com.jd.blockchain.ledger.IdentityMode)2 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)2 BindingConfig (com.jd.blockchain.tools.initializer.LedgerBindingConfig.BindingConfig)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