Ethereum (2): Account management, mining and transfer in the Ethereum private chain environment

Source: Internet
Author: User
Tags commit

In Ethereum (1): In the steps to build the Ethereum private chain on CentOS 6.5 we set up the Ethereum private chain, this time we will create accounts, mining and transfer operations in this private chain environment.

First of all, to review the construction process, the more important part of our talk.

We used the last step in the build./geth--rpc--rpccorsdomain "*"--datadir "/app/chain"--port "30303"--rpcapi "DB,ETH,NET,WEB3"--NETW Orkid 100000 Console This command carried out the Ethereum start, and finally entered the command line mode, if the terminal for some reason shut down, ethereum process will automatically disappear, we can run the above command again, start Ethereum again and the command line mode again, All the following commands in this lecture, if not specifically stated, are run in the command line mode. In the start command, we used the--datadir "/app/chain" as a parameter, we call/app/chain this directory as the data directory.

Let's start with the formal content:

1. View of the account

The structure of the Data Catalog (/app/chain in our example) is as follows when Ethereum is started:

[Root@zc_vm_10_100_142_62 bin]# ll/app/chain/
Total dosage 16
Drwxr-xr-x 2 root root 4096 September 14:11 Chaindata
Drwxr-xr-x 2 root root 4096 September 14:11 Dapp
-RW-------1 root root 0 September 14:11 history
-RW-------1 root root 64 September 14:11 Nodekey
Drwxr-xr-x 2 root root 4096 September 14:11 nodes

There is no subdirectory called KeyStore, and below we use command personal.listaccounts to list the existing accounts in the Ethereum terminal:

instance:geth/v1.4.12-stable/linux/go1.5.1
modules:admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

> personal.listaccounts
[]
>

Returns an empty array indicating that no account is currently available. We use the Personal.newaccount () command to create an account:

> Personal.newaccount ()
Passphrase:
Repeat Passphrase:
"0xa7c417243694fb3a880629879dfe4b71f7cffd3f"
>

Two times Enter the password (the password we entered here is 123456, the password will be used later), and finally returned to us the account, at this time we look at the structure of the Data directory:

[Root@zc_vm_10_100_142_62 chain]# LL
Total dosage 20
Drwxr-xr-x 2 root root 4096 September 14:15 Chaindata
Drwxr-xr-x 2 root root 4096 September 14:15 Dapp
SRW-------1 root root 0 September 14:16 GETH.IPC
-RW-------1 root root 0 September 14:11 history
drwx------2 root root 4096 September 14:19 KeyStore
-RW-------1 root root 64 September 14:11 Nodekey
Drwxr-xr-x 2 root root 4096 September 14:16 nodes

Compared to the last view of the data catalog, there is a subdirectory called KeyStore.

2. Mining

Once the account is created, we can dig mine. In fact, the first account we created in the first step is the default account, we are 0xa7c417243694fb3a880629879dfe4b71f7cffd3f here, we use Web3.fromwei (Eth.getbalance ( eth.coinbase), "ether") to check this default account balance:

> Web3.fromwei (eth.getbalance (eth.coinbase), "Ether")
0

The current default account balance is 0.

Below we use the Miner.start (1) command to start mining:

> Miner.start (1)
I0930 14:33:45.649533 miner/miner.go:119] Starting mining operation (Cpu=1 tot=2)
I0930 14:33:45.650048 miner/worker.go:573] Commit new work on the Block 1 with 0 TXs & 0 uncles. Took 453.037µs
I0930 14:33:45.650175 ethash.go:259] generating DAG for epoch 0 (size 1073739904) ( 0000000000000000000000000000000000000000000000000000000000000000)
True
> I0930 14:33:45.652702 eth/backend.go:454] Automatic pregeneration of Ethash DAG on (ethash dir:/root/.ethash)
I0930 14:33:45.652816 eth/backend.go:461] checking DAG (ethash dir:/root/.ethash)
I0930 14:33:46.661325 ethash.go:276] done generating DAG for epoch 0, it took 1.011158481s
I0930 14:33:50.613543 miner/worker.go:339] mined block (#1/ca8611de). Wait 5 blocks for confirmation
I0930 14:33:50.613968 miner/worker.go:573] Commit new work on the Block 2 with 0 TXs & 0 uncles. Took 389.891µs
I0930 14:33:50.614405 miner/worker.go:573] Commit new work on the Block 2 with 0 TXs & 0 uncles. Took 102.337µs

