Search in sources :

Example 16 with View

use of bftsmart.reconfiguration.views.View in project jdchain-core by blockchain-jd-com.

the class ParticipantManagerService4Bft method createPeerProxy.

private ServiceProxy createPeerProxy(Properties systemConfig, int viewId, List<NodeSettings> origConsensusNodes, SSLSecurity security) {
    HostsConfig hostsConfig;
    List<HostsConfig.Config> configList = new ArrayList<>();
    List<NodeNetwork> nodeAddresses = new ArrayList<>();
    try {
        int[] origConsensusProcesses = new int[origConsensusNodes.size()];
        for (int i = 0; i < origConsensusNodes.size(); i++) {
            BftsmartNodeSettings node = (BftsmartNodeSettings) origConsensusNodes.get(i);
            origConsensusProcesses[i] = node.getId();
            configList.add(new HostsConfig.Config(node.getId(), node.getNetworkAddress().getHost(), node.getNetworkAddress().getPort(), -1, node.getNetworkAddress().isSecure(), false));
            nodeAddresses.add(new NodeNetwork(node.getNetworkAddress().getHost(), node.getNetworkAddress().getPort(), -1, node.getNetworkAddress().isSecure(), false));
        }
        // 构建共识的代理客户端需要的主机配置和系统参数配置结构
        hostsConfig = new HostsConfig(configList.toArray(new HostsConfig.Config[configList.size()]));
        Properties tempSystemConfig = (Properties) systemConfig.clone();
        // 构建tom 配置
        TOMConfiguration tomConfig = new TOMConfiguration(-(new Random().nextInt(Integer.MAX_VALUE - 2) - 1), tempSystemConfig, hostsConfig);
        View view = new View(viewId, origConsensusProcesses, tomConfig.getF(), nodeAddresses.toArray(new NodeNetwork[nodeAddresses.size()]));
        LOGGER.info("ManagementController start updateView operation!, current view : {}", view.toString());
        // 构建共识的代理客户端,连接目标共识节点,并递交交易进行共识过程
        return new ServiceProxy(tomConfig, new MemoryBasedViewStorage(view), null, null, security);
    } catch (Exception e) {
        throw new CreateProxyClientException("create proxy client exception!");
    }
}
Also used : BftsmartNodeSettings(com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings) HostsConfig(bftsmart.reconfiguration.util.HostsConfig) ArrayList(java.util.ArrayList) HostsConfig(bftsmart.reconfiguration.util.HostsConfig) Properties(java.util.Properties) View(bftsmart.reconfiguration.views.View) Random(java.util.Random) ServiceProxy(bftsmart.tom.ServiceProxy) MemoryBasedViewStorage(bftsmart.reconfiguration.views.MemoryBasedViewStorage) NodeNetwork(bftsmart.reconfiguration.views.NodeNetwork) TOMConfiguration(bftsmart.reconfiguration.util.TOMConfiguration)

Example 17 with View

use of bftsmart.reconfiguration.views.View in project aware by bergerch.

the class ServerViewController method reconfigure.

private byte[] reconfigure(List<String> jSetInfo, List<Integer> jSet, List<Integer> rSet, int f, int cid) {
    // ReconfigureRequest request = (ReconfigureRequest) TOMUtil.getObject(req);
    // Hashtable<Integer, String> props = request.getProperties();
    // int f = Integer.valueOf(props.get(CHANGE_F));
    lastJoinStet = new int[jSet.size()];
    int[] nextV = new int[currentView.getN() + jSet.size() - rSet.size()];
    int p = 0;
    boolean forceLC = false;
    for (int i = 0; i < jSet.size(); i++) {
        lastJoinStet[i] = jSet.get(i);
        nextV[p++] = jSet.get(i);
    }
    for (int i = 0; i < currentView.getProcesses().length; i++) {
        if (!contains(currentView.getProcesses()[i], rSet)) {
            nextV[p++] = currentView.getProcesses()[i];
        } else if (tomLayer.execManager.getCurrentLeader() == currentView.getProcesses()[i]) {
            forceLC = true;
        }
    }
    if (f < 0) {
        f = currentView.getF();
    }
    InetSocketAddress[] addresses = new InetSocketAddress[nextV.length];
    for (int i = 0; i < nextV.length; i++) addresses[i] = getStaticConf().getRemoteAddress(nextV[i]);
    View newV = new View(currentView.getId() + 1, nextV, f, addresses);
    logger.info("New view: " + newV);
    logger.info("Installed on CID: " + cid);
    logger.info("lastJoinSet: " + jSet);
    // TODO:Remove all information stored about each process in rSet
    // processes execute the leave!!!
    reconfigureTo(newV);
    if (forceLC) {
        // TODO: Reactive it and make it work
        logger.info("Shortening LC timeout");
        tomLayer.requestsTimer.stopTimer();
        tomLayer.requestsTimer.setShortTimeout(3000);
        tomLayer.requestsTimer.startTimer();
    // tomLayer.triggerTimeout(new LinkedList<TOMMessage>());
    }
    return TOMUtil.getBytes(new ReconfigureReply(newV, jSetInfo.toArray(new String[0]), cid, tomLayer.execManager.getCurrentLeader()));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) View(bftsmart.reconfiguration.views.View)

