记录一下区块链的个人学习笔记,当前公链生态中最主流的是BTC和ETH链,因此本文介绍BTCÐ的私有链搭建教程,为后续基于ETH&BTC的进一步程序开发做好准备。说明:本机环境Mac12.2.1,不同环境可能略有差异1ETH私有链搭建教程安装go-et
安装 go-ethereum 客户端
brew tap ethereum/ethereum
brew install ethereum
检查安装是否成功:
geth --help
在 /Desktop/blockchain 下新建 ethereum 目录,在其中再建一个 genesis.json 文件和一个子目录 /data。 PoW 共识版本的 genesis.json 文件内容如下:
{
"config": {
"chainId": 1001,
"homesteadBlock": 0,
"eip150Block": 0,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"ethash": {}
},
"nonce": "0x0",
"timestamp": "0x5ddf8f3e",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x47b760",
"difficulty": "0x00002",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": { },
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
由于 eth 经历过一次升级从 PoW 改为 PoS,目前最新版的 geth 已经不再内置 ethash,因此 PoS 的 genesis.json 和之前的文章会略有不同。下面这个是 PoS 版的 genesis.json 文件:
{
"config": {
"chainId": 8888,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"berlinBlock": 0,
"clique": {
"period": 5,
"epoch": 30000
}
},
"difficulty": "1",
"gasLimit": "8000000",
"extradata": "0x0000000000000000000000000000000000000000000000000000000000000000b7e24438A3fe363f46994feEEdfbF5ad5078378d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"alloc": {
"27883476a0a617d8e6aa40888253608a0e05cfa4": { "balance": "300000" },
"b7e24438A3fe363f46994feEEdfbF5ad5078378d": { "balance": "300000" },
"87edfb2aed4875144fe1c3b28870284881990418": { "balance": "300000" }
}
}
运行
geth --datadir data init ./genesis.json
顺利初始化一条以太坊私链: 此时的目录结构如图所示: 目录说明:
启动以太坊私链:
geth --datadir data --networkid 8888 console 2>geth.log
至此 ETH 私链搭建完成。
在 bitcoin 官方查看 btc 客户端版本:https://bitcoin.org/bin/ 这里演示 Mac 下的操作,不同系统会略有差异,下载 https://bitcoin.org/bin/bitcoin-core-25.0/bitcoin-25.0-x86_64-apple-darwin.tar.gz
tar -zvxf bitcoin-25.0-x86_64-apple-darwin.tar.gz
sudo install -m 0755 bitcoin-25.0/bin/* /usr/local/bin
后台运行:
bitcoind -daemon
测试是否搭建成功:
bitcoin-cli getdifficulty
bitcoin-cli listtransactions
bitcoin-cli stop
新建配置文件:~/.bitcoin/bitcoin.conf
# Generated by https://jlopp.github.io/bitcoin-core-config-generator/
# This config should be placed in following path:
# ~/.bitcoin/bitcoin.conf
# [rpc]
# Accept command line and JSON-RPC commands.
server=1
txindex=1
# Username for JSON-RPC connections
rpcuser=bitcoinrpc
# Password for JSON-RPC connections
rpcpassword=bitcoinrpc
# Listen for JSON-RPC connections on this port
# rpcport=18332
# Allow JSON-RPC connections from specified source. Valid for <ip> are a single IP (e.g. 1.2.3.4),
# a network/netmask (e.g. 1.2.3.4/255.255.255.0) or a network/CIDR (e.g. 1.2.3.4/24). This option
# can be specified multiple times.
# rpcallowip=127.0.0.1
# rpcallowip=127.0.0.1
# 设定默认为私有链
regtest=1
#gen=1 表示节点可以挖矿
gen=0
启动服务:
bitcoind -conf=/Users/yuanbo.xie/.bitcoin/bitcoin.conf
启动成功。
由于众所周知的原因,在国内最常见的是网络问题。以下命令撤销原本的 git proxy 设置:
git config --global --unset http.proxy
git config --global --unset https.proxy
如果本地开了指定端口的代理请设置为对应端口。
LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 如果遇到 SSL 报错,可以关闭(但存在安全风险):
git config --global http.sslVerify false
权限问题:brew 安装 ethereum 时权限不够
sudo chown -R $(whoami) /usr/local/bin /usr/local/lib
查看 chainid 是否被占用: chainlist
Mac 无法打开“***”,因为无法验证开发者的问题解决: command+shift+G 输入 /usr/local/bin 第一次手工点击 bitcoind 然后打开,后续就可以从命令后调用了
[1] 区块链开发学习第一章:Mac搭建以太坊私有链 [2] 以太坊上创建并实现PoS私链 [3] 搭建比特币私链
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!