Search in sources :

Example 1 with ViewMessage

use of bftsmart.tom.core.messages.ViewMessage in project bftsmart by blockchain-jd-com.

the class MessageHandler method processData.

@SuppressWarnings("unchecked")
public void processData(SystemMessage sm) {
    if (sm instanceof ConsensusMessage) {
        int myId = tomLayer.controller.getStaticConf().getProcessId();
        ConsensusMessage consMsg = (ConsensusMessage) sm;
        if (// 
        consMsg.getSender() == myId || // 
        (!tomLayer.controller.getStaticConf().isUseMACs()) || consMsg.authenticated) {
            acceptor.deliver(consMsg);
        } else if (consMsg.getType() == MessageFactory.ACCEPT && consMsg.getProof() != null) {
            // We are going to verify the MAC vector at the algorithm level
            HashMap<Integer, byte[]> macVector = (HashMap<Integer, byte[]>) consMsg.getProof();
            byte[] recvMAC = macVector.get(myId);
            ConsensusMessage cm = new ConsensusMessage(MessageFactory.ACCEPT, consMsg.getNumber(), consMsg.getEpoch(), consMsg.getSender(), consMsg.getValue());
            ByteArrayOutputStream bOut = new ByteArrayOutputStream(248);
            try {
                new ObjectOutputStream(bOut).writeObject(cm);
            } catch (IOException ex) {
                throw new IllegalStateException("Error occurred while serializing ConsensusMessage[" + cm.toString() + "]! --" + ex.getMessage(), ex);
            }
            byte[] data = bOut.toByteArray();
            // byte[] hash = tomLayer.computeHash(data);
            byte[] myMAC = null;
            MacMessageCodec<SystemMessage> msgCodec = tomLayer.getCommunication().getServersCommunication().getMessageCodec(consMsg.getSender());
            MacKey macKey = msgCodec.getMacKey();
            if (macKey != null) {
                myMAC = macKey.generateMac(data);
            }
            if (recvMAC != null && myMAC != null && Arrays.equals(recvMAC, myMAC))
                acceptor.deliver(consMsg);
            else {
                LOGGER.error("(MessageHandler.processData) WARNING: invalid MAC from {}", sm.getSender());
            }
        } else {
            LOGGER.error("(MessageHandler.processData) Discarding unauthenticated message from {}", sm.getSender());
        }
    } else if (sm instanceof HeartBeatMessage) {
        // 心跳消息
        tomLayer.heartBeatTimer.receiveHeartBeatMessage((HeartBeatMessage) sm);
    } else if (sm instanceof ViewMessage) {
        // 视图消息
        // 通过该消息可更新本地视图
        ViewMessage viewMessage = (ViewMessage) sm;
        int remoteId = viewMessage.getSender();
        View view = viewMessage.getView();
        if (view != null) {
            tomLayer.viewSyncTimer.updateView(remoteId, view);
        }
    } else if (sm instanceof LeaderRequestMessage) {
        // 获取Leader节点请求的消息
        tomLayer.heartBeatTimer.receiveLeaderRequestMessage((LeaderRequestMessage) sm);
    } else if (sm instanceof LeaderResponseMessage) {
        // 获取Leader节点请求的消息
        tomLayer.heartBeatTimer.receiveLeaderResponseMessage((LeaderResponseMessage) sm);
    } else if (sm instanceof LeaderStatusResponseMessage) {
        // 该处理顺序必须在前面,因为LeaderStatusResponseMessage继承自LeaderStatusRequestMessage
        tomLayer.heartBeatTimer.receiveLeaderStatusResponseMessage((LeaderStatusResponseMessage) sm);
    } else if (sm instanceof LeaderStatusRequestMessage) {
        tomLayer.heartBeatTimer.receiveLeaderStatusRequestMessage((LeaderStatusRequestMessage) sm);
    } else {
        if ((!tomLayer.controller.getStaticConf().isUseMACs()) || sm.authenticated) {
            /**
             * This is Joao's code, related to leader change
             */
            if (sm instanceof LCMessage) {
                LCMessage lcMsg = (LCMessage) sm;
                tomLayer.getSynchronizer().deliverTimeoutRequest(lcMsg);
            /**
             ***********************************************************
             */
            } else if (sm instanceof ForwardedMessage) {
                TOMMessage request = ((ForwardedMessage) sm).getRequest();
                tomLayer.requestReceived(request);
            /**
             * This is Joao's code, to handle state transfer
             */
            } else if (sm instanceof SMMessage) {
                SMMessage smsg = (SMMessage) sm;
                LOGGER.debug("I am {}, receive SMMessage[{}], type = {} !", tomLayer.controller.getStaticConf().getProcessId(), smsg.getSender(), smsg.getType());
                switch(smsg.getType()) {
                    case TOMUtil.SM_REQUEST:
                        tomLayer.getStateManager().SMRequestDeliver(smsg, tomLayer.controller.getStaticConf().isBFT());
                        break;
                    case TOMUtil.SM_REPLY:
                        tomLayer.getStateManager().SMReplyDeliver(smsg, tomLayer.controller.getStaticConf().isBFT());
                        break;
                    case TOMUtil.SM_ASK_INITIAL:
                        tomLayer.getStateManager().currentConsensusIdAsked(smsg.getSender(), smsg.getView().getId());
                        break;
                    case TOMUtil.SM_REPLY_INITIAL:
                        tomLayer.getStateManager().currentConsensusIdReceived(smsg);
                        break;
                    default:
                        tomLayer.getStateManager().stateTimeout();
                        break;
                }
            /**
             ***************************************************************
             */
            } else if (sm instanceof StandardTRMessage) {
                StandardTRMessage trMessage = (StandardTRMessage) sm;
                LOGGER.info("I am {}, receive StandardTRMessage[{}], type = {} !", tomLayer.controller.getStaticConf().getProcessId(), trMessage.getSender(), trMessage.getType());
                switch(trMessage.getType()) {
                    case TOMUtil.SM_TRANSACTION_REPLAY_REQUEST_INFO:
                        tomLayer.getStateManager().transactionReplayAsked(trMessage.sender, trMessage.getTarget(), trMessage.getStartCid(), trMessage.getEndCid());
                        break;
                    case TOMUtil.SM_TRANSACTION_REPLAY_REPLY_INFO:
                        tomLayer.getStateManager().transactionReplayReplyDeliver(trMessage);
                        break;
                    default:
                        break;
                }
            } else {
                LOGGER.error("UNKNOWN MESSAGE TYPE: {}", sm);
            }
        } else {
            LOGGER.error("(MessageHandler.processData) Discarding unauthenticated message from {}", sm.getSender());
        }
    }
}
Also used : ViewMessage(bftsmart.tom.core.messages.ViewMessage) TOMMessage(bftsmart.tom.core.messages.TOMMessage) ForwardedMessage(bftsmart.tom.core.messages.ForwardedMessage) SMMessage(bftsmart.statemanagement.SMMessage) HashMap(java.util.HashMap) HeartBeatMessage(bftsmart.tom.leaderchange.HeartBeatMessage) LeaderResponseMessage(bftsmart.tom.leaderchange.LeaderResponseMessage) LeaderStatusResponseMessage(bftsmart.tom.leaderchange.LeaderStatusResponseMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) View(bftsmart.reconfiguration.views.View) ConsensusMessage(bftsmart.consensus.messages.ConsensusMessage) LeaderStatusRequestMessage(bftsmart.tom.leaderchange.LeaderStatusRequestMessage) LCMessage(bftsmart.tom.leaderchange.LCMessage) StandardTRMessage(bftsmart.statemanagement.strategy.StandardTRMessage) LeaderRequestMessage(bftsmart.tom.leaderchange.LeaderRequestMessage)

