Search in sources :

Example 1 with RaftConsensusSettings

use of com.jd.blockchain.consensus.raft.settings.RaftConsensusSettings in project jdchain-core by blockchain-jd-com.

the class GatewayQueryServiceHandler method consensusSettingsDecorator.

private ConsensusViewSettings consensusSettingsDecorator(ConsensusViewSettings consensusSettings) {
    if (consensusSettings instanceof BftsmartConsensusViewSettings) {
        // bft-smart单独处理
        BftsmartConsensusViewSettings bftsmartConsensusSettings = (BftsmartConsensusViewSettings) consensusSettings;
        NodeSettings[] nodes = bftsmartConsensusSettings.getNodes();
        BftsmartNodeSettings[] bftsmartNodes = null;
        if (nodes != null && nodes.length > 0) {
            bftsmartNodes = new BftsmartNodeSettings[nodes.length];
            for (int i = 0; i < nodes.length; i++) {
                NodeSettings node = nodes[i];
                if (node instanceof BftsmartNodeSettings) {
                    BftsmartNodeSettings bftsmartNodeSettings = (BftsmartNodeSettings) node;
                    bftsmartNodes[i] = new BftsmartNodeConfig(bftsmartNodeSettings.getPubKey(), bftsmartNodeSettings.getId(), bftsmartNodeSettings.getNetworkAddress());
                }
            }
        }
        return new BftsmartConsensusConfig(bftsmartNodes, bftsmartConsensusSettings.getSystemConfigs(), 0);
    } else if (consensusSettings instanceof RaftConsensusSettings) {
        RaftConsensusSettings raftConsensusSettings = (RaftConsensusSettings) consensusSettings;
        NodeSettings[] nodes = raftConsensusSettings.getNodes();
        RaftNodeSettings[] raftNodeSettings = new RaftNodeSettings[0];
        if (nodes != null && nodes.length > 0) {
            raftNodeSettings = new RaftNodeSettings[nodes.length];
            for (int i = 0; i < nodes.length; i++) {
                NodeSettings node = nodes[i];
                if (node instanceof RaftNodeSettings) {
                    RaftNodeSettings raftNodeSetting = (RaftNodeSettings) node;
                    raftNodeSettings[i] = new RaftNodeConfig(raftNodeSetting.getId(), raftNodeSetting.getAddress(), raftNodeSetting.getPubKey(), raftNodeSetting.getNetworkAddress());
                }
            }
        }
        RaftConsensusConfig raftConsensusConfig = new RaftConsensusConfig();
        RaftConfig raftConfig = new RaftConfig();
        RaftNetworkConfig raftNetworkConfig = new RaftNetworkConfig();
        BeanUtils.copyProperties(raftConsensusSettings, raftConsensusConfig);
        BeanUtils.copyProperties(raftConsensusSettings.getRaftSettings(), raftConfig);
        BeanUtils.copyProperties(raftConsensusSettings.getNetworkSettings(), raftNetworkConfig);
        raftConsensusConfig.setNodeSettingsList(Arrays.asList(raftNodeSettings));
        raftConsensusConfig.setRaftSettings(raftConfig);
        raftConsensusConfig.setNetworkSettings(raftNetworkConfig);
        return raftConsensusConfig;
    } else if (consensusSettings instanceof MsgQueueConsensusSettings) {
        MsgQueueConsensusSettings mqConsensusSettings = (MsgQueueConsensusSettings) consensusSettings;
        MsgQueueConsensusConfig mqConsensusConfig = new MsgQueueConsensusConfig();
        MsgQueueBlockSettings blockSettings = mqConsensusSettings.getBlockSettings();
        MsgQueueBlockConfig mqQueueBlockConfig = new MsgQueueBlockConfig();
        mqQueueBlockConfig.setMaxDelayMilliSecondsPerBlock(blockSettings.getMaxDelayMilliSecondsPerBlock());
        mqQueueBlockConfig.setTxSizePerBlock(blockSettings.getTxSizePerBlock());
        mqConsensusConfig.setBlockSettings(mqQueueBlockConfig);
        MsgQueueNetworkSettings networkSettings = mqConsensusSettings.getNetworkSettings();
        MsgQueueNetworkConfig mqQueueNetworkConfig = new MsgQueueNetworkConfig();
        mqQueueNetworkConfig.setBlockTopic(networkSettings.getBlockTopic());
        mqQueueNetworkConfig.setMsgResultTopic(networkSettings.getMsgResultTopic());
        mqQueueNetworkConfig.setMsgTopic(networkSettings.getMsgTopic());
        mqQueueNetworkConfig.setServer(networkSettings.getServer());
        mqQueueNetworkConfig.setTxTopic(networkSettings.getTxTopic());
        mqQueueNetworkConfig.setTxResultTopic(networkSettings.getTxResultTopic());
        mqConsensusConfig.setNetworkSettings(mqQueueNetworkConfig);
        for (int i = 0; i < mqConsensusSettings.getNodes().length; i++) {
            MsgQueueNodeSettings nodeSettings = (MsgQueueNodeSettings) mqConsensusSettings.getNodes()[i];
            MsgQueueNodeConfig msgQueueNodeConfig = new MsgQueueNodeConfig();
            msgQueueNodeConfig.setAddress(nodeSettings.getAddress());
            msgQueueNodeConfig.setPubKey(nodeSettings.getPubKey());
            msgQueueNodeConfig.setId(nodeSettings.getId());
            mqConsensusConfig.addNodeSettings(msgQueueNodeConfig);
        }
        return mqConsensusConfig;
    }
    return consensusSettings;
}
Also used : BftsmartConsensusViewSettings(com.jd.blockchain.consensus.bftsmart.BftsmartConsensusViewSettings) RaftNetworkConfig(com.jd.blockchain.consensus.raft.config.RaftNetworkConfig) BftsmartNodeSettings(com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings) MsgQueueBlockSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueBlockSettings) MsgQueueConsensusConfig(com.jd.blockchain.consensus.mq.config.MsgQueueConsensusConfig) MsgQueueBlockConfig(com.jd.blockchain.consensus.mq.config.MsgQueueBlockConfig) RaftConsensusSettings(com.jd.blockchain.consensus.raft.settings.RaftConsensusSettings) RaftConsensusConfig(com.jd.blockchain.consensus.raft.config.RaftConsensusConfig) NodeSettings(com.jd.blockchain.consensus.NodeSettings) RaftNodeSettings(com.jd.blockchain.consensus.raft.settings.RaftNodeSettings) MsgQueueNodeSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueNodeSettings) BftsmartNodeSettings(com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings) RaftNodeSettings(com.jd.blockchain.consensus.raft.settings.RaftNodeSettings) MsgQueueConsensusSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings) RaftNodeConfig(com.jd.blockchain.consensus.raft.config.RaftNodeConfig) BftsmartConsensusConfig(com.jd.blockchain.consensus.bftsmart.BftsmartConsensusConfig) MsgQueueNodeConfig(com.jd.blockchain.consensus.mq.config.MsgQueueNodeConfig) MsgQueueNetworkConfig(com.jd.blockchain.consensus.mq.config.MsgQueueNetworkConfig) RaftConfig(com.jd.blockchain.consensus.raft.config.RaftConfig) MsgQueueNodeSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueNodeSettings) BftsmartNodeConfig(com.jd.blockchain.consensus.bftsmart.BftsmartNodeConfig) MsgQueueNetworkSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueNetworkSettings)

