Getting Started with Geth

Getting Started with Geth

To use Geth, you need to install it first. You can install Geth in a variety of ways that you can find in the “Install and Build” section. These include installing it via your favorite package manager, downloading a standalone pre-built binary, running it as a docker container or building it yourself.

要使用Geth,您需要先安装它。您可以通过“Install and Build”部分中找到的多种方式来安装Geth。其中包括通过您最喜欢的软件包管理器进行安装,下载独立的预构建二进制文件,将其作为docker容器运行或自行构建。

For this guide, we assume you have Geth installed and are ready to find out how to use it. The guide shows you how  create accounts, sync to a network, and then send transactions between accounts.

对于本指南,我们假设您已经安装了Geth,并准备好找到如何使用它。该指南介绍了如何创建帐户,同步网络以及如何在帐户之间发送交易。

This guide uses Clef, which is our preferred tool for signing transactions with Geth, and will replace Geth’s account management.

本指南使用Clef,这是我们与Geth签署交易的首选工具,它将取代Geth的帐户管理。

Initialize Clef

First, initialize Clef with a random master seed, which is also encrypted with the password you define. The password must be at least 10 characters.

首先,使用随机主种子初始化Clef,该种子也使用您定义的密码进行加密。密码必须至少为10个字符。

clef init

Clef init command

Create accounts

Create two accounts with the clef newaccount command, set a password for each of them, and note the public address for each.

使用clef newaccount命令创建两个帐户,为每个帐户设置一个密码,并记下每个帐户的公共地址。

Create new account command

Clef outputs a debug message about Failed to reload keystore contents, we fix that in a later step.

Start Geth

Networks

You can connect a Geth node to several different networks using the network name as an argument. These include the main Ethereum network, a private network you create, and three test networks that use different consensus algorithms:

您可以使用网络名称作为参数将Geth节点连接到多个不同的网络。其中包括主要的以太坊网络,您创建的专用网络以及使用不同共识算法的三个测试网络:

  • Ropsten: Proof-of-work test network 工作量证明测试网络
  • Rinkeby: Proof-of-authority test network 授权证明测试网络
  • Görli: Proof-of-authority test network 授权证明测试网络

For this guide, we use the Görli network.

Sync modes

You can start Geth in one of three different sync modes using the --syncmode "<mode>" argument that determines what sort of node it is in the network.

您可以使用--syncmode "<mode>" 以三种不同的同步模式启动Geth。确定它在网络中属于哪种节点。

These are:

  • Full: Downloads all blocks (including headers, transactions, and receipts) and generates the state of the blockchain incrementally by executing every block.下载所有块(包括头信息,交易和收据),并通过执行每个块来增量生成区块链的状态。
  • Fast (Default): Downloads all blocks (including headers, transactions and receipts), verifies all headers, and downloads the state and verifies it against the headers.(默认):下载所有块(包括标题,交易和收据),验证所有标题,然后下载状态并针对标题进行验证。
  • Light: Downloads all block headers, block data, and verifies some randomly.下载所有块标题,块数据,并随机进行验证。

For this tutorial, we use a light sync:

Start Clef

Start Clef, setting the keystore and chain id (goerli is 5) for the network we want to connect to:

启动Clef,为我们要连接的网络设置密钥库和链ID(goerli为5):

clef --keystore <GETH_LOCATION>/keystore --chainid 5

To begin with, you see errors about a missing keystore, and we fix that soon.

首先,您会看到有关缺少密钥库的错误,我们会尽快解决。

Start Geth

The command below also enables the Geth RPC interface (which we cover below), and sets Clef as the transaction signer.

下面的命令还启用了 Geth RPC interface(我们将在下面介绍),并将Clef设置为事务签名者。

geth --goerli --syncmode "light" --rpc --signer=<CLEF_LOCATION>/clef.ipc

Get ETH

Unless you have Ether in another account on the Görli network, you can use a faucet to send ETH to one of your new account addresses to use for this guide.

Connect to Geth with IPC or RPC

You can interact with Geth in two ways: Directly with the node using the JavaScript console over IPC, or connecting to the node remotely over HTTP using RPC.

您可以通过两种方式与Geth交互:使用基于IPC的JavaScript控制台直接与节点交互,或使用RPC通过HTTP远程连接至节点。

IPC allows you to do more, especially when it comes to creating and interacting with accounts, but you need direct access to the node.

IPC允许您做更多的事情,尤其是在创建和与帐户进行交互时,但是您需要直接访问该节点。

RPC allows remote applications to access your node but has limitations and security considerations, and by default only allows access to methods in the eth and shh namespaces. Find out how to override this setting in the RPC docs.

RPC允许远程应用程序访问您的节点,但具有局限性和安全性考虑,默认情况下仅允许访问eth和shh命名空间中的方法。在RPC文档中了解如何覆盖此设置。

Using IPC

Connect to console

Connect to the IPC console on a node from another terminal window:

从另一个终端窗口连接到节点上的IPC控制台:

geth attach <IPC_LOCATION>

Check account balance

web3.fromWei(eth.getBalance("<ADDRESS_1>"),"ether")

Getting the balance of an account does not require a signed transaction, so Clef does not ask for approval, and Geth returns the value.

获取帐户的余额不需要签署交易,因此Clef不会要求批准,Geth会返回该值。

Send ETH to account

Send 0.01 ETH from the account that you added ETH to with the Görli faucet, to the second account you created:

eth.sendTransaction({from:"<ADDRESS_0>",to:"<ADDRESS_1>", value: web3.toWei(0.01,"ether")})

This action does require signing the transaction, so Clef prompts you to approve it, and if you do, asks you for the password you are sending the ETH from. If the password is correct, Geth proceeds with the transaction.

To check, get the account balance of the second account:

web3.fromWei(eth.getBalance("<ADDRESS_1>"),"ether")

Using RPC

Connect to RPC

You can use standard HTTP requests to connect to a Geth node using the RPC APIs, for example:

curl -X POST http://<GETH_IP_ADDRESS>:8545 \
    -H "Content-Type: application/json" \
   --data'{"jsonrpc":"2.0", "method":"<API_METHOD>", "params":[], "id":1}'

Check account balance

curl -X POST http://<GETH_IP_ADDRESS>:8545 \
    -H "Content-Type: application/json" \
   --data '{"jsonrpc":"2.0", "method":"eth_getBalance", "params":["<ADDRESS_1>","latest"], "id":1}'

Getting the balance of an account does not require a signed transaction, so Clef does not ask for approval, and Geth returns the value.

Send ETH to accounts

Send 0.01 ETH from the account that you added ETH to with the Görli faucet, to the second account you created:

curl -X POST http://<GETH_IP_ADDRESS>:8545 \
    -H "Content-Type: application/json" \
   --data '{"jsonrpc":"2.0", "method":"eth_sendTransaction", "params":[{"from": "<ADDRESS_0>","to": "<ADDRESS_1>","value": "0x9184e72a"}], "id":1}'

This action does require signing, so Clef prompts you to approve it, and if you do, asks you for the password you are sending the ETH from. If the password is correct, Geth proceeds with the transaction.

To check, get the account balance of the second account:

curl -X POST http://<GETH_IP_ADDRESS>:8545 \
    -H "Content-Type: application/json" \
    --data '{"jsonrpc":"2.0", "method":"eth_getBalance", "params":["<ADDRESS_1>","latest"], "id":1}'