以太坊(ETH)私有链搭建指南

前期准备

Git

sudo yum install -y git

Go

参考官档:https://golang.org/doc/install

Geth

参考官档:https://ethereum.github.io/go-ethereum/install/

git clone https://github.com/ethereum/go-ethereum

cd go-ethereum/

make geth

#测试是否安装正确
build/bin/geth version

搭建私有链

1. 准备创世区块配置文件

{
  "config": {
        "chainId": 424,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
  "alloc"      : {},
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x2",
  "extraData"  : "",
  "gasLimit"   : "0x2fefd8",
  "nonce"      : "0x0000000000000042",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}

2. 初始化:写入创世区块

目录结构:

eta-test
├── data
└── customGenesis.json

初始化:

cd etc-test
geth --datadir data init customGenesis.json

初始化成功:

INFO [09-27|02:11:00.035] Maximum peer count                       ETH=25 LES=0 total=25
INFO [09-27|02:11:00.036] Allocated cache and file handles         database=/data1/eth-test/edata/geth/chaindata cache=16 handles=16
INFO [09-27|02:11:00.037] Writing custom genesis block 
INFO [09-27|02:11:00.037] Persisted trie from memory database      nodes=0 size=0.00B time=4.352µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [09-27|02:11:00.038] Successfully wrote genesis state         database=chaindata                            hash=40c510…7253cf
INFO [09-27|02:11:00.038] Allocated cache and file handles         database=/data1/eth-test/edata/geth/lightchaindata cache=16 handles=16
INFO [09-27|02:11:00.039] Writing custom genesis block 
INFO [09-27|02:11:00.039] Persisted trie from memory database      nodes=0 size=0.00B time=3.124µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [09-27|02:11:00.039] Successfully wrote genesis state         database=lightchaindata                            hash=40c510…7253cf

成功后目录结构:

eta-test
├── data
│   ├── geth
│   │   └── chaindata
│   │       ├── 000002.log
│   │       ├── CURRENT
│   │       ├── LOCK
│   │       ├── LOG
│   │       └── MANIFEST-000003
│   └── keystore
└── customGenesis.json

3. 启动私有链节点

启动节点并进入交互式控制台

geth --rpc --rpcport "8545" --datadir /mnt/eth-test/data --port "30303" --networkid 424 console

4. 创建账户

前面只是搭建了私有链,并没有自己的账户,可以在js console中输入eth.accounts来验证

eth.accounts
[]

#创建账户,两种方式
personal.newAccount()
Passphrase: 
Repeat passphrase: 
"0x94a5e2b161d2897f6e917a1011d0fdb8cc665662"


personal.newAccount("123456")
"0xb085911023c62e7756124affbd20ddf295adcf63"

eth.accounts
["0xb085911023c62e7756124affbd20ddf295adcf63", "0x94a5e2b161d2897f6e917a1011d0fdb8cc665662"]

5. 查看账户余额

> eth.getBalance(eth.accounts[0])
0
> eth.getBalance(eth.accounts[1])
0

6.启动&停止挖矿

#开始挖矿,参数表示线程数
miner.start(1)

#停止挖矿
miner.stop()

#默认情况下coinbase是本地账户中的第一个账户
eth.coinbase
"0xb085911023c62e7756124affbd20ddf295adcf63"

#修改coinbase
miner.setEtherbase(eth.accounts[1])

#查看余额
> eth.getBalance(eth.accounts[0])
50000000000000000000
> eth.getBalance(eth.accounts[1])
0

7.发送交易

#解锁账户
> personal.unlockAccount(eth.accounts[0])
Unlock account 0xb085911023c62e7756124affbd20ddf295adcf63
Passphrase: 
true

#发送交易
> eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(10, "ether")})
INFO [09-27|10:02:47.375] Setting new local account                address=0xB085911023C62e7756124AffBD20ddf295adCF63
INFO [09-27|10:02:47.375] Submitted transaction                    fullhash=0x0e2adfa24216ce46123939fc91b17c361e22e0fda48cc62fab6cc5ef0e30bc97 recipient=0x94A5e2B161d2897F6E917A1011D0Fdb8cc665662
"0x0e2adfa24216ce46123939fc91b17c361e22e0fda48cc62fab6cc5ef0e30bc97"

#查看交易池中的状态,目前有一个交易未被确认
> txpool.status
{
  pending: 1,
  queued: 0
}

#开始挖矿
miner.start(1);admin.sleepBlocks(1);miner.stop();

#查看交易池中的状态,刚才的交易被确认
> txpool.status
{
  pending: 0,
  queued: 0
}

8.查看交易和区块

#查看区块数量
> eth.blockNumber
12


