Search in sources :

Example 1 with LedgerInitDecision

use of com.jd.blockchain.ledger.core.LedgerInitDecision in project jdchain-core by blockchain-jd-com.

the class LedgerInitializeWebController method doSynchronizeDecision.

/**
 * 与指定的参与方同步“账本初始化决定({@link LedgerInitDecision})”;
 *
 * @param targetId
 * @param privKey
 * @param resultHandle
 * @param prompter
 * @return
 */
private boolean doSynchronizeDecision(int targetId, PrivKey privKey, DecisionResultHandle resultHandle, Prompter prompter) {
    try {
        LedgerInitConsensusService initConsensus = connectToParticipant(targetId);
        LedgerInitDecision targetDecision = null;
        int retry = 0;
        do {
            prompter.info("Start synchronizling decision from participant[%s] to participant[%s] ......", currentId, targetId);
            try {
                targetDecision = initConsensus.synchronizeDecision(localDecision);
            } catch (Exception e1) {
                prompter.info("Error occurred on synchronizing decision . --[%s] %s", e1.getClass().getName(), e1.getMessage());
            }
            if (targetDecision == null) {
                if (resultHandle.getValue() != null) {
                    // 已经验证过;
                    return true;
                }
                // 对方的账本初始化尚未就绪;隔5秒重试;重试超过3后提示确认是否继续重试;
                if (retry == 16) {
                    String r = prompter.confirm("Target participant[%s] isn't ready to do decision! Do you want to retry again?\r\n" + " Press 'Y' to retry, and any others to break this participant and to continue synchronizing with others. :>", targetId);
                    if (!"Y".equalsIgnoreCase(r)) {
                        return false;
                    }
                    retry = 0;
                }
                prompter.info("Target participant[%s] isn't ready to do decision! Waiting 5 seconds and retry again...", targetId);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                // ignore InterruptedException;
                }
                retry++;
            }
        } while (targetDecision == null);
        if (targetDecision.getParticipantId() != targetId && resultHandle.getValue() == null) {
            prompter.error("The received id of participant isn't equal to id of request target participant! --[Id=%s]", targetId);
            return false;
        }
        return validateAndRecordDecision(targetDecision, resultHandle);
    } catch (Exception e) {
        prompter.error(e, "Error occurred on synchronizing decision from participant[%s] to participant[%s] ! --%s", currentId, targetId, e.getMessage());
        return false;
    }
}
Also used : LedgerInitDecision(com.jd.blockchain.ledger.core.LedgerInitDecision) IOException(java.io.IOException) LedgerInitException(com.jd.blockchain.ledger.LedgerInitException)

Aggregations

LedgerInitException (com.jd.blockchain.ledger.LedgerInitException)1 LedgerInitDecision (com.jd.blockchain.ledger.core.LedgerInitDecision)1 IOException (java.io.IOException)1