安装geth 搭建以太坊私链

1 Ubuntu用户可以选择在线安装geth的方式,在终端中依次执行以下命令即可:

$ sudo add-apt-repository -y ppa:ethereum/ethereum
$ sudo apt-get update
$ sudo apt-get install ethereum

安装完成后就用geth help验证是否安装成功

ubuntu@i-umw7lzvn:~$ geth help
NAME:
   geth - the go-ethereum command line interface

   Copyright 2013-2017 The go-ethereum Authors

USAGE:
   geth [options] command [command options] [arguments...]

VERSION:
   **1.6.6-stable-10a45cb5**

COMMANDS:
   init        Bootstrap and initialize a new genesis block
   import      Import a blockchain file
   ```

2 下面开始建立私有以太坊网络:以太坊节点之间能够互相链接需要满足1)相同的协议版本2)相同的networkid,所以搭建私有网络最方便的方法就是通过geth命令中的–networkid选项,设置一个与主网不同的networkid(主网的networkid为1),这也是官方推荐的

ubuntu@i-umw7lzvn:~$ mkdir private-geth
ubuntu@i-umw7lzvn:~$ cd private-geth/

世纪区块文件,是一个json格式的文件:
vim genesis.json
以太坊支持自定义创世区块,要运行私有链,我们就需要定义自己的创世区块,创世区块信息写在一个json格式的配置文件中。首先将下面的内容保存到一个json文件中,例如genesis.json(涉及到权限问题所以进入root权限)

root@i-nhmyceuh:/home/ubuntu/private-geth# vim genesis.json

{
  "config": {
        "chainId": 15,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "coinbase" : "0x0000000000000000000000000000000000000000",
    "difficulty" : "0x40000",
    "extraData" : "",
    "gasLimit" : "0xffffffff",
    "nonce" : "0x0000000000000042",
    "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp" : "0x00",
    "alloc": { }
}

准备好创世区块配置文件后,需要初始化区块链,将上面的创世区块信息写入到区块链中。首先要新建一个目录用来存放区块链数据,假设新建的数据目录为~/private-geth/app/chain,genesis.json保存在~/private-geth中,此时目录结构应该是这样的:

private-geth
├── app
└── genesis.json

接下来进入private-geth中,执行初始化命令:

root@i-nhmyceuh:/home/ubuntu/private-geth# geth --datadir data0 init genesis.json
WARN [06-26|10:54:33] No etherbase set and no accounts found as default 
INFO [06-26|10:54:33] Allocated cache and file handles         database=/home/ubuntu/private-geth/data0/geth/chaindata cache=16 handles=16
INFO [06-26|10:54:33] Writing custom genesis block 
INFO [06-26|10:54:33] Successfully wrote genesis state         database=chaindata                                      hash=a0e580鈥5e82e
INFO [06-26|10:54:33] Allocated cache and file handles         database=/home/ubuntu/private-geth/data0/geth/lightchaindata cache=16 handles=16
INFO [06-26|10:54:33] Writing custom genesis block 
INFO [06-26|10:54:33] **Successfully** wrote genesis state         database=lightchaindata                                      hash=a0e580鈥5e82e

初始化成功后,会在数据目录app/cahin中生成geth和keystore两个文件夹,此时目录结构如下:

root@i-nhmyceuh:/home/ubuntu/private-geth# cd data0/
root@i-nhmyceuh:/home/ubuntu/private-geth/data0# ll
total 16
drwx------ 4 root root 4096 Jun 26 10:54 ./
drwxr-xr-x 3 root root 4096 Jun 26 10:54 ../
drwxr-xr-x 4 root root 4096 Jun 26 10:54 geth/
drwx------ 2 root root 4096 Jun 26 10:54 keystore/

其中geth/chaindata中存放的是区块数据,keystore中存放的是账户数据。
启动节点, 加上console 表示启动后,启用命令行:

root@i-nhmyceuh:/home/ubuntu/private-geth/data0# geth --datadir ./data0 --networkid 11 console
WARN [06-26|10:56:58] No etherbase set and no accounts found as default 
INFO [06-26|10:56:58] Starting peer-to-peer node               instance=Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.1
INFO [06-26|10:56:58] Allocated cache and file handles         database=/home/ubuntu/private-geth/data0/data0/geth/chaindata cache=128 handles=1024
INFO [06-26|10:56:58] Writing default main-net genesis block 
INFO [06-26|10:56:59] Initialised chain configuration          config="{ChainID: 1 Homestead: 1150000 DAO: 1920000 DAOSupport: true EIP150: 2463000 EIP155: 2675000 EIP158: 2675000 Metropolis: 9223372036854775807 Engine: ethash}"
INFO [06-26|10:56:59] Disk storage enabled for ethash caches   dir=/home/ubuntu/private-geth/data0/data0/geth/ethash count=3
INFO [06-26|10:56:59] Disk storage enabled for ethash DAGs     dir=/root/.ethash                                     count=2
WARN [06-26|10:56:59] Upgrading db log bloom bins 
INFO [06-26|10:56:59] Bloom-bin upgrade completed              elapsed=71.646碌s
INFO [06-26|10:56:59] Initialising Ethereum protocol           versions="[63 62]" network=11
INFO [06-26|10:56:59] Loaded most recent local header          number=0 hash=d4e567鈥b8fa3 td=17179869184
INFO [06-26|10:56:59] Loaded most recent local full block      number=0 hash=d4e567鈥b8fa3 td=17179869184
INFO [06-26|10:56:59] Loaded most recent local fast block      number=0 hash=d4e567鈥b8fa3 td=17179869184
INFO [06-26|10:56:59] Starting P2P networking 
INFO [06-26|10:57:01] UDP listener up                          self=enode://51ebb32b131ac52c9caca88396a3d0f77f89bd4cfceb9f59bca6615557d5f7b6333cdfad20ec2d1bb26feb211e0abf8ba11a1a1e9c3f4d2987aca4be7e855020@[::]:30303
INFO [06-26|10:57:01] RLPx listener up                         self=enode://51ebb32b131ac52c9caca88396a3d0f77f89bd4cfceb9f59bca6615557d5f7b6333cdfad20ec2d1bb26feb211e0abf8ba11a1a1e9c3f4d2987aca4be7e855020@[::]:30303
INFO [06-26|10:57:01] IPC endpoint opened: /home/ubuntu/private-geth/data0/data0/geth.ipc 
Welcome to the Geth JavaScript console!

instance: Geth/v1.6.6-stable-10a45cb5/linux-amd64/go1.8.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
 ```
 ```
 查看账户
> eth.accounts
[]
创建账户密码为123
> personal.newAccount("123")
"0x04db853fe43494022f7f961091c1764709aa5f87"
> INFO [06-26|10:57:57] New wallet appeared                      url=keystore:///home/ubuntu/private鈥status=Locked
开始挖矿
> miner.start()
INFO [06-26|10:14:06] Updated mining threads                   threads=0
INFO [06-26|10:14:06] Transaction pool price threshold updated price=18000000000
INFO [06-26|10:14:06] Starting mining operation 
null
> INFO [06-26|10:14:06] Commit new mining work                   number=1 txs=0 uncles=0 elapsed=444.165碌s
INFO [06-26|10:14:10] Generating DAG in progress 

猜你喜欢

转载自blog.csdn.net/DDFFR/article/details/73733850
今日推荐