#查看具体区块
> eth.getBlock(11)
{
  difficulty: 131072,
  extraData: "0xd883010811846765746888676f312e31302e32856c696e7578",
  gasLimit: 3175485,
  gasUsed: 21000,
  hash: "0xb3fad2205908b610d0d3335f3e2b685f057e82834d287f45c4fa610a0fd45afd",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0xb085911023c62e7756124affbd20ddf295adcf63",
  mixHash: "0x5d5df773b9add8d958cff2ccc89dc8400893482a858ba4757ce1bd322223aa97",
  nonce: "0x0d940da244fb3795",
  number: 11,
  parentHash: "0x89826d39ad180b11e3b4049ba7aca228cdb4b53ebb2c9d443faa8aee04c60eed",
  receiptsRoot: "0xd9ed9d79657d80fe58b809cb9e5c132866413f6bc61c11a6968ed86121cf6a62",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 651,
  stateRoot: "0x7b072033ecbb8d35c0d4bf577176923919101a2d346e82c75ab4939ba0f1b1a4",
  timestamp: 1538013830,
  totalDifficulty: 1444674,
  transactions: ["0x0e2adfa24216ce46123939fc91b17c361e22e0fda48cc62fab6cc5ef0e30bc97"],
  transactionsRoot: "0xc2700ca799a4fed8a86f744207e582a5f08ba652b7165d6bfe24733d7e05555f",
  uncles: []
}

#查看交易
> eth.getTransaction("0x0e2adfa24216ce46123939fc91b17c361e22e0fda48cc62fab6cc5ef0e30bc97")
{
  blockHash: "0xb3fad2205908b610d0d3335f3e2b685f057e82834d287f45c4fa610a0fd45afd",
  blockNumber: 11,
  from: "0xb085911023c62e7756124affbd20ddf295adcf63",
  gas: 90000,
  gasPrice: 1000000000,
  hash: "0x0e2adfa24216ce46123939fc91b17c361e22e0fda48cc62fab6cc5ef0e30bc97",
  input: "0x",
  nonce: 0,
  r: "0xda178d983cf68327f7f4ddde5d174568d5b34953dcf94970742a88062b63def1",
  s: "0xa301a525e72e227967cadaae260bf43964c92b44803a3bcf5b96f67f06cad51",
  to: "0x94a5e2b161d2897f6e917a1011d0fdb8cc665662",
  transactionIndex: 0,
  v: "0x373",
  value: 10000000000000000000
}

9.连接到其他节点

在节点2上,重复步骤1和3,但是要注意rpcport和port参数要与节点1不同(最好数据文件夹也不同)。在节点1上执行

#127.0.0.1替换成节点1的IP
> admin.nodeInfo.enode
"enode://46de08fc64222512dd120f3876aa6d9947fd7e045e5a09bf95e90fcf3984d600dc25dc06cbb04e72b175bf23031ffb65adecf275dae229e69e3e32c67b5deec6@127.0.0.1:30303?discport=0"

在节点2执行:

#注意多了bootnodes参数
geth --rpc --rpcport "8546" --datadir /data1/eth-test/edata --port "30304" --networkid 424 --bootnodes "enode://46de08fc64222512dd120f3876aa6d9947fd7e045e5a09bf95e90fcf3984d600dc25dc06cbb04e72b175bf23031ffb65adecf275dae229e69e3e32c67b5deec6@127.0.0.1:30303" console
> INFO [09-27|02:15:44.075] Block synchronisation started 
INFO [09-27|02:15:44.078] Imported new state entries               count=3 elapsed=50.591µs processed=3 pending=0 retry=0 duplicate=0 unexpected=0
INFO [09-27|02:15:44.857] Imported new block headers               count=12 elapsed=777.982ms number=12 hash=9e2a63…c55e4e age=11m53s
INFO [09-27|02:15:44.860] Imported new chain segment               blocks=10 txs=0 mgas=0.000 elapsed=2.468ms   mgasps=0.000 number=10 hash=89826d…c60eed age=13h23m54s cache=2.13kB
INFO [09-27|02:15:44.861] Imported new chain segment               blocks=2  txs=1 mgas=0.021 elapsed=1.079ms   mgasps=19.461 number=12 hash=9e2a63…c55e4e age=11m53s    cache=3.12kB
INFO [09-27|02:15:44.861] Fast sync complete, auto disabling

#也可以不加bootnodes参数,启动后调用命令添加节点
admin.addPeer("enode://46de08fc64222512dd120f3876aa6d9947fd7e045e5a09bf95e90fcf3984d600dc25dc06cbb04e72b175bf23031ffb65adecf275dae229e69e3e32c67b5deec6@127.0.0.1:30303")

#查看节点
> admin.peers
[{
    caps: ["eth/62", "eth/63"],
    id: "e97aca78953b0432a81a0115d3735849b961769b99e9e46fda42061c38140a64",
    name: "Geth/v1.8.17-unstable-d3441ebb/linux-amd64/go1.10.2",
    network: {
      inbound: false,
      localAddress: "172.25.1.164:52528",
      remoteAddress: "172.25.1.72:30303",
      static: false,
      trusted: false
    },
    protocols: {
      eth: {
        difficulty: 1575810,
        head: "0x9e2a63793575d23c7b0ca15b1d0da746da835fdc2e67770a4f90ed0171c55e4e",
        version: 63
      }
    }
}]

#查看节点数
> net.peerCount
1

#查看刚才在节点1中的交易后的账户余额
> eth.getBalance("0xb085911023c62e7756124affbd20ddf295adcf63")
50000000000000000000
> eth.getBalance("0x94a5e2b161d2897f6e917a1011d0fdb8cc665662")
10000000000000000000

更多操作探索中。。。

猜你喜欢

转载自blog.csdn.net/feng12345zi/article/details/82863459