Search in sources :

Example 16 with NodeSettings

use of com.jd.blockchain.consensus.NodeSettings in project jdchain-core by blockchain-jd-com.

the class BftsmartConsensusSettingsBuilder method addReplicaSetting.

@Override
public ConsensusViewSettings addReplicaSetting(ConsensusViewSettings viewSettings, Replica replica) {
    if (!(viewSettings instanceof BftsmartConsensusViewSettings)) {
        throw new IllegalArgumentException("The specified view-settings is not a bftsmart-consensus-settings!");
    }
    if (!(replica instanceof BftsmartReplica)) {
        throw new IllegalArgumentException("The specified replica is not a bftsmart-replica!");
    }
    BftsmartConsensusViewSettings bftsmartSettings = (BftsmartConsensusViewSettings) viewSettings;
    BftsmartReplica newReplica = (BftsmartReplica) replica;
    NodeSettings[] origNodes = bftsmartSettings.getNodes();
    if (origNodes.length == 0) {
        throw new IllegalStateException("The number of nodes is zero!");
    }
    // 更新节点列表;
    BftsmartNodeSettings[] newNodes = new BftsmartNodeSettings[origNodes.length + 1];
    for (int i = 0; i < origNodes.length; i++) {
        newNodes[i] = (BftsmartNodeSettings) origNodes[i];
    }
    newNodes[origNodes.length] = new BftsmartNodeConfig(newReplica.getPubKey(), newReplica.getId(), newReplica.getNetworkAddress());
    // 更新系统属性;
    Properties systemProps = PropertiesUtils.createProperties(bftsmartSettings.getSystemConfigs());
    systemProps.setProperty(SERVER_NUM_KEY, String.valueOf(newNodes.length));
    int f = computeBFTNumber(newNodes.length);
    systemProps.setProperty(F_NUM_KEY, String.valueOf(f));
    String[] processIds = new String[newNodes.length];
    for (int i = 0; i < processIds.length; i++) {
        processIds[i] = String.valueOf(newNodes[i].getId());
    }
    String viewPIDs = String.join(",", processIds);
    systemProps.setProperty(SERVER_VIEW_KEY, viewPIDs);
    // viewID increment;
    int viewId = bftsmartSettings.getViewId() + 1;
    return new BftsmartConsensusConfig(newNodes, PropertiesUtils.getOrderedValues(systemProps), viewId);
}
Also used : Properties(java.util.Properties) NodeSettings(com.jd.blockchain.consensus.NodeSettings)

Example 17 with NodeSettings

use of com.jd.blockchain.consensus.NodeSettings in project jdchain-core by blockchain-jd-com.

the class BftsmartConsensusSettingsBuilder method createNewNodeSetting.

