Search in sources :

Example 1 with MsgQueueConsensusSettings

use of com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings 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 MsgQueueConsensusSettings

use of com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings in project jdchain-core by blockchain-jd-com.

the class ParticipantManagerService4MQ method createMQClient.

private DefaultMessageTransmitter createMQClient(ParticipantContext context) throws Exception {
    MsgQueueConsensusSettings consensusSettings = (MsgQueueConsensusSettings) getConsensusSetting(context);
    String server = consensusSettings.getNetworkSettings().getServer();
    String txTopic = consensusSettings.getNetworkSettings().getTxTopic();
    String txResultTopic = consensusSettings.getNetworkSettings().getTxResultTopic();
    String msgTopic = consensusSettings.getNetworkSettings().getMsgTopic();
    String msgResultTopic = consensusSettings.getNetworkSettings().getMsgResultTopic();
    MsgQueueProducer txProducer = MsgQueueFactory.newProducer(server, txTopic, false);
    MsgQueueConsumer txResultConsumer = MsgQueueFactory.newConsumer(server, txResultTopic, false);
    MsgQueueProducer msgProducer = MsgQueueFactory.newProducer(server, msgTopic, false);
    MsgQueueConsumer msgResultConsumer = MsgQueueFactory.newConsumer(server, msgResultTopic, false);
    DefaultMessageTransmitter transmitter = new DefaultMessageTransmitter().setTxProducer(txProducer).setTxResultConsumer(txResultConsumer).setMsgProducer(msgProducer).setMsgResultConsumer(msgResultConsumer);
    transmitter.connect();
    return transmitter;
}
Also used : MsgQueueProducer(com.jd.blockchain.consensus.mq.producer.MsgQueueProducer) MsgQueueConsensusSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings) MsgQueueConsumer(com.jd.blockchain.consensus.mq.consumer.MsgQueueConsumer) DefaultMessageTransmitter(com.jd.blockchain.consensus.mq.client.DefaultMessageTransmitter)

Example 3 with MsgQueueConsensusSettings

use of com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings in project jdchain-core by blockchain-jd-com.

the class MsgQueueNodeServerFactory method buildServerSettings.

@Override
public MsgQueueServerSettings buildServerSettings(String realmName, ConsensusViewSettings viewSettings, String nodeAddress, SSLSecurity sslSecurity, Properties properties) {
    if (!(viewSettings instanceof MsgQueueConsensusSettings)) {
        throw new IllegalArgumentException("ConsensusSettings data isn't supported! Accept MsgQueueConsensusSettings only!");
    }
    int id = -1;
    for (NodeSettings nodeSettings : viewSettings.getNodes()) {
        MsgQueueNodeSettings settings = (MsgQueueNodeSettings) nodeSettings;
        if (settings.getAddress().equals(nodeAddress)) {
            id = settings.getId();
            break;
        }
    }
    MsgQueueNodeSettings nodeSettings = new MsgQueueNodeConfig().setAddress(nodeAddress).setId(id);
    MsgQueueServerSettings serverSettings = new MsgQueueServerConfig().setRealmName(realmName).setNodeSettings(nodeSettings).setConsensusSettings((MsgQueueConsensusSettings) viewSettings);
    return serverSettings;
}
Also used : NodeSettings(com.jd.blockchain.consensus.NodeSettings) MsgQueueNodeSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueNodeSettings) MsgQueueConsensusSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings) MsgQueueServerConfig(com.jd.blockchain.consensus.mq.config.MsgQueueServerConfig) MsgQueueServerSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueServerSettings) MsgQueueNodeConfig(com.jd.blockchain.consensus.mq.config.MsgQueueNodeConfig) MsgQueueNodeSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueNodeSettings)

Example 4 with MsgQueueConsensusSettings

use of com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings in project jdchain-core by blockchain-jd-com.

the class MsgQueueConsensusSettingsBuilder method updateSettings.