Example 2 with RaftConsensusSettings

use of com.jd.blockchain.consensus.raft.settings.RaftConsensusSettings in project jdchain-core by blockchain-jd-com.

the class RaftNodeServerFactory method buildServerSettings.

@Override
public ServerSettings buildServerSettings(String realmName, ConsensusViewSettings viewSettings, String nodeAddress, SSLSecurity sslSecurity, Properties properties) {
    if (!(viewSettings instanceof RaftConsensusSettings)) {
        throw new IllegalStateException("view settings should be raft-consensus settings");
    }
    if (null == properties || properties.size() == 0) {
        throw new IllegalStateException("Extra properties empty");
    }
    RaftServerSettingsConfig settingsConfig = new RaftServerSettingsConfig();
    NodeSettings currentNodeSettings = null;
    for (NodeSettings nodeSettings : viewSettings.getNodes()) {
        if (nodeSettings.getAddress().equals(nodeAddress)) {
            currentNodeSettings = nodeSettings;
            break;
        }
    }
    if (currentNodeSettings == null) {
        throw new IllegalArgumentException("node address does not exist in view settings!");
    }
    settingsConfig.setRealmName(realmName);
    settingsConfig.setConsensusSettings((RaftConsensusSettings) viewSettings);
    settingsConfig.setReplicaSettings(currentNodeSettings);
    settingsConfig.setExtraProperties(properties);
    if (sslSecurity == null) {
        return settingsConfig;
    }
    // TLS适配
    boolean enableTLS = false;
    RaftNodeSettings raftNodeConfig = (RaftNodeSettings) currentNodeSettings;
    if (raftNodeConfig.getNetworkAddress().isSecure() && !Strings.isNullOrEmpty(sslSecurity.getKeyStore())) {
        enableTLS = true;
        GmSSLProvider.enableGMSupport(sslSecurity.getProtocol());
    }
    if (!enableTLS) {
        return settingsConfig;
    }
    // Node节点作为服务端时, 配置私钥信息
    setSystemProperty("bolt.ssl.protocol", sslSecurity.getProtocol());
    setSystemProperty("bolt.server.ssl.enable", "true");
    setSystemProperty("bolt.server.ssl.keystore", sslSecurity.getKeyStore());
    setSystemProperty("bolt.server.ssl.keyalias", sslSecurity.getKeyAlias());
    setSystemProperty("bolt.server.ssl.keystore.password", sslSecurity.getKeyStorePassword());
    setSystemProperty("bolt.server.ssl.keystore.type", sslSecurity.getKeyStoreType());
    if (sslSecurity.getEnabledProtocols() != null) {
        setSystemProperty("bolt.ssl.enabled-protocols", String.join(",", sslSecurity.getEnabledProtocols()));
    }
    if (sslSecurity.getCiphers() != null) {
        setSystemProperty("bolt.ssl.ciphers", String.join(",", sslSecurity.getCiphers()));
    }
    // raft共识服务端开启TLS后,raft连接客户端也需开启TLS请求
    setSystemProperty("bolt.client.ssl.enable", "true");
    // Node节点配置信任证书,以及作为客户端链接其他节点时的信任证书
    if (!Strings.isNullOrEmpty(sslSecurity.getTrustStore())) {
        // 服务端配置: 此时服务端有keystore, truststore, 此时开启双向认证
        setSystemProperty("bolt.server.ssl.clientAuth", "true");
        setSystemProperty("bolt.client.ssl.keystore", sslSecurity.getTrustStore());
        setSystemProperty("bolt.client.ssl.keystore.password", sslSecurity.getTrustStorePassword());
        setSystemProperty("bolt.client.ssl.keystore.type", sslSecurity.getTrustStoreType());
    }
    return settingsConfig;
}
Also used : NodeSettings(com.jd.blockchain.consensus.NodeSettings) RaftNodeSettings(com.jd.blockchain.consensus.raft.settings.RaftNodeSettings) RaftNodeSettings(com.jd.blockchain.consensus.raft.settings.RaftNodeSettings) RaftServerSettingsConfig(com.jd.blockchain.consensus.raft.config.RaftServerSettingsConfig) RaftConsensusSettings(com.jd.blockchain.consensus.raft.settings.RaftConsensusSettings)