Example 18 with View

use of bftsmart.reconfiguration.views.View in project aware by bergerch.

the class ViewManager method executeUpdates.

public void executeUpdates() {
    connect();
    ReconfigureReply r = rec.execute();
    View v = r.getView();
    logger.info("New view f: " + v.getF());
    VMMessage msg = new VMMessage(id, r);
    if (addIds.size() > 0) {
        sendResponse(addIds.toArray(new Integer[1]), msg);
        addIds.clear();
    }
}
Also used : View(bftsmart.reconfiguration.views.View)

Example 19 with View

use of bftsmart.reconfiguration.views.View in project aware by bergerch.

the class StandardStateManager method SMReplyDeliver.

@Override
public void SMReplyDeliver(SMMessage msg, boolean isBFT) {
    lockTimer.lock();
    if (SVController.getStaticConf().isStateTransferEnabled()) {
        if (waitingCID != -1 && msg.getCID() == waitingCID) {
            int currentRegency = -1;
            int currentLeader = -1;
            View currentView = null;
            CertifiedDecision currentProof = null;
            if (!appStateOnly) {
                senderRegencies.put(msg.getSender(), msg.getRegency());
                senderLeaders.put(msg.getSender(), msg.getLeader());
                senderViews.put(msg.getSender(), msg.getView());
                senderProofs.put(msg.getSender(), msg.getState().getCertifiedDecision(SVController));
                if (enoughRegencies(msg.getRegency())) {
                    currentRegency = msg.getRegency();
                }
                if (enoughLeaders(msg.getLeader())) {
                    currentLeader = msg.getLeader();
                }
                if (enoughViews(msg.getView())) {
                    currentView = msg.getView();
                }
                if (enoughProofs(waitingCID, this.tomLayer.getSynchronizer().getLCManager())) {
                    currentProof = msg.getState().getCertifiedDecision(SVController);
                }
            } else {
                currentLeader = tomLayer.execManager.getCurrentLeader();
                currentRegency = tomLayer.getSynchronizer().getLCManager().getLastReg();
                currentView = SVController.getCurrentView();
            }
            if (msg.getSender() == replica && msg.getState().getSerializedState() != null) {
                logger.debug("Expected replica sent state. Setting it to state");
                state = msg.getState();
                if (stateTimer != null) {
                    stateTimer.cancel();
                }
            }
            senderStates.put(msg.getSender(), msg.getState());
            logger.debug("Verifying more than F replies");
            if (enoughReplies()) {
                logger.debug("More than F confirmed");
                ApplicationState otherReplicaState = getOtherReplicaState();
                int haveState = 0;
                if (state != null) {
                    byte[] hash = null;
                    hash = tomLayer.computeHash(state.getSerializedState());
                    if (otherReplicaState != null) {
                        if (Arrays.equals(hash, otherReplicaState.getStateHash())) {
                            haveState = 1;
                        } else if (getNumEqualStates() > SVController.getCurrentViewF()) {
                            haveState = -1;
                        }
                    }
                }
                if (otherReplicaState != null && haveState == 1 && currentRegency > -1 && currentLeader > -1 && currentView != null && (!isBFT || currentProof != null || appStateOnly)) {
                    logger.info("Received state. Will install it");
                    tomLayer.getSynchronizer().getLCManager().setLastReg(currentRegency);
                    tomLayer.getSynchronizer().getLCManager().setNextReg(currentRegency);
                    tomLayer.getSynchronizer().getLCManager().setNewLeader(currentLeader);
                    tomLayer.execManager.setNewLeader(currentLeader);
                    if (currentProof != null && !appStateOnly) {
                        logger.debug("Installing proof for consensus " + waitingCID);
                        Consensus cons = execManager.getConsensus(waitingCID);
                        Epoch e = null;
                        for (ConsensusMessage cm : currentProof.getConsMessages()) {
                            e = cons.getEpoch(cm.getEpoch(), true, SVController);
                            if (e.getTimestamp() != cm.getEpoch()) {
                                logger.warn("Strange... proof contains messages from more than just one epoch");
                                e = cons.getEpoch(cm.getEpoch(), true, SVController);
                            }
                            e.addToProof(cm);
                            if (cm.getType() == MessageFactory.ACCEPT) {
                                e.setAccept(cm.getSender(), cm.getValue());
                            } else if (cm.getType() == MessageFactory.WRITE) {
                                e.setWrite(cm.getSender(), cm.getValue());
                            }
                        }
                        if (e != null) {
                            byte[] hash = tomLayer.computeHash(currentProof.getDecision());
                            e.propValueHash = hash;
                            e.propValue = currentProof.getDecision();
                            e.deserializedPropValue = tomLayer.checkProposedValue(currentProof.getDecision(), false);
                            cons.decided(e, false);
                            logger.info("Successfully installed proof for consensus " + waitingCID);
                        } else {
                            logger.error("Failed to install proof for consensus " + waitingCID);
                        }
                    }
                    // stop my re-transmission of STOP messages for all regencies up to the current one
                    if (currentRegency > 0) {
                        tomLayer.getSynchronizer().removeSTOPretransmissions(currentRegency - 1);
                    }
                    // if (currentRegency > 0)
                    // tomLayer.requestsTimer.setTimeout(tomLayer.requestsTimer.getTimeout() * (currentRegency * 2));
                    dt.deliverLock();
                    waitingCID = -1;
                    dt.update(state);
                    if (!appStateOnly && execManager.stopped()) {
                        Queue<ConsensusMessage> stoppedMsgs = execManager.getStoppedMsgs();
                        for (ConsensusMessage stopped : stoppedMsgs) {
                            if (stopped.getNumber() > state.getLastCID()) /*msg.getCID()*/
                            {
                                execManager.addOutOfContextMessage(stopped);
                            }
                        }
                        execManager.clearStopped();
                        execManager.restart();
                    }
                    tomLayer.processOutOfContext();
                    if (SVController.getCurrentViewId() != currentView.getId()) {
                        logger.info("Installing current view!");
                        SVController.reconfigureTo(currentView);
                    }
                    isInitializing = false;
                    dt.canDeliver();
                    dt.deliverUnlock();
                    reset();
                    logger.info("I updated the state!");
                    tomLayer.requestsTimer.Enabled(true);
                    tomLayer.requestsTimer.startTimer();
                    if (stateTimer != null) {
                        stateTimer.cancel();
                    }
                    if (appStateOnly) {
                        appStateOnly = false;
                        tomLayer.getSynchronizer().resumeLC();
                    }
                } else if (otherReplicaState == null && (SVController.getCurrentViewN() / 2) < getReplies()) {
                    waitingCID = -1;
                    reset();
                    if (stateTimer != null) {
                        stateTimer.cancel();
                    }
                    if (appStateOnly) {
                        requestState();
                    }
                } else if (haveState == -1) {
                    logger.debug("The replica from which I expected the state, sent one which doesn't match the hash of the others, or it never sent it at all");
                    changeReplica();
                    reset();
                    requestState();
                    if (stateTimer != null) {
                        stateTimer.cancel();
                    }
                } else if (haveState == 0 && (SVController.getCurrentViewN() - SVController.getCurrentViewF()) <= getReplies()) {
                    logger.debug("Could not obtain the state, retrying");
                    reset();
                    if (stateTimer != null) {
                        stateTimer.cancel();
                    }
                    waitingCID = -1;
                // requestState();
                } else {
                    logger.debug("State transfer not yet finished");
                }
            }
        }
    }
    lockTimer.unlock();
}
Also used : CertifiedDecision(bftsmart.tom.leaderchange.CertifiedDecision) ConsensusMessage(bftsmart.consensus.messages.ConsensusMessage) ApplicationState(bftsmart.statemanagement.ApplicationState) Epoch(bftsmart.consensus.Epoch) View(bftsmart.reconfiguration.views.View) Consensus(bftsmart.consensus.Consensus)