When the word "mined block" appears, indicating that the mining success, at this time, we can use the Miner.stop () command to stop mining, note, at this time because the mining is ongoing and constantly play the log, the screen will continue to scroll, we do not have to ignore, continue to enter, but do not lose the wrong.

We then use Web3.fromwei (Eth.getbalance (eth.coinbase), "Ether") again to query this default account balance:

> Web3.fromwei (eth.getbalance (eth.coinbase), "Ether")
45

45 is the reward we get from mining.

3. Transfer

The rewards for mining (known as Ethereum in Ethereum, we use the term "aether" in the future) can be transferred to other accounts, and in order to complete the transfer, we first need to create another account locally as the recipient of the transfer:

> Personal.newaccount ()
Passphrase:
Repeat Passphrase:
"0X0EE35A76B6DBC9FCB44AFBBF92362B6653EBCD1A"

The password we entered here is 234567.

We use Eth.sendtransaction ({from: ' 0xa7c417243694fb3a880629879dfe4b71f7cffd3f ', to: ' 0x0ee35a76b6dbc9fcb44afbbf92362b6653ebcd1a ', Value:web3.toWei (1, "Ether")}) to transfer 1 etheric coins from our default account to our newly created account:

> Eth.sendtransaction ({from: ' 0xa7c417243694fb3a880629879dfe4b71f7cffd3f ', to: ' 0x0ee35a76b6dbc9fcb44afbbf92362b6653ebcd1a ', Value:web3.toWei (1, "Ether")})
Account is locked
At web3.js:3119:20
At web3.js:6023:15
At web3.js:4995:36
At:1:1

The result is that the account is locked and the account is transferred out, so we first use Personal.unlockaccount ("0xa7c417243694fb3a880629879dfe4b71f7cffd3f", "123456" , 300) to unlock our transfer account and make another transfer:

> Personal.unlockaccount ("0xa7c417243694fb3a880629879dfe4b71f7cffd3f", "123456", 300)
True
> Eth.sendtransaction ({from: ' 0xa7c417243694fb3a880629879dfe4b71f7cffd3f ', to: ' 0x0ee35a76b6dbc9fcb44afbbf92362b6653ebcd1a ', Value:web3.toWei (1, "Ether")})
I0930 14:55:08.645509 eth/api.go:1193] Tx (0X4BB6B79DBC8090D0EDC3A291760F3FA5DAD6E54F7AE61E5051A0F9D5E6403FA5) to: 0x0ee35a76b6dbc9fcb44afbbf92362b6653ebcd1a
"0x4bb6b79dbc8090d0edc3a291760f3fa5dad6e54f7ae61e5051a0f9d5e6403fa5"

We check the balance of these two accounts:

> Web3.fromwei (eth.getbalance (eth.coinbase), "Ether")
45
> Web3.fromwei (eth.getbalance (eth.accounts[1]), "Ether")
0

As can be seen, the transfer account balance has not been reduced, the transfer account has not increased, because our transaction has not been successfully written into the block, written into the block way is mining, we open again mining, mining success, and stop mining:

> Miner.start (1)
I0930 15:00:16.587705 miner/miner.go:119] Starting mining operation (Cpu=1 tot=2)
I0930 15:00:16.594106 miner/worker.go:573] Commit new work on block with 1 TXs & 0 uncles. Took 6.331868ms
True
> miner.stop (I0930 15:00:22.725363 miner/worker.go:339] mined block (#10/9a7ef215). Wait 5 blocks for confirmation

。。。。。

。。。。。

I0930 15:00:26.219075 miner/worker.go:339] mined stale block (#14/240ad5b5).
I0930 15:00:26.219203 miner/worker.go:573] Commit new work on Block 0 TXs & 0 uncles. Took 108.818µs
Miner.stop ()
True

Check the balance again:

> Web3.fromwei (eth.getbalance (eth.coinbase), "Ether")
69
> Web3.fromwei (eth.getbalance (eth.accounts[1]), "Ether")
1

At this point, you can see the transfer account, adding 1 bitcoin. The transfer out of the account, due to re-mining and get rewarded, so the balance increased, but the increase is 5 each time, where the balance is 69, it is true that the transfer from the ether.

As the

ends, the content of this section is complete. Next time, we'll show you a simple smart contract.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.