Search in sources :

Example 1 with BlockCommittedException

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

the class BlockCommitService method commitBlock.

public boolean commitBlock(Block block, BlockClosure done) throws BlockCommittedException {
    boolean result = true;
    long latestBlockHeight = ledgerRepository.retrieveLatestBlockHeight();
    if (latestBlockHeight >= block.getHeight()) {
        throw new BlockCommittedException(block.getHeight());
    }
    if (latestBlockHeight + 1 != block.getHeight()) {
        notifyCatchUp(block.getHeight());
        LOGGER.error("commit block ignore. expect height:{}, latest block: {}", block.getHeight(), latestBlockHeight);
        return false;
    }
    RaftConsensusMessageContext context = RaftConsensusMessageContext.createContext(realmName);
    context.setTimestamp(block.getProposalTimestamp());
    String batch = messageHandle.beginBatch(context);
    context.setBatchId(batch);
    LoggerUtils.debugIfEnabled(LOGGER, "commit block start, batchId: {}", batch);
    Status status = Status.OK();
    try {
        int msgId = 0;
        for (byte[] tx : block.getTxs()) {
            AsyncFuture<byte[]> asyncFuture = messageHandle.processOrdered(msgId++, tx, context);
            Optional.ofNullable(done).ifPresent(d -> d.addFuture(asyncFuture));
        }
        messageHandle.completeBatch(context);
        // todo ?
        messageHandle.commitBatch(context);
        LedgerBlock repositoryLatestBlock = ledgerRepository.getLatestBlock();
        assert repositoryLatestBlock.getHeight() == block.getHeight();
        block.setPreBlockHash(repositoryLatestBlock.getPreviousHash());
        block.setCurrentBlockHash(repositoryLatestBlock.getHash());
        blockCommitCallbackList.forEach(c -> c.commitCallBack(block, true));
    } catch (Exception e) {
        LOGGER.error("commitBlock error", e);
        result = false;
        messageHandle.rollbackBatch(TransactionState.CONSENSUS_ERROR.CODE, context);
        status = new Status(TransactionState.CONSENSUS_ERROR.CODE, e.getMessage());
        blockCommitCallbackList.forEach(c -> c.commitCallBack(block, false));
    }
    LoggerUtils.debugIfEnabled(LOGGER, "commit block end, batchId: {}, blockHeight: {}, status: {}", batch, block.getHeight(), status);
    if (done != null) {
        done.run(status);
    }
    return result;
}
Also used : Status(com.alipay.sofa.jraft.Status) Longs(com.google.common.primitives.Longs) Logger(org.slf4j.Logger) MessageHandle(com.jd.blockchain.consensus.service.MessageHandle) TransactionState(com.jd.blockchain.ledger.TransactionState) BlockCommitCallback(com.jd.blockchain.consensus.raft.consensus.BlockCommitCallback) MessageBus(com.jd.blockchain.consensus.raft.msgbus.MessageBus) BlockCommittedException(com.jd.blockchain.consensus.raft.consensus.BlockCommittedException) LoggerFactory(org.slf4j.LoggerFactory) LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) Status(com.alipay.sofa.jraft.Status) LoggerUtils(com.jd.blockchain.consensus.raft.util.LoggerUtils) ArrayList(java.util.ArrayList) List(java.util.List) AsyncFuture(utils.concurrent.AsyncFuture) BLOCK_CATCH_UP_TOPIC(com.jd.blockchain.consensus.raft.msgbus.MessageBus.BLOCK_CATCH_UP_TOPIC) Block(com.jd.blockchain.consensus.raft.consensus.Block) Optional(java.util.Optional) BlockCommitter(com.jd.blockchain.consensus.raft.consensus.BlockCommitter) LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) BlockCommittedException(com.jd.blockchain.consensus.raft.consensus.BlockCommittedException) BlockCommittedException(com.jd.blockchain.consensus.raft.consensus.BlockCommittedException)

Aggregations

Status (com.alipay.sofa.jraft.Status)1 Longs (com.google.common.primitives.Longs)1 Block (com.jd.blockchain.consensus.raft.consensus.Block)1 BlockCommitCallback (com.jd.blockchain.consensus.raft.consensus.BlockCommitCallback)1 BlockCommittedException (com.jd.blockchain.consensus.raft.consensus.BlockCommittedException)1 BlockCommitter (com.jd.blockchain.consensus.raft.consensus.BlockCommitter)1 MessageBus (com.jd.blockchain.consensus.raft.msgbus.MessageBus)1 BLOCK_CATCH_UP_TOPIC (com.jd.blockchain.consensus.raft.msgbus.MessageBus.BLOCK_CATCH_UP_TOPIC)1 LoggerUtils (com.jd.blockchain.consensus.raft.util.LoggerUtils)1 MessageHandle (com.jd.blockchain.consensus.service.MessageHandle)1 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)1 TransactionState (com.jd.blockchain.ledger.TransactionState)1 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 AsyncFuture (utils.concurrent.AsyncFuture)1