1. 程式人生 > >以太坊(ETH)私有鏈搭建指南

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

前期準備

Git

sudo yum install -y git

Go

Geth

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://46de08fc64222512dd120f3876aa6d9947fd7e045e5a09bf95e90fcf3984d600d[email protected]127.0.0.1:30303?discport=0"

在節點2執行:

#注意多了bootnodes引數
geth --rpc --rpcport "8546" --datadir /data1/eth-test/edata --port "30304" --networkid 424 --bootnodes "enode://46de08fc64222512dd120f3876aa6d9947fd7e045e5a09bf95e90fcf3984d600d[email protected]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://46de08fc64222512dd120f3876aa6d9947fd7e045e5a09bf95e90fcf3984d600d[email protected]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

更多操作探索中。。。