2019-10-30 06:34:07 -05:00
|
|
|
|
---
|
|
|
|
|
title: Getting Started with Geth
|
2019-11-05 06:46:00 -06:00
|
|
|
|
permalink: docs/getting-started
|
|
|
|
|
sort_key: A
|
2019-10-30 06:34:07 -05:00
|
|
|
|
---
|
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
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](install-and-build/installing-geth)" 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.
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
For this guide, we assume you have Geth installed and are ready to find out how to use it.
|
|
|
|
|
The guide shows you how to create accounts, sync to a network, and then send transactions
|
|
|
|
|
between accounts.
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
This guide uses [Clef](clef/tutorial), which is our preferred tool for signing transactions with Geth,
|
|
|
|
|
and will replace Geth's account management.
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
## Initialize Clef
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
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.
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
```shell
|
|
|
|
|
clef init
|
|
|
|
|
```
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
![Clef init command](../../static/images/clef-init.gif)
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
## Create accounts
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
Create two accounts with the `clef newaccount` command,
|
|
|
|
|
set a password for each of them, and note the public address for each.
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
![Create new account command](../../static/images/clef-account-new.gif)
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
Clef outputs a debug message about `Failed to reload keystore contents`, we fix that in a later step.
|
|
|
|
|
|
|
|
|
|
## Start Geth
|
|
|
|
|
|
|
|
|
|
### Networks
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
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](getting-started/private-net) you create,
|
|
|
|
|
and three test networks that use different consensus algorithms:
|
|
|
|
|
|
2021-06-22 08:12:50 -05:00
|
|
|
|
- **Ropsten**: Proof-of-work test network
|
|
|
|
|
- **Rinkeby**: Proof-of-authority test network
|
|
|
|
|
- **Görli**: Proof-of-authority test network
|
2020-09-10 07:48:15 -05:00
|
|
|
|
|
2021-06-22 08:12:50 -05:00
|
|
|
|
For this guide, we use the Görli network. The default port is 30303, so you need to enable at least
|
|
|
|
|
outgoing access from your node to that port.
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
|
|
|
|
### Sync modes
|
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
You can start Geth in one of three different sync modes using the `--syncmode "<mode>"`
|
2019-11-05 06:46:00 -06:00
|
|
|
|
argument that determines what sort of node it is in the network.
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
|
|
|
|
These are:
|
|
|
|
|
|
2021-06-22 08:12:50 -05:00
|
|
|
|
- **Full**: Downloads all blocks (including headers, transactions, and receipts) and
|
|
|
|
|
generates the state of the blockchain incrementally by executing every block.
|
|
|
|
|
- **Fast**: Downloads all blocks (including headers, transactions and
|
|
|
|
|
receipts), verifies all headers, and downloads the state and verifies it against the
|
|
|
|
|
headers.
|
|
|
|
|
- **Snap** (Default): Same functionality as fast, but with a faster algorithm.
|
|
|
|
|
- **Light**: Downloads all block headers, block data, and verifies some randomly.
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
For this tutorial, we use a `light` sync:
|
|
|
|
|
|
2021-06-22 08:12:50 -05:00
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
## Start Clef
|
|
|
|
|
|
|
|
|
|
Start Clef, setting the keystore and chain id (goerli is 5) for the network we want to connect to:
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
|
|
|
|
```shell
|
2021-04-21 03:35:09 -05:00
|
|
|
|
clef --keystore <GETH_DATA_DIR>/keystore --chainid 5
|
2019-10-30 06:34:07 -05:00
|
|
|
|
```
|
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
To begin with, you see errors about a missing keystore, and we fix that soon.
|
|
|
|
|
|
2021-06-22 08:12:50 -05:00
|
|
|
|
Under Linux the default Geth data directory is `~/.ethereum`
|
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
## Start Geth
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
2021-06-22 08:12:50 -05:00
|
|
|
|
Open another command line window and run command below, which also enables the
|
|
|
|
|
[Geth RPC interface](clef/tutorial) (see below), and sets Clef as the transaction signer.
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
|
|
|
|
```shell
|
2021-02-16 03:38:52 -06:00
|
|
|
|
geth --goerli --syncmode "light" --http --signer=<CLEF_LOCATION>/clef.ipc
|
2019-10-30 06:34:07 -05:00
|
|
|
|
```
|
|
|
|
|
|
2021-06-22 08:12:50 -05:00
|
|
|
|
By default Clef's location under Linux is `~/.clef`, but the signer's location cannot
|
|
|
|
|
include a `~` so replace it with your home directory.
|
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
## Get ETH
|
|
|
|
|
|
|
|
|
|
Unless you have Ether in another account on the Görli network, you can use a
|
|
|
|
|
[faucet](https://goerli-faucet.slock.it/) 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.
|
|
|
|
|
|
|
|
|
|
IPC allows you to do more, especially when it comes to creating and interacting
|
|
|
|
|
with accounts, but you need direct access to the node.
|
|
|
|
|
|
|
|
|
|
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/server#http-server).
|
|
|
|
|
|
|
|
|
|
## Using IPC
|
|
|
|
|
|
|
|
|
|
### Connect to console
|
|
|
|
|
|
|
|
|
|
Connect to the IPC console on a node from another terminal window:
|
2020-04-28 03:10:22 -05:00
|
|
|
|
|
|
|
|
|
```shell
|
2020-09-10 07:48:15 -05:00
|
|
|
|
geth attach <IPC_LOCATION>
|
2020-04-28 03:10:22 -05:00
|
|
|
|
```
|
|
|
|
|
|
2021-06-22 08:12:50 -05:00
|
|
|
|
You can see the IPC location in the output of the network node `geth` process. By default, when using
|
|
|
|
|
Görli, it is `~/.ethereum/goerli/geth.ipc`.
|
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
### Check account balance
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
```javascript
|
|
|
|
|
web3.fromWei(eth.getBalance("<ADDRESS_1>"),"ether")
|
2019-10-30 06:34:07 -05:00
|
|
|
|
```
|
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
Getting the balance of an account does not require a signed transaction,
|
|
|
|
|
so Clef does not ask for approval, and Geth returns the value.
|
|
|
|
|
|
2021-06-22 08:12:50 -05:00
|
|
|
|
Note that this step requires the initial synchronization to end. If you get an error message, return to the
|
|
|
|
|
console with the network node Geth and wait until it is synchronized. You
|
|
|
|
|
know that your Geth is synchronized when it is only importing a small number of blocks (one or two, usually)
|
|
|
|
|
at a time.
|
|
|
|
|
|
2020-09-10 07:48:15 -05:00
|
|
|
|
### 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:
|
|
|
|
|
|
|
|
|
|
```javascript
|
2021-06-22 08:12:50 -05:00
|
|
|
|
eth.sendTransaction({from:"<ADDRESS_1>",to:"<ADDRESS_2>", value: web3.toWei(0.01,"ether")})
|
2020-09-10 07:48:15 -05:00
|
|
|
|
```
|
|
|
|
|
|
2021-06-22 08:12:50 -05:00
|
|
|
|
This action does require signing the transaction, so go to the command line window with Clef running
|
|
|
|
|
to see that Clef prompts you to approve it, and when you do, asks you for the password for the account you are
|
|
|
|
|
sending the ETH from. If the password is correct, Geth proceeds with the transaction.
|
2020-09-10 07:48:15 -05:00
|
|
|
|
|
|
|
|
|
To check, get the account balance of the second account:
|
|
|
|
|
|
|
|
|
|
```javascript
|
2021-06-22 08:12:50 -05:00
|
|
|
|
web3.fromWei(eth.getBalance("<ADDRESS_2>"),"ether")
|
2020-09-10 07:48:15 -05:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Using RPC
|
|
|
|
|
|
|
|
|
|
### Connect to RPC
|
|
|
|
|
|
2021-06-22 08:12:50 -05:00
|
|
|
|
You can use standard HTTP requests to connect to a Geth node using the RPC APIs, using
|
|
|
|
|
this syntax:
|
2020-09-10 07:48:15 -05:00
|
|
|
|
|
|
|
|
|
```shell
|
2020-09-24 06:03:56 -05:00
|
|
|
|
curl -X POST http://<GETH_IP_ADDRESS>:8545 \
|
2021-06-22 08:12:50 -05:00
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
|
--data'{"jsonrpc":"2.0", "method":"<API_METHOD>", "params":[], "id":1}'
|
2020-09-10 07:48:15 -05:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Check account balance
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-09-24 06:03:56 -05:00
|
|
|
|
curl -X POST http://<GETH_IP_ADDRESS>:8545 \
|
2021-06-22 08:12:50 -05:00
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
|
--data '{"jsonrpc":"2.0", "method":"eth_getBalance", "params":["<ADDRESS_1>","latest"], "id":1}'
|
2020-09-10 07:48:15 -05:00
|
|
|
|
```
|
|
|
|
|
|
2021-06-22 08:12:50 -05:00
|
|
|
|
Getting the balance of an account does not require a signed transaction, so Geth returns the value without invoking
|
|
|
|
|
Clef. Note that the value returned is in hexadecimal and WEI. To get the ETH value, convert to decimal and divide by 10^18.
|
2020-09-10 07:48:15 -05:00
|
|
|
|
|
|
|
|
|
### 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:
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-09-24 06:03:56 -05:00
|
|
|
|
curl -X POST http://<GETH_IP_ADDRESS>:8545 \
|
2021-06-22 08:12:50 -05:00
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
|
--data '{"jsonrpc":"2.0", "method":"eth_sendTransaction", "params":[{"from": "<ADDRESS_1>","to": "<ADDRESS_2>","value": "0x9184e72a"}], "id":1}'
|
2020-09-10 07:48:15 -05:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
This action does require signing, so Clef prompts you to approve it, and if you do,
|
2021-06-22 08:12:50 -05:00
|
|
|
|
asks you for the password of the account from which you are sending the ETH. If the password is correct,
|
2020-09-10 07:48:15 -05:00
|
|
|
|
Geth proceeds with the transaction.
|
|
|
|
|
|
|
|
|
|
To check, get the account balance of the second account:
|
2019-10-30 06:34:07 -05:00
|
|
|
|
|
|
|
|
|
```shell
|
2020-09-24 06:03:56 -05:00
|
|
|
|
curl -X POST http://<GETH_IP_ADDRESS>:8545 \
|
2021-06-22 08:12:50 -05:00
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
|
--data '{"jsonrpc":"2.0", "method":"eth_getBalance", "params":["<ADDRESS_2>","latest"], "id":1}'
|
2019-10-30 06:34:07 -05:00
|
|
|
|
```
|