Example 20 with View

use of bftsmart.reconfiguration.views.View in project aware by bergerch.

the class AsynchServiceProxy method replyReceived.

/**
 * This is the method invoked by the client side communication system.
 *
 * @param reply The reply delivered by the client side communication system
 */
@Override
public void replyReceived(TOMMessage reply) {
    logger.debug("Asynchronously received reply from " + reply.getSender() + " with sequence number " + reply.getSequence() + " and operation ID " + reply.getOperationId());
    try {
        canReceiveLock.lock();
        RequestContext requestContext = requestsContext.get(reply.getOperationId());
        if (requestContext == null) {
            // it is not a asynchronous request
            super.replyReceived(reply);
            return;
        }
        if (contains(requestContext.getTargets(), reply.getSender()) && (reply.getSequence() == requestContext.getReqId()) && // && (reply.getOperationId() == requestContext.getOperationId())
        (reply.getReqType().compareTo(requestContext.getRequestType())) == 0) {
            logger.debug("Deliverying message from " + reply.getSender() + " with sequence number " + reply.getSequence() + " and operation ID " + reply.getOperationId() + " to the listener");
            ReplyListener replyListener = requestContext.getReplyListener();
            View v = null;
            if (replyListener != null) {
                // if (reply.getViewID() > getViewManager().getCurrentViewId()) { // Deal with a system reconfiguration
                if ((v = newView(reply.getContent())) != null && !requestsAlias.containsKey(reply.getOperationId())) {
                    // Deal with a system reconfiguration
                    TOMMessage[] replies = requestsReplies.get(reply.getOperationId());
                    int sameContent = 1;
                    int replyQuorum = getReplyQuorum();
                    int pos = getViewManager().getCurrentViewPos(reply.getSender());
                    replies[pos] = reply;
                    for (int i = 0; i < replies.length; i++) {
                        if ((replies[i] != null) && (i != pos || getViewManager().getCurrentViewN() == 1) && (reply.getReqType() != TOMMessageType.ORDERED_REQUEST || Arrays.equals(replies[i].getContent(), reply.getContent()))) {
                            sameContent++;
                        }
                    }
                    if (sameContent >= replyQuorum) {
                        if (v.getId() > getViewManager().getCurrentViewId()) {
                            reconfigureTo(v);
                        }
                        requestContext.getReplyListener().reset();
                        Thread t = new Thread() {

                            @Override
                            public void run() {
                                int id = invokeAsynch(requestContext.getRequest(), requestContext.getTargets(), requestContext.getReplyListener(), TOMMessageType.ORDERED_REQUEST);
                                requestsAlias.put(reply.getOperationId(), id);
                            }
                        };
                        t.start();
                    }
                } else if (!requestsAlias.containsKey(reply.getOperationId())) {
                    requestContext.getReplyListener().replyReceived(requestContext, reply);
                }
            }
        }
    } catch (Exception ex) {
        logger.error("Error processing received request", ex);
    } finally {
        canReceiveLock.unlock();
    }
}
Also used : TOMMessage(bftsmart.tom.core.messages.TOMMessage) ReplyListener(bftsmart.communication.client.ReplyListener) View(bftsmart.reconfiguration.views.View)

