主页 > imtoken地址查询 > 搭建以太坊私链环境

搭建以太坊私链环境

imtoken地址查询 2023-01-17 04:20:00

1.编译g​​eth

geth是以太坊开发中最常用的工具,是官方客户端。 可以运行以太坊节点、创建和管理账户、发送交易、挖矿、部署智能合约等。

由于我们基于以太坊进行了一些代码更改。 所以在搭建环境的时候推荐使用直接从源码编译的方式。

从源码编译geth的过程:

go version

git clone https://github.com/ethereum/go-ethereum.git

make geth

export PATH=$PATH:$GOPATH/src/github.com/ethereum/go-ethereum/build/bin/

2.编写创世块配置

您可以从 Internet 粘贴创世块配置。 例如,以下配置保存在 genesis.json 文件中(文件名可选)。

{
    "config":{
        "chainId":1999,
        "homesteadBlock":0,
        "eip155Block":0,
        "eip158Block":0
    },
    "nonce": "0x0",
    "timestamp": "0x00",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x00",
    "gasLimit": "0x8000000",
    "difficulty": "0x4000",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0x0000000000000000000000000000000000000000",
    "alloc": {     }
}

也可以使用官方的puppeth工具生成创世块的配置文件

make all 

以太坊创世区块不能挖矿了_以太坊挖矿难度大了吗_2017以太坊挖矿教程

或者如果你只是想单独制作 puppeth

build/env.sh go run build/ci.go install ./cmd/puppeth

[**********@******]$ puppeth
+-----------------------------------------------------------+
| Welcome to puppeth, your Ethereum private network manager |
|                                                           |
| This tool lets you create a new Ethereum network down to  |
| the genesis block, bootnodes, miners and ethstats servers |
| without the hassle that it would normally entail.         |
|                                                           |
| Puppeth uses SSH to dial in to remote servers, and builds |
| its network components out of Docker containers using the |
| docker-compose toolset.                                   |
+-----------------------------------------------------------+
Please specify a network name to administer (no spaces or hyphens, please)
>

输入测试网络的名称,例如test

> test
Sweet, you can set this via --network=test next time!
INFO [06-19|14:46:08] Administering Ethereum network           name=test
WARN [06-19|14:46:08] No previous configurations found         path=/Users/yuanzhenxia/.puppeth/test
What would you like to do? (default = stats)
 1. Show network stats
 2. Configure new genesis
 3. Track new remote server
 4. Deploy network components
>

2017以太坊挖矿教程_以太坊创世区块不能挖矿了_以太坊挖矿难度大了吗

配置一个新的创世块,选择2

> 2
Which consensus engine to use? (default = clique)
 1. Ethash - proof-of-work
 2. Clique - proof-of-authority
>

选择共识模式,Ethash为PoW共识,Clique为PoA共识。 这里可以先选1

> 1
Which accounts should be pre-funded? (advisable at least one)
> 0x

选择默认帐户。 如果这个地方已经有账号,可以自己设置,也可以直接回车。

> 0x
Specify your chain/network ID if you want an explicit one (default = random)
>

选择网络id以太坊创世区块不能挖矿了,可以输入一个整数,也可以直接回车。 完成这一步后,配置就完成了。

>
INFO [06-19|14:50:49] Configured new genesis block
What would you like to do? (default = stats)
 1. Show network stats
 2. Manage existing genesis
 3. Track new remote server
 4. Deploy network components

配置已经完成以太坊创世区块不能挖矿了,接下来选择2管理创世块的配置

> 2

以太坊创世区块不能挖矿了_2017以太坊挖矿教程_以太坊挖矿难度大了吗

1. Modify existing fork rules 2. Export genesis configuration 3. Remove genesis configuration >

选择2导出配置文件

> 2
Which file to save the genesis into? (default = test.json)
>

输入文件名,默认名称为第一步输入的网络名和.json。

Which file to save the genesis into? (default = test.json)
>
INFO [06-19|14:53:48] Exported existing genesis block

ctrl+c 退出puppeth命令行

3. 初始化并启动节点 3.1 初始化节点

在以太坊节点运行之前,需要先运行geth init命令进行初始化。 其中***.json是第二步生成的创世块的配置文件。 初始化的主要作用是将json文件中配置的内容转化为以太坊链的配置,并生成block 0的创世块,写入数据库(可以看到里面应该多了geth)指定目录文件夹,进入geth文件夹后,可以看到chaindata和lightchaindata两个文件夹)。

