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;
}
}
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);
}
}
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);
}
Aggregations