[LINUX] ubuntu 20.04 + Geth environment account settings and ether mining

After installing Geth on ubuntu, create an account and open a procedure to mine.

Click here for the previous article summarizing the environment construction.

https://qiita.com/kanazwk/items/76ffda7b4d6b7dc1ba27

Create an EOA account

View the list of EOAs created within this node. Since it is initially empty, a blank is displayed.

> eth.accounts
[]
>

Create it with personal.newAccount (password string). Here, create two accounts.

> personal.newAccount("p@ssw0rd01")
"0x911e18204582a6de08dd2b1f7540b9167ce93b67"
>
> eth.accounts
["0x911e18204582a6de08dd2b1f7540b9167ce93b67"]
> personal.newAccount("p@ssw0rd02")
"0x911e18204582a6de08dd2b1f7540b9167ce93b67"
>
> eth.accounts
["0x911e18204582a6de08dd2b1f7540b9167ce93b67", "0x634c921bfbad7756c7988ea74e4bdf5e5387620d"]

An EOA account is an ** Externally Owned Account **. The user account that generates transactions is used to transfer ether to other accounts and execute contract code. Ether mining is also done with this account.

There are two types of Ethereum accounts, the other is an automated agent called ** Contract **. A transaction issued by EOA triggers the ** contract code ** of this Contract account to be executed. After execution, the data fields of the Contract account will also be updated.

Confirm the address of etherbase (coinbase)

When you execute the eth.coinbase command, one of the created EOA accounts will be displayed. This is called etherbase (coinbase). The etherbase (coinbase) is the address of the EOA that associates the reward when mining with each node.

> eth.coinbase
"0x911e18204582a6de08dd2b1f7540b9167ce93b67"

etherbase (coinbase) is linked to the execution result (primary account address) of the eth.accounts [0] command.

> eth.accounts
["0x911e18204582a6de08dd2b1f7540b9167ce93b67", "0x634c921bfbad7756c7988ea74e4bdf5e5387620d"]
>
> eth.accounts[0]
"0x911e18204582a6de08dd2b1f7540b9167ce93b67"
> eth.accounts[1]
"0x634c921bfbad7756c7988ea74e4bdf5e5387620d"
>

This address can be changed. To change it, execute the miner.setEtherbase (new_adress) command as follows.

> miner.setEtherbase(eth.accounts[1])
true
> eth.coinbase
"0x634c921bfbad7756c7988ea74e4bdf5e5387620d"
>

Ether mining

Mining ether coins is done with miner.start (thread_num). Specify the number of CPU threads to execute in thread_num. If not specified, it will be automatically executed according to the number of CPU cores/threads in the operating environment.

> miner.start()
null

When mining is started, the CPU usage rate (% usr) rises at once.

$ dstat -tc
----system---- --total-cpu-usage--
     time     |usr sys idl wai stl
27-12 22:14:34|  0   0 100   0   0
27-12 22:14:35|  0   1  99   0   0
27-12 22:14:36|  0   0 100   0   0
27-12 22:14:37|  0   0 100   0   0
27-12 22:14:38|  0   0 100   0   0
27-12 22:14:39|  0   0 100   0   0
27-12 22:14:40|  0   0 100   0   0
27-12 22:14:41|  1   0  99   0   0
27-12 22:14:42|  0   0 100   0   0
27-12 22:14:43|  0   0 100   0   0
27-12 22:14:44|  0   0 100   0   0
27-12 22:14:45|  0   0 100   0   0
27-12 22:14:46|  0   0 100   0   0
27-12 22:14:47|  0   0 100   0   0
27-12 22:14:48|  0   0 100   0   0
27-12 22:14:49| 58   1  41   0   0
27-12 22:14:50|100   0   0   0   0
27-12 22:14:51| 99   1   0   0   0
27-12 22:14:52|100   0   0   0   0
27-12 22:14:53| 98   2   0   0   0
27-12 22:14:54| 99   1   0   0   0
27-12 22:14:55| 98   2   0   0   0
27-12 22:14:56| 99   1   0   0   0
27-12 22:14:57| 98   2   0   0   0

If the dstat command is not installed, install it with the following command.

$ sudo apt install dstat

Use the eth.mining command to confirm that mining has started. If true is displayed, mining is in progress, and if false is displayed, mining is stopped.

> eth.mining
true
>

The mined ether forms a block. You can check how many blocks are connected to the blockchain with the eth.blockNumber command.

> eth.blockNumber
20
>

When you check the hash rate, the numbers are displayed as shown below. The hash rate is the calculation power and mining speed per second, and the unit is hash/s. The operation continues as long as this value is displayed.

> eth.hashrate
2455
>

Stop mining ether

Use the following command to stop mining.

> miner.stop()
null