Example 2 with ViewMessage

use of bftsmart.tom.core.messages.ViewMessage in project bftsmart by blockchain-jd-com.

the class ViewSyncTimer method start.

public void start() {
    sendThreadPool.scheduleWithFixedDelay(() -> {
        lock.lock();
        try {
            View view = tomLayer.controller.getCurrentView();
            ViewMessage viewMessage = new ViewMessage(processId, view);
            int[] otherAcceptors = tomLayer.controller.getCurrentViewOtherAcceptors();
            LOGGER.info("I am {}, my view -> {} !", processId, view);
            for (int id : otherAcceptors) {
                LOGGER.info("I am {}, send view message to {} !", processId, id);
            }
            tomLayer.getCommunication().send(tomLayer.controller.getCurrentViewOtherAcceptors(), viewMessage);
        } finally {
            lock.unlock();
        }
    }, SEND_DELAY, SEND_PERIOD, TimeUnit.MILLISECONDS);
}
Also used : ViewMessage(bftsmart.tom.core.messages.ViewMessage) View(bftsmart.reconfiguration.views.View)

Aggregations

View (bftsmart.reconfiguration.views.View)2 ViewMessage (bftsmart.tom.core.messages.ViewMessage)2 ConsensusMessage (bftsmart.consensus.messages.ConsensusMessage)1 SMMessage (bftsmart.statemanagement.SMMessage)1 StandardTRMessage (bftsmart.statemanagement.strategy.StandardTRMessage)1 ForwardedMessage (bftsmart.tom.core.messages.ForwardedMessage)1 TOMMessage (bftsmart.tom.core.messages.TOMMessage)1 HeartBeatMessage (bftsmart.tom.leaderchange.HeartBeatMessage)1 LCMessage (bftsmart.tom.leaderchange.LCMessage)1 LeaderRequestMessage (bftsmart.tom.leaderchange.LeaderRequestMessage)1 LeaderResponseMessage (bftsmart.tom.leaderchange.LeaderResponseMessage)1 LeaderStatusRequestMessage (bftsmart.tom.leaderchange.LeaderStatusRequestMessage)1 LeaderStatusResponseMessage (bftsmart.tom.leaderchange.LeaderStatusResponseMessage)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectOutputStream (java.io.ObjectOutputStream)1 HashMap (java.util.HashMap)1