Search in sources :

Example 16 with PubKey

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;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) LedgerInitProposal(com.jd.blockchain.ledger.core.LedgerInitProposal) ParticipantNode(com.jd.blockchain.ledger.ParticipantNode) SignatureDigest(com.jd.blockchain.crypto.SignatureDigest) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) InvocationResult(utils.concurrent.InvocationResult) LedgerInitException(com.jd.blockchain.ledger.LedgerInitException)

Example 17 with PubKey

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();
        });
    }
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) NodeSettings(com.jd.blockchain.consensus.NodeSettings) BftsmartNodeSettings(com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings) BftsmartServerSettingConfig(com.jd.blockchain.consensus.bftsmart.service.BftsmartServerSettingConfig) BftsmartNodeSettings(com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings) NetworkAddress(utils.net.NetworkAddress) BftsmartConsensusConfig(com.jd.blockchain.consensus.bftsmart.BftsmartConsensusConfig) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) BftsmartNodeConfig(com.jd.blockchain.consensus.bftsmart.BftsmartNodeConfig) BftsmartNodeServer(com.jd.blockchain.consensus.bftsmart.service.BftsmartNodeServer) MemoryStorage(utils.io.MemoryStorage)

Example 18 with PubKey

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;
}
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 19 with PubKey

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;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) Bytes(utils.Bytes) MsgQueueConsensusConfig(com.jd.blockchain.consensus.mq.config.MsgQueueConsensusConfig) MsgQueueBlockConfig(com.jd.blockchain.consensus.mq.config.MsgQueueBlockConfig) MsgQueueNodeConfig(com.jd.blockchain.consensus.mq.config.MsgQueueNodeConfig) MsgQueueNetworkConfig(com.jd.blockchain.consensus.mq.config.MsgQueueNetworkConfig) Properties(java.util.Properties)

Example 20 with PubKey

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;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) NodeSettings(com.jd.blockchain.consensus.NodeSettings) NetworkAddress(utils.net.NetworkAddress)

Aggregations

PubKey (com.jd.blockchain.crypto.PubKey)21 SignatureFunction (com.jd.blockchain.crypto.SignatureFunction)6 SignatureDigest (com.jd.blockchain.crypto.SignatureDigest)5 Bytes (utils.Bytes)5 PrivKey (com.jd.blockchain.crypto.PrivKey)4 ParticipantNode (com.jd.blockchain.ledger.ParticipantNode)4 NetworkAddress (utils.net.NetworkAddress)4 NodeSettings (com.jd.blockchain.consensus.NodeSettings)3 LedgerInitException (com.jd.blockchain.ledger.LedgerInitException)3 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 MsgQueueConsensusConfig (com.jd.blockchain.consensus.mq.config.MsgQueueConsensusConfig)2 MsgQueueNodeConfig (com.jd.blockchain.consensus.mq.config.MsgQueueNodeConfig)2 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)2 LedgerException (com.jd.blockchain.ledger.LedgerException)2 NetworkReplica (com.jd.blockchain.consensus.NetworkReplica)1 BftsmartClientAuthCredit (com.jd.blockchain.consensus.bftsmart.BftsmartClientAuthCredit)1 BftsmartConsensusConfig (com.jd.blockchain.consensus.bftsmart.BftsmartConsensusConfig)1 BftsmartNodeConfig (com.jd.blockchain.consensus.bftsmart.BftsmartNodeConfig)1 BftsmartNodeSettings (com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings)1