Aggregations

View (bftsmart.reconfiguration.views.View)32 NodeNetwork (bftsmart.reconfiguration.views.NodeNetwork)10 TOMMessage (bftsmart.tom.core.messages.TOMMessage)7 ConsensusMessage (bftsmart.consensus.messages.ConsensusMessage)5 ArrayList (java.util.ArrayList)5 TOMConfiguration (bftsmart.reconfiguration.util.TOMConfiguration)4 ApplicationState (bftsmart.statemanagement.ApplicationState)4 IOException (java.io.IOException)4 MemoryBasedViewStorage (bftsmart.reconfiguration.views.MemoryBasedViewStorage)3 Properties (java.util.Properties)3 ReplyListener (bftsmart.communication.client.ReplyListener)2 Consensus (bftsmart.consensus.Consensus)2 Epoch (bftsmart.consensus.Epoch)2 ReconfigureReply (bftsmart.reconfiguration.ReconfigureReply)2 HostsConfig (bftsmart.reconfiguration.util.HostsConfig)2 AsynchServiceProxy (bftsmart.tom.AsynchServiceProxy)2 ViewMessage (bftsmart.tom.core.messages.ViewMessage)2 CertifiedDecision (bftsmart.tom.leaderchange.CertifiedDecision)2 LCMessage (bftsmart.tom.leaderchange.LCMessage)2 LeaderRegency (bftsmart.tom.leaderchange.LeaderRegency)2