mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
rpc investigation
This commit is contained in:
parent
bca140b73d
commit
e5f490cea1
4 changed files with 28 additions and 0 deletions
20
rpc/README.md
Normal file
20
rpc/README.md
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
# Walk-through of rpc package
|
||||||
|
|
||||||
|
## rpc package
|
||||||
|
|
||||||
|
If you go through every single files under rpc package you can see there is no import from other packages except logging or cors. This means rpc package is very modulalized and separated module from the rest in go-ethereum code base. We can incorporate easily by copying the whole directory.
|
||||||
|
|
||||||
|
In high-level there are implmentation of client and server.
|
||||||
|
|
||||||
|
### Client
|
||||||
|
|
||||||
|
- To create a client, we call [newClient](https://github.com/daywednes/go-ethereum/blob/master/rpc/client.go#L194)
|
||||||
|
|
||||||
|
## Links from other package to rpc
|
||||||
|
|
||||||
|
- from [cmd/swarm/global-store](https://github.com/daywednes/go-ethereum/blob/bca140b73dc107676c912d87f6fe9c352d5fd0d8/cmd/swarm/global-store/global_store.go#L96)
|
||||||
|
- from [node/node.go:startRPC](https://github.com/daywednes/go-ethereum/blob/bca140b73dc107676c912d87f6fe9c352d5fd0d8/node/node.go#L281) which has calls [In-Process RPC endpoint](https://github.com/daywednes/go-ethereum/blob/bca140b73dc107676c912d87f6fe9c352d5fd0d8/node/node.go#L288), call to [IPC RPC endpoint](https://github.com/daywednes/go-ethereum/blob/bca140b73dc107676c912d87f6fe9c352d5fd0d8/node/node.go#L334), [HTTP RPC endpoint](https://github.com/daywednes/go-ethereum/blob/bca140b73dc107676c912d87f6fe9c352d5fd0d8/node/node.go#L363). startRPC is called from [node.Start](https://github.com/daywednes/go-ethereum/blob/bca140b73dc107676c912d87f6fe9c352d5fd0d8/node/node.go#L162)
|
||||||
|
- [node.Start](https://github.com/daywednes/go-ethereum/blob/bca140b73dc107676c912d87f6fe9c352d5fd0d8/node/node.go#L162) is called from
|
||||||
|
- [mobile/n.node.Start](https://github.com/daywednes/go-ethereum/blob/bca140b73dc107676c912d87f6fe9c352d5fd0d8/mobile/geth.go#L202)
|
||||||
|
- [cmd/faucet:stack.Start()](https://github.com/daywednes/go-ethereum/blob/bca140b73dc107676c912d87f6fe9c352d5fd0d8/cmd/faucet/faucet.go#L254)
|
||||||
|
- [cmd/utils/StartNode](https://github.com/daywednes/go-ethereum/blob/bca140b73dc107676c912d87f6fe9c352d5fd0d8/cmd/utils/cmd.go#L66) which is called from geth/main.go
|
||||||
|
|
@ -110,6 +110,7 @@ type clientConn struct {
|
||||||
handler *handler
|
handler *handler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// minhdoan: create a client connection with ServerCodec.
|
||||||
func (c *Client) newClientConn(conn ServerCodec) *clientConn {
|
func (c *Client) newClientConn(conn ServerCodec) *clientConn {
|
||||||
ctx := context.WithValue(context.Background(), clientContextKey{}, c)
|
ctx := context.WithValue(context.Background(), clientContextKey{}, c)
|
||||||
handler := newHandler(ctx, conn, c.idgen, c.services)
|
handler := newHandler(ctx, conn, c.idgen, c.services)
|
||||||
|
|
@ -165,6 +166,7 @@ func Dial(rawurl string) (*Client, error) {
|
||||||
//
|
//
|
||||||
// The context is used to cancel or time out the initial connection establishment. It does
|
// The context is used to cancel or time out the initial connection establishment. It does
|
||||||
// not affect subsequent interactions with the client.
|
// not affect subsequent interactions with the client.
|
||||||
|
// minhdoan: Dial with context and different rpc types.
|
||||||
func DialContext(ctx context.Context, rawurl string) (*Client, error) {
|
func DialContext(ctx context.Context, rawurl string) (*Client, error) {
|
||||||
u, err := url.Parse(rawurl)
|
u, err := url.Parse(rawurl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -518,6 +520,7 @@ func (c *Client) reconnect(ctx context.Context) error {
|
||||||
// dispatch is the main loop of the client.
|
// dispatch is the main loop of the client.
|
||||||
// It sends read messages to waiting calls to Call and BatchCall
|
// It sends read messages to waiting calls to Call and BatchCall
|
||||||
// and subscription notifications to registered subscriptions.
|
// and subscription notifications to registered subscriptions.
|
||||||
|
// minhdoan: main loop of client.
|
||||||
func (c *Client) dispatch(codec ServerCodec) {
|
func (c *Client) dispatch(codec ServerCodec) {
|
||||||
var (
|
var (
|
||||||
lastOp *requestOp // tracks last send operation
|
lastOp *requestOp // tracks last send operation
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,7 @@ type callProc struct {
|
||||||
notifiers []*Notifier
|
notifiers []*Notifier
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// minhdoan: RPC handler
|
||||||
func newHandler(connCtx context.Context, conn jsonWriter, idgen func() ID, reg *serviceRegistry) *handler {
|
func newHandler(connCtx context.Context, conn jsonWriter, idgen func() ID, reg *serviceRegistry) *handler {
|
||||||
rootCtx, cancelRoot := context.WithCancel(connCtx)
|
rootCtx, cancelRoot := context.WithCancel(connCtx)
|
||||||
h := &handler{
|
h := &handler{
|
||||||
|
|
@ -93,6 +94,7 @@ func newHandler(connCtx context.Context, conn jsonWriter, idgen func() ID, reg *
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleBatch executes all messages in a batch and returns the responses.
|
// handleBatch executes all messages in a batch and returns the responses.
|
||||||
|
// minhdoan: handle a list of rpc messages.
|
||||||
func (h *handler) handleBatch(msgs []*jsonrpcMessage) {
|
func (h *handler) handleBatch(msgs []*jsonrpcMessage) {
|
||||||
// Emit error response for empty batches:
|
// Emit error response for empty batches:
|
||||||
if len(msgs) == 0 {
|
if len(msgs) == 0 {
|
||||||
|
|
@ -131,6 +133,7 @@ func (h *handler) handleBatch(msgs []*jsonrpcMessage) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleMsg handles a single message.
|
// handleMsg handles a single message.
|
||||||
|
// minhdoan: handle a single rpc json message.
|
||||||
func (h *handler) handleMsg(msg *jsonrpcMessage) {
|
func (h *handler) handleMsg(msg *jsonrpcMessage) {
|
||||||
if ok := h.handleImmediate(msg); ok {
|
if ok := h.handleImmediate(msg); ok {
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -170,6 +170,7 @@ type jsonCodec struct {
|
||||||
|
|
||||||
// NewCodec creates a new RPC server codec with support for JSON-RPC 2.0 based
|
// NewCodec creates a new RPC server codec with support for JSON-RPC 2.0 based
|
||||||
// on explicitly given encoding and decoding methods.
|
// on explicitly given encoding and decoding methods.
|
||||||
|
// minhdoan: this is where codec is generated by all rpc types
|
||||||
func NewCodec(conn Conn, encode, decode func(v interface{}) error) ServerCodec {
|
func NewCodec(conn Conn, encode, decode func(v interface{}) error) ServerCodec {
|
||||||
codec := &jsonCodec{
|
codec := &jsonCodec{
|
||||||
closed: make(chan interface{}),
|
closed: make(chan interface{}),
|
||||||
|
|
@ -184,6 +185,7 @@ func NewCodec(conn Conn, encode, decode func(v interface{}) error) ServerCodec {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewJSONCodec creates a new RPC server codec with support for JSON-RPC 2.0.
|
// NewJSONCodec creates a new RPC server codec with support for JSON-RPC 2.0.
|
||||||
|
// minhdoan: this is where codec is generated by all rpc types
|
||||||
func NewJSONCodec(conn Conn) ServerCodec {
|
func NewJSONCodec(conn Conn) ServerCodec {
|
||||||
enc := json.NewEncoder(conn)
|
enc := json.NewEncoder(conn)
|
||||||
dec := json.NewDecoder(conn)
|
dec := json.NewDecoder(conn)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue