【努力学习区块链】-使用Geth搭建以太坊私有链网络

什么是Geth

Geth是 ethereum 协议的Go语言实现,Geth支持ethereum中的各种操作;并且得益于Go语言的多平台特性,Geth也支持在多个平台上使用。

安装Geth

安装分为两种:

  1. 直接下载编译好的Geth二进制文件
  2. 从源码编译生成二进制文件

在本示例中,不需要对Geth代码进行修改,所以选择下载编译好的Geth二进制文件

在MacOS上安装

在terminal中执行

brew tap ethereum/ethereum
brew install ethereum

在Ubuntu上安装

在terminal中执行

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum

验证安装成功

在terminal中执行

geth version

出现如下显示,即为安装成功

Geth
Version: 1.7.3-stable
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9.3
Operating System: darwin
GOPATH=/Users/lamo/go/default
GOROOT=/Users/lamo/.gvm/gos/go1.8

创世块

任何以太链都以一个创世块开始,为此我们需要定义一些创世块的参数,以便更好的服务于我们的开发和测试。

以下是一个创世块的样例配置,该配置是json格式的:

{
  "config": {
        "chainId": 11
    },
  "alloc"      : {},
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x20000",
  "extraData"  : "",
  "gasLimit"   : "0x2fefd8",
  "nonce"      : "0x0000000000000042",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}
  • config.chainId // 以太链的ID,用来唯一标记一条以太链

  • coinbase // 矿工账号,第一个区块挖出后将给这个矿工账号发送奖励

  • difficulty // 难度值,越大越难

  • extraData // 附加信息随便填

  • gasLimit // gas 的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以填最大

  • nonce // 一个 64 位随机数

  • mixhash // 与 nonce 配合用于挖矿,由上一个区块的一部分生成的 hash

  • parentHash // 上一个区块的 hash 值

  • alloc // 预设账号以及账号的以太币数量,这里不做配置

初始化以太链

在定义了一个创世块的配置文件之后,使用该创世块的配置文件初始化以太链;

geth --datadir "~/geth/nodes/node_1" init genesis.json

出现如下显示

WARN [02-11|17:49:04] No etherbase set and no accounts found as default 
INFO [02-11|17:49:04] Allocated cache and file handles         database=/Users/lamo/geth/node_1/geth/chaindata cache=16 handles=16
INFO [02-11|17:49:04] Writing custom genesis block 
INFO [02-11|17:49:04] Successfully wrote genesis state         database=chaindata                              hash=7d416a…b75a26
INFO [02-11|17:49:04] Allocated cache and file handles         database=/Users/lamo/geth/node_1/geth/lightchaindata cache=16 handles=16
INFO [02-11|17:49:04] Writing custom genesis block 
INFO [02-11|17:49:04] Successfully wrote genesis state         database=lightchaindata                              hash=7d416a…b75a26

初始化完成之后,启动geth进入交互式命令行工具

geth --datadir "~/geth/nodes/node_1" \
 --networkid 11 \
console 2> ~/geth/logs/console.log

查看节点信息

> admin.nodeInfo
{
  enode: "enode://860d8e7e94dfdc72c0c3ca8f5aa919f1a32cf3a2461a74eb0c1b0f8b3563b757b1e0c9cd53068e79774da3ec0d652acbfddeb75420b670fe9b208313293600af@[::]:30303",
  id: "860d8e7e94dfdc72c0c3ca8f5aa919f1a32cf3a2461a74eb0c1b0f8b3563b757b1e0c9cd53068e79774da3ec0d652acbfddeb75420b670fe9b208313293600af",
  ip: "::",
  listenAddr: "[::]:30303",
  name: "Geth/v1.7.3-stable/darwin-amd64/go1.9.3",
  ports: {
    discovery: 30303,
    listener: 30303
  },
  protocols: {
    eth: {
      difficulty: 65536,
      genesis: "0x7d416a4ed27810d9154d9407a9b2b8ea4094fe8c5ce1d87e50bf6016d6b75a26",
      head: "0x7d416a4ed27810d9154d9407a9b2b8ea4094fe8c5ce1d87e50bf6016d6b75a26",
      network: 11
    }
  }
}