@Override
public ConsensusViewSettings updateSettings(ConsensusViewSettings oldConsensusSettings, Properties newProps) {
    String op = newProps.getProperty("participant.op");
    int id = Integer.valueOf(newProps.getProperty("participant.id"));
    String pubkeyBase58 = newProps.getProperty("system.server." + id + ".pubkey");
    PubKey pubkey = Crypto.resolveAsPubKey(Base58Utils.decode(pubkeyBase58));
    Bytes address = AddressEncoding.generateAddress(pubkey);
    MsgQueueConsensusSettings consensusSettings = (MsgQueueConsensusSettings) oldConsensusSettings;
    NodeSettings[] nodes = consensusSettings.getNodes();
    List<MsgQueueNodeConfig> mqNodes = new ArrayList<>();
    boolean needAdd = true;
    for (NodeSettings settings : nodes) {
        MsgQueueNodeSettings node = (MsgQueueNodeSettings) settings;
        if (node.getPubKey().equals(pubkey)) {
            if (op.equals("deactive")) {
                needAdd = false;
                continue;
            }
        } else {
            MsgQueueNodeConfig nodeConfig = new MsgQueueNodeConfig();
            nodeConfig.setAddress(node.getAddress());
            nodeConfig.setPubKey(node.getPubKey());
            nodeConfig.setId(node.getId());
            mqNodes.add(nodeConfig);
        }
    }
    if (needAdd) {
        MsgQueueNodeConfig nodeConfig = new MsgQueueNodeConfig();
        nodeConfig.setAddress(address.toString());
        nodeConfig.setId(id);
        nodeConfig.setPubKey(pubkey);
        mqNodes.add(nodeConfig);
    }
    MsgQueueConsensusConfig newSettings = new MsgQueueConsensusConfig();
    newSettings.setBlockSettings(consensusSettings.getBlockSettings());
    newSettings.setNetworkSettings(consensusSettings.getNetworkSettings());
    for (MsgQueueNodeConfig node : mqNodes) {
        newSettings.addNodeSettings(node);
    }
    return newSettings;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) MsgQueueConsensusConfig(com.jd.blockchain.consensus.mq.config.MsgQueueConsensusConfig) ArrayList(java.util.ArrayList) Bytes(utils.Bytes) NodeSettings(com.jd.blockchain.consensus.NodeSettings) MsgQueueNodeSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueNodeSettings) MsgQueueConsensusSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings) MsgQueueNodeConfig(com.jd.blockchain.consensus.mq.config.MsgQueueNodeConfig) MsgQueueNodeSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueNodeSettings)

Example 5 with MsgQueueConsensusSettings

use of com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings in project jdchain-core by blockchain-jd-com.

the class MsgQueueConsensusSettingsBuilder method convertToProperties.

