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);
}
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;
}
Aggregations