Dev mode

Geth has a development mode that sets up a single node Ethereum test network with options optimized for developing on local machines. You enable it with the --dev argument.

Geth的开发模式可以建立单节点以太坊测试网络,并提供针对在本地计算机上开发而优化的选项。使用–dev参数启用它。

Starting geth in dev mode does the following:

在开发模式下启动geth会执行以下操作:

  • Initializes the data directory with a testing genesis block 用一个测试生成块初始化数据目录
  • Sets max peers to 0
  • Turns off discovery by other nodes
  • Sets the gas price to 0
  • Uses the Clique PoA consensus engine with which allows blocks to be mined as-needed without excessive CPU and memory consumption使用Clique PoA共识引擎,可根据需要挖掘块,而不会占用过多的CPU和内存
  • Uses on-demand block generation, producing blocks when transactions are waiting to be mined使用按需块生成,在等待挖掘交易时生成块

Start Geth in Dev Mode

You can specify a data directory to maintain state between runs using the --datadir option, otherwise, databases are ephemeral and in-memory:

mkdir test-chain-dir

For this guide, start geth in dev mode, and enable RPC so you can connect other applications to geth. For this guide, we use Remix, the web-based Ethereum IDE, so also allow its domains to accept cross-origin requests.

geth --datadir test-chain-dir --rpc --dev --rpccorsdomain "https://remix.ethereum.org,http://remix.ethereum.org"

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

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

geth attach <IPC_LOCATION>

Once geth is running in dev mode, you can interact with it in the same way as when geth is running in other ways.

一旦geth在开发模式下运行,您就可以与geth以其他方式运行时相同的方式与之交互。

For example, create a test account:

> personal.newAccount()

And transfer ether from the coinbase to the new account:

> eth.sendTransaction({from:eth.coinbase, to:eth.accounts[1], value: web3.toWei(0.05, "ether")})

And check the balance of the account:

> eth.getBalance(eth.accounts[1])

If you want to test your dapps with a realistic block time use the --dev.period option when you start dev mode with the --dev.period 14 argument.

Connect Remix to Geth

With geth now running, open https://remix.ethereum.org. Compile the contract as normal, but when you deploy and run a contract, select Web3 Provider from the Environment dropdown, and add “http://127.0.0.1:8545” to the popup box. Click Deploy, and interact with the contract. You should see contract creation, mining, and transaction activity.