@Override
public Properties convertToProperties(ConsensusViewSettings settings) {
    Properties props = new Properties();
    if (!(settings instanceof MsgQueueConsensusSettings)) {
        throw new IllegalArgumentException("ConsensusSettings data isn't supported! Accept MsgQueueConsensusSettings only!");
    }
    MsgQueueConsensusSettings consensusSettings = (MsgQueueConsensusSettings) settings;
    MsgQueueNetworkSettings networkSettings = consensusSettings.getNetworkSettings();
    if (networkSettings == null || networkSettings.getServer() == null || networkSettings.getServer().length() <= 0) {
        throw new IllegalArgumentException("MsgQueue Consensus server is empty!");
    }
    String server = networkSettings.getServer();
    props.setProperty(MSG_QUEUE_SERVER, server);
    String txTopic = networkSettings.getTxTopic();
    if (txTopic == null || txTopic.length() <= 0) {
        txTopic = DEFAULT_TOPIC_TX;
    }
    props.setProperty(MSG_QUEUE_TOPIC_TX, txTopic);
    String txResultTopic = networkSettings.getTxResultTopic();
    if (txResultTopic == null || txResultTopic.length() <= 0) {
        txResultTopic = DEFAULT_TOPIC_TX_RESULT;
    }
    props.setProperty(MSG_QUEUE_TOPIC_TX_RESULT, txResultTopic);
    String blockTopic = networkSettings.getBlockTopic();
    if (blockTopic == null || blockTopic.length() <= 0) {
        blockTopic = DEFAULT_TOPIC_BLOCK;
    }
    props.setProperty(MSG_QUEUE_TOPIC_BLOCK, blockTopic);
    String msgTopic = networkSettings.getMsgTopic();
    if (msgTopic == null || msgTopic.length() <= 0) {
        msgTopic = DEFAULT_TOPIC_MSG;
    }
    props.setProperty(MSG_QUEUE_TOPIC_MSG, msgTopic);
    String msgResultTopic = networkSettings.getMsgResultTopic();
    if (msgResultTopic == null || msgResultTopic.length() <= 0) {
        msgResultTopic = DEFAULT_TOPIC_MSG_RESULT;
    }
    props.setProperty(MSG_QUEUE_TOPIC_MSG_RESULT, msgResultTopic);
    MsgQueueBlockSettings blockSettings = consensusSettings.getBlockSettings();
    if (blockSettings == null) {
        props.setProperty(MSG_QUEUE_BLOCK_TXSIZE, DEFAULT_TXSIZE + "");
        props.setProperty(MSG_QUEUE_BLOCK_MAXDELAY, DEFAULT_MAXDELAY + "");
    } else {
        int txSize = blockSettings.getTxSizePerBlock();
        long maxDelay = blockSettings.getMaxDelayMilliSecondsPerBlock();
        props.setProperty(MSG_QUEUE_BLOCK_TXSIZE, txSize + "");
        props.setProperty(MSG_QUEUE_BLOCK_MAXDELAY, maxDelay + "");
    }
    NodeSettings[] nodes = consensusSettings.getNodes();
    props.setProperty(SERVER_NUM_KEY, nodes.length + "");
    for (int i = 0; i < nodes.length; i++) {
        props.setProperty(nodeKey(PUBKEY_PATTERN, i), nodes[i].getPubKey().toString());
    }
    return props;
}
Also used : NodeSettings(com.jd.blockchain.consensus.NodeSettings) MsgQueueNodeSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueNodeSettings) MsgQueueConsensusSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings) MsgQueueBlockSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueBlockSettings) Properties(java.util.Properties) MsgQueueNetworkSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueNetworkSettings)

Aggregations

MsgQueueConsensusSettings (com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings)6 NodeSettings (com.jd.blockchain.consensus.NodeSettings)4 MsgQueueNodeSettings (com.jd.blockchain.consensus.mq.settings.MsgQueueNodeSettings)4 MsgQueueNodeConfig (com.jd.blockchain.consensus.mq.config.MsgQueueNodeConfig)3 MsgQueueConsensusConfig (com.jd.blockchain.consensus.mq.config.MsgQueueConsensusConfig)2 MsgQueueConsumer (com.jd.blockchain.consensus.mq.consumer.MsgQueueConsumer)2 MsgQueueBlockSettings (com.jd.blockchain.consensus.mq.settings.MsgQueueBlockSettings)2 MsgQueueNetworkSettings (com.jd.blockchain.consensus.mq.settings.MsgQueueNetworkSettings)2 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 DefaultMessageTransmitter (com.jd.blockchain.consensus.mq.client.DefaultMessageTransmitter)1 MsgQueueBlockConfig (com.jd.blockchain.consensus.mq.config.MsgQueueBlockConfig)1 MsgQueueNetworkConfig (com.jd.blockchain.consensus.mq.config.MsgQueueNetworkConfig)1 MsgQueueServerConfig (com.jd.blockchain.consensus.mq.config.MsgQueueServerConfig)1 MsgQueueProducer (com.jd.blockchain.consensus.mq.producer.MsgQueueProducer)1 MsgQueueServerSettings (com.jd.blockchain.consensus.mq.settings.MsgQueueServerSettings)1 RaftConfig (com.jd.blockchain.consensus.raft.config.RaftConfig)1 RaftConsensusConfig (com.jd.blockchain.consensus.raft.config.RaftConsensusConfig)1