Example 3 with RaftConsensusSettings

use of com.jd.blockchain.consensus.raft.settings.RaftConsensusSettings in project jdchain-core by blockchain-jd-com.

the class RaftConsensusSettingsBuilder method convertToProperties.

@Override
public Properties convertToProperties(ConsensusViewSettings settings) {
    if (!(settings instanceof RaftConsensusSettings)) {
        throw new IllegalStateException("settings should be raft-consensus-settings type");
    }
    Properties properties = new Properties();
    RaftConsensusConfig raftConsensusConfig = new RaftConsensusConfig((RaftConsensusSettings) settings);
    PropertiesUtils.mergeFrom(properties, raftConsensusConfig.convert());
    for (NodeSettings nodeSettings : raftConsensusConfig.getNodes()) {
        PropertiesUtils.mergeFrom(properties, ((RaftNodeConfig) nodeSettings).convert());
    }
    RaftConfig raftConfig = (RaftConfig) raftConsensusConfig.getRaftSettings();
    PropertiesUtils.mergeFrom(properties, raftConfig.convert());
    RaftNetworkConfig raftNetworkConfig = (RaftNetworkConfig) raftConsensusConfig.getNetworkSettings();
    PropertiesUtils.mergeFrom(properties, raftNetworkConfig.convert());
    return properties;
}
Also used : NodeSettings(com.jd.blockchain.consensus.NodeSettings) RaftNodeSettings(com.jd.blockchain.consensus.raft.settings.RaftNodeSettings) RaftConsensusSettings(com.jd.blockchain.consensus.raft.settings.RaftConsensusSettings)

Example 4 with RaftConsensusSettings

use of com.jd.blockchain.consensus.raft.settings.RaftConsensusSettings in project jdchain-core by blockchain-jd-com.

the class ParticipantManagerService4Raft method createRpcClient.

