Skip to content

4.1 Code Geth CLI

From CLI#

https://github.com/ethereum/go-ethereum/blob/v1.11.2/cmd/geth/main.go L:335

func geth(ctx *cli.Context) error {
    ...
    stack, backend := makeFullNode(ctx)
    ...
    /* setup JSON-RPC endpoints */
    startNode(ctx, stack, backend, false)
    ...
    return nil
}

What consists of a (full) node is in https://github.com/ethereum/go-ethereum/blob/v1.11.2/cmd/geth/config.go:

func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
  // ...
  return stack, backend
}

  • stack

    From ChatGPT:
    The term "stack" refers to a data structure known as the "execution stack." The execution stack is used to store and manage temporary data during the execution of Ethereum smart contract code. It operates on the Last-In-First-Out (LIFO) principle, meaning that the most recently pushed item onto the stack is the first to be popped off.

  • backend

    From ChatGPT:
    In the context of the go-ethereum codebase, the term "backend" refers to the interface and implementation that handles the underlying data storage, retrieval, and interaction with the Ethereum blockchain and its associated components.

The backend is created by eth.New() defined in https://github.com/ethereum/go-ethereum/blob/v1.11.2/eth/backend.go, which contains the core logic to initialize an execution client.

Peer Discovery#

When starting geth command, you can either:

  1. supply a set of bootnodes (bootstrap node) or
  2. give a network name, like 'goerli', as documented
    , as documented here

From the code snippet above, follow the trace:

For case 1, each network including testnets have a preset of bootnodes defined in https://github.com/ethereum/go-ethereum/blob/v1.11.2/params/bootnodes.go.

ChatGPT: How does an Ethereum node find and connect to other nodes?

When an Ethereum node starts up, it attempts to find and connect to other nodes on the network. There are several ways that nodes can discover each other:
1. Bootnodes: Ethereum nodes can be pre-configured with a list of bootnodes, which are nodes that act as initial contact points for new nodes joining the network. When a node starts up, it attempts to connect to one or more bootnodes, which then provide it with a list of other nodes on the network.
2. Discovery protocol: Ethereum nodes can also use the discovery protocol to find other nodes on the network. The discovery protocol is a peer-to-peer protocol that allows nodes to discover each other without relying on centralized bootnodes. When a node starts up, it sends a discovery message to a random subset of nodes on the network, which respond with their own discovery messages. This process continues until the node has built a list of other nodes on the network.
3. Static node configuration: Ethereum nodes can also be configured with a list of static nodes that they should connect to. This is useful for private Ethereum networks or when nodes need to connect to a specific set of nodes.

Once an Ethereum node has discovered other nodes on the network, it attempts to establish connections with them using the DevP2P protocol. Nodes can then exchange blocks, transactions, and other information with each other to stay in sync with the network.

It's worth noting that some Ethereum nodes may not accept incoming connections, so it's possible for a node to be unable to connect to certain nodes on the network. Additionally, nodes may periodically drop connections with other nodes if they become inactive or if they are not syncing properly.

About Bootnodes#

Bootnodes holds 'enode' records, to identify each other. Similar mechanism for consensus client, see 3.1 Code - Lodestar - CLI Beacon Node#Create network, the most important step.

Source code for running bootnode:
cmd/bootnode/main.go. Its documention is here