Structural analysis of Hyperledger fabric (III.)

Source: Internet
Author: User
Tags error handling hyperledger fabric

This article solves the problem: how this peer node receives data from other nodes and how the data is processed.


The analysis in the previous two sections is that the command source is the CLI client, how to connect to the DevOps server, how to send it to the consensus module, how to send it to chaincodesupportclient, etc.

Next, we will analyze the issues discussed in this paper

1) During the network initialization process, the node is connected to the other peer as the client during the creation of the node engine during the execution of the following

Peerserver, err = Peer. Newpeerwithengine (Sechelperfunc, Helper.) Getengine)
</pre><pre name= "code" class= "CPP" style= "FONT-SIZE:15PX;" >func (P *impl) chatwithpeer (address string) error {
	PEERLOGGER.DEBUGF ("initiating Chat with peer address:%s", add RESS)
	conn, err: = newpeerclientconnectionwithaddress (address)
	if err! = Nil {
		Peerlogger.errorf ("Error Creating connection to peer address%s:%s ", address, Err)
		return err
	}
	serverclient: = PB. Newpeerclient (conn)
	CTX: = context. Background ()
	stream, Err: = Serverclient.chat (CTX)
	if err! = Nil {
		Peerlogger.errorf ("Error establishing Chat with peer address%s:%s ", address, Err)
		return err
	}
	PEERLOGGER.DEBUGF (" Established chat with peer ad Dress:%s ", address)
	err = P.handlechat (CTX, Stream, true)
	stream. Closesend ()
	if err! = Nil {
		Peerlogger.errorf ("Ending Chat with peer address%s due to error:%s", address, err)
		return err
	}
	return Nil
}

2. During handlechat execution, a message loop is established, and here the handler. Handlemessage. This handler has been introduced before, is the engine's message response handle, the message response processing from the consensus module

Func (P *impl) handlechat (CTX context. Context, Stream chatstream, initiatedstream bool) error {
	deadline, OK: = ctx. Deadline ()
	PEERLOGGER.DEBUGF ("current Context Deadline =%s, OK =%v", Deadline, OK)
	handler, err: = P.handlerfact Ory (P, stream, Initiatedstream, nil)
	if err! = Nil {
		return FMT. Errorf ("Error creating handler during Handlechat Initiation:%s", err)
	}
	defer handler. Stop ()
	for {
		in, err: = stream. RECV ()
		If err = = Io. EOF {
			peerlogger.debug ("Received eof, ending Chat")
			return nil
		}
		if err! = Nil {
			e: = fmt. Errorf ("Error during Chat, stopping handler:%s", err)
			Peerlogger.error (E.error ())
			return e
		}
		err = handler. Handlemessage (in)
		if err! = Nil {
			Peerlogger.errorf ("Error Handling message:%s", err)
			//return err
		}
	}
}
3.HandleMessage function Consenterchan This channel is more important, and the write operation will trigger the Engine.consensusfan message loop

Func (Handler *consensushandler) handlemessage (msg *PB. Message) Error {
	if Msg. Type = = PB. Message_consensus {
		Senderpe, _: = handler. to ()
		Select {case
		Handler.consenterchan <-&util. message{
			msg:    msg,
			Sender:senderPE.ID,
		}:
			return nil
		default:
			err: = FMT. Errorf ("Message channel for%v full, rejecting", senderpe.id)
			logger. Errorf ("Failed to queue consensus message because:%v", err)
			return err
		}
	}

	if logger. Isenabledfor (logging. DEBUG) {
		logger. DEBUGF ("Did not handle message of type '%s, passing on to next MessageHandler", Msg. Type)
	}
	return handler. Messagehandler.handlemessage (msg)
}

4. See recvmsg This function is not a bit familiar, this operation and Hyperledger fabric structure analysis The last process is the same.

Func Getengine (coord peer. Messagehandlercoordinator) (peer. Engine, error) {
	var err error
	Engineonce.do (func () {
		engine = new (Engineimpl)
		engine.helper = Newhelper (coord)
		Engine.consenter = controller. Newconsenter (Engine.helper)
		engine.helper.setConsenter (engine.consenter)
		engine.peerendpoint, err = coord . Getpeerendpoint ()
		engine.consensusfan = util. Newmessagefan ()

		go func () {
			logger. Debug ("Starting up message thread for Consenter")

			//The channel never closes, so this should never break for
			ms G: = Range Engine.consensusFan.GetOutChannel () {
				ENGINE.CONSENTER.RECVMSG (msg). MSG, MSG. Sender)
			}} ()}
	)
	return engine, Err
}
5. The downstream process is consistent with the HYPERLEDGER fabric Structure Analysis (iii).








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.