Mining may not stop immediately after executing the command. The same is true at the start of mining, and the process does not start immediately, but these movements seem to be the first-time operations associated with DAG file generation.

This is because a huge 1GB file called ** DAG ** is created, and the DAG file is a data file used for hash calculation of the blockchain. During this DAG file generation, the mining process starts and ends. It took about 10 minutes with AMD Ryzen 7.

The DAG file was created in the .ethash folder directly under the execution user's home directory.

$ ls -lh /home/kana/.ethash
Total 2.1G
-rw-rw-r-- 1 kana kana 1.0G December 27 22:24 full-R23-0000000000000000
-rw-rw-r-- 1 kana kana 1.1G December 27 22:35 full-R23-290decd9548b62a8

Confirmation of mined blocks

Display the contents of the mined block. Based on the display result of the eth.blockNumber command, check any block with theeth.getBlock (number)command.

> eth.getBlock(19)
{
  difficulty: 132160,
  extraData: "0xd883010919846765746888676f312e31352e36856c696e7578",
  gasLimit: 131749155,
  gasUsed: 0,
  hash: "0xf38872f5d72d585f6c8ba761b14d2e1c2445686c0fa04a0e13904bf772a7e918",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0x634c921bfbad7756c7988ea74e4bdf5e5387620d",
  mixHash: "0x29b6d7cd55847d90b0d91853093ece9ded53babc5308dbedfc60f4af52c77168",
  nonce: "0x3de0229868f19a05",
  number: 19,
  parentHash: "0x02af21c8c989e4d2ac63846c3acb8ef708c16d86006a0367994ca585b0c35c3e",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 537,
  stateRoot: "0x9cdf5e057662eafa56503ed8d8fedb7580605d22961a5591665d1f5beea20618",
  timestamp: 1609075546,
  totalDifficulty: 2516544,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}
>
> eth.getBlock(20)
{
  difficulty: 132224,
  extraData: "0xd883010919846765746888676f312e31352e36856c696e7578",
  gasLimit: 131620495,
  gasUsed: 0,
  hash: "0xb53bf57600f78f9f4d0015b9e15f0901b99049d23e65ebc9dfa6460eba8e83ae",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0x634c921bfbad7756c7988ea74e4bdf5e5387620d",
  mixHash: "0xd62344a2afbd419ce1e7c38c8664fcdeb35340a4de57fba2c2e5e7e94bdf42fc",
  nonce: "0x45064ec15863ac8d",
  number: 20,
  parentHash: "0xf38872f5d72d585f6c8ba761b14d2e1c2445686c0fa04a0e13904bf772a7e918",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 537,
  stateRoot: "0xd59b4f9158a04312b607464ac5c331142fa2c18f1d324b512f310791b7010a18",
  timestamp: 1609075547,
  totalDifficulty: 2648768,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}
> 
> eth.getBlock(21)
null
>

The hash value of the 19th block is written in the parentHash of the 20th block. parentHash refers to the block header hash of the parent block. It means that the child of 19 blocks is the 20th block.

In this way, the blocks are recorded in the ledger so as to be continuous. It's an easy-to-understand part called a blockchain.

Confirmation of mining reward

To check the mined reward, use the eth.getBalance (eth.accounts [Number]) command to specify the EOA account. If you check the EOA account information with the eth.accounts command and execute each, the ether holding will be displayed in the [1] th account which is coinbase. It can be judged that the mining has been successful. It is a command that displays the amount of money in units of "wei".

> eth.coinbase
"0x634c921bfbad7756c7988ea74e4bdf5e5387620d"
>
> eth.accounts
["0x911e18204582a6de08dd2b1f7540b9167ce93b67", "0x634c921bfbad7756c7988ea74e4bdf5e5387620d"]
>
> eth.getBalance(eth.accounts[0])
0
> eth.getBalance(eth.accounts[1])
100000000000000000000

To display the unit as "ether", use the web3.fromWei (eth.getBalance (eth.accounts [Number])," ether ") command.

> web3.fromWei(eth.getBalance(eth.accounts[0]),"ether")
0
> web3.fromWei(eth.getBalance(eth.accounts[1]),"ether")
100

Exactly 100 ether.

Summary

If you operate while watching the CPU load, you can feel the processing load of mining, so please try it.

Recommended Posts

ubuntu 20.04 + Geth environment account settings and ether mining
ubuntu 20.04 + geth environment ether remittance
Ubuntu 19.10 Eoan Ermine Japanese / Japanese locale environment and time zone settings
Build Python3 and OpenCV environment on Ubuntu 18.04
Python virtual environment and packages on Ubuntu
[Django3] Environment construction and various settings summary [Python3]
python environment settings
Summary of python environment settings for myself [mac] [ubuntu]
Install Ubuntu 20.04 with GUI and prepare the development environment