geth --datadir=[yourdir] init ***.json

注意命令中的--datadir参数,geth命令的参数比较多,详见geth help的内容。 这里--datadir的意思是将以太坊私链的内容存放在指定的目录下。 如果未指定,则默认位于 $HOME/.ethereum 目录中。

3.2 启动节点

使用geth命令启动节点,你只需要简单的使用geth命令启动节点即可,如果你想要自己的需要,只需要按照相应的参数即可

geth --datadir=[yourdir]

3.3 启动web3

如果像3.2那样启动节点,只能看到节点的日志,不能和节点交互。 我们还需要一个客户端进行交互。 以太坊支持多种交互方式,其中web3是最常用的方式。

geth --datadir=[yourdir] console

geth --datadir=[yourdir] attach

4.挖矿

在进行挖矿之前,首先要有一个账户作为挖矿的受益人。

以太坊挖矿难度大了吗_2017以太坊挖矿教程_以太坊创世区块不能挖矿了

4.1 创建新账户

创建新帐户有两种方法:

> personal.newAccount()
Passphrase:
Repeat passphrase:
"0x****************************************" //这个就是此次新建的地址

[****@****]$ geth --datadir=./ account new
INFO [06-19|19:51:23] Maximum peer count                       ETH=25 LES=0 total=25
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase:
Repeat passphrase:
Address: {****************************************}

4.2 解锁账户

在以太坊挖矿或发送交易时,需要解锁账户,否则会提示相应的错误。

输入密码后使用web3的personal.unlockAccount方法解锁账号。

> personal.unlockAccount(eth.accounts[0])
Unlock account 0x****************************************
Passphrase:
true

默认情况下,eth.accounts 中的第一个账户被用作矿工。 如果你想使用其他账户作为矿工,需要使用miner.setEtherbase()进行设置,然后记得解锁对应的账户。

miner.setEtherbase(eth.accounts[1])

4.3 开始挖矿

使用 miner.start() 开始挖矿。

>miner.start()

通过eth.blockNumber可以看到区块号在增加

>eth.blockNumber

4.4 停止挖矿

使用 miner.stop() 停止挖矿。

以太坊创世区块不能挖矿了_2017以太坊挖矿教程_以太坊挖矿难度大了吗

miner.stop()

5.发送交易

在不安装其他智能合约的情况下,以太坊也可以使用 eth.sendTransaction 进行基本的以太坊转账操作(发送方账户必须解锁)。 下面的命令是从账户0转1个以太币到账户1。

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

6.多节点的情况

组成以太坊网络的多个节点需要保证它们的创世块是一致的。

下一步是启动节点并使节点相互连接。 有两种方法。

6.1 通过bootnodes保证联通。

> admin.nodeInfo.enode
"enode://bafe93edee0c5cfab6a3d12927553abe6f9a3cf31b56b58d65c05e52edf184dcf88f8a2a3d84f3911bc2233c0140188e1e8717c26ff98a0268298c3a933c1855@[::]:30303"

geth --datadir=./ --bootnodes=enode://bafe93edee0c5cfab6a3d12927553abe6f9a3cf31b56b58d65c05e52edf184dcf88f8a2a3d84f3911bc2233c0140188e1e8717c26ff98a0268298c3a933c1855@127.0.0.1:30303 --port=31303 console

6.2 通过admin.addPeer确保联通

如果节点已经启动,或者临时有新节点想加入网络,可以使用admin.addPeer方法添加,参数为上一节介绍的enode。

admin.addPeer("enode://bafe93edee0c5cfab6a3d12927553abe6f9a3cf31b56b58d65c05e52edf184dcf88f8a2a3d84f3911bc2233c0140188e1e8717c26ff98a0268298c3a933c1855@127.0.0.1:30303")

可以通过admin.peers查看节点状态。

admin.peers

7.常用的web3命令

web3的详细API参考,本文只介绍常用的API。

7.1 eth相关命令

eth相关命令主要用于查看链的当前状态,向链发送交易等操作。 在web3窗口输入eth可以看到。

常用的有:

7.2 矿机相关命令

miner 是以太坊中与矿工相关的命令。 常用的有:

7.3 admin相关命令

其中比较常用的有:

7.4个人相关命令

其中比较常用的有:

7.5 txpool相关命令

txpool 主要用来查看当前交易池的状态。 交易发送时,先进入交易池,然后由矿工挖矿,进入区块后从交易池中删除。

7.6 其他工具命令