Search in sources :

Example 1 with InvocationResult

use of utils.concurrent.InvocationResult 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)

Aggregations

PubKey (com.jd.blockchain.crypto.PubKey)1 SignatureDigest (com.jd.blockchain.crypto.SignatureDigest)1 LedgerInitException (com.jd.blockchain.ledger.LedgerInitException)1 ParticipantNode (com.jd.blockchain.ledger.ParticipantNode)1 LedgerInitProposal (com.jd.blockchain.ledger.core.LedgerInitProposal)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 InvocationResult (utils.concurrent.InvocationResult)1