Ethereum Service mechanism

go-ethereum v1.19.12

1. Service


Service abstracts the collection of two types of functions:
Protocol: Interaction protocol between p2p nodes
API: RPC interface provided for clients

service interface definition service.go

// Service is an individual protocol that can be registered into a node.
//
// Notes:
//
// • Service life-cycle management is delegated to the node. The service is allowed to
// initialize itself upon creation, but no goroutines should be spun up outside of the
// Start method.
//
// • Restart logic is not required as the node will create a fresh instance
// every time a service is started.
type Service interface {
    
    
	// Protocols retrieves the P2P protocols the service wishes to start.
	Protocols() []p2p.Protocol

	// APIs retrieves the list of RPC descriptors the service provides
	APIs() []rpc.API

	// Start is called after all services have been constructed and the networking
	// layer was also initialized to spawn any goroutines required by the service.
	Start(server *p2p.Server) error

	// Stop terminates all goroutines belonging to the service, blocking until they
	// are all terminated.
	Stop() error
}

1.1 Protocol


Protocol represents the implementation of the P2P sub-protocol. When other peers connect, it will Protocolsmatch Runthe functions defined in the protocol and then call them.

Protocol structure definition protocol.go

// Protocol represents a P2P subprotocol implementation.
type Protocol struct {
    
    
	// Name should contain the official protocol name,
	// often a three-letter word.
	Name string

	// Version should contain the version number of the protocol.
	Version uint

	// Length should contain the number of message codes used
	// by the protocol.
	Length uint64

	// Run is called in a new goroutine when the protocol has been
	// negotiated with a peer. It should read and write messages from
	// rw. The Payload for each message must be fully consumed.
	//
	// The peer connection is closed when Start returns. It should return
	// any protocol-level error (such as an I/O error) that is
	// encountered.
	Run func(peer *Peer, rw MsgReadWriter) error

	// NodeInfo is an optional helper method to retrieve protocol specific metadata
	// about the host node.
	NodeInfo func() interface{
    
    }

	// PeerInfo is an optional helper method to retrieve protocol specific metadata
	// about a certain peer in the network. If an info retrieval function is set,
	// but returns nil, it is assumed that the protocol handshake is still running.
	PeerInfo func(id enode.ID) interface{
    
    }

	// DialCandidates, if non-nil, is a way to tell Server about protocol-specific nodes
	// that should be dialed. The server continuously reads nodes from the iterator and
	// attempts to create connections to them.
	DialCandidates enode.Iterator

	// Attributes contains protocol specific information for the node record.
	Attributes []enr.Entry
}

1.2 API


The API describes the external RPC interface, reflectand collects theService specific implementations specified by the field into the same through the mechanism, and then the client calls the API through + .Exported MethodNamespaceNamespaceMethod Name

The method provided externally by the API Service needs to follow a certain format:
PubSub: the first parameter is context.Context, and the return value is defined as (rpc.Subscription, error)
Other : the method return value cannot exceed two, and the last one must beerror

From the understanding of the code logic, under the same Namespace, the method has the risk of coverage, see rpc/service.go for API registration

API structure definition types.go

// API describes the set of methods offered over the RPC interface
type API struct {
    
    
	Namespace string      // namespace under which the rpc methods of Service are exposed
	Version   string      // api version for DApp's
	Service   interface{
    
    } // receiver instance which holds the methods
	Public    bool        // indication if the methods must be considered safe for public use
}

2. Node


The Node example represents a Ethereumnode , gethwhich Noderegisters Service with during the startup process , and in the Node Start phase:

  1. Call Service constructors in registration order
  2. Protocols are managed by p2p.Server by Protocols()collecting the p2p sub-protocols supported by the service
  3. Call the Start method of Service
  4. Call Node's startRPC method

insert image description here

insert image description here

3. There is a problem


  1. There is no perfect dependency management between services, and similar functions are handled better by containerd
  2. Methods under the same namespace may be overwritten, no prompt

Guess you like

Origin blog.csdn.net/DAOSHUXINDAN/article/details/105134916