Search in sources :

Example 1 with BlockSyncException

use of com.jd.blockchain.consensus.raft.consensus.BlockSyncException in project jdchain-core by blockchain-jd-com.

the class BlockSyncService method onMessage.

@Override
public void onMessage(byte[] message) {
    long untilHeight = Longs.fromByteArray(message);
    long latestBlockHeight = repository.retrieveLatestBlockHeight();
    if (latestBlockHeight >= untilHeight) {
        LoggerUtils.debugIfEnabled(LOGGER, "sync height: {} less than current latest height: {}", untilHeight, latestBlockHeight);
        return;
    }
    if (RaftNodeServerContext.getInstance().isLeader(repository.getHash())) {
        LoggerUtils.debugIfEnabled(LOGGER, "current node is leader, can not sync block");
        return;
    }
    PeerId leader = RaftNodeServerContext.getInstance().getLeader(repository.getHash());
    if (leader == null) {
        LoggerUtils.errorIfEnabled(LOGGER, "current leader is null");
        return;
    }
    ServiceEndpoint consensusNodeManagerInfo = getConsensusNodeManagerInfo(leader.getEndpoint());
    LOGGER.debug("get leader: {} manager info: {}", leader, consensusNodeManagerInfo);
    if (consensusNodeManagerInfo == null) {
        LoggerUtils.errorIfEnabled(LOGGER, "get leader: {} manager info is null", leader);
        return;
    }
    isSyncing = true;
    try {
        for (long height = latestBlockHeight + 1; height <= untilHeight; height++) {
            try {
                sync(consensusNodeManagerInfo, repository.getHash(), height);
            } catch (BlockSyncException e) {
                LOGGER.error("sync height: {} error", height, e);
                break;
            }
        }
    } finally {
        isSyncing = false;
    }
}
Also used : BlockSyncException(com.jd.blockchain.consensus.raft.consensus.BlockSyncException) ServiceEndpoint(com.jd.httpservice.agent.ServiceEndpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 2 with BlockSyncException

use of com.jd.blockchain.consensus.raft.consensus.BlockSyncException in project jdchain-core by blockchain-jd-com.

the class BlockSyncService method sync.

public void sync(ServiceEndpoint serviceEndpoint, HashDigest ledger, long height) throws BlockSyncException {
    if (!repository.getHash().equals(ledger)) {
        LOGGER.error("sync ledger not match, expect: {} actual: {}", ledger, repository.getHash());
        return;
    }
    try {
        try (ServiceConnection httpConnection = ServiceConnectionManager.connect(serviceEndpoint)) {
            HttpBlockchainBrowserService queryService = HttpServiceAgent.createService(HttpBlockchainBrowserService.class, httpConnection, null);
            LedgerBlock block = queryService.getBlock(ledger, height);
            sync(queryService, ledger, block);
        }
    } catch (Exception e) {
        throw new BlockSyncException(e);
    }
}
Also used : HttpBlockchainBrowserService(com.jd.blockchain.sdk.proxy.HttpBlockchainBrowserService) ServiceConnection(com.jd.httpservice.agent.ServiceConnection) BlockSyncException(com.jd.blockchain.consensus.raft.consensus.BlockSyncException) BlockSyncException(com.jd.blockchain.consensus.raft.consensus.BlockSyncException)

Example 3 with BlockSyncException

use of com.jd.blockchain.consensus.raft.consensus.BlockSyncException in project jdchain-core by blockchain-jd-com.

the class BlockSyncService method sync.

@Override
public void sync(Endpoint peerEndpoint, HashDigest ledger, long height) throws BlockSyncException {
    ServiceEndpoint consensusNodeManagerInfo = getConsensusNodeManagerInfo(peerEndpoint);
    if (consensusNodeManagerInfo == null) {
        LoggerUtils.errorIfEnabled(LOGGER, "get peer: {} manager info is null", peerEndpoint);
        throw new BlockSyncException("get peer manager info is null");
    }
    sync(consensusNodeManagerInfo, ledger, height);
}
Also used : BlockSyncException(com.jd.blockchain.consensus.raft.consensus.BlockSyncException) ServiceEndpoint(com.jd.httpservice.agent.ServiceEndpoint)

Aggregations

BlockSyncException (com.jd.blockchain.consensus.raft.consensus.BlockSyncException)3 ServiceEndpoint (com.jd.httpservice.agent.ServiceEndpoint)2 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 HttpBlockchainBrowserService (com.jd.blockchain.sdk.proxy.HttpBlockchainBrowserService)1 ServiceConnection (com.jd.httpservice.agent.ServiceConnection)1