「Blockchain」- Ethereum(以太坊)

  CREATED BY JENKINSBOT

部署环境

go ethereum – How to view latest block number of a geth node in the console? – Ethereum Stack Exchange
新鲜出炉的以太坊开发环境搭建及私链搭建指南💥💥💥 – 掘金
ethereum/go-ethereum: Official Go implementation of the Ethereum protocol
Beginners guide to Ethereum (2) — run multiple nodes on a private network in 5 lines
go ethereum – geth connectivity behind a firewall – Ethereum Stack Exchange

系统 Ubuntu18.04,网络 node-01: 172.17.15.155, node-02: 172.17.15.156,部署路径 /data/ethereum
Geth Version: 1.10.25-stable

node-01 && node-02

cat > genesis.json <<EOF
{
  "config": {
    "chainId": 0,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "berlinBlock": 0,
    "londonBlock": 0
  },
  "alloc": {},
  "coinbase": "0x0000000000000000000000000000000000000000",
  "difficulty": "0x20000",
  "extraData": "",
  "gasLimit": "0x2fefd8",
  "nonce": "0x0000000000000042",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp": "0x00"
}
EOF

geth --datadir data init genesis.json

geth --datadir "./data" console 2>>tail.log

node-02

admin.nodeInfo.enode

node-01

> admin.addPeer("enode://c70dxxxxxxxxxx40ba@<node-01 ip address>:30303")
true

> personal.newAccount()
Passphrase: [ENTER]
Repeat passphrase: [ENTER] 
"0xe9b2cc8372728da6067453cfa67f7aacbb0ba8dd"

> miner.setEtherbase(eth.accounts[0])
true

> miner.start()
null

> eth.blockNumber
2100

部署文档(旧版,废弃)

节点 1 搭建步骤

安装 geth 程序:

apt-get install -y software-properties-common

add-apt-repository -y ppa:ethereum/ethereum \
    && apt-get update && apt-get install ethereum

创建网络的创世状态

cat > genesis.json << EOF
{
   "config":{
	  "chainId":10,
	  "homesteadBlock":0,
	  "eip155Block":0,
	  "eip158Block":0,
	  "eip150Block":0,
	  "eip150Hash":"0x0000000000000000000000000000000000000000000000000000000000000000"
   },
   "alloc":{
	  "bd2d69e3e68e1ab3944a865b3e566ca5c48740da":{
		 "balance":"9000000000000000000000000000000"
	  }
   },
   "nonce":"0x0000000000000042",
   "difficulty":"0x080000",
   "mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000",
   "coinbase":"0x0000000000000000000000000000000000000000",
   "timestamp":"0x00",
   "parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
   "extraData":"0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
   "gasLimit":"0x4c4b40"
}
EOF

初始化创世块:

geth --datadir /data/ethereum/data --networkid 31415926                         \
    --rpc --rpcapi "eth,web3,miner,admin,personal,net"                         \
    --rpc --rpccorsdomain "*" --rpcaddr "0.0.0.0" --allow-insecure-unlock      \
    init ./genesis.json

导入创世块中的用户keystore

cd /data/eth/ethereum/keystore

cat > UTC--2021-03-18T06-41-06.161625000Z--bd2d69e3e68e1ab3944a865b3e566ca5c48740da << EOF
> {"address":"bd2d69e3e68e1ab3944a865b3e566ca5c48740da","crypto":{"cipher":"aes-128-ctr","ciphertext":"caf1b6673e7397f60e1051fcede6bb248d041a546156b3a358542c0fb30668be","cipherparams":{"iv":"de310935dc3d83ae74db842c3e6f8ffa"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"4e705e65cde0a9167b1021854b6d1d3c73933487f504aecea9234d1ee084b4a0"},"mac":"435fc1d22313248d66ae513f49c031ce27ce83b5ed2d11d432de8eb20199ee56"},"id":"acfa723b-c658-4672-ae29-d2c17125a998","version":3}
> EOF

启动

cd /data/ethnohup geth --rpc --rpccorsdomain "*" --datadir /data/eth/ethereum --port "30303" --rpcport "30000" --rpcaddr "0.0.0.0" --rpc --rpcapi "db,eth,web3,miner,admin,personal,net" --networkid 31415926 --allow-insecure-unlock &

进入控制台开启挖矿

geth attach http://127.0.0.1:8545
> miner.start(1) #起一个线程挖矿

节点2搭建步骤:

下载安装 geth 同节点1
创建网络的”创世”状态同节点1
导入创世块中的用户keystore 同节点1
启动(创世块要与节点1相同)
cd /data/eth
nohup geth –rpc –rpccorsdomain “*” –datadir /data/eth/ethereum –port “30303” –rpcport “30000” –rpcaddr “0.0.0.0” –rpc –rpcapi “db,eth,web3,miner,admin,personal,net” –networkid 31415926 –nodiscover –allow-insecure-unlock &
注意加上–nodiscover 代表该链条不希望被其他节点发现,networkid 要和节点1一样,如果都部署在同一机器上rpcport和port 不要冲突
5.进入节点2控制台
#端口为启动脚本的rpcport
geth attach http://127.0.0.1:30000 #进入控制台后查询node信息(会得到”enode://d4c1xxxxxxx3e30@127.0.0.1:30303?discport=0″等示例信息把IP换成可访问的IP或局域网IP)
> admin.nodeInfo.enode

将节点2与节点1互通
将步骤5中的信息拷贝再节点1的控制台执行如下
admin.addPeer(“enode://f6235b2de8256d4c132e30@192.168.10.119:30303?discport=0″)
#执行查看数量(成功为1)
net.peerCount

进入节点2控制台开启挖矿
#起一个线程挖矿miner.start(1)