private BftsmartNodeSettings[] createNewNodeSetting(NodeSettings[] oldNodeSettings, Properties newProps) {
    BftsmartNodeSettings[] bftsmartNodeSettings = null;
    if (newProps.getProperty(PARTICIPANT_OP_KEY) != null) {
        if (newProps.getProperty(PARTICIPANT_OP_KEY).equals("active")) {
            // organize new participant node
            int activeId = Integer.parseInt(newProps.getProperty(ACTIVE_PARTICIPANT_ID_KEY));
            String host = newProps.getProperty(keyOfNode(CONSENSUS_HOST_PATTERN, activeId));
            int port = Integer.parseInt(newProps.getProperty(keyOfNode(CONSENSUS_PORT_PATTERN, activeId)));
            boolean secure = Boolean.parseBoolean(newProps.getProperty(keyOfNode(CONSENSUS_SECURE_PATTERN, activeId)));
            byte[] pubKeyBytes = Base58Utils.decode(newProps.getProperty(keyOfNode(PUBKEY_PATTERN, activeId)));
            PubKey pubKey = Crypto.resolveAsPubKey(pubKeyBytes);
            BftsmartNodeConfig bftsmartNodeConfig = new BftsmartNodeConfig(pubKey, activeId, new NetworkAddress(host, port, secure));
            int index = oldNodeSettings.length;
            for (int i = 0; i < oldNodeSettings.length; i++) {
                NodeSettings settings = oldNodeSettings[i];
                if (settings.getAddress().equals(bftsmartNodeConfig.getAddress())) {
                    index = i;
                }
            }
            if (index == oldNodeSettings.length) {
                bftsmartNodeSettings = new BftsmartNodeSettings[oldNodeSettings.length + 1];
            } else {
                bftsmartNodeSettings = new BftsmartNodeSettings[oldNodeSettings.length];
            }
            for (int i = 0; i < oldNodeSettings.length; i++) {
                bftsmartNodeSettings[i] = (BftsmartNodeSettings) oldNodeSettings[i];
            }
            bftsmartNodeSettings[index] = bftsmartNodeConfig;
        } else if (newProps.getProperty(PARTICIPANT_OP_KEY).equals("deactive")) {
            int deActiveId = Integer.parseInt(newProps.getProperty(DEACTIVE_PARTICIPANT_ID_KEY));
            bftsmartNodeSettings = new BftsmartNodeSettings[oldNodeSettings.length - 1];
            int j = 0;
            for (int i = 0; i < oldNodeSettings.length; i++) {
                BftsmartNodeSettings bftsmartNodeSetting = (BftsmartNodeSettings) oldNodeSettings[i];
                if (bftsmartNodeSetting.getId() != deActiveId) {
                    bftsmartNodeSettings[j++] = bftsmartNodeSetting;
                }
            }
        } else {
            throw new IllegalArgumentException("createNewNodeSetting properties error!");
        }
    } else {
        bftsmartNodeSettings = new BftsmartNodeSettings[oldNodeSettings.length];
        for (int i = 0; i < oldNodeSettings.length; i++) {
            bftsmartNodeSettings[i] = (BftsmartNodeSettings) oldNodeSettings[i];
        }
    }
    return bftsmartNodeSettings;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) NodeSettings(com.jd.blockchain.consensus.NodeSettings) NetworkAddress(utils.net.NetworkAddress)

Aggregations

NodeSettings (com.jd.blockchain.consensus.NodeSettings)17 BftsmartNodeSettings (com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings)7 RaftNodeSettings (com.jd.blockchain.consensus.raft.settings.RaftNodeSettings)6 MsgQueueConsensusSettings (com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings)4 MsgQueueNodeSettings (com.jd.blockchain.consensus.mq.settings.MsgQueueNodeSettings)4 MsgQueueNodeConfig (com.jd.blockchain.consensus.mq.config.MsgQueueNodeConfig)3 RaftConsensusSettings (com.jd.blockchain.consensus.raft.settings.RaftConsensusSettings)3 PubKey (com.jd.blockchain.crypto.PubKey)3 NetworkAddress (utils.net.NetworkAddress)3 BftsmartConsensusConfig (com.jd.blockchain.consensus.bftsmart.BftsmartConsensusConfig)2 BftsmartConsensusViewSettings (com.jd.blockchain.consensus.bftsmart.BftsmartConsensusViewSettings)2 BftsmartNodeConfig (com.jd.blockchain.consensus.bftsmart.BftsmartNodeConfig)2 MsgQueueConsensusConfig (com.jd.blockchain.consensus.mq.config.MsgQueueConsensusConfig)2 MsgQueueBlockSettings (com.jd.blockchain.consensus.mq.settings.MsgQueueBlockSettings)2 MsgQueueNetworkSettings (com.jd.blockchain.consensus.mq.settings.MsgQueueNetworkSettings)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 HostsConfig (bftsmart.reconfiguration.util.HostsConfig)1 NodeNetwork (bftsmart.reconfiguration.views.NodeNetwork)1 Configuration (com.alipay.sofa.jraft.conf.Configuration)1