ETH & BTC 私链搭建

ETH & BTC 私链搭建

电报联系方式

本文将分享在Mac 12.2.1环境下搭建BTC和ETH的私有链的教程,为后续基于ETH和BTC的进一步程序开发提供良好的准备。请注意,由于不同环境的差异,具体步骤可能会有所调整。教程的目标是帮助读者建立本地的私有区块链,以支持更深入的以太坊和比特币相关应用程序开发。

ETH & BTC 私链搭建

ETH 私有链搭建教程

安装 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”
}

由于以太坊经历了一次升级,从工作量证明(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

顺利初始化一条以太坊私链:

ETH & BTC 私链搭建

此时的目录结构如图所示:

ETH & BTC 私链搭建

目录说明:

  • keystore 用来保存账户信息
  • geth 用来保存区块信息

启动以太坊私链:

geth –datadir data –networkid 8888 console 2>geth.log

  • networkid 为上面的 genesis.json 配置的 id,以太坊主网 id 为 1
  • 2>geth.log 这里的 2 是代表 stderr,这里是为了让私链的以太坊日志独立输出,防止影响命令行交互的显示
ETH & BTC 私链搭建

至此 ETH 私链搭建完成。

BTC 私有链搭建教程

在 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

ETH & BTC 私链搭建

启动成功

常见问题排查

由于众所周知的原因,在国内最常见的是网络问题。以下命令撤销原本的 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 然后打开,后续就可以从命令后调用了。

开发联系:DEXDAO

 

© 版权声明

Related posts

No comments

No comments...