use of com.jd.blockchain.crypto.PubKey in project jdchain-core by blockchain-jd-com.
the class LedgerInitializeWebController method startRequestPermissions.
/**
* 请求所有其它参与方的账本创建许可;
*
* @param privKey
* @return
*/
private boolean startRequestPermissions(int currentId, PrivKey privKey) {
SignatureDigest reqAuthSign = signPermissionRequest(currentId, privKey);
ParticipantNode[] participants = ledgerInitConfig.getParticipants();
// 异步请求结果列表;不包括已经获得许可的参与方;
InvocationResult<?>[] results = new InvocationResult<?>[participants.length];
int unpermittedCount = 0;
for (int i = 0; i < participants.length; i++) {
if (this.permissions[i] == null) {
unpermittedCount++;
}
}
// 发起请求;
CountDownLatch latch = new CountDownLatch(unpermittedCount);
for (int i = 0; i < participants.length; i++) {
if (participants[i].getParticipantNodeState() != ParticipantNodeState.CONSENSUS) {
latch.countDown();
continue;
}
if (this.permissions[i] == null) {
results[i] = doRequestPermission(participants[i].getId(), reqAuthSign, latch);
}
}
// 等待结果;
try {
while (!latch.await(5000, TimeUnit.MILLISECONDS)) {
List<String> waitingIds = new ArrayList<>();
for (int i = 0; i < results.length; i++) {
if (results[i] != null) {
if (results[i].getValue() == null) {
waitingIds.add("" + (i + 1));
}
}
}
prompter.info("\r\nWaiting for permissions of participants[%s] ...", String.join(",", waitingIds));
}
} catch (InterruptedException e) {
throw new LedgerInitException("Process of requesting participant permissions was interrupted! --" + e.getMessage(), e);
}
// 校验接入许可;
boolean allPermitted = true;
for (int i = 0; i < results.length; i++) {
if (results[i] == null) {
// 忽略自己;
continue;
}
if (results[i].getError() != null) {
prompter.error(results[i].getError(), "Error occurred on requesting permission from participant[Id=%s, name=%s]!", participants[i].getAddress(), participants[i].getName());
allPermitted = false;
continue;
}
PubKey pubKey = participants[i].getPubKey();
LedgerInitProposal permission = (LedgerInitProposal) results[i].getValue();
if (permission.getParticipantId() != participants[i].getId()) {
prompter.error("\r\nThe id of received permission isn't equal to it's participant ! --[Id=%s][name=%s]", participants[i].getAddress(), participants[i].getName());
allPermitted = false;
continue;
}
if (!SignatureUtils.verifySignature(initializer.getCryptoSetting().getHashAlgorithm(), initializer.getTransactionContent(), permission.getTransactionSignature(), pubKey)) {
prompter.error("Invalid permission from participant! --[Id=%s][name=%s]", participants[i].getAddress(), participants[i].getName());
allPermitted = false;
continue;
}
this.permissions[i] = permission;
}
return allPermitted;
}
use of com.jd.blockchain.crypto.PubKey in project jdchain-core by blockchain-jd-com.
the class ProxyClientTest method peerStart.
public void peerStart(BftsmartNodeServer[] nodeServers) {
BftsmartNodeSettings[] nodesSettings = new BftsmartNodeSettings[nodeNum];
for (int i = 0; i < nodeNum; i++) {
BlockchainKeypair keyPair = BlockchainKeyGenerator.getInstance().generate();
PubKey pubKey = keyPair.getPubKey();
NetworkAddress peerNodeServ = new NetworkAddress("127.0.0.1", peerStartPort + i * 10);
NodeSettings node = new BftsmartNodeConfig(pubKey, i, peerNodeServ);
nodesSettings[i] = (BftsmartNodeSettings) node;
}
BftsmartConsensusConfig consensusConfig = new BftsmartConsensusConfig(nodesSettings, PropertiesUtils.getOrderedValues(bftsmartConf), 0);
for (int j = 0; j < nodeNum; j++) {
BftsmartServerSettingConfig serverSettings = new BftsmartServerSettingConfig();
serverSettings.setReplicaSettings(nodesSettings[j]);
serverSettings.setConsensusSettings(consensusConfig);
BftsmartNodeServer server = new BftsmartNodeServer(serverSettings, null, null, new MemoryStorage("test"));
nodeServers[j] = server;
nodeStartPools.execute(() -> {
server.start();
startPeer.countDown();
});
}
}
use of com.jd.blockchain.crypto.PubKey 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;
}
use of com.jd.blockchain.crypto.PubKey in project jdchain-core by blockchain-jd-com.
the class MsgQueueConsensusSettingsBuilder method createSettings.
@Override
public MsgQueueConsensusSettings createSettings(Properties props, Replica[] participantNodes) {
MsgQueueNetworkConfig networkConfig = new MsgQueueNetworkConfig();
Properties resolvingProps = PropertiesUtils.cloneFrom(props);
String server = PropertiesUtils.getProperty(resolvingProps, MSG_QUEUE_SERVER, true);
if (server == null || server.length() <= 0) {
throw new IllegalArgumentException(String.format("Property[%s] is empty!", MSG_QUEUE_SERVER));
}
networkConfig.setServer(server).setTxTopic(initProp(resolvingProps, MSG_QUEUE_TOPIC_TX, DEFAULT_TOPIC_TX)).setTxResultTopic(initProp(resolvingProps, MSG_QUEUE_TOPIC_TX_RESULT, DEFAULT_TOPIC_TX_RESULT)).setBlockTopic(initProp(resolvingProps, MSG_QUEUE_TOPIC_BLOCK, DEFAULT_TOPIC_BLOCK)).setMsgTopic(initProp(resolvingProps, MSG_QUEUE_TOPIC_MSG, DEFAULT_TOPIC_MSG)).setMsgResultTopic(initProp(resolvingProps, MSG_QUEUE_TOPIC_MSG_RESULT, DEFAULT_TOPIC_MSG_RESULT));
MsgQueueBlockConfig blockConfig = new MsgQueueBlockConfig().setTxSizePerBlock(initProp(resolvingProps, MSG_QUEUE_BLOCK_TXSIZE, DEFAULT_TXSIZE)).setMaxDelayMilliSecondsPerBlock(initProp(resolvingProps, MSG_QUEUE_BLOCK_MAXDELAY, DEFAULT_MAXDELAY));
MsgQueueConsensusConfig consensusConfig = new MsgQueueConsensusConfig().setBlockSettings(blockConfig).setNetworkSettings(networkConfig);
// load node settings
int serversNum = PropertiesUtils.getInt(resolvingProps, SERVER_NUM_KEY);
for (int i = 0; i < serversNum; i++) {
int id = i;
String keyOfPubkey = nodeKey(PUBKEY_PATTERN, id);
String base58PubKey = PropertiesUtils.getRequiredProperty(resolvingProps, keyOfPubkey);
PubKey pubKey = KeyGenUtils.decodePubKey(base58PubKey);
// PubKey pubKey = new PubKey(Base58Utils.decode(base58PubKey));
resolvingProps.remove(keyOfPubkey);
Bytes address = AddressEncoding.generateAddress(pubKey);
String networkAddress = address.toBase58();
MsgQueueNodeConfig nodeConfig = new MsgQueueNodeConfig().setAddress(networkAddress).setPubKey(pubKey).setId(id);
consensusConfig.addNodeSettings(nodeConfig);
}
return consensusConfig;
}
use of com.jd.blockchain.crypto.PubKey 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