此时,我们已经成功创建了一个只有一个创世块、只有一个节点的以太坊的私有链,但是这个以太链中还没有账户以及交易,下面我们来创建账户。

创建账户

在交互式命令行中运行

> personal.newAccount('geth')
"0xb6e09535504452d5f1be75b510944fa5942efb08"

创建了一个名为0xb6e09535504452d5f1be75b510944fa5942efb08,密码为geth的账户

通过以下命令确认这一点

> personal.listAccounts
["0xb6e09535504452d5f1be75b510944fa5942efb08"]

增加一个节点

打开一个新的terminal,使用刚才的创世块配置文件,初始化一个节点并启动

geth --datadir "~/geth/nodes/node_2" init genesis.json

geth --datadir "~/geth/nodes/node_2" \
 --networkid 11 \
 --port 30304 \
console 2> ~/geth/logs/console2.log

这是第二个节点的信息

> admin.nodeInfo
{
  enode: "enode://a94ece1f720ba0c5a83f52ac7915bb495c83f3b0a6f8691eb7e97f61c6f9c1c7becb6c6d4af7410ec634cd2ef35a5ea3d2ed8f0eab81cd7d7a559f9eb416893c@[::]:30304",
  id: "a94ece1f720ba0c5a83f52ac7915bb495c83f3b0a6f8691eb7e97f61c6f9c1c7becb6c6d4af7410ec634cd2ef35a5ea3d2ed8f0eab81cd7d7a559f9eb416893c",
  ip: "::",
  listenAddr: "[::]:30304",
  name: "Geth/v1.7.3-stable/darwin-amd64/go1.9.3",
  ports: {
    discovery: 30304,
    listener: 30304
  },
  protocols: {
    eth: {
      difficulty: 65536,
      genesis: "0x7d416a4ed27810d9154d9407a9b2b8ea4094fe8c5ce1d87e50bf6016d6b75a26",
      head: "0x7d416a4ed27810d9154d9407a9b2b8ea4094fe8c5ce1d87e50bf6016d6b75a26",
      network: 11
    }
  }
}

连接两个节点

以上操作仅仅创建了两个隔离的节点

在第二个节点上执行

> admin.peers
[]

发现都是空的,现在要把两个节点连接起来,在第二个节点上执行

admin.addPeer("enode://860d8e7e94dfdc72c0c3ca8f5aa919f1a32cf3a2461a74eb0c1b0f8b3563b757b1e0c9cd53068e79774da3ec0d652acbfddeb75420b670fe9b208313293600af@[::]:30303")

其中admin.addPeer("enode://860d8e7e94dfdc72c0c3ca8f5aa919f1a32cf3a2461a74eb0c1b0f8b3563b757b1e0c9cd53068e79774da3ec0d652acbfddeb75420b670fe9b208313293600af@[::]:30303")这一串地址是节点1的信息。

此时就可以看到

> admin.peers

[{
    caps: ["eth/63"],
    id: "860d8e7e94dfdc72c0c3ca8f5aa919f1a32cf3a2461a74eb0c1b0f8b3563b757b1e0c9cd53068e79774da3ec0d652acbfddeb75420b670fe9b208313293600af",
    name: "Geth/v1.7.3-stable/darwin-amd64/go1.9.3",
    network: {
      localAddress: "[::1]:64498",
      remoteAddress: "[::1]:30303"
    },
    protocols: {
      eth: {
        difficulty: 65536,
        head: "0x7d416a4ed27810d9154d9407a9b2b8ea4094fe8c5ce1d87e50bf6016d6b75a26",
        version: 63
      }
    }
}, {
    caps: ["eth/62", "eth/63", "par/1", "par/2", "pip/1"],
    id: "c2b237a4fabf631170f058f54f1aa685b7893ae75ff6260aa08e0698fd636573cae7bd1c303d41927aeebc46c5aebb8888994201226fff29c4664d9099b0281b",
    name: "Parity/v1.8.7-stable-e322fd8-20180124/x86_64-linux-gnu/rustc1.23.0",
    network: {
      localAddress: "100.100.34.41:64751",
      remoteAddress: "109.230.215.17:30304"
    },
    protocols: {
      eth: "handshake"
    }
}]

