Search in sources :

Example 1 with NetworkReplica

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

the class BftsmartConsensusSettingsBuilder method createSettings.

@Override
public BftsmartConsensusViewSettings createSettings(Properties props, Replica[] participantNodes) {
    Properties resolvingProps = PropertiesUtils.cloneFrom(props);
    int serversNum = PropertiesUtils.getInt(resolvingProps, SERVER_NUM_KEY);
    if (serversNum < 0) {
        throw new IllegalArgumentException(String.format("Property[%s] is negative!", SERVER_NUM_KEY));
    }
    if (serversNum < 4) {
        throw new IllegalArgumentException(String.format("Property[%s] is less than 4!", SERVER_NUM_KEY));
    }
    if (participantNodes == null) {
        throw new IllegalArgumentException("ParticipantNodes is Empty !!!");
    }
    // if (serversNum != participantNodes.length) {
    // throw new IllegalArgumentException(
    // String.format("Property[%s] which is [%s] unequal " + "ParticipantNodes's length which is [%s] !",
    // SERVER_NUM_KEY, serversNum, participantNodes.length));
    // }
    serversNum = participantNodes.length;
    // BftsmartCommitBlockConfig blockConfig = createBlockConfig(resolvingProps);
    BftsmartNodeSettings[] nodesSettings = new BftsmartNodeSettings[serversNum];
    for (int i = 0; i < serversNum; i++) {
        int id = i;
        // String keyOfPubkey = keyOfNode(PUBKEY_PATTERN, id);
        // String base58PubKey = PropertiesUtils.getRequiredProperty(resolvingProps, keyOfPubkey);
        // PubKey pubKey = new PubKey(Base58Utils.decode(base58PubKey));
        // PubKey pubKey = KeyGenCommand.decodePubKey(base58PubKey);
        PubKey pubKey = participantNodes[i].getPubKey();
        // resolvingProps.remove(keyOfPubkey);
        String keyOfHost = keyOfNode(CONSENSUS_HOST_PATTERN, participantNodes[id].getId());
        String networkAddressHost = PropertiesUtils.getOptionalProperty(resolvingProps, keyOfHost, null);
        resolvingProps.remove(keyOfHost);
        String keyOfPort = keyOfNode(CONSENSUS_PORT_PATTERN, participantNodes[id].getId());
        int networkAddressPort = PropertiesUtils.getIntOptional(resolvingProps, keyOfPort, -1);
        resolvingProps.remove(keyOfPort);
        String keyOfSecure = keyOfNode(CONSENSUS_SECURE_PATTERN, participantNodes[id].getId());
        boolean networkAddressSecure = PropertiesUtils.getBooleanOptional(resolvingProps, keyOfSecure, false);
        resolvingProps.remove(keyOfSecure);
        if (participantNodes[i] instanceof NetworkReplica) {
            NetworkReplica replica = (NetworkReplica) participantNodes[i];
            networkAddressHost = replica.getNetworkAddress().getHost();
            networkAddressPort = replica.getNetworkAddress().getPort();
            networkAddressSecure = replica.getNetworkAddress().isSecure();
        }
        BftsmartNodeConfig nodeConfig = new BftsmartNodeConfig(pubKey, participantNodes[id].getId(), new NetworkAddress(networkAddressHost, networkAddressPort, networkAddressSecure));
        nodesSettings[i] = nodeConfig;
    }
    BftsmartConsensusConfig config = new BftsmartConsensusConfig(nodesSettings, // blockConfig,
    PropertiesUtils.getOrderedValues(resolvingProps), 0);
    return config;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) NetworkAddress(utils.net.NetworkAddress) Properties(java.util.Properties) NetworkReplica(com.jd.blockchain.consensus.NetworkReplica)

Aggregations

NetworkReplica (com.jd.blockchain.consensus.NetworkReplica)1 PubKey (com.jd.blockchain.crypto.PubKey)1 Properties (java.util.Properties)1 NetworkAddress (utils.net.NetworkAddress)1