private CliClientServiceImpl createRpcClient(ParticipantContext context) {
    if (context.getProperty(RPC_CLIENT) != null) {
        return (CliClientServiceImpl) context.getProperty(RPC_CLIENT);
    }
    RaftConsensusSettings consensusSetting = (RaftConsensusSettings) getConsensusSetting(context);
    RaftNetworkSettings networkSettings = consensusSetting.getNetworkSettings();
    SSLSecurity sslSecurity = context.sslSecurity();
    // 启用TLS
    if (sslSecurity != null && sslSecurity.getKeyStore() != null) {
        GmSSLProvider.enableGMSupport(sslSecurity.getProtocol());
        setSystemProperty("bolt.client.ssl.enable", "true");
        setSystemProperty("bolt.client.ssl.keystore", sslSecurity.getTrustStore());
        setSystemProperty("bolt.client.ssl.keystore.password", sslSecurity.getTrustStorePassword());
        setSystemProperty("bolt.client.ssl.keystore.type", sslSecurity.getTrustStoreType() == null ? "JKS" : sslSecurity.getTrustStoreType());
        setSystemProperty("bolt.ssl.protocol", sslSecurity.getProtocol());
        setSystemProperty("bolt.server.ssl.enable", "true");
        setSystemProperty("bolt.server.ssl.keystore", sslSecurity.getKeyStore());
        setSystemProperty("bolt.server.ssl.keyalias", sslSecurity.getKeyAlias());
        setSystemProperty("bolt.server.ssl.keystore.password", sslSecurity.getKeyStorePassword());
        setSystemProperty("bolt.server.ssl.keystore.type", sslSecurity.getKeyStoreType());
        if (sslSecurity.getEnabledProtocols() != null && sslSecurity.getEnabledProtocols().length > 0) {
            setSystemProperty("bolt.ssl.enabled-protocols", String.join(",", sslSecurity.getEnabledProtocols()));
        }
        if (sslSecurity.getCiphers() != null && sslSecurity.getCiphers().length > 0) {
            setSystemProperty("bolt.ssl.ciphers", String.join(",", sslSecurity.getCiphers()));
        }
    }
    CliOptions cliOptions = new CliOptions();
    cliOptions.setRpcConnectTimeoutMs(networkSettings.getRpcConnectTimeoutMs());
    cliOptions.setRpcDefaultTimeout(networkSettings.getRpcDefaultTimeoutMs());
    cliOptions.setRpcInstallSnapshotTimeout(networkSettings.getRpcSnapshotTimeoutMs());
    cliOptions.setTimeoutMs(networkSettings.getRpcRequestTimeoutMs() * 2);
    cliOptions.setMaxRetry(MAX_RETRY_TIMES);
    CliClientServiceImpl clientService = new CliClientServiceImpl();
    clientService.init(cliOptions);
    context.setProperty(RPC_CLIENT, clientService);
    context.setProperty(RPC_QUEST_TIMEOUT_MS, networkSettings.getRpcRequestTimeoutMs() * 2);
    return clientService;
}
Also used : RaftNetworkSettings(com.jd.blockchain.consensus.raft.settings.RaftNetworkSettings) CliClientServiceImpl(com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl) RaftConsensusSettings(com.jd.blockchain.consensus.raft.settings.RaftConsensusSettings) SSLSecurity(utils.net.SSLSecurity) CliOptions(com.alipay.sofa.jraft.option.CliOptions)

Aggregations

RaftConsensusSettings (com.jd.blockchain.consensus.raft.settings.RaftConsensusSettings)4 NodeSettings (com.jd.blockchain.consensus.NodeSettings)3 RaftNodeSettings (com.jd.blockchain.consensus.raft.settings.RaftNodeSettings)3 CliOptions (com.alipay.sofa.jraft.option.CliOptions)1 CliClientServiceImpl (com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl)1 BftsmartConsensusConfig (com.jd.blockchain.consensus.bftsmart.BftsmartConsensusConfig)1 BftsmartConsensusViewSettings (com.jd.blockchain.consensus.bftsmart.BftsmartConsensusViewSettings)1 BftsmartNodeConfig (com.jd.blockchain.consensus.bftsmart.BftsmartNodeConfig)1 BftsmartNodeSettings (com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings)1 MsgQueueBlockConfig (com.jd.blockchain.consensus.mq.config.MsgQueueBlockConfig)1 MsgQueueConsensusConfig (com.jd.blockchain.consensus.mq.config.MsgQueueConsensusConfig)1 MsgQueueNetworkConfig (com.jd.blockchain.consensus.mq.config.MsgQueueNetworkConfig)1 MsgQueueNodeConfig (com.jd.blockchain.consensus.mq.config.MsgQueueNodeConfig)1 MsgQueueBlockSettings (com.jd.blockchain.consensus.mq.settings.MsgQueueBlockSettings)1 MsgQueueConsensusSettings (com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings)1 MsgQueueNetworkSettings (com.jd.blockchain.consensus.mq.settings.MsgQueueNetworkSettings)1 MsgQueueNodeSettings (com.jd.blockchain.consensus.mq.settings.MsgQueueNodeSettings)1 RaftConfig (com.jd.blockchain.consensus.raft.config.RaftConfig)1 RaftConsensusConfig (com.jd.blockchain.consensus.raft.config.RaftConsensusConfig)1 RaftNetworkConfig (com.jd.blockchain.consensus.raft.config.RaftNetworkConfig)1