至此,一个有两个互联节点的以太坊链就成功搭建起来了。

挖矿

搭建起来的第一件事就是挖矿,在随便一个节点上执行

>  miner.start(1)

null

挖矿便开始了,随后风扇呜呜的声音就起来了
此时查看挖到的块数

> eth.blockNumber
0

发现开始0

为了能查看进度,执行

tail -f ~/geth/logs/console.log

或者

tail -f ~/geth/logs/console2.log

取决于你用哪个节点挖矿

出现如下输出

INFO [02-11|18:09:52] Generating DAG in progress               epoch=0 percentage=29 elapsed=35.565s
INFO [02-11|18:09:53] Generating DAG in progress               epoch=0 percentage=30 elapsed=36.822s
INFO [02-11|18:09:54] Generating DAG in progress               epoch=0 percentage=31 elapsed=38.024s
INFO [02-11|18:09:55] Generating DAG in progress               epoch=0 percentage=32 elapsed=39.206s
INFO [02-11|18:09:56] Generating DAG in progress               epoch=0 percentage=33 elapsed=40.389s
INFO [02-11|18:09:58] Generating DAG in progress               epoch=0 percentage=34 elapsed=41.649s
INFO [02-11|18:09:59] Generating DAG in progress               epoch=0 percentage=35 elapsed=42.837s
INFO [02-11|18:10:00] Generating DAG in progress               epoch=0 percentage=36 elapsed=43.992s
INFO [02-11|18:10:01] Generating DAG in progress               epoch=0 percentage=37 elapsed=45.181s
INFO [02-11|18:10:02] Generating DAG in progress               epoch=0 percentage=38 elapsed=46.317s
INFO [02-11|18:10:04] Generating DAG in progress               epoch=0 percentage=39 elapsed=47.516s
INFO [02-11|18:10:05] Generating DAG in progress               epoch=0 percentage=40 elapsed=48.662s

当percentage=100的时候,挖矿便开始了,稍等一会之后,再执行

> eth.blockNumber
4
> eth.blockNumber
5

发现已经挖到了,为了风扇废掉,及时停止了挖矿

> miner.stop()

true

查看区块的信息

查看最后一个区块的信息

> eth.getBlock(eth.blockNumber)
{
  difficulty: 131904,
  extraData: "0xd883010703846765746887676f312e392e338664617277696e",
  gasLimit: 3187903,
  gasUsed: 0,
  hash: "0xbd6806c5a0c5f9e43106e0f55dfc5db1765ad89c9a72962b8e7a0b54ca53ee2b",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0xb6e09535504452d5f1be75b510944fa5942efb08",
  mixHash: "0x4951508a4d389ae7358154c332be764f80276c36823affb5bafb1b401d0602c2",
  nonce: "0x47c27c170524dd8f",
  number: 15,
  parentHash: "0xe4b8d62f2e765c5e89eb9f46757e350a6abc199afb3d0066ef3c6631b06a64b5",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 536,
  stateRoot: "0xcbe4197bbedb50693d3dc41a8cf36b418b3bd853cafcf598abdb78778d0cf9c0",
  timestamp: 1518343931,
  totalDifficulty: 2037440,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}

拿到挖到最后一个区块的矿工的名字

> eth.getBlock(eth.blockNumber).miner
"0xb6e09535504452d5f1be75b510944fa5942efb08"

查找该矿工总共有多少以太坊币

> eth.getBalance(eth.getBlock(eth.blockNumber).miner)
75000000000000000000

注意次数的以太币单位为Wei,是最小的不可分的单位,10^18个wei是一个以太币,故而上述矿工总共有75个以太币,因为总共有15个区块被挖出来,按照每个区块奖励5个以太币的设定,正好75个以太币。

  • vat2018 2018-03-12 09:59:41

    谢谢分享。补充一个适合新手的以太坊DApp开发教程:http://xc.hubwiz.com/course/5a952991adb3847553d205d1?affid=fabox7878