主页 > imtoken最新版官网 > 轻松搭建以太坊私链
轻松搭建以太坊私链
0x00 序言
最近无论是币圈还是技术圈搭建私有以太坊节点,区块链都火得不得了。 2017 年 9 月,ICO 在中国被禁止。 几天之内,国内所有数字货币交易所全部停摆。 消息一出,无数韭菜被迫割肉。 没想到,币圈脱离中国市场后,一路狂飙,以比特币为首的数字货币一路上扬,创下新高。 2017年初,作为数字货币基石的区块链技术更是备受推崇。 更何况,18年被称为区块链元年。 这是继人工智能之后又一被广泛看好的技术。 而且区块链的应用门槛会比人工智能低。 马太效应已经出现,资金大量涌入,隐藏着很多机会。
0x01 配置创世块
在构建私有链之前,您需要创建一个 json 配置文件来初始化创世块。 这里作者将创建一个名为 genesis.json 的文件。
内容如下:
{
"config": {
"chainId": 1000,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x400",
"extraData" : "Oh My God!",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc": {}
}
配置说明:
config.chainId// 区块链ID,geth命令中的--networkid参数需要和chainId的值保持一致
config.homesteadBlock // Homestead硬分叉区块高度,无需关注
config.eip155Block // EIP 155硬分叉高度,无需关注
config.eip158Block // EIP 158硬分叉高度,无需关注
coinbase // 矿工账户,挖出第一个区块后,矿工账户将获得以太坊奖励
difficulty // 难度值,越大越难
extraData // 填写附加信息
gasLimit // gas的总消耗限额,用于限制一个区块可以包含的交易信息总和。 由于我们是私有链,所以填写最大值
nonce // 一个64位的随机数
mixhash // 与nonce结合使用进行挖矿,前一个区块的一部分产生的hash
parentHash // 前一个区块的哈希值
alloc // 默认账户及账户中的以太币数量,私链挖矿更容易,不可配置
好了~说完配置项,就可以开始初始化创世块了! ! !
0x02 初始化创世块
这里需要用到geth的init命令。
geth --datadir $HOME/privateNet/bootnode init genesis.json
...
INFO [01-13|20:07:29] Successfully wrote genesis state database=lightchaindata hash=49c2dd…aff051
参数说明:
--datadir 指定存放数据的目录
0x03 启动私链起始节点(引导节点)
geth --datadir $HOME/privateNet/bootnode \
--networkid 1000 \
--identity "bootnode" \
--port 30303 \
--rpc \
--rpcport 8545 \
--rpccorsdomain "*" \
--nodiscover \
--verbosity 4 \
console
...
Welcome to the Geth JavaScript console!
instance: Geth/v1.7.2-stable/darwin-amd64/go1.9.2
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
>
参数说明:
--networkid与genesis.json中的chainId一致
--identity 设置节点ID
--port geth 节点端口默认值:30303
--rpc 启用 rpc 服务默认值:8545
--rpcport rpc服务端口
--rpccorsdomain 在这里配置"*"允许通过任意域名访问,或者指定一个特定的域名如:""
--nodiscover 关闭节点自动发现
--verbosity log level: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail(默认值:3)
暗示:
在geth控制台模式下搭建私有以太坊节点,可以使用eth.getBlock(block number)查看区块内容
> eth.getBlock(0)
{
difficulty: 16,
extraData: "0x01010101",
gasLimit: 3141592,
gasUsed: 0,
hash: "0xdd97c4469e8bf4054b7c756c1d620ee21d41e5ade129adfbb5ddce3937d6f66e",
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
miner: "0x0000000000000000000000000000000000000000",
mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
nonce: "0x0000000000000042",
number: 0,
parentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 508,
stateRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
timestamp: 0,
totalDifficulty: 16,
transactions: [],
transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
uncles: []
}
世界的创建完成之后,接下来的事情就简单多了~
0x04 创建新账户
在geth控制台模式下,其实就是一个JavaScript运行环境,有点类似于Node.js的命令行模式。 一些操作可以使用一些预设的对象方法来执行。 personal对象下的newAccount方法可以创建一个新的账户,该方法返回一个账户地址。
personal.newAccount("yourPassphrase")
"0xe6a8faf90ddc1ec0c335d0a83f660c5de9ec12d1"
0x05 挖矿
挖矿赚取以太奖励。 由于创世块的难度值设置的很低,所以挖矿难度不大。
miner.start()
挖矿时,可以使用geth attach命令打开另一个终端查看账户余额
geth attach $HOME/privateNet/bootnode/geth.ipc
...
> eth.getBalance(eth.accounts[0])
0x06 创建新节点
都说区块链是去中心化的账本,单机挖矿就满足了吗? 继续创建一个新节点并将其连接到我们的主节点。 这里需要注意以下几点:
geth --datadir $HOME/privateNet/node2 init genesis.json
geth --datadir $HOME/privateNet/node2 \
--fast \
--cache 512 \
--port 30304 \
--networkid 1000 \
--identity "node2" \
--rpc \
--rpcport 9545 \
--rpccorsdomain "*" \
--nodiscover \
--verbosity 4 \
console
接下来,我们需要建立节点之间的连接
以下是三种方式:
使用admin.addPeer()命令手动添加bootnode
获取引导节点上的 enode 信息:
admin.nodeInfo.enode
"enode://xxx"
将boot节点的enode信息写入node2
admin.addPeer("enode://xxx");
在geth控制台中显示
DEBUG[01-13|22:54:35] Ethereum peer connected id=b0ac9d91125e7a1b conn=inbound name=Geth/node2/v1.7.2-stable/darwin-amd64/go1.9.2
使用admin.peers命令查看两个节点已经连接
# boot node
> admin.peers
[{
caps: ["eth/63"],
id: "b0ac9d91125e7a1bebff6d473cd3a9bcf41243060e52f97c3744053c060f24c9e2c36cc181e589d46ede5559509c7dd2c8528d9d039ee5cc900e0700f56c27e7",
name: "Geth/node2/v1.7.2-stable/darwin-amd64/go1.9.2",
network: {
localAddress: "[::1]:30303",
remoteAddress: "[::1]:64087"
},
protocols: {
eth: {
difficulty: 245728379,
head: "0x65ae6da1d42ab4ea75bc468e47912cc61cd3a51b5d406482904c7d0c51785548",
version: 63
}
}
}]
# node2
> admin.peers
[{
caps: ["eth/62", "eth/63"],
id: "8cc33dabb8f5fa2033cf646549e6988418439c8756d96e62f418ebb57ecd0cc2cfbf7ceab4450d262331d8d4fe68358200030e6a16988f2ebe393bf6214380fe",
name: "Geth/bootnode/v1.7.2-stable/darwin-amd64/go1.9.2",
network: {
localAddress: "[::1]:64087",
remoteAddress: "[::1]:30303"
},
protocols: {
eth: {
difficulty: 245728379,
head: "0x65ae6da1d42ab4ea75bc468e47912cc61cd3a51b5d406482904c7d0c51785548",
version: 63
}
}
}]
使用 static-nodes.json 文件
除了在geth控制台下使用admin.addPeer()之外,还可以在--datadir指定的目录下添加一个static-nodes.json文件,让节点可以联系
[
"enode://xxx" // boot node 地址
]
使用命令行--bootnodes参数指定enode0x07节点间服务自发现
// 假设node2和node3都加入了bootnode,验证node2和node3之间是否可以自动发现服务
参考文献列表